11
11
import com .onesignal .example .BlankActivity ;
12
12
13
13
import org .json .JSONArray ;
14
+ import org .json .JSONException ;
14
15
import org .json .JSONObject ;
15
16
import org .junit .Before ;
16
17
import org .junit .BeforeClass ;
40
41
@ RunWith (RobolectricTestRunner .class )
41
42
public class NotificationChannelManagerRunner {
42
43
44
+ private Context mContext ;
43
45
private BlankActivity blankActivity ;
44
-
46
+
47
+ NotificationChannelManagerRunner setContext (Context context ) {
48
+ mContext = context ;
49
+ return this ;
50
+ }
51
+
45
52
@ BeforeClass // Runs only once, before any tests
46
53
public static void setUpClass () throws Exception {
47
54
ShadowLog .stream = System .out ;
@@ -51,6 +58,7 @@ public static void setUpClass() throws Exception {
51
58
public void beforeEachTest () throws Exception {
52
59
ActivityController <BlankActivity > blankActivityController = Robolectric .buildActivity (BlankActivity .class ).create ();
53
60
blankActivity = blankActivityController .get ();
61
+ mContext = blankActivity ;
54
62
}
55
63
56
64
@ Test
@@ -177,38 +185,43 @@ public void processPayloadCreatingNewChannel() throws Exception {
177
185
assertNotNull (getChannel ("OS_id1" ));
178
186
}
179
187
180
-
181
188
@ Test
182
189
public void processPayloadDeletingOldChannel () throws Exception {
190
+ NotificationChannelManager_processChannelList (blankActivity , createBasicChannelListPayload ());
191
+ assertChannelsForBasicChannelList ();
192
+ }
193
+
194
+ JSONObject createBasicChannelListPayload () throws JSONException {
183
195
createChannel ("local_existing_id" );
184
196
createChannel ("OS_existing_id" );
185
197
186
198
JSONArray channelList = new JSONArray ();
187
199
JSONObject channelItem = new JSONObject ();
188
-
200
+
189
201
channelItem .put ("id" , "OS_id1" );
190
-
202
+
191
203
channelList .put (channelItem );
192
204
JSONObject payload = new JSONObject ();
193
205
payload .put ("chnl_lst" , channelList );
194
-
195
- NotificationChannelManager_processChannelList (blankActivity , payload );
196
-
206
+ return payload ;
207
+ }
208
+
209
+ void assertChannelsForBasicChannelList () {
197
210
assertNotNull (getChannel ("local_existing_id" ));
198
211
assertNull (getChannel ("OS_existing_id" ));
199
212
assertNotNull (getChannel ("OS_id1" ));
200
213
}
201
214
202
215
private NotificationChannel getChannel (String id ) {
203
216
NotificationManager notificationManager =
204
- (NotificationManager )blankActivity .getSystemService (Context .NOTIFICATION_SERVICE );
217
+ (NotificationManager )mContext .getSystemService (Context .NOTIFICATION_SERVICE );
205
218
return notificationManager .getNotificationChannel (id );
206
219
}
207
220
208
221
private void createChannel (String id ) {
209
222
NotificationManager notificationManager =
210
- (NotificationManager )blankActivity .getSystemService (Context .NOTIFICATION_SERVICE );
223
+ (NotificationManager )mContext .getSystemService (Context .NOTIFICATION_SERVICE );
211
224
NotificationChannel channel = new NotificationChannel (id ,"name" , NotificationManager .IMPORTANCE_DEFAULT );
212
225
notificationManager .createNotificationChannel (channel );
213
226
}
214
- }
227
+ }
0 commit comments