|
24 | 24 | import com.optimizely.ab.android.event_handler.DefaultEventHandler;
|
25 | 25 | import com.optimizely.ab.bucketing.Bucketer;
|
26 | 26 | import com.optimizely.ab.bucketing.DecisionService;
|
27 |
| -import com.optimizely.ab.config.Attribute; |
28 | 27 | import com.optimizely.ab.config.Experiment;
|
29 | 28 | import com.optimizely.ab.config.ProjectConfig;
|
30 | 29 | import com.optimizely.ab.config.Variation;
|
31 | 30 | import com.optimizely.ab.event.EventHandler;
|
32 | 31 | import com.optimizely.ab.event.LogEvent;
|
33 | 32 | import com.optimizely.ab.event.internal.EventBuilder;
|
| 33 | +import com.optimizely.ab.notification.ActivateNotificationListener; |
| 34 | +import com.optimizely.ab.notification.NotificationCenter; |
34 | 35 | import com.optimizely.ab.notification.NotificationListener;
|
| 36 | +import com.optimizely.ab.notification.TrackNotificationListener; |
35 | 37 |
|
36 | 38 | import org.junit.Assert;
|
37 | 39 | import org.junit.Before;
|
@@ -132,6 +134,52 @@ public void testGoodActivation() {
|
132 | 134 |
|
133 | 135 | }
|
134 | 136 |
|
| 137 | + @Test |
| 138 | + public void testGoodActivationWithListener() { |
| 139 | + OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, |
| 140 | + logger); |
| 141 | + final boolean[] callbackCalled = new boolean[1]; |
| 142 | + final Variation[] callbackVariation = new Variation[1]; |
| 143 | + |
| 144 | + callbackCalled[0] = false; |
| 145 | + int notificationId = optimizelyClient.getNotificationCenter().addNotification(NotificationCenter.NotificationType.Activate, new ActivateNotificationListener() { |
| 146 | + @Override |
| 147 | + public void onActivate( Experiment experiment, String userId, Map<String, String> attributes, Variation variation, LogEvent event) { |
| 148 | + callbackCalled[0] = true; |
| 149 | + callbackVariation[0] = variation; |
| 150 | + } |
| 151 | + }); |
| 152 | + |
| 153 | + Variation v = optimizelyClient.activate("android_experiment_key", "1"); |
| 154 | + |
| 155 | + assertEquals(v, callbackVariation[0]); |
| 156 | + assertEquals(true, callbackCalled[0]); |
| 157 | + assertEquals(1, notificationId); |
| 158 | + assertTrue(optimizelyClient.getNotificationCenter().removeNotification(notificationId)); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + public void testBadActivationWithListener() { |
| 163 | + OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, |
| 164 | + logger); |
| 165 | + final boolean[] callbackCalled = new boolean[1]; |
| 166 | + |
| 167 | + callbackCalled[0] = false; |
| 168 | + int notificationId = optimizelyClient.getNotificationCenter().addNotification(NotificationCenter.NotificationType.Activate, new TrackNotificationListener() { |
| 169 | + @Override |
| 170 | + public void onTrack(String eventKey, String userId, Map<String, String> attributes, Map<String, ?> eventTags, LogEvent event) { |
| 171 | + callbackCalled[0] = true; |
| 172 | + } |
| 173 | + }); |
| 174 | + |
| 175 | + Variation v = optimizelyClient.activate("android_experiment_key", "userId"); |
| 176 | + |
| 177 | + assertEquals(false, callbackCalled[0]); |
| 178 | + assertTrue(notificationId <= 0); |
| 179 | + assertFalse(optimizelyClient.getNotificationCenter().removeNotification(notificationId)); |
| 180 | + |
| 181 | + } |
| 182 | + |
135 | 183 | @Test
|
136 | 184 | public void testGoodForcedActivation() {
|
137 | 185 | OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
|
@@ -195,7 +243,6 @@ public void testGoodActivationBucketingId() {
|
195 | 243 | attributes.put(DecisionService.BUCKETING_ATTRIBUTE, bucketingId);
|
196 | 244 | Variation v = optimizelyClient.activate("android_experiment_key", "userId", attributes);
|
197 | 245 | verify(bucketer).bucket( experiment, bucketingId);
|
198 |
| - assertNotNull(v); |
199 | 246 | }
|
200 | 247 |
|
201 | 248 | @Test
|
@@ -274,6 +321,51 @@ public void testGoodTrack() {
|
274 | 321 | verifyZeroInteractions(logger);
|
275 | 322 | }
|
276 | 323 |
|
| 324 | + @Test |
| 325 | + public void testBadTrackWithListener() { |
| 326 | + OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, |
| 327 | + logger); |
| 328 | + |
| 329 | + final boolean[] numberOfCalls = new boolean[1]; |
| 330 | + numberOfCalls[0]= false; |
| 331 | + |
| 332 | + int notificationId = optimizelyClient.getNotificationCenter().addNotification(NotificationCenter.NotificationType.Activate, |
| 333 | + new TrackNotificationListener() { |
| 334 | + @Override |
| 335 | + public void onTrack(String eventKey, String userId, Map<String, String> attributes, Map<String, ?> eventTags, LogEvent event) { |
| 336 | + numberOfCalls[0] = true; |
| 337 | + } |
| 338 | + }); |
| 339 | + optimizelyClient.track("test_event", "userId"); |
| 340 | + assertTrue(notificationId <= 0); |
| 341 | + assertFalse(optimizelyClient.getNotificationCenter().removeNotification(notificationId)); |
| 342 | + assertEquals(false, numberOfCalls[0]); |
| 343 | + verifyZeroInteractions(logger); |
| 344 | + |
| 345 | + } |
| 346 | + |
| 347 | + @Test |
| 348 | + public void testGoodTrackWithListener() { |
| 349 | + OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, |
| 350 | + logger); |
| 351 | + |
| 352 | + final boolean[] numberOfCalls = new boolean[1]; |
| 353 | + numberOfCalls[0]= false; |
| 354 | + |
| 355 | + int notificationId = optimizelyClient.getNotificationCenter().addNotification(NotificationCenter.NotificationType.Track, |
| 356 | + new TrackNotificationListener() { |
| 357 | + @Override |
| 358 | + public void onTrack(String eventKey, String userId, Map<String, String> attributes, Map<String, ?> eventTags, LogEvent event) { |
| 359 | + numberOfCalls[0] = true; |
| 360 | + } |
| 361 | + }); |
| 362 | + optimizelyClient.track("test_event", "1"); |
| 363 | + assertTrue(notificationId > 0); |
| 364 | + assertTrue(optimizelyClient.getNotificationCenter().removeNotification(notificationId)); |
| 365 | + assertEquals(true, numberOfCalls[0]); |
| 366 | + verifyZeroInteractions(logger); |
| 367 | + } |
| 368 | + |
277 | 369 | @Test
|
278 | 370 | public void testGoodTrackBucketing() {
|
279 | 371 | OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
|
@@ -635,7 +727,6 @@ public void testGoodGetVariationBucketingId() {
|
635 | 727 | attributes.put(DecisionService.BUCKETING_ATTRIBUTE, bucketingId);
|
636 | 728 | Variation v = optimizelyClient.getVariation("android_experiment_key", "userId", attributes);
|
637 | 729 | verify(bucketer).bucket(experiment, bucketingId);
|
638 |
| - assertNotNull(v); |
639 | 730 | }
|
640 | 731 |
|
641 | 732 | @Test
|
|
0 commit comments