import 'package:flutter/cupertino.dart'; import 'package:flutter/material.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'; void main() { test( 'patch typography preserves the native host style without overrides', () { const native = TextStyle(fontSize: 19, fontFamily: 'host-font'); expect(nativeDiffText(native), same(native)); }, ); 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), ); final context = tester.element(find.byType(Scaffold)); final add = diffLineColors(context, ChangeLineKind.add); final remove = diffLineColors(context, ChangeLineKind.remove); expect(add.background, isNull); expect(remove.background, isNull); expect(add.text, isNot(remove.text), reason: '$brightness text'); expect(add.text, Theme.of(context).colorScheme.primary); expect(remove.text, Theme.of(context).colorScheme.error); } }, ); 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)); }); 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('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); }); testWidgets('a Cupertino phone dialog is inset from the screen edge', ( tester, ) async { tester.view.physicalSize = const Size(390, 844); tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); await tester.pumpWidget( cupertinoHost( ChangesViewer( steps: [ stepFixture( fileChanges: captureFixture( files: [ fileChangeFixture( path: 'lib/feature.dart', patch: multiHunkPatch, ), ], ), ), ], ), ), ); await tester.tap(find.byKey(const ValueKey('change-1-lib/feature.dart'))); await tester.pumpAndSettle(); final rect = tester.getRect(find.byType(CupertinoPopupSurface)); expect(rect.width, lessThanOrEqualTo(390 - 24)); expect(rect.left, greaterThan(8)); expect(rect.right, lessThan(382)); expect(tester.takeException(), isNull); }); }