Skip to content

Commit cfd85c6

Browse files
authored
Merge pull request #34 from optimizely/josh/test-app-enhancements-2
Gets Espresso tests recording and asserting on events
2 parents 7a5903a + 347a3f6 commit cfd85c6

File tree

21 files changed

+524
-168
lines changed

21 files changed

+524
-168
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ public void track(@NonNull String eventName,
113113
@NonNull Map<String, String> attributes) throws UnknownEventTypeException {
114114
if (optimizely != null) {
115115
optimizely.track(eventName, userId, attributes);
116+
116117
} else {
117118
logger.warn("Optimizely is not initialized, could not track event {} for user {}" +
118119
" with attributes", eventName, userId);

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@
3535
* @hide
3636
*/
3737
public class DataFileService extends Service {
38-
static final String EXTRA_PROJECT_ID = "com.optimizely.ab.android.EXTRA_PROJECT_ID";
38+
/**
39+
* Extra containing the project id this instance of Optimizely was built with
40+
*/
41+
public static final String EXTRA_PROJECT_ID = "com.optimizely.ab.android.EXTRA_PROJECT_ID";
3942
@NonNull private final IBinder binder = new LocalBinder();
4043
Logger logger = LoggerFactory.getLogger(getClass());
4144
private boolean isBound;

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

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import android.support.annotation.Nullable;
3434
import android.support.annotation.RawRes;
3535
import android.support.annotation.RequiresApi;
36+
import android.support.annotation.VisibleForTesting;
3637

3738
import com.optimizely.ab.Optimizely;
3839
import com.optimizely.ab.android.event_handler.OptlyEventHandler;
@@ -66,6 +67,7 @@ public class OptimizelyManager {
6667
@NonNull private final Logger logger;
6768
@Nullable private DataFileServiceConnection dataFileServiceConnection;
6869
@Nullable private OptimizelyStartListener optimizelyStartListener;
70+
@Nullable private UserExperimentRecord userExperimentRecord;
6971

7072
OptimizelyManager(@NonNull String projectId,
7173
@NonNull Long eventHandlerDispatchInterval,
@@ -265,6 +267,7 @@ protected void onPostExecute(UserExperimentRecord userExperimentRecord) {
265267

266268
try {
267269
OptimizelyManager.androidOptimizely = buildOptimizely(context, dataFile, userExperimentRecord);
270+
OptimizelyManager.this.userExperimentRecord = userExperimentRecord;
268271
logger.info("Sending Optimizely instance to listener");
269272

270273
if (optimizelyStartListener != null) {
@@ -295,6 +298,21 @@ private AndroidOptimizely buildOptimizely(@NonNull Context context, @NonNull Str
295298
return new AndroidOptimizely(optimizely, LoggerFactory.getLogger(AndroidOptimizely.class));
296299
}
297300

301+
@VisibleForTesting
302+
public UserExperimentRecord getUserExperimentRecord() {
303+
return userExperimentRecord;
304+
}
305+
306+
private boolean isAndroidVersionSupported() {
307+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
308+
return true;
309+
} else {
310+
logger.warn("Optimizely will not work on this phone. It's Android version {} is less the minimum supported" +
311+
"version {}", Build.VERSION.SDK_INT, Build.VERSION_CODES.ICE_CREAM_SANDWICH);
312+
return false;
313+
}
314+
}
315+
298316
@RequiresApi(api = Build.VERSION_CODES.ICE_CREAM_SANDWICH)
299317
static class OptlyActivityLifecycleCallbacks implements Application.ActivityLifecycleCallbacks {
300318

@@ -486,14 +504,4 @@ public OptimizelyManager build() {
486504

487505
}
488506
}
489-
490-
private boolean isAndroidVersionSupported() {
491-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
492-
return true;
493-
} else {
494-
logger.warn("Optimizely will not work on this phone. It's Android version {} is less the minimum supported" +
495-
"version {}", Build.VERSION.SDK_INT, Build.VERSION_CODES.ICE_CREAM_SANDWICH);
496-
return false;
497-
}
498-
}
499507
}

event-handler/src/main/java/com/optimizely/ab/android/event_handler/EventDispatcher.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import android.support.annotation.NonNull;
2323
import android.util.Pair;
2424

25+
import com.optimizely.ab.android.shared.CountingIdlingResourceManager;
2526
import com.optimizely.ab.android.shared.OptlyStorage;
2627
import com.optimizely.ab.android.shared.ServiceScheduler;
2728

@@ -136,6 +137,8 @@ private boolean dispatch(Event event) {
136137
boolean eventWasSent = eventClient.sendEvent(event);
137138

138139
if (eventWasSent) {
140+
CountingIdlingResourceManager.decrement();
141+
CountingIdlingResourceManager.recordEvent(new Pair<>(event.getURL().toString(), event.getRequestBody()));
139142
return true;
140143
} else {
141144
boolean eventWasStored = eventDAO.storeEvent(event);
@@ -148,6 +151,4 @@ private boolean dispatch(Event event) {
148151
}
149152
}
150153
}
151-
152-
153154
}

shared/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ dependencies {
5454
compile "com.noveogroup.android:android-logger:$android_logger_ver"
5555
compile "com.optimizely.ab:core-api:$java_core_ver"
5656
compile "com.google.code.gson:gson:$gson_ver"
57+
compile "com.android.support.test.espresso:espresso-idling-resource:$espresso_ver"
5758
provided "com.android.support:support-annotations:$support_annotations_ver"
5859

5960
testCompile "junit:junit:$junit_ver"
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2016, Optimizely
3+
* <p/>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p/>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p/>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.optimizely.ab.android.shared;
18+
19+
import android.support.annotation.NonNull;
20+
import android.support.annotation.Nullable;
21+
import android.support.annotation.VisibleForTesting;
22+
import android.support.test.espresso.idling.CountingIdlingResource;
23+
import android.util.Pair;
24+
25+
import java.util.LinkedList;
26+
import java.util.List;
27+
28+
/**
29+
* Manages an Espresso {@link CountingIdlingResource}
30+
*/
31+
@VisibleForTesting
32+
public class CountingIdlingResourceManager {
33+
34+
@Nullable private static CountingIdlingResource countingIdlingResource;
35+
@NonNull private static List<Pair<String, String>> eventList = new LinkedList<>();
36+
37+
@VisibleForTesting
38+
@Nullable
39+
public static CountingIdlingResource getIdlingResource() {
40+
if (countingIdlingResource == null) {
41+
countingIdlingResource = new CountingIdlingResource("optimizely", true);
42+
}
43+
return countingIdlingResource;
44+
}
45+
46+
@VisibleForTesting
47+
public static void setIdlingResource(@NonNull CountingIdlingResource countingIdlingResource) {
48+
CountingIdlingResourceManager.countingIdlingResource = countingIdlingResource;
49+
}
50+
51+
@VisibleForTesting
52+
public static void increment() {
53+
if (countingIdlingResource != null) {
54+
countingIdlingResource.increment();
55+
}
56+
}
57+
58+
@VisibleForTesting
59+
public static void decrement() {
60+
if (countingIdlingResource != null) {
61+
countingIdlingResource.decrement();
62+
}
63+
}
64+
65+
@VisibleForTesting
66+
public static void recordEvent(Pair<String, String> event) {
67+
if (countingIdlingResource != null) {
68+
eventList.add(event);
69+
}
70+
}
71+
72+
@VisibleForTesting
73+
public static void clearEvents() {
74+
eventList.clear();
75+
}
76+
77+
@VisibleForTesting
78+
public static List<Pair<String, String>> getEvents() {
79+
return eventList;
80+
}
81+
}

shared/src/main/java/com/optimizely/ab/android/shared/ServiceScheduler.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,16 @@ public void unschedule(Intent intent) {
8585
logger.info("Unscheduled {}", intent.getComponent().toShortString());
8686
}
8787

88+
/**
89+
* Is an {@link Intent} for a service scheduled
90+
* @param intent the intent in question
91+
* @return is it scheduled or not
92+
* @hide
93+
*/
94+
public boolean isScheduled(Intent intent) {
95+
return pendingIntentFactory.hasPendingIntent(intent);
96+
}
97+
8898
/**
8999
* Handles the complexities around PendingIntent flags
90100
*

test-app/build.gradle

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ dependencies {
3636
// compile 'com.optimizely.ab:android-sdk:0.1.2-SNAPSHOT'
3737
compile 'com.android.support:appcompat-v7:24.2.1'
3838
compile 'com.android.support:design:24.2.1'
39-
compile "com.android.support.test.espresso:espresso-idling-resource:$espresso_ver"
40-
4139

4240
testCompile "junit:junit:$junit_ver"
4341
testCompile "org.mockito:mockito-core:$mockito_ver"

0 commit comments

Comments
 (0)