Search in sources :

Example 31 with ContactChange

use of com.vodafone360.people.engine.contactsync.ContactChange in project 360-Engine-for-Android by 360.

the class PeopleContactsApiTest method testGetNativeContactsIdsMerge.

/**
     * Tests the getNativeContactsIds() method when a merge has to be performed.
     */
public void testGetNativeContactsIdsMerge() {
    final long[] NATIVE_IDS = { 10, 85, 12, 103, 44, 38 };
    final long[] DELETED_IDS = { NATIVE_IDS[0], NATIVE_IDS[5], NATIVE_IDS[3] };
    PeopleContactsApi pca = new PeopleContactsApi(mDatabaseHelper);
    // the database is empty, it shall not return any ids
    assertNull(pca.getNativeContactsIds());
    // let's add the contacts
    for (int i = 0; i < NATIVE_IDS.length; i++) {
        ContactChange[] contact = filterContactChanges(ContactChangeHelper.randomContact(ContactChange.INVALID_ID, ContactChange.INVALID_ID, NATIVE_IDS[i]));
        assertTrue(pca.addNativeContact(contact));
    }
    // check that they exist and in the right order (ascending)
    long[] ids = pca.getNativeContactsIds();
    assertEquals(NATIVE_IDS.length, ids.length);
    assertTrue(checkExistAndAscendingOrder(NATIVE_IDS, ids));
    // delete them from CAB as it was coming from backend or user
    // this has to be done without PeopleContactsApi wrapper as it does not support
    // operations for Contacts not related to the native address book
    List<ContactIdInfo> ciiList = new ArrayList<ContactIdInfo>(DELETED_IDS.length);
    final SQLiteDatabase readableDb = mDatabaseHelper.getReadableDatabase();
    try {
        final SQLiteStatement nativeToLocalId = ContactsTable.fetchLocalFromNativeIdStatement(readableDb);
        for (int i = 0; i < DELETED_IDS.length; i++) {
            final ContactIdInfo cii = new ContactIdInfo();
            cii.localId = ContactsTable.fetchLocalFromNativeId((int) DELETED_IDS[i], nativeToLocalId);
            cii.nativeId = (int) DELETED_IDS[i];
            ciiList.add(cii);
        }
    } finally {
        readableDb.close();
    }
    mDatabaseHelper.syncDeleteContactList(ciiList, false, true);
    // check the returned list of ids which should be similar as before
    ids = pca.getNativeContactsIds();
    assertEquals(NATIVE_IDS.length, ids.length);
    assertTrue(checkExistAndAscendingOrder(NATIVE_IDS, ids));
    // check that the deleted ids have the deleted flag
    for (int i = 0; i < DELETED_IDS.length; i++) {
        final ContactChange[] cc = pca.getContact(DELETED_IDS[i]);
        assertEquals(cc[0].getType(), ContactChange.TYPE_DELETE_CONTACT);
    }
}
Also used : ContactIdInfo(com.vodafone360.people.database.tables.ContactsTable.ContactIdInfo) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLiteStatement(android.database.sqlite.SQLiteStatement) ArrayList(java.util.ArrayList) PeopleContactsApi(com.vodafone360.people.engine.contactsync.PeopleContactsApi) ContactChange(com.vodafone360.people.engine.contactsync.ContactChange)

Example 32 with ContactChange

use of com.vodafone360.people.engine.contactsync.ContactChange in project 360-Engine-for-Android by 360.

the class PeopleContactsApiTest method checkContactUpdates.

/**
     * Checks that the updated ContactChange array matches the original ContactChange
     * array with the performed ContactChange updates.
     *  
     * @param original the original ContactChange array
     * @param updates the array of ContacChange updates that have been applied
     * @param updatedContact the array of ContactChange representing the original contact after having performed the updates
     * @return true if the new ContactChange reflect the updates, false if not
     */
private void checkContactUpdates(ContactChange[] original, ContactChange[] updates, ContactChange[] updatedContact) {
    int addedCount = 0;
    int deletedCount = 0;
    int updatedCount = 0;
    int index;
    for (int i = 0; i < updates.length; i++) {
        final ContactChange cc = updates[i];
        switch(cc.getType()) {
            case ContactChange.TYPE_ADD_DETAIL:
                index = findContactChangeIndex(updatedContact, cc);
                assertTrue(index != -1);
                addedCount++;
                break;
            case ContactChange.TYPE_UPDATE_DETAIL:
                index = findContactChangeIndex(updatedContact, cc);
                assertTrue(index != -1);
                updatedCount++;
                break;
            case ContactChange.TYPE_DELETE_DETAIL:
                index = findContactChangeIndex(updatedContact, cc);
                assertTrue(index == -1);
                deletedCount++;
                break;
        }
    }
    assertEquals(original.length + addedCount - deletedCount, updatedContact.length);
}
Also used : ContactChange(com.vodafone360.people.engine.contactsync.ContactChange)

Example 33 with ContactChange

