Skip to content

Commit 6517f4a

Browse files
authored
Merge pull request #57 from optimizely/elliot/notification
Elliot/notification
2 parents a00b1f6 + 3b935f3 commit 6517f4a

File tree

3 files changed

+125
-1
lines changed

3 files changed

+125
-1
lines changed

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.optimizely.ab.UnknownEventTypeException;
2525
import com.optimizely.ab.config.Experiment;
2626
import com.optimizely.ab.config.Variation;
27+
import com.optimizely.ab.notification.NotificationListener;
2728

2829
import org.slf4j.Logger;
2930

@@ -348,4 +349,47 @@ public void track(@NonNull String eventName,
348349
return null;
349350
}
350351
}
352+
353+
//======== Notification listeners ========//
354+
355+
/**
356+
* Add a {@link NotificationListener} if it does not exist already.
357+
* <p>
358+
* Listeners are held by weak reference and may automatically be garbage collected. You may
359+
* need to re-register them, for example if your Activity subclass implements the listener
360+
* interface, you will need to re-register the listener on each onCreate.
361+
*
362+
* @param listener listener to add
363+
*/
364+
public void addNotificationListener(@NonNull NotificationListener listener) {
365+
if (optimizely != null) {
366+
optimizely.addNotificationListener(listener);
367+
} else {
368+
logger.warn("Optimizely is not initialized, could not add notification listener");
369+
}
370+
}
371+
372+
/**
373+
* Remove a {@link NotificationListener} if it exists.
374+
*
375+
* @param listener listener to remove
376+
*/
377+
public void removeNotificationListener(@NonNull NotificationListener listener) {
378+
if (optimizely != null) {
379+
optimizely.removeNotificationListener(listener);
380+
} else {
381+
logger.warn("Optimizely is not initialized, could not remove notification listener");
382+
}
383+
}
384+
385+
/**
386+
* Remove all {@link NotificationListener} instances.
387+
*/
388+
public void clearNotificationListeners() {
389+
if (optimizely != null) {
390+
optimizely.clearNotificationListeners();
391+
} else {
392+
logger.warn("Optimizely is not initialized, could not clear notification listeners");
393+
}
394+
}
351395
}

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

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
package com.optimizely.ab.android.sdk;
1818

1919
import com.optimizely.ab.Optimizely;
20+
import com.optimizely.ab.config.Experiment;
21+
import com.optimizely.ab.config.Variation;
22+
import com.optimizely.ab.notification.NotificationListener;
2023

2124
import org.junit.Test;
2225
import org.junit.runner.RunWith;
@@ -26,6 +29,7 @@
2629

2730
import java.util.Collections;
2831
import java.util.HashMap;
32+
import java.util.Map;
2933

3034
import static junit.framework.Assert.assertTrue;
3135
import static junit.framework.Assert.assertFalse;
@@ -250,4 +254,80 @@ public void testBadGetVariableFloat() {
250254
verify(logger).warn("Optimizely is not initialized, could not get live variable {} " +
251255
"for user {}", "test_key", "userId");
252256
}
257+
258+
//======== Notification listeners ========//
259+
260+
@Test
261+
public void testGoodAddNotificationListener() {
262+
OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
263+
NotificationListener listener = new NotificationListener() {
264+
@Override
265+
public void onExperimentActivated(Experiment experiment,
266+
String s,
267+
Map<String, String> map,
268+
Variation variation) {
269+
}
270+
};
271+
optimizelyClient.addNotificationListener(listener);
272+
verify(optimizely).addNotificationListener(listener);
273+
}
274+
275+
@Test
276+
public void testBadAddNotificationListener() {
277+
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
278+
NotificationListener listener = new NotificationListener() {
279+
@Override
280+
public void onExperimentActivated(Experiment experiment,
281+
String s,
282+
Map<String, String> map,
283+
Variation variation) {
284+
}
285+
};
286+
optimizelyClient.addNotificationListener(listener);
287+
verify(logger).warn("Optimizely is not initialized, could not add notification listener");
288+
}
289+
290+
@Test
291+
public void testGoodRemoveNotificationListener() {
292+
OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
293+
NotificationListener listener = new NotificationListener() {
294+
@Override
295+
public void onExperimentActivated(Experiment experiment,
296+
String s,
297+
Map<String, String> map,
298+
Variation variation) {
299+
}
300+
};
301+
optimizelyClient.removeNotificationListener(listener);
302+
verify(optimizely).removeNotificationListener(listener);
303+
}
304+
305+
@Test
306+
public void testBadRemoveNotificationListener() {
307+
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
308+
NotificationListener listener = new NotificationListener() {
309+
@Override
310+
public void onExperimentActivated(Experiment experiment,
311+
String s,
312+
Map<String, String> map,
313+
Variation variation) {
314+
}
315+
};
316+
optimizelyClient.removeNotificationListener(listener);
317+
verify(logger).warn("Optimizely is not initialized, could not remove notification listener");
318+
}
319+
320+
@Test
321+
public void testGoodClearNotificationListeners() {
322+
OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
323+
optimizelyClient.clearNotificationListeners();
324+
verify(optimizely).clearNotificationListeners();
325+
}
326+
327+
@Test
328+
public void testBadClearNotificationListeners() {
329+
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
330+
optimizelyClient.clearNotificationListeners();
331+
verify(logger).warn("Optimizely is not initialized, could not clear notification listeners");
332+
}
253333
}

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ buildscript {
3333
jcenter()
3434
}
3535
dependencies {
36-
classpath 'com.android.tools.build:gradle:2.2.2'
36+
classpath 'com.android.tools.build:gradle:2.2.3'
3737

3838
// NOTE: Do not place your application dependencies here; they belong
3939
// in the individual module build.gradle files

0 commit comments

Comments
 (0)