Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public void injectOptimizely() {
verify(userExperimentRecord).start();
verify(serviceScheduler).schedule(captor.capture(), eq(TimeUnit.HOURS.toMillis(1L)));
verify(logger).info("Sending Optimizely instance to listener");
verify(startListener).onStart(any(AndroidOptimizely.class));
verify(startListener).onStart(any(OptimizelyClient.class));
}

@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
Expand Down Expand Up @@ -204,7 +204,7 @@ public void injectOptimizelyDoesNotDuplicateCallback() {
verify(serviceScheduler).schedule(captor.capture(), eq(TimeUnit.HOURS.toMillis(1L)));

verify(logger).info("Sending Optimizely instance to listener");
verify(startListener).onStart(any(AndroidOptimizely.class));
verify(startListener).onStart(any(OptimizelyClient.class));

optimizelyManager.injectOptimizely(context, userExperimentRecord, serviceScheduler, minDataFile);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@
* Optimizely through this interface checking for null is not required. If Optimizely is null warnings
* will be logged.
*/
public class AndroidOptimizely {
public class OptimizelyClient {

private final Logger logger;

@Nullable private Optimizely optimizely;

AndroidOptimizely(@Nullable Optimizely optimizely, @NonNull Logger logger) {
OptimizelyClient(@Nullable Optimizely optimizely, @NonNull Logger logger) {
this.optimizely = optimizely;
this.logger = logger;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@
* Handles loading the Optimizely data file
*/
public class OptimizelyManager {
@NonNull private AndroidOptimizely androidOptimizely = new AndroidOptimizely(null,
LoggerFactory.getLogger(AndroidOptimizely.class));
@NonNull private OptimizelyClient optimizelyClient = new OptimizelyClient(null,
LoggerFactory.getLogger(OptimizelyClient.class));
@NonNull private final String projectId;
@NonNull private final Long eventHandlerDispatchInterval;
@NonNull private final TimeUnit eventHandlerDispatchIntervalTimeUnit;
Expand Down Expand Up @@ -117,14 +117,14 @@ void setOptimizelyStartListener(@Nullable OptimizelyStartListener optimizelyStar
/**
* Starts Optimizely asynchronously
* <p>
* An {@link AndroidOptimizely} instance will be delivered to
* {@link OptimizelyStartListener#onStart(AndroidOptimizely)}. The callback will only be hit
* An {@link OptimizelyClient} instance will be delivered to
* {@link OptimizelyStartListener#onStart(OptimizelyClient)}. The callback will only be hit
* once. If there is a cached datafile the returned instance will be built from it. The cached
* datafile will be updated from network if it is different from the cache. If there is no
* cached datafile the returned instance will always be built from the remote datafile.
*
* @param activity an Activity, used to automatically unbind {@link DataFileService}
* @param optimizelyStartListener callback that {@link AndroidOptimizely} instances are sent to.
* @param optimizelyStartListener callback that {@link OptimizelyClient} instances are sent to.
*/
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public void start(@NonNull Activity activity, @NonNull OptimizelyStartListener optimizelyStartListener) {
Expand All @@ -141,7 +141,7 @@ public void start(@NonNull Activity activity, @NonNull OptimizelyStartListener o
* This method does the same thing except it can be used with a generic {@link Context}.
* When using this method be sure to call {@link #stop(Context)} to unbind {@link DataFileService}.
* @param context any type of context instance
* @param optimizelyStartListener callback that {@link AndroidOptimizely} instances are sent to.
* @param optimizelyStartListener callback that {@link OptimizelyClient} instances are sent to.
*/
public void start(@NonNull Context context, @NonNull OptimizelyStartListener optimizelyStartListener) {
if (!isAndroidVersionSupported()) {
Expand Down Expand Up @@ -183,36 +183,36 @@ public void stop(@NonNull Context context) {
* Gets a cached Optimizely instance
*
* If {@link #start(Activity, OptimizelyStartListener)} or {@link #start(Context, OptimizelyStartListener)}
* has not been called yet the returned {@link AndroidOptimizely} instance will be a dummy instance
* has not been called yet the returned {@link OptimizelyClient} instance will be a dummy instance
* that logs warnings in order to prevent {@link NullPointerException}.
*
* If {@link #getOptimizely(Context, int)} was used the built {@link AndroidOptimizely} instance
* If {@link #getOptimizely(Context, int)} was used the built {@link OptimizelyClient} instance
* will be updated. Using {@link #start(Activity, OptimizelyStartListener)} or {@link #start(Context, OptimizelyStartListener)}
* will update the cached instance with a new {@link AndroidOptimizely} built from a cached local
* will update the cached instance with a new {@link OptimizelyClient} built from a cached local
* datafile on disk or a remote datafile on the CDN.
* @return the cached instance of {@link AndroidOptimizely}
* @return the cached instance of {@link OptimizelyClient}
*/
@NonNull
public AndroidOptimizely getOptimizely() {
public OptimizelyClient getOptimizely() {
isAndroidVersionSupported();
return androidOptimizely;
return optimizelyClient;
}

/**
* Create an instance of {@link AndroidOptimizely} from a compiled in datafile
* Create an instance of {@link OptimizelyClient} from a compiled in datafile
*
* After using this method successfully {@link #getOptimizely()} will contain a
* cached instance built from the compiled in datafile. The datafile should be
* stored in res/raw.
*
* @param context any {@link Context} instance
* @param dataFileRes the R id that the data file is located under.
* @return an {@link AndroidOptimizely} instance
* @return an {@link OptimizelyClient} instance
*/
@NonNull
public AndroidOptimizely getOptimizely(@NonNull Context context, @RawRes int dataFileRes) {
public OptimizelyClient getOptimizely(@NonNull Context context, @RawRes int dataFileRes) {
if (!isAndroidVersionSupported()) {
return androidOptimizely;
return optimizelyClient;
}

AndroidUserExperimentRecord userExperimentRecord =
Expand All @@ -223,14 +223,14 @@ public AndroidOptimizely getOptimizely(@NonNull Context context, @RawRes int dat
// terribly expensive. Blocking the UI the thread prevents touch input...
userExperimentRecord.start();
try {
androidOptimizely = buildOptimizely(context, loadRawResource(context, dataFileRes), userExperimentRecord);
optimizelyClient = buildOptimizely(context, loadRawResource(context, dataFileRes), userExperimentRecord);
} catch (ConfigParseException e) {
logger.error("Unable to parse compiled data file", e);
} catch (IOException e) {
logger.error("Unable to load compiled data file", e);
}

return androidOptimizely;
return optimizelyClient;
}

private String loadRawResource(Context context, @RawRes int rawRes) throws IOException {
Expand Down Expand Up @@ -266,12 +266,12 @@ protected void onPostExecute(UserExperimentRecord userExperimentRecord) {
serviceScheduler.schedule(intent, dataFileDownloadIntervalTimeUnit.toMillis(dataFileDownloadInterval));

try {
OptimizelyManager.this.androidOptimizely = buildOptimizely(context, dataFile, userExperimentRecord);
OptimizelyManager.this.optimizelyClient = buildOptimizely(context, dataFile, userExperimentRecord);
OptimizelyManager.this.userExperimentRecord = userExperimentRecord;
logger.info("Sending Optimizely instance to listener");

if (optimizelyStartListener != null) {
optimizelyStartListener.onStart(androidOptimizely);
optimizelyStartListener.onStart(optimizelyClient);
// Prevent the onOptimizelyStarted(AndroidOptimizely) callback from being hit twice
// This could happen if the local data file is not null and is different
// from the remote data file. Setting the listener to null handles this case.
Expand All @@ -287,15 +287,15 @@ protected void onPostExecute(UserExperimentRecord userExperimentRecord) {
initUserExperimentRecordTask.executeOnExecutor(executor);
}

private AndroidOptimizely buildOptimizely(@NonNull Context context, @NonNull String dataFile, @NonNull UserExperimentRecord userExperimentRecord) throws ConfigParseException {
private OptimizelyClient buildOptimizely(@NonNull Context context, @NonNull String dataFile, @NonNull UserExperimentRecord userExperimentRecord) throws ConfigParseException {
OptlyEventHandler eventHandler = OptlyEventHandler.getInstance(context);
eventHandler.setDispatchInterval(eventHandlerDispatchInterval, eventHandlerDispatchIntervalTimeUnit);
Optimizely optimizely = Optimizely.builder(dataFile, eventHandler)
.withUserExperimentRecord(userExperimentRecord)
.withClientEngine(Event.ClientEngine.ANDROID_SDK)
.withClientVersion(BuildConfig.CLIENT_VERSION)
.build();
return new AndroidOptimizely(optimizely, LoggerFactory.getLogger(AndroidOptimizely.class));
return new OptimizelyClient(optimizely, LoggerFactory.getLogger(OptimizelyClient.class));
}

@VisibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
import android.content.Context;

/**
* Listens for new instances of {@link AndroidOptimizely} that are built after calling
* Listens for new instances of {@link OptimizelyClient} that are built after calling
* {@link OptimizelyManager#start(Activity, OptimizelyStartListener)} or {@link OptimizelyManager#start(Context, OptimizelyStartListener)}
*/
public interface OptimizelyStartListener {
/**
* Receives a started {@link AndroidOptimizely} instances
* Receives a started {@link OptimizelyClient} instances
*
* @param optimizely an {@link AndroidOptimizely} that is started
* @param optimizely an {@link OptimizelyClient} that is started
*/
void onStart(AndroidOptimizely optimizely);
void onStart(OptimizelyClient optimizely);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,137 +29,137 @@
import static org.mockito.Mockito.verify;

/**
* Tests for {@link AndroidOptimizely}
* Tests for {@link OptimizelyClient}
*/
@RunWith(MockitoJUnitRunner.class)
public class AndroidOptimizelyTest {
public class OptimizelyClientTest {

@Mock Logger logger;
@Mock Optimizely optimizely;

@Test
public void testGoodActivation1() {
AndroidOptimizely androidOptimizely = new AndroidOptimizely(optimizely, logger);
androidOptimizely.activate("1", "1");
OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
optimizelyClient.activate("1", "1");
verify(optimizely).activate("1", "1");
}

@Test
public void testBadActivation1() {
AndroidOptimizely androidOptimizely = new AndroidOptimizely(null, logger);
androidOptimizely.activate("1", "1");
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
optimizelyClient.activate("1", "1");
verify(logger).warn("Optimizely is not initialized, can't activate experiment {} " +
"for user {}", "1", "1");
}

@Test
public void testGoodActivation2() {
AndroidOptimizely androidOptimizely = new AndroidOptimizely(optimizely, logger);
OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
final HashMap<String, String> attributes = new HashMap<>();
androidOptimizely.activate("1", "1", attributes);
optimizelyClient.activate("1", "1", attributes);
verify(optimizely).activate("1", "1", attributes);
}

@Test
public void testBadActivation2() {
AndroidOptimizely androidOptimizely = new AndroidOptimizely(null, logger);
androidOptimizely.activate("1", "1", new HashMap<String, String>());
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
optimizelyClient.activate("1", "1", new HashMap<String, String>());
verify(logger).warn("Optimizely is not initialized, can't activate experiment {} " +
"for user {} with attributes", "1", "1");
}


@Test
public void testGoodTrack1() {
AndroidOptimizely androidOptimizely = new AndroidOptimizely(optimizely, logger);
androidOptimizely.track("event1", "1");
OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
optimizelyClient.track("event1", "1");
verify(optimizely).track("event1", "1");
}

@Test
public void testBadTrack1() {
AndroidOptimizely androidOptimizely = new AndroidOptimizely(null, logger);
androidOptimizely.track("event1", "1");
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
optimizelyClient.track("event1", "1");
verify(logger).warn("Optimizely is not initialized, could not track event {} for user {}", "event1", "1");
}

@Test
public void testGoodTrack2() {
AndroidOptimizely androidOptimizely = new AndroidOptimizely(optimizely, logger);
OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
final HashMap<String, String> attributes = new HashMap<>();
androidOptimizely.track("event1", "1", attributes);
optimizelyClient.track("event1", "1", attributes);
verify(optimizely).track("event1", "1", attributes);
}

@Test
public void testBadTrack2() {
AndroidOptimizely androidOptimizely = new AndroidOptimizely(null, logger);
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
final HashMap<String, String> attributes = new HashMap<>();
androidOptimizely.track("event1", "1", attributes);
optimizelyClient.track("event1", "1", attributes);
verify(logger).warn("Optimizely is not initialized, could not track event {} for user {} " +
"with attributes", "event1", "1");
}

@Test
public void testGoodTrack3() {
AndroidOptimizely androidOptimizely = new AndroidOptimizely(optimizely, logger);
androidOptimizely.track("event1", "1", 1L);
OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
optimizelyClient.track("event1", "1", 1L);
verify(optimizely).track("event1", "1", 1L);
}

@Test
public void testBadTrack3() {
AndroidOptimizely androidOptimizely = new AndroidOptimizely(null, logger);
androidOptimizely.track("event1", "1", 1L);
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
optimizelyClient.track("event1", "1", 1L);
verify(logger).warn("Optimizely is not initialized, could not track event {} for user {} " +
"with value {}", "event1", "1", 1L);
}

@Test
public void testGoodTrack4() {
AndroidOptimizely androidOptimizely = new AndroidOptimizely(optimizely, logger);
OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
final HashMap<String, String> attributes = new HashMap<>();
androidOptimizely.track("event1", "1", attributes, 1L);
optimizelyClient.track("event1", "1", attributes, 1L);
verify(optimizely).track("event1", "1", attributes, 1L);
}

@Test
public void testBadTrack4() {
AndroidOptimizely androidOptimizely = new AndroidOptimizely(null, logger);
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
final HashMap<String, String> attributes = new HashMap<>();
androidOptimizely.track("event1", "1", attributes, 1L);
optimizelyClient.track("event1", "1", attributes, 1L);
verify(logger).warn("Optimizely is not initialized, could not track event {} for user {} " +
"with value {} and attributes", "event1", "1", 1L);
}

@Test
public void testGoodGetVariation1() {
AndroidOptimizely androidOptimizely = new AndroidOptimizely(optimizely, logger);
androidOptimizely.getVariation("1", "1");
OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
optimizelyClient.getVariation("1", "1");
verify(optimizely).getVariation("1", "1");
}

@Test
public void testBadGetVariation1() {
AndroidOptimizely androidOptimizely = new AndroidOptimizely(null, logger);
androidOptimizely.getVariation("1", "1");
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
optimizelyClient.getVariation("1", "1");
verify(logger).warn("Optimizely is not initialized, could not get variation for experiment {} " +
"for user {}", "1", "1");
}

@Test
public void testGoodGetVariation3() {
AndroidOptimizely androidOptimizely = new AndroidOptimizely(optimizely, logger);
OptimizelyClient optimizelyClient = new OptimizelyClient(optimizely, logger);
final HashMap<String, String> attributes = new HashMap<>();
androidOptimizely.getVariation("1", "1", attributes);
optimizelyClient.getVariation("1", "1", attributes);
verify(optimizely).getVariation("1", "1", attributes);
}

@Test
public void testBadGetVariation3() {
AndroidOptimizely androidOptimizely = new AndroidOptimizely(null, logger);
OptimizelyClient optimizelyClient = new OptimizelyClient(null, logger);
final HashMap<String, String> attributes = new HashMap<>();
androidOptimizely.getVariation("1", "1", attributes);
optimizelyClient.getVariation("1", "1", attributes);
verify(logger).warn("Optimizely is not initialized, could not get variation for experiment {} " +
"for user {} with attributes", "1", "1");
}
Expand Down
Loading