import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/servers/server_catalog.dart'; import 'package:pipelines_app/core/servers/server_catalog_store.dart'; import 'package:pipelines_app/core/transport/pipelines_client.dart' as transport; import 'package:pipelines_app/features/attention/notify/notification_controller.dart'; import 'package:pipelines_app/features/attention/notify/notification_store.dart'; import 'package:pipelines_app/features/settings/saved_server_row.dart'; import 'package:pipelines_app/features/settings/saved_servers_section.dart'; import 'package:pipelines_app/features/settings/server_connections.dart'; import 'package:pipelines_app/features/settings/settings_group.dart'; import 'package:pipelines_app/features/settings/settings_layout.dart'; import 'package:pipelines_app/features/settings/settings_screen.dart'; import 'package:pipelines_app/features/settings/settings_store.dart'; import 'package:pipelines_app/features/settings/settings_trust_warning.dart'; import 'package:pipelines_app/ui/theme/app_theme.dart'; import 'fake_channel.dart'; import 'settings_topic_test_helper.dart'; void main() { testWidgets('groups start at the bar inset and stop at the reading width', ( tester, ) async { await _pump(tester, size: const Size(1200, 1600)); final groups = find.byType(SettingsGroup); expect(groups, findsWidgets); for (final group in groups.evaluate()) { final rect = tester.getRect(find.byWidget(group.widget)); // The column starts where the bar's title pill does, not centred. expect(rect.left, closeTo(settingsPageSidePadding, 0.01)); expect(rect.width, closeTo(settingsContentMaxWidth, 0.01)); } // A header's text lines up with its rows' text. final header = tester.getRect(find.text('Connection').last); expect( header.left, closeTo(settingsPageSidePadding + settingsRowInset, 0.01), ); }); testWidgets('Material groups are flat surfaces, not elevated cards', ( tester, ) async { await _pump(tester); expect(find.byType(Card), findsNothing); final surfaces = tester.widgetList( find.descendant( of: find.byType(SettingsGroup), matching: find.byType(Material), ), ); final scheme = Theme.of( tester.element(find.byType(SettingsGroup).first), ).colorScheme; final groupSurfaces = surfaces.where( (material) => material.color == scheme.surfaceContainer, ); expect(groupSurfaces, isNotEmpty); for (final material in groupSurfaces) { expect(material.elevation, 0); } // The trust warning is the Connection group's footer, not a card. expect(find.byType(SettingsTrustWarning), findsOneWidget); final title = tester.widget(find.text('Connection').last); final theme = Theme.of(tester.element(find.text('Connection').last)); expect(title.style?.color, theme.colorScheme.primary); }); testWidgets('content starts below the bar and persistent topic tabs', ( tester, ) async { const barHeight = 72.0; await _pump(tester, topPadding: barHeight); final first = tester.getRect(find.byType(SettingsGroup).first); expect( first.top, closeTo( barHeight + tester.getSize(find.byType(TabBar)).height + settingsPageTopPadding, 0.01, ), ); // The topic selector stays above each page scroll view. expect( tester.getRect(find.byKey(const ValueKey('settings.scroll')).last).top, barHeight + tester.getSize(find.byType(TabBar)).height, ); }); testWidgets('Cupertino uses inset-grouped sections on the grouped page', ( tester, ) async { await _pump(tester, cupertino: true); expect(find.byType(Card), findsNothing); expect(find.byType(CupertinoListSection), findsWidgets); for (final section in tester.widgetList( find.byType(CupertinoListSection), )) { expect(section.type, CupertinoListSectionType.insetGrouped); } final context = tester.element(find.byType(SettingsGroup).first); expect( find.byWidgetPredicate( (widget) => widget is ColoredBox && widget.color == AppColors.groupedBackground.resolveFrom(context), ), findsWidgets, ); }); testWidgets('the notification tile is the section\'s only title', ( tester, ) async { final notifications = NotificationController( store: MemoryNotificationStore(), ); addTearDown(notifications.dispose); await _pump(tester, notifications: notifications); await selectSettingsTopic(tester, 'Notifications'); expect(find.text('Notifications'), findsNWidgets(2)); expect(find.text('Connection').last, findsOneWidget); }); testWidgets('Cupertino keeps the compact secondary field label', ( tester, ) async { await _pump(tester, cupertino: true); final label = tester.widget(find.text('Server URL')); final context = tester.element(find.text('Server URL')); expect( label.style?.color, CupertinoColors.secondaryLabel.resolveFrom(context), ); expect(find.byType(CupertinoTextField), findsNWidgets(2)); }); for (final cupertino in [false, true]) { testWidgets('server rows show which server is selected ($cupertino)', ( tester, ) async { final connections = ServerConnections( store: MemoryServerCatalogStore( catalog: ServerCatalog( servers: [ SavedServer.validated( id: 'Home', name: 'Home', endpoints: const ['wss://home.test/connect'], ), SavedServer.validated( id: 'Work', name: 'Work', endpoints: const ['wss://work.test/connect'], ), ], selectedId: 'Home', ), ), onSelected: (_) async {}, ); addTearDown(connections.dispose); await connections.load(); final body = SingleChildScrollView( child: SavedServersSection(connections: connections), ); await tester.pumpWidget( cupertino ? CupertinoApp(home: CupertinoPageScaffold(child: body)) : MaterialApp(home: Scaffold(body: body)), ); expect(find.byType(SavedServerRow), findsNWidgets(2)); expect(find.text('Pipelines and runs server'), findsOneWidget); expect( find.byWidgetPredicate( (widget) => widget is Icon && widget.semanticLabel == 'Selected', ), findsOneWidget, ); expect(find.text('Address: wss://work.test/connect'), findsOneWidget); expect(find.byKey(savedServerMenuKey('Home')), findsOneWidget); expect(find.text('Add server'), findsOneWidget); // Tapping another server's row uses it. await tester.tap(find.text('Work')); await tester.pumpAndSettle(); expect(connections.catalog.active?.id, 'Work'); }); } } Future _pump( WidgetTester tester, { bool cupertino = false, NotificationController? notifications, Size size = const Size(800, 1600), double topPadding = 0, }) async { await tester.binding.setSurfaceSize(size); addTearDown(() => tester.binding.setSurfaceSize(null)); final factory = FakeChannelFactory(); final client = transport.PipelinesClient(channelFactory: factory.call); addTearDown(client.dispose); final screen = SettingsScreen( client: client, store: MemorySettingsStore(), notifications: notifications, embedded: true, ); Widget padded(BuildContext context, Widget? child) => MediaQuery( data: MediaQuery.of( context, ).copyWith(padding: EdgeInsets.only(top: topPadding)), child: child!, ); await tester.pumpWidget( cupertino ? CupertinoApp( builder: padded, home: CupertinoPageScaffold(child: screen), ) : MaterialApp( builder: padded, home: Scaffold(body: screen), ), ); await tester.pump(); await tester.pump(); await selectSettingsTopic(tester, 'Connection'); }