|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:flutter/services.dart'; |
| 6 | +import 'package:flutter_test/flutter_test.dart'; |
| 7 | + |
| 8 | +void main() { |
| 9 | + TestWidgetsFlutterBinding.ensureInitialized(); |
| 10 | + |
| 11 | + test('ProcessTextService.queryTextActions emits correct method call', () async { |
| 12 | + final List<MethodCall> log = <MethodCall>[]; |
| 13 | + |
| 14 | + TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler(SystemChannels.processText, (MethodCall methodCall) async { |
| 15 | + log.add(methodCall); |
| 16 | + return null; |
| 17 | + }); |
| 18 | + |
| 19 | + final ProcessTextService processTextService = DefaultProcessTextService(); |
| 20 | + await processTextService.queryTextActions(); |
| 21 | + |
| 22 | + expect(log, hasLength(1)); |
| 23 | + expect(log.single, isMethodCall('ProcessText.queryTextActions', arguments: null)); |
| 24 | + }); |
| 25 | + |
| 26 | + test('ProcessTextService.processTextAction emits correct method call', () async { |
| 27 | + final List<MethodCall> log = <MethodCall>[]; |
| 28 | + |
| 29 | + TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler(SystemChannels.processText, (MethodCall methodCall) async { |
| 30 | + log.add(methodCall); |
| 31 | + return null; |
| 32 | + }); |
| 33 | + |
| 34 | + final ProcessTextService processTextService = DefaultProcessTextService(); |
| 35 | + const String fakeActionId = 'fakeActivity.fakeAction'; |
| 36 | + const String textToProcess = 'Flutter'; |
| 37 | + await processTextService.processTextAction(fakeActionId, textToProcess, false); |
| 38 | + |
| 39 | + expect(log, hasLength(1)); |
| 40 | + expect(log.single, isMethodCall('ProcessText.processTextAction', arguments: <Object>[fakeActionId, textToProcess, false])); |
| 41 | + }); |
| 42 | + |
| 43 | + test('ProcessTextService handles engine answers over the channel', () async { |
| 44 | + const String action1Id = 'fakeActivity.fakeAction1'; |
| 45 | + const String action2Id = 'fakeActivity.fakeAction2'; |
| 46 | + |
| 47 | + // Fake channel that simulates responses returned from the engine. |
| 48 | + final MethodChannel fakeChannel = FakeProcessTextChannel((MethodCall call) async { |
| 49 | + if (call.method == 'ProcessText.queryTextActions') { |
| 50 | + return <String, String>{ |
| 51 | + action1Id: 'Action1', |
| 52 | + action2Id: 'Action2', |
| 53 | + }; |
| 54 | + } |
| 55 | + if (call.method == 'ProcessText.processTextAction') { |
| 56 | + final List<dynamic> args = call.arguments as List<dynamic>; |
| 57 | + final String actionId = args[0] as String; |
| 58 | + final String testToProcess = args[1] as String; |
| 59 | + if (actionId == action1Id) { |
| 60 | + // Simulates an action that returns a transformed text. |
| 61 | + return '$testToProcess!!!'; |
| 62 | + } |
| 63 | + // Simulates an action that failed or does not transform text. |
| 64 | + return null; |
| 65 | + } |
| 66 | + }); |
| 67 | + |
| 68 | + final DefaultProcessTextService processTextService = DefaultProcessTextService(); |
| 69 | + processTextService.setChannel(fakeChannel); |
| 70 | + |
| 71 | + final List<ProcessTextAction> actions = await processTextService.queryTextActions(); |
| 72 | + expect(actions, hasLength(2)); |
| 73 | + |
| 74 | + const String textToProcess = 'Flutter'; |
| 75 | + String? processedText; |
| 76 | + |
| 77 | + processedText = await processTextService.processTextAction(action1Id, textToProcess, false); |
| 78 | + expect(processedText, 'Flutter!!!'); |
| 79 | + |
| 80 | + processedText = await processTextService.processTextAction(action2Id, textToProcess, false); |
| 81 | + expect(processedText, null); |
| 82 | + }); |
| 83 | +} |
| 84 | + |
| 85 | +class FakeProcessTextChannel implements MethodChannel { |
| 86 | + FakeProcessTextChannel(this.outgoing); |
| 87 | + |
| 88 | + Future<dynamic> Function(MethodCall) outgoing; |
| 89 | + Future<void> Function(MethodCall)? incoming; |
| 90 | + |
| 91 | + List<MethodCall> outgoingCalls = <MethodCall>[]; |
| 92 | + |
| 93 | + @override |
| 94 | + BinaryMessenger get binaryMessenger => throw UnimplementedError(); |
| 95 | + |
| 96 | + @override |
| 97 | + MethodCodec get codec => const StandardMethodCodec(); |
| 98 | + |
| 99 | + @override |
| 100 | + Future<List<T>> invokeListMethod<T>(String method, [dynamic arguments]) => throw UnimplementedError(); |
| 101 | + |
| 102 | + @override |
| 103 | + Future<Map<K, V>> invokeMapMethod<K, V>(String method, [dynamic arguments]) => throw UnimplementedError(); |
| 104 | + |
| 105 | + @override |
| 106 | + Future<T> invokeMethod<T>(String method, [dynamic arguments]) async { |
| 107 | + final MethodCall call = MethodCall(method, arguments); |
| 108 | + outgoingCalls.add(call); |
| 109 | + return await outgoing(call) as T; |
| 110 | + } |
| 111 | + |
| 112 | + @override |
| 113 | + String get name => 'flutter/processtext'; |
| 114 | + |
| 115 | + @override |
| 116 | + void setMethodCallHandler(Future<void> Function(MethodCall call)? handler) => incoming = handler; |
| 117 | +} |
0 commit comments