Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 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
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
7 changes: 7 additions & 0 deletions firebase-segmentation/firebase-segmentation.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,18 @@ android {

dependencies {
implementation project(':firebase-common')
implementation project(':protolite-well-known-types')

implementation('com.google.firebase:firebase-iid:17.0.3') {
exclude group: "com.google.firebase", module: "firebase-common"
}
implementation 'io.grpc:grpc-stub:1.21.0'
implementation 'io.grpc:grpc-protobuf-lite:1.21.0'
implementation 'io.grpc:grpc-okhttp:1.21.0'
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.multidex:multidex:2.0.0'
implementation 'com.google.android.gms:play-services-tasks:16.0.1'
implementation 'com.squareup.okhttp:okhttp:2.7.5'

compileOnly "com.google.auto.value:auto-value-annotations:1.6.5"
annotationProcessor "com.google.auto.value:auto-value:1.6.2"
Expand All @@ -104,4 +109,6 @@ dependencies {
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'
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,218 @@

package com.google.firebase.segmentation;

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.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;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.InstanceIdResult;
import com.google.firebase.segmentation.local.CustomInstallationIdCache;
import com.google.firebase.segmentation.local.CustomInstallationIdCacheEntryValue;
import com.google.firebase.segmentation.remote.SegmentationServiceClient;
import java.util.concurrent.ExecutionException;
import org.junit.After;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

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

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;

@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
FirebaseApp.clearInstancesForTest();
firebaseApp =
FirebaseApp.initializeApp(
ApplicationProvider.getApplicationContext(),
new FirebaseOptions.Builder().setApplicationId("1:123456789:android:abcdef").build());
actualCache = new CustomInstallationIdCache(firebaseApp);

when(backendClientReturnsOk.updateCustomInstallationId(
anyLong(), anyString(), anyString(), anyString()))
.thenReturn(Tasks.forResult(SegmentationServiceClient.Code.OK));
when(backendClientReturnsOk.clearCustomInstallationId(anyLong(), anyString(), anyString()))
.thenReturn(Tasks.forResult(SegmentationServiceClient.Code.OK));
when(backendClientReturnsError.updateCustomInstallationId(
anyLong(), anyString(), anyString(), anyString()))
.thenReturn(Tasks.forResult(SegmentationServiceClient.Code.SERVER_INTERNAL_ERROR));
when(backendClientReturnsError.clearCustomInstallationId(anyLong(), anyString(), anyString()))
.thenReturn(Tasks.forResult(SegmentationServiceClient.Code.SERVER_INTERNAL_ERROR));
when(firebaseInstanceId.getInstanceId())
.thenReturn(
Tasks.forResult(
new InstanceIdResult() {
@NonNull
@Override
public String getId() {
return FIREBASE_INSTANCE_ID;
}

@NonNull
@Override
public String getToken() {
return "iid_token";
}
}));
when(cacheReturnsError.insertOrUpdateCacheEntry(any())).thenReturn(Tasks.forResult(false));
when(cacheReturnsError.readCacheEntryValue()).thenReturn(null);
}

@After
public void cleanUp() throws Exception {
Tasks.await(actualCache.clear());
}

@Test
public void testUpdateCustomInstallationId_CacheOk_BackendOk() throws Exception {
FirebaseSegmentation firebaseSegmentation =
new FirebaseSegmentation(
firebaseApp, firebaseInstanceId, actualCache, backendClientReturnsOk);

// No exception, means success.
assertNull(Tasks.await(firebaseSegmentation.setCustomInstallationId(CUSTOM_INSTALLATION_ID)));
CustomInstallationIdCacheEntryValue entryValue = actualCache.readCacheEntryValue();
assertThat(entryValue.getCustomInstallationId()).isEqualTo(CUSTOM_INSTALLATION_ID);
assertThat(entryValue.getFirebaseInstanceId()).isEqualTo(FIREBASE_INSTANCE_ID);
assertThat(entryValue.getCacheStatus()).isEqualTo(CustomInstallationIdCache.CacheStatus.SYNCED);
}

@Test
public void testUpdateCustomInstallationId_CacheOk_BackendError() throws InterruptedException {
FirebaseSegmentation firebaseSegmentation =
new FirebaseSegmentation(
firebaseApp, firebaseInstanceId, actualCache, backendClientReturnsError);

// Expect exception
try {
Tasks.await(firebaseSegmentation.setCustomInstallationId(CUSTOM_INSTALLATION_ID));
fail();
} catch (ExecutionException expected) {
Throwable cause = expected.getCause();
assertThat(cause).isInstanceOf(SetCustomInstallationIdException.class);
assertThat(((SetCustomInstallationIdException) cause).getStatus())
.isEqualTo(SetCustomInstallationIdException.Status.BACKEND_ERROR);
}

CustomInstallationIdCacheEntryValue entryValue = actualCache.readCacheEntryValue();
assertThat(entryValue.getCustomInstallationId()).isEqualTo(CUSTOM_INSTALLATION_ID);
assertThat(entryValue.getFirebaseInstanceId()).isEqualTo(FIREBASE_INSTANCE_ID);
assertThat(entryValue.getCacheStatus())
.isEqualTo(CustomInstallationIdCache.CacheStatus.PENDING_UPDATE);
}

@Test
public void useAppContext() {
assertNull(FirebaseSegmentation.getInstance().setCustomInstallationId("123123").getResult());
public void testUpdateCustomInstallationId_CacheError_BackendOk() throws InterruptedException {
FirebaseSegmentation firebaseSegmentation =
new FirebaseSegmentation(
firebaseApp, firebaseInstanceId, cacheReturnsError, backendClientReturnsOk);

// Expect exception
try {
Tasks.await(firebaseSegmentation.setCustomInstallationId(CUSTOM_INSTALLATION_ID));
fail();
} catch (ExecutionException expected) {
Throwable cause = expected.getCause();
assertThat(cause).isInstanceOf(SetCustomInstallationIdException.class);
assertThat(((SetCustomInstallationIdException) cause).getStatus())
.isEqualTo(SetCustomInstallationIdException.Status.CLIENT_ERROR);
}
}

@Test
public void testClearCustomInstallationId_CacheOk_BackendOk() throws Exception {
Tasks.await(
actualCache.insertOrUpdateCacheEntry(
CustomInstallationIdCacheEntryValue.create(
CUSTOM_INSTALLATION_ID,
FIREBASE_INSTANCE_ID,
CustomInstallationIdCache.CacheStatus.SYNCED)));
FirebaseSegmentation firebaseSegmentation =
new FirebaseSegmentation(
firebaseApp, firebaseInstanceId, actualCache, backendClientReturnsOk);

// No exception, means success.
assertNull(Tasks.await(firebaseSegmentation.setCustomInstallationId(null)));
CustomInstallationIdCacheEntryValue entryValue = actualCache.readCacheEntryValue();
assertNull(entryValue);
}

@Test
public void testClearCustomInstallationId_CacheOk_BackendError() throws Exception {
Tasks.await(
actualCache.insertOrUpdateCacheEntry(
CustomInstallationIdCacheEntryValue.create(
CUSTOM_INSTALLATION_ID,
FIREBASE_INSTANCE_ID,
CustomInstallationIdCache.CacheStatus.SYNCED)));
FirebaseSegmentation firebaseSegmentation =
new FirebaseSegmentation(
firebaseApp, firebaseInstanceId, actualCache, backendClientReturnsError);

// Expect exception
try {
Tasks.await(firebaseSegmentation.setCustomInstallationId(null));
fail();
} catch (ExecutionException expected) {
Throwable cause = expected.getCause();
assertThat(cause).isInstanceOf(SetCustomInstallationIdException.class);
assertThat(((SetCustomInstallationIdException) cause).getStatus())
.isEqualTo(SetCustomInstallationIdException.Status.BACKEND_ERROR);
}

CustomInstallationIdCacheEntryValue entryValue = actualCache.readCacheEntryValue();
assertThat(entryValue.getCustomInstallationId().isEmpty()).isTrue();
assertThat(entryValue.getFirebaseInstanceId()).isEqualTo(FIREBASE_INSTANCE_ID);
assertThat(entryValue.getCacheStatus())
.isEqualTo(CustomInstallationIdCache.CacheStatus.PENDING_CLEAR);
}

@Test
public void testClearCustomInstallationId_CacheError_BackendOk() throws InterruptedException {
FirebaseSegmentation firebaseSegmentation =
new FirebaseSegmentation(
firebaseApp, firebaseInstanceId, cacheReturnsError, backendClientReturnsOk);

// Expect exception
try {
Tasks.await(firebaseSegmentation.setCustomInstallationId(CUSTOM_INSTALLATION_ID));
fail();
} catch (ExecutionException expected) {
Throwable cause = expected.getCause();
assertThat(cause).isInstanceOf(SetCustomInstallationIdException.class);
assertThat(((SetCustomInstallationIdException) cause).getStatus())
.isEqualTo(SetCustomInstallationIdException.Status.CLIENT_ERROR);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public class CustomInstallationIdCacheTest {

private FirebaseApp firebaseApp0;
private FirebaseApp firebaseApp1;
private CustomInstallationIdCache cache;
private CustomInstallationIdCache cache0;
private CustomInstallationIdCache cache1;

@Before
public void setUp() {
Expand All @@ -48,42 +49,44 @@ public void setUp() {
ApplicationProvider.getApplicationContext(),
new FirebaseOptions.Builder().setApplicationId("1:987654321:android:abcdef").build(),
"firebase_app_1");
cache = CustomInstallationIdCache.getInstance();
cache0 = new CustomInstallationIdCache(firebaseApp0);
cache1 = new CustomInstallationIdCache(firebaseApp1);
}

@After
public void cleanUp() throws Exception {
Tasks.await(cache.clearAll());
Tasks.await(cache0.clear());
Tasks.await(cache1.clear());
}

@Test
public void testReadCacheEntry_Null() {
assertNull(cache.readCacheEntryValue(firebaseApp0));
assertNull(cache.readCacheEntryValue(firebaseApp1));
assertNull(cache0.readCacheEntryValue());
assertNull(cache1.readCacheEntryValue());
}

@Test
public void testUpdateAndReadCacheEntry() throws Exception {
assertTrue(
Tasks.await(
cache.insertOrUpdateCacheEntry(
firebaseApp0,
cache0.insertOrUpdateCacheEntry(
CustomInstallationIdCacheEntryValue.create(
"123456", "cAAAAAAAAAA", CustomInstallationIdCache.CacheStatus.PENDING))));
CustomInstallationIdCacheEntryValue entryValue = cache.readCacheEntryValue(firebaseApp0);
"123456",
"cAAAAAAAAAA",
CustomInstallationIdCache.CacheStatus.PENDING_UPDATE))));
CustomInstallationIdCacheEntryValue entryValue = cache0.readCacheEntryValue();
assertThat(entryValue.getCustomInstallationId()).isEqualTo("123456");
assertThat(entryValue.getFirebaseInstanceId()).isEqualTo("cAAAAAAAAAA");
assertThat(entryValue.getCacheStatus())
.isEqualTo(CustomInstallationIdCache.CacheStatus.PENDING);
assertNull(cache.readCacheEntryValue(firebaseApp1));
.isEqualTo(CustomInstallationIdCache.CacheStatus.PENDING_UPDATE);
assertNull(cache1.readCacheEntryValue());

assertTrue(
Tasks.await(
cache.insertOrUpdateCacheEntry(
firebaseApp0,
cache0.insertOrUpdateCacheEntry(
CustomInstallationIdCacheEntryValue.create(
"123456", "cAAAAAAAAAA", CustomInstallationIdCache.CacheStatus.SYNCED))));
entryValue = cache.readCacheEntryValue(firebaseApp0);
entryValue = cache0.readCacheEntryValue();
assertThat(entryValue.getCustomInstallationId()).isEqualTo("123456");
assertThat(entryValue.getFirebaseInstanceId()).isEqualTo("cAAAAAAAAAA");
assertThat(entryValue.getCacheStatus()).isEqualTo(CustomInstallationIdCache.CacheStatus.SYNCED);
Expand Down
Loading