use of com.vodafone360.people.engine.contactsync.ContactChange in project 360-Engine-for-Android by 360.

the class PeopleContactsApiTest method testAddNativeContact.

/**
     * Tests the addNativeContact() method.
     */
public void testAddNativeContact() {
    final long NATIVE_ID = 15;
    PeopleContactsApi pca = new PeopleContactsApi(mDatabaseHelper);
    // the database is empty, it shall not return any ids
    assertNull(pca.getNativeContactsIds());
    // let's add a contact
    final ContactChange[] contact = ContactChangeHelper.randomContact(ContactChange.INVALID_ID, ContactChange.INVALID_ID, NATIVE_ID);
    assertTrue(pca.addNativeContact(contact));
    // check that it exists
    final long[] ids = pca.getNativeContactsIds();
    assertEquals(1, ids.length);
    assertEquals(NATIVE_ID, ids[0]);
}
Also used : PeopleContactsApi(com.vodafone360.people.engine.contactsync.PeopleContactsApi) ContactChange(com.vodafone360.people.engine.contactsync.ContactChange)

Example 34 with ContactChange

use of com.vodafone360.people.engine.contactsync.ContactChange in project 360-Engine-for-Android by 360.

the class PeopleContactsApiTest method testAddDeleteNativeContact.

/**
     * Tests the methods sequence addNativeContact() then deleteNativeContact().
     */
public void testAddDeleteNativeContact() {
    final long NATIVE_ID = 15;
    PeopleContactsApi pca = new PeopleContactsApi(mDatabaseHelper);
    // the database is empty, it shall not return any ids
    assertNull(pca.getNativeContactsIds());
    // let's add a contact
    final ContactChange[] contact = ContactChangeHelper.randomContact(ContactChange.INVALID_ID, ContactChange.INVALID_ID, NATIVE_ID);
    assertTrue(pca.addNativeContact(contact));
    // check that it exists
    long[] ids = pca.getNativeContactsIds();
    assertEquals(1, ids.length);
    assertEquals(NATIVE_ID, ids[0]);
    // now let's remove it
    assertTrue(pca.deleteNativeContact(NATIVE_ID, false));
    // check that it no longer exists
    ids = pca.getNativeContactsIds();
    assertNull(ids);
}
Also used : PeopleContactsApi(com.vodafone360.people.engine.contactsync.PeopleContactsApi) ContactChange(com.vodafone360.people.engine.contactsync.ContactChange)

Example 35 with ContactChange

use of com.vodafone360.people.engine.contactsync.ContactChange in project 360-Engine-for-Android by 360.

the class UpdateNativeContactsTest method compareContactWithContactChange.

/**
     * Compares a Contact details with an array of ContactChange.
     * 
     * @param contact the Contact to compare
     * @param contactChange the array of ContactChange to compare
     * @return true if the Contact is equivalent to the array of ContactChange, false otherwise
     */
private boolean compareContactWithContactChange(Contact contact, ContactChange[] contactChanges) {
    final NativeContactsApi nca = NativeContactsApi.getInstance();
    final List<ContactDetail> details = contact.details;
    for (int i = 0; i < details.size(); i++) {
        final ContactDetail detail = details.get(i);
        if (nca.isKeySupported(ContactDetailsTable.mapInternalKeyToContactChangeKey(detail.key.ordinal()))) {
            // check if there is a corresponding ContactChange
            if (!hasEquivalentContactChange(contactChanges, detail)) {
                return false;
            }
        }
    }
    return true;
}
Also used : ContactDetail(com.vodafone360.people.datatypes.ContactDetail) NativeContactsApi(com.vodafone360.people.engine.contactsync.NativeContactsApi)

Aggregations

ContactChange (com.vodafone360.people.engine.contactsync.ContactChange)33 Name (com.vodafone360.people.datatypes.VCardHelper.Name)6 Organisation (com.vodafone360.people.datatypes.VCardHelper.Organisation)6 PeopleContactsApi (com.vodafone360.people.engine.contactsync.PeopleContactsApi)6 Cursor (android.database.Cursor)5 Suppress (android.test.suitebuilder.annotation.Suppress)5 ContactDetail (com.vodafone360.people.datatypes.ContactDetail)5 ArrayList (java.util.ArrayList)5 SQLException (android.database.SQLException)4 SQLiteException (android.database.sqlite.SQLiteException)4 Uri (android.net.Uri)4 PostalAddress (com.vodafone360.people.datatypes.VCardHelper.PostalAddress)4 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)3 Contact (com.vodafone360.people.datatypes.Contact)3 ContentValues (android.content.ContentValues)2 StructuredName (android.provider.ContactsContract.CommonDataKinds.StructuredName)2 MediumTest (android.test.suitebuilder.annotation.MediumTest)2 SmallTest (android.test.suitebuilder.annotation.SmallTest)2 ContactIdInfo (com.vodafone360.people.database.tables.ContactsTable.ContactIdInfo)2 NativeContactsApi (com.vodafone360.people.engine.contactsync.NativeContactsApi)2