Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
4 changes: 4 additions & 0 deletions packages/quick_actions/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.3.1

* Added support for 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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,21 @@ class ShortcutItem {

/// Quick actions plugin.
class QuickActions {
const QuickActions();
factory QuickActions() => _instance;

@visibleForTesting
QuickActions.withMethodChannel(MethodChannel channel) : _channel = 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
Original file line number Diff line number Diff line change
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"
52 changes: 52 additions & 0 deletions packages/quick_actions/test/quick_actions_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// 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.withMethodChannel(
mockChannel,
);
});

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)
],
);
verify(mockChannel.invokeMethod<void>(
'setShortcutItems',
<Map<String, String>>[
<String, String>{
'type': type,
'localizedTitle': localizedTitle,
'icon': icon,
}
],
));
});

test('initialize', () {
quickActions.initialize((_) {});
verify(mockChannel.setMethodCallHandler(any));
});

test('clearShortcutItems', () {
quickActions.clearShortcutItems();
verify(mockChannel.invokeMethod<void>('clearShortcutItems'));
});
}

class MockMethodChannel extends Mock implements MethodChannel {}