Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
[quick_actions]make QuickActions testable (#1067)
Browse files Browse the repository at this point in the history
  • Loading branch information
The-Redhat authored and collinjackson committed Jul 10, 2019
1 parent d4cd9ec commit a1867b7
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 6 deletions.
4 changes: 4 additions & 0 deletions 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.
Expand Down
2 changes: 1 addition & 1 deletion packages/quick_actions/example/lib/main.dart
Expand Up @@ -36,7 +36,7 @@ class _MyHomePageState extends State<MyHomePage> {
@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.');
Expand Down
16 changes: 12 additions & 4 deletions packages/quick_actions/lib/quick_actions.dart
Expand Up @@ -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);
});
Expand All @@ -52,12 +60,12 @@ class QuickActions {
Future<void> setShortcutItems(List<ShortcutItem> items) async {
final List<Map<String, String>> itemsList =
items.map(_serializeItem).toList();
await _kChannel.invokeMethod<void>('setShortcutItems', itemsList);
await channel.invokeMethod<void>('setShortcutItems', itemsList);
}

/// Removes all [ShortcutItem]s registered for the app.
Future<void> clearShortcutItems() =>
_kChannel.invokeMethod<void>('clearShortcutItems');
channel.invokeMethod<void>('clearShortcutItems');

Map<String, String> _serializeItem(ShortcutItem item) {
return <String, String>{
Expand Down
8 changes: 7 additions & 1 deletion packages/quick_actions/pubspec.yaml
Expand Up @@ -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 <flutter-dev@googlegroups.com>
homepage: https://github.com/flutter/plugins/tree/master/packages/quick_actions
version: 0.3.0+2
version: 0.3.1

flutter:
plugin:
Expand All @@ -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"
58 changes: 58 additions & 0 deletions 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<MethodCall> log = <MethodCall>[];

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>[
ShortcutItem(type: type, localizedTitle: localizedTitle, icon: icon)
],
);
expect(
log,
<Matcher>[
isMethodCall(
'setShortcutItems',
arguments: <Map<String, String>>[
<String, String>{
'type': type,
'localizedTitle': localizedTitle,
'icon': icon,
}
],
),
],
);
log.clear();
});

test('clearShortcutItems', () {
quickActions.clearShortcutItems();
expect(
log,
<Matcher>[
isMethodCall('clearShortcutItems', arguments: null),
],
);
});
}

0 comments on commit a1867b7

Please sign in to comment.