Skip to content

Commit aa4771d

Browse files
refactor notification listeners for lambda. update test-app to use android java_8 (#189)
1 parent 184d9e6 commit aa4771d

File tree

6 files changed

+40
-222
lines changed

6 files changed

+40
-222
lines changed

android-sdk/src/androidTest/java/com/optimizely/ab/android/sdk/OptimizelyClientTest.java

Lines changed: 10 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public void testGoodActivationWithListener() {
188188
final Variation[] callbackVariation = new Variation[1];
189189

190190
callbackCalled[0] = false;
191-
int notificationId = optimizelyClient.getNotificationCenter().addNotification(NotificationCenter.NotificationType.Activate, new ActivateNotificationListener() {
191+
int notificationId = optimizelyClient.getNotificationCenter().addNotificationListener(NotificationCenter.NotificationType.Activate, new ActivateNotificationListener() {
192192
@Override
193193
public void onActivate(@Nonnull Experiment experiment, @Nonnull String userId, @Nonnull Map<String, String> attributes, @Nonnull Variation variation, @Nonnull LogEvent event) {
194194
callbackCalled[0] = true;
@@ -202,13 +202,13 @@ public void onActivate(@Nonnull Experiment experiment, @Nonnull String userId, @
202202
assertEquals(v, callbackVariation[0]);
203203
assertEquals(true, callbackCalled[0]);
204204
assertEquals(1, notificationId);
205-
assertTrue(optimizelyClient.getNotificationCenter().removeNotification(notificationId));
205+
assertTrue(optimizelyClient.getNotificationCenter().removeNotificationListener(notificationId));
206206
}
207207
else {
208208
assertNull(v);
209209
assertEquals(false, callbackCalled[0]);
210210
assertEquals(1, notificationId);
211-
assertTrue(optimizelyClient.getNotificationCenter().removeNotification(notificationId));
211+
assertTrue(optimizelyClient.getNotificationCenter().removeNotificationListener(notificationId));
212212
}
213213

214214
}
@@ -220,7 +220,7 @@ public void testBadActivationWithListener() {
220220
final boolean[] callbackCalled = new boolean[1];
221221

222222
callbackCalled[0] = false;
223-
int notificationId = optimizelyClient.getNotificationCenter().addNotification(NotificationCenter.NotificationType.Activate, new TrackNotificationListener() {
223+
int notificationId = optimizelyClient.getNotificationCenter().addNotificationListener(NotificationCenter.NotificationType.Activate, new TrackNotificationListener() {
224224
@Override
225225
public void onTrack(@Nonnull String eventKey, @Nonnull String userId, @Nonnull Map<String, String> attributes, @Nonnull Map<String, ?> eventTags, @Nonnull LogEvent event) {
226226
callbackCalled[0] = true;
@@ -231,7 +231,7 @@ public void onTrack(@Nonnull String eventKey, @Nonnull String userId, @Nonnull M
231231

232232
assertEquals(false, callbackCalled[0]);
233233
assertTrue(notificationId <= 0);
234-
assertFalse(optimizelyClient.getNotificationCenter().removeNotification(notificationId));
234+
assertFalse(optimizelyClient.getNotificationCenter().removeNotificationListener(notificationId));
235235

236236
}
237237

@@ -404,7 +404,7 @@ public void testBadTrackWithListener() {
404404
final boolean[] numberOfCalls = new boolean[1];
405405
numberOfCalls[0]= false;
406406

407-
int notificationId = optimizelyClient.getNotificationCenter().addNotification(NotificationCenter.NotificationType.Activate,
407+
int notificationId = optimizelyClient.getNotificationCenter().addNotificationListener(NotificationCenter.NotificationType.Activate,
408408
new TrackNotificationListener() {
409409
@Override
410410
public void onTrack(@Nonnull String eventKey, @Nonnull String userId, @Nonnull Map<String, String> attributes, @Nonnull Map<String, ?> eventTags, @Nonnull LogEvent event) {
@@ -413,7 +413,7 @@ public void onTrack(@Nonnull String eventKey, @Nonnull String userId, @Nonnull M
413413
});
414414
optimizelyClient.track("test_event", GENERIC_USER_ID);
415415
assertTrue(notificationId <= 0);
416-
assertFalse(optimizelyClient.getNotificationCenter().removeNotification(notificationId));
416+
assertFalse(optimizelyClient.getNotificationCenter().removeNotificationListener(notificationId));
417417
assertEquals(false, numberOfCalls[0]);
418418
verifyZeroInteractions(logger);
419419

@@ -427,7 +427,7 @@ public void testGoodTrackWithListener() {
427427
final boolean[] numberOfCalls = new boolean[1];
428428
numberOfCalls[0]= false;
429429

430-
int notificationId = optimizelyClient.getNotificationCenter().addNotification(NotificationCenter.NotificationType.Track,
430+
int notificationId = optimizelyClient.getNotificationCenter().addNotificationListener(NotificationCenter.NotificationType.Track,
431431
new TrackNotificationListener() {
432432
@Override
433433
public void onTrack(@Nonnull String eventKey, @Nonnull String userId, @Nonnull Map<String, String> attributes, @Nonnull Map<String, ?> eventTags, @Nonnull LogEvent event) {
@@ -436,7 +436,7 @@ public void onTrack(@Nonnull String eventKey, @Nonnull String userId, @Nonnull M
436436
});
437437
optimizelyClient.track("test_event", GENERIC_USER_ID);
438438
assertTrue(notificationId > 0);
439-
assertTrue(optimizelyClient.getNotificationCenter().removeNotification(notificationId));
439+
assertTrue(optimizelyClient.getNotificationCenter().removeNotificationListener(notificationId));
440440
if (datafileVersion == 3) {
441441
assertEquals(true, numberOfCalls[0]);
442442
}
@@ -1051,77 +1051,6 @@ public void testDefaultAttributes() {
10511051
Assert.assertEquals(map.size(), 4);
10521052
}
10531053

1054-
//======== Notification listeners ========//
1055-
1056-
@Test
1057-
public void testGoodAddNotificationListener() {
1058-
OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely,
1059-
logger);
1060-
NotificationListener listener = new NotificationListener() {
1061-
@Override
1062-
public void onExperimentActivated(Experiment experiment,
1063-
String s,
1064-
Map<String, String> map,
1065-
Variation variation) {
1066-
}
1067-
1068-
@Override
1069-
public void notify(Object... args) {}
1070-
};
1071-
optimizelyClient.addNotificationListener(listener);
1072-
optimizelyClient.removeNotificationListener(listener);
1073-
verifyZeroInteractions(logger);
1074-
}
1075-
1076-
@Test
1077-
public void testBadAddNotificationListener() {
1078-
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
1079-
NotificationListener listener = new NotificationListener() {
1080-
@Override
1081-
public void onExperimentActivated(Experiment experiment,
1082-
String s,
1083-
Map<String, String> map,
1084-
Variation variation) {
1085-
}
1086-
1087-
@Override
1088-
public void notify(Object... args) {}
1089-
};
1090-
optimizelyClient.addNotificationListener(listener);
1091-
verify(logger).warn("Optimizely is not initialized, could not add notification listener");
1092-
}
1093-
1094-
@Test
1095-
public void testBadRemoveNotificationListener() {
1096-
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
1097-
NotificationListener listener = new NotificationListener() {
1098-
@Override
1099-
public void onExperimentActivated(Experiment experiment,
1100-
String s,
1101-
Map<String, String> map,
1102-
Variation variation) {
1103-
}
1104-
@Override
1105-
public void notify(Object... args) {}
1106-
};
1107-
optimizelyClient.removeNotificationListener(listener);
1108-
verify(logger).warn("Optimizely is not initialized, could not remove notification listener");
1109-
}
1110-
1111-
@Test
1112-
public void testGoodClearNotificationListeners() {
1113-
OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
1114-
optimizelyClient.clearNotificationListeners();
1115-
verifyZeroInteractions(logger);
1116-
}
1117-
1118-
@Test
1119-
public void testBadClearNotificationListeners() {
1120-
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
1121-
optimizelyClient.clearNotificationListeners();
1122-
verify(logger).warn("Optimizely is not initialized, could not clear notification listeners");
1123-
}
1124-
11251054
//Feature variation Testing
11261055

11271056
//Test when optimizelyClient initialized with valid optimizely and without attributes
@@ -1252,6 +1181,7 @@ public void testGetEnabledFeaturesWithValidUserID(){
12521181
optimizely,
12531182
logger
12541183
);
1184+
12551185
List<String> enabledFeatures = optimizelyClient.getEnabledFeatures(GENERIC_USER_ID,
12561186
Collections.singletonMap("house", "Gryffindor"));
12571187
assertFalse(enabledFeatures.isEmpty());

android-sdk/src/main/java/com/optimizely/ab/android/sdk/OptimizelyClient.java

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -298,51 +298,7 @@ public boolean setForcedVariation(@NonNull String experimentKey,
298298
return null;
299299
}
300300

301-
//======== Notification listeners ========//
302-
303-
/**
304-
* Add a {@link NotificationListener} if it does not exist already.
305-
* <p>
306-
* Listeners are held by weak reference and may automatically be garbage collected. You may
307-
* need to re-register them, for example if your Activity subclass implements the listener
308-
* interface, you will need to re-register the listener on each onCreate.
309-
*
310-
* @param listener listener to add
311-
*/
312-
@Deprecated
313-
public void addNotificationListener(@NonNull NotificationListener listener) {
314-
if (isValid()) {
315-
optimizely.addNotificationListener(listener);
316-
} else {
317-
logger.warn("Optimizely is not initialized, could not add notification listener");
318-
}
319-
}
320-
321-
/**
322-
* Remove a {@link NotificationListener} if it exists.
323-
*
324-
* @param listener listener to remove
325-
*/
326-
@Deprecated
327-
public void removeNotificationListener(@NonNull NotificationListener listener) {
328-
if (isValid()) {
329-
optimizely.removeNotificationListener(listener);
330-
} else {
331-
logger.warn("Optimizely is not initialized, could not remove notification listener");
332-
}
333-
}
334-
335-
/**
336-
* Remove all {@link NotificationListener} instances.
337-
*/
338-
@Deprecated
339-
public void clearNotificationListeners() {
340-
if (isValid()) {
341-
optimizely.clearNotificationListeners();
342-
} else {
343-
logger.warn("Optimizely is not initialized, could not clear notification listeners");
344-
}
345-
}
301+
//======== FeatureFlag APIs ========//
346302

347303
/**
348304
* Get the list of features that are enabled for the user.
@@ -360,7 +316,6 @@ public List<String> getEnabledFeatures(@NonNull String userId, @NonNull Map<Stri
360316
return null;
361317
}
362318
}
363-
//======== FeatureFlag APIs ========//
364319

365320
/**
366321
* Determine whether a feature is enabled for a user.

android-sdk/src/test/java/com/optimizely/ab/android/sdk/OptimizelyClientTest.java

Lines changed: 11 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,8 @@ public void onActivate(Experiment experiment,String userId, Map<String, String>
239239
}
240240
};
241241

242-
int notificationId = optimizelyClient.getNotificationCenter().addNotification(NotificationCenter.NotificationType.Activate, listener);
243-
verify(optimizely.notificationCenter).addNotification(NotificationCenter.NotificationType.Activate, listener);
242+
int notificationId = optimizelyClient.getNotificationCenter().addNotificationListener(NotificationCenter.NotificationType.Activate, listener);
243+
verify(optimizely.notificationCenter).addNotificationListener(NotificationCenter.NotificationType.Activate, listener);
244244
}
245245

246246
@Test
@@ -252,8 +252,8 @@ public void onActivate(Experiment experiment, String userId, Map<String, String>
252252

253253
}
254254
};
255-
optimizelyClient.getNotificationCenter().addNotification(NotificationCenter.NotificationType.Activate, listener);
256-
verify(optimizely.notificationCenter).addNotification(NotificationCenter.NotificationType.Activate, listener);
255+
optimizelyClient.getNotificationCenter().addNotificationListener(NotificationCenter.NotificationType.Activate, listener);
256+
verify(optimizely.notificationCenter).addNotificationListener(NotificationCenter.NotificationType.Activate, listener);
257257
}
258258

259259
@Test
@@ -265,9 +265,9 @@ public void onTrack( String eventKey, String userId, Map<String, String> attribu
265265

266266
}
267267
};
268-
int note = optimizelyClient.getNotificationCenter().addNotification(NotificationCenter.NotificationType.Track, listener);
269-
optimizelyClient.getNotificationCenter().removeNotification(note);
270-
verify(optimizely.notificationCenter).removeNotification(note);
268+
int note = optimizelyClient.getNotificationCenter().addNotificationListener(NotificationCenter.NotificationType.Track, listener);
269+
optimizelyClient.getNotificationCenter().removeNotificationListener(note);
270+
verify(optimizely.notificationCenter).removeNotificationListener(note);
271271
}
272272

273273
@Test
@@ -276,106 +276,24 @@ public void testBadRemoveNotificationCenterListener() {
276276

277277
NotificationCenter notificationCenter = optimizelyClient.getNotificationCenter() != null ?
278278
optimizelyClient.getNotificationCenter() : new NotificationCenter();
279-
notificationCenter.removeNotification(1);
279+
notificationCenter.removeNotificationListener(1);
280280
verify(logger).warn("Optimizely is not initialized, could not get the notification listener");
281281
}
282282

283283
@Test
284284
public void testGoodClearNotificationCenterListeners() {
285285
OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
286-
optimizelyClient.getNotificationCenter().clearAllNotifications();
287-
verify(optimizely.notificationCenter).clearAllNotifications();
286+
optimizelyClient.getNotificationCenter().clearAllNotificationListeners();
287+
verify(optimizely.notificationCenter).clearAllNotificationListeners();
288288
}
289289

290290
@Test
291291
public void testBadClearNotificationCenterListeners() {
292292
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
293293
NotificationCenter notificationCenter = optimizelyClient.getNotificationCenter() != null ?
294294
optimizelyClient.getNotificationCenter() : new NotificationCenter();
295-
notificationCenter.clearAllNotifications();
295+
notificationCenter.clearAllNotificationListeners();
296296
verify(logger).warn("Optimizely is not initialized, could not get the notification listener");
297297
}
298298

299-
@Test
300-
public void testGoodAddNotificationListener() {
301-
OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
302-
NotificationListener listener = new NotificationListener() {
303-
@Override
304-
public void onExperimentActivated(Experiment experiment,
305-
String s,
306-
Map<String, String> map,
307-
Variation variation) {
308-
}
309-
@Override
310-
public void notify(Object... args) {}
311-
};
312-
optimizelyClient.addNotificationListener(listener);
313-
verify(optimizely).addNotificationListener(listener);
314-
}
315-
316-
@Test
317-
public void testBadAddNotificationListener() {
318-
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
319-
NotificationListener listener = new NotificationListener() {
320-
@Override
321-
public void onExperimentActivated(Experiment experiment,
322-
String s,
323-
Map<String, String> map,
324-
Variation variation) {
325-
}
326-
@Override
327-
public void notify(Object... args) {}
328-
};
329-
optimizelyClient.addNotificationListener(listener);
330-
verify(logger).warn("Optimizely is not initialized, could not add notification listener");
331-
}
332-
333-
@Test
334-
public void testGoodRemoveNotificationListener() {
335-
OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
336-
NotificationListener listener = new NotificationListener() {
337-
@Override
338-
public void onExperimentActivated(Experiment experiment,
339-
String s,
340-
Map<String, String> map,
341-
Variation variation) {
342-
}
343-
@Override
344-
public void notify(Object... args) {}
345-
};
346-
optimizelyClient.removeNotificationListener(listener);
347-
verify(optimizely).removeNotificationListener(listener);
348-
}
349-
350-
@Test
351-
public void testBadRemoveNotificationListener() {
352-
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
353-
NotificationListener listener = new NotificationListener() {
354-
@Override
355-
public void onExperimentActivated(Experiment experiment,
356-
String s,
357-
Map<String, String> map,
358-
Variation variation) {
359-
}
360-
@Override
361-
public void notify(Object... args) {}
362-
};
363-
optimizelyClient.removeNotificationListener(listener);
364-
verify(logger).warn("Optimizely is not initialized, could not remove notification listener");
365-
}
366-
367-
@Test
368-
public void testGoodClearNotificationListeners() {
369-
OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
370-
optimizelyClient.clearNotificationListeners();
371-
verify(optimizely).clearNotificationListeners();
372-
}
373-
374-
@Test
375-
public void testBadClearNotificationListeners() {
376-
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
377-
optimizelyClient.clearNotificationListeners();
378-
verify(logger).warn("Optimizely is not initialized, could not clear notification listeners");
379-
}
380-
381299
}

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ ext {
5353
build_tools_version = "27.0.0"
5454
min_sdk_version = 10
5555
target_sdk_version = 26
56-
java_core_ver = "2.0.0-beta7"
56+
java_core_ver = "2.0.0-beta8"
5757
android_logger_ver = "1.3.6"
5858
support_annotations_ver = "24.2.1"
5959
junit_ver = "4.12"

0 commit comments

Comments
 (0)