Skip to content

Commit

Permalink
fix: support datafile download and events upload for API-16 (#373)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaeopt authored Jun 2, 2021
1 parent d2ab6fc commit 25f472b
Show file tree
Hide file tree
Showing 29 changed files with 168 additions and 298 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ env:
- ADB_INSTALL_TIMEOUT=5 # minutes
- ANDROID_API=29 # api is same as gradle file
matrix:
- EMULATOR_API=19
- EMULATOR_API=21
- EMULATOR_API=22
android:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/****************************************************************************
* Copyright 2017, Optimizely, Inc. and contributors *
* Copyright 2017,2021, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
Expand Down Expand Up @@ -34,7 +34,6 @@

@RunWith(AndroidJUnit4.class)
public class OptimizelyClientEngineTest {
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB_MR2)
@Test
public void testGetClientEngineFromContextAndroidTV() {
Context context = mock(Context.class);
Expand All @@ -44,7 +43,6 @@ public void testGetClientEngineFromContextAndroidTV() {
assertEquals(EventBatch.ClientEngine.ANDROID_TV_SDK, OptimizelyClientEngine.getClientEngineFromContext(context));
}

@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB_MR2)
@Test
public void testGetClientEngineFromContextAndroid() {
Context context = mock(Context.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import android.content.pm.PackageManager;
import android.os.Build;

import androidx.annotation.RequiresApi;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.SdkSuppress;
import androidx.test.platform.app.InstrumentationRegistry;

import com.optimizely.ab.android.datafile_handler.DatafileHandler;
Expand Down Expand Up @@ -356,7 +356,6 @@ public void stop() {
assertNull(optimizelyManager.getOptimizelyStartListener());
}

@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
@Test
public void injectOptimizely() {
Context context = mock(Context.class);
Expand All @@ -377,7 +376,6 @@ public void injectOptimizely() {

}

@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
@Test
public void injectOptimizelyWithDatafileLisener() {
Context context = mock(Context.class);
Expand All @@ -398,7 +396,7 @@ public void injectOptimizelyWithDatafileLisener() {
}

@Test
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.LOLLIPOP)
public void injectOptimizelyNullListener() {
Context context = mock(Context.class);
PackageManager packageManager = mock(PackageManager.class);
Expand Down Expand Up @@ -442,7 +440,6 @@ public void injectOptimizelyNullListener() {
assertEquals(optimizelyManager.getDatafileConfig().toJSONString(), intent2.getStringExtra(DatafileService.EXTRA_DATAFILE_CONFIG));
}

@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
@Test
public void injectOptimizelyHandlesInvalidDatafile() {
Context context = mock(Context.class);
Expand Down Expand Up @@ -472,7 +469,6 @@ public void injectOptimizelyHandlesInvalidDatafile() {
assertFalse(optimizelyManager.getOptimizely().isValid());
}

@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
@Test
public void injectOptimizelyDoesNotDuplicateCallback() {
Context context = mock(Context.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,6 @@ private String safeLoadResource(Context context, @RawRes final Integer datafileR

DatafileLoadedListener getDatafileLoadedListener(final Context context, @RawRes final Integer datafileRes) {
return new DatafileLoadedListener() {
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
@Override
public void onDatafileLoaded(@Nullable String datafile) {
if (datafile != null && !datafile.isEmpty()) {
Expand Down Expand Up @@ -516,7 +515,6 @@ private void startDatafileHandler(Context context) {
});
}

@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
void injectOptimizely(@NonNull final Context context, final @NonNull UserProfileService userProfileService,
@NonNull final String datafile) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/****************************************************************************
* Copyright 2016, Optimizely, Inc. and contributors *
* Copyright 2016,2021, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
Expand All @@ -18,7 +18,6 @@

import android.content.Context;
import android.os.Build;
import androidx.annotation.RequiresApi;

import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -55,7 +54,6 @@ public void setup() {
datafileServiceConnection = new DatafileServiceConnection(optimizelyManager.getDatafileConfig(), context, optimizelyManager.getDatafileLoadedListener(context,null));
}

@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
@Test
public void onServiceConnected() {
DatafileService.LocalBinder binder = mock(DatafileService.LocalBinder.class);
Expand All @@ -72,7 +70,6 @@ public void onServiceConnected() {
verify(service).getDatafile(eq(sameString), any(DatafileLoader.class), any(DatafileLoadedListener.class));
}

@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
@Test
public void onServiceConnectedNullServiceFromBinder() {
DatafileService.LocalBinder binder = mock(DatafileService.LocalBinder.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/****************************************************************************
* Copyright 2016, Optimizely, Inc. and contributors *
* Copyright 2016,2021, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
Expand All @@ -18,7 +18,6 @@

import android.app.Activity;
import android.os.Build;
import androidx.annotation.RequiresApi;

import org.junit.Before;
import org.junit.Test;
Expand All @@ -38,13 +37,11 @@ public class OptimizelyManagerOptlyActivityLifecycleCallbacksTest {
@Mock Activity activity;
private OptimizelyManager.OptlyActivityLifecycleCallbacks optlyActivityLifecycleCallbacks;

@RequiresApi(api = Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Before
public void setup() {
optlyActivityLifecycleCallbacks = new OptimizelyManager.OptlyActivityLifecycleCallbacks(optimizelyManager);
}

@RequiresApi(api = Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Test
public void onActivityStopped() {
optlyActivityLifecycleCallbacks.onActivityStopped(activity);
Expand Down
19 changes: 8 additions & 11 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/****************************************************************************
* Copyright 2016-2020, Optimizely, Inc. and contributors *
* Copyright 2016-2021, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
Expand Down Expand Up @@ -75,26 +75,23 @@ task clean(type: Delete) {

task cleanAllModules () {
logger.info("Running clean for all modules")
dependsOn(':android-sdk:clean', ':event-handler:clean',
':user-profile:clean', ':shared:clean', ':datafile-handler:clean')
}

cleanAllModules.dependsOn(':android-sdk:clean', ':event-handler:clean',
':user-profile:clean', ':shared:clean', ':datafile-handler:clean')

task testAllModules () {
logger.info("Running android tests for all modules")
dependsOn('testAllModulesTravis', ':test-app:connectedAndroidTest')
}

task testAllModulesTravis () {
logger.info("Running android tests for Travis")
dependsOn(':android-sdk:connectedAndroidTest', ':android-sdk:test',
':event-handler:connectedAndroidTest', ':event-handler:test',
':datafile-handler:connectedAndroidTest', ':datafile-handler:test',
':user-profile:connectedAndroidTest', ':shared:connectedAndroidTest')
}

testAllModulesTravis.dependsOn(':android-sdk:connectedAndroidTest', ':android-sdk:test',
':event-handler:connectedAndroidTest', ':event-handler:test',
':datafile-handler:connectedAndroidTest', ':datafile-handler:test',
':user-profile:connectedAndroidTest', ':shared:connectedAndroidTest')

testAllModules.dependsOn('testAllModulesTravis', ':test-app:connectedAndroidTest')

// Publish to MavenCentral

task ship() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/****************************************************************************
* Copyright 2016, Optimizely, Inc. and contributors *
* Copyright 2016,2021, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
Expand All @@ -21,6 +21,7 @@

import androidx.annotation.RequiresApi;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.SdkSuppress;
import androidx.test.platform.app.InstrumentationRegistry;

import com.optimizely.ab.android.shared.Cache;
Expand Down Expand Up @@ -57,7 +58,6 @@
/**
* Tests for {@link DatafileLoader}
*/
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
@RunWith(AndroidJUnit4.class)
public class DatafileLoaderTest {

Expand Down Expand Up @@ -147,6 +147,8 @@ public void noCacheAndLoadFromCDNFails() {
}

@Test
// flacky with lower API
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.LOLLIPOP)
public void warningsAreLogged() throws IOException {
final ExecutorService executor = Executors.newSingleThreadExecutor();
Cache cache = mock(Cache.class);
Expand All @@ -172,6 +174,8 @@ public void warningsAreLogged() throws IOException {
}

@Test
// flacky with lower API
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.LOLLIPOP)
public void debugLogged() throws IOException {
final ExecutorService executor = Executors.newSingleThreadExecutor();
Cache cache = mock(Cache.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/****************************************************************************
* Copyright 2016, Optimizely, Inc. and contributors *
* Copyright 2016,2021, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
Expand Down Expand Up @@ -81,7 +81,6 @@ public void receivedActionBootCompleted() {
verify(logger).info("Received intent with action {}", Intent.ACTION_BOOT_COMPLETED);
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
@Test
public void receivedActionMyPackageReplaced() {
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/****************************************************************************
* Copyright 2016, Optimizely, Inc. and contributors *
* Copyright 2016,2021, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
Expand Down Expand Up @@ -71,7 +71,6 @@ public void setup() {
executor = Executors.newSingleThreadExecutor();
}

@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
@Test
@Ignore
public void testBinding() throws TimeoutException {
Expand Down Expand Up @@ -99,7 +98,6 @@ public void testBinding() throws TimeoutException {
assertTrue(datafileService.isBound());
}

@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
@Test
public void testValidStart() throws TimeoutException {
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
Expand All @@ -119,7 +117,6 @@ public void testValidStart() throws TimeoutException {
assertEquals(val, START_FLAG_REDELIVERY);
}

@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
@Test
@Ignore
public void testNullIntentStart() throws TimeoutException {
Expand All @@ -139,7 +136,6 @@ public void testNullIntentStart() throws TimeoutException {
verify(logger).warn("Data file service received a null intent");
}

@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
@Test
@Ignore
public void testNoProjectIdIntentStart() throws TimeoutException {
Expand All @@ -159,7 +155,6 @@ public void testNoProjectIdIntentStart() throws TimeoutException {
verify(logger).warn("Data file service received an intent with no project id extra");
}

@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
@Test
@Ignore
public void testUnbind() throws TimeoutException {
Expand All @@ -180,7 +175,6 @@ public void testUnbind() throws TimeoutException {
verify(logger).info("All clients are unbound from data file service");
}

@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
@Test
@Ignore
public void testIntentExtraData(){
Expand Down Expand Up @@ -209,7 +203,6 @@ public void testIntentExtraData(){
assertTrue(intent2.getComponent().getShortClassName().contains("DatafileService"));
}

@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
@Test
public void testGetDatafileUrl(){
// HARD-CODING link here to make sure we don't unintentionally mess up the datafile version
Expand All @@ -218,8 +211,7 @@ public void testGetDatafileUrl(){
String datafileUrl = new DatafileConfig("1", null).getUrl();
assertEquals("https://cdn.optimizely.com/json/1.json", datafileUrl);
}
@
RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)

@Test
public void testGetDatafileEnvironmentUrl(){
// HARD-CODING link here to make sure we don't unintentionally mess up the datafile version
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/****************************************************************************
* Copyright 2016-2017, Optimizely, Inc. and contributors *
* Copyright 2016-2017,2021, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
Expand All @@ -16,6 +16,8 @@

package com.optimizely.ab.android.datafile_handler;

import android.os.Build;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ private void saveDownloadTime(String url) {
storage.saveLong(url + datafileDownloadTime, time);
}

@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
public void getDatafile(@NonNull String datafileUrl, @Nullable DatafileLoadedListener datafileLoadedListener) {
if (!allowDownload(datafileUrl, datafileLoadedListener)) {
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/****************************************************************************
* Copyright 2016-2017, Optimizely, Inc. and contributors *
* Copyright 2016-2017,2021, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
Expand Down Expand Up @@ -54,7 +54,6 @@ public class DatafileService extends Service {
* @hide
* @see Service#onStartCommand(Intent, int, int)
*/
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null) {
Expand Down Expand Up @@ -111,7 +110,6 @@ public void stop() {
stopSelf();
}

@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
public void getDatafile(String datafileUrl, DatafileLoader datafileLoader, DatafileLoadedListener loadedListener) {
datafileLoader.getDatafile(datafileUrl, loadedListener);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/****************************************************************************
* Copyright 2017, Optimizely, Inc. and contributors *
* Copyright 2017,2021, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
Expand Down Expand Up @@ -62,7 +62,6 @@ public DatafileServiceConnection(@NonNull DatafileConfig datafileConfig, @NonNul
*
* @see ServiceConnection#onServiceConnected(ComponentName, IBinder)
*/
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
@Override
public void onServiceConnected(ComponentName className,
IBinder service) {
Expand Down
Loading

0 comments on commit 25f472b

Please sign in to comment.