109 lines
3.8 KiB
Dart
109 lines
3.8 KiB
Dart
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:app_upgrade_plugin/app_upgrade_simple.dart';
|
|
import 'package:app_upgrade_plugin/app_upgrade_plugin_platform_interface.dart';
|
|
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
|
|
|
|
class MockAppUpgradePluginPlatform extends AppUpgradePluginPlatform
|
|
with MockPlatformInterfaceMixin {
|
|
Map<String, String> _appInfo = {};
|
|
|
|
void setAppInfo(Map<String, String> info) {
|
|
_appInfo = info;
|
|
}
|
|
|
|
@override
|
|
Future<Map<String, String>> getAppInfo() async {
|
|
return _appInfo;
|
|
}
|
|
}
|
|
|
|
void main() {
|
|
TestWidgetsFlutterBinding.ensureInitialized();
|
|
|
|
late MockAppUpgradePluginPlatform mockPlatform;
|
|
|
|
setUp(() {
|
|
mockPlatform = MockAppUpgradePluginPlatform();
|
|
AppUpgradePluginPlatform.instance = mockPlatform;
|
|
});
|
|
|
|
group('AppUpgradeSimple', () {
|
|
test('isVersionUpdated returns true when target build number is higher', () async {
|
|
mockPlatform.setAppInfo({
|
|
'version': '1.0.0',
|
|
'buildNumber': '10',
|
|
'packageName': 'com.example.app',
|
|
});
|
|
|
|
// Current 10 < Target 11 => Updated (Target is newer)
|
|
// Wait, the method name is isVersionUpdated.
|
|
// Let's check logic:
|
|
// if (currentBuildNumber < targetBuildNumber) -> false (Current is older than target, so not updated TO target? Or implies target IS the update?)
|
|
// The method doc says: "Return true indicates current version has updated to target version or higher"
|
|
// So if current < target, it returns FALSE.
|
|
|
|
final result = await AppUpgradeSimple.instance.isVersionUpdated('1.0.0', 11);
|
|
expect(result, isFalse);
|
|
});
|
|
|
|
test('isVersionUpdated returns true when current build number is equal or higher', () async {
|
|
mockPlatform.setAppInfo({
|
|
'version': '1.0.0',
|
|
'buildNumber': '10',
|
|
'packageName': 'com.example.app',
|
|
});
|
|
|
|
// Current 10 >= Target 10 => True
|
|
expect(await AppUpgradeSimple.instance.isVersionUpdated('1.0.0', 10), isTrue);
|
|
|
|
// Current 10 > Target 9 => True
|
|
expect(await AppUpgradeSimple.instance.isVersionUpdated('1.0.0', 9), isTrue);
|
|
});
|
|
|
|
test('isVersionUpdated uses version name when build number is equal', () async {
|
|
mockPlatform.setAppInfo({
|
|
'version': '1.0.1',
|
|
'buildNumber': '10',
|
|
'packageName': 'com.example.app',
|
|
});
|
|
|
|
// Build numbers equal (10 == 10).
|
|
// Current 1.0.1 > Target 1.0.0 => True
|
|
expect(await AppUpgradeSimple.instance.isVersionUpdated('1.0.0', 10), isTrue);
|
|
|
|
// Current 1.0.1 < Target 1.0.2 => False
|
|
expect(await AppUpgradeSimple.instance.isVersionUpdated('1.0.2', 10), isFalse);
|
|
});
|
|
|
|
test('isVersionUpdated handles missing build numbers', () async {
|
|
mockPlatform.setAppInfo({
|
|
'version': '1.0.0',
|
|
'buildNumber': '0', // Default if missing parsing
|
|
});
|
|
|
|
// Target build number null/0 -> Compare versions
|
|
// Current 1.0.0 < Target 2.0.0 => False
|
|
expect(await AppUpgradeSimple.instance.isVersionUpdated('2.0.0', null), isFalse);
|
|
|
|
// Current 1.0.0 == Target 1.0.0 => True
|
|
expect(await AppUpgradeSimple.instance.isVersionUpdated('1.0.0', null), isTrue);
|
|
});
|
|
|
|
test('configure updates configuration', () {
|
|
final config = UpgradeConfig(
|
|
autoDownload: true,
|
|
autoInstall: true,
|
|
enableDebugLog: false,
|
|
);
|
|
|
|
// Since we can't easily inspect private _config, we might test behavior or side effects if possible.
|
|
// But here we just check if method runs without error.
|
|
// A more robust test would check if the config is actually used in checkUpdate,
|
|
// but checkUpdate involves UI (Dialog) which is hard to unit test without pumping widgets.
|
|
|
|
AppUpgradeSimple.instance.configure(config);
|
|
});
|
|
});
|
|
}
|
|
|