| 1 | import 'package:flutter/services.dart'; |
| 2 | import 'package:flutter_test/flutter_test.dart'; |
| 3 | import 'package:flutter_tts/flutter_tts.dart'; |
| 4 | import 'package:localmind/features/tts/data/system_tts_configuration.dart'; |
| 5 | |
| 6 | void main() { |
| 7 | TestWidgetsFlutterBinding.ensureInitialized(); |
| 8 | const channel = MethodChannel('flutter_tts'); |
| 9 | final calls = <MethodCall>[]; |
| 10 | String? engine; |
| 11 | bool fail = false; |
| 12 | |
| 13 | setUp(() { |
| 14 | calls.clear(); |
| 15 | engine = 'org.example.sherpatts'; |
| 16 | fail = false; |
| 17 | TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger |
| 18 | .setMockMethodCallHandler(channel, (call) async { |
| 19 | calls.add(call); |
| 20 | if (call.method == 'getDefaultEngine') return engine; |
| 21 | if (call.method == 'setEngine' && fail) { |
| 22 | throw PlatformException(code: 'TtsError'); |
| 23 | } |
| 24 | return 1; |
| 25 | }); |
| 26 | }); |
| 27 | tearDown(() { |
| 28 | TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger |
| 29 | .setMockMethodCallHandler(channel, null); |
| 30 | }); |
| 31 | |
| 32 | test( |
| 33 | 'uses selected third-party engine without forcing language or voice', |
| 34 | () async { |
| 35 | final config = SystemTtsConfiguration(); |
| 36 | final tts = FlutterTts(); |
| 37 | await config.prepare(tts, isAndroid: true); |
| 38 | await config.prepare(tts, isAndroid: true); |
| 39 | expect(calls.map((c) => c.method), [ |
| 40 | 'getDefaultEngine', |
| 41 | 'setEngine', |
| 42 | 'getDefaultEngine', |
| 43 | ]); |
| 44 | expect(calls[1].arguments, 'org.example.sherpatts'); |
| 45 | engine = 'org.example.other'; |
| 46 | await config.prepare(tts, isAndroid: true); |
| 47 | expect(calls.last.method, 'setEngine'); |
| 48 | expect(calls.last.arguments, engine); |
| 49 | }, |
| 50 | ); |
| 51 | |
| 52 | test('initialization failure is surfaced and retried next time', () async { |
| 53 | final config = SystemTtsConfiguration(); |
| 54 | final tts = FlutterTts(); |
| 55 | fail = true; |
| 56 | await expectLater( |
| 57 | config.prepare(tts, isAndroid: true), |
| 58 | throwsA(isA<PlatformException>()), |
| 59 | ); |
| 60 | fail = false; |
| 61 | await config.prepare(tts, isAndroid: true); |
| 62 | expect(calls.where((c) => c.method == 'setEngine'), hasLength(2)); |
| 63 | }); |
| 64 | |
| 65 | test( |
| 66 | 'does not send Android calls on other platforms or select an empty engine', |
| 67 | () async { |
| 68 | final config = SystemTtsConfiguration(); |
| 69 | final tts = FlutterTts(); |
| 70 | await config.prepare(tts, isAndroid: false); |
| 71 | expect(calls, isEmpty); |
| 72 | engine = null; |
| 73 | await config.prepare(tts, isAndroid: true); |
| 74 | expect(calls.map((c) => c.method), ['getDefaultEngine']); |
| 75 | }, |
| 76 | ); |
| 77 | } |