Skip to content

Commit

Permalink
Make a MockMessagingController
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
nan-li committed Jan 28, 2025
1 parent 0768f8d commit ecdd49a
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 1 deletion.
25 changes: 25 additions & 0 deletions iOS_SDK/OneSignalSDK/OneSignal.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@
3C0C67172D493E5A00F80448 /* IAMTestHelpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C0C67162D493E5A00F80448 /* IAMTestHelpers.swift */; };
3C0C671C2D49449C00F80448 /* OneSignalInAppMessages.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DEBAAE282A4211D900BF2C1C /* OneSignalInAppMessages.framework */; };
3C0C671D2D49449C00F80448 /* OneSignalInAppMessages.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = DEBAAE282A4211D900BF2C1C /* OneSignalInAppMessages.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
3C0C67232D49469200F80448 /* MockMessagingController.m in Sources */ = {isa = PBXBuildFile; fileRef = 3C0C67222D49469200F80448 /* MockMessagingController.m */; };
3C0C67242D49469200F80448 /* MockMessagingController.h in Headers */ = {isa = PBXBuildFile; fileRef = 3C0C67212D49469200F80448 /* MockMessagingController.h */; settings = {ATTRIBUTES = (Public, ); }; };
3C0C67252D4946BD00F80448 /* OneSignalCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DE7D17E627026B95002D3A5D /* OneSignalCore.framework */; };
3C0C67262D4946BD00F80448 /* OneSignalCore.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = DE7D17E627026B95002D3A5D /* OneSignalCore.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
3C0EF49E28A1DBCB00E5434B /* OSUserInternalImpl.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C0EF49D28A1DBCB00E5434B /* OSUserInternalImpl.swift */; };
3C115165289A259500565C41 /* OneSignalOSCore.docc in Sources */ = {isa = PBXBuildFile; fileRef = 3C115164289A259500565C41 /* OneSignalOSCore.docc */; };
3C115171289A259500565C41 /* OneSignalOSCore.h in Headers */ = {isa = PBXBuildFile; fileRef = 3C115163289A259500565C41 /* OneSignalOSCore.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -637,6 +641,13 @@
remoteGlobalIDString = DEBAAE272A4211D900BF2C1C;
remoteInfo = OneSignalInAppMessages;
};
3C0C67272D4946BD00F80448 /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = 37747F8B19147D6400558FAD /* Project object */;
proxyType = 1;
remoteGlobalIDString = DE7D17E527026B95002D3A5D;
remoteInfo = OneSignalCore;
};
3C115194289AF85400565C41 /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = 37747F8B19147D6400558FAD /* Project object */;
Expand Down Expand Up @@ -1125,6 +1136,7 @@
dstPath = "";
dstSubfolderSpec = 10;
files = (
3C0C67262D4946BD00F80448 /* OneSignalCore.framework in Embed Frameworks */,
3C0C671D2D49449C00F80448 /* OneSignalInAppMessages.framework in Embed Frameworks */,
);
name = "Embed Frameworks";
Expand Down Expand Up @@ -1268,6 +1280,8 @@
3C01518E2C2E298E0079E076 /* OneSignalInAppMessagesTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = OneSignalInAppMessagesTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
3C01519B2C2E29F90079E076 /* IAMRequestTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = IAMRequestTests.m; sourceTree = "<group>"; };
3C0C67162D493E5A00F80448 /* IAMTestHelpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IAMTestHelpers.swift; sourceTree = "<group>"; };
3C0C67212D49469200F80448 /* MockMessagingController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MockMessagingController.h; sourceTree = "<group>"; };
3C0C67222D49469200F80448 /* MockMessagingController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MockMessagingController.m; sourceTree = "<group>"; };
3C0EF49D28A1DBCB00E5434B /* OSUserInternalImpl.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OSUserInternalImpl.swift; sourceTree = "<group>"; };
3C115161289A259500565C41 /* OneSignalOSCore.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = OneSignalOSCore.framework; sourceTree = BUILT_PRODUCTS_DIR; };
3C115163289A259500565C41 /* OneSignalOSCore.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OneSignalOSCore.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1767,6 +1781,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
3C0C67252D4946BD00F80448 /* OneSignalCore.framework in Frameworks */,
3C0C671C2D49449C00F80448 /* OneSignalInAppMessages.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -2171,6 +2186,8 @@
children = (
3C3FABD72D4898E500B7090D /* OneSignalInAppMessagesMocks.h */,
3C0C67162D493E5A00F80448 /* IAMTestHelpers.swift */,
3C0C67212D49469200F80448 /* MockMessagingController.h */,
3C0C67222D49469200F80448 /* MockMessagingController.m */,
);
path = OneSignalInAppMessagesMocks;
sourceTree = "<group>";
Expand Down Expand Up @@ -3117,6 +3134,7 @@
buildActionMask = 2147483647;
files = (
3C3FABD92D4898E500B7090D /* OneSignalInAppMessagesMocks.h in Headers */,
3C0C67242D49469200F80448 /* MockMessagingController.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -3396,6 +3414,7 @@
);
dependencies = (
3C0C671F2D49449C00F80448 /* PBXTargetDependency */,
3C0C67282D4946BD00F80448 /* PBXTargetDependency */,
);
name = OneSignalInAppMessagesMocks;
packageProductDependencies = (
Expand Down Expand Up @@ -4287,6 +4306,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
3C0C67232D49469200F80448 /* MockMessagingController.m in Sources */,
3C0C67172D493E5A00F80448 /* IAMTestHelpers.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -4692,6 +4712,11 @@
target = DEBAAE272A4211D900BF2C1C /* OneSignalInAppMessages */;
targetProxy = 3C0C671E2D49449C00F80448 /* PBXContainerItemProxy */;
};
3C0C67282D4946BD00F80448 /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
target = DE7D17E527026B95002D3A5D /* OneSignalCore */;
targetProxy = 3C0C67272D4946BD00F80448 /* PBXContainerItemProxy */;
};
3C115195289AF85400565C41 /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
target = DE7D17E527026B95002D3A5D /* OneSignalCore */;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
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>
#import <OneSignalCore/OneSignalCore.h>

NS_ASSUME_NONNULL_BEGIN

@interface MockMessagingController : NSObject
+ (NSArray <OSInAppMessage *> *)messageDisplayQueue; // TODO: may need to be changed to return OSInAppMessageInternal
+ (BOOL)isInAppMessageShowing;
@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
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 "MockMessagingController.h"
#import "OSMessagingController.h"

@interface OSMessagingController ()
@property (strong, nonatomic, nonnull) NSMutableArray <OSInAppMessageInternal *> *messageDisplayQueue;
@end

@implementation MockMessagingController
+ (void)resetState {
// TODO: reseting OSMessagingController state between tests
}

+ (NSArray *)messageDisplayQueue {
return OSMessagingController.sharedInstance.messageDisplayQueue;
}

+ (BOOL)isInAppMessageShowing {
return OSMessagingController.sharedInstance.isInAppMessageShowing;
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ 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>


#import <OneSignalInAppMessagesMocks/MockMessagingController.h>

0 comments on commit ecdd49a

Please sign in to comment.