2e5ac1984d
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
320 lines
13 KiB
Dart
320 lines
13 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:pipelines_app/features/chat/domain/chat_colors.dart';
|
|
import 'package:pipelines_app/features/chat/ui/sessions/chat_color_palette.dart';
|
|
import 'package:pipelines_app/ui/adaptive/platform.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/app_design_tokens.dart';
|
|
import 'package:pipelines_app/ui/theme/app_theme.dart';
|
|
import 'package:pipelines_app/ui/theme/high_contrast_palette.dart';
|
|
|
|
ThemeData _theme(AppDesign design, Brightness brightness) =>
|
|
brightness == Brightness.dark ? design.dark() : design.light();
|
|
|
|
/// Fails with the pair's name and ratio when [fg] on [bg] is below [min].
|
|
void _expectContrast(String pair, Color fg, Color bg, double min) {
|
|
final ratio = contrastRatio(fg, bg);
|
|
expect(
|
|
ratio,
|
|
greaterThanOrEqualTo(min),
|
|
reason: '$pair: ${ratio.toStringAsFixed(2)}:1, wanted $min:1',
|
|
);
|
|
}
|
|
|
|
void main() {
|
|
test('High contrast is listed with a stable id, a label and one line', () {
|
|
expect(AppDesign.values, contains(AppDesign.highContrast));
|
|
expect(AppDesign.fromId('high-contrast'), AppDesign.highContrast);
|
|
expect(AppDesign.highContrast.label, 'High contrast');
|
|
expect(AppDesign.highContrast.description, isNot(contains('\n')));
|
|
expect(AppDesign.highContrast.usesInterfaceFont, isTrue);
|
|
});
|
|
|
|
test(
|
|
'tokens: boxed transcript, inverted row, 6px corners, 2px frames, a '
|
|
'thicker usage ring, a 3px focus ring, 48px targets, a contrast floor',
|
|
() {
|
|
for (final brightness in Brightness.values) {
|
|
final theme = _theme(AppDesign.highContrast, brightness);
|
|
final scheme = theme.colorScheme;
|
|
final tokens = theme.extension<AppDesignTokens>()!;
|
|
expect(tokens.design, AppDesign.highContrast);
|
|
expect(tokens.transcript, TranscriptStyle.boxed);
|
|
expect(tokens.selectedRow, SelectedRowStyle.inverted);
|
|
expect(tokens.composer, ComposerStyle.pill);
|
|
expect(tokens.radius, 6);
|
|
expect(tokens.flat, isTrue);
|
|
expect(tokens.stroke, 2);
|
|
expect(tokens.frameSide, BorderSide(color: scheme.outline, width: 2));
|
|
expect(tokens.ringStroke, 3);
|
|
expect(tokens.focusRing?.width, 3);
|
|
expect(tokens.focusRing?.offset, greaterThan(0));
|
|
expect(tokens.focusRing?.color, scheme.primary);
|
|
expect(tokens.largeTargets, isTrue);
|
|
expect(tokens.minContrast, highContrastMinimum);
|
|
expect(tokens.windowRadius, 6);
|
|
expect(tokens.snapSwitch, isTrue);
|
|
expect(tokens.ansi, isNotNull);
|
|
}
|
|
},
|
|
);
|
|
|
|
test('pages are pure black and pure white, every container the page', () {
|
|
final dark = AppDesign.highContrast.dark().colorScheme;
|
|
final light = AppDesign.highContrast.light().colorScheme;
|
|
expect(dark.surface, const Color(0xFF000000));
|
|
expect(dark.onSurface, const Color(0xFFFFFFFF));
|
|
expect(light.surface, const Color(0xFFFFFFFF));
|
|
expect(light.onSurface, const Color(0xFF000000));
|
|
for (final scheme in [dark, light]) {
|
|
for (final container in [
|
|
scheme.surfaceContainerLowest,
|
|
scheme.surfaceContainerLow,
|
|
scheme.surfaceContainer,
|
|
scheme.surfaceContainerHigh,
|
|
scheme.surfaceContainerHighest,
|
|
]) {
|
|
expect(container, scheme.surface);
|
|
}
|
|
}
|
|
});
|
|
|
|
group('contrast', () {
|
|
for (final brightness in Brightness.values) {
|
|
test('$brightness: body text and status words reach 7:1, everything '
|
|
'else 4.5:1', () {
|
|
final theme = _theme(AppDesign.highContrast, brightness);
|
|
final s = theme.colorScheme;
|
|
final page = s.surface;
|
|
// WCAG AAA (7:1): reading text, the accent, errors and every
|
|
// status colour a word is written in.
|
|
for (final (name, fg, bg) in [
|
|
('ink on page', s.onSurface, page),
|
|
('secondary ink on page', s.onSurfaceVariant, page),
|
|
('accent on page', s.primary, page),
|
|
('text on accent', s.onPrimary, s.primary),
|
|
('error on page', s.error, page),
|
|
('error text on its container', s.onErrorContainer, s.errorContainer),
|
|
('inverted row text', s.onSecondaryContainer, s.secondaryContainer),
|
|
('tooltip/snack bar text', s.onInverseSurface, s.inverseSurface),
|
|
('snack bar action', s.inversePrimary, s.inverseSurface),
|
|
('green status', theme.ext.ansi!.green, page),
|
|
('cyan status', theme.ext.ansi!.cyan, page),
|
|
('yellow status', theme.ext.ansi!.yellow, page),
|
|
('red status', theme.ext.ansi!.red, page),
|
|
('magenta status', theme.ext.ansi!.magenta, page),
|
|
('blue status', theme.ext.ansi!.blue, page),
|
|
]) {
|
|
_expectContrast(name, fg, bg, highContrastText);
|
|
}
|
|
// AA (4.5:1) for everything else, disabled text included.
|
|
for (final (name, fg, bg) in [
|
|
('control border', s.outline, page),
|
|
('rule', s.outlineVariant, page),
|
|
('warning (usage ring)', s.tertiary, page),
|
|
('text on warning', s.onTertiary, s.tertiary),
|
|
('text on error', s.onError, s.error),
|
|
('disabled text', theme.disabledColor, page),
|
|
('hint text', theme.hintColor, page),
|
|
('focus ring', theme.ext.focusRing!.color, page),
|
|
]) {
|
|
_expectContrast(name, fg, bg, highContrastMinimum);
|
|
}
|
|
});
|
|
|
|
testWidgets('$brightness: every chat colour reaches 4.5:1 on the page, '
|
|
'and keeps its hue', (tester) async {
|
|
final theme = _theme(AppDesign.highContrast, brightness);
|
|
late BuildContext context;
|
|
await tester.pumpWidget(
|
|
Theme(
|
|
data: theme,
|
|
child: Builder(
|
|
builder: (c) {
|
|
context = c;
|
|
return const SizedBox();
|
|
},
|
|
),
|
|
),
|
|
);
|
|
for (final name in chatColorNames) {
|
|
final shade = chatColorShade(context, name)!;
|
|
_expectContrast(
|
|
'chat colour $name',
|
|
shade,
|
|
theme.scaffoldBackgroundColor,
|
|
highContrastMinimum,
|
|
);
|
|
}
|
|
// The orange's hue survives the darkening on white.
|
|
final orange = HSVColor.fromColor(chatColorShade(context, 'orange')!);
|
|
expect(orange.hue, inInclusiveRange(15, 45));
|
|
});
|
|
}
|
|
|
|
test('colorWithContrast moves away from the page, never past it', () {
|
|
const yellow = Color(0xFFFFEB3B);
|
|
final onWhite = colorWithContrast(yellow, const Color(0xFFFFFFFF), 4.5);
|
|
expect(contrastRatio(onWhite, const Color(0xFFFFFFFF)), greaterThan(4.5));
|
|
expect(onWhite.computeLuminance(), lessThan(yellow.computeLuminance()));
|
|
// Already enough: unchanged.
|
|
expect(
|
|
colorWithContrast(yellow, const Color(0xFF000000), 4.5),
|
|
same(yellow),
|
|
);
|
|
});
|
|
});
|
|
|
|
test('flat, bordered surfaces: no elevation, 2px borders, 6px corners', () {
|
|
for (final brightness in Brightness.values) {
|
|
final theme = _theme(AppDesign.highContrast, brightness);
|
|
final scheme = theme.colorScheme;
|
|
expect(theme.cardTheme.elevation, 0);
|
|
expect(theme.dialogTheme.elevation, 0);
|
|
expect(theme.popupMenuTheme.elevation, 0);
|
|
expect(theme.snackBarTheme.elevation, 0);
|
|
expect(theme.floatingActionButtonTheme.elevation, 0);
|
|
expect(theme.appBarTheme.scrolledUnderElevation, 0);
|
|
for (final shape in [
|
|
theme.cardTheme.shape,
|
|
theme.dialogTheme.shape,
|
|
theme.popupMenuTheme.shape,
|
|
theme.snackBarTheme.shape,
|
|
]) {
|
|
final border = shape! as RoundedRectangleBorder;
|
|
expect(border.borderRadius, BorderRadius.circular(6));
|
|
expect(border.side.width, 2);
|
|
}
|
|
expect(
|
|
(theme.cardTheme.shape! as RoundedRectangleBorder).side.color,
|
|
scheme.outline,
|
|
);
|
|
// Tooltips: large, inverted, framed.
|
|
final tip = theme.tooltipTheme.decoration! as BoxDecoration;
|
|
expect(tip.color, scheme.inverseSurface);
|
|
expect(theme.tooltipTheme.textStyle!.fontSize, greaterThanOrEqualTo(16));
|
|
// Fields: 2px, 3px of the accent while focused.
|
|
final field = theme.inputDecorationTheme.border!;
|
|
final idle = WidgetStateProperty.resolveAs<InputBorder>(field, {});
|
|
final focused = WidgetStateProperty.resolveAs<InputBorder>(field, {
|
|
WidgetState.focused,
|
|
});
|
|
expect(idle.borderSide.width, 2);
|
|
expect(focused.borderSide.width, 3);
|
|
expect(focused.borderSide.color, scheme.primary);
|
|
// Outlined and filled buttons framed at 2px; disabled ones in the
|
|
// disabled ink, still 4.5:1.
|
|
final outlined = theme.outlinedButtonTheme.style!;
|
|
expect(outlined.side!.resolve({})!.width, 2);
|
|
expect(
|
|
outlined.foregroundColor!.resolve({WidgetState.disabled}),
|
|
theme.disabledColor,
|
|
);
|
|
expect(theme.filledButtonTheme.style!.side!.resolve({})!.width, 2);
|
|
}
|
|
});
|
|
|
|
test('larger, heavier type: body 16-17px at weight 500', () {
|
|
final text = AppDesign.highContrast.light().textTheme;
|
|
expect(text.bodyLarge!.fontSize, 17);
|
|
expect(text.bodyMedium!.fontSize, 16);
|
|
expect(text.bodySmall!.fontSize, greaterThanOrEqualTo(14));
|
|
for (final style in [text.bodyLarge, text.bodyMedium, text.bodySmall]) {
|
|
expect(style!.fontWeight, FontWeight.w500);
|
|
}
|
|
expect(text.labelSmall!.fontSize, greaterThanOrEqualTo(13));
|
|
});
|
|
|
|
test('the inverse theme is the same design in the other brightness', () {
|
|
final dark = AppDesign.highContrast.dark();
|
|
final inverse = AppDesignTokens.inverseTheme(dark);
|
|
expect(inverse.brightness, Brightness.light);
|
|
expect(
|
|
inverse.extension<AppDesignTokens>()!.design,
|
|
AppDesign.highContrast,
|
|
);
|
|
expect(AppDesignTokens.inverseTheme(dark), same(inverse));
|
|
});
|
|
|
|
group('the others are unchanged', () {
|
|
test('Classic is the original theme; new tokens default for every other '
|
|
'design', () {
|
|
expect(AppDesign.standard.light(), appLightTheme());
|
|
expect(AppDesign.standard.dark(), appDarkTheme());
|
|
for (final design in AppDesign.values) {
|
|
if (design == AppDesign.highContrast) continue;
|
|
for (final brightness in Brightness.values) {
|
|
final tokens =
|
|
_theme(design, brightness).extension<AppDesignTokens>() ??
|
|
AppDesignTokens.standard;
|
|
expect(tokens.stroke, 1, reason: design.id);
|
|
expect(tokens.ringStroke, 2, reason: design.id);
|
|
expect(tokens.focusRing, isNull, reason: design.id);
|
|
expect(tokens.largeTargets, isFalse, reason: design.id);
|
|
expect(tokens.minContrast, isNull, reason: design.id);
|
|
expect(tokens.windowRadius, isNull, reason: design.id);
|
|
// Glass snaps too: its clear pages do not blend with an opaque
|
|
// design's.
|
|
expect(
|
|
tokens.snapSwitch,
|
|
design == AppDesign.glass,
|
|
reason: design.id,
|
|
);
|
|
expect(tokens.transcript, isNot(TranscriptStyle.boxed));
|
|
expect(tokens.selectedRow, isNot(SelectedRowStyle.inverted));
|
|
final frame = tokens.frame;
|
|
expect(
|
|
tokens.frameSide,
|
|
frame == null ? isNull : BorderSide(color: frame),
|
|
);
|
|
}
|
|
}
|
|
});
|
|
});
|
|
|
|
test('a switch into or out of High contrast snaps; Paper to Terminal '
|
|
'still blends', () async {
|
|
final designs = AppDesignController(store: MemoryAppDesignStore());
|
|
await designs.select(AppDesign.paper);
|
|
await designs.select(AppDesign.terminal);
|
|
expect(designs.themeAnimationDuration, kThemeAnimationDuration);
|
|
await designs.select(AppDesign.highContrast);
|
|
expect(designs.themeAnimationDuration, Duration.zero);
|
|
await designs.select(AppDesign.paper);
|
|
expect(designs.themeAnimationDuration, Duration.zero);
|
|
});
|
|
|
|
testWidgets('touch targets on a desktop only under High contrast', (
|
|
tester,
|
|
) async {
|
|
debugDefaultTargetPlatformOverride = TargetPlatform.linux;
|
|
addTearDown(() => debugDefaultTargetPlatformOverride = null);
|
|
final compact = <AppDesign, bool>{};
|
|
for (final design in AppDesign.values) {
|
|
await tester.pumpWidget(
|
|
Theme(
|
|
data: design.light(),
|
|
child: Builder(
|
|
builder: (context) {
|
|
compact[design] = prefersCompactHitTargets(context);
|
|
return const SizedBox();
|
|
},
|
|
),
|
|
),
|
|
);
|
|
}
|
|
debugDefaultTargetPlatformOverride = null;
|
|
expect(compact[AppDesign.highContrast], isFalse);
|
|
for (final design in AppDesign.values) {
|
|
if (design == AppDesign.highContrast) continue;
|
|
expect(compact[design], isTrue, reason: design.id);
|
|
}
|
|
});
|
|
}
|
|
|
|
extension on ThemeData {
|
|
AppDesignTokens get ext => extension<AppDesignTokens>()!;
|
|
}
|