Skip to content

[FLoC] Change FLoC instrumental tests to be unit tests. #1058

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 67 commits into from
Dec 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
8881325
Implement Firebase segmentation SDK device local cache
diwu-arete Jun 13, 2019
864748f
[Firebase Segmentation] Add custom installation id cache layer and te…
diwu-arete Jun 14, 2019
0a3ebf6
Add test for updating cache
diwu-arete Jun 14, 2019
2d158ed
Switch to use SQLiteOpenHelper
diwu-arete Jun 15, 2019
f118d39
Switch to use SharedPreferences from SQLite.
diwu-arete Jun 17, 2019
4da5d31
Change the cache class to be singleton
diwu-arete Jun 18, 2019
d1ff0ec
Wrap shared pref commit in a async task.
diwu-arete Jun 18, 2019
2c5102c
Merge branch 'master' of github.com:firebase/firebase-android-sdk int…
diwu-arete Jun 18, 2019
41fbfee
Address comments
diwu-arete Jun 18, 2019
5fd2fa0
Google format fix
diwu-arete Jun 18, 2019
e950003
Merge branch 'master' of github.com:firebase/firebase-android-sdk int…
diwu-arete Jun 18, 2019
dba0c0e
Replace some deprecated code.
diwu-arete Jun 18, 2019
dc37bf8
Merge branch 'floc-master' of github.com:firebase/firebase-android-sd…
diwu-arete Jun 18, 2019
ebdd626
Merge branch 'master' of github.com:firebase/firebase-android-sdk int…
diwu-arete Jun 18, 2019
f26741e
Merge branch 'floc-master' of github.com:firebase/firebase-android-sd…
diwu-arete Jun 18, 2019
38c403f
Merge branch 'floc-master' of github.com:firebase/firebase-android-sd…
diwu-arete Jun 18, 2019
a9a43a4
Package refactor
diwu-arete Jun 18, 2019
ca6dacf
nit
diwu-arete Jun 18, 2019
e7fff81
nit
diwu-arete Jun 18, 2019
bb8bf45
Merge branch 'floc-master' of github.com:firebase/firebase-android-sd…
diwu-arete Jun 18, 2019
b381889
Add the state machine of updating custom installation id in the local
diwu-arete Jun 19, 2019
a72cf60
Merge branch 'floc-master' of github.com:firebase/firebase-android-sd…
diwu-arete Jun 19, 2019
1adcfbd
minor format fix
diwu-arete Jun 20, 2019
6091f82
Address comments #1
diwu-arete Jun 20, 2019
ada5577
Merge branch 'master' of github.com:firebase/firebase-android-sdk int…
diwu-arete Jun 20, 2019
af5bcd1
Merge branch 'floc-master' of github.com:firebase/firebase-android-sd…
diwu-arete Jun 20, 2019
de6fb6d
Merge branch 'master' of github.com:firebase/firebase-android-sdk int…
diwu-arete Jun 24, 2019
75e523b
Merge branch 'floc-master' of github.com:firebase/firebase-android-sd…
diwu-arete Jun 24, 2019
134f06e
Merge branch 'floc-master' of github.com:firebase/firebase-android-sd…
diwu-arete Jun 24, 2019
6a0f502
Http client in Firebase Segmentation SDK to call backend service.
diwu-arete Jun 26, 2019
4ac7db5
Merge branch 'floc-master' of github.com:firebase/firebase-android-sd…
diwu-arete Jun 26, 2019
7f40978
Revert unintentional change
diwu-arete Jun 26, 2019
b2fc302
Fix connected device test
diwu-arete Jun 26, 2019
1f2ab34
Fix connected device test
diwu-arete Jun 26, 2019
402d8b6
Merge branch 'arete-floc' of github.com:firebase/firebase-android-sdk…
diwu-arete Jun 26, 2019
ce698cf
Merge branch 'master' of github.com:firebase/firebase-android-sdk int…
diwu-arete Jul 8, 2019
0e0278e
Merge branch 'floc-master' of github.com:firebase/firebase-android-sd…
diwu-arete Jul 8, 2019
9880624
1. Add a few annotations to make java code Kotlin friendly
diwu-arete Jul 8, 2019
8ffb5bb
Fix java format
diwu-arete Jul 8, 2019
daf4698
Fix API version
diwu-arete Jul 8, 2019
7985e14
Change the segmentation API implementation to synchronous and put the
diwu-arete Jul 9, 2019
9f36d35
Fix a async getResult race issue.
diwu-arete Jul 9, 2019
047c0af
OkHttpClient -> HttpsUrlConnection
diwu-arete Jul 10, 2019
8b39c31
Use gzip for compressing content and fix ourput stream memory leak risk.
diwu-arete Jul 18, 2019
dc1a63c
Addressed a few comments
diwu-arete Jul 22, 2019
b401d31
Merge branch 'master' of github.com:firebase/firebase-android-sdk int…
diwu-arete Jul 22, 2019
8ba1c9b
Merge branch 'floc-master' of github.com:firebase/firebase-android-sd…
diwu-arete Jul 22, 2019
143ed74
FirebaseSegmentation SDK
diwu-arete Jul 30, 2019
e5f0cd3
Merge branch 'master' of github.com:firebase/firebase-android-sdk int…
diwu-arete Jul 30, 2019
a5250c1
Merge branch 'floc-master' of github.com:firebase/firebase-android-sd…
diwu-arete Jul 30, 2019
6daae2a
Merge branch 'master' of github.com:firebase/firebase-android-sdk int…
diwu-arete Aug 5, 2019
4d5b2de
Merge branch 'floc-master' of github.com:firebase/firebase-android-sd…
diwu-arete Aug 6, 2019
78ce7cd
Merge branch 'floc-master' of github.com:firebase/firebase-android-sd…
diwu-arete Aug 6, 2019
d467379
Restrict Firebase API key to Android app package name.
diwu-arete Aug 6, 2019
818e471
Explicitly add internet permission
diwu-arete Aug 6, 2019
e2fbc88
Merge branch 'floc-master' of github.com:firebase/firebase-android-sd…
diwu-arete Aug 6, 2019
a7ef2db
Merge branch 'floc-master' of github.com:firebase/firebase-android-sd…
diwu-arete Aug 6, 2019
f866315
Merge branch 'master' of github.com:firebase/firebase-android-sdk int…
diwu-arete Dec 10, 2019
a4fcd34
Disable registrar test for FirebaseSegmentation.
diwu-arete Dec 10, 2019
557ea7a
Merge branch 'floc-master' of github.com:firebase/firebase-android-sd…
diwu-arete Dec 10, 2019
0149308
Merge branch 'floc-master' of github.com:firebase/firebase-android-sd…
diwu-arete Dec 10, 2019
f757b26
Disable test lab
diwu-arete Dec 10, 2019
34caf2c
Merge branch 'floc-master' of github.com:firebase/firebase-android-sd…
diwu-arete Dec 10, 2019
461f654
Add api info for segmentation API
diwu-arete Dec 12, 2019
2a0a7a4
[FLoC] Replace the custom installation id cache SharedPref
diwu-arete Dec 13, 2019
7b36ad5
[FLoC] Change FLoC instrumental tests to be unit tests.
diwu-arete Dec 14, 2019
b5bd948
Merge branch 'floc-master' of github.com:firebase/firebase-android-sd…
diwu-arete Dec 14, 2019
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
12 changes: 3 additions & 9 deletions firebase-segmentation/firebase-segmentation.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,7 @@ dependencies {
testImplementation 'androidx.test:core:1.2.0'
testImplementation 'junit:junit:4.12'
testImplementation "org.robolectric:robolectric:$robolectricVersion"

androidTestImplementation "androidx.annotation:annotation:1.1.0"
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test:rules:1.2.0'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation "com.google.truth:truth:$googleTruthVersion"
androidTestImplementation 'junit:junit:4.12'
androidTestImplementation 'org.mockito:mockito-core:2.25.0'
androidTestImplementation 'org.mockito:mockito-android:2.25.0'
testImplementation "com.google.truth:truth:$googleTruthVersion"
testImplementation 'org.mockito:mockito-core:2.25.0'
testImplementation 'org.mockito:mockito-inline:2.25.0'
}
26 changes: 0 additions & 26 deletions firebase-segmentation/src/androidTest/AndroidManifest.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.when;

