Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ios] Add automated tests for push notifications #141

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .yamato/upm-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ test_iOS_{{ editor.version }}:
- chmod u+x utr
- pip install unity-downloader-cli --index-url {{ artifactory.production }}pypi/pypi/simple --upgrade
- unity-downloader-cli -c Editor -c iOS -u {{ editor.version }} --wait --fast
- ./utr --testproject=TestProjects/Main --editor-location=.Editor --suite=playmode --platform=ios --artifacts_path=upm-ci~/test-results/ios --timeout=900 --extra-editor-arg="-upmNoDefaultPackages" --player-save-path=build/players --build-only
- ./utr --testproject=TestProjects/Main --editor-location=.Editor --suite=playmode --platform=ios --artifacts_path=upm-ci~/test-results/ios --timeout=900 --player-load-path=build/players
- ./utr --testproject=TestProjects/Main --editor-location=.Editor --suite=playmode --platform=ios --artifacts_path=upm-ci~/test-results/ios --timeout=900 --extra-editor-arg="-upmNoDefaultPackages"
artifacts:
packages:
paths:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,76 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.TestTools;
using NUnit.Framework;
using System.Collections;
using Unity.Notifications.iOS;
#if UNITY_EDITOR
using System.Diagnostics;
using System.IO;
using Unity.Notifications;
using UnityEditor;
using UnityEditor.TestTools.TestRunner.Api;


[InitializeOnLoad]
class SendPushNotifications : ICallbacks
{
static SendPushNotifications()
{
var api = ScriptableObject.CreateInstance<TestRunnerApi>();
api.RegisterCallbacks(new SendPushNotifications());
}

readonly Dictionary<string, string> pushNotifications = new Dictionary<string, string>()
{
{ "PushNotification_WithSimpleString_IsReceived", "\"Test data\"" },
{ "PushNotification_WithInteger_IsReceived", "15" },
{ "PushNotification_WithBool_IsReceived", "true" },
{ "PushNotification_WithJSON_IsReceived", "{ \"item1\": 3, \"item2\": \"value2\" }" },
{ "PushNotification_CustomData_IsReceived", "\"test\", \"CustomKey\": 25" },
};

readonly string pushTemplate = @"{
""aps"": {
""alert"": ""Push Notifications Test"",
""sound"": ""default"",
""badge"": 1
},
""data"": $data$
}";

void ICallbacks.RunFinished(ITestResultAdaptor result)
{
}

void ICallbacks.RunStarted(ITestAdaptor testsToRun)
{
}

void ICallbacks.TestFinished(ITestResultAdaptor result)
{
}

void ICallbacks.TestStarted(ITestAdaptor test)
{
string data;
if (pushNotifications.TryGetValue(test.Name, out data))
SendPush(data);
}

void SendPush(string data)
{
string fileName = "/tmp/push.apns";
File.WriteAllText(fileName, pushTemplate.Replace("$data$", data));
var process = new Process();
process.StartInfo.UseShellExecute = true;
process.StartInfo.FileName = "xcrun";
process.StartInfo.Arguments = $"simctl push booted com.unity3d.mobilenotificationtests {fileName}";
process.Start();
}
}

#endif

class iOSNotificationTests
Expand Down Expand Up @@ -65,7 +129,7 @@ public void BeforeTests()
msg += "\n .Body: " + receivedNotification.Body;
msg += "\n .CategoryIdentifier: " + receivedNotification.CategoryIdentifier;
msg += "\n .Subtitle: " + receivedNotification.Subtitle;
Debug.Log(msg);
UnityEngine.Debug.Log(msg);
};
}

Expand All @@ -75,6 +139,7 @@ public void AfterEachTest()
receivedNotificationCount = 0;
lastReceivedNotification = null;
iOSNotificationCenter.RemoveAllScheduledNotifications();
iOSNotificationCenter.RemoveAllDeliveredNotifications();
}
#endif

Expand Down Expand Up @@ -181,9 +246,9 @@ IEnumerator SendNotificationUsingCalendarTrigger_NotificationIsReceived(string t
};

iOSNotificationCenter.ScheduleNotification(notification);
Debug.Log($"SendNotificationUsingCalendarTrigger_NotificationIsReceived, Now: {dateTime}, Notification should arrive on: {dt}");
UnityEngine.Debug.Log($"SendNotificationUsingCalendarTrigger_NotificationIsReceived, Now: {dateTime}, Notification should arrive on: {dt}");
yield return WaitForNotification(20.0f);
Debug.Log($"SendNotificationUsingCalendarTrigger_NotificationIsReceived, wait finished at: {DateTime.Now}");
UnityEngine.Debug.Log($"SendNotificationUsingCalendarTrigger_NotificationIsReceived, wait finished at: {DateTime.Now}");
Assert.AreEqual(1, receivedNotificationCount);
Assert.IsNotNull(lastReceivedNotification);
Assert.AreEqual(text, lastReceivedNotification.Title);
Expand All @@ -205,6 +270,56 @@ public IEnumerator SendNotificationUsingCalendarTriggerUtcTime_NotificationIsRec
yield return SendNotificationUsingCalendarTrigger_NotificationIsReceived("SendNotificationUsingCalendarTriggerUtcTime_NotificationIsReceived", true);
}

IEnumerator PushNotificationIsReceived(string data)
{
yield return WaitForNotification(20.0f);
Assert.AreEqual(1, receivedNotificationCount);
Assert.AreEqual(data, lastReceivedNotification.Data);
}

[UnityTest]
[UnityPlatform(RuntimePlatform.IPhonePlayer)]
public IEnumerator PushNotification_WithSimpleString_IsReceived()
{
yield return PushNotificationIsReceived("Test data");
}

[UnityTest]
[UnityPlatform(RuntimePlatform.IPhonePlayer)]
public IEnumerator PushNotification_WithInteger_IsReceived()
{
yield return PushNotificationIsReceived("15");
}

[UnityTest]
[UnityPlatform(RuntimePlatform.IPhonePlayer)]
public IEnumerator PushNotification_WithBool_IsReceived()
{
yield return PushNotificationIsReceived("true");
}

[UnityTest]
[UnityPlatform(RuntimePlatform.IPhonePlayer)]
public IEnumerator PushNotification_WithJSON_IsReceived()
{
yield return WaitForNotification(20.0f);
Assert.AreEqual(1, receivedNotificationCount);

// clear all whitespace, so json formatting is not an issue
string data = lastReceivedNotification.Data.Replace("\n", "").Replace(" ", "");
Assert.AreEqual("{\"item1\":3,\"item2\":\"value2\"}", data);
}

[UnityTest]
[UnityPlatform(RuntimePlatform.IPhonePlayer)]
public IEnumerator PushNotification_CustomData_IsReceived()
{
yield return WaitForNotification(20.0f);
Assert.AreEqual(1, receivedNotificationCount);
Assert.IsTrue(lastReceivedNotification.UserInfo.ContainsKey("CustomKey"));
Assert.AreEqual("25", lastReceivedNotification.UserInfo["CustomKey"]);
}

[Test]
public void iOSNotificationCalendarTrigger_ToUtc_DoesNotConvertUtcTrigger()
{
Expand Down