-
Notifications
You must be signed in to change notification settings - Fork 624
Add new smoke tests (Firestore) #319
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
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
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,38 @@ | ||
# Firebase Smoke Test Suite | ||
|
||
This directory contains smoke tests for Firebase on Android. These tests are | ||
intended to verify the integrations between different components and versions of | ||
Firebase. The tests should not include additional overhead, such as user | ||
interfaces. However, the tests should strive to remain similar to real use | ||
cases. As such, these tests run on devices or emulators (no Robolectric). This | ||
is a work in progress, and the following list shows what is complete: | ||
|
||
- [ ] Create first set of tests to replace old test apps. | ||
- [ ] Reliably run smoke tests on CI. | ||
- [ ] Support version matrices. | ||
- [ ] Extend to collect system health metrics. | ||
|
||
# Project Structure | ||
|
||
This Gradle project is split into flavors for each Firebase product. Test code | ||
lives in these flavors. Each flavor has one or more test classes in the app APK. | ||
These are regular JUnit tests. | ||
|
||
There is only one testing variant, `androidTest`. This contains the | ||
`SmokeTestSuite` JUnit test runner. This custom runner finds the test classes by | ||
searching for metadata in the application's Android manifest. Therefore, a | ||
metadata tag named `com.google.firebase.testing.classes` is needed in each | ||
application variant with a comma-separated list of test class names. The | ||
`androidTest` variant does not need to be modified by test authors. | ||
|
||
# Test Workflow | ||
|
||
Android instrumentation uses two APKs: one for the app and the other for tests. | ||
As we want to test building as a third-party consumer, the test code and | ||
Firebase dependencies all belong in the application APK. This can be optionally | ||
obfsucated like a real application. | ||
|
||
The Android instrumentation runner loads one test class, `SmokeTests`. This is | ||
executed by the `SmokeTestSuite`. `SmokeTests` is simply a placeholder class to | ||
bootstrap the logic contained in the suite runner, which then delegates to the | ||
test runners for the classes referenced by the Android manifest. |
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,70 @@ | ||
// Copyright 2018 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. | ||
|
||
buildscript { | ||
repositories { | ||
google() | ||
jcenter() | ||
} | ||
|
||
dependencies { | ||
classpath "com.android.tools.build:gradle:3.3.2" | ||
classpath "com.google.gms:google-services:4.0.0" | ||
} | ||
} | ||
|
||
apply plugin: "com.android.application" | ||
|
||
android { | ||
compileSdkVersion 24 | ||
|
||
defaultConfig { | ||
minSdkVersion 16 | ||
multiDexEnabled true | ||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||
} | ||
|
||
flavorDimensions "systemUnderTest" | ||
|
||
productFlavors { | ||
firestore { | ||
dimension "systemUnderTest" | ||
applicationId "com.google.firebase.testing.firestore" | ||
} | ||
} | ||
} | ||
|
||
repositories { | ||
google() | ||
jcenter() | ||
} | ||
|
||
dependencies { | ||
// Common | ||
|
||
api "androidx.test:runner:1.1.0" | ||
api "junit:junit:4.12" | ||
|
||
implementation "androidx.test:rules:1.1.0" | ||
implementation "com.google.firebase:firebase-common:16.0.7" | ||
|
||
// Firestore | ||
firestoreImplementation "com.google.android.gms:play-services-tasks:16.0.1" | ||
firestoreImplementation "com.google.firebase:firebase-auth:16.1.0" | ||
firestoreImplementation "com.google.firebase:firebase-firestore:18.1.0" | ||
firestoreImplementation "com.google.truth:truth:0.43" | ||
|
||
} | ||
|
||
apply plugin: "com.google.gms.google-services" |
108 changes: 108 additions & 0 deletions
108
smoke-tests/src/androidTest/java/com/google/firebase/testing/common/SmokeTestSuite.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,108 @@ | ||
// Copyright 2018 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.testing.common; | ||
|
||
import android.content.Context; | ||
import android.content.pm.ApplicationInfo; | ||
import android.content.pm.PackageManager; | ||
import androidx.test.InstrumentationRegistry; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import org.junit.runner.Description; | ||
import org.junit.runner.Runner; | ||
import org.junit.runner.notification.RunNotifier; | ||
import org.junit.runners.ParentRunner; | ||
import org.junit.runners.model.InitializationError; | ||
import org.junit.runners.model.RunnerBuilder; | ||
|
||
/** | ||
* A runner for smoke tests. | ||
* | ||
* <p>This is a JUnit runner inspired by the {@code Suite} runner from JUnit. That runner uses an | ||
* annotation to determine the classes in the suite. This runner, on the other hand, uses metadata | ||
* in the application's Android manifest. Therefore, a single test class can be reused with many | ||
* different test suites by simply changing the manifest. | ||
* | ||
* <p>The application's manifest must include metadata with the name {@code | ||
* com.google.firebase.testing.classes} or else this run with fail to run. This item is expected to | ||
* be a comma-separated list of class names. These class names must be valid, loadable JUnit test | ||
* classes in the application APK. | ||
*/ | ||
public class SmokeTestSuite extends ParentRunner<Runner> { | ||
|
||
private static final String TAG = "SmokeTestSuite"; | ||
private static final String TEST_CLASSES_KEY = "com.google.firebase.testing.classes"; | ||
|
||
private final List<Runner> runners; | ||
|
||
public SmokeTestSuite(Class<?> clazz, RunnerBuilder builder) throws InitializationError { | ||
super(clazz); | ||
this.runners = builder.runners(clazz, getTestClasses()); | ||
} | ||
|
||
@Override | ||
protected Description describeChild(Runner runner) { | ||
return runner.getDescription(); | ||
} | ||
|
||
@Override | ||
protected List<Runner> getChildren() { | ||
return runners; | ||
} | ||
|
||
@Override | ||
protected void runChild(Runner runner, RunNotifier notifier) { | ||
runner.run(notifier); | ||
} | ||
|
||
private static List<Class<?>> getTestClasses() throws InitializationError { | ||
Context ctx = InstrumentationRegistry.getTargetContext(); | ||
return getTestClasses(ctx); | ||
} | ||
|
||
private static List<Class<?>> getTestClasses(Context ctx) throws InitializationError { | ||
List<String> names = getTestClassNames(ctx); | ||
ArrayList<Class<?>> classes = new ArrayList<>(names.size()); | ||
|
||
try { | ||
for (String name : names) { | ||
Class<?> c = Class.forName(name); | ||
classes.add(c); | ||
} | ||
} catch (ClassNotFoundException ex) { | ||
throw new InitializationError(ex); | ||
} | ||
|
||
return classes; | ||
} | ||
|
||
private static List<String> getTestClassNames(Context ctx) throws InitializationError { | ||
String name = ctx.getPackageName(); | ||
try { | ||
PackageManager pm = ctx.getPackageManager(); | ||
ApplicationInfo ai = pm.getApplicationInfo(name, PackageManager.GET_META_DATA); | ||
String names = ai.metaData.getString(TEST_CLASSES_KEY); | ||
|
||
if (names == null) { | ||
throw new InitializationError("No test classes found in Application Manifest"); | ||
} | ||
|
||
return Arrays.asList(names.split(",")); | ||
} catch (PackageManager.NameNotFoundException ex) { | ||
throw new InitializationError(ex); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
smoke-tests/src/androidTest/java/com/google/firebase/testing/common/SmokeTests.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,21 @@ | ||
// Copyright 2018 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.testing.common; | ||
|
||
import org.junit.runner.RunWith; | ||
|
||
/** An empty class that serves as the entrypoint for the smoke tests. */ | ||
@RunWith(SmokeTestSuite.class) | ||
public final class SmokeTests {} |
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,17 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.google.firebase.testing"> | ||
|
||
<uses-permission android:name="android.permission.INTERNET" /> | ||
|
||
<application> | ||
<activity android:name="android.app.Activity"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
|
||
<meta-data android:name="com.google.firebase.testing.classes" | ||
android:value="com.google.firebase.testing.firestore.FirestoreTest" /> | ||
</application> | ||
</manifest> |
88 changes: 88 additions & 0 deletions
88
smoke-tests/src/firestore/java/com/google/firebase/testing/firestore/FirestoreTest.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,88 @@ | ||
// Copyright 2018 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.testing.firestore; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import android.app.Activity; | ||
import androidx.test.rule.ActivityTestRule; | ||
import androidx.test.runner.AndroidJUnit4; | ||
import com.google.android.gms.tasks.Task; | ||
import com.google.android.gms.tasks.TaskCompletionSource; | ||
import com.google.firebase.auth.FirebaseAuth; | ||
import com.google.firebase.firestore.DocumentReference; | ||
import com.google.firebase.firestore.DocumentSnapshot; | ||
import com.google.firebase.firestore.EventListener; | ||
import com.google.firebase.firestore.FirebaseFirestore; | ||
import com.google.firebase.firestore.FirebaseFirestoreException; | ||
import com.google.firebase.firestore.ListenerRegistration; | ||
import com.google.firebase.testing.common.Tasks2; | ||
import java.util.HashMap; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
/** Firestore smoke tests. */ | ||
@RunWith(AndroidJUnit4.class) | ||
public final class FirestoreTest { | ||
|
||
@Rule public final ActivityTestRule<Activity> activity = new ActivityTestRule<>(Activity.class); | ||
|
||
@Test | ||
public void listenForUpdate() throws Exception { | ||
FirebaseAuth auth = FirebaseAuth.getInstance(); | ||
FirebaseFirestore firestore = FirebaseFirestore.getInstance(); | ||
|
||
auth.signOut(); | ||
Task<?> signInTask = auth.signInWithEmailAndPassword("[email protected]", "password"); | ||
Tasks2.waitForSuccess(signInTask); | ||
|
||
DocumentReference doc = firestore.collection("restaurants").document("Baadal"); | ||
SnapshotListener listener = new SnapshotListener(); | ||
ListenerRegistration registration = doc.addSnapshotListener(listener); | ||
|
||
try { | ||
HashMap<String, Object> data = new HashMap<>(); | ||
data.put("location", "Google NYC"); | ||
|
||
Task<?> setTask = doc.set(new HashMap<>(data)); | ||
Task<DocumentSnapshot> snapshotTask = listener.toTask(); | ||
Tasks2.waitForSuccess(setTask); | ||
Tasks2.waitForSuccess(snapshotTask); | ||
|
||
DocumentSnapshot result = snapshotTask.getResult(); | ||
assertThat(result.getData()).isEqualTo(data); | ||
} finally { | ||
registration.remove(); | ||
} | ||
} | ||
|
||
private static class SnapshotListener implements EventListener<DocumentSnapshot> { | ||
private final TaskCompletionSource<DocumentSnapshot> taskFactory = new TaskCompletionSource<>(); | ||
|
||
@Override | ||
public void onEvent(DocumentSnapshot snapshot, FirebaseFirestoreException error) { | ||
if (error != null) { | ||
taskFactory.trySetException(error); | ||
} else if (snapshot.exists()) { | ||
taskFactory.trySetResult(snapshot); | ||
} | ||
} | ||
|
||
public Task<DocumentSnapshot> toTask() { | ||
return taskFactory.getTask(); | ||
} | ||
} | ||
} |
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,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.google.firebase.testing"> | ||
|
||
<uses-permission android:name="android.permission.INTERNET" /> | ||
|
||
</manifest> |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.