Skip to content
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
333 changes: 330 additions & 3 deletions iOS_SDK/OneSignalSDK/OneSignal.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,13 @@ NS_ASSUME_NONNULL_BEGIN

+ (OSMessagingController *)sharedInstance;
+ (void)start;
+ (void)removeInstance;
- (void)presentInAppMessage:(OSInAppMessageInternal *)message;
- (void)getInAppMessagesFromServer:(NSString * _Nullable)subscriptionId;
- (void)messageViewImpressionRequest:(OSInAppMessageInternal *)message;
- (void)messageViewPageImpressionRequest:(OSInAppMessageInternal *)message withPageId:(NSString *)pageId;

- (BOOL)isInAppMessagingPaused;
- (void)setInAppMessagingPaused:(BOOL)pause;
- (void)addTriggers:(NSDictionary<NSString *, id> *)triggers;
- (void)removeTriggersForKeys:(NSArray<NSString *> *)keys;
- (void)clearTriggers;
- (NSDictionary<NSString *, id> *)getTriggers;
- (id)getTriggerValueForKey:(NSString *)key;

- (void)addInAppMessageClickListener:(NSObject<OSInAppMessageClickListener> *_Nullable)listener;
- (void)removeInAppMessageClickListener:(NSObject<OSInAppMessageClickListener> *_Nullable)listener;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
*/

#import "OSMessagingController.h"
#import "UIApplication+OneSignal.h" // Previously imported via "OneSignalHelper.h"
#import "NSDateFormatter+OneSignal.h" // Previously imported via "OneSignalHelper.h"
#import "UIApplication+OneSignal.h"
#import "NSDateFormatter+OneSignal.h"
#import <OneSignalCore/OneSignalCore.h>
#import "OSInAppMessageClickResult.h"
#import "OSInAppMessageClickEvent.h"
Expand Down Expand Up @@ -165,6 +165,7 @@ + (OSMessagingController *)sharedInstance {
return sharedInstance;
}

