From 301007fcd37904b9d481d35a8f6dfb078e5734ab Mon Sep 17 00:00:00 2001 From: gregor Date: Sat, 12 Jan 2019 14:06:18 +0100 Subject: [PATCH 01/12] made QuickActions testable --- packages/quick_actions/example/lib/main.dart | 2 +- packages/quick_actions/lib/quick_actions.dart | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/quick_actions/example/lib/main.dart b/packages/quick_actions/example/lib/main.dart index 3c0a75f971f6..7ca344dd0ab7 100644 --- a/packages/quick_actions/example/lib/main.dart +++ b/packages/quick_actions/example/lib/main.dart @@ -36,7 +36,7 @@ class _MyHomePageState extends State { @override void initState() { super.initState(); - final QuickActions quickActions = const QuickActions(); + final QuickActions quickActions = QuickActions(); quickActions.initialize((String shortcutType) { if (shortcutType == 'action_main') { print('The user tapped on the "Main view" action.'); diff --git a/packages/quick_actions/lib/quick_actions.dart b/packages/quick_actions/lib/quick_actions.dart index 7747495d37b8..a007aaae34e0 100644 --- a/packages/quick_actions/lib/quick_actions.dart +++ b/packages/quick_actions/lib/quick_actions.dart @@ -7,9 +7,6 @@ import 'dart:async'; import 'package:flutter/services.dart'; import 'package:meta/meta.dart'; -const MethodChannel _kChannel = - MethodChannel('plugins.flutter.io/quick_actions'); - /// Handler for a quick action launch event. /// /// The argument [type] corresponds to the [ShortcutItem]'s field. @@ -36,7 +33,16 @@ class ShortcutItem { /// Quick actions plugin. class QuickActions { - const QuickActions(); + factory QuickActions() => _instance; + + @visibleForTesting + QuickActions.private(MethodChannel channel) : _kChannel = channel; + + static final QuickActions _instance = QuickActions.private( + const MethodChannel('plugins.flutter.io/quick_actions'), + ); + + final MethodChannel _kChannel; /// Initializes this plugin. /// From af09a259f8d4a05ca0396c9fc36b7a7d2aa2dac5 Mon Sep 17 00:00:00 2001 From: gregor Date: Fri, 22 Mar 2019 17:01:02 +0100 Subject: [PATCH 02/12] update Changelog.md --- packages/quick_actions/CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/quick_actions/CHANGELOG.md b/packages/quick_actions/CHANGELOG.md index 7632ba7710f8..ed0a77565241 100644 --- a/packages/quick_actions/CHANGELOG.md +++ b/packages/quick_actions/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.2.3 + +* Added support for unit tests + ## 0.2.2 * Allow to register more than once. From 7431b96a2081472053468b41d50c17929b315542 Mon Sep 17 00:00:00 2001 From: gregor Date: Fri, 22 Mar 2019 17:04:09 +0100 Subject: [PATCH 03/12] update version --- packages/quick_actions/pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/quick_actions/pubspec.yaml b/packages/quick_actions/pubspec.yaml index cb4f1c8b5c74..20e97f22d0a8 100644 --- a/packages/quick_actions/pubspec.yaml +++ b/packages/quick_actions/pubspec.yaml @@ -3,7 +3,7 @@ description: Flutter plugin for creating shortcuts on home screen, also known as Quick Actions on iOS and App Shortcuts on Android. author: Flutter Team homepage: https://github.com/flutter/plugins/tree/master/packages/quick_actions -version: 0.2.2 +version: 0.2.3 flutter: plugin: From af388528d1098510326b27ff55e7dfa9f2a788d4 Mon Sep 17 00:00:00 2001 From: gregor Date: Fri, 22 Mar 2019 17:17:02 +0100 Subject: [PATCH 04/12] fixed flutter analyze --- packages/quick_actions/lib/quick_actions.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/quick_actions/lib/quick_actions.dart b/packages/quick_actions/lib/quick_actions.dart index a007aaae34e0..395db8d4a416 100644 --- a/packages/quick_actions/lib/quick_actions.dart +++ b/packages/quick_actions/lib/quick_actions.dart @@ -58,7 +58,7 @@ class QuickActions { Future setShortcutItems(List items) async { final List> itemsList = items.map(_serializeItem).toList(); - await _kChannel.invokeMethod('setShortcutItems', itemsList); + await _kChannel.invokeMethod('setShortcutItems', itemsList); } /// Removes all [ShortcutItem]s registered for the app. From c8cffba5a9e9c4efc0763957c3b45df192d29a5d Mon Sep 17 00:00:00 2001 From: gregor Date: Fri, 22 Mar 2019 17:41:00 +0100 Subject: [PATCH 05/12] added unit tests --- packages/quick_actions/pubspec.yaml | 6 ++ .../test/quick_actions_test.dart | 58 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 packages/quick_actions/test/quick_actions_test.dart diff --git a/packages/quick_actions/pubspec.yaml b/packages/quick_actions/pubspec.yaml index e6053bcf9c6e..6d7a18a43eb7 100644 --- a/packages/quick_actions/pubspec.yaml +++ b/packages/quick_actions/pubspec.yaml @@ -16,6 +16,12 @@ dependencies: sdk: flutter meta: ^1.0.5 +dev_dependencies: + test: ^1.3.0 + mockito: ^3.0.0 + flutter_test: + sdk: flutter + environment: sdk: ">=2.0.0-dev.28.0 <3.0.0" flutter: ">=0.1.4 <2.0.0" diff --git a/packages/quick_actions/test/quick_actions_test.dart b/packages/quick_actions/test/quick_actions_test.dart new file mode 100644 index 000000000000..281fbf47b23d --- /dev/null +++ b/packages/quick_actions/test/quick_actions_test.dart @@ -0,0 +1,58 @@ +// Copyright 2017 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +import 'package:flutter/services.dart'; +import 'package:mockito/mockito.dart'; +import 'package:quick_actions/quick_actions.dart'; +import 'package:test/test.dart'; + +void main() { + MockMethodChannel mockChannel; + QuickActions quickActions; + + setUp(() { + mockChannel = MockMethodChannel(); + quickActions = QuickActions.private( + mockChannel, + ); + }); + + test('setShortcutItems with demo data', () async { + const String type = 'type'; + const String localizedTitle = 'localizedTitle'; + const String icon = 'icon'; + await quickActions.setShortcutItems( + const [ + ShortcutItem(type: type, localizedTitle: localizedTitle, icon: icon) + ], + ); + // TODO(amirh): remove this on when the invokeMethod update makes it to stable Flutter. + // https://github.com/flutter/flutter/issues/26431 + // ignore: strong_mode_implicit_dynamic_method + verify(mockChannel.invokeMethod( + 'setShortcutItems', + >[ + { + 'type': type, + 'localizedTitle': localizedTitle, + 'icon': icon, + } + ], + )); + }); + + test('initialize', () { + quickActions.initialize((_) {}); + verify(mockChannel.setMethodCallHandler(any)); + }); + + test('clearShortcutItems', () { + quickActions.clearShortcutItems(); + // TODO(amirh): remove this on when the invokeMethod update makes it to stable Flutter. + // https://github.com/flutter/flutter/issues/26431 + // ignore: strong_mode_implicit_dynamic_method + verify(mockChannel.invokeMethod('clearShortcutItems')); + }); +} + +class MockMethodChannel extends Mock implements MethodChannel {} From 67f113cf4f355bd4e8fb8004a55fc7db9b2ecfc2 Mon Sep 17 00:00:00 2001 From: gregor Date: Tue, 11 Jun 2019 22:51:28 +0200 Subject: [PATCH 06/12] removed todos --- packages/quick_actions/test/quick_actions_test.dart | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/packages/quick_actions/test/quick_actions_test.dart b/packages/quick_actions/test/quick_actions_test.dart index 281fbf47b23d..f9d3506cbcf6 100644 --- a/packages/quick_actions/test/quick_actions_test.dart +++ b/packages/quick_actions/test/quick_actions_test.dart @@ -26,10 +26,7 @@ void main() { ShortcutItem(type: type, localizedTitle: localizedTitle, icon: icon) ], ); - // TODO(amirh): remove this on when the invokeMethod update makes it to stable Flutter. - // https://github.com/flutter/flutter/issues/26431 - // ignore: strong_mode_implicit_dynamic_method - verify(mockChannel.invokeMethod( + verify(mockChannel.invokeMethod( 'setShortcutItems', >[ { @@ -48,10 +45,7 @@ void main() { test('clearShortcutItems', () { quickActions.clearShortcutItems(); - // TODO(amirh): remove this on when the invokeMethod update makes it to stable Flutter. - // https://github.com/flutter/flutter/issues/26431 - // ignore: strong_mode_implicit_dynamic_method - verify(mockChannel.invokeMethod('clearShortcutItems')); + verify(mockChannel.invokeMethod('clearShortcutItems')); }); } From 15549b97b37571a744feaa86b4c94992fb4eb529 Mon Sep 17 00:00:00 2001 From: gregor Date: Tue, 11 Jun 2019 22:52:03 +0200 Subject: [PATCH 07/12] added const MethodChannel --- packages/quick_actions/lib/quick_actions.dart | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/packages/quick_actions/lib/quick_actions.dart b/packages/quick_actions/lib/quick_actions.dart index 01e834e2e396..4442342852b0 100644 --- a/packages/quick_actions/lib/quick_actions.dart +++ b/packages/quick_actions/lib/quick_actions.dart @@ -7,6 +7,9 @@ import 'dart:async'; import 'package:flutter/services.dart'; import 'package:meta/meta.dart'; +const MethodChannel _kChannel = + MethodChannel('plugins.flutter.io/quick_actions'); + /// Handler for a quick action launch event. /// /// The argument [type] corresponds to the [ShortcutItem]'s field. @@ -36,19 +39,17 @@ class QuickActions { factory QuickActions() => _instance; @visibleForTesting - QuickActions.private(MethodChannel channel) : _kChannel = channel; + QuickActions.private(MethodChannel channel) : _channel = channel; - static final QuickActions _instance = QuickActions.private( - const MethodChannel('plugins.flutter.io/quick_actions'), - ); + static final QuickActions _instance = QuickActions.private(_kChannel); - final MethodChannel _kChannel; + final MethodChannel _channel; /// Initializes this plugin. /// /// Call this once before any further interaction with the the plugin. void initialize(QuickActionHandler handler) { - _kChannel.setMethodCallHandler((MethodCall call) async { + _channel.setMethodCallHandler((MethodCall call) async { assert(call.method == 'launch'); handler(call.arguments); }); @@ -58,12 +59,12 @@ class QuickActions { Future setShortcutItems(List items) async { final List> itemsList = items.map(_serializeItem).toList(); - await _kChannel.invokeMethod('setShortcutItems', itemsList); + await _channel.invokeMethod('setShortcutItems', itemsList); } /// Removes all [ShortcutItem]s registered for the app. Future clearShortcutItems() => - _kChannel.invokeMethod('clearShortcutItems'); + _channel.invokeMethod('clearShortcutItems'); Map _serializeItem(ShortcutItem item) { return { From 9d0b8797daf4b8921253bde4fbe169e7fff8542e Mon Sep 17 00:00:00 2001 From: gregor Date: Tue, 11 Jun 2019 22:53:27 +0200 Subject: [PATCH 08/12] renamed constructor --- packages/quick_actions/lib/quick_actions.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/quick_actions/lib/quick_actions.dart b/packages/quick_actions/lib/quick_actions.dart index 4442342852b0..40f8182224e1 100644 --- a/packages/quick_actions/lib/quick_actions.dart +++ b/packages/quick_actions/lib/quick_actions.dart @@ -39,9 +39,9 @@ class QuickActions { factory QuickActions() => _instance; @visibleForTesting - QuickActions.private(MethodChannel channel) : _channel = channel; + QuickActions.withMethodChannel(MethodChannel channel) : _channel = channel; - static final QuickActions _instance = QuickActions.private(_kChannel); + static final QuickActions _instance = QuickActions.withMethodChannel(_kChannel); final MethodChannel _channel; From dea9d2773f8dfb7d83359709a2ee525f4e99bcfa Mon Sep 17 00:00:00 2001 From: gregor Date: Tue, 11 Jun 2019 22:54:07 +0200 Subject: [PATCH 09/12] renamed constructor --- packages/quick_actions/test/quick_actions_test.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/quick_actions/test/quick_actions_test.dart b/packages/quick_actions/test/quick_actions_test.dart index f9d3506cbcf6..ecb42625ba1a 100644 --- a/packages/quick_actions/test/quick_actions_test.dart +++ b/packages/quick_actions/test/quick_actions_test.dart @@ -12,7 +12,7 @@ void main() { setUp(() { mockChannel = MockMethodChannel(); - quickActions = QuickActions.private( + quickActions = QuickActions.withMethodChannel( mockChannel, ); }); From d4b775f78e78be3815d2b3a0e52a4ae90387671b Mon Sep 17 00:00:00 2001 From: gregor Date: Tue, 11 Jun 2019 23:01:42 +0200 Subject: [PATCH 10/12] flutter format --- packages/quick_actions/lib/quick_actions.dart | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/quick_actions/lib/quick_actions.dart b/packages/quick_actions/lib/quick_actions.dart index 40f8182224e1..d3a3835626e4 100644 --- a/packages/quick_actions/lib/quick_actions.dart +++ b/packages/quick_actions/lib/quick_actions.dart @@ -41,7 +41,8 @@ class QuickActions { @visibleForTesting QuickActions.withMethodChannel(MethodChannel channel) : _channel = channel; - static final QuickActions _instance = QuickActions.withMethodChannel(_kChannel); + static final QuickActions _instance = + QuickActions.withMethodChannel(_kChannel); final MethodChannel _channel; From f45a02d49dec07cb13ef0a3fbf3a3cd3f32168b4 Mon Sep 17 00:00:00 2001 From: gregor Date: Wed, 12 Jun 2019 00:01:12 +0200 Subject: [PATCH 11/12] added tests with setMockMethodCallHandler --- packages/quick_actions/lib/quick_actions.dart | 10 ++-- .../test/quick_actions_test.dart | 52 +++++++++++-------- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/packages/quick_actions/lib/quick_actions.dart b/packages/quick_actions/lib/quick_actions.dart index d3a3835626e4..7b60df586319 100644 --- a/packages/quick_actions/lib/quick_actions.dart +++ b/packages/quick_actions/lib/quick_actions.dart @@ -39,18 +39,18 @@ class QuickActions { factory QuickActions() => _instance; @visibleForTesting - QuickActions.withMethodChannel(MethodChannel channel) : _channel = channel; + QuickActions.withMethodChannel(this.channel); static final QuickActions _instance = QuickActions.withMethodChannel(_kChannel); - final MethodChannel _channel; + final MethodChannel channel; /// Initializes this plugin. /// /// Call this once before any further interaction with the the plugin. void initialize(QuickActionHandler handler) { - _channel.setMethodCallHandler((MethodCall call) async { + channel.setMethodCallHandler((MethodCall call) async { assert(call.method == 'launch'); handler(call.arguments); }); @@ -60,12 +60,12 @@ class QuickActions { Future setShortcutItems(List items) async { final List> itemsList = items.map(_serializeItem).toList(); - await _channel.invokeMethod('setShortcutItems', itemsList); + await channel.invokeMethod('setShortcutItems', itemsList); } /// Removes all [ShortcutItem]s registered for the app. Future clearShortcutItems() => - _channel.invokeMethod('clearShortcutItems'); + channel.invokeMethod('clearShortcutItems'); Map _serializeItem(ShortcutItem item) { return { diff --git a/packages/quick_actions/test/quick_actions_test.dart b/packages/quick_actions/test/quick_actions_test.dart index ecb42625ba1a..91f500af7fe8 100644 --- a/packages/quick_actions/test/quick_actions_test.dart +++ b/packages/quick_actions/test/quick_actions_test.dart @@ -2,18 +2,20 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. import 'package:flutter/services.dart'; -import 'package:mockito/mockito.dart'; +import 'package:flutter_test/flutter_test.dart'; import 'package:quick_actions/quick_actions.dart'; -import 'package:test/test.dart'; void main() { - MockMethodChannel mockChannel; QuickActions quickActions; + final List log = []; setUp(() { - mockChannel = MockMethodChannel(); - quickActions = QuickActions.withMethodChannel( - mockChannel, + quickActions = QuickActions(); + quickActions.channel.setMockMethodCallHandler( + (MethodCall methodCall) async { + log.add(methodCall); + return null; + }, ); }); @@ -26,27 +28,31 @@ void main() { ShortcutItem(type: type, localizedTitle: localizedTitle, icon: icon) ], ); - verify(mockChannel.invokeMethod( - 'setShortcutItems', - >[ - { - 'type': type, - 'localizedTitle': localizedTitle, - 'icon': icon, - } + expect( + log, + [ + isMethodCall( + 'setShortcutItems', + arguments: >[ + { + 'type': type, + 'localizedTitle': localizedTitle, + 'icon': icon, + } + ], + ), ], - )); - }); - - test('initialize', () { - quickActions.initialize((_) {}); - verify(mockChannel.setMethodCallHandler(any)); + ); + log.clear(); }); test('clearShortcutItems', () { quickActions.clearShortcutItems(); - verify(mockChannel.invokeMethod('clearShortcutItems')); + expect( + log, + [ + isMethodCall('clearShortcutItems', arguments: null), + ], + ); }); } - -class MockMethodChannel extends Mock implements MethodChannel {} From bbcc13775f5eec71258e47ee94f1b8a3619dde31 Mon Sep 17 00:00:00 2001 From: Collin Jackson Date: Thu, 20 Jun 2019 10:47:48 -0700 Subject: [PATCH 12/12] Update CHANGELOG.md --- packages/quick_actions/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/quick_actions/CHANGELOG.md b/packages/quick_actions/CHANGELOG.md index 1fb3882c7138..328199d6c5b8 100644 --- a/packages/quick_actions/CHANGELOG.md +++ b/packages/quick_actions/CHANGELOG.md @@ -1,6 +1,6 @@ ## 0.3.1 -* Added support for unit tests +* Added unit tests. ## 0.3.0+2