|
| 1 | +// |
| 2 | +/**************************************************************************** |
| 3 | +* Copyright 2019, Optimizely, Inc. and contributors * |
| 4 | +* * |
| 5 | +* Licensed under the Apache License, Version 2.0 (the "License"); * |
| 6 | +* you may not use this file except in compliance with the License. * |
| 7 | +* You may obtain a copy of the License at * |
| 8 | +* * |
| 9 | +* http://www.apache.org/licenses/LICENSE-2.0 * |
| 10 | +* * |
| 11 | +* Unless required by applicable law or agreed to in writing, software * |
| 12 | +* distributed under the License is distributed on an "AS IS" BASIS, * |
| 13 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * |
| 14 | +* See the License for the specific language governing permissions and * |
| 15 | +* limitations under the License. * |
| 16 | +***************************************************************************/ |
| 17 | + |
| 18 | + |
| 19 | +import XCTest |
| 20 | + |
| 21 | +class DecisionListenerTests: XCTestCase { |
| 22 | + |
| 23 | + func testDecisionListenerWithActivateWhenUserInExperiment() { |
| 24 | + let attributes: [String: Any?] = ["s_foo": "foo", |
| 25 | + "b_true": "N/A", |
| 26 | + "i_42": 44, |
| 27 | + "d_4_2": "N/A"] |
| 28 | + let optimizely = OTUtils.createOptimizely(datafileName: "audience_targeting", clearUserProfileService: true) |
| 29 | + var notificationVariation : String? |
| 30 | + var notificationExperiment : String? |
| 31 | + var notificationType: String? |
| 32 | + |
| 33 | + _ = optimizely?.notificationCenter.addDecisionNotificationListener(decisionListener: { (type, userId, attributes, decisionInfo) in |
| 34 | + notificationExperiment = decisionInfo[Constants.NotificationKeys.OptimizelyNotificationExperiment] as? String |
| 35 | + notificationVariation = decisionInfo[Constants.NotificationKeys.OptimizelyNotificationVariation] as? String |
| 36 | + notificationType = type |
| 37 | + }) |
| 38 | + |
| 39 | + let variation = try? optimizely?.activate(experimentKey: |
| 40 | + "ab_running_exp_audience_combo_empty_conditions", |
| 41 | + userId: "test_user_1", |
| 42 | + attributes: attributes) |
| 43 | + |
| 44 | + XCTAssertEqual(variation, "all_traffic_variation") |
| 45 | + XCTAssertEqual(notificationExperiment, "ab_running_exp_audience_combo_empty_conditions") |
| 46 | + XCTAssertEqual(notificationVariation, "all_traffic_variation") |
| 47 | + XCTAssertEqual(notificationType, Constants.NotificationKeys.OptimizelyDecisionTypeExperiment) |
| 48 | + } |
| 49 | + |
| 50 | + func testDecisionListenerWithActivateWhenUserNotInExperiment() { |
| 51 | + let optimizely = OTUtils.createOptimizely(datafileName: "audience_targeting", clearUserProfileService: true) |
| 52 | + var notificationVariation : String? |
| 53 | + var notificationExperiment : String? |
| 54 | + var notificationType: String? |
| 55 | + |
| 56 | + _ = optimizely?.notificationCenter.addDecisionNotificationListener(decisionListener: { (type, userId, attributes, decisionInfo) in |
| 57 | + notificationExperiment = decisionInfo[Constants.NotificationKeys.OptimizelyNotificationExperiment] as? String |
| 58 | + notificationVariation = decisionInfo[Constants.NotificationKeys.OptimizelyNotificationVariation] as? String |
| 59 | + notificationType = type |
| 60 | + }) |
| 61 | + |
| 62 | + _ = try? optimizely?.activate(experimentKey: |
| 63 | + "ab_running_exp_audience_combo_exact_foo_or_true__and__42_or_4_2", |
| 64 | + userId: "test_user_1", |
| 65 | + attributes: nil) |
| 66 | + |
| 67 | + XCTAssertEqual(notificationExperiment, nil) |
| 68 | + XCTAssertEqual(notificationVariation, nil) |
| 69 | + XCTAssertEqual(notificationType, Constants.NotificationKeys.OptimizelyDecisionTypeExperiment) |
| 70 | + } |
| 71 | + |
| 72 | + func testDecisionListenerWithGetVariationWhenUserInExperiment() { |
| 73 | + let attributes: [String: Any?] = ["s_foo": "foo", |
| 74 | + "b_true": "N/A", |
| 75 | + "i_42": 44, |
| 76 | + "d_4_2": "N/A"] |
| 77 | + let optimizely = OTUtils.createOptimizely(datafileName: "audience_targeting", clearUserProfileService: true) |
| 78 | + var notificationVariation : String? |
| 79 | + var notificationExperiment : String? |
| 80 | + var notificationType: String? |
| 81 | + |
| 82 | + _ = optimizely?.notificationCenter.addDecisionNotificationListener(decisionListener: { (type, userId, _attributes, decisionInfo) in |
| 83 | + notificationExperiment = decisionInfo[Constants.NotificationKeys.OptimizelyNotificationExperiment] as? String |
| 84 | + notificationVariation = decisionInfo[Constants.NotificationKeys.OptimizelyNotificationVariation] as? String |
| 85 | + notificationType = type |
| 86 | + }) |
| 87 | + |
| 88 | + _ = try? optimizely?.getVariation(experimentKey: "ab_running_exp_audience_combo_empty_conditions", |
| 89 | + userId: "test_user_1", |
| 90 | + attributes: attributes) |
| 91 | + |
| 92 | + XCTAssertEqual(notificationExperiment, "ab_running_exp_audience_combo_empty_conditions") |
| 93 | + XCTAssertEqual(notificationVariation, "all_traffic_variation") |
| 94 | + XCTAssertEqual(notificationType, Constants.NotificationKeys.OptimizelyDecisionTypeExperiment) |
| 95 | + } |
| 96 | + |
| 97 | + func testDecisionListenerWithGetVariationWhenUserNotInExperiment() { |
| 98 | + let optimizely = OTUtils.createOptimizely(datafileName: "audience_targeting", clearUserProfileService: true) |
| 99 | + var notificationVariation : String? |
| 100 | + var notificationExperiment : String? |
| 101 | + var notificationType: String? |
| 102 | + |
| 103 | + _ = optimizely?.notificationCenter.addDecisionNotificationListener(decisionListener: { (type, userId, attributes, decisionInfo) in |
| 104 | + notificationExperiment = decisionInfo[Constants.NotificationKeys.OptimizelyNotificationExperiment] as? String |
| 105 | + notificationVariation = decisionInfo[Constants.NotificationKeys.OptimizelyNotificationVariation] as? String |
| 106 | + notificationType = type |
| 107 | + }) |
| 108 | + |
| 109 | + _ = try? optimizely?.getVariation(experimentKey: "ab_running_exp_audience_combo_exact_foo_or_true__and__42_or_4_2", userId: "test_user_1") |
| 110 | + |
| 111 | + XCTAssertEqual(notificationExperiment, nil) |
| 112 | + XCTAssertEqual(notificationVariation, nil) |
| 113 | + XCTAssertEqual(notificationType, Constants.NotificationKeys.OptimizelyDecisionTypeExperiment) |
| 114 | + } |
| 115 | + |
| 116 | +} |
0 commit comments