import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:yx_asr/yx_asr.dart'; /// 测试辅助工具类 class TestHelper { /// 创建测试用的 MaterialApp 包装器 static Widget createTestApp(Widget child) { return MaterialApp( home: Scaffold( body: child, ), ); } /// 等待异步操作完成 static Future waitForAsync(WidgetTester tester, {int milliseconds = 100}) async { await tester.pump(Duration(milliseconds: milliseconds)); await tester.pumpAndSettle(); } /// 验证组件是否存在 static void expectWidgetExists(Type widgetType) { expect(find.byType(widgetType), findsOneWidget); } /// 验证文本是否存在 static void expectTextExists(String text) { expect(find.text(text), findsOneWidget); } /// 验证图标是否存在 static void expectIconExists(IconData icon) { expect(find.byIcon(icon), findsOneWidget); } /// 创建测试用的语音识别结果 static SpeechRecognitionResult createTestResult({ String text = '测试结果', double confidence = 0.95, List alternatives = const [], }) { return SpeechRecognitionResult( recognizedWords: text, ); } /// 创建测试用的语音识别错误 static SpeechRecognitionError createTestError({ SpeechRecognitionErrorType type = SpeechRecognitionErrorType.service, String message = '测试错误', String? code, }) { return SpeechRecognitionError( errorType: type, errorMsg: message, errorCode: code, ); } /// 创建测试用的语音识别配置 static SpeechRecognitionConfig createTestConfig({ String? modelPath, String? localeId, int sampleRate = 16000, bool onDevice = false, Map customConfig = const {}, }) { return SpeechRecognitionConfig( modelPath: modelPath, localeId: localeId, sampleRate: sampleRate, onDevice: onDevice, customConfig: customConfig, ); } /// 模拟点击事件 static Future tapWidget(WidgetTester tester, Type widgetType) async { await tester.tap(find.byType(widgetType)); await tester.pump(); } /// 模拟长按事件 static Future longPressWidget( WidgetTester tester, Type widgetType) async { await tester.longPress(find.byType(widgetType)); await tester.pump(); } /// 验证回调是否被调用 static void verifyCallbackCalled(bool called, String callbackName) { expect(called, true, reason: '$callbackName 回调应该被调用'); } /// 验证回调没有被调用 static void verifyCallbackNotCalled(bool called, String callbackName) { expect(called, false, reason: '$callbackName 回调不应该被调用'); } /// 创建性能测试的计时器 static Stopwatch createStopwatch() { return Stopwatch()..start(); } /// 验证性能指标 static void verifyPerformance( Stopwatch stopwatch, int maxMilliseconds, String operationName, ) { stopwatch.stop(); expect( stopwatch.elapsedMilliseconds, lessThan(maxMilliseconds), reason: '$operationName 应该在 ${maxMilliseconds}ms 内完成,实际用时: ${stopwatch.elapsedMilliseconds}ms', ); } /// 打印性能信息 static void printPerformanceInfo( Stopwatch stopwatch, String operationName, { int? iterations, }) { stopwatch.stop(); if (iterations != null) { final averageTime = stopwatch.elapsedMilliseconds / iterations; print( '$operationName 平均用时: ${averageTime.toStringAsFixed(2)}ms (总计: ${stopwatch.elapsedMilliseconds}ms, 迭代: $iterations 次)'); } else { print('$operationName 用时: ${stopwatch.elapsedMilliseconds}ms'); } } /// 验证流是否正常工作 static Future verifyStreamWorks( Stream stream, T testValue, void Function(T) emitValue, ) async { bool received = false; T? receivedValue; final subscription = stream.listen((value) { received = true; receivedValue = value; }); emitValue(testValue); await Future.delayed(const Duration(milliseconds: 10)); expect(received, true, reason: '流应该接收到值'); expect(receivedValue, equals(testValue), reason: '接收到的值应该匹配发送的值'); await subscription.cancel(); } /// 创建测试用的颜色 static const Color testIdleColor = Colors.blue; static const Color testRecordingColor = Colors.red; static const Color testDisabledColor = Colors.grey; /// 测试用的常量 static const String testModelPath = 'test/models'; static const String testLocaleId = 'zh-CN'; static const double testButtonSize = 100.0; static const String testTooltip = '测试提示'; } /// 测试用的扩展方法 extension WidgetTesterExtensions on WidgetTester { /// 等待并结算所有动画 Future pumpAndSettleWithTimeout( [Duration timeout = const Duration(seconds: 10)]) async { await pumpAndSettle(timeout); } /// 查找并点击指定类型的组件 Future tapByType(Type widgetType) async { await tap(find.byType(widgetType)); await pump(); } /// 查找并长按指定类型的组件 Future longPressByType(Type widgetType) async { await longPress(find.byType(widgetType)); await pump(); } /// 验证组件存在 void expectWidgetExists(Type widgetType) { expect(find.byType(widgetType), findsOneWidget); } /// 验证组件不存在 void expectWidgetNotExists(Type widgetType) { expect(find.byType(widgetType), findsNothing); } /// 验证文本存在 void expectTextExists(String text) { expect(find.text(text), findsOneWidget); } /// 验证图标存在 void expectIconExists(IconData icon) { expect(find.byIcon(icon), findsOneWidget); } }