Merge commit '4273f9d801f20420be383be90e60cf6b30cfeb58' into pipelines/run-20261110-2013-surfacevisibility-signal-replaces-ticker-a247
This commit is contained in:
@@ -0,0 +1,222 @@
|
||||
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);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
import 'dart:convert';
|
||||
|
||||
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_store.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class _Kind extends WindowKind {
|
||||
_Kind(this.key, {this.serverScoped = false});
|
||||
@override
|
||||
final String key;
|
||||
@override
|
||||
final bool serverScoped;
|
||||
@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
|
||||
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;
|
||||
}
|
||||
|
||||
final _kinds = WindowKinds()
|
||||
..register(_Kind('chat', serverScoped: true))
|
||||
..register(_Kind('terminal'));
|
||||
|
||||
Map<String, Object?> _window(Map<String, Object?> overrides) => {
|
||||
'id': 'chat-1',
|
||||
'kind': 'chat',
|
||||
'content': 's1',
|
||||
'server': 'home',
|
||||
'group': 'chat',
|
||||
'frame': [10, 20, 400, 300],
|
||||
...overrides,
|
||||
}..removeWhere((key, value) => value == '#remove');
|
||||
|
||||
WindowsRecord? _decode(Object windows, {Object? frames, Object? hidden}) =>
|
||||
WindowsRecord.decode(
|
||||
jsonEncode({
|
||||
'version': 1,
|
||||
'windows': windows,
|
||||
'hidden_groups': ?hidden,
|
||||
'frames': ?frames,
|
||||
}),
|
||||
_kinds,
|
||||
);
|
||||
|
||||
void main() {
|
||||
test('a record round-trips', () {
|
||||
const record = WindowsRecord(
|
||||
windows: [
|
||||
WindowEntry(
|
||||
id: 'terminal-1',
|
||||
kind: 'terminal',
|
||||
contentId: 'terminal',
|
||||
frame: Rect.fromLTWH(80, 80, 720, 420),
|
||||
minimized: true,
|
||||
),
|
||||
WindowEntry(
|
||||
id: 'chat-2',
|
||||
kind: 'chat',
|
||||
contentId: 's2',
|
||||
serverKey: 'home',
|
||||
groupId: 'chats',
|
||||
frame: Rect.fromLTWH(1.5, 2, 480, 560),
|
||||
maximized: true,
|
||||
data: {'pane': 'p2'},
|
||||
),
|
||||
],
|
||||
hiddenGroups: {'terminal'},
|
||||
frames: {'chat\nhome\ns9': Rect.fromLTWH(5, 6, 700, 800)},
|
||||
);
|
||||
final decoded = WindowsRecord.decode(record.encode(_kinds), _kinds)!;
|
||||
expect(decoded.windows.map((w) => w.toString()), [
|
||||
for (final w in record.windows) w.toString(),
|
||||
]);
|
||||
expect(decoded.windows.last.groupId, 'chats');
|
||||
expect(decoded.windows.last.data, {'pane': 'p2'});
|
||||
expect(decoded.hiddenGroups, {'terminal'});
|
||||
expect(decoded.frames, record.frames);
|
||||
});
|
||||
|
||||
test('not a record', () {
|
||||
expect(WindowsRecord.decode('nope', _kinds), isNull);
|
||||
expect(WindowsRecord.decode('[]', _kinds), isNull);
|
||||
expect(WindowsRecord.decode('{"version":2,"windows":[]}', _kinds), isNull);
|
||||
expect(WindowsRecord.decode('{"version":1}', _kinds), isNull);
|
||||
expect(_decode(const [], frames: const []), isNull);
|
||||
expect(
|
||||
_decode([for (var i = 0; i <= windowStoreMaxWindows; i++) _window({})]),
|
||||
isNull,
|
||||
);
|
||||
expect(WindowsRecord.decode('x' * 262145, _kinds), isNull);
|
||||
});
|
||||
|
||||
test('a bad window is dropped and the rest kept', () {
|
||||
final good = _window({'id': 'chat-9', 'content': 's9'});
|
||||
for (final bad in [
|
||||
_window({'kind': 'run'}),
|
||||
_window({'id': ''}),
|
||||
_window({'content': ' padded '}),
|
||||
_window({'server': '#remove'}),
|
||||
_window({'kind': 'terminal'}),
|
||||
_window({'frame': [1, 2, 3]}),
|
||||
_window({'frame': [1, 2, 0, 5]}),
|
||||
_window({'frame': [1, 2, 'a', 5]}),
|
||||
_window({'data': 7}),
|
||||
'text',
|
||||
]) {
|
||||
final record = _decode([bad, good])!;
|
||||
expect(record.windows.single.id, 'chat-9', reason: '$bad');
|
||||
}
|
||||
// A repeated id keeps the first.
|
||||
expect(_decode([good, good])!.windows, hasLength(1));
|
||||
});
|
||||
|
||||
test('bad frames and groups are skipped', () {
|
||||
final record = _decode(
|
||||
const [],
|
||||
frames: {
|
||||
'a': [1, 2, 3, 4],
|
||||
'b': [1, 2, -3, 4],
|
||||
'c': 'x',
|
||||
},
|
||||
hidden: ['chat', '', 7],
|
||||
)!;
|
||||
expect(record.frames.keys, ['a']);
|
||||
expect(record.hiddenGroups, {'chat'});
|
||||
});
|
||||
|
||||
test('fit keeps a window inside the area at its minimum size', () {
|
||||
const min = Size(320, 240);
|
||||
expect(
|
||||
fitWindowFrame(
|
||||
const Rect.fromLTWH(900, -50, 100, 100),
|
||||
const Size(1000, 800),
|
||||
min,
|
||||
),
|
||||
const Rect.fromLTWH(680, 0, 320, 240),
|
||||
);
|
||||
expect(
|
||||
fitWindowFrame(
|
||||
const Rect.fromLTWH(10, 10, 2000, 2000),
|
||||
const Size(300, 200),
|
||||
min,
|
||||
),
|
||||
const Rect.fromLTWH(0, 0, 300, 200),
|
||||
);
|
||||
const entry = WindowEntry(
|
||||
id: 'w',
|
||||
kind: 'chat',
|
||||
contentId: 'c',
|
||||
frame: Rect.fromLTWH(10, 10, 400, 300),
|
||||
maximized: true,
|
||||
);
|
||||
expect(
|
||||
drawnWindowFrame(entry, const Size(1000, 800), min),
|
||||
const Rect.fromLTWH(8, 8, 984, 784),
|
||||
);
|
||||
expect(
|
||||
drawnWindowFrame(
|
||||
entry.copyWith(maximized: false),
|
||||
const Size(1000, 800),
|
||||
min,
|
||||
),
|
||||
entry.frame,
|
||||
);
|
||||
});
|
||||
|
||||
test('the shared-preferences store reads what it wrote', () async {
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
const store = SharedPreferencesWindowStore();
|
||||
expect(await store.read(), isNull);
|
||||
expect(await store.write('{"version":1,"windows":[]}'), isTrue);
|
||||
expect(await store.read(), '{"version":1,"windows":[]}');
|
||||
expect(
|
||||
(await SharedPreferences.getInstance()).getString(windowsPreferenceKey),
|
||||
isNotNull,
|
||||
);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user