Skip to content

Commit 363d3a7

Browse files
committed
Fixed badge and button regression bugs
* Fixed 2.2.0 bug where if no launcher was set (no app icon) a null expectation would be thrown. * Fixed 2.4.0 bug where notification buttons would not work unless a NotificationExtender was setup.
1 parent 343ca04 commit 363d3a7

File tree

7 files changed

+99
-31
lines changed

7 files changed

+99
-31
lines changed
Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,35 @@
1+
/**
2+
* Modified MIT License
3+
*
4+
* Copyright 2016 OneSignal
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* 1. The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* 2. All copies of substantial portions of the Software may only be used in connection
17+
* with services provided by OneSignal.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
128
package com.onesignal;
229

330
import android.content.Context;
4-
import android.database.sqlite.SQLiteDatabase;
31+
32+
import com.onesignal.shortcutbadger.ShortcutBadger;
533

634
import org.robolectric.annotation.Implements;
735

@@ -10,7 +38,8 @@ public class ShadowBadgeCountUpdater {
1038

1139
public static int lastCount = 0;
1240

13-
private static void updateCount(int count, Context context) {
41+
static void updateCount(int count, Context context) {
1442
lastCount = count;
43+
ShortcutBadger.applyCount(context, count);
1544
}
1645
}

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

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import android.os.Bundle;
3939

4040
import com.onesignal.BuildConfig;
41+
import com.onesignal.GcmBroadcastReceiver;
4142
import com.onesignal.NotificationExtenderService;
4243
import com.onesignal.NotificationOpenedProcessor;
4344
import com.onesignal.OSNotificationDisplayedResult;
@@ -95,16 +96,6 @@ public void beforeEachTest() throws Exception {
9596
blankActivity = Robolectric.buildActivity(BlankActivity.class).create().get();
9697
blankActivity.getApplicationInfo().name = "UnitTestApp";
9798

98-
// Add our launcher Activity to the run time to simulate a real app.
99-
// getRobolectricPackageManager is null if run in BeforeClass for some reason.
100-
Intent launchIntent = new Intent(Intent.ACTION_MAIN);
101-
launchIntent.setPackage("com.onesignal.example");
102-
launchIntent.addCategory(Intent.CATEGORY_LAUNCHER);
103-
ResolveInfo resolveInfo = new ResolveInfo();
104-
resolveInfo.activityInfo = new ActivityInfo();
105-
resolveInfo.activityInfo.packageName = "com.onesignal.example";
106-
resolveInfo.activityInfo.name = "com.onesignal.example.BlankActivity";
107-
RuntimeEnvironment.getRobolectricPackageManager().addResolveInfoForIntent(launchIntent, resolveInfo);
10899
ShadowBadgeCountUpdater.lastCount = 0;
109100
}
110101

@@ -280,6 +271,32 @@ public void shouldGenerate2BasicGroupNotifications() throws Exception {
280271
cursor.close();
281272
}
282273

274+
275+
276+
@Test
277+
public void shouldSetButtonsCorrectly() throws Exception {
278+
Intent intentGcm = new Intent();
279+
intentGcm.setAction("com.google.android.c2dm.intent.RECEIVE");
280+
intentGcm.putExtra("message_type", "gcm");
281+
Bundle bundle = getBaseNotifBundle();
282+
bundle.putString("o", "[{\"n\": \"text1\", \"i\": \"id1\"}]");
283+
intentGcm.putExtras(bundle);
284+
285+
GcmBroadcastReceiver gcmBroadcastReceiver = new GcmBroadcastReceiver();
286+
try {
287+
gcmBroadcastReceiver.onReceive(blankActivity, intentGcm);
288+
} // setResultCode throws this error due to onReceive not designed to be called manually.
289+
catch (java.lang.IllegalStateException e) {}
290+
291+
Intent intent = Shadows.shadowOf(blankActivity).getNextStartedService();
292+
Assert.assertEquals("com.onesignal.GcmIntentService", intent.getComponent().getClassName());
293+
Assert.assertEquals(null, intent.getStringExtra("o"));
294+
JSONObject customJson = new JSONObject(intent.getStringExtra("custom"));
295+
JSONObject additionalData = new JSONObject((customJson.getString("a")));
296+
Assert.assertEquals("id1", additionalData.getJSONArray("actionButtons").getJSONObject(0).getString("id"));
297+
}
298+
299+
283300
@Test
284301
@Config(shadows = {ShadowOneSignal.class})
285302
public void shouldFireNotificationExtenderService() throws Exception {

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

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454

5555
import org.json.JSONArray;
5656
import org.json.JSONObject;
57+
import org.junit.AfterClass;
5758
import org.junit.Before;
5859
import org.junit.BeforeClass;
5960
import org.junit.Test;
@@ -83,11 +84,11 @@ public class MainOneSignalClassRunner {
8384

8485
private static final String ONESIGNAL_APP_ID = "b2f7f966-d8cc-11e4-bed1-df8f05be55ba";
8586
private static int testSleepTime;
86-
private Activity blankActivity;
87+
private static Activity blankActivity;
8788
private static String callBackUseId, getCallBackRegId;
8889
private static String notificationOpenedMessage;
8990
private static JSONObject lastGetTags;
90-
private ActivityController<BlankActivity> blankActivityController;
91+
private static ActivityController<BlankActivity> blankActivityController;
9192

9293
private static void GetIdsAvailable() {
9394
OneSignal.idsAvailable(new OneSignal.IdsAvailableHandler() {
@@ -108,6 +109,22 @@ public void tagsAvailable(JSONObject tags) {
108109
});
109110
}
110111

112+
private static void cleanUp() {
113+
callBackUseId = getCallBackRegId = null;
114+
StaticResetHelper.restSetStaticFields();
115+
116+
ShadowOneSignalRestClient.nextSuccessResponse = null;
117+
ShadowOneSignalRestClient.failNext = false;
118+
ShadowOneSignalRestClient.failAll = false;
119+
ShadowOneSignalRestClient.interruptibleDelayNext = false;
120+
ShadowOneSignalRestClient.networkCallCount = 0;
121+
ShadowOneSignalRestClient.testThread = Thread.currentThread();
122+
123+
ShadowPushRegistratorGPS.fail = false;
124+
notificationOpenedMessage = null;
125+
lastGetTags = null;
126+
}
127+
111128
@BeforeClass // Runs only once, before any tests
112129
public static void setUpClass() throws Exception {
113130
ShadowLog.stream = System.out;
@@ -121,25 +138,21 @@ public static void setUpClass() throws Exception {
121138
StaticResetHelper.saveStaticValues();
122139
}
123140

124-
@Before // Before each test
141+
@Before
125142
public void beforeEachTest() throws Exception {
126-
callBackUseId = getCallBackRegId = null;
127-
StaticResetHelper.restSetStaticFields();
128143
blankActivityController = Robolectric.buildActivity(BlankActivity.class).create();
129144
blankActivity = blankActivityController.get();
130145

131-
ShadowOneSignalRestClient.nextSuccessResponse = null;
132-
ShadowOneSignalRestClient.failNext = false;
133-
ShadowOneSignalRestClient.failAll = false;
134-
ShadowOneSignalRestClient.interruptibleDelayNext = false;
135-
ShadowOneSignalRestClient.networkCallCount = 0;
136-
ShadowOneSignalRestClient.testThread = Thread.currentThread();
146+
cleanUp();
147+
}
137148

138-
ShadowPushRegistratorGPS.fail = false;
139-
notificationOpenedMessage = null;
140-
lastGetTags = null;
149+
@AfterClass
150+
public static void afterEverything() {
151+
cleanUp();
141152
}
142153

154+
155+
143156
@Test
144157
public void testInitFromApplicationContext() throws Exception {
145158
// Application.onCreate

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ static void update(SQLiteDatabase readableDb, Context context) {
6868
}
6969
}
7070

71-
private static void updateCount(int count, Context context) {
71+
static void updateCount(int count, Context context) {
7272
ShortcutBadger.applyCount(context, count);
7373
}
7474
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,11 @@ private static void processOrderBroadcast(final Context context, Intent intent,
6969
if (processBundle(context, bundle))
7070
return;
7171

72-
ComponentName comp = new ComponentName(context.getPackageName(), GcmIntentService.class.getName());
73-
startWakefulService(context, intent.setComponent(comp));
72+
Intent intentForService = new Intent();
73+
intentForService.putExtras(bundle);
74+
intentForService.setComponent(new ComponentName(context.getPackageName(),
75+
GcmIntentService.class.getName()));
76+
startWakefulService(context, intentForService);
7477
}
7578

7679
static boolean processBundle(Context context, final Bundle bundle) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public void init() {
160160
private static TrackGooglePurchase trackGooglePurchase;
161161
private static TrackAmazonPurchase trackAmazonPurchase;
162162

163-
public static final String VERSION = "020402";
163+
public static final String VERSION = "020403";
164164

165165
private static AdvertisingIdentifierProvider mainAdIdProvider = new AdvertisingIdProviderGPS();
166166

OneSignalSDK/onesignal/src/main/java/com/onesignal/shortcutbadger/ShortcutBadger.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,13 @@ public static void removeCountOrThrow(Context context) throws ShortcutBadgeExcep
8989
}
9090

9191
private static void initBadger(Context context) {
92-
sComponentName = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName()).getComponent();
92+
Intent launcherIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
93+
94+
// App does not have a launcher intent. (AKA app drawer icon.)
95+
if (launcherIntent == null)
96+
return;
97+
98+
sComponentName = launcherIntent.getComponent();
9399

94100
Log.d(LOG_TAG, "Finding badger");
95101

0 commit comments

Comments
 (0)