Skip to content

Commit

Permalink
Added unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
larryaasen committed Dec 13, 2023
1 parent cffaaf5 commit e829617
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 6 deletions.
2 changes: 1 addition & 1 deletion example/lib/main-gorouter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void main() async {
runApp(MyApp());
}

GoRouter routerConfig = GoRouter(
final routerConfig = GoRouter(
initialLocation: '/page2',
routes: [
GoRoute(
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@ dev_dependencies:
# From Dart Team: Mock library for Dart inspired by Mockito.
mockito: ^5.4.3
flutter_lints: ^2.0.3
go_router: ^7.1.1

flutter:
93 changes: 92 additions & 1 deletion test/device_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,28 @@ void main() {
await device.getOsVersionString(MockUpgraderOS(android: true)), isNull);
});

test('testing UpgraderDevice iOS', () async {
deviceInfo = _iosInfo(baseOS: '1.2.3');
final device = UpgraderDevice();
expect(await device.getOsVersionString(MockUpgraderOS(ios: true)), '1.2.3');

// Verify invalid OS version
deviceInfo = _iosInfo(baseOS: '.');
expect(await device.getOsVersionString(MockUpgraderOS(ios: true)), isNull);
});

// Note: this test causes exception in DeviceInfoPlugin
// test('testing UpgraderDevice Linux', () async {
// deviceInfo = _linuxInfo(baseOS: '1.2.3');
// final device = UpgraderDevice();
// expect(
// await device.getOsVersionString(MockUpgraderOS(linux: true)), '1.2.3');
// // Verify invalid OS version
// deviceInfo = _linuxInfo(baseOS: '.');
// expect(
// await device.getOsVersionString(MockUpgraderOS(linux: true)), isNull);
// });

test('testing UpgraderDevice macOS', () async {
deviceInfo = _macOSInfo(baseOS: '1.2.3');
final device = UpgraderDevice();
Expand All @@ -43,6 +65,23 @@ void main() {
expect(
await device.getOsVersionString(MockUpgraderOS(macos: true)), isNull);
});

test('testing UpgraderDevice Web', () async {
final device = UpgraderDevice();
expect(await device.getOsVersionString(MockUpgraderOS(web: true)), '0.0.0');
});

// Note: this test causes exception in DeviceInfoPlugin
// test('testing UpgraderDevice Windows', () async {
// deviceInfo = _windowsInfo(baseOS: '1.2.3');
// final device = UpgraderDevice();
// expect(await device.getOsVersionString(MockUpgraderOS(windows: true)),
// '1.2.3');
// // Verify invalid OS version
// deviceInfo = _windowsInfo(baseOS: '.');
// expect(
// await device.getOsVersionString(MockUpgraderOS(windows: true)), isNull);
// });
}

Map _androidInfo({required String baseOS}) {
Expand All @@ -61,7 +100,6 @@ Map _androidInfo({required String baseOS}) {
'sdkInt': 1,
'securityPatch': 'a',
};

final build = <String, dynamic>{
'board': 'a',
'bootloader': 'a',
Expand Down Expand Up @@ -89,6 +127,29 @@ Map _androidInfo({required String baseOS}) {
return build;
}

Map _iosInfo({required String baseOS}) {
final info = {
'systemVersion': baseOS,
};
return info;
}

Map _linuxInfo({required String baseOS}) {
return {
'name': 'a',
'version': baseOS,
'id': 'a',
'idLike': ['a'],
'versionCodename': 'a',
'versionId': 'a',
'prettyName': 'a',
'buildId': 'a',
'variant': 'a',
'variantId': 'a',
'machineId': 'a',
};
}

Map _macOSInfo({required String baseOS}) {
final info = {
'computerName': 'a',
Expand All @@ -106,6 +167,36 @@ Map _macOSInfo({required String baseOS}) {
'cpuFrequency': 0,
'systemGUID': 'a',
};
return info;
}

Map _windowsInfo({required String baseOS}) {
final info = {
'computerName': 'a',
'numberOfCores': 'a',
'systemMemoryInMegabytes': 'a',
'userName': 'a',
'majorVersion': 'a',
'minorVersion': 'a',
'buildNumber': 'a',
'platformId': 'a',
'csdVersion': 'a',
'servicePackMajor': 'a',
'servicePackMinor': 'a',
'suitMask': 'a',
'productType': 'a',
'reserved': 'a',
'buildLab': 'a',
'buildLabEx': 'a',
'digitalProductId': 'a',
'displayVersion': baseOS,
'editionId': 'a',
'installDate': 'a',
'productId': 'a',
'productName': 'a',
'registeredOwner': 'a',
'releaseId': 'a',
'deviceId': 'a',
};
return info;
}
76 changes: 72 additions & 4 deletions test/upgrader_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:go_router/go_router.dart';
import 'package:http/src/client.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:shared_preferences/shared_preferences.dart';
Expand Down Expand Up @@ -350,8 +351,11 @@ void main() {

testWidgets('test UpgradeAlert ignore', (WidgetTester tester) async {
final client = MockITunesSearchClient.setupMockClient();
final upgrader =
Upgrader(upgraderOS: MockUpgraderOS(ios: true), client: client);
final upgrader = Upgrader(
upgraderOS: MockUpgraderOS(ios: true),
client: client,
debugLogging: true,
);

upgrader.installPackageInfo(
packageInfo: PackageInfo(
Expand Down Expand Up @@ -402,8 +406,11 @@ void main() {

testWidgets('test UpgradeAlert later', (WidgetTester tester) async {
final client = MockITunesSearchClient.setupMockClient();
final upgrader =
Upgrader(upgraderOS: MockUpgraderOS(ios: true), client: client);
final upgrader = Upgrader(
upgraderOS: MockUpgraderOS(ios: true),
client: client,
debugLogging: true,
);

upgrader.installPackageInfo(
packageInfo: PackageInfo(
Expand Down Expand Up @@ -1120,6 +1127,67 @@ void main() {
expect(upgrader.appName(), isEmpty);
expect(upgrader.currentInstalledVersion(), isEmpty);
}, skip: false);

testWidgets('test UpgradeAlert with GoRouter', (WidgetTester tester) async {
final client = MockITunesSearchClient.setupMockClient();
final upgrader = Upgrader(
upgraderOS: MockUpgraderOS(ios: true),
client: client,
debugLogging: true,
);

upgrader.installPackageInfo(
packageInfo: PackageInfo(
appName: 'Upgrader',
packageName: 'com.larryaasen.upgrader',
version: '0.9.9',
buildNumber: '400'));

GoRouter routerConfig = GoRouter(
initialLocation: '/page2',
routes: [
GoRoute(
path: '/page1',
builder: (BuildContext context, GoRouterState state) => Scaffold(
appBar: AppBar(title: const Text('Upgrader GoRouter Example')),
body: const Center(child: Text('Checking... page1'))),
),
GoRoute(
path: '/page2',
builder: (BuildContext context, GoRouterState state) => Scaffold(
appBar: AppBar(title: const Text('Upgrader GoRouter Example')),
body: const Center(child: Text('Checking... page2')),
),
),
],
);

final router = MaterialApp.router(
title: 'Upgrader GoRouter Example',
routerConfig: routerConfig,
builder: (context, child) {
return UpgradeAlert(
upgrader: upgrader,
navigatorKey: routerConfig.routerDelegate.navigatorKey,
child: child ?? const Text('child'),
);
},
);

await tester.pumpWidget(router);

// Pump the UI so the upgrade card is displayed
await tester.pumpAndSettle();
await tester.pumpAndSettle();

expect(find.text('Upgrader GoRouter Example'), findsOneWidget);

expect(find.text('Update App?'), findsOneWidget);
expect(find.text('IGNORE'), findsOneWidget);
expect(find.text('LATER'), findsOneWidget);
expect(find.text('UPDATE NOW'), findsOneWidget);
expect(find.text('Release Notes'), findsOneWidget);
});
});

test('test UpgraderMessages', () {
Expand Down

0 comments on commit e829617

Please sign in to comment.