import 'dart:ui' as ui; import 'package:flutter/material.dart'; import 'package:flutter/semantics.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/features/chat/ui/sessions/chat_adaptive.dart'; import 'package:pipelines_app/features/terminal/terminal_layout.dart'; import 'package:pipelines_app/features/terminal/terminal_scope.dart'; import 'package:pipelines_app/features/terminal/terminal_toggle_button.dart'; import 'terminal_fixtures.dart'; class _Actions implements TerminalActions { int toggles = 0; @override void toggle() => toggles++; @override bool get claimsTabKeys => false; @override Future openAt(ApiGateway gateway, String cwd) async => null; } const _filesKey = ValueKey('files'); const _terminalKey = ValueKey('shell.terminal'); /// A chat's bar: Files beside the terminal toggle, as the shell shows them. Widget _bar(TerminalLayout layout, _Actions actions, ThemeData theme) => MaterialApp( theme: theme, home: TerminalScope( actions: actions, child: Scaffold( body: Row( mainAxisAlignment: MainAxisAlignment.end, children: [ ChatBarAction( key: _filesKey, label: 'Files', onPressed: () {}, materialIcon: Icons.folder_outlined, ), TerminalToggleButton(layout: layout), ], ), ), ), ); /// The colour an icon inside [finder] resolves to in the current theme. Color? _iconColor(WidgetTester tester, Finder finder) { final icon = find.descendant(of: finder, matching: find.byType(Icon)); final element = tester.element(icon); return tester.widget(icon).color ?? IconTheme.of(element).color; } /// Whether the terminal button's semantics (or one of its merged parents) /// is announced as toggled on. bool _toggled(WidgetTester tester) { for ( SemanticsNode? node = tester.getSemantics( find.byTooltip(TerminalToggleButton.label), ); node != null; node = node.parent ) { if (node.flagsCollection.isToggled == ui.Tristate.isTrue) return true; } return false; } void main() { for (final theme in [ ThemeData(brightness: Brightness.light, useMaterial3: true), ThemeData(brightness: Brightness.dark, useMaterial3: true), ]) { final mode = theme.brightness.name; testWidgets('$mode: the terminal button is a plain bar button, shown or ' 'hidden, like Files', (tester) async { final layout = TerminalLayout(store: MemoryLayoutStore()); final actions = _Actions(); await tester.pumpWidget(_bar(layout, actions, theme)); await tester.pump(); final files = find.byKey(_filesKey); final terminal = find.byKey(_terminalKey); for (final shown in [false, true]) { if (shown) layout.show(); await tester.pump(); final button = tester.widget(terminal); expect(button.isSelected, isNull, reason: 'shown: $shown'); expect( _iconColor(tester, terminal), _iconColor(tester, files), reason: 'icon colour differs from Files (shown: $shown)', ); expect(tester.getSize(terminal), tester.getSize(files)); } }); testWidgets('$mode: the shown state is announced without a tint', ( tester, ) async { final handle = tester.ensureSemantics(); final layout = TerminalLayout(store: MemoryLayoutStore()); final actions = _Actions(); await tester.pumpWidget(_bar(layout, actions, theme)); await tester.pump(); expect(_toggled(tester), isFalse); layout.show(); await tester.pump(); expect(_toggled(tester), isTrue); await tester.tap(find.byKey(_terminalKey)); expect(actions.toggles, 1); handle.dispose(); }); } }