11package com .iterable .iterableapi ;
22
3+ import android .annotation .TargetApi ;
34import android .app .Application ;
4- import android .content .pm .ApplicationInfo ;
5+ import android .app .NotificationManager ;
6+ import android .app .Service ;
7+ import android .content .Context ;
8+ import android .os .AsyncTask ;
9+ import android .os .Build ;
510import android .os .Bundle ;
611import android .test .ApplicationTestCase ;
712
13+ import java .util .concurrent .CountDownLatch ;
14+ import java .util .concurrent .TimeUnit ;
15+
816/**
917 * <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
1018 */
1119public class IterableNotificationTest extends ApplicationTestCase <Application > {
1220 public IterableNotificationTest () {
1321 super (Application .class );
1422 }
15-
23+ Context appContext ;
24+ NotificationManager mNotificationManager ;
25+
26+ //EX: itbl_notif = "{\"itbl\":{\"templateId\":28,\"campaignId\":17,\"messageId\":\"85fbb5663ada4f40b5ae0d437e040fa6\",\"isGhostPush\":false},\"body\":\"Pushy\",\"badge\":2,\"sound\":\"me.wav\"}}";
27+
28+ String body = "my first push message" ;
29+ String sound = "me.wav" ;
30+ String itbl_ghost = "{\" templateId\" :1,\" campaignId\" :1,\" messageId\" :\" 11111111111111111111111111111111\" ,\" isGhostPush\" :true}" ;
31+ String itbl1 = "{\" templateId\" :1,\" campaignId\" :1,\" messageId\" :\" 11111111111111111111111111111111\" ,\" isGhostPush\" :false}" ;
32+ String itbl2 = "{\" templateId\" :2,\" campaignId\" :2,\" messageId\" :\" 22222222222222222222222222222222\" ,\" isGhostPush\" :false}}" ;
33+
34+ public void setUp () throws Exception {
35+ super .setUp ();
36+
37+ appContext = getContext ().getApplicationContext ();
38+ mNotificationManager = (NotificationManager )
39+ getContext ().getSystemService (Context .NOTIFICATION_SERVICE );
40+ mNotificationManager .cancelAll ();
41+ }
42+
1643 public void testEmptyBundle () throws Exception {
1744 IterableNotification iterableNotification = IterableNotification .createNotification (getContext (), new Bundle (), Application .class );
1845 assertTrue (iterableNotification .requestCode < System .currentTimeMillis ());
1946 }
2047
21- public void testGhostPushTrue () throws Exception {
22-
23- String itbl = "{\" templateId\" :25,\" campaignId\" :15,\" messageId\" :\" 6780f5b5cd394b80ba944b5c08d7f9a2\" ,\" isGhostPush\" :true}" ;
24-
48+ public void testGhostPush () throws Exception {
2549 Bundle notif1 = new Bundle ();
26- notif1 .putString (IterableConstants .ITERABLE_DATA_KEY , itbl );
27-
50+ notif1 .putString (IterableConstants .ITERABLE_DATA_KEY , itbl_ghost );
2851 IterableNotification iterableNotification = IterableNotification .createNotification (getContext (), notif1 , Application .class );
52+ IterableNotification .postNotificationOnDevice (appContext , iterableNotification );
2953 assertTrue (iterableNotification .iterableNotificationData .getIsGhostPush ());
54+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
55+ assertEquals (0 , mNotificationManager .getActiveNotifications ().length );
56+ }
3057 }
3158
32- public void testGhostPushFalse () throws Exception {
3359
34- String itbl = "{\" templateId\" :25,\" campaignId\" :15,\" messageId\" :\" 6780f5b5cd394b80ba944b5c08d7f9a2\" ,\" isGhostPush\" :false}" ;
60+ public void testNotificationText () throws Exception {
61+ Bundle notif = new Bundle ();
62+ notif .putString (IterableConstants .ITERABLE_DATA_KEY , itbl2 );
63+ notif .putString (IterableConstants .ITERABLE_DATA_BODY , body );
64+ notif .putString (IterableConstants .ITERABLE_DATA_SOUND , sound );
65+
66+ IterableNotification iterableNotification = IterableNotification .createNotification (getContext (), notif , Application .class );
67+ IterableNotification .postNotificationOnDevice (appContext , iterableNotification );
68+ assertEquals ("IterableAPI" , iterableNotification .mContentTitle );
69+ if (android .os .Build .VERSION .SDK_INT >= android .os .Build .VERSION_CODES .M ) {
70+ Bundle notificationExtras = mNotificationManager .getActiveNotifications ()[0 ].getNotification ().extras ;
71+ assertEquals ("IterableAPI" , notificationExtras .get ("android.title" ));
72+ assertEquals (body , notificationExtras .get ("android.text" ));
73+ }
74+ }
3575
76+ public void testMessage () throws Exception {
3677 Bundle notif1 = new Bundle ();
37- notif1 .putString (IterableConstants .ITERABLE_DATA_KEY , itbl );
78+ notif1 .putString (IterableConstants .ITERABLE_DATA_KEY , itbl1 );
3879
3980 IterableNotification iterableNotification = IterableNotification .createNotification (getContext (), notif1 , Application .class );
81+ IterableNotification .postNotificationOnDevice (appContext , iterableNotification );
4082 assertFalse (iterableNotification .iterableNotificationData .getIsGhostPush ());
41- }
83+ assertEquals ("11111111111111111111111111111111" , iterableNotification .iterableNotificationData .getMessageId ());
84+ assertEquals ("11111111111111111111111111111111" .hashCode (), iterableNotification .requestCode );
85+ assertEquals (1 , iterableNotification .iterableNotificationData .getCampaignId ());
86+ assertEquals (1 , iterableNotification .iterableNotificationData .getTemplateId ());
87+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
88+ assertEquals (1 , mNotificationManager .getActiveNotifications ().length );
89+ }
4290
43- public void testMessage () throws Exception {
44- String itbl = "{ \" templateId \" :25, \" campaignId \" :15, \" messageId \" : \" 6780f5b5cd394b80ba944b5c08d7f9a2 \" , \" isGhostPush \" :false}" ;
91+ Bundle notif2 = new Bundle ();
92+ notif2 . putString ( IterableConstants . ITERABLE_DATA_KEY , itbl2 ) ;
4593
46- Bundle notif1 = new Bundle ();
47- notif1 .putString (IterableConstants .ITERABLE_DATA_KEY , itbl );
94+ IterableNotification iterableNotification2 = IterableNotification .createNotification (getContext (), notif2 , Application .class );
95+ IterableNotification .postNotificationOnDevice (appContext , iterableNotification2 );
96+ assertFalse (iterableNotification2 .iterableNotificationData .getIsGhostPush ());
97+ assertEquals ("22222222222222222222222222222222" , iterableNotification2 .iterableNotificationData .getMessageId ());
98+ assertEquals ("22222222222222222222222222222222" .hashCode (), iterableNotification2 .requestCode );
99+ assertEquals (2 , iterableNotification2 .iterableNotificationData .getCampaignId ());
100+ assertEquals (2 , iterableNotification2 .iterableNotificationData .getTemplateId ());
101+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
102+ assertEquals (2 , mNotificationManager .getActiveNotifications ().length );
103+ }
48104
49- IterableNotification iterableNotification = IterableNotification .createNotification (getContext (), notif1 , Application .class );
50- assertEquals ("6780f5b5cd394b80ba944b5c08d7f9a2" , iterableNotification .iterableNotificationData .getMessageId ());
51- assertEquals ("6780f5b5cd394b80ba944b5c08d7f9a2" .hashCode (), iterableNotification .requestCode );
52105 }
53106}
0 commit comments