fix(app): retain native pipeline tabs at every text scale
This commit is contained in:
@@ -1,253 +1,96 @@
|
||||
import 'dart:math' as math;
|
||||
import 'dart:ui' show PointerDeviceKind;
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/rendering.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/pipelines/widgets/pipeline_tabs.dart';
|
||||
|
||||
const double _paneInset = 16;
|
||||
|
||||
/// A label that wrapped inside its word renders narrower than the width the
|
||||
/// paragraph needs on one line.
|
||||
void expectWholeLabel(WidgetTester tester, String label) {
|
||||
final paragraph = tester.renderObject<RenderParagraph>(find.text(label));
|
||||
expect(
|
||||
paragraph.size.width,
|
||||
greaterThanOrEqualTo(
|
||||
paragraph.getMaxIntrinsicWidth(double.infinity) - 0.01,
|
||||
),
|
||||
reason: '"$label" must not wrap inside the word',
|
||||
);
|
||||
}
|
||||
|
||||
double labelWidth(WidgetTester tester, String label) =>
|
||||
tester.getSize(find.text(label)).width;
|
||||
|
||||
Rect tabRect(WidgetTester tester, PipelineDetailTab tab) =>
|
||||
tester.getRect(find.byKey(ValueKey('pipeline-tab-${tab.name}')));
|
||||
|
||||
Future<void> pumpTabs(
|
||||
WidgetTester tester, {
|
||||
required double width,
|
||||
required double textScale,
|
||||
bool cupertino = false,
|
||||
ValueChanged<PipelineDetailTab>? onSelected,
|
||||
}) async {
|
||||
tester.view.physicalSize = Size(width, 640);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.resetPhysicalSize);
|
||||
addTearDown(tester.view.resetDevicePixelRatio);
|
||||
final tabs = StatefulBuilder(
|
||||
builder: (context, setState) => PipelineDetailTabs(
|
||||
selected: PipelineDetailTab.summary,
|
||||
onSelected: (tab) {
|
||||
onSelected?.call(tab);
|
||||
setState(() {});
|
||||
},
|
||||
),
|
||||
);
|
||||
final content = Align(alignment: Alignment.topLeft, child: tabs);
|
||||
Widget scaled(BuildContext context, Widget? child) => MediaQuery(
|
||||
data: MediaQuery.of(
|
||||
context,
|
||||
).copyWith(textScaler: TextScaler.linear(textScale)),
|
||||
child: child!,
|
||||
);
|
||||
await tester.pumpWidget(
|
||||
cupertino
|
||||
? CupertinoApp(
|
||||
builder: scaled,
|
||||
home: CupertinoPageScaffold(child: content),
|
||||
)
|
||||
: MaterialApp(
|
||||
builder: scaled,
|
||||
home: Scaffold(body: content),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
import 'package:pipelines_app/features/pipelines/widgets/pipeline_detail_styles.dart';
|
||||
|
||||
void main() {
|
||||
for (final (width, scale) in [
|
||||
(320.0, 1.5),
|
||||
(320.0, 2.0),
|
||||
(390.0, 2.0),
|
||||
(720.0, 2.0),
|
||||
]) {
|
||||
testWidgets(
|
||||
'Material tab labels stay whole at ${width.toInt()}px and ${scale}x text',
|
||||
(tester) async {
|
||||
await pumpTabs(tester, width: width, textScale: scale);
|
||||
expect(tester.takeException(), isNull);
|
||||
testWidgets('code-like values still use native host typography', (
|
||||
tester,
|
||||
) async {
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: Builder(
|
||||
builder: (context) {
|
||||
expect(
|
||||
pipelineValueStyle(context, monospace: true),
|
||||
Theme.of(context).textTheme.bodyMedium,
|
||||
);
|
||||
return const SizedBox.shrink();
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
for (final cupertino in [false, true]) {
|
||||
for (final width in [320.0, 390.0, 720.0, 1200.0]) {
|
||||
testWidgets('native tabs at $width, 2x, Cupertino=$cupertino', (
|
||||
tester,
|
||||
) async {
|
||||
tester.view.physicalSize = Size(width, 640);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.reset);
|
||||
PipelineDetailTab? selected;
|
||||
final body = Align(
|
||||
alignment: Alignment.topLeft,
|
||||
child: PipelineDetailTabs(
|
||||
selected: PipelineDetailTab.summary,
|
||||
onSelected: (value) => selected = value,
|
||||
),
|
||||
);
|
||||
Widget scaled(BuildContext context, Widget? child) => MediaQuery(
|
||||
data: MediaQuery.of(
|
||||
context,
|
||||
).copyWith(textScaler: TextScaler.linear(2)),
|
||||
child: child!,
|
||||
);
|
||||
await tester.pumpWidget(
|
||||
cupertino
|
||||
? CupertinoApp(
|
||||
builder: scaled,
|
||||
home: CupertinoPageScaffold(child: body),
|
||||
)
|
||||
: MaterialApp(
|
||||
builder: scaled,
|
||||
home: Scaffold(body: body),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(
|
||||
cupertino
|
||||
? find.byType(CupertinoSegmentedControl<PipelineDetailTab>)
|
||||
: find.byType(SegmentedButton<PipelineDetailTab>),
|
||||
findsOneWidget,
|
||||
);
|
||||
for (final tab in PipelineDetailTab.values) {
|
||||
expect(find.text(tab.label), findsOneWidget);
|
||||
expectWholeLabel(tester, tab.label);
|
||||
final paragraph = tester.renderObject<RenderParagraph>(
|
||||
find.text(tab.label),
|
||||
);
|
||||
expect(
|
||||
paragraph.size.width,
|
||||
greaterThanOrEqualTo(
|
||||
paragraph.getMaxIntrinsicWidth(double.infinity) - 0.01,
|
||||
),
|
||||
reason: 'Whole labels must not be squeezed inside a native segment',
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
await tester.drag(
|
||||
find.byType(SingleChildScrollView),
|
||||
const Offset(-1000, 0),
|
||||
kind: PointerDeviceKind.mouse,
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.text('Source'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(selected, PipelineDetailTab.source);
|
||||
expect(tester.takeException(), isNull);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
testWidgets('per-label segments size to their own labels', (tester) async {
|
||||
await pumpTabs(tester, width: 320, textScale: 2);
|
||||
final widths = {
|
||||
for (final tab in PipelineDetailTab.values)
|
||||
tab: tabRect(tester, tab).width,
|
||||
};
|
||||
expect(
|
||||
widths[PipelineDetailTab.summary],
|
||||
isNot(widths[PipelineDetailTab.graph]),
|
||||
);
|
||||
expect(
|
||||
widths[PipelineDetailTab.graph],
|
||||
isNot(widths[PipelineDetailTab.source]),
|
||||
);
|
||||
for (final tab in PipelineDetailTab.values) {
|
||||
expect(
|
||||
widths[tab],
|
||||
closeTo(labelWidth(tester, tab.label) + 2 * 8, 0.5),
|
||||
reason: '"${tab.label}" must not get the widest-label share',
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
testWidgets('a fitting tab row shows every label inside the pane', (
|
||||
tester,
|
||||
) async {
|
||||
await pumpTabs(tester, width: 320, textScale: 2);
|
||||
final contentWidth =
|
||||
tabRect(tester, PipelineDetailTab.source).right -
|
||||
tabRect(tester, PipelineDetailTab.summary).left;
|
||||
final width = contentWidth + 2 * _paneInset + 2;
|
||||
await pumpTabs(tester, width: width, textScale: 2);
|
||||
expect(tester.takeException(), isNull);
|
||||
for (final tab in PipelineDetailTab.values) {
|
||||
expectWholeLabel(tester, tab.label);
|
||||
final rect = tester.getRect(find.text(tab.label));
|
||||
expect(rect.left, greaterThanOrEqualTo(_paneInset - 0.5));
|
||||
expect(rect.right, lessThanOrEqualTo(width - _paneInset + 0.5));
|
||||
}
|
||||
expect(
|
||||
tester.getSize(find.byKey(const Key('pipeline-detail-tabs'))).width,
|
||||
closeTo(width - 2 * _paneInset, 0.5),
|
||||
reason: 'a row whose labels fit still fills the pane',
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('a tab row that overflows only scrolls by the labels\' need', (
|
||||
tester,
|
||||
) async {
|
||||
PipelineDetailTab? selected;
|
||||
await pumpTabs(tester, width: 320, textScale: 2);
|
||||
final contentWidth =
|
||||
tabRect(tester, PipelineDetailTab.source).right -
|
||||
tabRect(tester, PipelineDetailTab.summary).left;
|
||||
final width = contentWidth + 2 * _paneInset - 20;
|
||||
await pumpTabs(
|
||||
tester,
|
||||
width: width,
|
||||
textScale: 2,
|
||||
onSelected: (tab) => selected = tab,
|
||||
);
|
||||
final paneRight = width - _paneInset;
|
||||
final source = tester.getRect(find.text('Source'));
|
||||
expect(
|
||||
tabRect(tester, PipelineDetailTab.source).left,
|
||||
lessThan(paneRight),
|
||||
reason: 'the third tab must not be an empty cell',
|
||||
);
|
||||
expect(
|
||||
math.min(source.right, paneRight) - math.max(source.left, _paneInset),
|
||||
greaterThan(20),
|
||||
reason: 'a readable share of the last label stays in the pane',
|
||||
);
|
||||
expect(source.right, greaterThan(paneRight));
|
||||
|
||||
final row = find.byType(SingleChildScrollView);
|
||||
await tester.drag(
|
||||
row,
|
||||
const Offset(-400, 0),
|
||||
kind: PointerDeviceKind.mouse,
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(
|
||||
tester.getRect(find.text('Source')).right,
|
||||
lessThanOrEqualTo(paneRight + 0.01),
|
||||
reason: 'the last tab is fully visible after dragging',
|
||||
);
|
||||
await tester.tap(find.text('Source'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(selected, PipelineDetailTab.source);
|
||||
});
|
||||
|
||||
testWidgets('720px at 2x text shows all three labels at rest', (
|
||||
tester,
|
||||
) async {
|
||||
await pumpTabs(tester, width: 720, textScale: 2);
|
||||
for (final tab in PipelineDetailTab.values) {
|
||||
final rect = tester.getRect(find.text(tab.label));
|
||||
expect(rect.left, greaterThanOrEqualTo(_paneInset - 0.5));
|
||||
expect(rect.right, lessThanOrEqualTo(720 - _paneInset + 0.5));
|
||||
}
|
||||
});
|
||||
|
||||
testWidgets('a fitting tab row still fills the pane at 1x', (tester) async {
|
||||
await pumpTabs(tester, width: 400, textScale: 1);
|
||||
expect(
|
||||
tester.getSize(find.byKey(const Key('pipeline-detail-tabs'))).width,
|
||||
closeTo(368, 0.5),
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('the wide row keeps the platform Material control', (
|
||||
tester,
|
||||
) async {
|
||||
await pumpTabs(tester, width: 1200, textScale: 1);
|
||||
expect(find.byType(SegmentedButton<PipelineDetailTab>), findsOneWidget);
|
||||
expect(
|
||||
tester.getSize(find.byKey(const Key('pipeline-detail-tabs'))).width,
|
||||
closeTo(1168, 0.5),
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('Cupertino tab labels stay whole at 320px and 2x text', (
|
||||
tester,
|
||||
) async {
|
||||
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
|
||||
try {
|
||||
await pumpTabs(tester, width: 320, textScale: 2, cupertino: true);
|
||||
expect(tester.takeException(), isNull);
|
||||
// The labels do not fit an equal-share control here, so the per-label
|
||||
// row of standard Cupertino buttons is used.
|
||||
expect(
|
||||
find.byType(CupertinoButton),
|
||||
findsNWidgets(PipelineDetailTab.values.length),
|
||||
);
|
||||
for (final tab in PipelineDetailTab.values) {
|
||||
expectWholeLabel(tester, tab.label);
|
||||
}
|
||||
} finally {
|
||||
debugDefaultTargetPlatformOverride = null;
|
||||
}
|
||||
});
|
||||
|
||||
testWidgets('Cupertino keeps its native segmented control where it fits', (
|
||||
tester,
|
||||
) async {
|
||||
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
|
||||
try {
|
||||
await pumpTabs(tester, width: 1200, textScale: 1, cupertino: true);
|
||||
expect(
|
||||
find.byType(CupertinoSegmentedControl<PipelineDetailTab>),
|
||||
findsOneWidget,
|
||||
);
|
||||
for (final tab in PipelineDetailTab.values) {
|
||||
expectWholeLabel(tester, tab.label);
|
||||
}
|
||||
} finally {
|
||||
debugDefaultTargetPlatformOverride = null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user