Skip to content

Commit 8a42b5f

Browse files
authored
UC in splitio_ios (#74)
1 parent f09bd8d commit 8a42b5f

File tree

5 files changed

+89
-0
lines changed

5 files changed

+89
-0
lines changed

splitio_ios/example/ios/SplitTests/SplitMethodParserTests.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,28 @@ class SplitMethodParserTests: XCTestCase {
297297
XCTAssert(providerHelper.splitClientConfigValue?.impressionListener != nil)
298298
XCTAssert(providerHelper.splitClientConfigValue?.streamingEnabled == false)
299299
}
300+
301+
func testGetUserConsent() {
302+
methodParser?.onMethodCall(methodName: "getUserConsent", arguments: [], result: { (_: Any?) in })
303+
304+
guard let wrapper = splitWrapper as? SplitWrapperStub else {
305+
XCTFail()
306+
return
307+
}
308+
309+
XCTAssert(wrapper.userConsent == "unknown")
310+
}
311+
312+
func testSetUserConsent() {
313+
methodParser?.onMethodCall(methodName: "setUserConsent", arguments: [], result: { (_: Any?) in })
314+
315+
guard let wrapper = splitWrapper as? SplitWrapperStub else {
316+
XCTFail()
317+
return
318+
}
319+
320+
XCTAssert(wrapper.userConsent == "declined")
321+
}
300322
}
301323

302324
class SplitWrapperStub: SplitWrapper {
@@ -316,6 +338,7 @@ class SplitWrapperStub: SplitWrapper {
316338
var attributeNameValue: String = ""
317339
var splitsCalled = false
318340
var splitNamesCalled = false
341+
var userConsent = "unknown"
319342

320343
func getClient(matchingKey: String, bucketingKey: String?) -> SplitClient? {
321344
matchingKeyValue = matchingKey
@@ -440,6 +463,18 @@ class SplitWrapperStub: SplitWrapper {
440463
splitNameValue = splitName
441464
return nil
442465
}
466+
467+
func getUserConsent() -> String {
468+
return userConsent
469+
}
470+
471+
func setUserConsent(enabled: Bool) {
472+
if (enabled) {
473+
userConsent = "granted"
474+
} else {
475+
userConsent = "declined"
476+
}
477+
}
443478
}
444479

445480
class SplitProviderHelperStub: SplitProviderHelper {

splitio_ios/example/ios/SplitTests/SplitTests.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,26 @@ class SplitTests: XCTestCase {
181181
let split = splitWrapper.split(splitName: "my-split")
182182
XCTAssert(manager.splitNameValue == "my-split")
183183
}
184+
185+
func testGetUserConsent() {
186+
let manager = SplitManagerStub()
187+
let factoryProvider = SplitFactoryProviderStub(manager: manager)
188+
splitWrapper = DefaultSplitWrapper(splitFactoryProvider: factoryProvider)
189+
let userConsent = splitWrapper.getUserConsent()
190+
XCTAssert(userConsent == "unknown")
191+
}
192+
193+
func testSetUserConsent() {
194+
let manager = SplitManagerStub()
195+
let factoryProvider = SplitFactoryProviderStub(manager: manager)
196+
splitWrapper = DefaultSplitWrapper(splitFactoryProvider: factoryProvider)
197+
splitWrapper.setUserConsent(enabled: true)
198+
let grantedUserConsent = splitWrapper.getUserConsent()
199+
splitWrapper.setUserConsent(enabled: false)
200+
let declinedUserConsent = splitWrapper.getUserConsent()
201+
XCTAssert(grantedUserConsent == "granted")
202+
XCTAssert(declinedUserConsent == "declined")
203+
}
184204
}
185205

186206
class SplitFactoryProviderStub: SplitFactoryProvider {

splitio_ios/ios/Classes/Constants.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ enum Method: String {
2424
case splits = "splits"
2525
case split = "split"
2626
case impressionLog = "impressionLog"
27+
case getUserConsent = "getUserConsent"
28+
case setUserConsent = "setUserConsent"
2729
}
2830

2931
enum Argument: String {

splitio_ios/ios/Classes/SplitMethodParser.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ class DefaultSplitMethodParser: SplitMethodParser {
133133
case .split:
134134
result(SplitView.asMap(splitView: splitWrapper?.split(splitName: argumentParser.getStringArgument(argumentName: .splitName, arguments: arguments) ?? "")))
135135
break
136+
case .getUserConsent:
137+
result(splitWrapper?.getUserConsent())
138+
break
139+
case .setUserConsent:
140+
splitWrapper?.setUserConsent(enabled: argumentParser.getBooleanArgument(argumentName: .value, arguments: arguments))
141+
result(nil)
136142
default:
137143
result(FlutterMethodNotImplemented)
138144
break

splitio_ios/ios/Classes/SplitWrapper.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ protocol SplitWrapper: EvaluationWrapper, AttributesWrapper {
1616
func splits() -> [SplitView]
1717

1818
func split(splitName: String) -> SplitView?
19+
20+
func getUserConsent() -> String
21+
22+
func setUserConsent(enabled: Bool)
1923
}
2024

2125
protocol EvaluationWrapper {
@@ -227,4 +231,26 @@ class DefaultSplitWrapper: SplitWrapper {
227231
return nil
228232
}
229233
}
234+
235+
func getUserConsent() -> String {
236+
if let splitFactory = splitFactory {
237+
let userConsent: UserConsent = splitFactory.userConsent
238+
239+
if (userConsent == .granted) {
240+
return "granted"
241+
} else if (userConsent == .declined) {
242+
return "declined"
243+
} else {
244+
return "unknown"
245+
}
246+
} else {
247+
return "unknown"
248+
}
249+
}
250+
251+
func setUserConsent(enabled: Bool) {
252+
if let splitFactory = splitFactory {
253+
splitFactory.setUserConsent(enabled: enabled)
254+
}
255+
}
230256
}

0 commit comments

Comments
 (0)