|
| 1 | +// Copyright 2024 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 | +// |
| 6 | +// You may obtain a copy of the License at |
| 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 org.mockito.ArgumentMatchers.eq; |
| 18 | +import static org.mockito.Mockito.mock; |
| 19 | +import static org.mockito.Mockito.when; |
| 20 | + |
| 21 | +import androidx.test.core.app.ApplicationProvider; |
| 22 | + |
| 23 | +import com.google.android.gms.tasks.Task; |
| 24 | +import com.google.android.gms.tasks.TaskCompletionSource; |
| 25 | +import com.google.android.gms.tasks.Tasks; |
| 26 | +import com.google.firebase.firestore.core.ComponentProvider; |
| 27 | +import com.google.firebase.firestore.integration.AsyncTaskAccumulator; |
| 28 | +import com.google.firebase.firestore.model.DatabaseId; |
| 29 | +import com.google.firebase.firestore.remote.GrpcCallProvider; |
| 30 | +import com.google.firebase.firestore.remote.RemoteComponenetProvider; |
| 31 | +import com.google.firebase.firestore.testutil.EmptyAppCheckTokenProvider; |
| 32 | +import com.google.firebase.firestore.testutil.EmptyCredentialsProvider; |
| 33 | +import com.google.firebase.firestore.util.AsyncQueue; |
| 34 | +import com.google.firestore.v1.FirestoreGrpc; |
| 35 | +import com.google.firestore.v1.ListenRequest; |
| 36 | +import com.google.firestore.v1.ListenResponse; |
| 37 | +import com.google.firestore.v1.WriteRequest; |
| 38 | +import com.google.firestore.v1.WriteResponse; |
| 39 | + |
| 40 | +import com.google.firebase.firestore.integration.TestClientCall; |
| 41 | + |
| 42 | +/** |
| 43 | + * Factory for producing FirebaseFirestore instances that has mocked gRPC layer. |
| 44 | + * |
| 45 | + * <ol> |
| 46 | + * <li>Response protos from server can be faked. |
| 47 | + * <li>Request protos from SDK can be verified. |
| 48 | + * </ol> |
| 49 | + * |
| 50 | + * <p> |
| 51 | + * The FirebaseFirestoreIntegrationTestFactory is located in this package to gain package-private |
| 52 | + * access to FirebaseFirestore methods. |
| 53 | + */ |
| 54 | +public final class FirebaseFirestoreIntegrationTestFactory { |
| 55 | + |
| 56 | + /** |
| 57 | + * Everytime the `componentProviderFactory` on FirebaseFirestore is run, a new instance is added. |
| 58 | + */ |
| 59 | + public final AsyncTaskAccumulator<Instance> instances = new AsyncTaskAccumulator<>(); |
| 60 | + |
| 61 | + /** |
| 62 | + * Instance of Firestore components. |
| 63 | + */ |
| 64 | + public static class Instance { |
| 65 | + |
| 66 | + /** Instance of ComponentProvider */ |
| 67 | + public final ComponentProvider componentProvider; |
| 68 | + |
| 69 | + /** Every listen stream created is captured here. */ |
| 70 | + private final AsyncTaskAccumulator<TestClientCall<ListenRequest, ListenResponse>> listens = new AsyncTaskAccumulator<>(); |
| 71 | + |
| 72 | + /** Every write stream created is captured here. */ |
| 73 | + private final AsyncTaskAccumulator<TestClientCall<WriteRequest, WriteResponse>> writes = new AsyncTaskAccumulator<>(); |
| 74 | + |
| 75 | + private Instance(ComponentProvider componentProvider) { |
| 76 | + this.componentProvider = componentProvider; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Queues work on AsyncQueue. This is required when faking responses from server since they |
| 81 | + * must be handled through the AsyncQueue of the FirestoreClient. |
| 82 | + */ |
| 83 | + public Task<Void> enqueue(Runnable runnable) { |
| 84 | + return configuration.asyncQueue.enqueue(runnable); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Configuration passed to `ComponentProvider` |
| 89 | + * |
| 90 | + * <p> |
| 91 | + * This is never null because `Task<Instance>` completes after initialization. The |
| 92 | + * `FirebaseFirestoreIntegrationTestFactory` will set `Instance.configuration` from within |
| 93 | + * the ComponentProvider override. |
| 94 | + */ |
| 95 | + private ComponentProvider.Configuration configuration; |
| 96 | + |
| 97 | + /** Every listen stream created */ |
| 98 | + public Task<TestClientCall<ListenRequest, ListenResponse>> getListenClient(int i) { |
| 99 | + return listens.get(i); |
| 100 | + } |
| 101 | + |
| 102 | + /** Every write stream created */ |
| 103 | + public Task<TestClientCall<WriteRequest, WriteResponse>> getWriteClient(int i) { |
| 104 | + return writes.get(i); |
| 105 | + } |
| 106 | + |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * The FirebaseFirestore instance. |
| 111 | + */ |
| 112 | + public final FirebaseFirestore firestore; |
| 113 | + |
| 114 | + /** |
| 115 | + * Mockito Mock of `FirebaseFirestore.InstanceRegistry` that was passed into FirebaseFirestore |
| 116 | + * constructor. |
| 117 | + */ |
| 118 | + public final FirebaseFirestore.InstanceRegistry instanceRegistry = mock(FirebaseFirestore.InstanceRegistry.class); |
| 119 | + |
| 120 | + public FirebaseFirestoreIntegrationTestFactory(DatabaseId databaseId) { |
| 121 | + firestore = new FirebaseFirestore( |
| 122 | + ApplicationProvider.getApplicationContext(), |
| 123 | + databaseId, |
| 124 | + "k", |
| 125 | + new EmptyCredentialsProvider(), |
| 126 | + new EmptyAppCheckTokenProvider(), |
| 127 | + new AsyncQueue(), |
| 128 | + this::componentProvider, |
| 129 | + null, |
| 130 | + instanceRegistry, |
| 131 | + null |
| 132 | + ); |
| 133 | + } |
| 134 | + |
| 135 | + public void useMemoryCache() { |
| 136 | + FirebaseFirestoreSettings.Builder builder = new FirebaseFirestoreSettings.Builder(firestore.getFirestoreSettings()); |
| 137 | + builder.setLocalCacheSettings(MemoryCacheSettings.newBuilder().build()); |
| 138 | + firestore.setFirestoreSettings(builder.build()); |
| 139 | + } |
| 140 | + |
| 141 | + private GrpcCallProvider mockGrpcCallProvider(Instance instance) { |
| 142 | + GrpcCallProvider mockGrpcCallProvider = mock(GrpcCallProvider.class); |
| 143 | + when(mockGrpcCallProvider.createClientCall(eq(FirestoreGrpc.getListenMethod()))) |
| 144 | + .thenAnswer(invocation -> Tasks.forResult(new TestClientCall<>(instance.listens.next()))); |
| 145 | + when(mockGrpcCallProvider.createClientCall(eq(FirestoreGrpc.getWriteMethod()))) |
| 146 | + .thenAnswer(invocation -> Tasks.forResult(new TestClientCall<>(instance.writes.next()))); |
| 147 | + return mockGrpcCallProvider; |
| 148 | + } |
| 149 | + |
| 150 | + private ComponentProvider componentProvider(FirebaseFirestoreSettings settings) { |
| 151 | + TaskCompletionSource<Instance> next = instances.next(); |
| 152 | + ComponentProvider componentProvider = ComponentProvider.defaultFactory(settings); |
| 153 | + Instance instance = new Instance(componentProvider); |
| 154 | + componentProvider.setRemoteProvider(new RemoteComponenetProvider() { |
| 155 | + @Override |
| 156 | + protected GrpcCallProvider createGrpcCallProvider(ComponentProvider.Configuration configuration) { |
| 157 | + instance.configuration = configuration; |
| 158 | + next.setResult(instance); |
| 159 | + return mockGrpcCallProvider(instance); |
| 160 | + } |
| 161 | + }); |
| 162 | + return componentProvider; |
| 163 | + } |
| 164 | +} |
0 commit comments