-
Notifications
You must be signed in to change notification settings - Fork 624
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
firebase-ml-modeldownloader/ml-data-collection-tests/ml-data-collection-tests.gradle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
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' | ||
} |
30 changes: 30 additions & 0 deletions
30
firebase-ml-modeldownloader/ml-data-collection-tests/src/main/AndroidManifest.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
|
63 changes: 63 additions & 0 deletions
63
...ava/com/google/firebase/ml_data_collection_tests/MlDataCollectionDefaultDisabledTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
.../src/test/java/com/google/firebase/ml_data_collection_tests/MlDataCollectionTestUtil.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed