From eee57f1e9205620a349c64221ed6fcd2da1e6249 Mon Sep 17 00:00:00 2001 From: Aurimas Cernius Date: Tue, 11 Jan 2022 16:03:47 +0200 Subject: [PATCH 1/2] [ios] Add automated tests for push notifications --- .../Tests/Runtime/iOS/iOSNotificationTests.cs | 123 +++++++++++++++++- 1 file changed, 119 insertions(+), 4 deletions(-) diff --git a/com.unity.mobile.notifications/Tests/Runtime/iOS/iOSNotificationTests.cs b/com.unity.mobile.notifications/Tests/Runtime/iOS/iOSNotificationTests.cs index 7b4b5608..2dee8695 100644 --- a/com.unity.mobile.notifications/Tests/Runtime/iOS/iOSNotificationTests.cs +++ b/com.unity.mobile.notifications/Tests/Runtime/iOS/iOSNotificationTests.cs @@ -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(); + api.RegisterCallbacks(new SendPushNotifications()); + } + + readonly Dictionary pushNotifications = new Dictionary() + { + { "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 @@ -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); }; } @@ -75,6 +139,7 @@ public void AfterEachTest() receivedNotificationCount = 0; lastReceivedNotification = null; iOSNotificationCenter.RemoveAllScheduledNotifications(); + iOSNotificationCenter.RemoveAllDeliveredNotifications(); } #endif @@ -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); @@ -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() { From 9976f1b80214bbf12f534be6f7bd803937b42ea2 Mon Sep 17 00:00:00 2001 From: Aurimas Cernius Date: Fri, 13 Jan 2023 15:48:56 +0200 Subject: [PATCH 2/2] Do not split build and run --- .yamato/upm-ci.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.yamato/upm-ci.yml b/.yamato/upm-ci.yml index 8a320bc2..0ba3eb4e 100644 --- a/.yamato/upm-ci.yml +++ b/.yamato/upm-ci.yml @@ -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: