-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 2 mocking frameworks and 2 testing bundles
* Add mocking framework called `OneSignalCoreMocks` to simplify working with `OneSignalCore` in unit tests - Add helper method `clearUserDefaults()` - Create `MockOneSignalClient` class to replace `OneSignalClient` in unit tests * Add mocking framework called `OneSignalUserMocks` to simplify working with `OneSignalUser` in unit tests - Add some helper methods to reset state between tests, WIP * Add unit testing bundle `OneSignalCoreTests`, meant to test `OneSignalCore` - Add one test `testNotificationJson`, to get started * Add unit testing bundle `OneSignalUserTests`, meant to test `OneSignalUser` - Add one test `testLoginSetsExternalId`, to get started
- Loading branch information
Showing
10 changed files
with
1,824 additions
and
55 deletions.
There are no files selected for viewing
1,305 changes: 1,250 additions & 55 deletions
1,305
iOS_SDK/OneSignalSDK/OneSignal.xcodeproj/project.pbxproj
Large diffs are not rendered by default.
Oops, something went wrong.
54 changes: 54 additions & 0 deletions
54
iOS_SDK/OneSignalSDK/OneSignal.xcodeproj/xcshareddata/xcschemes/OneSignalCoreTests.xcscheme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Scheme | ||
LastUpgradeVersion = "1520" | ||
version = "1.7"> | ||
<BuildAction | ||
parallelizeBuildables = "YES" | ||
buildImplicitDependencies = "YES"> | ||
</BuildAction> | ||
<TestAction | ||
buildConfiguration = "Debug" | ||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" | ||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" | ||
shouldUseLaunchSchemeArgsEnv = "YES" | ||
shouldAutocreateTestPlan = "YES"> | ||
<Testables> | ||
<TestableReference | ||
skipped = "NO" | ||
parallelizable = "YES"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "3CC063A02B6D7A8D002BB07F" | ||
BuildableName = "OneSignalCoreTests.xctest" | ||
BlueprintName = "OneSignalCoreTests" | ||
ReferencedContainer = "container:OneSignal.xcodeproj"> | ||
</BuildableReference> | ||
</TestableReference> | ||
</Testables> | ||
</TestAction> | ||
<LaunchAction | ||
buildConfiguration = "Debug" | ||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" | ||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" | ||
launchStyle = "0" | ||
useCustomWorkingDirectory = "NO" | ||
ignoresPersistentStateOnLaunch = "NO" | ||
debugDocumentVersioning = "YES" | ||
debugServiceExtension = "internal" | ||
allowLocationSimulation = "YES"> | ||
</LaunchAction> | ||
<ProfileAction | ||
buildConfiguration = "Release" | ||
shouldUseLaunchSchemeArgsEnv = "YES" | ||
savedToolIdentifier = "" | ||
useCustomWorkingDirectory = "NO" | ||
debugDocumentVersioning = "YES"> | ||
</ProfileAction> | ||
<AnalyzeAction | ||
buildConfiguration = "Debug"> | ||
</AnalyzeAction> | ||
<ArchiveAction | ||
buildConfiguration = "Release" | ||
revealArchiveInOrganizer = "YES"> | ||
</ArchiveAction> | ||
</Scheme> |
54 changes: 54 additions & 0 deletions
54
iOS_SDK/OneSignalSDK/OneSignal.xcodeproj/xcshareddata/xcschemes/OneSignalUserTests.xcscheme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Scheme | ||
LastUpgradeVersion = "1520" | ||
version = "1.7"> | ||
<BuildAction | ||
parallelizeBuildables = "YES" | ||
buildImplicitDependencies = "YES"> | ||
</BuildAction> | ||
<TestAction | ||
buildConfiguration = "Debug" | ||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" | ||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" | ||
shouldUseLaunchSchemeArgsEnv = "YES" | ||
shouldAutocreateTestPlan = "YES"> | ||
<Testables> | ||
<TestableReference | ||
skipped = "NO" | ||
parallelizable = "YES"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "3CC063EA2B6D7FE8002BB07F" | ||
BuildableName = "OneSignalUserTests.xctest" | ||
BlueprintName = "OneSignalUserTests" | ||
ReferencedContainer = "container:OneSignal.xcodeproj"> | ||
</BuildableReference> | ||
</TestableReference> | ||
</Testables> | ||
</TestAction> | ||
<LaunchAction | ||
buildConfiguration = "Debug" | ||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" | ||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" | ||
launchStyle = "0" | ||
useCustomWorkingDirectory = "NO" | ||
ignoresPersistentStateOnLaunch = "NO" | ||
debugDocumentVersioning = "YES" | ||
debugServiceExtension = "internal" | ||
allowLocationSimulation = "YES"> | ||
</LaunchAction> | ||
<ProfileAction | ||
buildConfiguration = "Release" | ||
shouldUseLaunchSchemeArgsEnv = "YES" | ||
savedToolIdentifier = "" | ||
useCustomWorkingDirectory = "NO" | ||
debugDocumentVersioning = "YES"> | ||
</ProfileAction> | ||
<AnalyzeAction | ||
buildConfiguration = "Debug"> | ||
</AnalyzeAction> | ||
<ArchiveAction | ||
buildConfiguration = "Release" | ||
revealArchiveInOrganizer = "YES"> | ||
</ArchiveAction> | ||
</Scheme> |
141 changes: 141 additions & 0 deletions
141
iOS_SDK/OneSignalSDK/OneSignalCoreMocks/MockOneSignalClient.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/* | ||
Modified MIT License | ||
Copyright 2024 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 OneSignalCore | ||
|
||
/** | ||
This mock client is still adapting, and some logic from the existing OneSignalClientOverrider have been brought to here. | ||
*/ | ||
@objc | ||
public class MockOneSignalClient: NSObject, IOneSignalClient { | ||
public let executionQueue: DispatchQueue = DispatchQueue(label: "com.onesignal.execution") | ||
|
||
var mockResponses: [String: [String: Any]] = [:] | ||
public var lastHTTPRequest: OneSignalRequest? | ||
public var networkRequestCount = 0 | ||
public var executedRequests: [OneSignalRequest] = [] | ||
public var executeInstantaneously = true | ||
|
||
var remoteParamsResponse: [String: Any]? | ||
var shouldUseProvisionalAuthorization = false //new in iOS 12 (aka Direct to History) | ||
var remoteParamsOutcomes: [String: Any] = [:] | ||
|
||
/** May add to or change this default remote params response*/ | ||
public func getRemoteParamsResponse() -> [String: Any] { | ||
return remoteParamsResponse ?? [ | ||
IOS_FBA: true, | ||
IOS_USES_PROVISIONAL_AUTHORIZATION : shouldUseProvisionalAuthorization, | ||
IOS_RECEIVE_RECEIPTS_ENABLE: true, | ||
"outcomes" : remoteParamsOutcomes, | ||
] | ||
} | ||
|
||
public func enableOutcomes() { | ||
remoteParamsOutcomes = [ | ||
"direct": [ | ||
"enabled": true | ||
], | ||
"indirect": [ | ||
"notification_attribution": [ | ||
"minutes_since_displayed": 1440, | ||
"limit": 10 | ||
], | ||
"enabled": true | ||
], | ||
"unattributed": [ | ||
"enabled": true | ||
] | ||
] | ||
} | ||
|
||
// Temp. method to log info while building unit tests | ||
@objc public func logSelfInfo() { | ||
print("🧪 MockOneSignalClient with executionQueue \(executionQueue)") | ||
} | ||
|
||
public func reset() { | ||
mockResponses = [:] | ||
lastHTTPRequest = nil | ||
networkRequestCount = 0 | ||
executedRequests.removeAll() | ||
executeInstantaneously = true | ||
remoteParamsResponse = nil | ||
shouldUseProvisionalAuthorization = false | ||
remoteParamsOutcomes = [:] | ||
} | ||
|
||
|
||
public func execute(_ request: OneSignalRequest, onSuccess successBlock: @escaping OSResultSuccessBlock, onFailure failureBlock: @escaping OSFailureBlock) { | ||
print("🧪 MockOneSignalClient execute called") | ||
|
||
executedRequests.append(request) | ||
|
||
if (executeInstantaneously) { | ||
finishExecutingRequest(request, onSuccess: successBlock, onFailure: failureBlock) | ||
} else { | ||
executionQueue.async { | ||
self.finishExecutingRequest(request, onSuccess: successBlock, onFailure: failureBlock) | ||
} | ||
} | ||
} | ||
|
||
func finishExecutingRequest(_ request: OneSignalRequest, onSuccess successBlock: OSResultSuccessBlock, onFailure failureBlock: OSFailureBlock) { | ||
|
||
// TODO: This entire method needs to contained within the equivalent of @synchronized ❗️ | ||
print("🧪 completing HTTP request: \(request)") | ||
|
||
// TODO: Check for existence of app_id in the request and fail if not. | ||
|
||
self.didCompleteRequest(request) | ||
|
||
// Switch between types of requests with mock responses | ||
if (request.isKind(of: OSRequestGetIosParams.self)) { | ||
// send a mock remote params response | ||
successBlock(["mockTodo": "responseTodo"]) | ||
} | ||
if ((mockResponses[String(describing: request)]) != nil) { | ||
successBlock(mockResponses[String(describing: request)]) | ||
} | ||
else { | ||
print("🧪 cannot find a mock response for request: \(request)") | ||
} | ||
} | ||
|
||
func didCompleteRequest(_ request: OneSignalRequest) { | ||
networkRequestCount += 1 | ||
|
||
print("🧪 didCompleteRequest url(\(networkRequestCount)): \(String(describing: request.urlRequest().url)) params: \(String(describing: request.parameters))") | ||
|
||
lastHTTPRequest = request | ||
} | ||
|
||
/** This is not currently hooked up to anything to run */ | ||
@objc public func runBackgroundThreads() { | ||
// Obj-C implementation: dispatch_sync(executionQueue, ^{}) | ||
executionQueue.sync {} | ||
} | ||
|
||
public func setMockResponseForRequest(request: String, response: [String: Any]) { | ||
mockResponses[request] = response | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
iOS_SDK/OneSignalSDK/OneSignalCoreMocks/OneSignalCoreMocks.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
Modified MIT License | ||
Copyright 2024 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 OneSignalCoreMocks. | ||
FOUNDATION_EXPORT double OneSignalCoreMocksVersionNumber; | ||
|
||
//! Project version string for OneSignalCoreMocks. | ||
FOUNDATION_EXPORT const unsigned char OneSignalCoreMocksVersionString[]; | ||
|
||
// In this header, you should import all the public headers of your framework using statements like #import <OneSignalCoreMocks/PublicHeader.h> | ||
|
||
|
46 changes: 46 additions & 0 deletions
46
iOS_SDK/OneSignalSDK/OneSignalCoreMocks/OneSignalCoreMocks.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
Modified MIT License | ||
Copyright 2024 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 OneSignalCore | ||
|
||
@objc | ||
public class OneSignalCoreMocks : NSObject { | ||
|
||
public static func clearUserDefaults() { | ||
guard let userDefaults = OneSignalUserDefaults.initStandard().userDefaults else { | ||
return | ||
} | ||
let dictionary = userDefaults.dictionaryRepresentation() | ||
for key in dictionary.keys { | ||
userDefaults.removeObject(forKey: key) | ||
} | ||
|
||
guard let sharedUserDefaults = OneSignalUserDefaults.initShared().userDefaults else { | ||
return | ||
} | ||
let sharedDictionary = sharedUserDefaults.dictionaryRepresentation() | ||
for key in sharedDictionary.keys { | ||
sharedUserDefaults.removeObject(forKey: key) | ||
} | ||
} | ||
} |
Oops, something went wrong.