import 'dart:io'; 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; case 'save_proxy_profile': return null; default: throw StateError('Unexpected call: ${call.method}'); } }); }); tearDown( () => binding.defaultBinaryMessenger.setMockMethodCallHandler(channel, null), ); // The overview shows only the newest log lines; read the full log list. List logs(WidgetTester tester) => ((tester.state(find.byType(LabScreen, skipOffstage: false)) as dynamic) .logs as List) .cast(); Future waitForLog(WidgetTester tester, String text) async { for ( var i = 0; i < 100 && !logs(tester).any((line) => line.contains(text)); i++ ) { await tester.runAsync( () => Future.delayed(const Duration(milliseconds: 50)), ); await tester.pump(); } expect(logs(tester), contains(contains(text))); } Future openApp(WidgetTester tester) async { await tester.pumpWidget(const MaterialApp(home: LabScreen())); await waitForLog(tester, '原生库已加载'); await waitForLog(tester, '载入'); } Future openPage(WidgetTester tester, String tab, String entry) async { await tester.tap(find.text(tab).last); await tester.pumpAndSettle(); await tester.tap(find.text(entry)); await tester.pumpAndSettle(); } Future back(WidgetTester tester) async { await tester.pageBack(); 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 openApp(tester); await openPage(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 waitForLog(tester, '网络配置已保存到'); expect(stored, contains('我的网络')); await tester.pumpWidget(const SizedBox.shrink()); await tester.runAsync( () => Future.delayed(const Duration(milliseconds: 100)), ); await openApp(tester); await waitForLog(tester, '已自动载入保存的网络配置'); await openPage(tester, '组网', '基础配置'); String text(String label) => tester.widget(field(label)).controller!.text; expect(text('网络名称'), '我的网络'); expect(text('网络密钥'), 'test-only-secret'); expect(text('节点名称'), 'saved-node'); expect(text('虚拟 IPv4 / 前缀'), '10.79.0.11/24'); await back(tester); await openPage(tester, '设置', '默认配置'); await tester.tap(find.text('删除已保存配置')); await waitForLog(tester, '已删除保存的配置'); expect(stored, isNull); await tester.pumpWidget(const SizedBox.shrink()); }, skip: Platform.environment['MESHPORT_LIBRARY'] == null, ); 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 openApp(tester); await waitForLog(tester, '已自动载入保存的网络配置'); await openPage(tester, '设置', '默认配置'); await tester.tap(find.text('保存配置')); await waitForLog(tester, '网络配置已保存到'); expect(stored, original); await tester.pumpWidget(const SizedBox.shrink()); }, skip: Platform.environment['MESHPORT_LIBRARY'] == null, ); testWidgets( 'legacy profile is migrated and written back on restore', (tester) async { stored = ''' hostname = "legacy-node" ipv4 = "10.79.0.11/24" subnet_routes = ["192.168.21.0/24"] [network_identity] network_name = "legacy" network_secret = "test-only" '''; await openApp(tester); await waitForLog(tester, '已自动载入保存的网络配置'); expect(stored, isNot(contains('subnet_routes'))); expect(stored, contains('legacy-node')); await tester.pumpWidget(const SizedBox.shrink()); }, skip: Platform.environment['MESHPORT_LIBRARY'] == null, ); }