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/changes/changes.dart'; import 'fixtures.dart'; import 'widget_hosts.dart'; String _patch(String path, String line) => '--- "a/$path"\n' '+++ "b/$path"\n' '@@ -1,1 +1,1 @@\n' '-old $line\n' '+new $line\n'; List _steps() => [ stepFixture( fileChanges: captureFixture( files: [ fileChangeFixture(path: 'lib/a.dart', patch: _patch('lib/a.dart', 'A')), fileChangeFixture(path: 'lib/b.dart', patch: _patch('lib/b.dart', 'B')), fileChangeFixture(path: 'lib/c.dart', patch: _patch('lib/c.dart', 'C')), ], ), ), ]; Future _pumpAt( WidgetTester tester, Size size, { bool finalOnly = true, }) async { tester.view.physicalSize = size; tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); await tester.pumpWidget( materialHost(ChangesViewer(steps: _steps(), finalOnly: finalOnly)), ); await tester.pumpAndSettle(); } bool _shows(String text) => find.textContaining(text, findRichText: true).evaluate().isNotEmpty; void main() { testWidgets('a wide pane lists the files beside the first file\'s diff', ( tester, ) async { await _pumpAt(tester, const Size(1400, 900)); expect(find.byKey(const ValueKey('changes-final-split')), findsOneWidget); expect(find.byType(FileDiffViewer), findsOneWidget); expect( find.byKey(const ValueKey('changes-split-diff-lib/a.dart')), findsOneWidget, ); expect(_shows('new A'), isTrue); expect(_shows('new B'), isFalse); }); testWidgets('tapping a file shows its diff without leaving the pane', ( tester, ) async { await _pumpAt(tester, const Size(1400, 900)); await tester.tap(find.bySemanticsLabel(RegExp('^Show diff for lib/c'))); await tester.pumpAndSettle(); expect(find.byType(FileDiffPage), findsNothing); expect( find.byKey(const ValueKey('changes-split-diff-lib/c.dart')), findsOneWidget, ); expect(_shows('new C'), isTrue); }); testWidgets('j/k and the arrows move between files once focused', ( tester, ) async { await _pumpAt(tester, const Size(1400, 900)); await tester.tap(find.bySemanticsLabel(RegExp('^Show diff for lib/a'))); await tester.pumpAndSettle(); Future press(LogicalKeyboardKey key) async { await tester.sendKeyEvent(key); await tester.pumpAndSettle(); } await press(LogicalKeyboardKey.keyJ); expect(_shows('new B'), isTrue); await press(LogicalKeyboardKey.arrowDown); expect(_shows('new C'), isTrue); // The last file stays selected rather than wrapping. await press(LogicalKeyboardKey.keyJ); expect(_shows('new C'), isTrue); await press(LogicalKeyboardKey.keyK); expect(_shows('new B'), isTrue); await press(LogicalKeyboardKey.arrowUp); expect(_shows('new A'), isTrue); // Enter opens the selected file on its own page. await press(LogicalKeyboardKey.enter); expect(find.byType(FileDiffPage), findsOneWidget); }); testWidgets('a visit page splits too, under the visit heading', ( tester, ) async { await _pumpAt(tester, const Size(1400, 900), finalOnly: false); expect( find.byKey(const ValueKey('changes-visit-split-implementer-1')), findsOneWidget, ); expect(find.textContaining('Visit 1'), findsOneWidget); expect(_shows('new A'), isTrue); }); testWidgets('a phone keeps one row per file opening its own page', ( tester, ) async { await _pumpAt(tester, const Size(560, 1000)); expect(find.byKey(const ValueKey('changes-final-split')), findsNothing); expect(find.byType(FileDiffViewer), findsNothing); await tester.tap(find.bySemanticsLabel(RegExp('^Open diff for lib/b'))); await tester.pumpAndSettle(); expect(find.byType(FileDiffPage), findsOneWidget); }); testWidgets( 'a detail pane beside a list splits from 720 px in a wide window', (tester) async { tester.view.physicalSize = const Size(1440, 900); tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); Widget pane(double width) => materialHost( Align( alignment: Alignment.topRight, child: SizedBox( width: width, child: ChangesViewer(steps: _steps(), finalOnly: true), ), ), ); await tester.pumpWidget(pane(900)); await tester.pumpAndSettle(); expect(find.byKey(const ValueKey('changes-final-split')), findsOneWidget); await tester.pumpWidget(pane(600)); await tester.pumpAndSettle(); expect(find.byKey(const ValueKey('changes-final-split')), findsNothing); }, ); }