Skip to content

Commit aa1f2f8

Browse files
committed
Fixed tests & improved project number format checking
1 parent 2c6a2b0 commit aa1f2f8

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

OneSignalSDK/onesignal/src/main/java/com/onesignal/PushRegistratorGPS.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,15 @@ public void registerForPush(Context context, String googleProjectNumber, Registe
5656
appContext = context;
5757
registeredHandler = callback;
5858

59-
if (googleProjectNumber == null || googleProjectNumber.equals("REMOTE")) {
59+
boolean isProjectNumberValidFormat;
60+
try {
61+
Float.parseFloat(googleProjectNumber);
62+
isProjectNumberValidFormat = true;
63+
} catch(Throwable t) {
64+
isProjectNumberValidFormat = false;
65+
}
66+
67+
if (!isProjectNumberValidFormat) {
6068
OneSignal.Log(OneSignal.LOG_LEVEL.ERROR, "Missing Google Project number!\nPlease enter a Google Project number / Sender ID on under App Settings > Android > Configuration on the OneSignal dashboard.");
6169
registeredHandler.complete(null, -6);
6270
return;

OneSignalSDK/unittest/src/test/java/com/test/onesignal/GenerateNotificationRunner.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -671,16 +671,18 @@ public void shouldSetButtonsCorrectly() throws Exception {
671671

672672
GcmBroadcastReceiver gcmBroadcastReceiver = new GcmBroadcastReceiver();
673673
gcmBroadcastReceiver.onReceive(blankActivity, intentGcm);
674-
675-
Intent intent = Shadows.shadowOf(blankActivity).getNextStartedService();
676-
Assert.assertEquals("com.onesignal.GcmIntentService", intent.getComponent().getClassName());
677-
678-
JSONObject jsonPayload = new JSONObject(intent.getStringExtra("json_payload"));
679-
680-
Assert.assertEquals(null, jsonPayload.optString("o", null));
681-
JSONObject customJson = new JSONObject(jsonPayload.optString("custom"));
682-
JSONObject additionalData = new JSONObject((customJson.getString("a")));
683-
Assert.assertEquals("id1", additionalData.getJSONArray("actionButtons").getJSONObject(0).getString("id"));
674+
675+
// Normal notifications should be generated right from the BroadcastReceiver
676+
// without creating a service.
677+
Assert.assertNull(Shadows.shadowOf(blankActivity).getNextStartedService());
678+
679+
Map<Integer, PostedNotification> postedNotifs = ShadowRoboNotificationManager.notifications;
680+
Iterator<Map.Entry<Integer, PostedNotification>> postedNotifsIterator = postedNotifs.entrySet().iterator();
681+
PostedNotification lastNotification = postedNotifsIterator.next().getValue();
682+
683+
Assert.assertEquals(1, lastNotification.notif.actions.length);
684+
String json_data = shadowOf(lastNotification.notif.actions[0].actionIntent).getSavedIntent().getStringExtra("onesignal_data");
685+
Assert.assertEquals("id1", new JSONObject(json_data).optString("actionSelected"));
684686
}
685687

686688
@Test

OneSignalSDK/unittest/src/test/java/com/test/onesignal/MainOneSignalClassRunner.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -556,11 +556,12 @@ public void testInvalidGoogleProjectNumberWithSuccessfulRegisterResponse() throw
556556
GetIdsAvailable();
557557
// A more real test would be "missing support library" but bad project number is an easier setup
558558
// and is testing the same logic.
559+
ShadowPushRegistratorGPS.fail = true;
559560
OneSignalInitWithBadProjectNum();
560561

561562
threadAndTaskWait();
562563
Robolectric.getForegroundThreadScheduler().runOneTask();
563-
Assert.assertEquals(-6, ShadowOneSignalRestClient.lastPost.getInt("notification_types"));
564+
Assert.assertEquals(-7, ShadowOneSignalRestClient.lastPost.getInt("notification_types"));
564565

565566
// Test that idsAvailable still fires
566567
Assert.assertEquals(ShadowOneSignalRestClient.testUserId, callBackUseId);
@@ -590,7 +591,7 @@ public void testInvalidGoogleProjectNumberWithFailedRegisterResponse() throws Ex
590591

591592
threadAndTaskWait();
592593
Robolectric.getForegroundThreadScheduler().runOneTask();
593-
Assert.assertEquals(-6, ShadowOneSignalRestClient.lastPost.getInt("notification_types"));
594+
Assert.assertEquals(-7, ShadowOneSignalRestClient.lastPost.getInt("notification_types"));
594595

595596
// Test that idsAvailable still fires
596597
Assert.assertEquals(ShadowOneSignalRestClient.testUserId, callBackUseId);
@@ -668,12 +669,14 @@ public void shouldSetSubscriptionCorrectlyEvenAfterFirstOneSignalRestInitFail()
668669

669670
@Test
670671
public void shouldUpdateNotificationTypesCorrectlyEvenWhenSetSubscriptionIsCalledInAnErrorState() throws Exception {
671-
OneSignalInitWithBadProjectNum();
672+
ShadowPushRegistratorGPS.fail = true;
673+
OneSignalInit();
672674
threadAndTaskWait();
673675
OneSignal.setSubscription(true);
674676

675677
// Restart app - Should send subscribe with on_session call.
676678
StaticResetHelper.restSetStaticFields();
679+
ShadowPushRegistratorGPS.fail = false;
677680
OneSignalInit();
678681
threadAndTaskWait();
679682
Assert.assertEquals(1, ShadowOneSignalRestClient.lastPost.getInt("notification_types"));

0 commit comments

Comments
 (0)