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? 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 _window(Map 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, ); }); }