|
| 1 | +import 'dart:async' show Completer; |
| 2 | + |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | +import 'package:flutter_riverpod/flutter_riverpod.dart'; |
| 5 | +import 'package:flutter_test/flutter_test.dart'; |
| 6 | +import 'package:shared_preferences/shared_preferences.dart'; |
| 7 | +import 'package:kalinka/data_model/data_model.dart'; |
| 8 | +import 'package:kalinka/data_model/playqueue_events.dart'; |
| 9 | +import 'package:kalinka/providers/app_state_provider.dart'; |
| 10 | +import 'package:kalinka/providers/connection_settings_provider.dart'; |
| 11 | +import 'package:kalinka/providers/connection_state_provider.dart'; |
| 12 | +import 'package:kalinka/providers/kalinka_player_api_provider.dart'; |
| 13 | +import 'package:kalinka/providers/onboarding_provider.dart'; |
| 14 | +import 'package:kalinka/providers/playback_time_provider.dart'; |
| 15 | +import 'package:kalinka/providers/source_modules_provider.dart'; |
| 16 | +import 'package:kalinka/providers/url_resolver.dart'; |
| 17 | +import 'package:kalinka/providers/websocket_provider.dart'; |
| 18 | +import 'package:kalinka/screens/music_player_screen.dart'; |
| 19 | +import 'package:kalinka/widgets/clear_all_confirm_dialog.dart'; |
| 20 | +import 'package:web_socket_channel/web_socket_channel.dart'; |
| 21 | + |
| 22 | +// Full-screen repro for the clear-all crash with semantics enabled (TalkBack): |
| 23 | +// 'identical(childRenderObject, parentRenderObject)' assertion in |
| 24 | +// _SemanticsGeometry.computeChildGeometry during flushSemantics. |
| 25 | + |
| 26 | +class _SettableQueueNotifier extends PlayQueueStateStore { |
| 27 | + _SettableQueueNotifier(this._initial); |
| 28 | + final PlayQueueState _initial; |
| 29 | + @override |
| 30 | + PlayQueueState build() => _initial; |
| 31 | + void set(PlayQueueState s) => state = s; |
| 32 | +} |
| 33 | + |
| 34 | +class _FakeConnectionNotifier extends ConnectionStateNotifier { |
| 35 | + @override |
| 36 | + ConnectionStatus build() => ConnectionStatus.connected; |
| 37 | +} |
| 38 | + |
| 39 | +class _FakePlaybackTimeNotifier extends PlaybackTimeMsNotifier { |
| 40 | + @override |
| 41 | + int build() => 0; |
| 42 | +} |
| 43 | + |
| 44 | +class _FakeApi implements KalinkaPlayerProxy { |
| 45 | + _FakeApi(this.onClear); |
| 46 | + final void Function() onClear; |
| 47 | + |
| 48 | + @override |
| 49 | + Future<void> clear() async => onClear(); |
| 50 | + |
| 51 | + @override |
| 52 | + dynamic noSuchMethod(Invocation invocation) => |
| 53 | + throw UnimplementedError('${invocation.memberName}'); |
| 54 | +} |
| 55 | + |
| 56 | +Track _track(String id) => Track( |
| 57 | + id: id, |
| 58 | + title: 'Track $id', |
| 59 | + duration: 200000, |
| 60 | + performer: Artist(id: 'a', name: 'Someone'), |
| 61 | +); |
| 62 | + |
| 63 | +PlayQueueState _queueWithTracks() => PlayQueueState( |
| 64 | + playbackState: PlaybackState(state: PlayerStateType.playing, index: 2), |
| 65 | + trackList: [_track('a'), _track('b'), _track('c'), _track('d'), _track('e')], |
| 66 | + playbackMode: PlaybackMode.empty, |
| 67 | + seq: 0, |
| 68 | +); |
| 69 | + |
| 70 | +PlayQueueState _emptyQueue() => PlayQueueState( |
| 71 | + playbackState: PlaybackState(state: PlayerStateType.stopped, index: null), |
| 72 | + trackList: const [], |
| 73 | + playbackMode: PlaybackMode.empty, |
| 74 | + seq: 1, |
| 75 | +); |
| 76 | + |
| 77 | +void main() { |
| 78 | + testWidgets('clear all from the tray with semantics enabled does not throw', ( |
| 79 | + tester, |
| 80 | + ) async { |
| 81 | + // Tablet/desktop layout (>= 900px wide) — matches the Linux desktop |
| 82 | + // window where the crash was reported. |
| 83 | + tester.view.physicalSize = const Size(1280, 720); |
| 84 | + tester.view.devicePixelRatio = 1.0; |
| 85 | + addTearDown(tester.view.reset); |
| 86 | + final semantics = tester.ensureSemantics(); |
| 87 | + SharedPreferences.setMockInitialValues({ |
| 88 | + 'Kalinka.host': 'localhost', |
| 89 | + 'Kalinka.port': 8080, |
| 90 | + 'Kalinka.name': 'Test', |
| 91 | + OnboardingStatusNotifier.sharedPrefOobeComplete: true, |
| 92 | + OnboardingStatusNotifier.sharedPrefCoachMarksShown: true, |
| 93 | + }); |
| 94 | + final prefs = await SharedPreferences.getInstance(); |
| 95 | + final queueNotifier = _SettableQueueNotifier(_queueWithTracks()); |
| 96 | + final api = _FakeApi(() => queueNotifier.set(_emptyQueue())); |
| 97 | + |
| 98 | + await tester.pumpWidget( |
| 99 | + ProviderScope( |
| 100 | + overrides: [ |
| 101 | + sharedPrefsProvider.overrideWithValue(prefs), |
| 102 | + kalinkaProxyProvider.overrideWithValue(api), |
| 103 | + playQueueStateStoreProvider.overrideWith(() => queueNotifier), |
| 104 | + connectionStateProvider.overrideWith(() => _FakeConnectionNotifier()), |
| 105 | + playbackTimeMsProvider.overrideWith( |
| 106 | + () => _FakePlaybackTimeNotifier(), |
| 107 | + ), |
| 108 | + sourceModulesProvider.overrideWith((ref) => <ModuleInfo>[]), |
| 109 | + // The real family dials a socket and arms a 5s connect timeout that |
| 110 | + // outlives the test; a never-completing future keeps it inert. |
| 111 | + webSocketProvider.overrideWith( |
| 112 | + (ref, path) => Completer<WebSocketChannel>().future, |
| 113 | + ), |
| 114 | + sourceCountProvider.overrideWithValue(1), |
| 115 | + urlResolverProvider.overrideWithValue(UrlResolver('')), |
| 116 | + ], |
| 117 | + child: const MaterialApp(home: MusicPlayerScreen()), |
| 118 | + ), |
| 119 | + ); |
| 120 | + await tester.pumpAndSettle(); |
| 121 | + |
| 122 | + // Open the queue management tray. |
| 123 | + await tester.tap(find.byIcon(Icons.more_vert).first); |
| 124 | + await tester.pumpAndSettle(); |
| 125 | + expect(find.text('Clear all'), findsOneWidget); |
| 126 | + |
| 127 | + // Pick "Clear all" → tray closes, 160ms delay, confirm dialog opens. |
| 128 | + await tester.tap(find.text('Clear all')); |
| 129 | + await tester.pumpAndSettle(); |
| 130 | + await tester.pump(const Duration(milliseconds: 200)); |
| 131 | + await tester.pumpAndSettle(); |
| 132 | + expect(find.text('Clear entire queue?'), findsOneWidget); |
| 133 | + |
| 134 | + // Confirm — the queue empties behind the dialog as it pops, which is the |
| 135 | + // frame that used to assert. |
| 136 | + await tester.tap( |
| 137 | + find.descendant( |
| 138 | + of: find.byType(ClearAllConfirmDialog), |
| 139 | + matching: find.text('Clear all'), |
| 140 | + ), |
| 141 | + ); |
| 142 | + await tester.pump(); |
| 143 | + expect(tester.takeException(), isNull); |
| 144 | + await tester.pump(const Duration(milliseconds: 50)); |
| 145 | + expect(tester.takeException(), isNull); |
| 146 | + await tester.pumpAndSettle(const Duration(milliseconds: 100)); |
| 147 | + expect(tester.takeException(), isNull); |
| 148 | + |
| 149 | + expect(find.text('Clear entire queue?'), findsNothing); |
| 150 | + expect(find.text('Nothing Queued'), findsOneWidget); |
| 151 | + |
| 152 | + // Let the toast expire so no timers outlive the test. |
| 153 | + await tester.pump(const Duration(seconds: 10)); |
| 154 | + await tester.pumpAndSettle(); |
| 155 | + |
| 156 | + semantics.dispose(); |
| 157 | + }); |
| 158 | +} |
0 commit comments