Skip to content

Commit eee57f1

Browse files
committed
[ios] Add automated tests for push notifications
1 parent 9bdc83f commit eee57f1

File tree

1 file changed

+119
-4
lines changed

1 file changed

+119
-4
lines changed

com.unity.mobile.notifications/Tests/Runtime/iOS/iOSNotificationTests.cs

+119-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,76 @@
11
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
24
using UnityEngine;
35
using UnityEngine.TestTools;
46
using NUnit.Framework;
5-
using System.Collections;
67
using Unity.Notifications.iOS;
78
#if UNITY_EDITOR
9+
using System.Diagnostics;
10+
using System.IO;
811
using Unity.Notifications;
912
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+
1074
#endif
1175

1276
class iOSNotificationTests
@@ -65,7 +129,7 @@ public void BeforeTests()
65129
msg += "\n .Body: " + receivedNotification.Body;
66130
msg += "\n .CategoryIdentifier: " + receivedNotification.CategoryIdentifier;
67131
msg += "\n .Subtitle: " + receivedNotification.Subtitle;
68-
Debug.Log(msg);
132+
UnityEngine.Debug.Log(msg);
69133
};
70134
}
71135

@@ -75,6 +139,7 @@ public void AfterEachTest()
75139
receivedNotificationCount = 0;
76140
lastReceivedNotification = null;
77141
iOSNotificationCenter.RemoveAllScheduledNotifications();
142+
iOSNotificationCenter.RemoveAllDeliveredNotifications();
78143
}
79144
#endif
80145

@@ -181,9 +246,9 @@ IEnumerator SendNotificationUsingCalendarTrigger_NotificationIsReceived(string t
181246
};
182247

183248
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}");
185250
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}");
187252
Assert.AreEqual(1, receivedNotificationCount);
188253
Assert.IsNotNull(lastReceivedNotification);
189254
Assert.AreEqual(text, lastReceivedNotification.Title);
@@ -205,6 +270,56 @@ public IEnumerator SendNotificationUsingCalendarTriggerUtcTime_NotificationIsRec
205270
yield return SendNotificationUsingCalendarTrigger_NotificationIsReceived("SendNotificationUsingCalendarTriggerUtcTime_NotificationIsReceived", true);
206271
}
207272

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+
208323
[Test]
209324
public void iOSNotificationCalendarTrigger_ToUtc_DoesNotConvertUtcTrigger()
210325
{

0 commit comments

Comments
 (0)