Skip to content

add manifest flag unit tests. #2446

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 2 commits into from
Feb 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2021 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.

plugins {
id 'com.android.application'
}

android {
adbOptions {
timeOutInMs 60 * 1000
}

compileSdkVersion project.targetSdkVersion
defaultConfig {
minSdkVersion 16
targetSdkVersion project.targetSdkVersion
versionName version
multiDexEnabled true
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
testOptions {
unitTests {
includeAndroidResources = true
}
}
}

dependencies {
implementation project(':firebase-common')
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are all these dependencies necessary for the actual implementation of the test? Otherwise I think you can get away with just depending on common (for firebaseApp) and mldownloader.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

implementation project(':firebase-ml-modeldownloader')
testImplementation 'androidx.test:runner:1.2.0'
testImplementation 'androidx.test.ext:junit:1.1.1'
testImplementation "org.robolectric:robolectric:$robolectricVersion"
testImplementation 'junit:junit:4.12'
testImplementation "com.google.truth:truth:$googleTruthVersion"
implementation 'androidx.core:core:1.2.0'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2021 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. -->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.firebase.ml_data_collection_tests">

<application>
<meta-data android:name="firebase_model_downloader_collection_enabled"
android:value="false" />

<service android:name="com.google.firebase.components.ComponentDiscoveryService" android:exported="false">
<meta-data
android:name="com.google.firebase.components:com.google.firebase.ml.modeldownloader.FirebaseModelDownloaderRegistrar"
android:value="com.google.firebase.components.ComponentRegistrar" />
</service>
</application>
</manifest>

Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2021 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.ml_data_collection_tests;

import static com.google.common.truth.Truth.assertThat;
import static com.google.firebase.ml_data_collection_tests.MlDataCollectionTestUtil.getSharedPreferencesUtil;
import static com.google.firebase.ml_data_collection_tests.MlDataCollectionTestUtil.setSharedPreferencesTo;
import static com.google.firebase.ml_data_collection_tests.MlDataCollectionTestUtil.withApp;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
public class MlDataCollectionDefaultDisabledTest {

@Test
public void isMlDataCollectionDefaultEnabled_whenMetadataFalse_shouldReturnFalse() {
withApp(
app ->
assertThat(getSharedPreferencesUtil(app).getCustomModelStatsCollectionFlag())
.isFalse());
}

@Test
public void isMlDataCollectionDefaultEnabled_whenMetadataFalseAndPrefsFalse_shouldReturnFalse() {
withApp(
app -> {
setSharedPreferencesTo(app, false);
assertThat(getSharedPreferencesUtil(app).getCustomModelStatsCollectionFlag()).isFalse();
});
}

@Test
public void isMlDataCollectionDefaultEnabled_whenMetadataFalseAndPrefsTrue_shouldReturnTrue() {
withApp(
app -> {
setSharedPreferencesTo(app, true);
assertThat(getSharedPreferencesUtil(app).getCustomModelStatsCollectionFlag()).isTrue();
});
}

@Test
public void isMlDataCollectionDefaultEnabled_whenMetadataFalseAndPrefsNull_shouldReturnFalse() {
withApp(
app -> {
setSharedPreferencesTo(app, null);
assertThat(getSharedPreferencesUtil(app).getCustomModelStatsCollectionFlag()).isFalse();
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2021 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.ml_data_collection_tests;

import android.content.Context;
import android.content.SharedPreferences;
import androidx.test.core.app.ApplicationProvider;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.ml.modeldownloader.internal.SharedPreferencesUtil;
import java.util.function.Consumer;

final class MlDataCollectionTestUtil {
private static final String APP_NAME = "someApp";
private static final String APP_ID = "appId";
private static final String TEST_PROJECT_ID = "777777777777";

private static final FirebaseOptions OPTIONS =
new FirebaseOptions.Builder().setApplicationId(APP_ID).setProjectId(TEST_PROJECT_ID).build();

private MlDataCollectionTestUtil() {}

static void withApp(Consumer<FirebaseApp> callable) {
withApp(APP_NAME, callable);
}

static void withApp(String name, Consumer<FirebaseApp> callable) {
FirebaseApp app =
FirebaseApp.initializeApp(ApplicationProvider.getApplicationContext(), OPTIONS, name);
try {
callable.accept(app);
} finally {
app.delete();
}
}

static SharedPreferencesUtil getSharedPreferencesUtil(FirebaseApp app) {
return new SharedPreferencesUtil(app);
}

static SharedPreferences getSharedPreferences(FirebaseApp app) {
return app.getApplicationContext()
.getSharedPreferences(SharedPreferencesUtil.PREFERENCES_PACKAGE_NAME, Context.MODE_PRIVATE);
}

static void setSharedPreferencesTo(FirebaseApp app, Boolean enabled) {
getSharedPreferencesUtil(app).setCustomModelStatsCollectionEnabled(enabled);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
/** @hide */
public class SharedPreferencesUtil {

private static final String FIREBASE_MODELDOWNLOADER_COLLECTION_ENABLED =
public static final String FIREBASE_MODELDOWNLOADER_COLLECTION_ENABLED =
"firebase_model_downloader_collection_enabled";
public static final String DOWNLOADING_MODEL_ID_MATCHER = "downloading_model_id_(.*?)_([^/]+)/?";

Expand Down
1 change: 1 addition & 0 deletions subprojects.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ firebase-installations-interop
firebase-installations
firebase-installations:ktx
firebase-ml-modeldownloader
firebase-ml-modeldownloader:ml-data-collection-tests
firebase-perf
firebase-perf:ktx
firebase-perf:dev-app
Expand Down