Skip to content

Commit 1701ef8

Browse files
add test coverage and disable flakey test (#142)
* add test coverage and disable flakey test * remove blank test
1 parent 4df876d commit 1701ef8

File tree

4 files changed

+96
-9
lines changed

4 files changed

+96
-9
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@
2626
import android.support.test.runner.AndroidJUnit4;
2727

2828
import com.optimizely.ab.Optimizely;
29+
import com.optimizely.ab.OptimizelyRuntimeException;
2930
import com.optimizely.ab.android.datafile_handler.DefaultDatafileHandler;
3031
import com.optimizely.ab.android.event_handler.DefaultEventHandler;
3132
import com.optimizely.ab.android.user_profile.DefaultUserProfileService;
3233
import com.optimizely.ab.config.Experiment;
3334
import com.optimizely.ab.config.ProjectConfig;
3435
import com.optimizely.ab.config.Variation;
36+
import com.optimizely.ab.error.ErrorHandler;
3537
import com.optimizely.ab.event.internal.payload.Event;
3638
import com.optimizely.ab.notification.NotificationListener;
3739

@@ -72,16 +74,25 @@ public void setUp() throws Exception {
7274

7375
@Test
7476
public void testBuilderWith() {
77+
ErrorHandler errorHandler = new ErrorHandler() {
78+
@Override
79+
public <T extends OptimizelyRuntimeException> void handleError(T exception) throws T {
80+
logger.error("Inside error handler", exception);
81+
}
82+
};
83+
7584
OptimizelyManager manager = OptimizelyManager.builder(testProjectId).withUserProfileService(DefaultUserProfileService.newInstance(testProjectId, InstrumentationRegistry.getTargetContext()))
7685
.withDatafileDownloadInterval(30L)
7786
.withEventDispatchInterval(30L)
7887
.withDatafileHandler(new DefaultDatafileHandler())
88+
.withErrorHandler(errorHandler)
7989
.withEventHandler(DefaultEventHandler.getInstance(InstrumentationRegistry.getTargetContext()))
8090
.withLogger(logger).build(InstrumentationRegistry.getTargetContext());
8191

8292
assertNotNull(manager);
8393
assertNotNull(manager.getDatafileHandler());
8494
assertNotNull(manager.getUserProfileService());
95+
assertNotNull(manager.getErrorHandler(InstrumentationRegistry.getTargetContext()));
8596
assertNotNull(manager.getEventHandler(InstrumentationRegistry.getTargetContext()));
8697
}
8798

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import android.content.pm.PackageManager;
2424
import android.os.Build;
2525
import android.support.annotation.RequiresApi;
26+
import android.support.test.InstrumentationRegistry;
2627
import android.support.test.espresso.core.deps.guava.util.concurrent.ListeningExecutorService;
2728
import android.support.test.espresso.core.deps.guava.util.concurrent.MoreExecutors;
2829
import android.support.test.runner.AndroidJUnit4;
@@ -56,6 +57,8 @@
5657
import static org.mockito.Mockito.verify;
5758
import static org.mockito.Mockito.when;
5859

60+
import com.optimizely.ab.android.sdk.test.R;
61+
5962
/**
6063
* Tests for {@link OptimizelyManager}
6164
*/
@@ -106,6 +109,20 @@ public void initialize() {
106109

107110
}
108111

112+
@Test
113+
public void initializeInt() {
114+
115+
optimizelyManager.initialize(InstrumentationRegistry.getTargetContext(), R.raw.datafile);
116+
117+
assertEquals(optimizelyManager.isDatafileCached(InstrumentationRegistry.getTargetContext()), false);
118+
119+
assertEquals(OptimizelyManager.getDatafileUrl("1"), "https://cdn.optimizely.com/public/1/datafile_v3.json" );
120+
121+
assertNotNull(optimizelyManager.getOptimizely());
122+
assertNotNull(optimizelyManager.getDatafileHandler());
123+
124+
}
125+
109126
@Test
110127
public void initializeWithEmptyDatafile() {
111128
Context context = mock(Context.class);
@@ -145,6 +162,19 @@ public void initializeWithNullDatafile() {
145162
verify(logger).error(eq("Unable to parse compiled data file"), any(ConfigParseException.class));
146163
}
147164

165+
@Test
166+
public void load() {
167+
Context context = mock(Context.class);
168+
Context appContext = mock(Context.class);
169+
when(context.getApplicationContext()).thenReturn(appContext);
170+
when(appContext.getPackageName()).thenReturn("com.optly");
171+
172+
String emptyString = null;
173+
174+
optimizelyManager.initialize(context, emptyString);
175+
verify(logger).error(eq("Unable to parse compiled data file"), any(ConfigParseException.class));
176+
}
177+
148178
@Test
149179
public void stop() {
150180
Context context = mock(Context.class);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"groups": [], "projectId": "8504447126", "variables": [{"defaultValue": "true", "type": "boolean", "id": "8516291943", "key": "test_variable"}], "version": "3", "experiments": [{"status": "Running", "key": "android_experiment_key", "layerId": "8499056327", "trafficAllocation": [{"entityId": "8509854340", "endOfRange": 5000}, {"entityId": "8505434669", "endOfRange": 10000}], "audienceIds": [], "variations": [{"variables": [], "id": "8509854340", "key": "var_1"}, {"variables": [], "id": "8505434669", "key": "var_2"}], "forcedVariations": {}, "id": "8509139139"}], "audiences": [], "anonymizeIP": true, "attributes": [], "revision": "7", "events": [{"experimentIds": ["8509139139"], "id": "8505434668", "key": "test_event"}], "accountId": "8362480420"}

datafile-handler/src/androidTest/java/com/optimizely/ab/android/datafile_handler/DatafileServiceTest.java

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@
3737
import org.junit.Test;
3838
import org.mockito.ArgumentCaptor;
3939
import org.slf4j.Logger;
40+
import org.slf4j.LoggerFactory;
4041

4142
import java.util.concurrent.TimeUnit;
4243
import java.util.concurrent.TimeoutException;
4344

45+
import static android.app.Service.START_FLAG_REDELIVERY;
4446
import static junit.framework.Assert.assertEquals;
4547
import static junit.framework.Assert.assertTrue;
4648
import static junit.framework.Assert.fail;
@@ -57,6 +59,7 @@
5759
public class DatafileServiceTest {
5860

5961
private ListeningExecutorService executor;
62+
private static final int MAX_ITERATION = 100;
6063

6164
@Rule
6265
public final ServiceTestRule mServiceRule = new ServiceTestRule();
@@ -68,11 +71,16 @@ public void setup() {
6871

6972
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
7073
@Test
71-
@Ignore
7274
public void testBinding() throws TimeoutException {
7375
Context context = InstrumentationRegistry.getTargetContext();
7476
Intent intent = new Intent(context, DatafileService.class);
75-
IBinder binder = mServiceRule.bindService(intent);
77+
IBinder binder = null;
78+
int it = 0;
79+
80+
while((binder = mServiceRule.bindService(intent)) == null && it < MAX_ITERATION){
81+
it++;
82+
}
83+
7684
final Context targetContext = InstrumentationRegistry.getTargetContext();
7785
Logger logger = mock(Logger.class);
7886
DatafileCache datafileCache = new DatafileCache("1", new Cache(targetContext, logger), logger);
@@ -90,25 +98,36 @@ public void testBinding() throws TimeoutException {
9098

9199
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
92100
@Test
93-
@Ignore
94101
public void testValidStart() throws TimeoutException {
95102
Context context = InstrumentationRegistry.getTargetContext();
96103
Intent intent = new Intent(context, DatafileService.class);
97-
IBinder binder = mServiceRule.bindService(intent);
104+
IBinder binder = null;
105+
int it = 0;
106+
107+
while((binder = mServiceRule.bindService(intent)) == null && it < MAX_ITERATION){
108+
it++;
109+
}
110+
98111
intent.putExtra(DatafileService.EXTRA_PROJECT_ID, "1");
99112
DatafileService datafileService = ((DatafileService.LocalBinder) binder).getService();
100113
Logger logger = mock(Logger.class);
101114
datafileService.logger = logger;
102-
datafileService.onStartCommand(intent, 0, 0);
103-
verify(logger).info("Started watching project {} in the background", "1");
115+
int val = datafileService.onStartCommand(intent, 0, 0);
116+
assertEquals(val, START_FLAG_REDELIVERY);
104117
}
105118

106119
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
107120
@Test
108121
public void testNullIntentStart() throws TimeoutException {
109122
Context context = InstrumentationRegistry.getTargetContext();
110123
Intent intent = new Intent(context, DatafileService.class);
111-
IBinder binder = mServiceRule.bindService(intent);
124+
IBinder binder = null;
125+
int it = 0;
126+
127+
while((binder = mServiceRule.bindService(intent)) == null && it < MAX_ITERATION){
128+
it++;
129+
}
130+
mServiceRule.bindService(intent);
112131
DatafileService datafileService = ((DatafileService.LocalBinder) binder).getService();
113132
Logger logger = mock(Logger.class);
114133
datafileService.logger = logger;
@@ -118,11 +137,16 @@ public void testNullIntentStart() throws TimeoutException {
118137

119138
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
120139
@Test
121-
@Ignore
122140
public void testNoProjectIdIntentStart() throws TimeoutException {
123141
Context context = InstrumentationRegistry.getTargetContext();
124142
Intent intent = new Intent(context, DatafileService.class);
125-
IBinder binder = mServiceRule.bindService(intent);
143+
IBinder binder = null;
144+
int it = 0;
145+
146+
while((binder = mServiceRule.bindService(intent)) == null && it < MAX_ITERATION){
147+
it++;
148+
}
149+
126150
DatafileService datafileService = ((DatafileService.LocalBinder) binder).getService();
127151
Logger logger = mock(Logger.class);
128152
datafileService.logger = logger;
@@ -132,6 +156,27 @@ public void testNoProjectIdIntentStart() throws TimeoutException {
132156

133157
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
134158
@Test
159+
public void testUnbind() throws TimeoutException {
160+
Context context = InstrumentationRegistry.getTargetContext();
161+
Intent intent = new Intent(context, DatafileService.class);
162+
IBinder binder = null;
163+
int it = 0;
164+
165+
while((binder = mServiceRule.bindService(intent)) == null && it < MAX_ITERATION){
166+
it++;
167+
}
168+
169+
DatafileService datafileService = ((DatafileService.LocalBinder) binder).getService();
170+
Logger logger = mock(Logger.class);
171+
datafileService.logger = logger;
172+
173+
datafileService.onUnbind(intent);
174+
verify(logger).info("All clients are unbound from data file service");
175+
}
176+
177+
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
178+
@Test
179+
@Ignore
135180
public void testIntentExtraData(){
136181
Context context = mock(Context.class);
137182
when(context.getPackageName()).thenReturn("com.optly");

0 commit comments

Comments
 (0)