Files

161 lines
4.9 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:remotedesk_control/app_preferences.dart';
import 'package:remotedesk_control/control_service.dart';
import 'package:remotedesk_control/main.dart';
class _MemoryPreferences implements AppPreferences {
_MemoryPreferences(this.settings);
AppSettings settings;
@override
Future<AppSettings> load() async => settings;
@override
Future<void> save(AppSettings settings) async {
this.settings = settings;
}
}
void main() {
test('saved host JSON round-trips control-service fields', () {
const host = SavedHost(
id: 'host-1',
name: 'Build server',
address: 'build.example:39500',
user: 'builder',
certificateSha256:
'0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef',
);
expect(SavedHost.fromJson(host.toJson()).toJson(), host.toJson());
});
testWidgets('renders devices and switches to session workspace', (
tester,
) async {
await tester.pumpWidget(const RemoteDeskControlApp());
await tester.pump();
await tester.pump();
expect(find.text('Devices'), findsOneWidget);
expect(find.text('New Linux host'), findsOneWidget);
expect(find.text('Connect desktop'), findsOneWidget);
await tester.tap(find.byIcon(Icons.monitor_heart_outlined));
await tester.pump();
expect(find.text('Sessions'), findsOneWidget);
expect(find.text('No sessions yet'), findsOneWidget);
await tester.pumpWidget(const SizedBox.shrink());
});
testWidgets('keeps the device workspace within a narrow window', (
tester,
) async {
tester.view.physicalSize = const Size(600, 800);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
await tester.pumpWidget(const RemoteDeskControlApp());
await tester.pump();
await tester.pump();
expect(find.byType(NavigationBar), findsOneWidget);
expect(find.text('New Linux host'), findsOneWidget);
expect(tester.takeException(), isNull);
await tester.pumpWidget(const SizedBox.shrink());
});
testWidgets('switches the workspace language from settings', (tester) async {
await tester.pumpWidget(const RemoteDeskControlApp());
await tester.pump();
await tester.pump();
await tester.tap(find.byIcon(Icons.tune_outlined));
await tester.pump();
expect(find.text('Language'), findsOneWidget);
await tester.tap(find.text('System default'));
await tester.pump();
await tester.tap(find.text('简体中文').last);
await tester.pump();
await tester.pump();
expect(find.text('设置'), findsOneWidget);
expect(find.text('语言'), findsOneWidget);
await tester.pumpWidget(const SizedBox.shrink());
});
testWidgets('restores and saves workspace preferences', (tester) async {
final preferences = _MemoryPreferences(
const AppSettings(localeCode: 'zh', closeToTray: false),
);
await tester.pumpWidget(RemoteDeskControlApp(preferences: preferences));
await tester.pump();
await tester.pump();
expect(find.text('设备'), findsOneWidget);
await tester.tap(find.byIcon(Icons.tune_outlined));
await tester.pump();
final closeToTray = tester.widget<SwitchListTile>(
find.widgetWithText(SwitchListTile, '关闭到系统托盘'),
);
expect(closeToTray.value, isFalse);
await tester.tap(find.text('关闭到系统托盘'));
await tester.pump();
expect(preferences.settings.closeToTray, isTrue);
await tester.pumpWidget(const SizedBox.shrink());
});
testWidgets('clears a host search with no matches', (tester) async {
await tester.pumpWidget(
RemoteDeskControlApp(
preferences: _MemoryPreferences(const AppSettings()),
),
);
await tester.pump();
await tester.pump();
await tester.enterText(find.byType(TextField).first, 'missing-host');
await tester.pump();
expect(find.text('No matching hosts'), findsOneWidget);
await tester.tap(find.byTooltip('Clear search'));
await tester.pump();
expect(find.text('No saved hosts'), findsOneWidget);
await tester.pumpWidget(const SizedBox.shrink());
});
testWidgets('confirms before discarding host edits', (tester) async {
await tester.pumpWidget(
RemoteDeskControlApp(
preferences: _MemoryPreferences(const AppSettings()),
),
);
await tester.pump();
await tester.pump();
await tester.enterText(
find.widgetWithText(TextFormField, 'Display name'),
'Unsaved host',
);
await tester.tap(find.byIcon(Icons.add));
await tester.pump();
expect(find.text('Discard unsaved changes?'), findsOneWidget);
await tester.tap(find.text('Keep editing'));
await tester.pump();
expect(find.text('Unsaved host'), findsOneWidget);
await tester.pumpWidget(const SizedBox.shrink());
});
}