Search in sources :

Example 1 with ListenOptions

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);
    });
}
Also used : ListenOptions(com.google.firebase.firestore.core.EventManager.ListenOptions) Query(com.google.firebase.firestore.core.Query) QueryListener(com.google.firebase.firestore.core.QueryListener)

Example 2 with ListenOptions

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);
}
Also used : ListenOptions(com.google.firebase.firestore.core.EventManager.ListenOptions) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 3 with ListenOptions

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);
}
Also used : ListenOptions(com.google.firebase.firestore.core.EventManager.ListenOptions) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 4 with ListenOptions

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);
}
Also used : ListenOptions(com.google.firebase.firestore.core.EventManager.ListenOptions) TargetChange(com.google.firebase.firestore.remote.TargetChange) ArrayList(java.util.ArrayList) MutableDocument(com.google.firebase.firestore.model.MutableDocument) Test(org.junit.Test)

Example 5 with ListenOptions

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);
}
Also used : ListenOptions(com.google.firebase.firestore.core.EventManager.ListenOptions) ArrayList(java.util.ArrayList) MutableDocument(com.google.firebase.firestore.model.MutableDocument) Test(org.junit.Test)

Aggregations

ListenOptions (com.google.firebase.firestore.core.EventManager.ListenOptions)16 Test (org.junit.Test)9 ArrayList (java.util.ArrayList)8 MutableDocument (com.google.firebase.firestore.model.MutableDocument)6 TaskCompletionSource (com.google.android.gms.tasks.TaskCompletionSource)3 ExecutionException (java.util.concurrent.ExecutionException)3 NonNull (androidx.annotation.NonNull)2 QueryListener (com.google.firebase.firestore.core.QueryListener)2 Activity (android.app.Activity)1 Nullable (androidx.annotation.Nullable)1 Task (com.google.android.gms.tasks.Task)1 Tasks (com.google.android.gms.tasks.Tasks)1 FirebaseFirestoreException (com.google.firebase.firestore.FirebaseFirestoreException)1 Code (com.google.firebase.firestore.FirebaseFirestoreException.Code)1 ActivityScope (com.google.firebase.firestore.core.ActivityScope)1 AsyncEventListener (com.google.firebase.firestore.core.AsyncEventListener)1 ListenerRegistrationImpl (com.google.firebase.firestore.core.ListenerRegistrationImpl)1 Query (com.google.firebase.firestore.core.Query)1 ParsedSetData (com.google.firebase.firestore.core.UserData.ParsedSetData)1 ParsedUpdateData (com.google.firebase.firestore.core.UserData.ParsedUpdateData)1