From 9de20c770966f66f7af30ef42b6d568a6be024d4 Mon Sep 17 00:00:00 2001 From: Nan Date: Tue, 28 Jan 2025 10:15:17 -0800 Subject: [PATCH] Add / migrate one existing IAM integration test * Attempt to migrate `testDisablingIAMs_stillCreatesMessageQueue_butPreventsMessageDisplay` to new tests, but behavior has changed. * It becomes `testPausingIAMs_doesNotCreateMessageQueue` --- .../IAMIntegrationTests.swift | 64 ++++++++++++++++--- .../InAppMessagingIntegrationTests.m | 1 + 2 files changed, 57 insertions(+), 8 deletions(-) diff --git a/iOS_SDK/OneSignalSDK/OneSignalInAppMessagesTests/IAMIntegrationTests.swift b/iOS_SDK/OneSignalSDK/OneSignalInAppMessagesTests/IAMIntegrationTests.swift index 119ef343d..4af25cffe 100644 --- a/iOS_SDK/OneSignalSDK/OneSignalInAppMessagesTests/IAMIntegrationTests.swift +++ b/iOS_SDK/OneSignalSDK/OneSignalInAppMessagesTests/IAMIntegrationTests.swift @@ -27,17 +27,65 @@ with services provided by OneSignal. import XCTest @testable import OneSignalInAppMessages +import OneSignalOSCore +import OneSignalUser +import OneSignalCoreMocks +import OneSignalOSCoreMocks +import OneSignalUserMocks +import OneSignalInAppMessagesMocks +/** + These tests can include some Obj-C InAppMessagingIntegrationTests migrations. + */ final class IAMIntegrationTests: XCTestCase { override func setUpWithError() throws { - // Put setup code here. This method is called before the invocation of each test method in the class. - } - override func tearDownWithError() throws { - // Put teardown code here. This method is called after the invocation of each test method in the class. - } - func testExample() throws { + OneSignalCoreMocks.clearUserDefaults() + OneSignalUserMocks.reset() + OSConsistencyManager.shared.reset() + // Temp. logging to help debug during testing OneSignalLog.setLogLevel(.LL_VERBOSE) - OneSignalInAppMessages.getFromServer("foobar") - // OSMessagingController.sharedInstance() + } + + override func tearDownWithError() throws { } + + /** + Pausing IAMs will not evaluate messages. + */ + func testPausingIAMs_doesNotCreateMessageQueue() throws { + /* Setup */ + + let client = MockOneSignalClient() + OneSignalCoreImpl.setSharedClient(client) + + // 1. App ID is set because there are guards against nil App ID + OneSignalConfigManager.setAppId("test-app-id") + + // 2. Set up mock responses for the anonymous user, as the user needs an OSID + MockUserRequests.setDefaultCreateAnonUserResponses(with: client) + + // 3. Set up mock responses for fetching IAMs + let message = IAMTestHelpers.testMessageJsonWithTrigger(property: "session_time", triggerId: "test_id1", type: 1, value: 10.0) + let response = IAMTestHelpers.testFetchMessagesResponse(messages: [message]) + client.setMockResponseForRequest( + request: "", + response: response) + + // 4. Unblock the Consistency Manager to allow fetching of IAMs + ConsistencyManagerTestHelpers.setDefaultRywToken(id: anonUserOSID) + + // 5. Pausing should prevent messages from being evaluated and shown + OneSignalInAppMessages.__paused(true) + + // 6. Start the user manager to generate a user instance + OneSignalUserManagerImpl.sharedInstance.start() + OneSignalCoreMocks.waitForBackgroundThreads(seconds: 0.5) + + // 7. Fetch IAMs + OneSignalInAppMessages.getFromServer(testPushSubId) + OneSignalCoreMocks.waitForBackgroundThreads(seconds: 0.5) + + // Make sure no IAM is showing, and the queue has no IAMs + XCTAssertFalse(MockMessagingController.isInAppMessageShowing()) + XCTAssertEqual(MockMessagingController.messageDisplayQueue().count, 0) } } diff --git a/iOS_SDK/OneSignalSDK/UnitTests/InAppMessagingIntegrationTests.m b/iOS_SDK/OneSignalSDK/UnitTests/InAppMessagingIntegrationTests.m index 9ae73b67f..0acdc59f7 100644 --- a/iOS_SDK/OneSignalSDK/UnitTests/InAppMessagingIntegrationTests.m +++ b/iOS_SDK/OneSignalSDK/UnitTests/InAppMessagingIntegrationTests.m @@ -1422,6 +1422,7 @@ of this software and associated documentation files (the "Software"), to deal // XCTAssertTrue(action.promptActions[1].hasPrompted); // } +/// ✅ Migrated to `testPausingIAMs_doesNotCreateMessageQueue`... This test statement below is no longer true as of `5.2.9`. // - (void)testDisablingIAMs_stillCreatesMessageQueue_butPreventsMessageDisplay { // let message = [OSInAppMessageTestHelper testMessageJsonWithTriggerPropertyName:OS_DYNAMIC_TRIGGER_KIND_SESSION_TIME withId:@"test_id1" withOperator:OSTriggerOperatorTypeLessThan withValue:@10.0]; // let registrationResponse = [OSInAppMessageTestHelper testRegistrationJsonWithMessages:@[message]];