From c23033e55736995e914ed3cc5af1c2a07f4bfba1 Mon Sep 17 00:00:00 2001 From: Tony Date: Wed, 6 May 2026 22:46:03 +0800 Subject: [PATCH] Update test --- .../plugin_integration_test.dart | 25 ++-- example/lib/main.dart | 130 +++++++++++++++--- example/test/widget_test.dart | 11 +- test/flutter_usb_serial_test.dart | 37 +++-- 4 files changed, 138 insertions(+), 65 deletions(-) diff --git a/example/integration_test/plugin_integration_test.dart b/example/integration_test/plugin_integration_test.dart index 23b28e4..0d6cb62 100644 --- a/example/integration_test/plugin_integration_test.dart +++ b/example/integration_test/plugin_integration_test.dart @@ -1,25 +1,16 @@ -// This is a basic Flutter integration test. -// -// Since integration tests run in a full Flutter application, they can interact -// with the host side of a plugin implementation, unlike Dart unit tests. -// -// For more information about Flutter integration tests, please see -// https://flutter.dev/to/integration-testing - - +import 'package:flutter/widgets.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:integration_test/integration_test.dart'; - -import 'package:flutter_usb_serial/flutter_usb_serial.dart'; +import 'package:flutter_usb_serial_example/main.dart' as app; void main() { IntegrationTestWidgetsFlutterBinding.ensureInitialized(); - testWidgets('getPlatformVersion test', (WidgetTester tester) async { - final FlutterUsbSerial plugin = FlutterUsbSerial(); - final String? version = await plugin.getPlatformVersion(); - // The version string depends on the host platform running the test, so - // just assert that some non-empty string is returned. - expect(version?.isNotEmpty, true); + testWidgets('renders device scanner UI', (WidgetTester tester) async { + await tester.pumpWidget(const app.MyApp()); + + expect(find.text('USB Serial Example'), findsOneWidget); + expect(find.text('Refresh devices'), findsOneWidget); + expect(find.byKey(const Key('status_text')), findsOneWidget); }); } diff --git a/example/lib/main.dart b/example/lib/main.dart index 4eeb6b9..d09b97e 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,6 +1,6 @@ -import 'package:flutter/material.dart'; import 'dart:async'; +import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_usb_serial/flutter_usb_serial.dart'; @@ -16,34 +16,67 @@ class MyApp extends StatefulWidget { } class _MyAppState extends State { - String _platformVersion = 'Unknown'; - final _flutterUsbSerialPlugin = FlutterUsbSerial(); + List _devices = const []; + String _status = 'Tap refresh to scan for attached USB serial devices.'; + bool _isLoading = false; @override void initState() { super.initState(); - initPlatformState(); + unawaited(_refreshDevices()); } - // Platform messages are asynchronous, so we initialize in an async method. - Future initPlatformState() async { - String platformVersion; - // Platform messages may fail, so we use a try/catch PlatformException. - // We also handle the message potentially returning null. + Future _refreshDevices() async { + setState(() { + _isLoading = true; + }); + try { - platformVersion = - await _flutterUsbSerialPlugin.getPlatformVersion() ?? 'Unknown platform version'; - } on PlatformException { - platformVersion = 'Failed to get platform version.'; + final devices = await FlutterUsbSerial.listDevices(); + if (!mounted) return; + setState(() { + _devices = devices; + _status = devices.isEmpty + ? 'No USB serial devices detected.' + : 'Found ${devices.length} USB serial device(s).'; + }); + } on MissingPluginException { + if (!mounted) return; + setState(() { + _devices = const []; + _status = 'USB serial support is only available on Android.'; + }); + } on PlatformException catch (error) { + if (!mounted) return; + setState(() { + _devices = const []; + _status = error.message ?? 'Failed to list USB serial devices.'; + }); + } finally { + if (mounted) { + setState(() { + _isLoading = false; + }); + } + } + } + + Future _requestPermission(UsbSerialDevice device) async { + String status; + try { + final granted = await FlutterUsbSerial.requestPermission(device); + status = granted + ? 'Permission granted for device ${device.deviceId}.' + : 'Permission denied for device ${device.deviceId}.'; + } on MissingPluginException { + status = 'USB serial support is only available on Android.'; + } on PlatformException catch (error) { + status = error.message ?? 'Failed to request USB permission.'; } - // If the widget was removed from the tree while the asynchronous platform - // message was in flight, we want to discard the reply rather than calling - // setState to update our non-existent appearance. if (!mounted) return; - setState(() { - _platformVersion = platformVersion; + _status = status; }); } @@ -52,10 +85,65 @@ class _MyAppState extends State { return MaterialApp( home: Scaffold( appBar: AppBar( - title: const Text('Plugin example app'), + title: const Text('USB Serial Example'), ), - body: Center( - child: Text('Running on: $_platformVersion\n'), + body: SafeArea( + child: Column( + children: [ + Padding( + padding: const EdgeInsets.all(16), + child: Row( + children: [ + Expanded( + child: Text( + _status, + key: const Key('status_text'), + ), + ), + const SizedBox(width: 12), + FilledButton.icon( + onPressed: _isLoading ? null : _refreshDevices, + icon: const Icon(Icons.refresh), + label: const Text('Refresh devices'), + ), + ], + ), + ), + Expanded( + child: _isLoading + ? const Center(child: CircularProgressIndicator()) + : _devices.isEmpty + ? const Center( + child: Text('Connect an Android USB serial device to see it here.'), + ) + : ListView.separated( + itemCount: _devices.length, + separatorBuilder: (_, __) => const Divider(height: 1), + itemBuilder: (context, index) { + final device = _devices[index]; + return ListTile( + leading: const Icon(Icons.usb), + title: Text( + device.deviceName.isEmpty + ? 'USB device ${device.deviceId}' + : device.deviceName, + ), + subtitle: Text( + 'VID 0x${device.vendorId.toRadixString(16).padLeft(4, '0')} ' + 'PID 0x${device.productId.toRadixString(16).padLeft(4, '0')}\n' + 'Manufacturer: ${device.manufacturerName.isEmpty ? 'Unknown' : device.manufacturerName}', + ), + isThreeLine: true, + trailing: OutlinedButton( + onPressed: () => _requestPermission(device), + child: const Text('Request permission'), + ), + ); + }, + ), + ), + ], + ), ), ), ); diff --git a/example/test/widget_test.dart b/example/test/widget_test.dart index 4fa5eb6..13826e1 100644 --- a/example/test/widget_test.dart +++ b/example/test/widget_test.dart @@ -15,13 +15,8 @@ void main() { // Build our app and trigger a frame. await tester.pumpWidget(const MyApp()); - // Verify that platform version is retrieved. - expect( - find.byWidgetPredicate( - (Widget widget) => widget is Text && - widget.data!.startsWith('Running on:'), - ), - findsOneWidget, - ); + expect(find.text('USB Serial Example'), findsOneWidget); + expect(find.byKey(const Key('status_text')), findsOneWidget); + expect(find.text('Refresh devices'), findsOneWidget); }); } diff --git a/test/flutter_usb_serial_test.dart b/test/flutter_usb_serial_test.dart index 238e4a5..e5daeeb 100644 --- a/test/flutter_usb_serial_test.dart +++ b/test/flutter_usb_serial_test.dart @@ -1,29 +1,28 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_usb_serial/flutter_usb_serial.dart'; -import 'package:flutter_usb_serial/flutter_usb_serial_platform_interface.dart'; import 'package:flutter_usb_serial/flutter_usb_serial_method_channel.dart'; -import 'package:plugin_platform_interface/plugin_platform_interface.dart'; - -class MockFlutterUsbSerialPlatform - with MockPlatformInterfaceMixin - implements FlutterUsbSerialPlatform { - - @override - Future getPlatformVersion() => Future.value('42'); -} void main() { - final FlutterUsbSerialPlatform initialPlatform = FlutterUsbSerialPlatform.instance; - - test('$MethodChannelFlutterUsbSerial is the default instance', () { - expect(initialPlatform, isInstanceOf()); + test('$MethodChannelFlutterUsbSerial can be constructed', () { + expect(MethodChannelFlutterUsbSerial(), isA()); }); - test('getPlatformVersion', () async { - FlutterUsbSerial flutterUsbSerialPlugin = FlutterUsbSerial(); - MockFlutterUsbSerialPlatform fakePlatform = MockFlutterUsbSerialPlatform(); - FlutterUsbSerialPlatform.instance = fakePlatform; + test('UsbSerialDevice maps fields from method channel payloads', () { + final device = UsbSerialDevice.fromMap({ + 'deviceId': 7, + 'vendorId': 0x1234, + 'productId': 0x5678, + 'deviceName': 'Adapter', + 'manufacturerName': 'Acme', + 'serialNumber': 'SN-42', + }); - expect(await flutterUsbSerialPlugin.getPlatformVersion(), '42'); + expect(device.deviceId, 7); + expect(device.vendorId, 0x1234); + expect(device.productId, 0x5678); + expect(device.deviceName, 'Adapter'); + expect(device.manufacturerName, 'Acme'); + expect(device.serialNumber, 'SN-42'); + expect(device.toMap()['deviceId'], 7); }); }