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
7 changes: 4 additions & 3 deletions .idea/gradle.xml

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

2 changes: 1 addition & 1 deletion .idea/modules.xml

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

7 changes: 5 additions & 2 deletions android-sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ android {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
testCoverageEnabled true
}
}
}

dependencies {
compile project(':event-handler')
compile project(':project-watcher')

compile project(':user-experiment-record')
compile project(':shared')
provided "com.android.support:support-annotations:$support_annotations_ver"

testCompile "junit:junit:$junit_ver"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.optimizely.ab.android.project_watcher;
package com.optimizely.ab.android.sdk;

import android.support.test.InstrumentationRegistry;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.optimizely.ab.android.project_watcher;
package com.optimizely.ab.android.sdk;

import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.optimizely.ab.android.project_watcher;
package com.optimizely.ab.android.sdk;


import com.optimizely.ab.android.shared.Client;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.optimizely.ab.android.project_watcher;
package com.optimizely.ab.android.sdk;

import org.junit.Before;
import org.junit.Test;
Expand All @@ -24,23 +24,23 @@ public class DataFileLoaderTest {
DataFileService datafileService;
DataFileCache dataFileCache;
DataFileClient dataFileClient;
OnDataFileLoadedListener onDataFileLoadedListener;
DataFileLoadedListener dataFileLoadedListener;
Logger logger;

@Before
public void setup() {
datafileService = mock(DataFileService.class);
dataFileCache = mock(DataFileCache.class);
dataFileClient = mock(DataFileClient.class);
onDataFileLoadedListener = mock(OnDataFileLoadedListener.class);
dataFileLoadedListener = mock(DataFileLoadedListener.class);
logger = mock(Logger.class);
}

@Test
public void existingDataFileWhenRequestingFromClient() throws MalformedURLException {
DataFileLoader.RequestDataFileFromClientTask task =
new DataFileLoader.RequestDataFileFromClientTask("1", datafileService, dataFileCache,
dataFileClient, onDataFileLoadedListener, logger);
dataFileClient, dataFileLoadedListener, logger);

String url = String.format(DataFileLoader.RequestDataFileFromClientTask.FORMAT_CDN_URL, "1");
when(dataFileClient.request(url)).thenReturn("");
Expand All @@ -56,7 +56,7 @@ public void existingDataFileWhenRequestingFromClient() throws MalformedURLExcept
public void existingDataFileWhenRequestingFromClientFailsToDelete() throws MalformedURLException {
DataFileLoader.RequestDataFileFromClientTask task =
new DataFileLoader.RequestDataFileFromClientTask("1", datafileService, dataFileCache,
dataFileClient, onDataFileLoadedListener, logger);
dataFileClient, dataFileLoadedListener, logger);

String url = String.format(DataFileLoader.RequestDataFileFromClientTask.FORMAT_CDN_URL, "1");
when(dataFileClient.request(url)).thenReturn("");
Expand All @@ -71,7 +71,7 @@ public void existingDataFileWhenRequestingFromClientFailsToDelete() throws Malfo
public void existingDataFileWhenRequestingFromClientFailsToSave() throws MalformedURLException {
DataFileLoader.RequestDataFileFromClientTask task =
new DataFileLoader.RequestDataFileFromClientTask("1", datafileService, dataFileCache,
dataFileClient, onDataFileLoadedListener, logger);
dataFileClient, dataFileLoadedListener, logger);

String url = String.format(DataFileLoader.RequestDataFileFromClientTask.FORMAT_CDN_URL, "1");
when(dataFileClient.request(url)).thenReturn("");
Expand All @@ -86,11 +86,11 @@ public void existingDataFileWhenRequestingFromClientFailsToSave() throws Malform
public void handlesNullDataFile() {
DataFileLoader.RequestDataFileFromClientTask task =
new DataFileLoader.RequestDataFileFromClientTask("1", datafileService, dataFileCache,
dataFileClient, onDataFileLoadedListener, logger);
dataFileClient, dataFileLoadedListener, logger);

when(datafileService.isBound()).thenReturn(false);
task.onPostExecute(null);
verify(onDataFileLoadedListener, never()).onDataFileLoaded("");
verify(dataFileLoadedListener, never()).onDataFileLoaded("");
verify(datafileService).stop();
}

Expand All @@ -103,14 +103,14 @@ public void handlesNullListener() {
when(datafileService.isBound()).thenReturn(false);
task.onPostExecute("");
verify(datafileService).stop();
verify(onDataFileLoadedListener, never()).onDataFileLoaded("");
verify(dataFileLoadedListener, never()).onDataFileLoaded("");
}

@Test
public void noStopIfBound() {
DataFileLoader.RequestDataFileFromClientTask task =
new DataFileLoader.RequestDataFileFromClientTask("1", datafileService, dataFileCache,
dataFileClient, onDataFileLoadedListener, logger);
dataFileClient, dataFileLoadedListener, logger);

when(datafileService.isBound()).thenReturn(true);
task.onPostExecute("");
Expand All @@ -119,32 +119,27 @@ public void noStopIfBound() {

@Test
public void loadFromCache() {
DataFileLoader.RequestDataFileFromClientTask requstTask = mock(DataFileLoader.RequestDataFileFromClientTask.class);
DataFileLoader.LoadDataFileFromCacheTask task = new DataFileLoader.LoadDataFileFromCacheTask(dataFileCache, requstTask, onDataFileLoadedListener);
DataFileLoader.LoadDataFileFromCacheTask task = new DataFileLoader.LoadDataFileFromCacheTask(dataFileCache, dataFileLoadedListener);

task.doInBackground(null);
verify(dataFileCache).load();
}

@Test
public void loadFromCacheNullDataFile() {
DataFileLoader.RequestDataFileFromClientTask requestTask = mock(DataFileLoader.RequestDataFileFromClientTask.class);
DataFileLoader.LoadDataFileFromCacheTask task = new DataFileLoader.LoadDataFileFromCacheTask(dataFileCache, requestTask, onDataFileLoadedListener);
DataFileLoader.LoadDataFileFromCacheTask task = new DataFileLoader.LoadDataFileFromCacheTask(dataFileCache, dataFileLoadedListener);

task.onPostExecute(null);
verify(onDataFileLoadedListener, never()).onDataFileLoaded(any(String.class));
verify(requestTask).start();
verify(dataFileLoadedListener, never()).onDataFileLoaded(any(String.class));
}

@Test
public void getDataFile() {
DataFileLoader.TaskChain taskChain = mock(DataFileLoader.TaskChain.class);
DataFileLoader dataFileLoader = new DataFileLoader(taskChain, logger);
DataFileLoader.LoadDataFileFromCacheTask task = mock(DataFileLoader.LoadDataFileFromCacheTask.class);
when(taskChain.get("1", onDataFileLoadedListener)).thenReturn(task);
assertTrue(dataFileLoader.getDataFile("1", onDataFileLoadedListener));
verify(taskChain).get("1", onDataFileLoadedListener);
verify(task).start();
assertTrue(dataFileLoader.getDataFile("1", dataFileLoadedListener));
verify(taskChain).start("1", dataFileLoadedListener);
verify(logger).info("Refreshing data file");
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.optimizely.ab.android.project_watcher;
package com.optimizely.ab.android.sdk;

import android.content.Context;
import android.content.Intent;
Expand All @@ -10,7 +10,6 @@
import org.slf4j.Logger;

import java.util.Arrays;
import java.util.List;

import static junit.framework.Assert.assertEquals;
import static org.mockito.Matchers.any;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.optimizely.ab.android.project_watcher;
package com.optimizely.ab.android.sdk;

import android.content.Context;
import android.content.Intent;
Expand Down Expand Up @@ -37,9 +37,9 @@ public void testBinding() throws TimeoutException {
IBinder binder = mServiceRule.bindService(intent);
DataFileService dataFileService = ((DataFileService.LocalBinder) binder).getService();
DataFileLoader dataFileLoader = mock(DataFileLoader.class);
OnDataFileLoadedListener onDataFileLoadedListener = mock(OnDataFileLoadedListener.class);
dataFileService.getDataFile("1", dataFileLoader, onDataFileLoadedListener);
verify(dataFileLoader).getDataFile("1", onDataFileLoadedListener);
DataFileLoadedListener dataFileLoadedListener = mock(DataFileLoadedListener.class);
dataFileService.getDataFile("1", dataFileLoader, dataFileLoadedListener);
verify(dataFileLoader).getDataFile("1", dataFileLoadedListener);

assertTrue(dataFileService.isBound());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.optimizely.ab.android.sdk;

import android.content.Context;

import com.optimizely.ab.android.shared.ServiceScheduler;
import com.optimizely.user_experiment_record.AndroidUserExperimentRecord;

import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;

import static junit.framework.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.same;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
* Created by jdeffibaugh on 8/16/16 for Optimizely.
*
* Tests {@link OptimizelyManager.DataFileServiceConnection}
*/
public class OptimizelyManagerDataFileServiceConnectionTest {

OptimizelyManager.DataFileServiceConnection dataFileServiceConnection;
OptimizelyManager optimizelyManager;

@Before
public void setup() {
optimizelyManager = mock(OptimizelyManager.class);
dataFileServiceConnection = new OptimizelyManager.DataFileServiceConnection(optimizelyManager);
}

@Test
public void onServiceConnected() {
DataFileService.LocalBinder binder = mock(DataFileService.LocalBinder.class);
DataFileService service = mock(DataFileService.class);
Context context = mock(Context.class);
when(service.getApplicationContext()).thenReturn(context);
when(binder.getService()).thenReturn(service);
when(optimizelyManager.getProjectId()).thenReturn("1");
ArgumentCaptor<DataFileLoadedListener> captor = ArgumentCaptor.forClass(DataFileLoadedListener.class);
dataFileServiceConnection.onServiceConnected(null, binder);
verify(service).getDataFile(same("1"), any(DataFileLoader.class), captor.capture());
DataFileLoadedListener listener = captor.getValue();
listener.onDataFileLoaded("");
verify(optimizelyManager).injectOptimizely(any(Context.class), any(AndroidUserExperimentRecord.class), any(ServiceScheduler.class), eq(""));
}

@Test
public void onServiceConnectedNotBoundWhenDataFileLoaded() {
DataFileService.LocalBinder binder = mock(DataFileService.LocalBinder.class);
DataFileService service = mock(DataFileService.class);
Context context = mock(Context.class);
when(service.getApplicationContext()).thenReturn(context);
when(binder.getService()).thenReturn(service);
when(optimizelyManager.getProjectId()).thenReturn("1");
ArgumentCaptor<DataFileLoadedListener> captor = ArgumentCaptor.forClass(DataFileLoadedListener.class);
dataFileServiceConnection.onServiceConnected(null, binder);
verify(service).getDataFile(same("1"), any(DataFileLoader.class), captor.capture());

dataFileServiceConnection.onServiceDisconnected(null);

DataFileLoadedListener listener = captor.getValue();
listener.onDataFileLoaded("");

verify(optimizelyManager, never()).injectOptimizely(any(Context.class), any(AndroidUserExperimentRecord.class), any(ServiceScheduler.class), eq(""));
}

@Test
public void onServiceConnectedNullServiceFromBinder() {
DataFileService.LocalBinder binder = mock(DataFileService.LocalBinder.class);
when(binder.getService()).thenReturn(null);

try {
dataFileServiceConnection.onServiceConnected(null, binder);
} catch (NullPointerException e) {
fail();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.optimizely.ab.android.sdk;

import android.app.Activity;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

/**
* Created by jdeffibaugh on 8/16/16 for Optimizely.
*
* Tests for {@link OptimizelyManager.OptlyActivityLifecycleCallbacks}
*/
@RunWith(AndroidJUnit4.class)
public class OptimizelyManagerOptlyActivityLifecycleCallbacksTest {

OptimizelyManager.OptlyActivityLifecycleCallbacks optlyActivityLifecycleCallbacks;
OptimizelyManager optimizelyManager;

@Before
public void setup() {
optimizelyManager = mock(OptimizelyManager.class);
optlyActivityLifecycleCallbacks = new OptimizelyManager.OptlyActivityLifecycleCallbacks(optimizelyManager);
}

@Test
public void onActivityStopped() {
Activity activity = mock(Activity.class);
optlyActivityLifecycleCallbacks.onActivityStopped(activity);
verify(optimizelyManager).stop(activity, optlyActivityLifecycleCallbacks);
}
}
Loading