Skip to content

Commit f309aeb

Browse files
Feature/remove espresso from release (#152)
* remove some dependencies * after much juggling, i think we have a pretty good solution. we show an example of overriding the logger and json parser in the test-app * fix so that espresso is included via interface. * final updates for removing espresso
1 parent 97c42ca commit f309aeb

File tree

8 files changed

+120
-23
lines changed

8 files changed

+120
-23
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ public static class Builder {
526526
* cached datafile. If you set this to -1, you disable background updates. If you don't set
527527
* a download interval (or set to less than 0), then no background updates will be scheduled or occur.
528528
*
529-
* @param interval the interval
529+
* @param interval the interval in seconds
530530
* @return this {@link Builder} instance
531531
*/
532532
public Builder withDatafileDownloadInterval(long interval) {
@@ -569,7 +569,7 @@ public Builder withErrorHandler(ErrorHandler errorHandler) {
569569
* If you set this to -1, you disable background updates. If you don't set
570570
* a event dispatch interval, then no background updates will be scheduled or occur.
571571
*
572-
* @param interval the interval
572+
* @param interval the interval in seconds
573573
* @return this {@link Builder} instance
574574
*/
575575
public Builder withEventDispatchInterval(long interval) {

proguard-rules.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
## https://developers.optimizely.com/x/solutions/sdks/reference/index.html?language=android&platform=mobile#installation
2121

2222
# Optimizely
23-
-keep class com.optimizely.ab.** { *; }
23+
#-keep class com.optimizely.ab.** { *; }
2424

2525
# Gson
2626
-keepnames class com.google.gson.Gson

shared/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ dependencies {
5555
compile "com.noveogroup.android:android-logger:$android_logger_ver"
5656
compile "com.optimizely.ab:core-api:$java_core_ver"
5757
compile "com.google.code.gson:gson:$gson_ver"
58-
compile "com.android.support.test.espresso:espresso-idling-resource:$espresso_ver"
5958
compile group: 'com.google.code.findbugs', name: 'jsr305', version: '2.0.1'
6059
provided "com.android.support:support-annotations:$support_annotations_ver"
6160

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/****************************************************************************
2+
* Copyright 2017, Optimizely, Inc. and contributors *
3+
* *
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+
* *
8+
* http://www.apache.org/licenses/LICENSE-2.0 *
9+
* *
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.content.Context;
20+
21+
import org.slf4j.Logger;
22+
import org.slf4j.LoggerFactory;
23+
24+
public class CachedCounter implements CountingIdlingResourceInterface {
25+
private final Cache cache;
26+
private final Logger logger = LoggerFactory.getLogger("CachedCounter");
27+
private final Context context;
28+
private final static String fileName = "OptlyCachedCounterKey";
29+
public CachedCounter(Context context) {
30+
this.context = context;
31+
this.cache = new Cache(context, logger);
32+
if (!cache.exists(fileName)) {
33+
cache.save(fileName, "0");
34+
}
35+
}
36+
37+
synchronized public void increment() {
38+
String value = cache.load(fileName);
39+
Integer val = Integer.valueOf(value);
40+
val += 1;
41+
cache.save(fileName, val.toString());
42+
}
43+
44+
synchronized public void decrement() {
45+
String value = cache.load(fileName);
46+
Integer val = Integer.valueOf(value);
47+
if (val == 0) {
48+
return;
49+
}
50+
val -= 1;
51+
cache.save(fileName, val.toString());
52+
}
53+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/****************************************************************************
2+
* Copyright 2017, Optimizely, Inc. and contributors *
3+
* *
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+
* *
8+
* http://www.apache.org/licenses/LICENSE-2.0 *
9+
* *
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+
/**
20+
* Interface for dealing with Espresso idling counter
21+
*/
22+
public interface CountingIdlingResourceInterface {
23+
public void increment();
24+
public void decrement();
25+
}

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

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,65 +16,55 @@
1616

1717
package com.optimizely.ab.android.shared;
1818

19+
import android.content.Context;
1920
import android.support.annotation.NonNull;
2021
import android.support.annotation.Nullable;
21-
import android.support.annotation.VisibleForTesting;
22-
import android.support.test.espresso.idling.CountingIdlingResource;
2322
import android.util.Pair;
2423

2524
import java.util.LinkedList;
2625
import java.util.List;
2726

2827
/**
29-
* Manages an Espresso {@link CountingIdlingResource}
28+
* Manages an Espresso like CountingIdlingResource
3029
*/
31-
@VisibleForTesting
3230
public class CountingIdlingResourceManager {
3331

34-
@Nullable private static CountingIdlingResource countingIdlingResource;
32+
@Nullable private static CountingIdlingResourceInterface countingIdlingResource;
3533
@NonNull private static List<Pair<String, String>> eventList = new LinkedList<>();
3634

37-
@VisibleForTesting
38-
@Nullable
39-
public static CountingIdlingResource getIdlingResource() {
35+
@Nullable public static CountingIdlingResourceInterface getIdlingResource(Context context) {
4036
if (countingIdlingResource == null) {
41-
countingIdlingResource = new CountingIdlingResource("optimizely", true);
37+
countingIdlingResource = new CachedCounter(context);
4238
}
4339
return countingIdlingResource;
4440
}
4541

46-
@VisibleForTesting
47-
public static void setIdlingResource(@NonNull CountingIdlingResource countingIdlingResource) {
42+
public static void setIdlingResource(@NonNull CountingIdlingResourceInterface countingIdlingResource) {
4843
CountingIdlingResourceManager.countingIdlingResource = countingIdlingResource;
4944
}
5045

51-
@VisibleForTesting
5246
public static void increment() {
5347
if (countingIdlingResource != null) {
5448
countingIdlingResource.increment();
5549
}
5650
}
5751

58-
@VisibleForTesting
5952
public static void decrement() {
6053
if (countingIdlingResource != null) {
6154
countingIdlingResource.decrement();
6255
}
6356
}
6457

65-
@VisibleForTesting
6658
public static void recordEvent(Pair<String, String> event) {
6759
if (countingIdlingResource != null) {
6860
eventList.add(event);
6961
}
7062
}
7163

72-
@VisibleForTesting
7364
public static void clearEvents() {
7465
eventList.clear();
7566
}
76-
77-
@VisibleForTesting
67+
7868
public static List<Pair<String, String>> getEvents() {
7969
return eventList;
8070
}

test-app/build.gradle

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,29 @@ android {
2424
sourceCompatibility JavaVersion.VERSION_1_7
2525
targetCompatibility JavaVersion.VERSION_1_7
2626
}
27+
28+
packagingOptions {
29+
exclude 'META-INF/LICENSE.txt'
30+
exclude 'META-INF/NOTICE.txt'
31+
exclude 'META-INF/LICENSE'
32+
exclude 'META-INF/NOTICE'
33+
}
2734
}
2835

2936
dependencies {
3037
// Includes the Optimizely X Full Stack Java SDK, event handler, and user profile
31-
compile project(':android-sdk')
38+
compile (project(':android-sdk')) {
39+
exclude group: 'com.google.code.gson', module:'gson'
40+
exclude group: 'com.noveogroup.android', module:'android-logger'
41+
}
3242
// compile 'com.optimizely.ab:android-sdk:1.0.0'
3343
compile 'com.android.support:appcompat-v7:24.2.1'
3444
compile 'com.android.support:design:24.2.1'
45+
// EXAMPLE REPLACE noveogroup android-looger with slf4j-android logger
46+
// https://mvnrepository.com/artifact/org.slf4j/slf4j-android
47+
compile group: 'org.slf4j', name: 'slf4j-android', version: '1.7.7'
48+
// EXAMPLE REPLACE gson json parsing with jackson-databind json parsing.
49+
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.7.1'
3550

3651
testCompile "junit:junit:$junit_ver"
3752
testCompile "org.mockito:mockito-core:$mockito_ver"
@@ -50,4 +65,5 @@ dependencies {
5065
androidTestCompile "com.google.dexmaker:dexmaker-mockito:$dexmaker_ver"
5166
// androidTestCompile 'com.optimizely.ab:android-sdk:1.0.0'
5267
androidTestCompile project(':android-sdk')
68+
androidTestCompile project(path: ':shared')
5369
}

test-app/src/androidTest/java/com/optimizely/ab/android/test_app/MainActivityEspressoTest.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import com.optimizely.ab.android.datafile_handler.DatafileService;
3232
import com.optimizely.ab.android.event_handler.EventIntentService;
33+
import com.optimizely.ab.android.shared.CountingIdlingResourceInterface;
3334
import com.optimizely.ab.android.shared.CountingIdlingResourceManager;
3435
import com.optimizely.ab.android.shared.ServiceScheduler;
3536
import com.optimizely.ab.bucketing.UserProfileService;
@@ -51,6 +52,7 @@
5152
import static android.support.test.espresso.action.ViewActions.click;
5253
import static android.support.test.espresso.assertion.ViewAssertions.matches;
5354
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
55+
import static android.support.test.espresso.matcher.ViewMatchers.supportsInputMethods;
5456
import static android.support.test.espresso.matcher.ViewMatchers.withId;
5557
import static junit.framework.Assert.assertFalse;
5658
import static junit.framework.Assert.assertNull;
@@ -71,7 +73,19 @@ public class MainActivityEspressoTest {
7173
@Override
7274
protected void before() throws Throwable {
7375
super.before();
74-
countingIdlingResource = CountingIdlingResourceManager.getIdlingResource();
76+
countingIdlingResource = new CountingIdlingResource("optly", true);
77+
CountingIdlingResourceInterface wrapper = new CountingIdlingResourceInterface() {
78+
@Override
79+
public void increment() {
80+
countingIdlingResource.increment();
81+
}
82+
83+
@Override
84+
public void decrement() {
85+
countingIdlingResource.decrement();
86+
}
87+
};
88+
CountingIdlingResourceManager.setIdlingResource(wrapper);
7589
// To prove that the test fails, omit this call:
7690
Espresso.registerIdlingResources(countingIdlingResource);
7791
}

0 commit comments

Comments
 (0)