Skip to content

Commit 95332f0

Browse files
1. Rename.
2. Redo some tests better. 3. Move function to IterableUtil.
1 parent 0694af4 commit 95332f0

10 files changed

+153
-146
lines changed

Tests/swift-sdk-swift-tests/IterableAPITests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ extension IterableAPI {
1717
config: IterableConfig = IterableConfig(),
1818
dateProvider: DateProviderProtocol = SystemDateProvider(),
1919
networkSession: @escaping @autoclosure () -> NetworkSessionProtocol = URLSession(configuration: URLSessionConfiguration.default)) {
20-
implementation = IterableAPIInternal.init(apiKey: apiKey, launchOptions: launchOptions, config: config, dateProvider: dateProvider, networkSession: networkSession)
20+
internalImplementation = IterableAPIInternal.init(apiKey: apiKey, launchOptions: launchOptions, config: config, dateProvider: dateProvider, networkSession: networkSession)
2121
}
2222
}
2323

Tests/swift-sdk-swift-tests/IterableActionRunnerTests.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import OHHTTPStubs
1010

1111
@testable import IterableSDK
1212

13+
let testExpectationTimeout = 5.0
14+
1315
class IterableActionRunnerTests: XCTestCase {
14-
1516
override func setUp() {
1617
super.setUp()
1718
// Put setup code here. This method is called before the invocation of each test method in the class.
@@ -71,31 +72,37 @@ class IterableActionRunnerTests: XCTestCase {
7172
let customActionName = "myCustomActionName"
7273
let action = IterableAction.action(fromDictionary: ["type" : customActionName])!
7374
let context = IterableActionContext(action: action, source: .push)
75+
let expection = XCTestExpectation(description: "callActionHandler")
7476
let customActionHandler: CustomActionHandler = {name in
7577
XCTAssertEqual(name, customActionName)
78+
expection.fulfill()
7679
return true
7780
}
7881

7982
let handled = IterableActionRunner.execute(action: action,
8083
context: context,
8184
customActionHandler: customActionHandler)
8285

86+
wait(for: [expection], timeout: testExpectationTimeout)
8387
XCTAssertTrue(handled)
8488
}
8589

8690
func testCustomActionOverride() {
8791
let customActionName = "myCustomActionName"
8892
let action = IterableAction.action(fromDictionary: ["type" : customActionName])!
8993
let context = IterableActionContext(action: action, source: .push)
94+
let expection = XCTestExpectation(description: "callActionHandler")
9095
let customActionHandler: CustomActionHandler = {name in
9196
XCTAssertEqual(name, customActionName)
97+
expection.fulfill()
9298
return false
9399
}
94100

95101
let handled = IterableActionRunner.execute(action: action,
96102
context: context,
97103
customActionHandler: customActionHandler)
98104

105+
wait(for: [expection], timeout: testExpectationTimeout)
99106
XCTAssertFalse(handled)
100107
}
101108

Tests/swift-sdk-swift-tests/IterableNotificationResponseTests.swift

Lines changed: 40 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,23 @@ class IterableNotificationResponseTests: XCTestCase {
3030
dateProvider.currentDate = Date()
3131
}
3232

33-
private static func customActionHandler(fromPromise promise: Promise<String, Error>) -> CustomActionHandler {
34-
return {(customActionName) in
35-
promise.resolve(with: customActionName)
36-
return true
37-
}
38-
}
39-
40-
private static func contextToCustomActionHandler(fromPromise promise: Promise<String, Error>) -> (IterableActionContext) -> CustomActionHandler {
41-
return {(context) in
42-
customActionHandler(fromPromise: promise)
43-
}
44-
}
45-
4633
func testTrackOpenPushWithCustomAction() {
4734
guard #available(iOS 10.0, *) else {
4835
return
4936
}
5037

38+
// we test with both 'true' and 'false' values below
39+
// to make sure that it doesn't influence the result
40+
// the return value is reserved for future use.
41+
testTrackOpenPushWithCustomAction(returnValue: true)
42+
testTrackOpenPushWithCustomAction(returnValue: false)
43+
}
44+
45+
private func testTrackOpenPushWithCustomAction(returnValue: Bool) {
46+
guard #available(iOS 10.0, *) else {
47+
return
48+
}
49+
5150
let messageId = UUID().uuidString
5251
let userInfo: [AnyHashable : Any] = [
5352
"itbl": [
@@ -60,31 +59,29 @@ class IterableNotificationResponseTests: XCTestCase {
6059
]
6160
]
6261
]
63-
62+
6463
let response = MockNotificationResponse(userInfo: userInfo, actionIdentifier: UNNotificationDefaultActionIdentifier)
6564
let pushTracker = MockPushTracker()
66-
let promise = Promise<String, Error>()
67-
promise.observe { (result) in
68-
switch (result) {
69-
case .error(let error):
70-
XCTFail(error.localizedDescription)
71-
case .value(let customActionName):
72-
XCTAssertEqual(customActionName, "customAction")
73-
}
65+
let expection = XCTestExpectation(description: "customActionDelegate is called")
66+
let customActionDelegate = MockCustomActionDelegate(returnValue: returnValue)
67+
customActionDelegate.callback = {(customActionName, context) in
68+
XCTAssertEqual(customActionName, "customAction")
69+
expection.fulfill()
7470
}
75-
71+
7672
let appIntegration = IterableAppIntegrationInternal(tracker: pushTracker,
7773
versionInfo: MockVersionInfo(version: 10),
78-
contextToUrlHandler: nil,
79-
contextToCustomActionHandler: IterableNotificationResponseTests.contextToCustomActionHandler(fromPromise: promise),
74+
customActionDelegate: customActionDelegate,
8075
urlOpener: MockUrlOpener())
8176
appIntegration.userNotificationCenter(nil, didReceive: response, withCompletionHandler: nil)
8277

78+
wait(for: [expection], timeout: testExpectationTimeout)
79+
8380
XCTAssertEqual(pushTracker.campaignId, 1234)
8481
XCTAssertEqual(pushTracker.templateId, 4321)
8582
XCTAssertEqual(pushTracker.messageId, messageId)
8683
XCTAssertFalse(pushTracker.appAlreadyRunnnig)
87-
84+
8885
XCTAssertEqual(pushTracker.dataFields?[ITBL_KEY_ACTION_IDENTIFIER] as? String, ITBL_VALUE_DEFAULT_PUSH_OPEN_ACTION_ID)
8986
}
9087

@@ -113,23 +110,21 @@ class IterableNotificationResponseTests: XCTestCase {
113110
let response = MockNotificationResponse(userInfo: userInfo, actionIdentifier: "buttonIdentifier")
114111
let pushTracker = MockPushTracker()
115112

116-
let promise = Promise<String, Error>()
117-
promise.observe { (result) in
118-
switch (result) {
119-
case .error(let error):
120-
XCTFail(error.localizedDescription)
121-
case .value(let customActionName):
122-
XCTAssertEqual(customActionName, "customAction")
123-
}
113+
let expection = XCTestExpectation(description: "customActionDelegate is called")
114+
let customActionDelegate = MockCustomActionDelegate(returnValue: true)
115+
customActionDelegate.callback = {(customActionName, context) in
116+
XCTAssertEqual(customActionName, "customAction")
117+
expection.fulfill()
124118
}
125-
119+
126120
let appIntegration = IterableAppIntegrationInternal(tracker: pushTracker,
127121
versionInfo: MockVersionInfo(version: 10),
128-
contextToUrlHandler: nil,
129-
contextToCustomActionHandler: IterableNotificationResponseTests.contextToCustomActionHandler(fromPromise: promise),
122+
customActionDelegate: customActionDelegate,
130123
urlOpener: MockUrlOpener())
131124
appIntegration.userNotificationCenter(nil, didReceive: response, withCompletionHandler: nil)
132125

126+
wait(for: [expection], timeout: testExpectationTimeout)
127+
133128
XCTAssertEqual(pushTracker.campaignId, 1234)
134129
XCTAssertEqual(pushTracker.templateId, 4321)
135130
XCTAssertEqual(pushTracker.messageId, messageId)
@@ -152,25 +147,21 @@ class IterableNotificationResponseTests: XCTestCase {
152147
]
153148

154149
let pushTracker = MockPushTracker()
155-
156-
let promise = Promise<String, Error>()
157-
promise.observe { (result) in
158-
switch (result) {
159-
case .error(let error):
160-
XCTFail(error.localizedDescription)
161-
case .value(let customActionName):
162-
XCTAssertEqual(customActionName, "customAction")
163-
}
150+
let expection = XCTestExpectation(description: "customActionDelegate is called")
151+
let customActionDelegate = MockCustomActionDelegate(returnValue: true)
152+
customActionDelegate.callback = {(customActionName, context) in
153+
XCTAssertEqual(customActionName, "customAction")
154+
expection.fulfill()
164155
}
165156

166157
let appIntegration = IterableAppIntegrationInternal(tracker: pushTracker,
167158
versionInfo: MockVersionInfo(version: 9),
168-
contextToUrlHandler: nil,
169-
contextToCustomActionHandler: IterableNotificationResponseTests.contextToCustomActionHandler(fromPromise: promise),
159+
customActionDelegate: customActionDelegate,
170160
urlOpener: MockUrlOpener())
171161
appIntegration.application(MockApplicationStateProvider(applicationState: .inactive), didReceiveRemoteNotification: userInfo, fetchCompletionHandler: nil)
172162

173-
163+
wait(for: [expection], timeout: testExpectationTimeout)
164+
174165
XCTAssertEqual(pushTracker.campaignId, 1234)
175166
XCTAssertEqual(pushTracker.templateId, 4321)
176167
XCTAssertEqual(pushTracker.messageId, messageId)
@@ -269,8 +260,6 @@ class IterableNotificationResponseTests: XCTestCase {
269260
let pushTracker = MockPushTracker()
270261
let appIntegration = IterableAppIntegrationInternal(tracker: pushTracker,
271262
versionInfo: MockVersionInfo(version: 10),
272-
contextToUrlHandler: nil,
273-
contextToCustomActionHandler: nil,
274263
urlOpener: urlOpener)
275264
appIntegration.userNotificationCenter(nil, didReceive: response, withCompletionHandler: nil)
276265

Tests/swift-sdk-swift-tests/Mocks.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,30 @@ public class MockUrlDelegate : NSObject, IterableURLDelegate {
5151
}
5252
}
5353

54+
@objcMembers
55+
public class MockCustomActionDelegate: NSObject, IterableCustomActionDelegate {
56+
// returnValue is reserved for future, don't rely on this
57+
private override convenience init() {
58+
self.init(returnValue: false)
59+
}
60+
61+
public init(returnValue: Bool) {
62+
self.returnValue = returnValue
63+
}
64+
65+
private (set) var returnValue: Bool
66+
private (set) var action: IterableAction?
67+
private (set) var context: IterableActionContext?
68+
var callback: ((String, IterableActionContext)->Void)? = nil
69+
70+
public func handle(iterableCustomAction action: IterableAction, inContext context: IterableActionContext) -> Bool {
71+
self.action = action
72+
self.context = context
73+
callback?(action.type, context)
74+
return returnValue
75+
}
76+
}
77+
5478
@objc public class MockUrlOpener : NSObject, UrlOpenerProtocol {
5579
@objc var ios10OpenedUrl: URL?
5680
@objc var preIos10openedUrl: URL?

0 commit comments

Comments
 (0)