import androidx.annotation.NonNull;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.gms.tasks.Tasks;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
Expand All @@ -34,6 +33,10 @@
import com.google.firebase.segmentation.local.CustomInstallationIdCacheEntryValue;
import com.google.firebase.segmentation.remote.SegmentationServiceClient;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.FixMethodOrder;
Expand All @@ -42,26 +45,24 @@
import org.junit.runners.MethodSorters;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;

/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
@RunWith(RobolectricTestRunner.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class FirebaseSegmentationInstrumentedTest {

public class FirebaseSegmentationTest {
private static final String CUSTOM_INSTALLATION_ID = "123";
private static final String FIREBASE_INSTANCE_ID = "cAAAAAAAAAA";

private FirebaseApp firebaseApp;
@Mock private FirebaseInstanceId firebaseInstanceId;
@Mock private SegmentationServiceClient backendClientReturnsOk;
@Mock private SegmentationServiceClient backendClientReturnsError;

private CustomInstallationIdCache actualCache;
@Mock private CustomInstallationIdCache cacheReturnsError;

private ExecutorService taskExecutor;

@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
Expand Down Expand Up @@ -105,6 +106,8 @@ public String getToken() {
}));
when(cacheReturnsError.insertOrUpdateCacheEntry(any())).thenReturn(false);
when(cacheReturnsError.readCacheEntryValue()).thenReturn(null);

taskExecutor = new ThreadPoolExecutor(0, 1, 30L, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
}

@After
Expand All @@ -119,7 +122,11 @@ public void testUpdateCustomInstallationId_CacheOk_BackendOk() throws Exception
firebaseApp, firebaseInstanceId, actualCache, backendClientReturnsOk);

