diff --git a/firebase-firestore/src/androidTest/java/com/google/firebase/firestore/FirestoreTest.java b/firebase-firestore/src/androidTest/java/com/google/firebase/firestore/FirestoreTest.java index f975b637789..bf48464a4b2 100644 --- a/firebase-firestore/src/androidTest/java/com/google/firebase/firestore/FirestoreTest.java +++ b/firebase-firestore/src/androidTest/java/com/google/firebase/firestore/FirestoreTest.java @@ -1653,4 +1653,56 @@ public void sdkOrdersQueryByDocumentIdTheSameWayOnlineAndOffline() { // Run query with snapshot listener checkOnlineAndOfflineResultsMatch(orderedQuery, expectedDocIds.toArray(new String[0])); } + + @Test + public void snapshotListenerSortsNumbersSameWayAsServer() { + Map> testDocs = + map( + "intMin", + map("value", Long.MIN_VALUE), + "doubleMin", + map("value", ((double) Long.MIN_VALUE) - 100), + "intMax", + map("value", Long.MAX_VALUE), + "doubleMax", + map("value", ((double) Long.MAX_VALUE) + 100), + "NAN", + map("value", Double.NaN), + "integerMax", + map("value", (long) Integer.MAX_VALUE), + "integerMin", + map("value", (long) Integer.MIN_VALUE), + "negativeInfinity", + map("value", Double.NEGATIVE_INFINITY), + "positiveInfinity", + map("value", Double.POSITIVE_INFINITY)); + + CollectionReference colRef = testCollectionWithDocs(testDocs); + + // Run get query + Query filteredQuery = colRef.orderBy("value"); + + QuerySnapshot getSnapshot = waitFor(filteredQuery.get()); + List getSnapshotDocIds = + getSnapshot.getDocuments().stream().map(ds -> ds.getId()).collect(Collectors.toList()); + + // Run query with snapshot listener + EventAccumulator eventAccumulator = new EventAccumulator(); + ListenerRegistration registration = + filteredQuery.addSnapshotListener(eventAccumulator.listener()); + + List watchSnapshotDocIds = new ArrayList<>(); + try { + QuerySnapshot watchSnapshot = eventAccumulator.await(); + watchSnapshotDocIds = + watchSnapshot.getDocuments().stream() + .map(documentSnapshot -> documentSnapshot.getId()) + .collect(Collectors.toList()); + } finally { + registration.remove(); + } + + // Assert that get and snapshot listener requests sort docs in the same, expected order + assertTrue(getSnapshotDocIds.equals(watchSnapshotDocIds)); + } }