1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.provider.cts.contacts; 18 19 import junit.framework.Assert; 20 21 import android.content.ContentProviderClient; 22 import android.content.ContentResolver; 23 import android.content.ContentValues; 24 import android.provider.ContactsContract; 25 import android.provider.ContactsContract.AggregationExceptions; 26 import android.provider.ContactsContract.CommonDataKinds.StructuredName; 27 import android.provider.ContactsContract.Contacts; 28 import android.provider.ContactsContract.RawContacts; 29 import android.provider.cts.contacts.ContactsContract_TestDataBuilder.TestContact; 30 import android.provider.cts.contacts.ContactsContract_TestDataBuilder.TestData; 31 import android.provider.cts.contacts.ContactsContract_TestDataBuilder.TestRawContact; 32 import android.test.AndroidTestCase; 33 34 /** 35 * CTS tests for the affect that {@link ContactsContract.Data#IS_SUPER_PRIMARY} has on names inside 36 * aggregated contacts. Additionally, this needs to test the affect that aggregating contacts 37 * together has on IS_SUPER_PRIMARY values in order to enforce the desired IS_SUPER_PRIMARY 38 * behavior. 39 */ 40 public class ContactsContract_IsSuperPrimaryName extends AndroidTestCase { 41 42 private ContentResolver mResolver; 43 private ContactsContract_TestDataBuilder mBuilder; 44 45 @Override 46 protected void setUp() throws Exception { 47 super.setUp(); 48 mResolver = getContext().getContentResolver(); 49 ContentProviderClient provider = 50 mResolver.acquireContentProviderClient(ContactsContract.AUTHORITY); 51 mBuilder = new ContactsContract_TestDataBuilder(provider); 52 } 53 54 @Override 55 protected void tearDown() throws Exception { 56 mBuilder.cleanup(); 57 super.tearDown(); 58 } 59 60 public void testIsSuperPrimary_name1SuperPrimary() throws Exception { 61 testInner_displayNameFromIsSuperPrimary(/* isFirstNamePrimary = */ true, "name1", "name2"); 62 } 63 64 public void testIsSuperPrimary_name2SuperPrimary() throws Exception { 65 testInner_displayNameFromIsSuperPrimary(/* isFirstNamePrimary = */ false, "name2", "name1"); 66 } 67 68 private void testInner_displayNameFromIsSuperPrimary(boolean isFirstNamePrimary, 69 String expectedDisplayName, String otherDisplayName) throws Exception { 70 71 // Setup: two raw contacts. One with a super primary name. One without. 72 TestRawContact rawContact1 = mBuilder.newRawContact() 73 .with(RawContacts.ACCOUNT_TYPE, "test_account") 74 .with(RawContacts.ACCOUNT_NAME, "test_name") 75 .insert(); 76 TestData name1 = rawContact1.newDataRow(StructuredName.CONTENT_ITEM_TYPE) 77 .with(StructuredName.GIVEN_NAME, "name1") 78 .with(StructuredName.IS_SUPER_PRIMARY, isFirstNamePrimary ? 1 : 0) 79 .insert(); 80 rawContact1.load(); 81 name1.load(); 82 83 TestRawContact rawContact2 = mBuilder.newRawContact() 84 .with(RawContacts.ACCOUNT_TYPE, "test_account") 85 .with(RawContacts.ACCOUNT_NAME, "test_name") 86 .insert(); 87 TestData name2 = rawContact2.newDataRow(StructuredName.CONTENT_ITEM_TYPE) 88 .with(StructuredName.GIVEN_NAME, "name2") 89 .with(StructuredName.IS_SUPER_PRIMARY, !isFirstNamePrimary ? 1 : 0) 90 .insert(); 91 rawContact2.load(); 92 name2.load(); 93 94 // Execute: aggregate the two raw contacts together 95 setAggregationException(rawContact1.getId(), rawContact2.getId()); 96 97 // Sanity check: two contacts are aggregated 98 rawContact1.load(); 99 rawContact2.load(); 100 Assert.assertEquals(rawContact1.getContactId(), rawContact2.getContactId()); 101 102 // Verify: the IS_SUPER_PRIMARY values are maintained after the merge 103 name1.assertColumn(StructuredName.IS_SUPER_PRIMARY, isFirstNamePrimary ? 1 : 0); 104 name2.assertColumn(StructuredName.IS_SUPER_PRIMARY, !isFirstNamePrimary ? 1 : 0); 105 106 // Verify: the display name is taken from the name with is_super_primary 107 TestContact contact = rawContact2.getContact().load(); 108 contact.assertColumn(Contacts.DISPLAY_NAME, expectedDisplayName); 109 110 // 111 // Now test what happens when you change IS_SUPER_PRIMARY on an existing contact 112 // 113 114 // Execute: make the non primary name IS_SUPER_PRIMARY 115 TestData nonPrimaryName = !isFirstNamePrimary ? name1 : name2; 116 ContentValues values = new ContentValues(); 117 values.put(StructuredName.IS_SUPER_PRIMARY, 1); 118 mResolver.update(nonPrimaryName.getUri(), values, null, null); 119 120 // Verify: the IS_SUPER_PRIMARY values swap 121 name1.load(); 122 name2.load(); 123 name1.assertColumn(StructuredName.IS_SUPER_PRIMARY, isFirstNamePrimary ? 0 : 1); 124 name2.assertColumn(StructuredName.IS_SUPER_PRIMARY, !isFirstNamePrimary ? 0 : 1); 125 126 // Verify: the display name is taken from the name with is_super_primary 127 contact.load(); 128 contact.assertColumn(Contacts.DISPLAY_NAME, otherDisplayName); 129 } 130 131 public void testIsSuperPrimaryName_mergeBothSuperPrimary() throws Exception { 132 // Setup: two raw contacts. Both names are super primary. 133 TestRawContact rawContact1 = mBuilder.newRawContact() 134 .with(RawContacts.ACCOUNT_TYPE, "test_account") 135 .with(RawContacts.ACCOUNT_NAME, "test_name") 136 .insert(); 137 TestData name1 = rawContact1.newDataRow(StructuredName.CONTENT_ITEM_TYPE) 138 .with(StructuredName.GIVEN_NAME, "name1") 139 .with(StructuredName.IS_SUPER_PRIMARY, 1) 140 .insert(); 141 rawContact1.load(); 142 name1.load(); 143 144 TestRawContact rawContact2 = mBuilder.newRawContact() 145 .with(RawContacts.ACCOUNT_TYPE, "test_account") 146 .with(RawContacts.ACCOUNT_NAME, "test_name") 147 .insert(); 148 TestData name2 = rawContact2.newDataRow(StructuredName.CONTENT_ITEM_TYPE) 149 .with(StructuredName.GIVEN_NAME, "name2") 150 .with(StructuredName.IS_SUPER_PRIMARY, 1) 151 .insert(); 152 rawContact2.load(); 153 name2.load(); 154 155 // Execute: aggregate the two contacts together 156 setAggregationException(rawContact1.getId(), rawContact2.getId()); 157 158 // Sanity check: two contacts are aggregated 159 rawContact1.load(); 160 rawContact2.load(); 161 Assert.assertEquals(rawContact1.getContactId(), rawContact2.getContactId()); 162 163 // Verify: both names are no longer super primary. 164 name1.load(); 165 name2.load(); 166 name1.assertColumn(StructuredName.IS_SUPER_PRIMARY, 0); 167 name2.assertColumn(StructuredName.IS_SUPER_PRIMARY, 0); 168 } 169 170 private void setAggregationException(long rawContactId1, long rawContactId2) { 171 ContentValues values = new ContentValues(); 172 values.put(AggregationExceptions.RAW_CONTACT_ID1, rawContactId1); 173 values.put(AggregationExceptions.RAW_CONTACT_ID2, rawContactId2); 174 values.put(AggregationExceptions.TYPE, AggregationExceptions.TYPE_KEEP_TOGETHER); 175 mResolver.update(AggregationExceptions.CONTENT_URI, values, null, null); 176 } 177 } 178