Skip to content

[Firebase Segmentation] Add custom installation id cache layer and tests for it. #524

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 20 commits into from
Jun 18, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
1962431
Add type arguments in StorageTaskManager (#517)
schmidt-sebastian Jun 13, 2019
b69b2c8
Output artifact list during local publishing. (#515)
allisonbm92 Jun 13, 2019
8881325
Implement Firebase segmentation SDK device local cache
diwu-arete Jun 13, 2019
771d23c
fix functions (#523)
VinayGuthal Jun 14, 2019
dc3f410
Set test type to release only in CI. (#522)
vkryachko Jun 14, 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
18d4e7e
Minor fix to error message to match the admin sdk. (#525)
rsgowman Jun 17, 2019
d65c21c
Added clean task to smoke tests. (#527)
allisonbm92 Jun 17, 2019
3e2cc89
Update deps to post-androidx gms versions. (#526)
vkryachko Jun 17, 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
c1f7644
Copy firebase-firestore-ktx dependencies on firestore into its own su…
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
097ff36
Bump firestore version for release (#530)
vkryachko 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
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
13 changes: 9 additions & 4 deletions firebase-segmentation/firebase-segmentation.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ android {
compileSdkVersion project.targetSdkVersion

defaultConfig {
minSdkVersion project.minSdkVersion
minSdkVersion 21
targetSdkVersion project.targetSdkVersion
multiDexEnabled true
versionName version
Expand Down Expand Up @@ -91,12 +91,17 @@ dependencies {
implementation 'androidx.multidex:multidex:2.0.0'
implementation 'com.google.android.gms:play-services-tasks:16.0.1'

compileOnly "com.google.auto.value:auto-value-annotations:1.6.5"
annotationProcessor "com.google.auto.value:auto-value:1.6.2"

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:runner:1.2.0'
androidTestImplementation 'androidx.test:rules:1.2.0'
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'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// 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 static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertNull;

import androidx.test.InstrumentationRegistry;
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;

/** Instrumented tests for {@link CustomInstallationIdCache} */
@RunWith(AndroidJUnit4.class)
public class CustomInstallationIdCacheTest {

private FirebaseApp firebaseApp0;
private FirebaseApp firebaseApp1;
private CustomInstallationIdCache cache;

@Before
public void setUp() {
FirebaseApp.clearInstancesForTest();
firebaseApp0 =
FirebaseApp.initializeApp(
InstrumentationRegistry.getContext(),
new FirebaseOptions.Builder().setApplicationId("1:123456789:android:abcdef").build());
firebaseApp1 =
FirebaseApp.initializeApp(
InstrumentationRegistry.getContext(),
new FirebaseOptions.Builder().setApplicationId("1:987654321:android:abcdef").build(),
"firebase_app_1");
cache = new CustomInstallationIdCache();
}

@After
public void cleanUp() {
cache.clear();
}

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

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

cache.insertOrUpdateCacheEntry(
firebaseApp0,
CustomInstallationIdCacheEntryValue.create(
"123456", "cAAAAAAAAAA", CustomInstallationIdCache.CacheStatus.SYNCED));
entryValue = cache.readCacheEntryValue(firebaseApp0);
assertThat(entryValue.getCustomInstallationId()).isEqualTo("123456");
assertThat(entryValue.getFirebaseInstanceId()).isEqualTo("cAAAAAAAAAA");
assertThat(entryValue.getCacheStatus()).isEqualTo(CustomInstallationIdCache.CacheStatus.SYNCED);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// 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 android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import com.google.android.gms.common.internal.Preconditions;
import com.google.firebase.FirebaseApp;

class CustomInstallationIdCache {

// Status of each cache entry
// NOTE: never change the ordinal of the enum values because the enum values are stored in cache
// as their ordinal numbers.
enum CacheStatus {
// Cache entry is synced to Firebase backend
SYNCED,
// Cache entry is waiting for Firebase backend response or pending internal retry for retryable
// errors.
PENDING,
// Cache entry is not accepted by Firebase backend.
ERROR
}

private static final String LOCAL_DB_NAME = "CustomInstallationIdCache";
private static final String TABLE_NAME = "InstallationIdMapping";

private static final String GMP_APP_ID_COLUMN_NAME = "GmpAppId";
private static final String FIREBASE_APP_NAME_COLUMN_NAME = "AppName";
private static final String CUSTOM_INSTALLATION_ID_COLUMN_NAME = "Cid";
private static final String INSTANCE_ID_COLUMN_NAME = "Iid";
private static final String CACHE_STATUS_COLUMN = "Status";

private static final String QUERY_WHERE_CLAUSE =
String.format(
"%s = ? " + "AND " + "%s = ?", GMP_APP_ID_COLUMN_NAME, FIREBASE_APP_NAME_COLUMN_NAME);

private final SQLiteDatabase localDb;

CustomInstallationIdCache() {
// Since different FirebaseApp in the same Android application should have the same application
// context and same dir path, so that use the context of the default FirebaseApp to create/open
// the database.
localDb =
SQLiteDatabase.openOrCreateDatabase(
FirebaseApp.getInstance()
.getApplicationContext()
.getNoBackupFilesDir()
.getAbsolutePath()
+ "/"
+ LOCAL_DB_NAME,
null);

localDb.execSQL(
String.format(
"CREATE TABLE IF NOT EXISTS %s(%s TEXT NOT NULL, %s TEXT NOT NULL, "
+ "%s TEXT NOT NULL, %s TEXT NOT NULL, %s INTEGER NOT NULL, PRIMARY KEY (%s, %s));",
TABLE_NAME,
GMP_APP_ID_COLUMN_NAME,
FIREBASE_APP_NAME_COLUMN_NAME,
CUSTOM_INSTALLATION_ID_COLUMN_NAME,
INSTANCE_ID_COLUMN_NAME,
CACHE_STATUS_COLUMN,
GMP_APP_ID_COLUMN_NAME,
FIREBASE_APP_NAME_COLUMN_NAME));
}

@Nullable
CustomInstallationIdCacheEntryValue readCacheEntryValue(FirebaseApp firebaseApp) {
String gmpAppId = firebaseApp.getOptions().getApplicationId();
String appName = firebaseApp.getName();
Cursor cursor =
localDb.query(
TABLE_NAME,
new String[] {
CUSTOM_INSTALLATION_ID_COLUMN_NAME, INSTANCE_ID_COLUMN_NAME, CACHE_STATUS_COLUMN
},
QUERY_WHERE_CLAUSE,
new String[] {gmpAppId, appName},
null,
null,
null);
CustomInstallationIdCacheEntryValue value = null;
while (cursor.moveToNext()) {
Preconditions.checkArgument(
value == null, "Multiple cache entries found for " + "firebase app %s", appName);
value =
CustomInstallationIdCacheEntryValue.create(
cursor.getString(cursor.getColumnIndex(CUSTOM_INSTALLATION_ID_COLUMN_NAME)),
cursor.getString(cursor.getColumnIndex(INSTANCE_ID_COLUMN_NAME)),
CacheStatus.values()[cursor.getInt(cursor.getColumnIndex(CACHE_STATUS_COLUMN))]);
}
return value;
}

void insertOrUpdateCacheEntry(
FirebaseApp firebaseApp, CustomInstallationIdCacheEntryValue entryValue) {
localDb.execSQL(
String.format(
"INSERT OR REPLACE INTO %s(%s, %s, %s, %s, %s) VALUES(%s, %s, %s, %s, %s)",
TABLE_NAME,
GMP_APP_ID_COLUMN_NAME,
FIREBASE_APP_NAME_COLUMN_NAME,
CUSTOM_INSTALLATION_ID_COLUMN_NAME,
INSTANCE_ID_COLUMN_NAME,
CACHE_STATUS_COLUMN,
"\"" + firebaseApp.getOptions().getApplicationId() + "\"",
"\"" + firebaseApp.getName() + "\"",
"\"" + entryValue.getCustomInstallationId() + "\"",
"\"" + entryValue.getFirebaseInstanceId() + "\"",
entryValue.getCacheStatus().ordinal()));
}

@VisibleForTesting
void clear() {
localDb.execSQL(String.format("DROP TABLE IF EXISTS %s", TABLE_NAME));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 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 com.google.auto.value.AutoValue;
import com.google.firebase.segmentation.CustomInstallationIdCache.CacheStatus;

/**
* This class represents a cache entry value in {@link CustomInstallationIdCache}, which contains a
* Firebase instance id, a custom installation id and the cache status of this entry.
*/
@AutoValue
abstract class CustomInstallationIdCacheEntryValue {
abstract String getCustomInstallationId();

abstract String getFirebaseInstanceId();

abstract CacheStatus getCacheStatus();

static CustomInstallationIdCacheEntryValue create(
String customInstallationId, String firebaseInstanceId, CacheStatus cacheStatus) {
return new AutoValue_CustomInstallationIdCacheEntryValue(
customInstallationId, firebaseInstanceId, cacheStatus);
}
}