use of com.google.firebase.firestore.core.EventManager.ListenOptions in project firebase-android-sdk by firebase.
the class QueryListenerTest method testRaisesDocumentMetadataEventsOnlyWhenSpecified.
@Test
public void testRaisesDocumentMetadataEventsOnlyWhenSpecified() {
List<ViewSnapshot> filteredAccum = new ArrayList<>();
List<ViewSnapshot> fullAccum = new ArrayList<>();
Query query = Query.atPath(path("rooms"));
MutableDocument doc1 = doc("rooms/eros", 1, map("name", "eros"));
MutableDocument doc1Prime = doc("rooms/eros", 1, map("name", "eros")).setHasLocalMutations();
MutableDocument doc2 = doc("rooms/hades", 2, map("name", "hades"));
MutableDocument doc3 = doc("rooms/other", 3, map("name", "other"));
ListenOptions options1 = new ListenOptions();
ListenOptions options2 = new ListenOptions();
options2.includeDocumentMetadataChanges = true;
QueryListener filteredListener = queryListener(query, options1, filteredAccum);
QueryListener fullListener = queryListener(query, options2, fullAccum);
View view = new View(query, DocumentKey.emptyKeySet());
ViewSnapshot snap1 = applyChanges(view, doc1, doc2);
ViewSnapshot snap2 = applyChanges(view, doc1Prime);
ViewSnapshot snap3 = applyChanges(view, doc3);
filteredListener.onViewSnapshot(snap1);
filteredListener.onViewSnapshot(snap2);
filteredListener.onViewSnapshot(snap3);
fullListener.onViewSnapshot(snap1);
fullListener.onViewSnapshot(snap2);
fullListener.onViewSnapshot(snap3);
assertEquals(asList(applyExpectedMetadata(snap1, MetadataChanges.EXCLUDE), applyExpectedMetadata(snap3, MetadataChanges.EXCLUDE)), filteredAccum);
// Second listener should receive doc1prime as added document not modified
assertEquals(asList(snap1, snap2, snap3), fullAccum);
}
use of com.google.firebase.firestore.core.EventManager.ListenOptions in project firebase-android-sdk by firebase.
the class QueryListenerTest method testMetadataOnlyDocumentChangesAreFilteredOut.
@Test
public void testMetadataOnlyDocumentChangesAreFilteredOut() {
List<ViewSnapshot> filteredAccum = new ArrayList<>();
Query query = Query.atPath(path("rooms"));
MutableDocument doc1 = doc("rooms/eros", 1, map("name", "eros"));
MutableDocument doc1Prime = doc("rooms/eros", 1, map("name", "eros")).setHasLocalMutations();
MutableDocument doc2 = doc("rooms/hades", 2, map("name", "hades"));
MutableDocument doc3 = doc("rooms/other", 3, map("name", "other"));
ListenOptions options = new ListenOptions();
options.includeDocumentMetadataChanges = false;
QueryListener filteredListener = queryListener(query, options, filteredAccum);
View view = new View(query, DocumentKey.emptyKeySet());
ViewSnapshot snap1 = applyChanges(view, doc1, doc2);
ViewSnapshot snap2 = applyChanges(view, doc1Prime, doc3);
filteredListener.onViewSnapshot(snap1);
filteredListener.onViewSnapshot(snap2);
DocumentViewChange change3 = DocumentViewChange.create(Type.ADDED, doc3);
ViewSnapshot expectedSnapshot2 = new ViewSnapshot(snap2.getQuery(), snap2.getDocuments(), snap1.getDocuments(), asList(change3), snap2.isFromCache(), snap2.getMutatedKeys(), snap2.didSyncStateChange(), /* excludesMetadataChanges= */
true);
assertEquals(asList(applyExpectedMetadata(snap1, MetadataChanges.EXCLUDE), expectedSnapshot2), filteredAccum);
}
use of com.google.firebase.firestore.core.EventManager.ListenOptions in project firebase-android-sdk by firebase.
the class QueryListenerTest method testRaisesErrorEvent.
@Test
public void testRaisesErrorEvent() {
Query query = Query.atPath(path("rooms/eros"));
AtomicBoolean hadEvent = new AtomicBoolean(false);
QueryListener listener = new QueryListener(query, new ListenOptions(), (value, error) -> {
assertNull(value);
assertNotNull(error);
hadEvent.set(true);
});
Status status = Status.ALREADY_EXISTS.withDescription("test error");
FirebaseFirestoreException error = Util.exceptionFromStatus(status);
listener.onError(error);
assertTrue(hadEvent.get());
}
use of com.google.firebase.firestore.core.EventManager.ListenOptions in project firebase-android-sdk by firebase.
the class QueryListenerTest method testRaisesQueryMetadataEventsOnlyWhenHasPendingWritesOnTheQueryChanges.
@Test
public void testRaisesQueryMetadataEventsOnlyWhenHasPendingWritesOnTheQueryChanges() {
List<ViewSnapshot> fullAccum = new ArrayList<>();
Query query = Query.atPath(path("rooms"));
MutableDocument doc1 = doc("rooms/eros", 1, map("name", "eros")).setHasLocalMutations();
MutableDocument doc2 = doc("rooms/hades", 2, map("name", "hades")).setHasLocalMutations();
MutableDocument doc1Prime = doc("rooms/eros", 1, map("name", "eros"));
MutableDocument doc2Prime = doc("rooms/hades", 2, map("name", "hades"));
MutableDocument doc3 = doc("rooms/other", 3, map("name", "other"));
ListenOptions options = new ListenOptions();
options.includeQueryMetadataChanges = true;
QueryListener fullListener = queryListener(query, options, fullAccum);
View view = new View(query, DocumentKey.emptyKeySet());
ViewSnapshot snap1 = applyChanges(view, doc1, doc2);
ViewSnapshot snap2 = applyChanges(view, doc1Prime);
ViewSnapshot snap3 = applyChanges(view, doc3);
ViewSnapshot snap4 = applyChanges(view, doc2Prime);
fullListener.onViewSnapshot(snap1);
// Emits no events
fullListener.onViewSnapshot(snap2);
fullListener.onViewSnapshot(snap3);
// Metadata change event
fullListener.onViewSnapshot(snap4);
ViewSnapshot expectedSnapshot4 = new ViewSnapshot(snap4.getQuery(), snap4.getDocuments(), snap3.getDocuments(), asList(), snap4.isFromCache(), snap4.getMutatedKeys(), snap4.didSyncStateChange(), /* excludeMetadataChanges= */
true);
// This test excludes document metadata changes
assertEquals(asList(applyExpectedMetadata(snap1, MetadataChanges.EXCLUDE), applyExpectedMetadata(snap3, MetadataChanges.EXCLUDE), expectedSnapshot4), fullAccum);
}
use of com.google.firebase.firestore.core.EventManager.ListenOptions in project firebase-android-sdk by firebase.
the class QueryListenerTest method testWillWaitForSyncIfOnline.
@Test
public void testWillWaitForSyncIfOnline() {
List<ViewSnapshot> events = new ArrayList<>();
Query query = Query.atPath(path("rooms"));
MutableDocument doc1 = doc("rooms/eros", 1, map("name", "eros"));
MutableDocument doc2 = doc("rooms/hades", 2, map("name", "hades"));
ListenOptions options = new ListenOptions();
options.waitForSyncWhenOnline = true;
QueryListener listener = queryListener(query, options, events);
View view = new View(query, DocumentKey.emptyKeySet());
ViewSnapshot snap1 = applyChanges(view, doc1);
ViewSnapshot snap2 = applyChanges(view, doc2);
DocumentChanges changes = view.computeDocChanges(docUpdates());
ViewSnapshot snap3 = view.applyChanges(changes, ackTarget(doc1, doc2)).getSnapshot();
// no event
listener.onOnlineStateChanged(OnlineState.ONLINE);
// no event
listener.onViewSnapshot(snap1);
// no event
listener.onOnlineStateChanged(OnlineState.UNKNOWN);
// no event
listener.onOnlineStateChanged(OnlineState.ONLINE);
// no event
listener.onViewSnapshot(snap2);
// event because synced
listener.onViewSnapshot(snap3);
DocumentViewChange change1 = DocumentViewChange.create(Type.ADDED, doc1);
DocumentViewChange change2 = DocumentViewChange.create(Type.ADDED, doc2);
ViewSnapshot expectedSnapshot = new ViewSnapshot(snap3.getQuery(), snap3.getDocuments(), DocumentSet.emptySet(snap3.getQuery().comparator()), asList(change1, change2), /* isFromCache= */
false, snap3.getMutatedKeys(), /* didSyncStateChange= */
true, /* excludesMetadataChanges= */
true);
assertEquals(asList(expectedSnapshot), events);
}
Aggregations