import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/ui/adaptive/segmented_bar.dart'; import 'package:pipelines_app/ui/adaptive/segmented_bar_pieces.dart'; import 'package:pipelines_app/ui/adaptive/window_controls.dart'; /// Where UIKit put iPadOS 26.4's window controls in a window on the /// simulator: safe area top 10, adapted left 66 and top 53. const _controls = Rect.fromLTRB(0, 0, 66, 53); void _window(WidgetTester tester, {double top = 10, double width = 800}) { tester.view.physicalSize = Size(width, 700); tester.view.devicePixelRatio = 1; tester.view.padding = FakeViewPadding(top: top); addTearDown(tester.view.reset); } SegmentedBar _bar({String title = 'Chat', List actions = const []}) => SegmentedBar(cupertino: false, title: title, actions: actions); Widget _page(SegmentedBar bar, {ValueListenable? controls}) { final page = Scaffold( extendBodyBehindAppBar: true, appBar: bar, body: const SizedBox.expand(), ); return MaterialApp( home: controls == null ? page : WindowControlsScope(rect: controls, child: page), ); } double _titleLeft(WidgetTester tester) => tester.getTopLeft(find.byType(SegmentedTitlePill)).dx; void main() { testWidgets('without window controls the title pill keeps its margin', ( tester, ) async { _window(tester); await tester.pumpWidget(_page(_bar(), controls: ValueNotifier(null))); await tester.pumpAndSettle(); expect(_titleLeft(tester), 12); }); testWidgets('the title pill starts clear of the window controls', ( tester, ) async { _window(tester); await tester.pumpWidget(_page(_bar(), controls: ValueNotifier(_controls))); await tester.pumpAndSettle(); expect(_titleLeft(tester), _controls.right + 8); // The pill keeps its height and row; only its leading edge moves. expect(tester.getTopLeft(find.byType(SegmentedTitlePill)).dy, 18); }); testWidgets('the clearance follows the controls as the window changes', ( tester, ) async { _window(tester); final controls = ValueNotifier(null); await tester.pumpWidget(_page(_bar(), controls: controls)); await tester.pumpAndSettle(); expect(_titleLeft(tester), 12); controls.value = _controls; await tester.pumpAndSettle(); expect(_titleLeft(tester), 74); // Back to full screen. controls.value = null; await tester.pumpAndSettle(); expect(_titleLeft(tester), 12); }); testWidgets('controls above the bar (below the status bar) leave it be', ( tester, ) async { _window(tester, top: 60); await tester.pumpWidget(_page(_bar(), controls: ValueNotifier(_controls))); await tester.pumpAndSettle(); expect(_titleLeft(tester), 12); }); testWidgets('a bar away from the controls, such as a detail pane\'s, keeps ' 'its margin', (tester) async { _window(tester); await tester.pumpWidget( MaterialApp( home: WindowControlsScope( rect: ValueNotifier(_controls), child: Row( children: [ const SizedBox(width: 300), Expanded( child: Scaffold( extendBodyBehindAppBar: true, appBar: _bar(title: 'Release notes'), body: const SizedBox.expand(), ), ), ], ), ), ), ); await tester.pumpAndSettle(); expect(_titleLeft(tester), 312); }); testWidgets('controls at the trailing corner keep the actions clear', ( tester, ) async { _window(tester); const trailing = Rect.fromLTRB(734, 0, 800, 53); await tester.pumpWidget( _page( _bar( actions: [IconButton(onPressed: () {}, icon: const Icon(Icons.add))], ), controls: ValueNotifier(trailing), ), ); await tester.pumpAndSettle(); expect(_titleLeft(tester), 12); expect( tester.getTopRight(find.byIcon(Icons.add)).dx, lessThanOrEqualTo(trailing.left - 8), ); }); testWidgets('a pushed page\'s Back clears the controls once it has slid in', ( tester, ) async { _window(tester); final navigator = GlobalKey(); await tester.pumpWidget( MaterialApp( navigatorKey: navigator, builder: (context, child) => WindowControlsScope(rect: ValueNotifier(_controls), child: child!), home: const SizedBox.expand(), ), ); navigator.currentState!.push( MaterialPageRoute( builder: (_) => Scaffold( extendBodyBehindAppBar: true, appBar: _bar(title: 'Release notes'), body: const SizedBox.expand(), ), ), ); await tester.pumpAndSettle(); expect(tester.getTopLeft(find.byType(BackButton)).dx, greaterThan(66)); }); group('platform rect', () { setUp(WindowControls.debugReset); tearDown(() { TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger .setMockMethodCallHandler(WindowControls.channel, null); WindowControls.debugReset(); }); testWidgets( 'comes from the iOS host and follows its updates', (tester) async { final messenger = TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger; messenger.setMockMethodCallHandler(WindowControls.channel, ( call, ) async { expect(call.method, 'start'); return {'left': 0, 'top': 0, 'right': 66, 'bottom': 53}; }); late ValueListenable rect; await tester.pumpWidget( Builder( builder: (context) { rect = WindowControls.of(context); return const SizedBox(); }, ), ); await tester.pump(); expect(rect.value, _controls); // The host reports the window going full screen. await messenger.handlePlatformMessage( WindowControls.channel.name, WindowControls.channel.codec.encodeMethodCall( const MethodCall('changed'), ), (_) {}, ); expect(rect.value, isNull); }, variant: TargetPlatformVariant.only(TargetPlatform.iOS), ); testWidgets( 'an iOS host without the plugin reports none', (tester) async { late ValueListenable rect; await tester.pumpWidget( Builder( builder: (context) { rect = WindowControls.of(context); return const SizedBox(); }, ), ); await tester.pump(); expect(rect.value, isNull); }, variant: TargetPlatformVariant.only(TargetPlatform.iOS), ); }); }