|
| 1 | +// Copyright 2019 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.firebase.firestore; |
| 16 | + |
| 17 | +import static com.google.firebase.firestore.testutil.TestUtil.doc; |
| 18 | +import static com.google.firebase.firestore.testutil.TestUtil.docSet; |
| 19 | +import static com.google.firebase.firestore.testutil.TestUtil.key; |
| 20 | +import static org.mockito.Mockito.mock; |
| 21 | + |
| 22 | +import androidx.annotation.Nullable; |
| 23 | +import com.google.android.gms.tasks.Task; |
| 24 | +import com.google.firebase.database.collection.ImmutableSortedSet; |
| 25 | +import com.google.firebase.firestore.core.DocumentViewChange; |
| 26 | +import com.google.firebase.firestore.core.DocumentViewChange.Type; |
| 27 | +import com.google.firebase.firestore.core.ViewSnapshot; |
| 28 | +import com.google.firebase.firestore.local.QueryData; |
| 29 | +import com.google.firebase.firestore.model.Document; |
| 30 | +import com.google.firebase.firestore.model.DocumentKey; |
| 31 | +import com.google.firebase.firestore.model.DocumentSet; |
| 32 | +import com.google.firebase.firestore.model.ResourcePath; |
| 33 | +import com.google.firebase.firestore.model.value.ObjectValue; |
| 34 | +import com.google.firebase.firestore.remote.WatchChangeAggregator; |
| 35 | +import java.util.ArrayList; |
| 36 | +import java.util.HashMap; |
| 37 | +import java.util.List; |
| 38 | +import java.util.Map; |
| 39 | +import org.junit.Assert; |
| 40 | +import org.robolectric.Robolectric; |
| 41 | + |
| 42 | +public class TestUtil { |
| 43 | + |
| 44 | + private static final FirebaseFirestore FIRESTORE = mock(FirebaseFirestore.class); |
| 45 | + |
| 46 | + public static FirebaseFirestore firestore() { |
| 47 | + return FIRESTORE; |
| 48 | + } |
| 49 | + |
| 50 | + public static CollectionReference collectionReference(String path) { |
| 51 | + return new CollectionReference(ResourcePath.fromString(path), FIRESTORE); |
| 52 | + } |
| 53 | + |
| 54 | + public static DocumentReference documentReference(String path) { |
| 55 | + return new DocumentReference(key(path), FIRESTORE); |
| 56 | + } |
| 57 | + |
| 58 | + public static DocumentSnapshot documentSnapshot( |
| 59 | + String path, Map<String, Object> data, boolean isFromCache) { |
| 60 | + if (data == null) { |
| 61 | + return DocumentSnapshot.fromNoDocument( |
| 62 | + FIRESTORE, key(path), isFromCache, /*hasPendingWrites=*/ false); |
| 63 | + } else { |
| 64 | + return DocumentSnapshot.fromDocument( |
| 65 | + FIRESTORE, doc(path, 1L, data), isFromCache, /*hasPendingWrites=*/ false); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public static Query query(String path) { |
| 70 | + return new Query(com.google.firebase.firestore.testutil.TestUtil.query(path), FIRESTORE); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * A convenience method for creating a particular query snapshot for tests. |
| 75 | + * |
| 76 | + * @param path To be used in constructing the query. |
| 77 | + * @param oldDocs Provides the prior set of documents in the QuerySnapshot. Each entry maps to a |
| 78 | + * document, with the key being the document id, and the value being the document contents. |
| 79 | + * @param docsToAdd Specifies data to be added into the query snapshot as of now. Each entry maps |
| 80 | + * to a document, with the key being the document id, and the value being the document |
| 81 | + * contents. |
| 82 | + * @param isFromCache Whether the query snapshot is cache result. |
| 83 | + * @return A query snapshot that consists of both sets of documents. |
| 84 | + */ |
| 85 | + public static QuerySnapshot querySnapshot( |
| 86 | + String path, |
| 87 | + Map<String, ObjectValue> oldDocs, |
| 88 | + Map<String, ObjectValue> docsToAdd, |
| 89 | + boolean hasPendingWrites, |
| 90 | + boolean isFromCache) { |
| 91 | + DocumentSet oldDocuments = docSet(Document.keyComparator()); |
| 92 | + ImmutableSortedSet<DocumentKey> mutatedKeys = DocumentKey.emptyKeySet(); |
| 93 | + for (Map.Entry<String, ObjectValue> pair : oldDocs.entrySet()) { |
| 94 | + String docKey = path + "/" + pair.getKey(); |
| 95 | + oldDocuments = |
| 96 | + oldDocuments.add( |
| 97 | + doc( |
| 98 | + docKey, |
| 99 | + 1L, |
| 100 | + pair.getValue(), |
| 101 | + hasPendingWrites |
| 102 | + ? Document.DocumentState.SYNCED |
| 103 | + : Document.DocumentState.LOCAL_MUTATIONS)); |
| 104 | + |
| 105 | + if (hasPendingWrites) { |
| 106 | + mutatedKeys = mutatedKeys.insert(key(docKey)); |
| 107 | + } |
| 108 | + } |
| 109 | + DocumentSet newDocuments = docSet(Document.keyComparator()); |
| 110 | + List<DocumentViewChange> documentChanges = new ArrayList<>(); |
| 111 | + for (Map.Entry<String, ObjectValue> pair : docsToAdd.entrySet()) { |
| 112 | + String docKey = path + "/" + pair.getKey(); |
| 113 | + Document docToAdd = |
| 114 | + doc( |
| 115 | + docKey, |
| 116 | + 1L, |
| 117 | + pair.getValue(), |
| 118 | + hasPendingWrites |
| 119 | + ? Document.DocumentState.SYNCED |
| 120 | + : Document.DocumentState.LOCAL_MUTATIONS); |
| 121 | + newDocuments = newDocuments.add(docToAdd); |
| 122 | + documentChanges.add(DocumentViewChange.create(Type.ADDED, docToAdd)); |
| 123 | + |
| 124 | + if (hasPendingWrites) { |
| 125 | + mutatedKeys = mutatedKeys.insert(key(docKey)); |
| 126 | + } |
| 127 | + } |
| 128 | + ViewSnapshot viewSnapshot = |
| 129 | + new ViewSnapshot( |
| 130 | + com.google.firebase.firestore.testutil.TestUtil.query(path), |
| 131 | + newDocuments, |
| 132 | + oldDocuments, |
| 133 | + documentChanges, |
| 134 | + isFromCache, |
| 135 | + mutatedKeys, |
| 136 | + true, |
| 137 | + /* excludesMetadataChanges= */ false); |
| 138 | + return new QuerySnapshot(query(path), viewSnapshot, FIRESTORE); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * An implementation of TargetMetadataProvider that provides controlled access to the |
| 143 | + * `TargetMetadataProvider` callbacks. Any target accessed via these callbacks must be registered |
| 144 | + * beforehand via `setSyncedKeys()`. |
| 145 | + */ |
| 146 | + public static class TestTargetMetadataProvider |
| 147 | + implements WatchChangeAggregator.TargetMetadataProvider { |
| 148 | + final Map<Integer, ImmutableSortedSet<DocumentKey>> syncedKeys = new HashMap<>(); |
| 149 | + final Map<Integer, QueryData> queryData = new HashMap<>(); |
| 150 | + |
| 151 | + @Override |
| 152 | + public ImmutableSortedSet<DocumentKey> getRemoteKeysForTarget(int targetId) { |
| 153 | + return syncedKeys.get(targetId) != null |
| 154 | + ? syncedKeys.get(targetId) |
| 155 | + : DocumentKey.emptyKeySet(); |
| 156 | + } |
| 157 | + |
| 158 | + @Nullable |
| 159 | + @Override |
| 160 | + public QueryData getQueryDataForTarget(int targetId) { |
| 161 | + return queryData.get(targetId); |
| 162 | + } |
| 163 | + |
| 164 | + /** Sets or replaces the local state for the provided query data. */ |
| 165 | + public void setSyncedKeys(QueryData queryData, ImmutableSortedSet<DocumentKey> keys) { |
| 166 | + this.queryData.put(queryData.getTargetId(), queryData); |
| 167 | + this.syncedKeys.put(queryData.getTargetId(), keys); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + public static <T> T waitFor(Task<T> task) { |
| 172 | + if (!task.isComplete()) { |
| 173 | + Robolectric.flushBackgroundThreadScheduler(); |
| 174 | + } |
| 175 | + Assert.assertTrue( |
| 176 | + "Expected task to be completed after background thread flush", task.isComplete()); |
| 177 | + return task.getResult(); |
| 178 | + } |
| 179 | +} |
0 commit comments