import 'dart:convert'; import 'dart:typed_data'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/artifacts/domain/artifact_groups.dart'; import 'package:pipelines_app/features/artifacts/ui/artifact_viewer_fetch.dart'; import 'package:pipelines_app/features/artifacts/ui/artifact_viewer_page.dart'; import 'package:pipelines_app/features/artifacts/ui/artifact_row_labels.dart'; import 'package:pipelines_app/features/changes/domain/change_kind.dart'; import 'package:pipelines_app/features/changes/domain/file_change.dart'; import 'package:pipelines_app/features/changes/widgets/changes_viewer.dart'; import 'package:pipelines_app/features/changes/widgets/file_diff_viewer.dart'; import 'package:pipelines_app/features/chat/conversation/chat_composer_bar.dart'; import 'package:pipelines_app/features/chat/conversation/chat_composer_controller.dart'; import 'package:pipelines_app/features/chat/conversation/chat_draft_store.dart'; import 'package:pipelines_app/ui/adaptive/glass_bar.dart'; import 'package:pipelines_app/ui/adaptive/glass_pill.dart'; import 'package:pipelines_app/ui/adaptive/segmented_bar.dart'; import '../../features/artifacts/ui/artifact_visit_list_fixture.dart'; import '../../features/chat/conversation/chat_composer_bar_fixtures.dart'; /// Every glass control bar keeps its first and last controls as far from the /// pill's sides as from its top and bottom, and spaces neighbours evenly, at /// phone width and at a larger text size. const _phone = Size(390, 844); const _scales = [1.0, 1.3]; final Uint8List _png = base64Decode( 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAE' 'hQGAhKmMIQAAAABJRU5ErkJggg==', ); Widget _host(Widget child) => MaterialApp(home: Scaffold(body: child)); Future _setUp(WidgetTester tester, double scale) async { tester.view.physicalSize = _phone * tester.view.devicePixelRatio; tester.platformDispatcher.textScaleFactorTestValue = scale; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.platformDispatcher.clearTextScaleFactorTestValue); } /// The host controls inside [pill], left to right. List _controls(WidgetTester tester, Finder pill) { final found = find.descendant( of: pill, matching: find.byWidgetPredicate((widget) => widget is IconButton), ); final rects = [ for (var i = 0; i < found.evaluate().length; i++) tester.getRect(found.at(i)), ]; rects.sort((a, b) => a.left.compareTo(b.left)); return rects; } /// The rule: an end control's gap from the pill's side equals its gap from /// the pill's top and bottom, (pill height - control height) / 2. void _expectEnd( Rect pill, Rect control, { required bool start, bool top = true, }) { final gap = (pill.height - control.height) / 2; expect(pill.bottom - control.bottom, closeTo(gap, 1), reason: 'bottom'); if (top) expect(control.top - pill.top, closeTo(gap, 1), reason: 'top'); expect( start ? control.left - pill.left : pill.right - control.right, closeTo(gap, 1), reason: start ? 'left' : 'right', ); } void _expectEqualEnds(Rect pill, Rect first, Rect last, {bool top = true}) { _expectEnd(pill, first, start: true, top: top); _expectEnd(pill, last, start: false, top: top); } /// Neighbouring controls in [rects] are evenly spaced. void _expectEvenNeighbours(List rects) { if (rects.length < 3) return; final gaps = [ for (var i = 1; i < rects.length; i++) rects[i].left - rects[i - 1].right, ]; for (final gap in gaps) { expect(gap, closeTo(gaps.first, 1), reason: 'neighbour gaps $gaps'); } } Rect _pillOf(WidgetTester tester, Finder inside) => tester.getRect( find.ancestor(of: inside, matching: find.byType(GlassPill)).first, ); void main() { for (final scale in _scales) { final when = 'text x$scale'; testWidgets('artifact viewer bar: equal end gaps ($when)', (tester) async { await _setUp(tester, scale); for (final count in [1, 3]) { final artifacts = [ for (var i = 0; i < count; i++) recordedArtifact( 'shot-$i.png', url: '/artifacts/1/1/shot-$i.png', contentType: 'image/png', ), ]; final group = ArtifactGroup( stageKey: 'review', visit: 1, artifacts: artifacts, ); await tester.pumpWidget( _host( ArtifactViewerPage( key: ValueKey(count), groups: [group], selectedIdentity: artifactVisitIdentity(group, artifacts.first), configuredEndpoint: 'ws://127.0.0.1:8081/connect', fetch: (_) async => ArtifactFetchResult(status: 200, bytes: _png), markdownBuilder: (context, value) => Text(value), openExternal: (_) async => true, ), ), ); await tester.pumpAndSettle(); final row = find.byKey(const Key('artifact-viewer-actions')); final pill = _pillOf(tester, row); final controls = _controls(tester, row); expect(controls.length, greaterThanOrEqualTo(2)); _expectEqualEnds(pill, controls.first, controls.last); _expectEvenNeighbours( count > 1 ? controls.sublist(0, controls.length - 2) : controls, ); if (count > 1) { _expectEvenNeighbours(controls.sublist(controls.length - 2)); } expect(pill.height, closeTo(GlassBarMetrics.height(), 1)); expect(tester.takeException(), isNull); } }); testWidgets('diff page pager: position and chevrons inset ($when)', ( tester, ) async { await _setUp(tester, scale); await tester.pumpWidget( _host( FileDiffPage( files: [ for (final name in ['a', 'b', 'c']) FileChange( path: 'lib/$name.dart', kind: ChangeKind.modified, patch: '--- a/lib/$name.dart\n+++ b/lib/$name.dart\n' '@@ -1 +1 @@\n-x\n+y\n', ), ], ), ), ); await tester.pumpAndSettle(); final pager = find.byKey(const ValueKey('file-diff-pager')); _expectPager(tester, pager, find.text('1 of 3')); }); testWidgets('changes visit pager: equal end gaps ($when)', (tester) async { await _setUp(tester, scale); Map visit(String stage, int sequence) => { 'stage': stage, 'sequence': sequence, 'status': 'completed', 'file_changes': { 'status': 'complete', 'scope': 'Git tracked files', 'skipped': 0, 'omitted': 0, 'diagnostic': '', 'files': [ {'path': 'lib/$stage.dart', 'kind': 'added'}, ], }, }; await tester.pumpWidget( _host( ChangesViewer( steps: [visit('build', 1), visit('review', 2)], showSummary: false, ), ), ); await tester.pumpAndSettle(); final position = find.byKey( const ValueKey('changes-visit-position'), ); _expectPager( tester, find.ancestor(of: position, matching: find.byType(Row)).first, position, ); }); testWidgets('chat composer: + and Send keep the pill gap ($when)', ( tester, ) async { await _setUp(tester, scale); final gateway = ComposerBarGateway(); addTearDown(gateway.close); final composer = ChatComposerController( gateway, 's', drafts: MemoryChatDraftStore(), ); addTearDown(composer.dispose); await tester.pumpWidget( _host( Align( alignment: Alignment.bottomCenter, child: ChatComposerBar( composer: composer, running: false, placeholder: 'Message', ), ), ), ); await tester.pumpAndSettle(); final pill = tester.getRect(find.byKey(const Key('chat-composer-pill'))); final attach = tester.getRect( find.byKey(const Key('chat-composer-attach')), ); final send = tester.getRect( find.byKey(const Key('chat-composer-primary')), ); // The controls keep to the field's last line: each end control is as // far from the pill's side as from its bottom, and while the field is // one line (no taller than a control) as far from its top. final row = GlassBarMetrics.controlExtent(); final oneLine = pill.height <= row + 2 * GlassBarMetrics.fieldEdgeGap + 0.5; for (final (control, start) in [(attach, true), (send, false)]) { final bottom = pill.bottom - control.bottom; final side = start ? control.left - pill.left : pill.right - control.right; expect(side, closeTo(bottom, 1), reason: 'side vs bottom'); if (oneLine) { expect(control.top - pill.top, closeTo(bottom, 1), reason: 'top'); expect(bottom, closeTo((pill.height - control.height) / 2, 1)); } } if (scale == 1.0) expect(oneLine, isTrue, reason: 'pill $pill'); expect(tester.takeException(), isNull); }); testWidgets('top bar actions pill: controls fill it evenly ($when)', ( tester, ) async { await _setUp(tester, scale); final actions = [ for (final name in ['a', 'b', 'c']) IconButton( key: Key('action-$name'), onPressed: () {}, icon: const Icon(Icons.info_outline), ), ]; await tester.pumpWidget( MaterialApp( home: Scaffold( appBar: SegmentedBar(title: 'Title', actions: actions), ), ), ); await tester.pumpAndSettle(); final first = find.byKey(const Key('action-a')); final pill = _pillOf(tester, first); final controls = [ for (final name in ['a', 'b', 'c']) tester.getRect(find.byKey(Key('action-$name'))), ]; _expectEqualEnds(pill, controls.first, controls.last); _expectEvenNeighbours(controls); expect(tester.takeException(), isNull); }); } } /// A pager row: its position text starts an icon's inset in from the pill's /// edge (where an icon at that end would be drawn), and its chevrons keep the /// rule's equal gaps at the other end. void _expectPager(WidgetTester tester, Finder inside, Finder position) { final pill = _pillOf(tester, inside); final controls = _controls(tester, inside); expect(controls, hasLength(2)); final last = controls.last; _expectEnd(pill, last, start: false); _expectEvenNeighbours(controls); final text = tester.getRect(position); final gap = (pill.height - last.height) / 2; expect(text.left - pill.left, closeTo(gap + GlassBarMetrics.iconInset(), 1)); // The chevrons keep the same gap from the position as from each other's // icons: one icon inset on each side. expect( controls.first.left - text.right, greaterThanOrEqualTo(GlassBarMetrics.iconInset() - 1), ); expect(pill.height, closeTo(GlassBarMetrics.height(), 1)); expect(tester.takeException(), isNull); }