Skip to content

Commit

Permalink
test(neon_framework): Generate static notification IDs on the fly to …
Browse files Browse the repository at this point in the history
…avoid mistakes

Signed-off-by: provokateurin <kate@provokateurin.de>
  • Loading branch information
provokateurin committed Oct 28, 2024
1 parent 71f3759 commit 1448989
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
14 changes: 8 additions & 6 deletions packages/neon_framework/lib/src/utils/push_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ class PushUtils {
for (final pushNotification in pushNotifications) {
if (pushNotification.type == 'background') {
if (pushNotification.subject.delete ?? false) {
await localNotificationsPlugin.cancel(_getNotificationID(accountID, pushNotification.subject.nid!));
await localNotificationsPlugin.cancel(getNotificationID(accountID, pushNotification.subject.nid!));
} else if (pushNotification.subject.deleteMultiple ?? false) {
await Future.wait([
for (final nid in pushNotification.subject.nids!)
localNotificationsPlugin.cancel(_getNotificationID(accountID, nid)),
localNotificationsPlugin.cancel(getNotificationID(accountID, nid)),
]);
} else if (pushNotification.subject.deleteAll ?? false) {
await localNotificationsPlugin.cancelAll();
Expand Down Expand Up @@ -171,7 +171,7 @@ class PushUtils {
final when = notification != null ? tz.TZDateTime.parse(tz.UTC, notification.datetime) : null;

await localNotificationsPlugin.show(
_getNotificationID(accountID, pushNotification.subject.nid!),
getNotificationID(accountID, pushNotification.subject.nid!),
message != null && appName != null ? '$appName: $title' : title,
message,
NotificationDetails(
Expand All @@ -197,7 +197,7 @@ class PushUtils {
if (defaultTargetPlatform == TargetPlatform.android) {
// Since we only support Android SDK 24+, we can just generate an empty summary notification as it will not be shown anyway.
await localNotificationsPlugin.show(
_getGroupSummaryID(accountID, appID),
getGroupSummaryID(accountID, appID),
null,
null,
NotificationDetails(
Expand Down Expand Up @@ -296,11 +296,13 @@ class PushUtils {
return ByteArrayAndroidBitmap(img.encodeBmp(img.decodePng(bytes!.buffer.asUint8List())!));
}

static int _getNotificationID(String accountID, int nid) {
@visibleForTesting
static int getNotificationID(String accountID, int nid) {
return sha256.convert(utf8.encode('$accountID$nid')).bytes.reduce((a, b) => a + b);
}

static int _getGroupSummaryID(String accountID, String app) {
@visibleForTesting
static int getGroupSummaryID(String accountID, String app) {
return sha256.convert(utf8.encode('$accountID$app')).bytes.reduce((a, b) => a + b);
}
}
18 changes: 9 additions & 9 deletions packages/neon_framework/test/push_utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ void main() {

await PushUtils.onMessage(_encryptPushNotifications(keypair, [payload]), account.id);

verify(() => localNotificationsPlatform.cancel(3811)).called(1);
verify(() => localNotificationsPlatform.cancel(PushUtils.getNotificationID(account.id, 1))).called(1);
});

test('Delete multiple', () async {
Expand All @@ -219,8 +219,8 @@ void main() {

await PushUtils.onMessage(_encryptPushNotifications(keypair, [payload]), account.id);

verify(() => localNotificationsPlatform.cancel(3811)).called(1);
verify(() => localNotificationsPlatform.cancel(4269)).called(1);
verify(() => localNotificationsPlatform.cancel(PushUtils.getNotificationID(account.id, 1))).called(1);
verify(() => localNotificationsPlatform.cancel(PushUtils.getNotificationID(account.id, 2))).called(1);
});

test('Delete all', () async {
Expand Down Expand Up @@ -295,7 +295,7 @@ void main() {

verify(
() => localNotificationsPlatform.show(
3811,
PushUtils.getNotificationID(account.id, 1),
'subject',
null,
notificationDetails: any(
Expand All @@ -320,7 +320,7 @@ void main() {
).called(1);
verify(
() => localNotificationsPlatform.show(
4082,
PushUtils.getGroupSummaryID(account.id, 'app'),
null,
null,
notificationDetails: any(
Expand Down Expand Up @@ -454,7 +454,7 @@ void main() {

verify(
() => localNotificationsPlatform.show(
3811,
PushUtils.getNotificationID(account.id, 1),
'Files: other subject',
'message',
notificationDetails: any(
Expand All @@ -479,7 +479,7 @@ void main() {
).called(1);
verify(
() => localNotificationsPlatform.show(
4405,
PushUtils.getGroupSummaryID(account.id, 'files'),
null,
null,
notificationDetails: any(
Expand Down Expand Up @@ -526,7 +526,7 @@ void main() {

verify(
() => localNotificationsPlatform.show(
3811,
PushUtils.getNotificationID(account.id, 1),
'subject',
null,
notificationDetails: any(
Expand All @@ -551,7 +551,7 @@ void main() {
).called(1);
verify(
() => localNotificationsPlatform.show(
4082,
PushUtils.getGroupSummaryID(account.id, 'app'),
null,
null,
notificationDetails: any(
Expand Down

0 comments on commit 1448989

Please sign in to comment.