Files
openchamber-android-native/test/archived_sessions_test.dart
T

125 lines
4.3 KiB
Dart
Raw Normal View History

import 'dart:convert';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:openchamber/api/client.dart';
import 'package:openchamber/api/transport.dart';
import 'package:openchamber/l10n.dart';
import 'package:openchamber/screens/archived_sessions_screen.dart';
import 'package:openchamber/state.dart';
class _FakeTransport implements Transport {
final List<Map<String, dynamic>> archived;
_FakeTransport(this.archived);
@override
Future<TransportResponse> send(TransportRequest r) async {
Object? body;
if (r.path == '/api/session') {
body = {'data': archived};
} else if (r.method == 'PATCH') {
archived.removeWhere((s) => s['id'] == r.path.split('/').last);
body = {'data': <String, dynamic>{}};
} else if (r.method == 'DELETE') {
archived.removeWhere((s) => s['id'] == r.path.split('/').last);
body = <String, dynamic>{};
} else {
body = <String, dynamic>{};
}
return TransportResponse(
200,
const {'content-type': 'application/json'},
Stream.value(Uint8List.fromList(utf8.encode(jsonEncode(body)))),
);
}
@override
Future<TransportSocket> webSocket(String path,
{String? rawQuery, Map<String, String> headers = const {}}) async =>
throw UnimplementedError();
@override
void setBearerToken(String? token) {}
@override
void close() {}
}
Map<String, dynamic> _session(
String id, String title, String directory, int archived) =>
{
'id': id,
'title': title,
'time': {'created': archived - 1000, 'updated': archived - 500, 'archived': archived},
'location': {'directory': directory},
};
void main() {
setUp(() => L10n.instance.apply(AppLanguage.en));
tearDown(() => L10n.instance.apply(AppLanguage.zh));
testWidgets('archived sessions are listed, filtered and restored',
(tester) async {
final app = AppState();
addTearDown(app.dispose);
final transport = _FakeTransport([
_session('a1', 'Old alpha', '/home/me/alpha', 1000),
_session('b1', 'Old beta', '/home/me/beta', 2000),
]);
app.client = Client(Uri.parse('http://test'), transport: transport);
await tester.pumpWidget(MaterialApp(home: ArchivedSessionsScreen(app: app)));
await tester.pumpAndSettle();
expect(find.text('Archive'), findsOneWidget);
expect(find.text('2 archived sessions'), findsOneWidget);
expect(find.text('Old alpha'), findsOneWidget);
expect(find.text('Old beta'), findsOneWidget);
expect(find.text('All (2)'), findsOneWidget);
expect(find.text('alpha (1)'), findsOneWidget);
// A project chip narrows the list; "All" brings both back.
await tester.tap(find.text('alpha (1)'));
await tester.pumpAndSettle();
expect(find.text('Old alpha'), findsOneWidget);
expect(find.text('Old beta'), findsNothing);
expect(find.text('1 archived session'), findsOneWidget);
await tester.tap(find.text('All (2)'));
await tester.pumpAndSettle();
expect(find.text('Old beta'), findsOneWidget);
// Search across projects.
await tester.enterText(find.byType(TextField), 'beta');
await tester.pumpAndSettle();
expect(find.text('Old alpha'), findsNothing);
expect(find.text('Old beta'), findsOneWidget);
expect(find.text('1 archived session'), findsOneWidget);
// Restoring removes the row from the archive and from the list.
await tester.tap(find.byIcon(Icons.unarchive_outlined));
await tester.pumpAndSettle();
expect(find.text('Old beta'), findsNothing);
expect(find.text('Session restored'), findsOneWidget);
expect(app.archivedSessions.map((s) => s.id), ['a1']);
});
testWidgets('a project chip shows only that project', (tester) async {
final app = AppState();
addTearDown(app.dispose);
final transport = _FakeTransport([
_session('a1', 'Old alpha', '/home/me/alpha', 1000),
_session('b1', 'Old beta', '/home/me/beta', 2000),
]);
app.client = Client(Uri.parse('http://test'), transport: transport);
await tester.pumpWidget(MaterialApp(home: ArchivedSessionsScreen(app: app)));
await tester.pumpAndSettle();
await tester.tap(find.text('beta (1)'));
await tester.pumpAndSettle();
expect(find.text('Old beta'), findsOneWidget);
expect(find.text('Old alpha'), findsNothing);
});
}