1
1
package com .iterable .iterableapi ;
2
2
3
+ import android .annotation .TargetApi ;
3
4
import 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 ;
5
10
import android .os .Bundle ;
6
11
import android .test .ApplicationTestCase ;
7
12
13
+ import java .util .concurrent .CountDownLatch ;
14
+ import java .util .concurrent .TimeUnit ;
15
+
8
16
/**
9
17
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
10
18
*/
11
19
public class IterableNotificationTest extends ApplicationTestCase <Application > {
12
20
public IterableNotificationTest () {
13
21
super (Application .class );
14
22
}
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
+
16
43
public void testEmptyBundle () throws Exception {
17
44
IterableNotification iterableNotification = IterableNotification .createNotification (getContext (), new Bundle (), Application .class );
18
45
assertTrue (iterableNotification .requestCode < System .currentTimeMillis ());
19
46
}
20
47
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 {
25
49
Bundle notif1 = new Bundle ();
26
- notif1 .putString (IterableConstants .ITERABLE_DATA_KEY , itbl );
27
-
50
+ notif1 .putString (IterableConstants .ITERABLE_DATA_KEY , itbl_ghost );
28
51
IterableNotification iterableNotification = IterableNotification .createNotification (getContext (), notif1 , Application .class );
52
+ IterableNotification .postNotificationOnDevice (appContext , iterableNotification );
29
53
assertTrue (iterableNotification .iterableNotificationData .getIsGhostPush ());
54
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
55
+ assertEquals (0 , mNotificationManager .getActiveNotifications ().length );
56
+ }
30
57
}
31
58
32
- public void testGhostPushFalse () throws Exception {
33
59
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
+ }
35
75
76
+ public void testMessage () throws Exception {
36
77
Bundle notif1 = new Bundle ();
37
- notif1 .putString (IterableConstants .ITERABLE_DATA_KEY , itbl );
78
+ notif1 .putString (IterableConstants .ITERABLE_DATA_KEY , itbl1 );
38
79
39
80
IterableNotification iterableNotification = IterableNotification .createNotification (getContext (), notif1 , Application .class );
81
+ IterableNotification .postNotificationOnDevice (appContext , iterableNotification );
40
82
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
+ }
42
90
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 ) ;
45
93
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
+ }
48
104
49
- IterableNotification iterableNotification = IterableNotification .createNotification (getContext (), notif1 , Application .class );
50
- assertEquals ("6780f5b5cd394b80ba944b5c08d7f9a2" , iterableNotification .iterableNotificationData .getMessageId ());
51
- assertEquals ("6780f5b5cd394b80ba944b5c08d7f9a2" .hashCode (), iterableNotification .requestCode );
52
105
}
53
106
}
0 commit comments