Skip to content

Commit

Permalink
[371] Added the parameter dialogKey to UpgraderAlert that is used…
Browse files Browse the repository at this point in the history
… by the alert dialog.
  • Loading branch information
larryaasen committed Jan 14, 2024
1 parent 2ee99be commit 66f3766
Show file tree
Hide file tree
Showing 8 changed files with 268 additions and 188 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 9.0.0-alpha.3

- [371] Added the parameter `dialogKey` to `UpgraderAlert` that is used by the alert dialog.

## 9.0.0-alpha.2

- Changed currentAppStoreListingURL, currentAppStoreVersion, and currentInstalledVersion from functions to getters.
Expand Down
1 change: 1 addition & 0 deletions example/lib/main-custom-alert.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class MyUpgradeAlertState extends UpgradeAlertState {
context: context,
builder: (BuildContext context) {
return AlertDialog(
key: key,
title: const Text('Update?'),
content: const SingleChildScrollView(
child: ListBody(
Expand Down
40 changes: 40 additions & 0 deletions example/lib/main_dialog_key.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) 2024 Larry Aasen. All rights reserved.

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:upgrader/upgrader.dart';

final dialogKey = GlobalKey(debugLabel: 'gloabl_upgrader_alert_dialog');

void main() async {
WidgetsFlutterBinding.ensureInitialized();

// Only call clearSavedSettings() during testing to reset internal values.
await Upgrader.clearSavedSettings(); // REMOVE this for release builds

final log =
() => print('$dialogKey mounted=${dialogKey.currentContext?.mounted}');
unawaited(Future.delayed(Duration(seconds: 0)).then((value) => log()));
unawaited(Future.delayed(Duration(seconds: 3)).then((value) => log()));
unawaited(Future.delayed(Duration(seconds: 4)).then((value) => log()));

runApp(MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({super.key});

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Upgrader Example',
home: UpgradeAlert(
dialogKey: dialogKey,
child: Scaffold(
appBar: AppBar(title: Text('Upgrader Example')),
body: Center(child: Text('Checking...')),
)),
);
}
}
7 changes: 6 additions & 1 deletion lib/src/upgrade_alert.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class UpgradeAlert extends StatefulWidget {
this.showLater = true,
this.showReleaseNotes = true,
this.cupertinoButtonTextStyle,
this.dialogKey,
this.navigatorKey,
this.child,
}) : upgrader = upgrader ?? Upgrader.sharedInstance;
Expand Down Expand Up @@ -71,6 +72,9 @@ class UpgradeAlert extends StatefulWidget {
/// [UpgradeDialogStyle.cupertino]. Optional.
final TextStyle? cupertinoButtonTextStyle;

/// The [Key] assigned to the dialog when it is shown.
final GlobalKey? dialogKey;

/// For use by the Router architecture as part of the RouterDelegate.
final GlobalKey<NavigatorState>? navigatorKey;

Expand Down Expand Up @@ -138,6 +142,7 @@ class UpgradeAlertState extends State<UpgradeAlert> {

Future.delayed(const Duration(milliseconds: 0), () {
showTheDialog(
key: widget.dialogKey ?? const Key('upgrader_alert_dialog'),
context: context,
title: appMessages.message(UpgraderMessage.title),
message: widget.upgrader.body(appMessages),
Expand Down Expand Up @@ -208,7 +213,7 @@ class UpgradeAlertState extends State<UpgradeAlert> {

/// Show the alert dialog.
void showTheDialog({
Key? key = const Key('upgrader_alert_dialog'),
Key? key,
required BuildContext context,
required String? title,
required String message,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: upgrader
description: Flutter package for prompting users to upgrade when there is a newer version of the app in the store.
version: 9.0.0-alpha.2
version: 9.0.0-alpha.3
homepage: https://github.com/larryaasen/upgrader

environment:
Expand Down
12 changes: 12 additions & 0 deletions test/test_utils.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) 2024 Larry Aasen. All rights reserved.

import 'package:flutter/material.dart';

Widget wrapper(Widget child) {
return MaterialApp(
home: Scaffold(
body: child,
appBar: AppBar(title: const Text('Upgrader test')),
),
);
}
199 changes: 199 additions & 0 deletions test/upgrade_card_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
// Copyright (c) 2024 Larry Aasen. All rights reserved.

import 'package:flutter_test/flutter_test.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:upgrader/upgrader.dart';

import 'mock_itunes_client.dart';
import 'test_utils.dart';

void main() {
TestWidgetsFlutterBinding.ensureInitialized();
late SharedPreferences preferences;

setUp(() async {
SharedPreferences.setMockInitialValues({});
preferences = await SharedPreferences.getInstance();
});

tearDown(() async {
await preferences.clear();
return true;
});
testWidgets('test UpgradeCard no update', (WidgetTester tester) async {
expect(Upgrader.sharedInstance.isTooSoon(), false);

final upgradeCard = wrapper(UpgradeCard());
await tester.pumpWidget(upgradeCard);

// Pump the UI
await tester.pumpAndSettle();

expect(find.text('IGNORE'), findsNothing);
expect(find.text('LATER'), findsNothing);
expect(find.text('UPDATE'), findsNothing);
expect(find.text('Release Notes'), findsNothing);
});

testWidgets('test UpgradeCard upgrade', (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'));
upgrader.initialize().then((value) {});
await tester.pumpAndSettle();

expect(upgrader.isTooSoon(), false);

var called = false;
var notCalled = true;
final upgradeCard = wrapper(
UpgradeCard(
upgrader: upgrader,
onUpdate: () {
called = true;
return true;
},
onIgnore: () {
notCalled = false;
return true;
},
onLater: () {
notCalled = false;
},
),
);
await tester.pumpWidget(upgradeCard);

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

expect(upgrader.messages, isNull);
upgrader.messages = UpgraderMessages();
expect(upgrader.messages, isNotNull);

expect(find.text(upgrader.messages!.releaseNotes), findsOneWidget);
expect(find.text(upgrader.releaseNotes!), findsOneWidget);
await tester.tap(find.text(upgrader.messages!.buttonTitleUpdate));
await tester.pumpAndSettle();

expect(called, true);
expect(notCalled, true);
expect(find.text(upgrader.messages!.buttonTitleUpdate), findsNothing);
}, skip: false);

testWidgets('test UpgradeCard ignore', (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'));
upgrader.initialize().then((value) {});
await tester.pumpAndSettle();

expect(upgrader.isTooSoon(), false);

var called = false;
var notCalled = true;
final upgradeCard = wrapper(
UpgradeCard(
upgrader: upgrader,
onUpdate: () {
notCalled = false;
return true;
},
onIgnore: () {
called = true;
return true;
},
onLater: () {
notCalled = false;
},
),
);
await tester.pumpWidget(upgradeCard);

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

expect(upgrader.messages, isNull);
upgrader.messages = UpgraderMessages();
expect(upgrader.messages, isNotNull);

await tester.tap(find.text(upgrader.messages!.buttonTitleIgnore));
await tester.pumpAndSettle();

expect(called, true);
expect(notCalled, true);
expect(find.text(upgrader.messages!.buttonTitleIgnore), findsNothing);
}, skip: false);

testWidgets('test UpgradeCard later', (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'));
upgrader.initialize().then((value) {});
await tester.pumpAndSettle();

expect(upgrader.isTooSoon(), false);

var called = false;
var notCalled = true;
final upgradeCard = wrapper(
UpgradeCard(
upgrader: upgrader,
onUpdate: () {
notCalled = false;
return true;
},
onIgnore: () {
notCalled = false;
return true;
},
onLater: () {
called = true;
},
),
);
await tester.pumpWidget(upgradeCard);

// Pump the UI so the upgrade card is displayed
await tester.pumpAndSettle(const Duration(milliseconds: 5000));

expect(upgrader.messages, isNull);
upgrader.messages = UpgraderMessages();
expect(upgrader.messages, isNotNull);

await tester.tap(find.text(upgrader.messages!.buttonTitleLater));
await tester.pumpAndSettle();

expect(called, true);
expect(notCalled, true);
expect(find.text(upgrader.messages!.buttonTitleLater), findsNothing);
}, skip: false);
}
Loading

0 comments on commit 66f3766

Please sign in to comment.