public void testTrimInsertInsert() { final ContactsSource source = getSource(); final Sources sources = getSources(source); final DataKind kindPhone = source.getKindForMimetype(Phone.CONTENT_ITEM_TYPE); final EditType typeHome = EntityModifier.getType(kindPhone, Phone.TYPE_HOME); // Try creating a contact with single empty entry final EntityDelta state = getEntity(null); final ValuesDelta values = EntityModifier.insertChild(state, kindPhone, typeHome); final EntitySet set = EntitySet.fromSingle(state); // Build diff, expecting two insert operations final ArrayList<ContentProviderOperation> diff = Lists.newArrayList(); state.buildDiff(diff); assertEquals("Unexpected operations", 3, diff.size()); { final ContentProviderOperation oper = diff.get(0); assertEquals("Incorrect type", TYPE_INSERT, oper.getType()); assertEquals("Incorrect target", RawContacts.CONTENT_URI, oper.getUri()); } { final ContentProviderOperation oper = diff.get(1); assertEquals("Incorrect type", TYPE_INSERT, oper.getType()); assertEquals("Incorrect target", Data.CONTENT_URI, oper.getUri()); } // Trim empty rows and try again, expecting silence EntityModifier.trimEmpty(set, sources); diff.clear(); state.buildDiff(diff); assertEquals("Unexpected operations", 0, diff.size()); }
public void testTrimLeaveValid() { final ContactsSource source = getSource(); final DataKind kindPhone = source.getKindForMimetype(Phone.CONTENT_ITEM_TYPE); final EditType typeHome = EntityModifier.getType(kindPhone, Phone.TYPE_HOME); // Test row that has type values with valid number final EntityDelta state = EntitySetTests.buildBeforeEntity(TEST_ID, VER_FIRST); final ValuesDelta values = EntityModifier.insertChild(state, kindPhone, typeHome); values.put(Phone.NUMBER, TEST_PHONE); // Build diff, expecting insert for data row and update enforcement EntitySetTests.assertDiffPattern( state, EntitySetTests.buildAssertVersion(VER_FIRST), EntitySetTests.buildUpdateAggregationSuspended(), EntitySetTests.buildOper( Data.CONTENT_URI, TYPE_INSERT, EntitySetTests.buildDataInsert(values, TEST_ID)), EntitySetTests.buildUpdateAggregationDefault()); // Trim empty rows and try again, expecting no differences EntityModifier.trimEmpty(state, source); EntitySetTests.assertDiffPattern( state, EntitySetTests.buildAssertVersion(VER_FIRST), EntitySetTests.buildUpdateAggregationSuspended(), EntitySetTests.buildOper( Data.CONTENT_URI, TYPE_INSERT, EntitySetTests.buildDataInsert(values, TEST_ID)), EntitySetTests.buildUpdateAggregationDefault()); }
public void testTrimUpdateUpdate() { final ContactsSource source = getSource(); final Sources sources = getSources(source); final DataKind kindPhone = source.getKindForMimetype(Phone.CONTENT_ITEM_TYPE); final EditType typeHome = EntityModifier.getType(kindPhone, Phone.TYPE_HOME); // Build "before" with two phone numbers final ContentValues first = new ContentValues(); first.put(Data._ID, TEST_ID); first.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE); first.put(kindPhone.typeColumn, typeHome.rawValue); first.put(Phone.NUMBER, TEST_PHONE); final EntityDelta state = getEntity(TEST_ID, first); final EntitySet set = EntitySet.fromSingle(state); // Build diff, expecting no changes final ArrayList<ContentProviderOperation> diff = Lists.newArrayList(); state.buildDiff(diff); assertEquals("Unexpected operations", 0, diff.size()); // Now update row by changing number to empty string, expecting single update final ValuesDelta child = state.getEntry(TEST_ID); child.put(Phone.NUMBER, ""); diff.clear(); state.buildDiff(diff); assertEquals("Unexpected operations", 3, diff.size()); { final ContentProviderOperation oper = diff.get(0); assertEquals("Expected aggregation mode change", TYPE_UPDATE, oper.getType()); assertEquals("Incorrect target", RawContacts.CONTENT_URI, oper.getUri()); } { final ContentProviderOperation oper = diff.get(1); assertEquals("Incorrect type", TYPE_UPDATE, oper.getType()); assertEquals("Incorrect target", Data.CONTENT_URI, oper.getUri()); } { final ContentProviderOperation oper = diff.get(2); assertEquals("Expected aggregation mode change", TYPE_UPDATE, oper.getType()); assertEquals("Incorrect target", RawContacts.CONTENT_URI, oper.getUri()); } // Now run trim, which should turn into deleting the whole contact EntityModifier.trimEmpty(set, sources); diff.clear(); state.buildDiff(diff); assertEquals("Unexpected operations", 1, diff.size()); { final ContentProviderOperation oper = diff.get(0); assertEquals("Incorrect type", TYPE_DELETE, oper.getType()); assertEquals("Incorrect target", RawContacts.CONTENT_URI, oper.getUri()); } }
/** * Insert various rows to test {@link EntityModifier#getValidTypes(EntityDelta, DataKind, * EditType)} */ public void testValidTypes() { // Build a source and pull specific types final ContactsSource source = getSource(); final DataKind kindPhone = source.getKindForMimetype(Phone.CONTENT_ITEM_TYPE); final EditType typeHome = EntityModifier.getType(kindPhone, Phone.TYPE_HOME); final EditType typeWork = EntityModifier.getType(kindPhone, Phone.TYPE_WORK); final EditType typeOther = EntityModifier.getType(kindPhone, Phone.TYPE_OTHER); List<EditType> validTypes; // Add first home, first work final EntityDelta state = getEntity(TEST_ID); EntityModifier.insertChild(state, kindPhone, typeHome); EntityModifier.insertChild(state, kindPhone, typeWork); // Expecting home, other validTypes = EntityModifier.getValidTypes(state, kindPhone, null); assertContains(validTypes, typeHome); assertNotContains(validTypes, typeWork); assertContains(validTypes, typeOther); // Add second home EntityModifier.insertChild(state, kindPhone, typeHome); // Expecting other validTypes = EntityModifier.getValidTypes(state, kindPhone, null); assertNotContains(validTypes, typeHome); assertNotContains(validTypes, typeWork); assertContains(validTypes, typeOther); // Add third and fourth home (invalid, but possible) EntityModifier.insertChild(state, kindPhone, typeHome); EntityModifier.insertChild(state, kindPhone, typeHome); // Expecting none validTypes = EntityModifier.getValidTypes(state, kindPhone, null); assertNotContains(validTypes, typeHome); assertNotContains(validTypes, typeWork); assertNotContains(validTypes, typeOther); }
/** Test {@link EntityModifier#canInsert(EntityDelta, DataKind)} by inserting various rows. */ public void testCanInsert() { // Build a source and pull specific types final ContactsSource source = getSource(); final DataKind kindPhone = source.getKindForMimetype(Phone.CONTENT_ITEM_TYPE); final EditType typeHome = EntityModifier.getType(kindPhone, Phone.TYPE_HOME); final EditType typeWork = EntityModifier.getType(kindPhone, Phone.TYPE_WORK); final EditType typeOther = EntityModifier.getType(kindPhone, Phone.TYPE_OTHER); // Add first home, first work final EntityDelta state = getEntity(TEST_ID); EntityModifier.insertChild(state, kindPhone, typeHome); EntityModifier.insertChild(state, kindPhone, typeWork); assertTrue("Unable to insert", EntityModifier.canInsert(state, kindPhone)); // Add two other, which puts us just under "5" overall limit EntityModifier.insertChild(state, kindPhone, typeOther); EntityModifier.insertChild(state, kindPhone, typeOther); assertTrue("Unable to insert", EntityModifier.canInsert(state, kindPhone)); // Add second home, which should push to snug limit EntityModifier.insertChild(state, kindPhone, typeHome); assertFalse("Able to insert", EntityModifier.canInsert(state, kindPhone)); }
/** * Test {@link EntityModifier#getBestValidType(EntityDelta, DataKind, boolean, int)} by asserting * expected best options in various states. */ public void testBestValidType() { // Build a source and pull specific types final ContactsSource source = getSource(); final DataKind kindPhone = source.getKindForMimetype(Phone.CONTENT_ITEM_TYPE); final EditType typeHome = EntityModifier.getType(kindPhone, Phone.TYPE_HOME); final EditType typeWork = EntityModifier.getType(kindPhone, Phone.TYPE_WORK); final EditType typeFaxWork = EntityModifier.getType(kindPhone, Phone.TYPE_FAX_WORK); final EditType typeOther = EntityModifier.getType(kindPhone, Phone.TYPE_OTHER); EditType suggested; // Default suggestion should be home final EntityDelta state = getEntity(TEST_ID); suggested = EntityModifier.getBestValidType(state, kindPhone, false, Integer.MIN_VALUE); assertEquals("Unexpected suggestion", typeHome, suggested); // Add first home, should now suggest work EntityModifier.insertChild(state, kindPhone, typeHome); suggested = EntityModifier.getBestValidType(state, kindPhone, false, Integer.MIN_VALUE); assertEquals("Unexpected suggestion", typeWork, suggested); // Add work fax, should still suggest work EntityModifier.insertChild(state, kindPhone, typeFaxWork); suggested = EntityModifier.getBestValidType(state, kindPhone, false, Integer.MIN_VALUE); assertEquals("Unexpected suggestion", typeWork, suggested); // Add other, should still suggest work EntityModifier.insertChild(state, kindPhone, typeOther); suggested = EntityModifier.getBestValidType(state, kindPhone, false, Integer.MIN_VALUE); assertEquals("Unexpected suggestion", typeWork, suggested); // Add work, now should suggest other EntityModifier.insertChild(state, kindPhone, typeWork); suggested = EntityModifier.getBestValidType(state, kindPhone, false, Integer.MIN_VALUE); assertEquals("Unexpected suggestion", typeOther, suggested); }
public void testIsEmptyDirectFields() { final ContactsSource source = getSource(); final DataKind kindPhone = source.getKindForMimetype(Phone.CONTENT_ITEM_TYPE); final EditType typeHome = EntityModifier.getType(kindPhone, Phone.TYPE_HOME); // Test row that has type values, but core fields are empty final EntityDelta state = getEntity(TEST_ID); final ValuesDelta values = EntityModifier.insertChild(state, kindPhone, typeHome); assertTrue("Expected empty", EntityModifier.isEmpty(values, kindPhone)); // Insert some data to trigger non-empty state values.put(Phone.NUMBER, TEST_PHONE); assertFalse("Expected non-empty", EntityModifier.isEmpty(values, kindPhone)); }
public void testTrimEmptySingle() { final ContactsSource source = getSource(); final DataKind kindPhone = source.getKindForMimetype(Phone.CONTENT_ITEM_TYPE); final EditType typeHome = EntityModifier.getType(kindPhone, Phone.TYPE_HOME); // Test row that has type values, but core fields are empty final EntityDelta state = getEntity(TEST_ID); final ValuesDelta values = EntityModifier.insertChild(state, kindPhone, typeHome); // Build diff, expecting insert for data row and update enforcement final ArrayList<ContentProviderOperation> diff = Lists.newArrayList(); state.buildDiff(diff); assertEquals("Unexpected operations", 3, diff.size()); { final ContentProviderOperation oper = diff.get(0); assertEquals("Expected aggregation mode change", TYPE_UPDATE, oper.getType()); assertEquals("Incorrect target", RawContacts.CONTENT_URI, oper.getUri()); } { final ContentProviderOperation oper = diff.get(1); assertEquals("Incorrect type", TYPE_INSERT, oper.getType()); assertEquals("Incorrect target", Data.CONTENT_URI, oper.getUri()); } { final ContentProviderOperation oper = diff.get(2); assertEquals("Expected aggregation mode change", TYPE_UPDATE, oper.getType()); assertEquals("Incorrect target", RawContacts.CONTENT_URI, oper.getUri()); } // Trim empty rows and try again, expecting delete of overall contact EntityModifier.trimEmpty(state, source); diff.clear(); state.buildDiff(diff); assertEquals("Unexpected operations", 1, diff.size()); { final ContentProviderOperation oper = diff.get(0); assertEquals("Incorrect type", TYPE_DELETE, oper.getType()); assertEquals("Incorrect target", RawContacts.CONTENT_URI, oper.getUri()); } }
public void testTrimEmptyUntouched() { final ContactsSource source = getSource(); final DataKind kindPhone = source.getKindForMimetype(Phone.CONTENT_ITEM_TYPE); final EditType typeHome = EntityModifier.getType(kindPhone, Phone.TYPE_HOME); // Build "before" that has empty row final EntityDelta state = getEntity(TEST_ID); final ContentValues before = new ContentValues(); before.put(Data._ID, TEST_ID); before.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE); state.addEntry(ValuesDelta.fromBefore(before)); // Build diff, expecting no changes final ArrayList<ContentProviderOperation> diff = Lists.newArrayList(); state.buildDiff(diff); assertEquals("Unexpected operations", 0, diff.size()); // Try trimming existing empty, which we shouldn't touch EntityModifier.trimEmpty(state, source); diff.clear(); state.buildDiff(diff); assertEquals("Unexpected operations", 0, diff.size()); }