161 lines
5.5 KiB
Dart
161 lines
5.5 KiB
Dart
// This is a basic Flutter widget test.
|
||
//
|
||
// To perform an interaction with a widget in your test, use the WidgetTester
|
||
// utility in the flutter_test package. For example, you can send tap and scroll
|
||
// gestures. You can also use WidgetTester to find child widgets in the widget
|
||
// tree, read text, and verify that the values of widget properties are correct.
|
||
|
||
import 'package:yx_app_upgrade_flutter/app_upgrade_plugin.dart';
|
||
import 'package:yx_app_upgrade_flutter/app_upgrade_plugin_enhanced.dart';
|
||
import 'package:app_upgrade_plugin_example/main.dart';
|
||
import 'package:flutter/services.dart';
|
||
import 'package:flutter_test/flutter_test.dart';
|
||
|
||
class MockAppUpgradePluginEnhanced implements AppUpgradePluginEnhanced {
|
||
// 在这里 mock AppUpgradePluginEnhanced 的所有方法和属性
|
||
// ...
|
||
// 你可以根据测试需要返回特定的值
|
||
|
||
@override
|
||
void addDownloadCallback(DownloadCallback callback) {}
|
||
@override
|
||
void addErrorCallback(ErrorCallback callback) {}
|
||
@override
|
||
void addUpgradeCallback(UpgradeCallback callback) {}
|
||
@override
|
||
Future<bool> checkApkExists(String version, String? md5) async => false;
|
||
@override
|
||
Future<UpgradeInfo?> checkUpdateSmart(String url,
|
||
{Map<String, dynamic>? params, bool forceRefresh = false, Duration? cacheDuration}) async =>
|
||
null;
|
||
@override
|
||
Future<void> clearCache() async {}
|
||
@override
|
||
UpgradeConfig get config => UpgradeConfig.instance;
|
||
@override
|
||
void configure(
|
||
{bool? debugMode,
|
||
int? checkIntervalHours,
|
||
bool? autoCheck,
|
||
bool? wifiOnly,
|
||
int? downloadTimeout,
|
||
int? connectTimeout,
|
||
int? maxRetryCount,
|
||
bool? supportBreakpoint,
|
||
bool? verifyIntegrity,
|
||
VersionCompareStrategy? versionStrategy,
|
||
Map<String, String>? customHeaders}) {}
|
||
@override
|
||
void dispose() {}
|
||
@override
|
||
Future<String?> downloadApkSmart(String url,
|
||
{String? versionName,
|
||
Function(DownloadProgress p1)? onProgress,
|
||
String? savePath,
|
||
String? md5,
|
||
String? sha256,
|
||
bool resumeIfExists = true}) async =>
|
||
null;
|
||
@override
|
||
Future<Map<String, String>> getAppInfo() async => {};
|
||
@override
|
||
Future<Map<String, dynamic>> getCacheStats() async => {};
|
||
@override
|
||
DownloadTask? getCurrentDownloadTask() => null;
|
||
@override
|
||
Future<String?> getPlatformVersion() async => 'mock';
|
||
@override
|
||
Future<bool> goToAppStore(String url) async => false;
|
||
@override
|
||
Future<bool> installApkSmart(String filePath) async => false;
|
||
@override
|
||
NetworkStatus? get networkStatus => NetworkStatus(
|
||
type: NetworkType.wifi,
|
||
quality: NetworkQuality.good,
|
||
isConnected: true,
|
||
isMetered: false,
|
||
);
|
||
@override
|
||
void pauseDownload() {}
|
||
@override
|
||
void removeDownloadCallback(DownloadCallback callback) {}
|
||
@override
|
||
void removeErrorCallback(ErrorCallback callback) {}
|
||
@override
|
||
void removeUpgradeCallback(UpgradeCallback callback) {}
|
||
@override
|
||
Future<bool> resumeDownload() async => false;
|
||
@override
|
||
Future<bool> retryDownload() async => false;
|
||
@override
|
||
Future<void> refreshNetworkStatus() async {}
|
||
@override
|
||
VersionComparator get versionComparator => VersionComparator();
|
||
@override
|
||
void cancelDownload() {}
|
||
}
|
||
|
||
void main() {
|
||
// 关键修复:确保测试绑定已初始化
|
||
TestWidgetsFlutterBinding.ensureInitialized();
|
||
|
||
setUp(() {
|
||
// 为所有方法通道设置一个默认的 mock 处理器,防止未 mock 的调用抛出异常
|
||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler(
|
||
const MethodChannel('plugins.flutter.io/package_info'),
|
||
(MethodCall methodCall) async {
|
||
if (methodCall.method == 'getAll') {
|
||
return <String, dynamic>{
|
||
'appName': 'app_upgrade_plugin_example',
|
||
'packageName': 'com.example.app_upgrade_plugin_example',
|
||
'version': '1.0.0',
|
||
'buildNumber': '1',
|
||
};
|
||
}
|
||
return null;
|
||
},
|
||
);
|
||
|
||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler(
|
||
const MethodChannel('dexterous.com/flutter/local_notifications'),
|
||
(MethodCall methodCall) async => null, // 对通知插件的所有调用都返回 null
|
||
);
|
||
|
||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler(
|
||
const MethodChannel('dev.flutter.plugins/connectivity'),
|
||
(MethodCall methodCall) async {
|
||
if (methodCall.method == 'check') {
|
||
return 'wifi'; // 模拟网络状态为 WiFi
|
||
}
|
||
return null;
|
||
},
|
||
);
|
||
});
|
||
|
||
testWidgets('Verify Platform version', (WidgetTester tester) async {
|
||
// 使用 Mock enhanced plugin
|
||
AppUpgradeSimple.instance = AppUpgradeSimple.private(plugin: MockAppUpgradePluginEnhanced());
|
||
|
||
await tester.pumpWidget(const MyApp());
|
||
|
||
// 只验证应用能正常启动,不验证具体的UI内容
|
||
expect(find.byType(MyApp), findsOneWidget);
|
||
});
|
||
|
||
testWidgets('Verify Network Status Detection', (WidgetTester tester) async {
|
||
// 使用 Mock enhanced plugin
|
||
final mockPlugin = MockAppUpgradePluginEnhanced();
|
||
AppUpgradeSimple.instance = AppUpgradeSimple.private(plugin: mockPlugin);
|
||
|
||
await tester.pumpWidget(const MyApp());
|
||
|
||
// 验证网络状态检测功能
|
||
final networkStatus = mockPlugin.networkStatus;
|
||
|
||
expect(networkStatus, isNotNull);
|
||
expect(networkStatus!.isConnected, isTrue);
|
||
expect(networkStatus.type, equals(NetworkType.wifi));
|
||
expect(networkStatus.isMetered, isFalse);
|
||
});
|
||
}
|