1
1
using System ;
2
+ using System . Collections ;
3
+ using System . Collections . Generic ;
2
4
using UnityEngine ;
3
5
using UnityEngine . TestTools ;
4
6
using NUnit . Framework ;
5
- using System . Collections ;
6
7
using Unity . Notifications . iOS ;
7
8
#if UNITY_EDITOR
9
+ using System . Diagnostics ;
10
+ using System . IO ;
8
11
using Unity . Notifications ;
9
12
using UnityEditor ;
13
+ using UnityEditor . TestTools . TestRunner . Api ;
14
+
15
+
16
+ [ InitializeOnLoad ]
17
+ class SendPushNotifications : ICallbacks
18
+ {
19
+ static SendPushNotifications ( )
20
+ {
21
+ var api = ScriptableObject . CreateInstance < TestRunnerApi > ( ) ;
22
+ api . RegisterCallbacks ( new SendPushNotifications ( ) ) ;
23
+ }
24
+
25
+ readonly Dictionary < string , string > pushNotifications = new Dictionary < string , string > ( )
26
+ {
27
+ { "PushNotification_WithSimpleString_IsReceived" , "\" Test data\" " } ,
28
+ { "PushNotification_WithInteger_IsReceived" , "15" } ,
29
+ { "PushNotification_WithBool_IsReceived" , "true" } ,
30
+ { "PushNotification_WithJSON_IsReceived" , "{ \" item1\" : 3, \" item2\" : \" value2\" }" } ,
31
+ { "PushNotification_CustomData_IsReceived" , "\" test\" , \" CustomKey\" : 25" } ,
32
+ } ;
33
+
34
+ readonly string pushTemplate = @"{
35
+ ""aps"": {
36
+ ""alert"": ""Push Notifications Test"",
37
+ ""sound"": ""default"",
38
+ ""badge"": 1
39
+ },
40
+ ""data"": $data$
41
+ }" ;
42
+
43
+ void ICallbacks . RunFinished ( ITestResultAdaptor result )
44
+ {
45
+ }
46
+
47
+ void ICallbacks . RunStarted ( ITestAdaptor testsToRun )
48
+ {
49
+ }
50
+
51
+ void ICallbacks . TestFinished ( ITestResultAdaptor result )
52
+ {
53
+ }
54
+
55
+ void ICallbacks . TestStarted ( ITestAdaptor test )
56
+ {
57
+ string data ;
58
+ if ( pushNotifications . TryGetValue ( test . Name , out data ) )
59
+ SendPush ( data ) ;
60
+ }
61
+
62
+ void SendPush ( string data )
63
+ {
64
+ string fileName = "/tmp/push.apns" ;
65
+ File . WriteAllText ( fileName , pushTemplate . Replace ( "$data$" , data ) ) ;
66
+ var process = new Process ( ) ;
67
+ process . StartInfo . UseShellExecute = true ;
68
+ process . StartInfo . FileName = "xcrun" ;
69
+ process . StartInfo . Arguments = $ "simctl push booted com.unity3d.mobilenotificationtests { fileName } ";
70
+ process . Start ( ) ;
71
+ }
72
+ }
73
+
10
74
#endif
11
75
12
76
class iOSNotificationTests
@@ -65,7 +129,7 @@ public void BeforeTests()
65
129
msg += "\n .Body: " + receivedNotification . Body ;
66
130
msg += "\n .CategoryIdentifier: " + receivedNotification . CategoryIdentifier ;
67
131
msg += "\n .Subtitle: " + receivedNotification . Subtitle ;
68
- Debug . Log ( msg ) ;
132
+ UnityEngine . Debug . Log ( msg ) ;
69
133
} ;
70
134
}
71
135
@@ -75,6 +139,7 @@ public void AfterEachTest()
75
139
receivedNotificationCount = 0 ;
76
140
lastReceivedNotification = null ;
77
141
iOSNotificationCenter . RemoveAllScheduledNotifications ( ) ;
142
+ iOSNotificationCenter . RemoveAllDeliveredNotifications ( ) ;
78
143
}
79
144
#endif
80
145
@@ -181,9 +246,9 @@ IEnumerator SendNotificationUsingCalendarTrigger_NotificationIsReceived(string t
181
246
} ;
182
247
183
248
iOSNotificationCenter . ScheduleNotification ( notification ) ;
184
- Debug . Log ( $ "SendNotificationUsingCalendarTrigger_NotificationIsReceived, Now: { dateTime } , Notification should arrive on: { dt } ") ;
249
+ UnityEngine . Debug . Log ( $ "SendNotificationUsingCalendarTrigger_NotificationIsReceived, Now: { dateTime } , Notification should arrive on: { dt } ") ;
185
250
yield return WaitForNotification ( 20.0f ) ;
186
- Debug . Log ( $ "SendNotificationUsingCalendarTrigger_NotificationIsReceived, wait finished at: { DateTime . Now } ") ;
251
+ UnityEngine . Debug . Log ( $ "SendNotificationUsingCalendarTrigger_NotificationIsReceived, wait finished at: { DateTime . Now } ") ;
187
252
Assert . AreEqual ( 1 , receivedNotificationCount ) ;
188
253
Assert . IsNotNull ( lastReceivedNotification ) ;
189
254
Assert . AreEqual ( text , lastReceivedNotification . Title ) ;
@@ -205,6 +270,56 @@ public IEnumerator SendNotificationUsingCalendarTriggerUtcTime_NotificationIsRec
205
270
yield return SendNotificationUsingCalendarTrigger_NotificationIsReceived ( "SendNotificationUsingCalendarTriggerUtcTime_NotificationIsReceived" , true ) ;
206
271
}
207
272
273
+ IEnumerator PushNotificationIsReceived ( string data )
274
+ {
275
+ yield return WaitForNotification ( 20.0f ) ;
276
+ Assert . AreEqual ( 1 , receivedNotificationCount ) ;
277
+ Assert . AreEqual ( data , lastReceivedNotification . Data ) ;
278
+ }
279
+
280
+ [ UnityTest ]
281
+ [ UnityPlatform ( RuntimePlatform . IPhonePlayer ) ]
282
+ public IEnumerator PushNotification_WithSimpleString_IsReceived ( )
283
+ {
284
+ yield return PushNotificationIsReceived ( "Test data" ) ;
285
+ }
286
+
287
+ [ UnityTest ]
288
+ [ UnityPlatform ( RuntimePlatform . IPhonePlayer ) ]
289
+ public IEnumerator PushNotification_WithInteger_IsReceived ( )
290
+ {
291
+ yield return PushNotificationIsReceived ( "15" ) ;
292
+ }
293
+
294
+ [ UnityTest ]
295
+ [ UnityPlatform ( RuntimePlatform . IPhonePlayer ) ]
296
+ public IEnumerator PushNotification_WithBool_IsReceived ( )
297
+ {
298
+ yield return PushNotificationIsReceived ( "true" ) ;
299
+ }
300
+
301
+ [ UnityTest ]
302
+ [ UnityPlatform ( RuntimePlatform . IPhonePlayer ) ]
303
+ public IEnumerator PushNotification_WithJSON_IsReceived ( )
304
+ {
305
+ yield return WaitForNotification ( 20.0f ) ;
306
+ Assert . AreEqual ( 1 , receivedNotificationCount ) ;
307
+
308
+ // clear all whitespace, so json formatting is not an issue
309
+ string data = lastReceivedNotification . Data . Replace ( "\n " , "" ) . Replace ( " " , "" ) ;
310
+ Assert . AreEqual ( "{\" item1\" :3,\" item2\" :\" value2\" }" , data ) ;
311
+ }
312
+
313
+ [ UnityTest ]
314
+ [ UnityPlatform ( RuntimePlatform . IPhonePlayer ) ]
315
+ public IEnumerator PushNotification_CustomData_IsReceived ( )
316
+ {
317
+ yield return WaitForNotification ( 20.0f ) ;
318
+ Assert . AreEqual ( 1 , receivedNotificationCount ) ;
319
+ Assert . IsTrue ( lastReceivedNotification . UserInfo . ContainsKey ( "CustomKey" ) ) ;
320
+ Assert . AreEqual ( "25" , lastReceivedNotification . UserInfo [ "CustomKey" ] ) ;
321
+ }
322
+
208
323
[ Test ]
209
324
public void iOSNotificationCalendarTrigger_ToUtc_DoesNotConvertUtcTrigger ( )
210
325
{
0 commit comments