From 58ef555eb3326eb479af054e20978709bddaf40a Mon Sep 17 00:00:00 2001 From: Haitao Pan Date: Tue, 24 Mar 2026 15:19:58 +0800 Subject: [PATCH] test: add pairing guide widget tests --- .../mobile/mobile_pairing_guide_suite.dart | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 test/features/mobile/mobile_pairing_guide_suite.dart diff --git a/test/features/mobile/mobile_pairing_guide_suite.dart b/test/features/mobile/mobile_pairing_guide_suite.dart new file mode 100644 index 00000000..b4be41e9 --- /dev/null +++ b/test/features/mobile/mobile_pairing_guide_suite.dart @@ -0,0 +1,97 @@ +@TestOn('vm') +library; + +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:xworkmate/features/mobile/mobile_gateway_pairing_guide_page.dart'; +import 'package:xworkmate/theme/app_theme.dart'; + +void main() { + TestWidgetsFlutterBinding.ensureInitialized(); + + const mobileScannerChannel = MethodChannel( + 'dev.steenbakker.mobile_scanner/scanner/method', + ); + + setUp(() { + TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger + .setMockMethodCallHandler(mobileScannerChannel, (call) async => null); + }); + + tearDown(() { + TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger + .setMockMethodCallHandler(mobileScannerChannel, null); + }); + + Future pumpGuide( + WidgetTester tester, { + required bool supportsQrScan, + required VoidCallback onManual, + required Future Function(String setupCode) onScanned, + }) async { + tester.view.devicePixelRatio = 1; + tester.view.physicalSize = const Size(430, 1200); + addTearDown(() { + tester.view.resetPhysicalSize(); + tester.view.resetDevicePixelRatio(); + }); + + await tester.pumpWidget( + MaterialApp( + theme: AppTheme.light(platform: TargetPlatform.iOS), + darkTheme: AppTheme.dark(platform: TargetPlatform.iOS), + home: MobileGatewayPairingGuidePage( + supportsQrScan: supportsQrScan, + onManualInput: onManual, + onScannedSetupCode: onScanned, + ), + ), + ); + await tester.pump(); + } + + testWidgets('guide shows xworkmate commands', (tester) async { + await pumpGuide( + tester, + supportsQrScan: true, + onManual: () {}, + onScanned: (_) async {}, + ); + + expect(find.text('配对网关'), findsOneWidget); + expect(find.text('npm install -g xworkmate'), findsOneWidget); + expect(find.text('xworkmate pair'), findsOneWidget); + expect( + find.byKey(const ValueKey('pairing-guide-install-command')), + findsOneWidget, + ); + }); + + testWidgets('manual button triggers callback', (tester) async { + var manualTapped = false; + await pumpGuide( + tester, + supportsQrScan: true, + onManual: () => manualTapped = true, + onScanned: (_) async {}, + ); + + await tester.tap(find.byKey(const ValueKey('pairing-guide-manual-button'))); + await tester.pumpAndSettle(); + expect(manualTapped, isTrue); + }); + + testWidgets('android scan button shows placeholder toast', (tester) async { + await pumpGuide( + tester, + supportsQrScan: false, + onManual: () {}, + onScanned: (_) async {}, + ); + + await tester.tap(find.byKey(const ValueKey('pairing-guide-scan-button'))); + await tester.pump(); + expect(find.textContaining('Android 扫码即将支持'), findsOneWidget); + }); +}