Search in sources :

Example 1 with DocumentChanges

use of com.google.firebase.firestore.core.View.DocumentChanges 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)1 DocumentChanges (com.google.firebase.firestore.core.View.DocumentChanges)1 MutableDocument (com.google.firebase.firestore.model.MutableDocument)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1