import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/changes/changes.dart'; import 'package:pipelines_app/features/changes/widgets/change_style.dart'; import 'fixtures.dart'; import 'widget_hosts.dart'; /// The patch has one short context line and one much longer added line, so a /// text-width tint and a full-hunk-width tint are visibly different sizes. const _raggedPatch = '@@ -1,4 +1,4 @@\n' ' short\n' '+this added line is much longer than every other recorded line here\n' '-x\n' ' tail\n'; /// Opens the real single-file diff modal from a host, so the test covers the /// dialog's own width and insets rather than the viewer in isolation. Widget _diffDialogOpener(FileChange file) => Builder( builder: (context) => TextButton( onPressed: () => showFileDiffViewer(context, file), child: const Text('Open diff'), ), ); /// Fails when a fixed control label is broken inside a word or clipped by its /// control. A label may wrap only between whole words, which is how a native /// menu keeps an accessibility-size label readable. void _expectWholeLabel(WidgetTester tester, Finder finder, String label) { final paragraph = tester.renderObject( find.descendant(of: finder, matching: find.byType(RichText)), ); final span = paragraph.text as TextSpan; expect(span.toPlainText(), label); expect(paragraph.didExceedMaxLines, isFalse); final boxes = paragraph .getBoxesForSelection( TextSelection(baseOffset: 0, extentOffset: label.length), ) .toList(); expect(boxes, isNotEmpty); // The laid-out paragraph must be tall enough to paint every line it broke. expect( boxes.last.bottom, lessThanOrEqualTo(paragraph.size.height + 0.5), reason: '"$label" must not be clipped vertically', ); final widestLine = boxes .map((box) => box.right - box.left) .reduce((a, b) => a > b ? a : b); final style = span.style ?? const TextStyle(); var widestWord = 0.0; for (final word in label.split(' ')) { final painter = TextPainter( text: TextSpan(text: word, style: style), textDirection: TextDirection.ltr, textScaler: paragraph.textScaler, )..layout(); if (painter.width > widestWord) widestWord = painter.width; } // A line narrower than a whole word means the label broke mid-word. expect( widestLine, greaterThanOrEqualTo(widestWord - 0.5), reason: '"$label" must not break inside a word', ); // A clipped line would paint wider than the paragraph's laid-out box. expect( widestLine, lessThanOrEqualTo(paragraph.size.width + 0.5), reason: '"$label" must not be clipped horizontally', ); } void main() { test('patch typography uses the bundled fixed-width font', () { const native = TextStyle(fontSize: 19, fontFamily: 'host-font'); expect(nativeDiffText(native), native.copyWith(fontFamily: 'RobotoMono')); }); testWidgets( 'light and dark themes give additions and removals distinct hues', (tester) async { for (final brightness in [Brightness.light, Brightness.dark]) { await tester.pumpWidget( materialHost(const SizedBox.shrink(), brightness: brightness), ); await tester.pumpAndSettle(); final context = tester.element(find.byType(Scaffold)); final add = diffLineColors(context, ChangeLineKind.add); final remove = diffLineColors(context, ChangeLineKind.remove); // GitHub's palette: green and red line tints and markers. final dark = brightness == Brightness.dark; expect( add.background, dark ? const Color(0x262EA043) : const Color(0xFFDAFBE1), ); expect( remove.background, dark ? const Color(0x1AF85149) : const Color(0xFFFFEBE9), ); expect( add.marker, dark ? const Color(0xFF3FB950) : const Color(0xFF1A7F37), ); expect( remove.marker, dark ? const Color(0xFFF85149) : const Color(0xFFD1242F), ); expect(add.text, Theme.of(context).colorScheme.onSurface); } }, ); testWidgets('every diff line shares the hunk width, not its own text width', ( tester, ) async { await tester.pumpWidget( materialHost( SizedBox( height: 400, child: FileDiffViewer( file: FileChange( path: 'lib/ragged.dart', kind: ChangeKind.modified, patch: _raggedPatch, ), ), ), ), ); await tester.pumpAndSettle(); final widths = [ for (var index = 0; index < 4; index++) tester.getSize(find.byKey(ValueKey('diff-line-$index'))).width, ]; for (final width in widths) { expect(width, closeTo(widths.first, 0.01)); } // The widest recorded line sets the hunk width, well beyond a short line. expect(widths.first, greaterThan(300)); }); testWidgets('the stat line colours additions and removals apart', ( tester, ) async { await tester.pumpWidget( materialHost( SizedBox( height: 400, child: FileDiffViewer( file: FileChange( path: 'lib/feature.dart', kind: ChangeKind.modified, patch: multiHunkPatch, ), ), ), ), ); await tester.pumpAndSettle(); final stat = tester.widgetList(find.byType(RichText)).firstWhere(( rich, ) { final span = rich.text; return span is TextSpan && span.children != null && span.toPlainText() == '+2 -2'; }); final root = (stat.text as TextSpan).children!.single as TextSpan; final spans = root.children!.cast(); final added = spans.firstWhere((span) => span.text == '+2'); final removed = spans.firstWhere((span) => span.text == '-2'); expect(added.style!.color, isNot(removed.style!.color)); expect( find.byWidgetPredicate((widget) { if (widget is! RichText || widget.text is! TextSpan) return false; return (widget.text as TextSpan).toPlainText() == '+2 -2'; }), findsOneWidget, reason: 'the file header is the only visible line-count summary', ); }); testWidgets('the raw view scrolls long lines instead of wrapping them', ( tester, ) async { await tester.pumpWidget( materialHost( SizedBox( height: 400, child: FileDiffViewer( file: FileChange( path: 'lib/feature.dart', kind: ChangeKind.modified, patch: multiHunkPatch, ), ), ), ), ); await tester.tap(find.text('Raw patch')); await tester.pumpAndSettle(); expect(find.text(multiHunkPatch), findsOneWidget); expect( find.ancestor( of: find.text(multiHunkPatch), matching: find.byWidgetPredicate( (widget) => widget is SingleChildScrollView && widget.scrollDirection == Axis.horizontal, ), ), findsOneWidget, ); }); testWidgets('the Cupertino raw patch is a native selectable field', ( tester, ) async { await tester.pumpWidget( cupertinoHost( SizedBox( height: 400, child: FileDiffViewer( file: FileChange( path: 'lib/feature.dart', kind: ChangeKind.modified, patch: multiHunkPatch, ), ), ), ), ); await tester.tap(find.text('Raw patch')); await tester.pumpAndSettle(); // A CupertinoApp has no MaterialLocalizations, so Raw must not use the // Material SelectionArea; it renders the recorded bytes in a native field. expect(tester.takeException(), isNull); expect(find.byType(SelectionArea), findsNothing); final field = tester.widget( find.byKey(const ValueKey('raw-patch-field')), ); expect(field.readOnly, isTrue); expect(field.maxLines, isNull); expect(field.controller?.text, multiHunkPatch); expect( find.ancestor( of: find.byKey(const ValueKey('raw-patch-field')), matching: find.byWidgetPredicate( (widget) => widget is SingleChildScrollView && widget.scrollDirection == Axis.horizontal, ), ), findsOneWidget, ); }); testWidgets('a Cupertino row wraps its recorded path instead of clipping it', ( tester, ) async { tester.view.physicalSize = const Size(320, 720); tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); const path = 'lib/features/changes/domain/a_rather_long_unified_patch_file_name.dart'; await tester.pumpWidget( cupertinoHost( ChangesViewer( steps: [ stepFixture( fileChanges: captureFixture( files: [fileChangeFixture(path: path, patch: multiHunkPatch)], ), ), ], ), ), ); final rich = tester .widgetList(find.byType(RichText)) .firstWhere((widget) => widget.text.toPlainText() == path); // CupertinoListTile caps a title at one ellipsized line; the row must lift // that cap so the filename stays visible. expect(rich.maxLines, isNull); expect(rich.overflow, TextOverflow.visible); expect(tester.getSize(find.text(path)).height, greaterThan(30)); expect(tester.takeException(), isNull); }); for (final cupertino in [false, true]) { testWidgets('a file opens as a page that steps to the previous and next ' 'file (${cupertino ? 'cupertino' : 'material'})', (tester) async { tester.view.physicalSize = const Size(390, 844); tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); final viewer = ChangesViewer( steps: [ stepFixture( fileChanges: captureFixture( files: [ fileChangeFixture( path: 'lib/first.dart', patch: multiHunkPatch, ), fileChangeFixture( path: 'lib/second.dart', patch: multiHunkPatch, ), ], ), ), ], ); await tester.pumpWidget( cupertino ? cupertinoHost(viewer) : materialHost(viewer), ); await tester.tap(find.byKey(const ValueKey('change-1-lib/first.dart'))); await tester.pumpAndSettle(); Finder control(String label) => cupertino ? find.bySemanticsLabel(label) : find.byTooltip(label); expect(find.byType(FileDiffPage), findsOneWidget); expect(find.text('first.dart'), findsOneWidget); expect(find.text('1 of 2'), findsOneWidget); // The pager sits at the bottom right. final pager = tester.getRect( find.byKey(const ValueKey('file-diff-pager')), ); expect(pager.bottom, greaterThan(844 - 120)); final next = tester.getRect(control('Next file')); expect(next.right, greaterThan(390 - 80)); await tester.tap(control('Next file')); await tester.pumpAndSettle(); expect(find.text('second.dart'), findsOneWidget); expect(find.text('2 of 2'), findsOneWidget); await tester.tap(control('Previous file')); await tester.pumpAndSettle(); expect(find.text('first.dart'), findsOneWidget); expect(tester.takeException(), isNull); }); } final diffFile = FileChange( path: 'lib/feature.dart', kind: ChangeKind.modified, patch: multiHunkPatch, ); for (final cupertino in [false, true]) { final host = cupertino ? 'Cupertino' : 'Material'; testWidgets('$host diff labels stay whole at 320x720 with 2x text', ( tester, ) async { tester.view.physicalSize = const Size(320, 720); tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); final opener = _diffDialogOpener(diffFile); await tester.pumpWidget( cupertino ? cupertinoHost(opener, scaler: const TextScaler.linear(2)) : materialHost(opener, scaler: const TextScaler.linear(2)), ); await tester.tap(find.text('Open diff')); await tester.pumpAndSettle(); // A segmented control cannot keep both fixed words at this size, so the // host's native menu carries them instead. "Rendered" used to break // mid-word and "Raw patch" used to be clipped to "Raw". expect(find.byType(SegmentedButton), findsNothing); expect( find.byType(CupertinoSlidingSegmentedControl), findsNothing, ); // Copy patch stays a standard action at intrinsic width; it must not // break its two words either. _expectWholeLabel(tester, find.text('Copy patch'), 'Copy patch'); await tester.tap( cupertino ? find.byIcon(CupertinoIcons.ellipsis) : find.byTooltip('Diff form'), ); await tester.pumpAndSettle(); final items = cupertino ? find.byType(CupertinoActionSheetAction) : find.byType(ListTile); expect(items, findsNWidgets(2)); for (final label in ['Rendered', 'Raw patch']) { _expectWholeLabel( tester, find.descendant(of: items, matching: find.text(label)), label, ); } await tester.tap( find.descendant(of: items, matching: find.text('Raw patch')), ); await tester.pumpAndSettle(); if (cupertino) { expect(find.byKey(const ValueKey('raw-patch-field')), findsOneWidget); } else { expect(find.text(multiHunkPatch), findsOneWidget); } expect(tester.takeException(), isNull); }); } for (final cupertino in [false, true]) { testWidgets( '${cupertino ? 'Cupertino' : 'Material'} wide host keeps the native ' 'segmented control', (tester) async { await tester.pumpWidget( cupertino ? cupertinoHost(_diffDialogOpener(diffFile)) : materialHost(_diffDialogOpener(diffFile)), ); await tester.tap(find.text('Open diff')); await tester.pumpAndSettle(); final control = cupertino ? find.byType(CupertinoSlidingSegmentedControl) : find.byType(SegmentedButton); expect(control, findsOneWidget); expect(tester.getSize(control).height, greaterThanOrEqualTo(44)); expect(find.text('Rendered'), findsOneWidget); expect(find.text('Raw patch'), findsOneWidget); expect(tester.takeException(), isNull); }, ); } for (final cupertino in [false, true]) { testWidgets('Escape closes the full-screen diff ' '(${cupertino ? 'cupertino' : 'material'})', (tester) async { final file = FileChange( path: 'lib/first.dart', kind: ChangeKind.modified, patch: _raggedPatch, ); final opener = _diffDialogOpener(file); await tester.pumpWidget( cupertino ? cupertinoHost(opener) : materialHost(opener), ); await tester.tap(find.text('Open diff')); await tester.pumpAndSettle(); expect(find.byType(FileDiffPage), findsOneWidget); await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.pumpAndSettle(); expect(find.byType(FileDiffPage), findsNothing); expect(find.text('Open diff'), findsOneWidget); }); } }