Skip to content

Commit 41ae9c9

Browse files
committed
Added test to ensure channels are synced on cold start
1 parent 2755fe2 commit 41ae9c9

File tree

4 files changed

+45
-12
lines changed

4 files changed

+45
-12
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1275,7 +1275,7 @@ static void saveUserId(String inUserId) {
12751275
final SharedPreferences prefs = getGcmPreferences(appContext);
12761276
SharedPreferences.Editor editor = prefs.edit();
12771277
editor.putString("GT_PLAYER_ID", userId);
1278-
editor.commit();
1278+
editor.apply();
12791279
}
12801280

12811281
static boolean getFilterOtherGCMReceivers(Context context) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ private void persistState() {
417417

418418
editor.putString("ONESIGNAL_USERSTATE_SYNCVALYES_" + persistKey, syncValues.toString());
419419
editor.putString("ONESIGNAL_USERSTATE_DEPENDVALYES_" + persistKey, dependValues.toString());
420-
editor.commit();
420+
editor.apply();
421421
}
422422
}
423423

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,26 @@ public void testNullProjectNumberSetsErrorType() throws Exception {
347347

348348
Assert.assertEquals(-6, ShadowOneSignalRestClient.lastPost.getInt("notification_types"));
349349
}
350+
351+
@Test
352+
@Config(shadows = {ShadowRoboNotificationManager.class}, sdk = 10000)
353+
public void testNotificationChannelListPayload() throws Exception {
354+
NotificationChannelManagerRunner testHelper = new NotificationChannelManagerRunner().setContext(blankActivity);
355+
JSONObject payloadList = testHelper.createBasicChannelListPayload();
356+
357+
JSONObject androidParams = testHelper.createBasicChannelListPayload();
358+
androidParams.put("awl_list", new JSONObject());
359+
// Get call will not return a Google project number if it hasn't been entered on the OneSignal dashboard.
360+
ShadowOneSignalRestClient.nextSuccessResponse = androidParams.toString();
361+
362+
// Don't fire the mock callback, it will be done from the real class.
363+
// ShadowPushRegistratorGPS.skipComplete = true;
364+
365+
OneSignal.init(blankActivity, null, ONESIGNAL_APP_ID);
366+
threadAndTaskWait();
367+
368+
testHelper.assertChannelsForBasicChannelList();
369+
}
350370

351371
@Test
352372
public void shouldCorrectlyRemoveOpenedHandlerAndFireMissedOnesWhenAddedBack() throws Exception {

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

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.onesignal.example.BlankActivity;
1212

1313
import org.json.JSONArray;
14+
import org.json.JSONException;
1415
import org.json.JSONObject;
1516
import org.junit.Before;
1617
import org.junit.BeforeClass;
@@ -40,8 +41,14 @@
4041
@RunWith(RobolectricTestRunner.class)
4142
public class NotificationChannelManagerRunner {
4243

44+
private Context mContext;
4345
private BlankActivity blankActivity;
44-
46+
47+
NotificationChannelManagerRunner setContext(Context context) {
48+
mContext = context;
49+
return this;
50+
}
51+
4552
@BeforeClass // Runs only once, before any tests
4653
public static void setUpClass() throws Exception {
4754
ShadowLog.stream = System.out;
@@ -51,6 +58,7 @@ public static void setUpClass() throws Exception {
5158
public void beforeEachTest() throws Exception {
5259
ActivityController<BlankActivity> blankActivityController = Robolectric.buildActivity(BlankActivity.class).create();
5360
blankActivity = blankActivityController.get();
61+
mContext = blankActivity;
5462
}
5563

5664
@Test
@@ -177,38 +185,43 @@ public void processPayloadCreatingNewChannel() throws Exception {
177185
assertNotNull(getChannel("OS_id1"));
178186
}
179187

180-
181188
@Test
182189
public void processPayloadDeletingOldChannel() throws Exception {
190+
NotificationChannelManager_processChannelList(blankActivity, createBasicChannelListPayload());
191+
assertChannelsForBasicChannelList();
192+
}
193+
194+
JSONObject createBasicChannelListPayload() throws JSONException {
183195
createChannel("local_existing_id");
184196
createChannel("OS_existing_id");
185197

186198
JSONArray channelList = new JSONArray();
187199
JSONObject channelItem = new JSONObject();
188-
200+
189201
channelItem.put("id", "OS_id1");
190-
202+
191203
channelList.put(channelItem);
192204
JSONObject payload = new JSONObject();
193205
payload.put("chnl_lst", channelList);
194-
195-
NotificationChannelManager_processChannelList(blankActivity, payload);
196-
206+
return payload;
207+
}
208+
209+
void assertChannelsForBasicChannelList() {
197210
assertNotNull(getChannel("local_existing_id"));
198211
assertNull(getChannel("OS_existing_id"));
199212
assertNotNull(getChannel("OS_id1"));
200213
}
201214

202215
private NotificationChannel getChannel(String id) {
203216
NotificationManager notificationManager =
204-
(NotificationManager)blankActivity.getSystemService(Context.NOTIFICATION_SERVICE);
217+
(NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
205218
return notificationManager.getNotificationChannel(id);
206219
}
207220

208221
private void createChannel(String id) {
209222
NotificationManager notificationManager =
210-
(NotificationManager)blankActivity.getSystemService(Context.NOTIFICATION_SERVICE);
223+
(NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
211224
NotificationChannel channel = new NotificationChannel(id,"name", NotificationManager.IMPORTANCE_DEFAULT);
212225
notificationManager.createNotificationChannel(channel);
213226
}
214-
}
227+
}

0 commit comments

Comments
 (0)