Files
pipelines/app/test/features/pipelines/pipeline_graph_note_test.dart
T

153 lines
5.0 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/graph/widgets/definition_graph_view.dart';
import 'package:pipelines_app/features/pipelines/pipeline_models.dart';
import 'package:pipelines_app/features/pipelines/widgets/pipeline_graph_tab.dart';
import 'fixtures.dart';
/// The note is measured and clipped by this tab, so these cases pump it at the
/// tab slots the real panel gives it (390x312 and 660x168 are the measured
/// slots at 390x600 and 1100x400 on a 2x text scale).
Future<void> _pumpTab(
WidgetTester tester,
Size size, {
double scale = 2,
}) async {
await tester.pumpWidget(
MaterialApp(
builder: (context, child) => MediaQuery(
data: MediaQuery.of(
context,
).copyWith(textScaler: TextScaler.linear(scale)),
child: child!,
),
home: Scaffold(
body: Center(
child: SizedBox(
width: size.width,
height: size.height,
child: PipelineGraphTab(
detail: _detail(),
selectedStage: null,
onSelected: (_) {},
),
),
),
),
),
);
await tester.pumpAndSettle();
}
PipelineDetail _detail() => PipelineDetail.fromJson(
pipelineDetail(
id: 1,
name: 'Alpha review',
version: 2,
latestVersion: 2,
graph: reviewGraph(),
),
);
/// The line heights the rendered [Text] actually uses at the current scale.
List<double> _lineHeights(WidgetTester tester) {
final note = tester.widget<Text>(find.text(pipelineGraphNote));
final noteSize = tester.getSize(find.byKey(const Key('pipeline-graph-note')));
final painter = TextPainter(
text: TextSpan(text: pipelineGraphNote, style: note.style),
textDirection: TextDirection.ltr,
textScaler: MediaQuery.textScalerOf(
tester.element(find.byType(PipelineGraphTab)),
),
)..layout(maxWidth: noteSize.width - 32);
return [for (final line in painter.computeLineMetrics()) line.height];
}
void main() {
for (final (size, tabSlot, scale, withinShare) in <(Size, double, double, bool)>[
(const Size(390, 312), 312, 2, true),
// Two whole lines are kept even though a third of the slot is only 56 px,
// so the note stays readable and the canvas keeps over half.
(const Size(660, 168), 168, 2, false),
// A 4x line is taller than a third of this tab; one whole line is kept and
// the canvas still gets more than half.
(const Size(390, 160), 160, 4, false),
]) {
testWidgets('a clipped note keeps whole lines and a visible scrollbar at '
'${size.width.toInt()}x${tabSlot.toInt()} @${scale}x', (tester) async {
await _pumpTab(tester, size, scale: scale);
final noteSize = tester.getSize(
find.byKey(const Key('pipeline-graph-note')),
);
final canvas = tester.getSize(find.byType(DefinitionGraphView));
expect(noteSize.height, greaterThan(0));
expect(
find.byKey(const Key('pipeline-graph-note-scrollbar')),
findsOneWidget,
);
if (withinShare) {
expect(
noteSize.height,
lessThanOrEqualTo(tabSlot * pipelineGraphNoteShare + 0.5),
);
}
// The clipped viewport ends on a line boundary: no half-drawn row.
final heights = _lineHeights(tester);
var whole = 0.0;
for (final height in heights) {
if (whole + height > noteSize.height + 0.5) break;
whole += height;
}
expect(whole, greaterThan(0));
expect(noteSize.height, closeTo(whole, 0.5));
expect(
noteSize.height,
lessThan(heights.fold<double>(0, (a, b) => a + b)),
);
// The hidden tail is really scrollable: a standard scrollbar plus wheel
// and drag, not an inert clipped label.
final scrollbar = tester.widget<Scrollbar>(
find.byKey(const Key('pipeline-graph-note-scrollbar')),
);
final controller = scrollbar.controller!;
expect(controller.position.maxScrollExtent, greaterThan(0));
await tester.drag(
find.byKey(const Key('pipeline-graph-note-scroll')),
const Offset(0, -400),
);
await tester.pumpAndSettle();
expect(controller.position.pixels, greaterThan(0));
// The canvas keeps the rest of the slot throughout.
expect(canvas.height, greaterThan(0));
expect(canvas.height, greaterThanOrEqualTo(tabSlot * 0.5));
expect(tester.takeException(), isNull);
});
}
testWidgets('a note that fits keeps its natural height and no scrollbar', (
tester,
) async {
await _pumpTab(tester, const Size(1000, 700), scale: 1);
expect(
find.byKey(const Key('pipeline-graph-note-scrollbar')),
findsNothing,
);
expect(find.byKey(const Key('pipeline-graph-note-scroll')), findsNothing);
expect(
tester.getSize(find.byKey(const Key('pipeline-graph-note'))).height,
lessThan(120),
);
expect(
tester.getSize(find.byType(DefinitionGraphView)).height,
greaterThan(500),
);
expect(tester.takeException(), isNull);
});
}