Files
MeshPort/apps/meshport_lab/test/profile_test.dart
T

140 lines
5.0 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:meshport_lab/features/lab_screen.dart';
void main() {
const channel = MethodChannel('meshport/system_vpn');
final binding = TestWidgetsFlutterBinding.ensureInitialized();
String? stored;
setUp(() {
stored = null;
binding.defaultBinaryMessenger.setMockMethodCallHandler(channel, (
call,
) async {
switch (call.method) {
case 'load_profile':
return stored;
case 'save_profile':
stored = call.arguments as String;
return null;
case 'delete_profile':
stored = null;
return null;
default:
throw StateError('Unexpected call: ${call.method}');
}
});
});
tearDown(
() =>
binding.defaultBinaryMessenger.setMockMethodCallHandler(channel, null),
);
Future<void> waitFor(WidgetTester tester, Finder finder) async {
for (var i = 0; i < 60 && finder.evaluate().isEmpty; i++) {
await tester.runAsync(
() => Future<void>.delayed(const Duration(milliseconds: 50)),
);
await tester.pump();
}
expect(finder, findsWidgets);
}
Future<void> openSettings(WidgetTester tester) async {
await tester.pumpWidget(const MaterialApp(home: LabScreen()));
await waitFor(tester, find.textContaining('原生库已加载'));
await waitFor(
tester,
find.byWidgetPredicate(
(widget) =>
widget is Text &&
(widget.data?.contains('已载入默认配置') == true ||
widget.data?.contains('已载入默认网络配置') == true ||
widget.data?.contains('已自动载入保存的网络配置') == true),
),
);
await tester.tap(find.text('设置').last);
await tester.pumpAndSettle();
}
Finder field(String label) => find.byWidgetPredicate(
(widget) => widget is TextField && widget.decoration?.labelText == label,
);
testWidgets(
'save without connecting, restore on restart, delete persisted profile',
(tester) async {
tester.view.physicalSize = const Size(1400, 1600);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
await openSettings(tester);
await tester.enterText(field('网络名称'), '我的网络');
await tester.enterText(field('网络密钥'), 'test-only-secret');
await tester.enterText(field('节点名称'), 'saved-node');
await tester.enterText(field('虚拟 IPv4'), '10.79.0.11/24');
await tester.tap(find.text('保存配置'));
await waitFor(tester, find.textContaining('网络配置已保存到 macOS 钥匙串'));
expect(stored, contains('我的网络'));
expect(find.text('尚未连接'), findsNothing); // The settings page is selected.
await tester.pumpWidget(const SizedBox.shrink());
await tester.runAsync(
() => Future<void>.delayed(const Duration(milliseconds: 100)),
);
await openSettings(tester);
await waitFor(tester, find.textContaining('已自动载入保存的网络配置'));
expect(tester.widget<TextField>(field('网络名称')).controller!.text, '我的网络');
expect(
tester.widget<TextField>(field('网络密钥')).controller!.text,
'test-only-secret',
);
expect(
tester.widget<TextField>(field('节点名称')).controller!.text,
'saved-node',
);
expect(
tester.widget<TextField>(field('虚拟 IPv4')).controller!.text,
'10.79.0.11/24',
);
await tester.tap(find.text('概览').last);
await tester.pumpAndSettle();
expect(find.text('尚未连接'), findsOneWidget);
await tester.tap(find.text('设置').last);
await tester.pumpAndSettle();
await tester.tap(find.text('删除已保存配置'));
await waitFor(tester, find.textContaining('已删除保存的配置'));
expect(stored, isNull);
await tester.pumpWidget(const SizedBox.shrink());
},
skip: !const bool.hasEnvironment('MESHPORT_LIBRARY'),
);
testWidgets(
'unchanged restored profile retains advanced TOML',
(tester) async {
stored = '''
hostname = "advanced-node"
ipv4 = "10.79.0.11/24"
listeners = []
stun_servers = []
stun_servers_v6 = []
[network_identity]
network_name = "advanced"
network_secret = "test-only"
[flags]
disable_udp_hole_punching = true
''';
final original = stored;
await openSettings(tester);
await waitFor(tester, find.textContaining('已自动载入保存的网络配置'));
await tester.ensureVisible(find.text('保存配置'));
await tester.tap(find.text('保存配置'));
await waitFor(tester, find.textContaining('网络配置已保存到 macOS 钥匙串'));
expect(stored, original);
await tester.pumpWidget(const SizedBox.shrink());
},
skip: !const bool.hasEnvironment('MESHPORT_LIBRARY'),
);
}