package com.couchbase.lite;

import com.couchbase.lite.internal.Body;
import com.couchbase.lite.internal.RevisionInternal;

import java.util.HashMap;
import java.util.Map;

public class ChangesTest extends LiteTestCase {

    private int changeNotifications = 0;

    public void testChangeNotification() throws CouchbaseLiteException {

        Database.ChangeListener changeListener = new Database.ChangeListener() {
            @Override
            public void changed(Database.ChangeEvent event) {
                changeNotifications++;
            }
        };

        // add listener to database
        database.addChangeListener(changeListener);

        // create a document
        Map<String, Object> documentProperties = new HashMap<String, Object>();
        documentProperties.put("foo", 1);
        documentProperties.put("bar", false);
        documentProperties.put("baz", "touch");

        Body body = new Body(documentProperties);
        RevisionInternal rev1 = new RevisionInternal(body, database);

        Status status = new Status();
        rev1 = database.putRevision(rev1, null, false, status);

        assertEquals(1, changeNotifications);
    }

}

@@ -117,16 +117,41 @@
 ernal;%0A%0A
+import java.util.Arrays;%0A
 import j
@@ -1198,10 +1198,1702 @@
 %0A    %7D%0A%0A
+    public void testLocalChangesAreNotExternal() throws CouchbaseLiteException %7B%0A        changeNotifications = 0;%0A        Database.ChangeListener changeListener = new Database.ChangeListener() %7B%0A            @Override public void changed(Database.ChangeEvent event) %7B%0A                changeNotifications++;%0A                assertFalse(event.isExternal());%0A            %7D%0A        %7D;%0A        database.addChangeListener(changeListener);%0A%0A        // Insert a document locally.%0A        Document document = database.createDocument();%0A        document.createRevision().save();%0A%0A        // Make sure that the assertion in changeListener was called.%0A        assertEquals(1, changeNotifications);%0A    %7D%0A%0A    public void testPulledChangesAreExternal() throws CouchbaseLiteException %7B%0A        changeNotifications = 0;%0A        Database.ChangeListener changeListener = new Database.ChangeListener() %7B%0A            @Override public void changed(Database.ChangeEvent event) %7B%0A                changeNotifications++;%0A                assertTrue(event.isExternal());%0A            %7D%0A        %7D;%0A        database.addChangeListener(changeListener);%0A%0A        // Insert a document as if it came from a remote source.%0A        RevisionInternal rev = new RevisionInternal(%22docId%22, %221-rev%22, false, database);%0A        Map%3CString, Object%3E properties = new HashMap%3CString, Object%3E();%0A        properties.put(%22_id%22, rev.getDocId());%0A        properties.put(%22_rev%22, rev.getRevId());%0A        rev.setProperties(properties);%0A        database.forceInsert(rev, Arrays.asList(rev.getRevId()), getReplicationURL());%0A%0A        // Make sure that the assertion in changeListener was called.%0A        assertEquals(1, changeNotifications);%0A    %7D%0A%0A
 %7D%0A

add failing test for DocumentChange.isExternal() flag