import 'dart:async'; import 'package:cutdex/app/editor_theme.dart'; import 'package:cutdex/features/preview/preview_fullscreen.dart'; import 'package:cutdex_ui/theme.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; void main() { testWidgets( 'disposal waits for native fullscreen entry before restoring the window', (tester) async { const channel = MethodChannel('cutdex_native/fullscreen'); final messenger = tester.binding.defaultBinaryMessenger; final entered = Completer(); final requests = []; messenger.setMockMethodCallHandler(channel, (call) async { final entering = call.arguments as bool; requests.add(entering); if (entering) await entered.future; if (!entering && !entered.isCompleted) { throw PlatformException(code: 'busy'); } return null; }); addTearDown(() => messenger.setMockMethodCallHandler(channel, null)); await tester.pumpWidget( const MaterialApp( home: Scaffold(body: PreviewFullscreenHost(child: _Toggle('enter'))), ), ); await tester.tap(find.text('enter')); await tester.pump(); await tester.pumpWidget(const SizedBox()); await tester.pump(); expect(requests, [true]); entered.complete(); await tester.pump(); expect(requests, [true, false]); }, variant: TargetPlatformVariant.only(TargetPlatform.macOS), ); testWidgets( 'native fullscreen exit restores the panel and disposal restores the window', (tester) async { const channel = MethodChannel('cutdex_native/fullscreen'); final messenger = TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger; final requests = []; messenger.setMockMethodCallHandler(channel, (call) async { requests.add(call.arguments as bool); return null; }); addTearDown(() => messenger.setMockMethodCallHandler(channel, null)); await tester.pumpWidget( MaterialApp( home: Scaffold( body: Center( child: SizedBox( width: 320, height: 200, child: PreviewFullscreenHost( child: _Preview(onInit: () {}, onDispose: () {}), ), ), ), ), ), ); await tester.tap(find.byType(TextButton)); await tester.pumpAndSettle(); expect(requests, [true]); messenger.handlePlatformMessage( channel.name, const StandardMethodCodec().encodeMethodCall( const MethodCall('exited'), ), (_) {}, ); await tester.pumpAndSettle(); expect(tester.getSize(find.byType(_Preview)), const Size(320, 200)); await tester.tap(find.byType(TextButton)); await tester.pumpAndSettle(); await tester.pumpWidget(const SizedBox()); await tester.pumpAndSettle(); expect(requests, [true, true, false]); }, variant: const TargetPlatformVariant({TargetPlatform.macOS}), ); testWidgets( 'fullscreen reparents the same preview and Escape restores its size', (tester) async { var initialized = 0; var disposed = 0; await tester.pumpWidget( MaterialApp( theme: EditorTheme.penLight, home: Scaffold( body: Center( child: SizedBox( width: 320, height: 200, child: PreviewFullscreenHost( child: _Preview( onInit: () => initialized++, onDispose: () => disposed++, ), ), ), ), ), ), ); final preview = find.byType(_Preview); final initialState = tester.state(preview); expect(tester.getSize(preview), const Size(320, 200)); expect(Theme.of(tester.element(preview)).brightness, Brightness.light); await tester.tap(find.byType(TextButton)); await tester.pumpAndSettle(); expect(tester.getSize(preview), const Size(800, 600)); expect(tester.state(preview), same(initialState)); final fullscreenTheme = Theme.of(tester.element(preview)); expect(fullscreenTheme.brightness, Brightness.dark); expect(fullscreenTheme.colorScheme.surface, Colors.black); expect( fullscreenTheme.extension()!.previewBackground, Colors.black, ); await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.pumpAndSettle(); expect(tester.getSize(preview), const Size(320, 200)); expect(Theme.of(tester.element(preview)).brightness, Brightness.light); expect(initialized, 1); expect(disposed, 0); await tester.tap(find.byType(TextButton)); await tester.pumpAndSettle(); await tester.tap(find.byType(TextButton)); await tester.pumpAndSettle(); expect(tester.getSize(preview), const Size(320, 200)); expect(disposed, 0); }, ); testWidgets( 'old fullscreen host disposal preserves the active exit listener', (tester) async { const channel = MethodChannel('cutdex_native/fullscreen'); final messenger = tester.binding.defaultBinaryMessenger; messenger.setMockMethodCallHandler(channel, (_) async => null); addTearDown(() => messenger.setMockMethodCallHandler(channel, null)); Widget page({required bool old, required bool next}) => MaterialApp( home: Scaffold( body: Row( children: [ if (old) const Expanded( key: ValueKey('old'), child: PreviewFullscreenHost(child: _Toggle('old')), ), if (next) const Expanded( key: ValueKey('new'), child: PreviewFullscreenHost(child: _Toggle('new')), ), ], ), ), ); await tester.pumpWidget(page(old: true, next: false)); await tester.tap(find.text('old')); await tester.pumpAndSettle(); await tester.tap(find.text('old')); await tester.pumpAndSettle(); await tester.pumpWidget(page(old: true, next: true)); await tester.pumpAndSettle(); await tester.tap(find.text('new')); await tester.pumpAndSettle(); await tester.pumpWidget(page(old: false, next: true)); await tester.pumpAndSettle(); await messenger.handlePlatformMessage( channel.name, const StandardMethodCodec().encodeMethodCall( const MethodCall('exited'), ), (_) {}, ); await tester.pumpAndSettle(); expect( PreviewFullscreenScope.of(tester.element(find.text('new')))!.fullscreen, isFalse, ); }, variant: TargetPlatformVariant.only(TargetPlatform.macOS), ); testWidgets( 'disposing a superseded fullscreen host does not exit the active window', (tester) async { const channel = MethodChannel('cutdex_native/fullscreen'); final messenger = tester.binding.defaultBinaryMessenger; final requests = []; messenger.setMockMethodCallHandler(channel, (call) async { requests.add(call.arguments as bool); return null; }); addTearDown(() => messenger.setMockMethodCallHandler(channel, null)); Widget page(bool old) => MaterialApp( home: Scaffold( body: Row( children: [ if (old) const Expanded( key: ValueKey('old'), child: PreviewFullscreenHost(child: _Toggle('old')), ), const Expanded( key: ValueKey('new'), child: PreviewFullscreenHost(child: _Toggle('new')), ), ], ), ), ); await tester.pumpWidget(page(true)); await tester.tap(find.text('old')); await tester.pumpAndSettle(); // A newly mounted route can take ownership while the old route is retiring. tester .widget(find.widgetWithText(TextButton, 'new')) .onPressed!(); await tester.pumpAndSettle(); await tester.pumpWidget(page(false)); await tester.pumpAndSettle(); expect(requests, [true, true]); await messenger.handlePlatformMessage( channel.name, const StandardMethodCodec().encodeMethodCall( const MethodCall('exited'), ), (_) {}, ); await tester.pumpAndSettle(); expect( PreviewFullscreenScope.of(tester.element(find.text('new')))!.fullscreen, isFalse, ); await tester.pumpWidget(const SizedBox()); await tester.pumpAndSettle(); }, variant: TargetPlatformVariant.only(TargetPlatform.macOS), ); } class _Preview extends StatefulWidget { const _Preview({required this.onInit, required this.onDispose}); final VoidCallback onInit; final VoidCallback onDispose; @override State<_Preview> createState() => _PreviewState(); } class _PreviewState extends State<_Preview> { @override void initState() { super.initState(); widget.onInit(); } @override void dispose() { widget.onDispose(); super.dispose(); } @override Widget build(BuildContext context) => Center( child: TextButton( onPressed: PreviewFullscreenScope.of(context)!.toggle, child: const Text('Fullscreen'), ), ); } class _Toggle extends StatelessWidget { const _Toggle(this.label); final String label; @override Widget build(BuildContext context) => Center( child: TextButton( onPressed: PreviewFullscreenScope.of(context)!.toggle, child: Text(label), ), ); }