Skip to content

Commit 845bfc1

Browse files
committed
chore(tests): Make a MockMessagingController
* Make a `MockMessagingController` in Objective C to allow us to access internal APIs on the OSMessagingController without needing to make them public. This is needed because Swift code cannot access Objective-C APIs unless they are public. This is a workaround for this limitation.
1 parent 6033a07 commit 845bfc1

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

iOS_SDK/OneSignalSDK/OneSignal.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@
103103
3C7022232ECF124B001768C6 /* OneSignalInAppMessagesMocks.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 3C70221C2ECF124B001768C6 /* OneSignalInAppMessagesMocks.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
104104
3C70222B2ECF126B001768C6 /* OneSignalInAppMessagesMocks.h in Headers */ = {isa = PBXBuildFile; fileRef = 3C7022292ECF126B001768C6 /* OneSignalInAppMessagesMocks.h */; settings = {ATTRIBUTES = (Public, ); }; };
105105
3C70222D2ECF12A5001768C6 /* IAMTestHelpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C70222C2ECF12A5001768C6 /* IAMTestHelpers.swift */; };
106+
3C7022302ECF1620001768C6 /* MockMessagingController.m in Sources */ = {isa = PBXBuildFile; fileRef = 3C70222F2ECF1620001768C6 /* MockMessagingController.m */; };
107+
3C7022312ECF1620001768C6 /* MockMessagingController.h in Headers */ = {isa = PBXBuildFile; fileRef = 3C70222E2ECF1620001768C6 /* MockMessagingController.h */; settings = {ATTRIBUTES = (Public, ); }; };
106108
3C70FA672D0B68A100031066 /* OneSignalClientError.h in Headers */ = {isa = PBXBuildFile; fileRef = 3C70FA652D0B68A100031066 /* OneSignalClientError.h */; settings = {ATTRIBUTES = (Public, ); }; };
107109
3C70FA682D0B68A100031066 /* OneSignalClientError.m in Sources */ = {isa = PBXBuildFile; fileRef = 3C70FA662D0B68A100031066 /* OneSignalClientError.m */; };
108110
3C789DBD293C2206004CF83D /* OSFocusInfluenceParam.m in Sources */ = {isa = PBXBuildFile; fileRef = 7A600B432453790700514A53 /* OSFocusInfluenceParam.m */; };
@@ -1304,6 +1306,8 @@
13041306
3C70221C2ECF124B001768C6 /* OneSignalInAppMessagesMocks.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = OneSignalInAppMessagesMocks.framework; sourceTree = BUILT_PRODUCTS_DIR; };
13051307
3C7022292ECF126B001768C6 /* OneSignalInAppMessagesMocks.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OneSignalInAppMessagesMocks.h; sourceTree = "<group>"; };
13061308
3C70222C2ECF12A5001768C6 /* IAMTestHelpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IAMTestHelpers.swift; sourceTree = "<group>"; };
1309+
3C70222E2ECF1620001768C6 /* MockMessagingController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MockMessagingController.h; sourceTree = "<group>"; };
1310+
3C70222F2ECF1620001768C6 /* MockMessagingController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MockMessagingController.m; sourceTree = "<group>"; };
13071311
3C70FA652D0B68A100031066 /* OneSignalClientError.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OneSignalClientError.h; sourceTree = "<group>"; };
13081312
3C70FA662D0B68A100031066 /* OneSignalClientError.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OneSignalClientError.m; sourceTree = "<group>"; };
13091313
3C7A39D42B7C18EE0082665E /* XCTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = XCTest.framework; path = Platforms/iPhoneOS.platform/Developer/Library/Frameworks/XCTest.framework; sourceTree = DEVELOPER_DIR; };
@@ -2174,6 +2178,8 @@
21742178
isa = PBXGroup;
21752179
children = (
21762180
3C7022292ECF126B001768C6 /* OneSignalInAppMessagesMocks.h */,
2181+
3C70222E2ECF1620001768C6 /* MockMessagingController.h */,
2182+
3C70222F2ECF1620001768C6 /* MockMessagingController.m */,
21772183
3C70222C2ECF12A5001768C6 /* IAMTestHelpers.swift */,
21782184
);
21792185
path = OneSignalInAppMessagesMocks;
@@ -3135,6 +3141,7 @@
31353141
buildActionMask = 2147483647;
31363142
files = (
31373143
3C70222B2ECF126B001768C6 /* OneSignalInAppMessagesMocks.h in Headers */,
3144+
3C7022312ECF1620001768C6 /* MockMessagingController.h in Headers */,
31383145
);
31393146
runOnlyForDeploymentPostprocessing = 0;
31403147
};
@@ -4306,6 +4313,7 @@
43064313
isa = PBXSourcesBuildPhase;
43074314
buildActionMask = 2147483647;
43084315
files = (
4316+
3C7022302ECF1620001768C6 /* MockMessagingController.m in Sources */,
43094317
3C70222D2ECF12A5001768C6 /* IAMTestHelpers.swift in Sources */,
43104318
);
43114319
runOnlyForDeploymentPostprocessing = 0;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Modified MIT License
3+
4+
Copyright 2025 OneSignal
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
1. The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
2. All copies of substantial portions of the Software may only be used in connection
17+
with services provided by OneSignal.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
THE SOFTWARE.
26+
*/
27+
28+
#import <Foundation/Foundation.h>
29+
#import <OneSignalCore/OneSignalCore.h>
30+
31+
NS_ASSUME_NONNULL_BEGIN
32+
33+
@interface MockMessagingController : NSObject
34+
+ (NSArray <OSInAppMessage *> *)messageDisplayQueue; // TODO: NL may need to be changed to return OSInAppMessageInternal
35+
+ (BOOL)isInAppMessageShowing;
36+
@end
37+
38+
NS_ASSUME_NONNULL_END
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Modified MIT License
3+
4+
Copyright 2025 OneSignal
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
1. The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
2. All copies of substantial portions of the Software may only be used in connection
17+
with services provided by OneSignal.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
THE SOFTWARE.
26+
*/
27+
28+
#import "MockMessagingController.h"
29+
#import "OSMessagingController.h"
30+
31+
@interface OSMessagingController ()
32+
@property (strong, nonatomic, nonnull) NSMutableArray <OSInAppMessageInternal *> *messageDisplayQueue;
33+
@end
34+
35+
@implementation MockMessagingController
36+
+ (void)resetState {
37+
// TODO: reseting OSMessagingController state between tests
38+
}
39+
40+
+ (NSArray *)messageDisplayQueue {
41+
return OSMessagingController.sharedInstance.messageDisplayQueue;
42+
}
43+
44+
+ (BOOL)isInAppMessageShowing {
45+
return OSMessagingController.sharedInstance.isInAppMessageShowing;
46+
}
47+
48+
@end

iOS_SDK/OneSignalSDK/OneSignalInAppMessagesMocks/OneSignalInAppMessagesMocks.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@ FOUNDATION_EXPORT double OneSignalInAppMessagesMocksVersionNumber;
3434
FOUNDATION_EXPORT const unsigned char OneSignalInAppMessagesMocksVersionString[];
3535

3636
// In this header, you should import all the public headers of your framework using statements like #import <OneSignalInAppMessagesMocks/PublicHeader.h>
37+
38+
#import <OneSignalInAppMessagesMocks/MockMessagingController.h>

0 commit comments

Comments
 (0)