Files
pipelines/app/test/shell/windows/window_manager_test.dart
T
alan 715efab778 Add the window manager, kinds and store for floating windows
Phase 1.2 of docs/plans/floating-windows.md: an app-lifetime WindowManager
with ordered WindowEntry values, a WindowKind registry and a device-local
versioned WindowStore whose server-scoped entries carry their server key.
Nothing uses it yet.
2026-11-10 20:16:56 +00:00

223 lines
7.5 KiB
Dart

import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/shell/windows/window_entry.dart';
import 'package:pipelines_app/shell/windows/window_kind.dart';
import 'package:pipelines_app/shell/windows/window_manager.dart';
import 'package:pipelines_app/shell/windows/window_store.dart';
class _Kind extends WindowKind {
_Kind(this.key, {this.serverScoped = false});
@override
final String key;
@override
final bool serverScoped;
final closedIds = <String>[];
@override
IconData get icon => const IconData(0x41);
@override
String title(BuildContext context, WindowEntry entry) => entry.contentId;
@override
Widget build(BuildContext context, WindowEntry entry) => const SizedBox();
@override
void closed(WindowEntry entry) => closedIds.add(entry.id);
@override
Map<String, Object?>? decodeData(Object? raw) => switch (raw) {
null => const {},
{'pane': final String pane} => {'pane': pane},
_ => null,
};
@override
Object? encodeData(WindowEntry entry) =>
entry.data.isEmpty ? null : entry.data;
}
class _Store implements WindowStore {
String? value;
bool fail = false;
int writes = 0;
@override
Future<String?> read() async => value;
@override
Future<bool> write(String record) async {
writes++;
if (fail) return false;
value = record;
return true;
}
}
WindowKinds _kinds() => WindowKinds()
..register(_Kind('chat', serverScoped: true))
..register(_Kind('terminal'));
void main() {
late _Store store;
late WindowManager manager;
setUp(() {
store = _Store();
manager = WindowManager(kinds: _kinds(), store: store, server: 'home');
});
List<String> order() => [for (final w in manager.windows) w.contentId];
test('open adds on top, cascades siblings and reuses an open window', () {
final a = manager.open('chat', 'a');
final b = manager.open('chat', 'b');
final t = manager.open('terminal', 'terminal');
expect(order(), ['a', 'b', 'terminal']);
expect(b.frame, a.frame.shift(windowCascadeStep));
expect(t.serverKey, isNull);
expect(a.serverKey, 'home');
expect(manager.active?.id, t.id);
final again = manager.open('chat', 'a');
expect(again.id, a.id);
expect(order(), ['b', 'terminal', 'a']);
expect(() => manager.open('nope', 'x'), throwsStateError);
});
test('a server-scoped window needs a server', () {
manager.server = null;
expect(() => manager.open('chat', 'a'), throwsStateError);
expect(manager.open('terminal', 'terminal').serverKey, isNull);
});
test('raise, frames, maximise and minimise', () {
final a = manager.open('chat', 'a');
final b = manager.open('chat', 'b');
expect(manager.raise(a.id), isTrue);
expect(manager.raise(a.id), isFalse);
expect(order(), ['b', 'a']);
var notified = 0;
manager.addListener(() => notified++);
final writes = store.writes;
manager.setFrame(a.id, const Rect.fromLTWH(1, 2, 400, 300), save: false);
expect(manager.byId(a.id)!.frame, const Rect.fromLTWH(1, 2, 400, 300));
expect(store.writes, writes);
manager.setFrame(a.id, const Rect.fromLTWH(1, 2, 400, 300), save: false);
expect(notified, 1);
manager.commit();
expect(store.writes, writes + 1);
manager.setMaximized(a.id, true);
expect(manager.byId(a.id)!.maximized, isTrue);
manager.setMinimized(a.id, true);
expect(manager.isVisible(manager.byId(a.id)!), isFalse);
expect(manager.active?.id, b.id);
manager.show(a.id);
expect(manager.active?.id, a.id);
expect(manager.byId(a.id)!.minimized, isFalse);
});
test('a group hides and shows together and brings back minimised', () {
final a = manager.open('chat', 'a');
manager.open('chat', 'b');
final t = manager.open('terminal', 'terminal');
manager.setMinimized(a.id, true);
expect(manager.toggleGroup('chat'), isTrue);
expect(manager.groupShown('chat'), isFalse);
expect(manager.windows.where(manager.isVisible).single.id, t.id);
manager.toggleGroup('chat');
expect(manager.windows.where(manager.isVisible), hasLength(3));
expect(manager.byId(a.id)!.minimized, isFalse);
// Every window minimised: the chord shows them rather than hiding.
for (final w in manager.inGroup('chat').toList()) {
manager.setMinimized(w.id, true);
}
manager.toggleGroup('chat');
expect(manager.inGroup('chat').every(manager.isVisible), isTrue);
expect(manager.toggleGroup('run'), isFalse);
// Opening a window of a hidden group shows the group.
manager.setGroupShown('chat', false);
manager.open('chat', 'c');
expect(manager.groupShown('chat'), isTrue);
});
test('close remembers the frame for the content and tells the kind', () {
final a = manager.open('chat', 'a');
manager.setFrame(a.id, const Rect.fromLTWH(300, 200, 500, 400));
manager.close(a.id);
expect(manager.windows, isEmpty);
expect((manager.kinds['chat']! as _Kind).closedIds, [a.id]);
final again = manager.open('chat', 'a');
expect(again.frame, const Rect.fromLTWH(300, 200, 500, 400));
expect(again.id, isNot(a.id));
});
test('cycle walks the visible windows both ways', () {
manager.open('chat', 'a');
manager.open('chat', 'b');
manager.open('chat', 'c');
manager.cycle();
expect(order(), ['b', 'c', 'a']);
manager.cycle(-1);
expect(order(), ['a', 'b', 'c']);
manager.setMinimized(manager.find('chat', 'a')!.id, true);
manager.cycle();
expect(manager.active?.contentId, 'b');
});
test('windows are listed per server and kept for the others', () {
manager.open('chat', 'a');
manager.open('terminal', 'terminal');
manager.server = 'work';
expect(order(), ['terminal']);
final b = manager.open('chat', 'a');
expect(b.serverKey, 'work');
expect(manager.record.windows, hasLength(3));
manager.server = 'home';
expect(order(), ['a', 'terminal']);
expect(manager.byId(b.id), isNull);
});
test('saves every change and restores order, state and frames', () async {
final a = manager.open('chat', 'a', data: const {'pane': 'p1'});
manager.open('terminal', 'terminal');
final c = manager.open('chat', 'c');
manager.setMaximized(a.id, true);
manager.close(c.id);
manager.setGroupShown('terminal', false);
await pumpEventQueue();
final next = WindowManager(kinds: _kinds(), store: store, server: 'home');
await next.restore();
expect([for (final w in next.windows) w.contentId], ['a', 'terminal']);
final restored = next.find('chat', 'a')!;
expect(restored.maximized, isTrue);
expect(restored.data, {'pane': 'p1'});
expect(restored.serverKey, 'home');
expect(next.groupShown('terminal'), isFalse);
expect(next.open('chat', 'c').frame, c.frame);
// New ids never collide with restored ones.
expect(next.open('chat', 'd').id, isNot(restored.id));
});
test('a restore never overwrites a change made while it loaded', () async {
manager.open('chat', 'a');
await pumpEventQueue();
final next = WindowManager(kinds: _kinds(), store: store, server: 'home');
final restoring = next.restore();
next.open('chat', 'z');
await restoring;
expect([for (final w in next.windows) w.contentId], ['z']);
});
test('a failed save is reported and clears on the next good one', () async {
store.fail = true;
final a = manager.open('chat', 'a');
await pumpEventQueue();
expect(manager.saveFailed, isTrue);
store.fail = false;
manager.raise(a.id);
manager.setMinimized(a.id, true);
await pumpEventQueue();
expect(manager.saveFailed, isFalse);
});
}