Skip to content

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 3 commits into from
Apr 2, 2019
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
38 changes: 38 additions & 0 deletions smoke-tests/README.md
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.
70 changes: 70 additions & 0 deletions smoke-tests/build.gradle
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"
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);
}
}
}
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 {}
17 changes: 17 additions & 0 deletions smoke-tests/src/firestore/AndroidManifest.xml
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>
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();
}
}
}
6 changes: 6 additions & 0 deletions smoke-tests/src/main/AndroidManifest.xml
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>
Loading