Skip to content

Commit 2dfbb32

Browse files
committed
Add / migrate some existing IAM integration tests
* Attempt to migrate `testDisablingIAMs_stillCreatesMessageQueue_butPreventsMessageDisplay` to new tests, but behavior has changed. It becomes `testPausingIAMs_doesNotCreateMessageQueue` * Migrate `testPreviewIAMIsDisplayedOnPause`
1 parent 598db82 commit 2dfbb32

File tree

4 files changed

+104
-8
lines changed

4 files changed

+104
-8
lines changed

iOS_SDK/OneSignalSDK/OneSignalInAppMessagesMocks/IAMTestHelpers.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ let OS_DUMMY_HTML = "<html><h1>Hello World</h1></html>"
3636

3737
@objc
3838
public class IAMTestHelpers: NSObject {
39+
40+
nonisolated(unsafe) static var messageIdIncrementer = 0
41+
3942
/// Convert OSTriggerOperatorType enum to string
4043
private static func OS_OPERATOR_TO_STRING(_ type: Int32) -> String {
4144
// Trigger operator strings
@@ -54,10 +57,12 @@ public class IAMTestHelpers: NSObject {
5457
return OS_OPERATOR_STRINGS[Int(type)]
5558
}
5659

60+
/// Returns the JSON of a minimal in-app message that can be used as a building block.
5761
@objc
5862
public static func testDefaultMessageJson() -> [String: Any] {
63+
messageIdIncrementer += 1
5964
return [
60-
"id": String(format: "%@_%i", OS_TEST_MESSAGE_ID, UUID().uuidString),
65+
"id": String(format: "%@_%i", OS_TEST_MESSAGE_ID, messageIdIncrementer),
6166
"variants": [
6267
"ios": [
6368
"default": OS_TEST_MESSAGE_VARIANT_ID,
@@ -71,6 +76,7 @@ public class IAMTestHelpers: NSObject {
7176
]
7277
}
7378

79+
/// Returns the JSON of an in-app message with trigger.
7480
@objc
7581
public static func testMessageJsonWithTrigger(property: String, triggerId: String, type: Int32, value: Any) -> [String: Any] {
7682
var testMessage = self.testDefaultMessageJson()
@@ -95,4 +101,12 @@ public class IAMTestHelpers: NSObject {
95101
"in_app_messages": messages
96102
]
97103
}
104+
105+
/// Returns the JSON of a preview or test in-app message.
106+
@objc
107+
public static func testMessagePreviewJson() -> [String: Any] {
108+
var message = self.testDefaultMessageJson()
109+
message["is_preview"] = true
110+
return message
111+
}
98112
}

iOS_SDK/OneSignalSDK/OneSignalInAppMessagesTests/IAMIntegrationTests.swift

Lines changed: 79 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,89 @@ with services provided by OneSignal.
2727

2828
import XCTest
2929
@testable import OneSignalInAppMessages
30+
import OneSignalOSCore
31+
import OneSignalUser
32+
import OneSignalCoreMocks
33+
import OneSignalOSCoreMocks
34+
import OneSignalUserMocks
35+
import OneSignalInAppMessagesMocks
3036

37+
/**
38+
These tests can include some Obj-C InAppMessagingIntegrationTests migrations.
39+
*/
3140
final class IAMIntegrationTests: XCTestCase {
3241
override func setUpWithError() throws {
33-
// Put setup code here. This method is called before the invocation of each test method in the class.
42+
OneSignalCoreMocks.clearUserDefaults()
43+
OneSignalUserMocks.reset()
44+
OSConsistencyManager.shared.reset()
45+
// Temp. logging to help debug during testing
46+
OneSignalLog.setLogLevel(.LL_VERBOSE)
3447
}
35-
override func tearDownWithError() throws {
36-
// Put teardown code here. This method is called after the invocation of each test method in the class.
48+
49+
override func tearDownWithError() throws { }
50+
51+
/**
52+
Test IAMs should display even when IAMs are paused.
53+
*/
54+
func testPreviewIAMIsDisplayedOnPause() throws {
55+
/* Setup */
56+
OneSignalCoreImpl.setSharedClient(MockOneSignalClient())
57+
// App ID is set because there are guards against nil App ID
58+
OneSignalConfigManager.setAppId("test-app-id")
59+
60+
// 1. Pause IAMs
61+
OneSignalInAppMessages.__paused(true)
62+
63+
// 2. Create a preview message
64+
let messageJson = IAMTestHelpers.testMessagePreviewJson()
65+
let message = OSInAppMessageInternal.instance(withJson: messageJson)
66+
XCTAssertNotNil(message, "Preview message should be created successfully")
67+
68+
// 3. Present the preview message
69+
OSMessagingController.sharedInstance().present(inAppPreviewMessage: message)
70+
71+
// 4. Verify that the preview IAM is showing even when paused
72+
XCTAssertTrue(OSMessagingController.sharedInstance().isInAppMessageShowing)
3773
}
38-
func testExample() throws {
39-
OneSignalLog.setLogLevel(.LL_VERBOSE)
40-
OneSignalInAppMessages.getFromServer("foobar")
41-
// OSMessagingController.sharedInstance()
74+
75+
/**
76+
Pausing IAMs will not evaluate messages.
77+
*/
78+
func testPausingIAMs_doesNotCreateMessageQueue() throws {
79+
/* Setup */
80+
81+
let client = MockOneSignalClient()
82+
OneSignalCoreImpl.setSharedClient(client)
83+
84+
// 1. App ID is set because there are guards against nil App ID
85+
OneSignalConfigManager.setAppId("test-app-id")
86+
87+
// 2. Set up mock responses for the anonymous user, as the user needs an OSID
88+
MockUserRequests.setDefaultCreateAnonUserResponses(with: client)
89+
90+
// 3. Set up mock responses for fetching IAMs
91+
let message = IAMTestHelpers.testMessageJsonWithTrigger(property: "session_time", triggerId: "test_id1", type: 1, value: 10.0)
92+
let response = IAMTestHelpers.testFetchMessagesResponse(messages: [message])
93+
client.setMockResponseForRequest(
94+
request: "<OSRequestGetInAppMessages from apps/test-app-id/subscriptions/\(testPushSubId)/iams>",
95+
response: response)
96+
97+
// 4. Unblock the Consistency Manager to allow fetching of IAMs
98+
ConsistencyManagerTestHelpers.setDefaultRywToken(id: anonUserOSID)
99+
100+
// 5. Pausing should prevent messages from being evaluated and shown
101+
OneSignalInAppMessages.__paused(true)
102+
103+
// 6. Start the user manager to generate a user instance
104+
OneSignalUserManagerImpl.sharedInstance.start()
105+
OneSignalCoreMocks.waitForBackgroundThreads(seconds: 0.5)
106+
107+
// 7. Fetch IAMs
108+
OneSignalInAppMessages.getFromServer(testPushSubId)
109+
OneSignalCoreMocks.waitForBackgroundThreads(seconds: 0.5)
110+
111+
// Make sure no IAM is showing, and the queue has no IAMs
112+
XCTAssertFalse(OSMessagingController.sharedInstance().isInAppMessageShowing)
113+
XCTAssertEqual(OSMessagingController.sharedInstance().messageDisplayQueue.count, 0)
42114
}
43115
}

iOS_SDK/OneSignalSDK/OneSignalInAppMessagesTests/OneSignalInAppMessagesTests-Bridging-Header.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,11 @@
22
// Use this file to import your target's public headers that you would like to expose to Swift.
33
//
44

5+
#import "OSInAppMessageInternal.h"
6+
#import "OSMessagingController.h"
7+
8+
// Expose private properties and methods for testing
9+
@interface OSMessagingController (Testing)
10+
@property (strong, nonatomic, nonnull) NSMutableArray <OSInAppMessageInternal *> *messageDisplayQueue;
11+
- (void)presentInAppPreviewMessage:(OSInAppMessageInternal *)message;
12+
@end

iOS_SDK/OneSignalSDK/UnitTests/InAppMessagingIntegrationTests.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,6 +1422,7 @@ of this software and associated documentation files (the "Software"), to deal
14221422
// XCTAssertTrue(action.promptActions[1].hasPrompted);
14231423
// }
14241424

1425+
/// ✅ Migrated to `testPausingIAMs_doesNotCreateMessageQueue`... This test statement below is no longer true as of `5.2.9`.
14251426
// - (void)testDisablingIAMs_stillCreatesMessageQueue_butPreventsMessageDisplay {
14261427
// let message = [OSInAppMessageTestHelper testMessageJsonWithTriggerPropertyName:OS_DYNAMIC_TRIGGER_KIND_SESSION_TIME withId:@"test_id1" withOperator:OSTriggerOperatorTypeLessThan withValue:@10.0];
14271428
// let registrationResponse = [OSInAppMessageTestHelper testRegistrationJsonWithMessages:@[message]];
@@ -1440,6 +1441,7 @@ of this software and associated documentation files (the "Software"), to deal
14401441
// XCTAssertEqual(OSMessagingControllerOverrider.messageDisplayQueue.count, 1);
14411442
// }
14421443

1444+
/// ✅ Migrated to `testPreviewIAMIsDisplayedOnPause`
14431445
// /*
14441446
// Test IAMs should display even when IAMs are paused
14451447
// */

0 commit comments

Comments
 (0)