|
14 | 14 |
|
15 | 15 | package com.google.firebase.firestore;
|
16 | 16 |
|
| 17 | +import static com.google.common.truth.Truth.assertThat; |
| 18 | +import static com.google.firebase.firestore.testutil.Assert.assertThrows; |
17 | 19 | import static com.google.firebase.firestore.testutil.IntegrationTestUtil.testAlternateFirestore;
|
18 | 20 | import static com.google.firebase.firestore.testutil.IntegrationTestUtil.testCollection;
|
19 | 21 | import static com.google.firebase.firestore.testutil.IntegrationTestUtil.testCollectionWithDocs;
|
|
30 | 32 |
|
31 | 33 | import android.support.test.InstrumentationRegistry;
|
32 | 34 | import android.support.test.runner.AndroidJUnit4;
|
| 35 | +import com.google.android.gms.tasks.TaskCompletionSource; |
33 | 36 | import com.google.firebase.FirebaseApp;
|
34 | 37 | import com.google.firebase.FirebaseOptions;
|
35 | 38 | import com.google.firebase.firestore.Transaction.Function;
|
@@ -438,6 +441,56 @@ public void queriesCannotBeCreatedFromDocumentsMissingSortValues() {
|
438 | 441 | expectError(() -> query.endAt(snapshot), reason);
|
439 | 442 | }
|
440 | 443 |
|
| 444 | + @Test |
| 445 | + public void queriesCannotBeSortedByAnUncommittedServerTimestamp() { |
| 446 | + CollectionReference collection = testCollection(); |
| 447 | + |
| 448 | + // Ensure the server timestamp stays uncommitted for the first half of the test |
| 449 | + waitFor(collection.firestore.getClient().disableNetwork()); |
| 450 | + |
| 451 | + TaskCompletionSource<Void> offlineCallbackDone = new TaskCompletionSource<>(); |
| 452 | + TaskCompletionSource<Void> onlineCallbackDone = new TaskCompletionSource<>(); |
| 453 | + |
| 454 | + collection.addSnapshotListener( |
| 455 | + (snapshot, error) -> { |
| 456 | + assertNotNull(snapshot); |
| 457 | + |
| 458 | + // Skip the initial empty snapshot. |
| 459 | + if (snapshot.isEmpty()) return; |
| 460 | + |
| 461 | + assertThat(snapshot.getDocuments()).hasSize(1); |
| 462 | + DocumentSnapshot docSnap = snapshot.getDocuments().get(0); |
| 463 | + |
| 464 | + if (snapshot.getMetadata().hasPendingWrites()) { |
| 465 | + // Offline snapshot. Since the server timestamp is uncommitted, we shouldn't be able to |
| 466 | + // query by it. |
| 467 | + assertThrows( |
| 468 | + IllegalArgumentException.class, |
| 469 | + () -> |
| 470 | + collection |
| 471 | + .orderBy("timestamp") |
| 472 | + .endAt(docSnap) |
| 473 | + .addSnapshotListener((snapshot2, error2) -> {})); |
| 474 | + offlineCallbackDone.setResult(null); |
| 475 | + } else { |
| 476 | + // Online snapshot. Since the server timestamp is committed, we should be able to query |
| 477 | + // by it. |
| 478 | + collection |
| 479 | + .orderBy("timestamp") |
| 480 | + .endAt(docSnap) |
| 481 | + .addSnapshotListener((snapshot2, error2) -> {}); |
| 482 | + onlineCallbackDone.setResult(null); |
| 483 | + } |
| 484 | + }); |
| 485 | + |
| 486 | + DocumentReference document = collection.document(); |
| 487 | + document.set(map("timestamp", FieldValue.serverTimestamp())); |
| 488 | + waitFor(offlineCallbackDone.getTask()); |
| 489 | + |
| 490 | + waitFor(collection.firestore.getClient().enableNetwork()); |
| 491 | + waitFor(onlineCallbackDone.getTask()); |
| 492 | + } |
| 493 | + |
441 | 494 | @Test
|
442 | 495 | public void queriesMustNotHaveMoreComponentsThanOrderBy() {
|
443 | 496 | CollectionReference collection = testCollection();
|
|
0 commit comments