// No exception, means success.
assertNull(Tasks.await(firebaseSegmentation.setCustomInstallationId(CUSTOM_INSTALLATION_ID)));
TestOnCompleteListener<Void> onCompleteListener = new TestOnCompleteListener<>();
firebaseSegmentation
.setCustomInstallationId(CUSTOM_INSTALLATION_ID)
.addOnCompleteListener(taskExecutor, onCompleteListener);
assertNull(onCompleteListener.await());
CustomInstallationIdCacheEntryValue entryValue = actualCache.readCacheEntryValue();
assertThat(entryValue.getCustomInstallationId()).isEqualTo(CUSTOM_INSTALLATION_ID);
assertThat(entryValue.getFirebaseInstanceId()).isEqualTo(FIREBASE_INSTANCE_ID);
Expand All @@ -135,7 +142,11 @@ public void testUpdateCustomInstallationId_CacheOk_BackendError_Retryable()

// Expect exception
try {
Tasks.await(firebaseSegmentation.setCustomInstallationId(CUSTOM_INSTALLATION_ID));
TestOnCompleteListener<Void> onCompleteListener = new TestOnCompleteListener<>();
firebaseSegmentation
.setCustomInstallationId(CUSTOM_INSTALLATION_ID)
.addOnCompleteListener(taskExecutor, onCompleteListener);
onCompleteListener.await();
fail();
} catch (ExecutionException expected) {
Throwable cause = expected.getCause();
Expand Down Expand Up @@ -163,7 +174,11 @@ public void testUpdateCustomInstallationId_CacheOk_BackendError_NotRetryable()

// Expect exception
try {
Tasks.await(firebaseSegmentation.setCustomInstallationId(CUSTOM_INSTALLATION_ID));
TestOnCompleteListener<Void> onCompleteListener = new TestOnCompleteListener<>();
firebaseSegmentation
.setCustomInstallationId(CUSTOM_INSTALLATION_ID)
.addOnCompleteListener(taskExecutor, onCompleteListener);
onCompleteListener.await();
fail();
} catch (ExecutionException expected) {
Throwable cause = expected.getCause();
Expand All @@ -184,7 +199,11 @@ public void testUpdateCustomInstallationId_CacheError_BackendOk() throws Interru

// Expect exception
try {
Tasks.await(firebaseSegmentation.setCustomInstallationId(CUSTOM_INSTALLATION_ID));
TestOnCompleteListener<Void> onCompleteListener = new TestOnCompleteListener<>();
firebaseSegmentation
.setCustomInstallationId(CUSTOM_INSTALLATION_ID)
.addOnCompleteListener(taskExecutor, onCompleteListener);
onCompleteListener.await();
Comment on lines +202 to +206
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: We can move this into an initialization method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, first, I don't think we can put this entire block into initialization() since it's not always the same in every test case.

If you meant that move TestOnCompleteListener onCompleteListener = new TestOnCompleteListener<>() into initialization, I also think putting here is better since it's a whole logic block with the two lines below. Also, if a test case uses onCompleteListener more than once, then putting it in initialization is also not perfect.

fail();
} catch (ExecutionException expected) {
Throwable cause = expected.getCause();
Expand All @@ -206,7 +225,11 @@ public void testClearCustomInstallationId_CacheOk_BackendOk() throws Exception {
firebaseApp, firebaseInstanceId, actualCache, backendClientReturnsOk);

// No exception, means success.
assertNull(Tasks.await(firebaseSegmentation.setCustomInstallationId(null)));
TestOnCompleteListener<Void> onCompleteListener = new TestOnCompleteListener<>();
firebaseSegmentation
.setCustomInstallationId(null)
.addOnCompleteListener(taskExecutor, onCompleteListener);
assertNull(onCompleteListener.await());
CustomInstallationIdCacheEntryValue entryValue = actualCache.readCacheEntryValue();
assertNull(entryValue);
}
Expand All @@ -224,7 +247,11 @@ public void testClearCustomInstallationId_CacheOk_BackendError() throws Exceptio

// Expect exception
try {
Tasks.await(firebaseSegmentation.setCustomInstallationId(null));
TestOnCompleteListener<Void> onCompleteListener = new TestOnCompleteListener<>();
firebaseSegmentation
.setCustomInstallationId(null)
.addOnCompleteListener(taskExecutor, onCompleteListener);
onCompleteListener.await();
fail();
} catch (ExecutionException expected) {
Throwable cause = expected.getCause();
Expand All @@ -248,7 +275,11 @@ public void testClearCustomInstallationId_CacheError_BackendOk() throws Interrup

// Expect exception
try {
Tasks.await(firebaseSegmentation.setCustomInstallationId(CUSTOM_INSTALLATION_ID));
TestOnCompleteListener<Void> onCompleteListener = new TestOnCompleteListener<>();
firebaseSegmentation
.setCustomInstallationId(null)
.addOnCompleteListener(taskExecutor, onCompleteListener);
onCompleteListener.await();
fail();
} catch (ExecutionException expected) {
Throwable cause = expected.getCause();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.firebase.segmentation;

import androidx.annotation.NonNull;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
* Helper listener that works around a limitation of the Tasks API where await() cannot be called on
* the main thread. This listener works around it by running itself on a different thread, thus
* allowing the main thread to be woken up when the Tasks complete.
*/
public class TestOnCompleteListener<TResult> implements OnCompleteListener<TResult> {
private static final long TIMEOUT_MS = 5000;
private final CountDownLatch latch = new CountDownLatch(1);
private Task<TResult> task;
private volatile TResult result;
private volatile Exception exception;
private volatile boolean successful;

@Override
public void onComplete(@NonNull Task<TResult> task) {
this.task = task;
successful = task.isSuccessful();
if (successful) {
result = task.getResult();
} else {
exception = task.getException();
}
latch.countDown();
}

/** Blocks until the {@link #onComplete} is called. */
public TResult await() throws InterruptedException, ExecutionException {
if (!latch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
throw new InterruptedException("timed out waiting for result");
}
if (successful) {
return result;
} else {
if (exception instanceof InterruptedException) {
throw (InterruptedException) exception;
}
if (exception instanceof SetCustomInstallationIdException) {
throw new ExecutionException(exception);
}
if (exception instanceof IOException) {
throw new ExecutionException(exception);
}
throw new IllegalStateException("got an unexpected exception type", exception);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
import static org.junit.Assert.assertTrue;

import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;

/** Instrumented tests for {@link CustomInstallationIdCache} */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Update the javadocs.

@RunWith(AndroidJUnit4.class)
@RunWith(RobolectricTestRunner.class)
public class CustomInstallationIdCacheTest {

private FirebaseApp firebaseApp0;
Expand Down