diff --git a/packages/quick_actions/CHANGELOG.md b/packages/quick_actions/CHANGELOG.md index 61fb69e28c5b..328199d6c5b8 100644 --- a/packages/quick_actions/CHANGELOG.md +++ b/packages/quick_actions/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.3.1 + +* Added unit tests. + ## 0.3.0+2 * Add missing template type parameter to `invokeMethod` calls. 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 c4292b5f6fdd..7b60df586319 100644 --- a/packages/quick_actions/lib/quick_actions.dart +++ b/packages/quick_actions/lib/quick_actions.dart @@ -36,13 +36,21 @@ class ShortcutItem { /// Quick actions plugin. class QuickActions { - const QuickActions(); + factory QuickActions() => _instance; + + @visibleForTesting + QuickActions.withMethodChannel(this.channel); + + static final QuickActions _instance = + QuickActions.withMethodChannel(_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); }); @@ -52,12 +60,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 { diff --git a/packages/quick_actions/pubspec.yaml b/packages/quick_actions/pubspec.yaml index e324ae087c64..3887c597c837 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.3.0+2 +version: 0.3.1 flutter: plugin: @@ -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: ">=1.5.0 <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..91f500af7fe8 --- /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:flutter_test/flutter_test.dart'; +import 'package:quick_actions/quick_actions.dart'; + +void main() { + QuickActions quickActions; + final List log = []; + + setUp(() { + quickActions = QuickActions(); + quickActions.channel.setMockMethodCallHandler( + (MethodCall methodCall) async { + log.add(methodCall); + return null; + }, + ); + }); + + 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) + ], + ); + expect( + log, + [ + isMethodCall( + 'setShortcutItems', + arguments: >[ + { + 'type': type, + 'localizedTitle': localizedTitle, + 'icon': icon, + } + ], + ), + ], + ); + log.clear(); + }); + + test('clearShortcutItems', () { + quickActions.clearShortcutItems(); + expect( + log, + [ + isMethodCall('clearShortcutItems', arguments: null), + ], + ); + }); +}