use of com.google.firebase.firestore.core.EventManager.ListenOptions in project firebase-android-sdk by firebase.
the class SpecTestCase method doListen.
//
// Methods for doing the steps of the spec test.
//
private void doListen(JSONObject listenSpec) throws Exception {
int expectedId = listenSpec.getInt("targetId");
Query query = parseQuery(listenSpec.getJSONObject("query"));
// TODO: Allow customizing listen options in spec tests
ListenOptions options = new ListenOptions();
options.includeDocumentMetadataChanges = true;
options.includeQueryMetadataChanges = true;
QueryListener listener = new QueryListener(query, options, (value, error) -> {
QueryEvent event = new QueryEvent();
event.query = query;
if (value != null) {
event.view = value;
} else {
event.error = error;
}
events.add(event);
});
queryListeners.put(query, listener);
queue.runSync(() -> {
int actualId = eventManager.addQueryListener(listener);
assertEquals(expectedId, actualId);
});
}
use of com.google.firebase.firestore.core.EventManager.ListenOptions in project firebase-android-sdk by firebase.
the class QueryListenerTest method testWillRaiseInitialEventWhenGoingOfflineAndThereAreNoDocs.
@Test
public void testWillRaiseInitialEventWhenGoingOfflineAndThereAreNoDocs() {
List<ViewSnapshot> events = new ArrayList<>();
Query query = Query.atPath(path("rooms"));
QueryListener listener = queryListener(query, new ListenOptions(), events);
View view = new View(query, DocumentKey.emptyKeySet());
ViewSnapshot snap1 = applyChanges(view);
// no event
listener.onOnlineStateChanged(OnlineState.ONLINE);
// no event
listener.onViewSnapshot(snap1);
// event
listener.onOnlineStateChanged(OnlineState.OFFLINE);
ViewSnapshot expectedSnapshot = new ViewSnapshot(snap1.getQuery(), snap1.getDocuments(), DocumentSet.emptySet(snap1.getQuery().comparator()), asList(), /* isFromCache= */
true, snap1.getMutatedKeys(), /* didSyncStateChange= */
true, /* excludesMetadataChanges= */
true);
assertEquals(asList(expectedSnapshot), events);
}
use of com.google.firebase.firestore.core.EventManager.ListenOptions in project firebase-android-sdk by firebase.
the class QueryListenerTest method testWillRaiseInitialEventWhenStartingOfflineAndThereAreNoDocs.
@Test
public void testWillRaiseInitialEventWhenStartingOfflineAndThereAreNoDocs() {
List<ViewSnapshot> events = new ArrayList<>();
Query query = Query.atPath(path("rooms"));
QueryListener listener = queryListener(query, new ListenOptions(), events);
View view = new View(query, DocumentKey.emptyKeySet());
ViewSnapshot snap1 = applyChanges(view);
listener.onOnlineStateChanged(OnlineState.OFFLINE);
listener.onViewSnapshot(snap1);
ViewSnapshot expectedSnapshot = new ViewSnapshot(snap1.getQuery(), snap1.getDocuments(), DocumentSet.emptySet(snap1.getQuery().comparator()), asList(), /* isFromCache= */
true, snap1.getMutatedKeys(), /* didSyncStateChange= */
true, /* excludesMetadataChanges= */
true);
assertEquals(asList(expectedSnapshot), events);
}
use of com.google.firebase.firestore.core.EventManager.ListenOptions in project firebase-android-sdk by firebase.
the class QueryListenerTest method testDoesNotRaiseEventsForMetadataChangesUnlessSpecified.
@Test
public void testDoesNotRaiseEventsForMetadataChangesUnlessSpecified() {
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 doc2 = doc("rooms/hades", 2, map("name", "hades"));
ListenOptions options1 = new ListenOptions();
ListenOptions options2 = new ListenOptions();
options2.includeQueryMetadataChanges = true;
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);
TargetChange ackTarget = ackTarget(doc1);
ViewSnapshot snap2 = view.applyChanges(view.computeDocChanges(docUpdates()), ackTarget).getSnapshot();
ViewSnapshot snap3 = applyChanges(view, doc2);
// local event
filteredListener.onViewSnapshot(snap1);
// no event
filteredListener.onViewSnapshot(snap2);
// doc2 update
filteredListener.onViewSnapshot(snap3);
// local event
fullListener.onViewSnapshot(snap1);
// no event
fullListener.onViewSnapshot(snap2);
// doc2 update
fullListener.onViewSnapshot(snap3);
assertEquals(asList(applyExpectedMetadata(snap1, MetadataChanges.EXCLUDE), applyExpectedMetadata(snap3, MetadataChanges.EXCLUDE)), filteredAccum);
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 testWillRaiseInitialEventWhenGoingOffline.
@Test
public void testWillRaiseInitialEventWhenGoingOffline() {
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);
// no event
listener.onOnlineStateChanged(OnlineState.ONLINE);
// no event
listener.onViewSnapshot(snap1);
// event
listener.onOnlineStateChanged(OnlineState.OFFLINE);
// event
listener.onOnlineStateChanged(OnlineState.ONLINE);
// no event
listener.onOnlineStateChanged(OnlineState.OFFLINE);
// event
listener.onViewSnapshot(snap2);
DocumentViewChange change1 = DocumentViewChange.create(Type.ADDED, doc1);
DocumentViewChange change2 = DocumentViewChange.create(Type.ADDED, doc2);
ViewSnapshot expectedSnapshot1 = new ViewSnapshot(snap1.getQuery(), snap1.getDocuments(), DocumentSet.emptySet(snap1.getQuery().comparator()), asList(change1), /* isFromCache= */
true, snap1.getMutatedKeys(), /* didSyncStateChange= */
true, /* excludesMetadataChanges= */
true);
ViewSnapshot expectedSnapshot2 = new ViewSnapshot(snap2.getQuery(), snap2.getDocuments(), snap1.getDocuments(), asList(change2), /* isFromCache= */
true, snap2.getMutatedKeys(), /* didSyncStateChange= */
false, /* excludesMetadataChanges= */
true);
assertEquals(asList(expectedSnapshot1, expectedSnapshot2), events);
}
Aggregations