Skip to content

Copy firebase-firestore-ktx dependencies on firestore into its own subfolder #528

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 5 commits into from
Jun 18, 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
11 changes: 6 additions & 5 deletions firebase-firestore/ktx/ktx.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ firebaseLibrary {
}

android {
compileSdkVersion project.targetSdkVersion
compileSdkVersion 28
defaultConfig {
minSdkVersion project.minSdkVersion
multiDexEnabled true
Expand All @@ -34,21 +34,22 @@ android {
main.java.srcDirs += 'src/main/kotlin'
test.java {
srcDir 'src/test/kotlin'
srcDir '../src/testUtil/java'
srcDir '../src/roboUtil/java'
srcDir 'src/test/java'
}
}
testOptions.unitTests.includeAndroidResources = true
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"

implementation project(':firebase-common')
implementation project(':firebase-common:ktx')
implementation project(':firebase-firestore')
implementation 'androidx.annotation:annotation:1.1.0'

testImplementation project(':firebase-database-collection')
testImplementation 'org.mockito:mockito-core:2.25.0'
testImplementation 'com.fasterxml.jackson.core:jackson-databind:2.9.8'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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.firestore;

import com.google.firebase.firestore.model.DocumentKey;

public final class TestAccessHelper {

/** Makes the DocumentReference constructor accessible. */
public static DocumentReference createDocumentReference(DocumentKey documentKey) {
// We can use null here because the tests only use this as a wrapper for documentKeys.
return new DocumentReference(documentKey, null);
}

/** Makes the getKey() method accessible. */
public static DocumentKey referenceKey(DocumentReference documentReference) {
return documentReference.getKey();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
// 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.firestore;

import static com.google.firebase.firestore.testutil.TestUtil.doc;
import static com.google.firebase.firestore.testutil.TestUtil.docSet;
import static com.google.firebase.firestore.testutil.TestUtil.key;
import static org.mockito.Mockito.mock;

import androidx.annotation.Nullable;
import com.google.android.gms.tasks.Task;
import com.google.firebase.database.collection.ImmutableSortedSet;
import com.google.firebase.firestore.core.DocumentViewChange;
import com.google.firebase.firestore.core.DocumentViewChange.Type;
import com.google.firebase.firestore.core.ViewSnapshot;
import com.google.firebase.firestore.local.QueryData;
import com.google.firebase.firestore.model.Document;
import com.google.firebase.firestore.model.DocumentKey;
import com.google.firebase.firestore.model.DocumentSet;
import com.google.firebase.firestore.model.ResourcePath;
import com.google.firebase.firestore.model.value.ObjectValue;
import com.google.firebase.firestore.remote.WatchChangeAggregator;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Assert;
import org.robolectric.Robolectric;

public class TestUtil {

private static final FirebaseFirestore FIRESTORE = mock(FirebaseFirestore.class);

public static FirebaseFirestore firestore() {
return FIRESTORE;
}

public static CollectionReference collectionReference(String path) {
return new CollectionReference(ResourcePath.fromString(path), FIRESTORE);
}

public static DocumentReference documentReference(String path) {
return new DocumentReference(key(path), FIRESTORE);
}

public static DocumentSnapshot documentSnapshot(
String path, Map<String, Object> data, boolean isFromCache) {
if (data == null) {
return DocumentSnapshot.fromNoDocument(
FIRESTORE, key(path), isFromCache, /*hasPendingWrites=*/ false);
} else {
return DocumentSnapshot.fromDocument(
FIRESTORE, doc(path, 1L, data), isFromCache, /*hasPendingWrites=*/ false);
}
}

public static Query query(String path) {
return new Query(com.google.firebase.firestore.testutil.TestUtil.query(path), FIRESTORE);
}

/**
* A convenience method for creating a particular query snapshot for tests.
*
* @param path To be used in constructing the query.
* @param oldDocs Provides the prior set of documents in the QuerySnapshot. Each entry maps to a
* document, with the key being the document id, and the value being the document contents.
* @param docsToAdd Specifies data to be added into the query snapshot as of now. Each entry maps
* to a document, with the key being the document id, and the value being the document
* contents.
* @param isFromCache Whether the query snapshot is cache result.
* @return A query snapshot that consists of both sets of documents.
*/
public static QuerySnapshot querySnapshot(
String path,
Map<String, ObjectValue> oldDocs,
Map<String, ObjectValue> docsToAdd,
boolean hasPendingWrites,
boolean isFromCache) {
DocumentSet oldDocuments = docSet(Document.keyComparator());
ImmutableSortedSet<DocumentKey> mutatedKeys = DocumentKey.emptyKeySet();
for (Map.Entry<String, ObjectValue> pair : oldDocs.entrySet()) {
String docKey = path + "/" + pair.getKey();
oldDocuments =
oldDocuments.add(
doc(
docKey,
1L,
pair.getValue(),
hasPendingWrites
? Document.DocumentState.SYNCED
: Document.DocumentState.LOCAL_MUTATIONS));

if (hasPendingWrites) {
mutatedKeys = mutatedKeys.insert(key(docKey));
}
}
DocumentSet newDocuments = docSet(Document.keyComparator());
List<DocumentViewChange> documentChanges = new ArrayList<>();
for (Map.Entry<String, ObjectValue> pair : docsToAdd.entrySet()) {
String docKey = path + "/" + pair.getKey();
Document docToAdd =
doc(
docKey,
1L,
pair.getValue(),
hasPendingWrites
? Document.DocumentState.SYNCED
: Document.DocumentState.LOCAL_MUTATIONS);
newDocuments = newDocuments.add(docToAdd);
documentChanges.add(DocumentViewChange.create(Type.ADDED, docToAdd));

if (hasPendingWrites) {
mutatedKeys = mutatedKeys.insert(key(docKey));
}
}
ViewSnapshot viewSnapshot =
new ViewSnapshot(
com.google.firebase.firestore.testutil.TestUtil.query(path),
newDocuments,
oldDocuments,
documentChanges,
isFromCache,
mutatedKeys,
true,
/* excludesMetadataChanges= */ false);
return new QuerySnapshot(query(path), viewSnapshot, FIRESTORE);
}

/**
* An implementation of TargetMetadataProvider that provides controlled access to the
* `TargetMetadataProvider` callbacks. Any target accessed via these callbacks must be registered
* beforehand via `setSyncedKeys()`.
*/
public static class TestTargetMetadataProvider
implements WatchChangeAggregator.TargetMetadataProvider {
final Map<Integer, ImmutableSortedSet<DocumentKey>> syncedKeys = new HashMap<>();
final Map<Integer, QueryData> queryData = new HashMap<>();

@Override
public ImmutableSortedSet<DocumentKey> getRemoteKeysForTarget(int targetId) {
return syncedKeys.get(targetId) != null
? syncedKeys.get(targetId)
: DocumentKey.emptyKeySet();
}

@Nullable
@Override
public QueryData getQueryDataForTarget(int targetId) {
return queryData.get(targetId);
}

/** Sets or replaces the local state for the provided query data. */
public void setSyncedKeys(QueryData queryData, ImmutableSortedSet<DocumentKey> keys) {
this.queryData.put(queryData.getTargetId(), queryData);
this.syncedKeys.put(queryData.getTargetId(), keys);
}
}

public static <T> T waitFor(Task<T> task) {
if (!task.isComplete()) {
Robolectric.flushBackgroundThreadScheduler();
}
Assert.assertTrue(
"Expected task to be completed after background thread flush", task.isComplete());
return task.getResult();
}
}
Loading