19
19
import XCTest
20
20
21
21
class DecisionListenerTests : XCTestCase {
22
-
22
+
23
+ // MARK: - Constants
24
+
25
+ let kFeatureKey = " feature_1 "
26
+ let kUserId = " 11111 "
27
+
28
+ // MARK: - Properties
29
+
30
+ var datafile : Data !
31
+ var optimizely : FakeManager !
32
+
33
+ // MARK: - SetUp
34
+
35
+ override func setUp( ) {
36
+ super. setUp ( )
37
+
38
+ self . datafile = OTUtils . loadJSONDatafile ( " typed_audience_datafile " )
39
+
40
+ self . optimizely = FakeManager ( sdkKey: " 12345 " ,
41
+ userProfileService: OTUtils . createClearUserProfileService ( ) )
42
+ try ! self . optimizely. initializeSDK ( datafile: datafile)
43
+ }
44
+
45
+ func testDecisionListenerForGetEnabledFeatures( ) {
46
+
47
+ let tmpDatafile = OTUtils . loadJSONDatafile ( " api_datafile " )
48
+
49
+ let tmpOptimizely = OptimizelyManager ( sdkKey: " 12345 " ,
50
+ userProfileService: OTUtils . createClearUserProfileService ( ) )
51
+ try ! tmpOptimizely. initializeSDK ( datafile: tmpDatafile!)
52
+
53
+ var count = 0
54
+ tmpOptimizely. notificationCenter. clearAllNotificationListeners ( )
55
+ _ = tmpOptimizely. notificationCenter. addDecisionNotificationListener { ( type, userId, attributes, decisionInfo) in
56
+ count += 1
57
+ }
58
+
59
+ _ = try ! tmpOptimizely. getEnabledFeatures ( userId: kUserId)
60
+ XCTAssertEqual ( count, 2 )
61
+ }
62
+
63
+ func testDecisionListenerWithUserNotInExperimentAndRollout( ) {
64
+
65
+ self . optimizely. setDecisionServiceData ( experiment: nil , variation: nil )
66
+ self . optimizely. notificationCenter. clearAllNotificationListeners ( )
67
+ _ = self . optimizely. notificationCenter. addDecisionNotificationListener { ( type, userId, attributes, decisionInfo) in
68
+ XCTAssertEqual ( decisionInfo [ Constants . DecisionInfoKeys. featureEnabled] as! Bool , false )
69
+ XCTAssertEqual ( decisionInfo [ Constants . DecisionInfoKeys. source] as! String , Constants . DecisionSource. Rollout)
70
+ XCTAssertNil ( decisionInfo [ Constants . DecisionInfoKeys. sourceExperiment] )
71
+ XCTAssertNil ( decisionInfo [ Constants . DecisionInfoKeys. sourceVariation] )
72
+ }
73
+ _ = try ? self . optimizely. isFeatureEnabled ( featureKey: kFeatureKey, userId: kUserId)
74
+ }
75
+
76
+ func testDecisionListenerWithUserInRollout( ) {
77
+
78
+ var variation : Variation = ( self . optimizely. config? . allExperiments. first!. variations. first) !
79
+ variation. featureEnabled = true
80
+ self . optimizely. setDecisionServiceData ( experiment: nil , variation: variation)
81
+ self . optimizely. notificationCenter. clearAllNotificationListeners ( )
82
+ _ = self . optimizely. notificationCenter. addDecisionNotificationListener { ( type, userId, attributes, decisionInfo) in
83
+ XCTAssertEqual ( decisionInfo [ Constants . DecisionInfoKeys. featureEnabled] as! Bool , true )
84
+ XCTAssertEqual ( decisionInfo [ Constants . DecisionInfoKeys. source] as! String , Constants . DecisionSource. Rollout)
85
+ XCTAssertNil ( decisionInfo [ Constants . DecisionInfoKeys. sourceExperiment] )
86
+ XCTAssertNil ( decisionInfo [ Constants . DecisionInfoKeys. sourceVariation] )
87
+ }
88
+ _ = try ! self . optimizely. isFeatureEnabled ( featureKey: kFeatureKey, userId: kUserId)
89
+
90
+ variation. featureEnabled = false
91
+ self . optimizely. setDecisionServiceData ( experiment: nil , variation: variation)
92
+ self . optimizely. notificationCenter. clearAllNotificationListeners ( )
93
+ _ = self . optimizely. notificationCenter. addDecisionNotificationListener { ( type, userId, attributes, decisionInfo) in
94
+ XCTAssertEqual ( decisionInfo [ Constants . DecisionInfoKeys. featureEnabled] as! Bool , false )
95
+ XCTAssertEqual ( decisionInfo [ Constants . DecisionInfoKeys. source] as! String , Constants . DecisionSource. Rollout)
96
+ XCTAssertNil ( decisionInfo [ Constants . DecisionInfoKeys. sourceExperiment] )
97
+ XCTAssertNil ( decisionInfo [ Constants . DecisionInfoKeys. sourceVariation] )
98
+ }
99
+ _ = try ! self . optimizely. isFeatureEnabled ( featureKey: kFeatureKey, userId: kUserId)
100
+ }
101
+
102
+ func testDecisionListenerWithUserInExperiment( ) {
103
+
104
+ let experiment : Experiment = ( self . optimizely. config? . allExperiments. first!) !
105
+ var variation : Variation = ( experiment. variations. first) !
106
+ variation. featureEnabled = true
107
+ self . optimizely. setDecisionServiceData ( experiment: experiment, variation: variation)
108
+ self . optimizely. notificationCenter. clearAllNotificationListeners ( )
109
+ _ = self . optimizely. notificationCenter. addDecisionNotificationListener { ( type, userId, attributes, decisionInfo) in
110
+ XCTAssertEqual ( decisionInfo [ Constants . DecisionInfoKeys. featureEnabled] as! Bool , true )
111
+ XCTAssertEqual ( decisionInfo [ Constants . DecisionInfoKeys. source] as! String , Constants . DecisionSource. Experiment)
112
+ XCTAssertEqual ( decisionInfo [ Constants . DecisionInfoKeys. sourceExperiment] as! String , experiment. key)
113
+ XCTAssertEqual ( decisionInfo [ Constants . DecisionInfoKeys. sourceVariation] as! String , variation. key)
114
+ }
115
+ _ = try ! self . optimizely. isFeatureEnabled ( featureKey: kFeatureKey, userId: kUserId)
116
+
117
+ variation. featureEnabled = false
118
+ self . optimizely. setDecisionServiceData ( experiment: experiment, variation: variation)
119
+ self . optimizely. notificationCenter. clearAllNotificationListeners ( )
120
+ _ = self . optimizely. notificationCenter. addDecisionNotificationListener { ( type, userId, attributes, decisionInfo) in
121
+ XCTAssertEqual ( decisionInfo [ Constants . DecisionInfoKeys. featureEnabled] as! Bool , false )
122
+ XCTAssertEqual ( decisionInfo [ Constants . DecisionInfoKeys. source] as! String , Constants . DecisionSource. Experiment)
123
+ XCTAssertEqual ( decisionInfo [ Constants . DecisionInfoKeys. sourceExperiment] as! String , experiment. key)
124
+ XCTAssertEqual ( decisionInfo [ Constants . DecisionInfoKeys. sourceVariation] as! String , variation. key)
125
+ }
126
+ _ = try ! self . optimizely. isFeatureEnabled ( featureKey: kFeatureKey, userId: kUserId)
127
+ }
128
+
23
129
func testDecisionListenerWithActivateWhenUserInExperiment( ) {
24
130
let attributes : [ String : Any ? ] = [ " s_foo " : " foo " ,
25
131
" b_true " : " N/A " ,
@@ -46,7 +152,7 @@ class DecisionListenerTests: XCTestCase {
46
152
XCTAssertEqual ( notificationVariation, " all_traffic_variation " )
47
153
XCTAssertEqual ( notificationType, Constants . DecisionTypeKeys. experiment)
48
154
}
49
-
155
+
50
156
func testDecisionListenerWithActivateWhenUserNotInExperiment( ) {
51
157
let optimizely = OTUtils . createOptimizely ( datafileName: " audience_targeting " , clearUserProfileService: true )
52
158
var notificationVariation : String ?
@@ -58,11 +164,11 @@ class DecisionListenerTests: XCTestCase {
58
164
notificationVariation = decisionInfo [ Constants . NotificationKeys. variation] as? String
59
165
notificationType = type
60
166
} )
61
-
167
+
62
168
_ = try ? optimizely? . activate ( experimentKey:
63
169
" ab_running_exp_audience_combo_exact_foo_or_true__and__42_or_4_2 " ,
64
- userId: " test_user_1 " ,
65
- attributes: nil )
170
+ userId: " test_user_1 " ,
171
+ attributes: nil )
66
172
67
173
XCTAssertEqual ( notificationExperiment, nil )
68
174
XCTAssertEqual ( notificationVariation, nil )
@@ -86,8 +192,8 @@ class DecisionListenerTests: XCTestCase {
86
192
} )
87
193
88
194
_ = try ? optimizely? . getVariation ( experimentKey: " ab_running_exp_audience_combo_empty_conditions " ,
89
- userId: " test_user_1 " ,
90
- attributes: attributes)
195
+ userId: " test_user_1 " ,
196
+ attributes: attributes)
91
197
92
198
XCTAssertEqual ( notificationExperiment, " ab_running_exp_audience_combo_empty_conditions " )
93
199
XCTAssertEqual ( notificationVariation, " all_traffic_variation " )
@@ -112,5 +218,52 @@ class DecisionListenerTests: XCTestCase {
112
218
XCTAssertEqual ( notificationVariation, nil )
113
219
XCTAssertEqual ( notificationType, Constants . DecisionTypeKeys. experiment)
114
220
}
221
+ }
222
+
223
+ class FakeManager : OptimizelyManager {
224
+
225
+ override var decisionService : OPTDecisionService {
226
+ get {
227
+ return HandlerRegistryService . shared. injectDecisionService ( sdkKey: self . sdkKey, isReintialize: true ) !
228
+ }
229
+ }
230
+
231
+ override init ( sdkKey: String , logger: OPTLogger ? = nil , eventDispatcher: OPTEventDispatcher ? = nil , userProfileService: OPTUserProfileService ? = nil , periodicDownloadInterval: Int ? = nil ) {
232
+
233
+ super. init ( sdkKey: sdkKey, logger: logger, eventDispatcher: eventDispatcher, userProfileService: userProfileService, periodicDownloadInterval: periodicDownloadInterval)
234
+ HandlerRegistryService . shared. removeAll ( )
235
+
236
+ let userProfileService = userProfileService ?? DefaultUserProfileService ( )
237
+ self . registerServices ( sdkKey: sdkKey,
238
+ logger: logger ?? DefaultLogger ( ) ,
239
+ eventDispatcher: eventDispatcher ?? DefaultEventDispatcher . sharedInstance,
240
+ datafileHandler: DefaultDatafileHandler ( ) ,
241
+ decisionService: FakeDecisionService ( userProfileService: userProfileService) ,
242
+ notificationCenter: DefaultNotificationCenter ( ) )
243
+ }
244
+
245
+ func setDecisionServiceData( experiment: Experiment ? , variation: Variation ? ) {
246
+ ( self . decisionService as! FakeDecisionService ) . experiment = experiment
247
+ ( self . decisionService as! FakeDecisionService ) . variation = variation
248
+ }
249
+ }
115
250
251
+ class FakeDecisionService : DefaultDecisionService {
252
+
253
+ var experiment : Experiment ?
254
+ var variation : Variation ?
255
+
256
+ override init ( userProfileService: OPTUserProfileService ) {
257
+ super. init ( userProfileService: DefaultUserProfileService ( ) )
258
+ }
259
+
260
+ override func getVariationForFeature( config: ProjectConfig , featureFlag: FeatureFlag , userId: String , attributes: OptimizelyAttributes ) -> ( experiment: Experiment ? , variation: Variation ? ) ? {
261
+ return ( self . experiment, self . variation)
262
+ }
263
+ }
264
+
265
+ fileprivate extension HandlerRegistryService {
266
+ func removeAll( ) {
267
+ self . binders. removeAll ( )
268
+ }
116
269
}
0 commit comments