/// Note: This method is used in tests only.
+ (void)removeInstance {
sharedInstance = nil;
once = 0;
Expand Down Expand Up @@ -815,10 +816,6 @@ - (void)clearTriggers {
return self.triggerController.getTriggers;
}

- (id)getTriggerValueForKey:(NSString *)key {
return [self.triggerController getTriggerValueForKey:key];
}

#pragma mark OSInAppMessageViewControllerDelegate Methods
- (void)messageViewControllerDidDisplay:(OSInAppMessageInternal *)message {
[self onDidDisplayInAppMessage:message];
Expand Down Expand Up @@ -1227,7 +1224,6 @@ - (void)addTriggers:(NSDictionary<NSString *, id> *)triggers {}
- (void)removeTriggersForKeys:(NSArray<NSString *> *)keys {}
- (void)clearTriggers {}
- (NSDictionary<NSString *, id> *)getTriggers { return @{}; }
- (id)getTriggerValueForKey:(NSString *)key { return 0; }
#pragma mark OSInAppMessageViewControllerDelegate Methods
- (void)messageViewControllerWasDismissed {}
- (void)messageViewDidSelectAction:(OSInAppMessageInternal *)message withAction:(OSInAppMessageClickResult *)action {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ NS_ASSUME_NONNULL_BEGIN
- (void)addTriggers:(NSDictionary<NSString *, id> *)triggers;
- (void)removeTriggersForKeys:(NSArray<NSString *> *)keys;
- (NSDictionary<NSString *, id> *)getTriggers;
- (id)getTriggerValueForKey:(NSString *)key;
- (void)timeSinceLastMessage:(NSDate *)date;

@end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,6 @@ - (void)removeTriggersForKeys:(NSArray<NSString *> *)keys {
}
}

- (id)getTriggerValueForKey:(NSString *)key {
@synchronized (self.triggers) {
return self.triggers[key];
}
}

/*
* Part of redisplay logic
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ of this software and associated documentation files (the "Software"), to deal
#import "OSInAppMessagingRequests.h"

@implementation OSRequestGetInAppMessages
- (NSString *)description {
return [NSString stringWithFormat:@"<OSRequestGetInAppMessages from %@>", self.path];
}

+ (instancetype _Nonnull) withSubscriptionId:(NSString * _Nonnull)subscriptionId
withSessionDuration:(NSNumber * _Nonnull)sessionDuration
withRetryCount:(NSNumber *)retryCount
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
disabled_rules:
- identifier_name
112 changes: 112 additions & 0 deletions iOS_SDK/OneSignalSDK/OneSignalInAppMessagesMocks/IAMTestHelpers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
Modified MIT License

Copyright 2025 OneSignal

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

1. The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

2. All copies of substantial portions of the Software may only be used in connection
with services provided by OneSignal.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

import Foundation
import OneSignalInAppMessages

let OS_TEST_MESSAGE_ID = "a4b3gj7f-d8cc-11e4-bed1-df8f05be55ba"
let OS_TEST_MESSAGE_VARIANT_ID = "m8dh7234f-d8cc-11e4-bed1-df8f05be55ba"
let OS_TEST_ENGLISH_VARIANT_ID = "11e4-bed1-df8f05be55ba-m8dh7234f-d8cc"

let OS_DUMMY_HTML = "<html><h1>Hello World</h1></html>"

@objc
public class IAMTestHelpers: NSObject {

nonisolated(unsafe) static var messageIdIncrementer = 0

/// Convert OSTriggerOperatorType enum to string
private static func OS_OPERATOR_TO_STRING(_ type: Int32) -> String {
// Trigger operator strings
let OS_OPERATOR_STRINGS: [String] = [
"greater",
"less",
"equal",
"not_equal",
"less_or_equal",
"greater_or_equal",
"exists",
"not_exists",
"in"
]

return OS_OPERATOR_STRINGS[Int(type)]
}

/// Returns the JSON of a minimal in-app message that can be used as a building block.
@objc
public static func testDefaultMessageJson() -> [String: Any] {
messageIdIncrementer += 1
return [
"id": String(format: "%@_%i", OS_TEST_MESSAGE_ID, messageIdIncrementer),
"variants": [
"ios": [
"default": OS_TEST_MESSAGE_VARIANT_ID,
"en": OS_TEST_ENGLISH_VARIANT_ID
],
"all": [
"default": "should_never_be_used_by_any_test"
]
],
"triggers": []
]
}

/// Returns the JSON of an in-app message with trigger.
@objc
public static func testMessageJsonWithTrigger(property: String, triggerId: String, type: Int32, value: Any) -> [String: Any] {
var testMessage = self.testDefaultMessageJson()

testMessage["triggers"] = [
[
[
"kind": property,
"property": property,
"operator": OS_OPERATOR_TO_STRING(type),
"value": value,
"id": triggerId
]
]
]
return testMessage
}

@objc
public static func testFetchMessagesResponse(messages: [[String: Any]]) -> [String: Any] {
return [
"in_app_messages": messages
]
}

/// Returns the JSON of a preview or test in-app message.
@objc
public static func testMessagePreviewJson() -> [String: Any] {
var message = self.testDefaultMessageJson()
message["is_preview"] = true
return message
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Modified MIT License

Copyright 2025 OneSignal

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

1. The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

2. All copies of substantial portions of the Software may only be used in connection
with services provided by OneSignal.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#import <Foundation/Foundation.h>

//! Project version number for OneSignalInAppMessagesMocks.
FOUNDATION_EXPORT double OneSignalInAppMessagesMocksVersionNumber;

//! Project version string for OneSignalInAppMessagesMocks.
FOUNDATION_EXPORT const unsigned char OneSignalInAppMessagesMocksVersionString[];

// In this header, you should import all the public headers of your framework using statements like #import <OneSignalInAppMessagesMocks/PublicHeader.h>
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
Modified MIT License

Copyright 2025 OneSignal

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

1. The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

2. All copies of substantial portions of the Software may only be used in connection
with services provided by OneSignal.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

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 {
OneSignalCoreMocks.clearUserDefaults()
OneSignalUserMocks.reset()
OSConsistencyManager.shared.reset()
// Temp. logging to help debug during testing
OneSignalLog.setLogLevel(.LL_VERBOSE)
}

override func tearDownWithError() throws { }

/**
Test IAMs should display even when IAMs are paused.
*/
func testPreviewIAMIsDisplayedOnPause() throws {
/* Setup */
OneSignalCoreImpl.setSharedClient(MockOneSignalClient())
// App ID is set because there are guards against nil App ID
OneSignalConfigManager.setAppId("test-app-id")

// 1. Pause IAMs
OneSignalInAppMessages.__paused(true)

// 2. Create a preview message
let messageJson = IAMTestHelpers.testMessagePreviewJson()
let message = OSInAppMessageInternal.instance(withJson: messageJson)
XCTAssertNotNil(message, "Preview message should be created successfully")

// 3. Present the preview message
OSMessagingController.sharedInstance().present(inAppPreviewMessage: message)

// 4. Verify that the preview IAM is showing even when paused
XCTAssertTrue(OSMessagingController.sharedInstance().isInAppMessageShowing)
}

/**
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: "<OSRequestGetInAppMessages from apps/test-app-id/subscriptions/\(testPushSubId)/iams>",
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(OSMessagingController.sharedInstance().isInAppMessageShowing)
XCTAssertEqual(OSMessagingController.sharedInstance().messageDisplayQueue.count, 0)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//

#import "OSInAppMessageInternal.h"
#import "OSMessagingController.h"

// Expose private properties and methods for testing
@interface OSMessagingController (Testing)
@property (strong, nonatomic, nonnull) NSMutableArray <OSInAppMessageInternal *> *messageDisplayQueue;
- (void)presentInAppPreviewMessage:(OSInAppMessageInternal *)message;
@end
Loading
Loading