Files
pipelines/app/test/shell/windows/window_rebuild_test.dart
alan 8361322fe2 Keep floating window moves pixel-stable
Round fitted window frames to whole logical pixels and accumulate drag deltas from the gesture start. This prevents floating-point size noise from rebuilding window content while preserving fractional pointer movement and resizing behavior.
2026-11-11 03:28:34 +00:00

240 lines
8.6 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/shell/windows/window_entry.dart';
import 'package:pipelines_app/shell/windows/window_frame.dart';
import 'package:pipelines_app/shell/windows/window_kind.dart';
import 'package:pipelines_app/shell/windows/window_layer.dart';
import 'package:pipelines_app/shell/windows/window_manager.dart';
import 'window_test_harness.dart';
/// A probe kind whose content keeps its element when the window is retargeted,
/// so a rebuild is visible in the same state's counter.
class _StableProbeKind extends WindowKind {
@override
String get key => 'stable';
@override
bool get serverScoped => false;
@override
IconData get icon => Icons.science_outlined;
@override
String title(BuildContext context, WindowEntry entry) =>
'Probe ${entry.contentId}';
@override
Widget build(BuildContext context, WindowEntry entry) =>
ProbeContent(key: const ValueKey('stable-content'), id: entry.contentId);
}
void main() {
late WindowManager manager;
setUp(() => manager = probeManager());
Finder window(String title) => find.byWidgetPredicate(
(w) => w is WindowLayer && 'Probe ${w.entry.contentId}' == title,
);
Finder inWindow(String title, Finder what) =>
find.descendant(of: window(title), matching: what);
ProbeContentState content(WidgetTester tester, String id) =>
tester.state<ProbeContentState>(find.byKey(ValueKey('content-$id')));
Size windowSize(WidgetTester tester, String title) => tester.getSize(
find.ancestor(of: find.text(title), matching: find.byType(WindowFrame)),
);
/// The elements inside the probe windows that depend on the window's size:
/// its buttons and the scroll view around them.
Set<Element> contentElements() => {
for (final id in const ['a', 'b'])
for (final type in const [TextButton, ListView, Scrollable])
...find
.descendant(
of: find.byKey(ValueKey('content-$id')),
matching: find.byType(type),
)
.evaluate(),
};
Future<void> openTwo(WidgetTester tester) async {
manager.open('probe', 'a', frame: const Rect.fromLTWH(100, 80, 360, 420));
manager.open('probe', 'b', frame: const Rect.fromLTWH(600, 200, 360, 420));
await pumpProbe(tester, manager);
}
testWidgets('dragging a window moves it without rebuilding any content', (
tester,
) async {
await openTwo(tester);
final a = content(tester, 'a');
final b = content(tester, 'b');
final beforeA = a.builds;
final beforeB = b.builds;
final start = windowRect(tester, 'Probe a').topLeft;
final fieldStart = tester
.getRect(find.byKey(const ValueKey('a-field')))
.topLeft;
final gesture = await tester.startGesture(
tester.getCenter(
inWindow('Probe a', find.byKey(const ValueKey('window-title'))),
),
);
for (var i = 0; i < 8; i++) {
await gesture.moveBy(const Offset(12, 6));
await tester.pump();
expect(a.builds, beforeA, reason: 'dragged window rebuilt at move $i');
expect(b.builds, beforeB, reason: 'other window rebuilt at move $i');
}
await gesture.up();
await tester.pumpAndSettle();
// It really moved, and its content followed it.
expect(windowRect(tester, 'Probe a').topLeft, start + const Offset(96, 48));
expect(
tester.getRect(find.byKey(const ValueKey('a-field'))).topLeft,
fieldStart + const Offset(96, 48),
);
expect(content(tester, 'a').builds, beforeA);
expect(content(tester, 'b').builds, beforeB);
});
testWidgets('a fractional title drag keeps the window size exactly and does '
'not rebuild content', (tester) async {
await openTwo(tester);
final start = windowRect(tester, 'Probe a').topLeft;
final startSize = windowSize(tester, 'Probe a');
final watched = contentElements();
expect(watched, isNotEmpty);
final rebuilt = <Element>{};
final gesture = await tester.startGesture(
tester.getCenter(
inWindow('Probe a', find.byKey(const ValueKey('window-title'))),
),
);
// The press raises and focuses the window before the drag starts; only
// the drag's own rebuilds are watched.
await tester.pump();
debugOnRebuildDirtyWidget = (element, builtOnce) {
if (watched.contains(element)) rebuilt.add(element);
};
try {
final a = content(tester, 'a');
final b = content(tester, 'b');
final beforeA = a.builds;
final beforeB = b.builds;
for (var i = 0; i < 8; i++) {
await gesture.moveBy(const Offset(10.3, 4.7));
await tester.pump();
expect(
rebuilt.map((e) => e.widget.runtimeType).toSet(),
isEmpty,
reason: 'window content rebuilt at move $i',
);
expect(
windowSize(tester, 'Probe a'),
startSize,
reason: 'window size changed at move $i',
);
}
await gesture.up();
await tester.pumpAndSettle();
expect(rebuilt, isEmpty);
expect(a.builds, beforeA);
expect(b.builds, beforeB);
// Eight fractional moves, rounded to whole pixels, without drift.
final moved = windowRect(tester, 'Probe a').topLeft - start;
expect(moved.dx, closeTo(8 * 10.3, 1));
expect(moved.dy, closeTo(8 * 4.7, 1));
} finally {
debugOnRebuildDirtyWidget = null;
}
});
testWidgets('resizing by fractional deltas gives whole-pixel sizes', (
tester,
) async {
await openTwo(tester);
final a = content(tester, 'a');
final b = content(tester, 'b');
final beforeA = a.builds;
final beforeB = b.builds;
final startSize = windowSize(tester, 'Probe b');
final startBody = b.lastSize!;
final titleHeight = startSize.height - startBody.height;
final gesture = await tester.startGesture(
tester.getCenter(
inWindow('Probe b', find.byKey(const ValueKey('window-resize'))),
),
);
for (var i = 0; i < 5; i++) {
await gesture.moveBy(const Offset(-10.3, -6.7));
await tester.pump();
final size = windowSize(tester, 'Probe b');
expect(size.width, size.width.roundToDouble(), reason: 'move $i width');
expect(
size.height,
size.height.roundToDouble(),
reason: 'move $i height',
);
expect(a.builds, beforeA, reason: 'other window rebuilt at move $i');
}
await gesture.up();
await tester.pumpAndSettle();
expect(content(tester, 'a').builds, beforeA);
final state = content(tester, 'b');
expect(state.builds, greaterThan(beforeB));
final size = windowSize(tester, 'Probe b');
expect(size.width, closeTo(startSize.width - 5 * 10.3, 1));
expect(size.height, closeTo(startSize.height - 5 * 6.7, 1));
// The content saw the new, whole-pixel size.
expect(state.lastSize!.width, size.width);
expect(state.lastSize!.height, size.height - titleHeight);
});
testWidgets('resizing a window keeps another window and sees the new size', (
tester,
) async {
await openTwo(tester);
final a = content(tester, 'a');
final b = content(tester, 'b');
final beforeA = a.builds;
final beforeB = b.builds;
final beforeSize = b.lastSize!;
final gesture = await tester.startGesture(
tester.getCenter(
inWindow('Probe b', find.byKey(const ValueKey('window-resize'))),
),
);
for (var i = 0; i < 5; i++) {
await gesture.moveBy(const Offset(-16, -10));
await tester.pump();
expect(a.builds, beforeA, reason: 'other window rebuilt at move $i');
}
await gesture.up();
await tester.pumpAndSettle();
expect(content(tester, 'a').builds, beforeA);
final state = content(tester, 'b');
expect(state.builds, greaterThan(beforeB));
expect(state.lastSize!.width, beforeSize.width - 80);
expect(state.lastSize!.height, beforeSize.height - 50);
});
testWidgets('content still rebuilds when its window is retargeted', (
tester,
) async {
manager = WindowManager(kinds: WindowKinds()..register(_StableProbeKind()));
final entry = manager.open('stable', 'a');
await pumpProbe(tester, manager);
final state = tester.state<ProbeContentState>(
find.byKey(const ValueKey('stable-content')),
);
final before = state.builds;
manager.setContent(entry.id, 'c');
await tester.pumpAndSettle();
final after = tester.state<ProbeContentState>(
find.byKey(const ValueKey('stable-content')),
);
expect(after, same(state));
expect(after.builds, greaterThan(before));
expect(after.widget.id, 'c');
expect(find.text('Probe c'), findsOneWidget);
});
}