Files
pipelines/app/test/ui/theme/high_contrast_widgets_test.dart
T

377 lines
13 KiB
Dart

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/core/api_gateway.dart';
import 'package:pipelines_app/features/chat/domain/chat_transcript.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_message_bubble.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_message_view.dart';
import 'package:pipelines_app/features/settings/settings_design_section.dart';
import 'package:pipelines_app/ui/layout/app_list_avatar.dart';
import 'package:pipelines_app/ui/layout/app_list_row.dart';
import 'package:pipelines_app/ui/theme/app_design.dart';
import 'package:pipelines_app/ui/theme/app_design_controller.dart';
import 'package:pipelines_app/ui/theme/design_focus_ring.dart';
JsonMap _tool(String id, String status) => {
'type': 'tool',
'id': id,
'name': 'Bash',
'state': {
'status': status,
'input': {'command': 'go test ./internal/notes/...'},
'content': [
{'type': 'text', 'text': 'ok notes 0.2s'},
],
if (status == 'error') 'error': {'message': 'exit status 1'},
},
};
/// An operator turn, then an answer with a passing and a failing tool call.
final List<JsonMap> _rows = transcriptRows([
{
'id': 'u1',
'role': 'user',
'parts': [
{
'type': 'text',
'text': 'The release notes job fails on Windows paths. Fix it.',
},
],
},
{
'id': 'a1',
'role': 'assistant',
'parts': [
_tool('t1', 'completed'),
_tool('t2', 'error'),
{'type': 'text', 'text': 'Fixed: `filepath.Join` now.'},
],
},
]);
/// [child] under [design]'s theme at [width], with [textScale], inside the
/// focus ring host the app's builders use.
Widget _host(
Widget child, {
AppDesign design = AppDesign.highContrast,
Brightness brightness = Brightness.dark,
double textScale = 1,
}) => MaterialApp(
theme: design.light(),
darkTheme: design.dark(),
themeMode: brightness == Brightness.dark ? ThemeMode.dark : ThemeMode.light,
builder: (context, child) => DesignFocusRing(
child: MediaQuery(
data: MediaQuery.of(
context,
).copyWith(textScaler: TextScaler.linear(textScale)),
child: child!,
),
),
home: Scaffold(body: child),
);
void _size(WidgetTester tester, Size size) {
tester.view.physicalSize = size;
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
}
FocusRingPainter? _ring(WidgetTester tester) => tester
.widgetList<CustomPaint>(find.byType(CustomPaint))
.map((paint) => paint.painter)
.whereType<FocusRingPainter>()
.firstOrNull;
void main() {
group('focus ring', () {
testWidgets('a 3px ring with an offset goes round the control with '
'keyboard focus', (tester) async {
_size(tester, const Size(600, 400));
await tester.pumpWidget(
_host(
Center(
child: FilledButton(onPressed: () {}, child: const Text('Go')),
),
),
);
expect(_ring(tester)?.rect.value, isNull);
await tester.sendKeyEvent(LogicalKeyboardKey.tab);
await tester.pump();
await tester.pump();
final ring = _ring(tester)!;
final rect = ring.rect.value!;
final button = tester.getRect(find.byType(FilledButton));
// The ring's target is the button itself (within its tap padding).
expect(button.inflate(1).contains(rect.topLeft), isTrue);
expect(button.inflate(1).contains(rect.bottomRight), isTrue);
expect(ring.style.width, 3);
expect(ring.style.offset, greaterThan(0));
final scheme = AppDesign.highContrast.dark().colorScheme;
expect(
find.byWidgetPredicate(
(w) => w is CustomPaint && w.painter is FocusRingPainter,
),
paints
..rrect(color: scheme.surface, strokeWidth: ring.style.offset)
..rrect(color: scheme.primary, strokeWidth: 3),
);
});
testWidgets('a text field rings its whole decorated box', (tester) async {
_size(tester, const Size(600, 400));
await tester.pumpWidget(
_host(const Center(child: SizedBox(width: 300, child: TextField()))),
);
await tester.sendKeyEvent(LogicalKeyboardKey.tab);
await tester.pump();
await tester.pump();
final rect = _ring(tester)!.rect.value!;
expect(rect.size, tester.getSize(find.byType(InputDecorator)));
});
testWidgets('a pointer press hides the ring, as :focus-visible does; '
'no ring under another design', (tester) async {
_size(tester, const Size(600, 400));
await tester.pumpWidget(
_host(
Center(
child: FilledButton(onPressed: () {}, child: const Text('Go')),
),
),
);
await tester.sendKeyEvent(LogicalKeyboardKey.tab);
await tester.pump();
await tester.pump();
expect(_ring(tester)!.rect.value, isNotNull);
await tester.tap(find.byType(FilledButton));
await tester.pump();
await tester.pump();
expect(_ring(tester)!.rect.value, isNull);
for (final design in AppDesign.values) {
if (design == AppDesign.highContrast) continue;
await tester.pumpWidget(
_host(
Center(
child: FilledButton(onPressed: () {}, child: const Text('Go')),
),
design: design,
),
);
await tester.pumpAndSettle();
await tester.sendKeyEvent(LogicalKeyboardKey.tab);
await tester.pump();
expect(_ring(tester), isNull, reason: design.id);
}
});
});
testWidgets('every control keeps a 48px target on a desktop', (tester) async {
debugDefaultTargetPlatformOverride = TargetPlatform.linux;
addTearDown(() => debugDefaultTargetPlatformOverride = null);
_size(tester, const Size(800, 900));
await tester.pumpWidget(
_host(
ListView(
children: [
Wrap(
children: [
IconButton(onPressed: () {}, icon: const Icon(Icons.add)),
TextButton(onPressed: () {}, child: const Text('Ok')),
OutlinedButton(onPressed: () {}, child: const Text('Ok')),
FilledButton(onPressed: () {}, child: const Text('Ok')),
Checkbox(value: true, onChanged: (_) {}),
Switch(value: true, onChanged: (_) {}),
ActionChip(label: const Text('Chip'), onPressed: () {}),
FilterChip(
label: const Text('Filter'),
selected: true,
onSelected: (_) {},
),
],
),
const ListTile(title: Text('Tile')),
MenuItemButton(onPressed: () {}, child: const Text('Item')),
],
),
),
);
for (final type in [
IconButton,
TextButton,
OutlinedButton,
FilledButton,
Checkbox,
Switch,
ActionChip,
FilterChip,
ListTile,
MenuItemButton,
]) {
final size = tester.getSize(find.byType(type).first);
expect(size.height, greaterThanOrEqualTo(48), reason: '$type');
expect(size.width, greaterThanOrEqualTo(48), reason: '$type');
}
debugDefaultTargetPlatformOverride = null;
});
group('boxed transcript', () {
testWidgets('the operator\'s turn is a framed box under "You"; tools are '
'bordered rows that say Done and Failed', (tester) async {
_size(tester, const Size(900, 900));
await tester.pumpWidget(
_host(
ListView(
children: [for (final row in _rows) ChatMessageView(row: row)],
),
),
);
expect(find.byKey(const Key('chat-boxed-quote')), findsOneWidget);
expect(find.byKey(const Key('chat-boxed-quote-label')), findsOneWidget);
expect(find.text('You'), findsOneWidget);
expect(find.byKey(const Key('chat-boxed-activity')), findsOneWidget);
expect(find.byKey(const Key('boxed-status-done')), findsOneWidget);
expect(find.byKey(const Key('boxed-status-failed')), findsOneWidget);
expect(find.text('Done'), findsOneWidget);
expect(find.text('Failed'), findsOneWidget);
// The activity block is one big target.
final block = tester.getSize(
find.byKey(const Key('chat-boxed-activity')),
);
expect(block.height, greaterThanOrEqualTo(48));
// The two tool names start at one column, whatever the status word.
final names = tester
.widgetList<RichText>(find.byType(RichText))
.where((t) => t.text.toPlainText().startsWith('Bash'));
final starts = {
for (final name in names) tester.getTopLeft(find.byWidget(name)).dx,
};
expect(starts, hasLength(1));
// No bubble fill: the agent's answer sits on the page.
expect(find.byType(ChatUserBubble), findsOneWidget);
});
});
group('inverted open row', () {
Widget rows({AppDesign design = AppDesign.highContrast}) => _host(
Column(
children: [
AppListRow(
key: const Key('open'),
selected: true,
leading: const AppListAvatar(child: Icon(Icons.check)),
title: const Text('Release train run'),
subtitle: const Text('release-train · 30m'),
time: '09:12',
onTap: () {},
),
AppListRow(
key: const Key('closed'),
title: const Text('Nightly docs run'),
subtitle: const Text('nightly-docs · 3m'),
time: '23:00',
onTap: () {},
),
],
),
design: design,
);
testWidgets('the open row is the ink with the page as its text, and a '
'thicker bar', (tester) async {
_size(tester, const Size(500, 400));
await tester.pumpWidget(rows());
final dark = AppDesign.highContrast.dark();
final surface = tester.widget<Material>(
find.descendant(
of: find.byKey(const Key('open')),
matching: find.byKey(appListRowSurfaceKey),
),
);
expect(surface.color, dark.colorScheme.onSurface);
final title = tester.widget<DefaultTextStyle>(
find
.ancestor(
of: find.text('Release train run'),
matching: find.byType(DefaultTextStyle),
)
.first,
);
expect(title.style.color, dark.colorScheme.surface);
final bar = tester.widget<DecoratedBox>(
find.byKey(appListRowSelectionKey),
);
final border = (bar.decoration as BoxDecoration).border!;
expect((border as BorderDirectional).start.width, 6);
// The closed row keeps the page and the ink.
final closed = tester.widget<DefaultTextStyle>(
find
.ancestor(
of: find.text('Nightly docs run'),
matching: find.byType(DefaultTextStyle),
)
.first,
);
expect(closed.style.color, dark.colorScheme.onSurface);
});
testWidgets('Terminal\'s open row is unchanged: washed and framed', (
tester,
) async {
_size(tester, const Size(500, 400));
await tester.pumpWidget(rows(design: AppDesign.terminal));
final bar = tester.widget<DecoratedBox>(
find.byKey(appListRowSelectionKey),
);
final border =
(bar.decoration as BoxDecoration).border! as BorderDirectional;
expect(border.start.width, kAppListRowBarWidth);
expect(border.top.width, 1);
});
});
group('200% text scale: nothing overflows', () {
for (final brightness in Brightness.values) {
testWidgets('transcript, rows and the design picker on a phone '
'(${brightness.name})', (tester) async {
_size(tester, const Size(360, 2400));
await tester.pumpWidget(
_host(
ListView(
children: [
for (final row in _rows) ChatMessageView(row: row),
AppListRow(
selected: true,
leading: const AppListAvatar(child: Icon(Icons.check)),
title: const Text('Release train run with a long title'),
subtitle: const Text('release-train · 30m 0s · v4'),
detail: const Text('Release 2.4 shipped; notes published.'),
time: '09:12',
menu: IconButton(
onPressed: () {},
icon: const Icon(Icons.more_vert),
),
onTap: () {},
),
SettingsDesignSection(
controller: AppDesignController(
store: MemoryAppDesignStore(AppDesign.highContrast.id),
),
),
],
),
brightness: brightness,
textScale: 2,
),
);
await tester.pumpAndSettle();
expect(tester.takeException(), isNull);
expect(find.text('Done'), findsOneWidget);
expect(find.text('High contrast'), findsWidgets);
});
}
});
}