Search in sources :

Example 11 with ListenOptions

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);
}
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)

Example 12 with ListenOptions

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);
}
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)

Example 13 with ListenOptions

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());
}
Also used : Status(io.grpc.Status) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ListenOptions(com.google.firebase.firestore.core.EventManager.ListenOptions) FirebaseFirestoreException(com.google.firebase.firestore.FirebaseFirestoreException) Test(org.junit.Test)

Example 14 with ListenOptions

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);
}
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)

Example 15 with ListenOptions

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