Home | History | Annotate | Download | only in dataitem
      1 /*
      2  * Copyright (C) 2014 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 package com.android.contacts.common.model.dataitem;
     17 
     18 import android.content.ContentValues;
     19 import android.provider.ContactsContract.CommonDataKinds.Email;
     20 import android.provider.ContactsContract.CommonDataKinds.Event;
     21 import android.provider.ContactsContract.CommonDataKinds.Im;
     22 import android.provider.ContactsContract.CommonDataKinds.Nickname;
     23 import android.provider.ContactsContract.CommonDataKinds.Note;
     24 import android.provider.ContactsContract.CommonDataKinds.Organization;
     25 import android.provider.ContactsContract.CommonDataKinds.Phone;
     26 import android.provider.ContactsContract.CommonDataKinds.Relation;
     27 import android.provider.ContactsContract.CommonDataKinds.SipAddress;
     28 import android.provider.ContactsContract.CommonDataKinds.StructuredName;
     29 import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
     30 import android.provider.ContactsContract.CommonDataKinds.Website;
     31 import android.provider.ContactsContract.Contacts.Data;
     32 import android.provider.ContactsContract.Contacts.Entity;
     33 import android.test.AndroidTestCase;
     34 
     35 import com.android.contacts.common.Collapser;
     36 import com.android.contacts.common.model.account.AccountType.EditType;
     37 import com.android.contacts.common.model.account.BaseAccountType;
     38 import com.android.contacts.common.model.account.GoogleAccountType;
     39 import com.android.contacts.common.model.dataitem.DataItem;
     40 import com.android.contacts.common.model.dataitem.DataKind;
     41 
     42 import java.lang.Math;
     43 import java.util.ArrayList;
     44 import java.util.List;
     45 
     46 /**
     47  * Test case for {@link DataItem}.
     48  */
     49 public class DataItemTests extends AndroidTestCase {
     50 
     51     private ContentValues mValues1;
     52     private ContentValues mValues2;
     53     private ContentValues mValues3;
     54     private ContentValues mValues4;
     55     private GoogleAccountType mGoogleAccountType;
     56 
     57     @Override
     58     protected void setUp() {
     59         mValues1 = new ContentValues();
     60         mValues2 = new ContentValues();
     61         mValues3 = new ContentValues();
     62         mValues4 = new ContentValues();
     63 
     64         mValues1.put(Data._ID, 1);
     65         mValues2.put(Data._ID, 2);
     66         mValues3.put(Data._ID, 3);
     67         mValues4.put(Data._ID, 4);
     68 
     69         mGoogleAccountType = new GoogleAccountType(getContext(), "packageName");
     70     }
     71 
     72     private List<DataItem> createDataItemsAndCollapse(DataKind kind, ContentValues... values) {
     73         final List<DataItem> dataList = new ArrayList<>(values.length);
     74         for (ContentValues value : values) {
     75             final DataItem data = DataItem.createFrom(value);
     76             data.setDataKind(kind);
     77             dataList.add(data);
     78         }
     79         Collapser.collapseList(dataList, getContext());
     80         return dataList;
     81     }
     82 
     83     public void testDataItemCollapsing_genericDataItemFields() {
     84         mValues1.put(Data.IS_SUPER_PRIMARY, 1);
     85         mValues2.put(Data.IS_PRIMARY, 0);
     86 
     87         mValues1.put(Entity.TIMES_USED, 5);
     88         mValues2.put(Entity.TIMES_USED, 4);
     89 
     90         mValues1.put(Entity.LAST_TIME_USED, 555);
     91         mValues2.put(Entity.LAST_TIME_USED, 999);
     92 
     93         final DataKind kind = new DataKind("test.mimetype", 0, 0, false);
     94         kind.actionBody = new BaseAccountType.SimpleInflater(0);
     95         kind.typeList = new ArrayList<>();
     96         kind.typeList.add(new EditType(1, -1));
     97         kind.typeList.add(new EditType(2, -1));
     98         kind.typeColumn = Data.DATA2;
     99 
    100         mValues1.put(kind.typeColumn, 2);
    101         mValues2.put(kind.typeColumn, 1);
    102 
    103         final List<DataItem> dataList = createDataItemsAndCollapse(kind, mValues1, mValues2);
    104 
    105         assertEquals(1, dataList.size());
    106         assertEquals(true, dataList.get(0).isSuperPrimary());
    107         assertEquals(true, dataList.get(0).isPrimary());
    108         assertEquals(9, (int) dataList.get(0).getTimesUsed());
    109         assertEquals(999L, (long) dataList.get(0).getLastTimeUsed());
    110         assertEquals(1, dataList.get(0).getKindTypeColumn(kind));
    111     }
    112 
    113     public void testDataItemCollapsing_email() {
    114         final String email1 = "email1 (at) google.com";
    115         final String email2 = "email2 (at) google.com";
    116 
    117         mValues1.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);
    118         mValues2.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);
    119         mValues3.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);
    120 
    121         mValues1.put(Email.ADDRESS, email1);
    122         mValues2.put(Email.ADDRESS, email1);
    123         mValues3.put(Email.ADDRESS, email2);
    124 
    125         mValues1.put(Email.TYPE, Email.TYPE_MOBILE);
    126         mValues2.put(Email.TYPE, Email.TYPE_HOME);
    127         mValues3.put(Email.TYPE, Email.TYPE_WORK);
    128 
    129         final DataKind kind = mGoogleAccountType.getKindForMimetype(Email.CONTENT_ITEM_TYPE);
    130 
    131         final List<DataItem> dataList =
    132                 createDataItemsAndCollapse(kind, mValues1, mValues2, mValues3);
    133 
    134         assertEquals(2, dataList.size());
    135         assertEquals(email1, ((EmailDataItem) dataList.get(0)).getAddress());
    136         assertEquals(email2, ((EmailDataItem) dataList.get(1)).getAddress());
    137         assertEquals(Math.min(Email.TYPE_MOBILE, Email.TYPE_HOME),
    138                 ((EmailDataItem) dataList.get(0)).getKindTypeColumn(kind));
    139     }
    140 
    141     public void testDataItemCollapsing_event() {
    142         final String date1 = "2014-01-01";
    143         final String date2 = "2014-02-02";
    144         final String customLabel1 = "custom label1";
    145         final String customLabel2 = "custom label2";
    146 
    147         mValues1.put(Data.MIMETYPE, Event.CONTENT_ITEM_TYPE);
    148         mValues2.put(Data.MIMETYPE, Event.CONTENT_ITEM_TYPE);
    149         mValues3.put(Data.MIMETYPE, Event.CONTENT_ITEM_TYPE);
    150         mValues4.put(Data.MIMETYPE, Event.CONTENT_ITEM_TYPE);
    151 
    152         mValues1.put(Event.START_DATE, date1);
    153         mValues2.put(Event.START_DATE, date1);
    154         mValues3.put(Event.START_DATE, date1);
    155         mValues4.put(Event.START_DATE, date2);
    156 
    157         mValues1.put(Event.TYPE, Event.TYPE_CUSTOM);
    158         mValues2.put(Event.TYPE, Event.TYPE_CUSTOM);
    159         mValues3.put(Event.TYPE, Event.TYPE_CUSTOM);
    160         mValues4.put(Event.TYPE, Event.TYPE_ANNIVERSARY);
    161 
    162         mValues1.put(Event.LABEL, customLabel1);
    163         mValues2.put(Event.LABEL, customLabel1);
    164         mValues3.put(Event.LABEL, customLabel2);
    165 
    166         final DataKind kind = mGoogleAccountType.getKindForMimetype(Event.CONTENT_ITEM_TYPE);
    167 
    168         final List<DataItem> dataList =
    169                 createDataItemsAndCollapse(kind, mValues1, mValues2, mValues3, mValues4);
    170 
    171         assertEquals(3, dataList.size());
    172         assertEquals(customLabel1, ((EventDataItem) dataList.get(0)).getLabel());
    173         assertEquals(customLabel2, ((EventDataItem) dataList.get(1)).getLabel());
    174         assertEquals(date2, ((EventDataItem) dataList.get(2)).getStartDate());
    175     }
    176 
    177     public void testDataItemCollapsing_im() {
    178         final String address1 = "address 1";
    179         final String address2 = "address 2";
    180         final String customProtocol1 = "custom 1";
    181         final String customProtocol2 = "custom 2";
    182 
    183         mValues1.put(Data.MIMETYPE, Im.CONTENT_ITEM_TYPE);
    184         mValues2.put(Data.MIMETYPE, Im.CONTENT_ITEM_TYPE);
    185         mValues3.put(Data.MIMETYPE, Im.CONTENT_ITEM_TYPE);
    186         mValues4.put(Data.MIMETYPE, Im.CONTENT_ITEM_TYPE);
    187 
    188         mValues1.put(Im.DATA, address1);
    189         mValues2.put(Im.DATA, address1);
    190         mValues3.put(Im.DATA, address1);
    191         mValues4.put(Im.DATA, address2);
    192 
    193         mValues1.put(Im.PROTOCOL, Im.PROTOCOL_CUSTOM);
    194         mValues2.put(Im.PROTOCOL, Im.PROTOCOL_CUSTOM);
    195         mValues3.put(Im.PROTOCOL, Im.PROTOCOL_CUSTOM);
    196         mValues4.put(Im.PROTOCOL, Im.PROTOCOL_AIM);
    197 
    198         mValues1.put(Im.CUSTOM_PROTOCOL, customProtocol1);
    199         mValues2.put(Im.CUSTOM_PROTOCOL, customProtocol1);
    200         mValues3.put(Im.CUSTOM_PROTOCOL, customProtocol2);
    201 
    202         final DataKind kind = mGoogleAccountType.getKindForMimetype(Im.CONTENT_ITEM_TYPE);
    203 
    204         final List<DataItem> dataList =
    205                 createDataItemsAndCollapse(kind, mValues1, mValues2, mValues3, mValues4);
    206 
    207         assertEquals(3, dataList.size());
    208         assertEquals(address1, ((ImDataItem) dataList.get(0)).getData());
    209         assertEquals(address1, ((ImDataItem) dataList.get(1)).getData());
    210         assertEquals(address2, ((ImDataItem) dataList.get(2)).getData());
    211 
    212         assertEquals(customProtocol1, ((ImDataItem) dataList.get(0)).getCustomProtocol());
    213         assertEquals(customProtocol2, ((ImDataItem) dataList.get(1)).getCustomProtocol());
    214         assertEquals(Im.PROTOCOL_AIM, (int) ((ImDataItem) dataList.get(2)).getProtocol());
    215     }
    216 
    217     public void testDataItemCollapsing_nickname() {
    218         final String nickname1 = "nickname 1";
    219         final String nickname2 = "nickname 2";
    220 
    221         mValues1.put(Data.MIMETYPE, Nickname.CONTENT_ITEM_TYPE);
    222         mValues2.put(Data.MIMETYPE, Nickname.CONTENT_ITEM_TYPE);
    223         mValues3.put(Data.MIMETYPE, Nickname.CONTENT_ITEM_TYPE);
    224 
    225         mValues1.put(Nickname.NAME, nickname1);
    226         mValues2.put(Nickname.NAME, nickname1);
    227         mValues3.put(Nickname.NAME, nickname2);
    228 
    229         final DataKind kind = mGoogleAccountType.getKindForMimetype(Nickname.CONTENT_ITEM_TYPE);
    230 
    231         final List<DataItem> dataList =
    232                 createDataItemsAndCollapse(kind, mValues1, mValues2, mValues3);
    233 
    234         assertEquals(2, dataList.size());
    235         assertEquals(nickname1, ((NicknameDataItem) dataList.get(0)).getName());
    236         assertEquals(nickname2, ((NicknameDataItem) dataList.get(1)).getName());
    237     }
    238 
    239     public void testDataItemCollapsing_note() {
    240         final String note1 = "note 1";
    241         final String note2 = "note 2";
    242 
    243         mValues1.put(Data.MIMETYPE, Note.CONTENT_ITEM_TYPE);
    244         mValues2.put(Data.MIMETYPE, Note.CONTENT_ITEM_TYPE);
    245         mValues3.put(Data.MIMETYPE, Note.CONTENT_ITEM_TYPE);
    246 
    247         mValues1.put(Note.NOTE, note1);
    248         mValues2.put(Note.NOTE, note1);
    249         mValues3.put(Note.NOTE, note2);
    250 
    251         DataKind kind = mGoogleAccountType.getKindForMimetype(Note.CONTENT_ITEM_TYPE);
    252 
    253         final List<DataItem> dataList =
    254                 createDataItemsAndCollapse(kind, mValues1, mValues2, mValues3);
    255 
    256         assertEquals(2, dataList.size());
    257         assertEquals(note1, ((NoteDataItem) dataList.get(0)).getNote());
    258         assertEquals(note2, ((NoteDataItem) dataList.get(1)).getNote());
    259     }
    260 
    261     public void testDataItemCollapsing_organization() {
    262         final String company1 = "company1";
    263         final String company2 = "company2";
    264         final String title1 = "title1";
    265         final String title2 = "title2";
    266 
    267         mValues1.put(Data.MIMETYPE, Organization.CONTENT_ITEM_TYPE);
    268         mValues2.put(Data.MIMETYPE, Organization.CONTENT_ITEM_TYPE);
    269         mValues3.put(Data.MIMETYPE, Organization.CONTENT_ITEM_TYPE);
    270         mValues4.put(Data.MIMETYPE, Organization.CONTENT_ITEM_TYPE);
    271 
    272         mValues1.put(Organization.COMPANY, company1);
    273         mValues2.put(Organization.COMPANY, company1);
    274         mValues3.put(Organization.COMPANY, company1);
    275         mValues4.put(Organization.COMPANY, company2);
    276 
    277         mValues1.put(Organization.TITLE, title1);
    278         mValues2.put(Organization.TITLE, title1);
    279         mValues3.put(Organization.TITLE, title2);
    280         mValues4.put(Organization.TITLE, title1);
    281 
    282         final DataKind kind =
    283                 mGoogleAccountType.getKindForMimetype(Organization.CONTENT_ITEM_TYPE);
    284 
    285         final List<DataItem> dataList =
    286                 createDataItemsAndCollapse(kind, mValues1, mValues2, mValues3, mValues4);
    287 
    288         assertEquals(3, dataList.size());
    289         assertEquals(company1, ((OrganizationDataItem) dataList.get(0)).getCompany());
    290         assertEquals(company1, ((OrganizationDataItem) dataList.get(1)).getCompany());
    291         assertEquals(company2, ((OrganizationDataItem) dataList.get(2)).getCompany());
    292 
    293         assertEquals(title1, ((OrganizationDataItem) dataList.get(0)).getTitle());
    294         assertEquals(title2, ((OrganizationDataItem) dataList.get(1)).getTitle());
    295     }
    296 
    297     public void testDataItemCollapsing_phone() {
    298         final String phone1 = "111-111-1111";
    299         final String phone1a = "1111111111";
    300         final String phone2 = "222-222-2222";
    301 
    302         mValues1.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
    303         mValues2.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
    304         mValues3.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
    305 
    306         mValues1.put(Phone.NUMBER, phone1);
    307         mValues2.put(Phone.NUMBER, phone1a);
    308         mValues3.put(Phone.NUMBER, phone2);
    309 
    310         mValues1.put(Phone.TYPE, Phone.TYPE_MOBILE);
    311         mValues2.put(Phone.TYPE, Phone.TYPE_HOME);
    312         mValues3.put(Phone.TYPE, Phone.TYPE_WORK);
    313 
    314         final DataKind kind = mGoogleAccountType.getKindForMimetype(Phone.CONTENT_ITEM_TYPE);
    315 
    316         final List<DataItem> dataList =
    317                 createDataItemsAndCollapse(kind, mValues1, mValues2, mValues3);
    318         assertEquals(2, dataList.size());
    319         assertEquals(phone1, ((PhoneDataItem) dataList.get(0)).getNumber());
    320         assertEquals(phone2, ((PhoneDataItem) dataList.get(1)).getNumber());
    321         assertEquals(Phone.TYPE_MOBILE,
    322                 ((PhoneDataItem) dataList.get(0)).getKindTypeColumn(kind));
    323     }
    324 
    325     public void testDataItemCollapsing_relation() {
    326         final String name1 = "name1";
    327         final String name2 = "name2";
    328         final String customRelation1 = "custom relation 1";
    329         final String customRelation2 = "custom relation 2";
    330 
    331         mValues1.put(Data.MIMETYPE, Relation.CONTENT_ITEM_TYPE);
    332         mValues2.put(Data.MIMETYPE, Relation.CONTENT_ITEM_TYPE);
    333         mValues3.put(Data.MIMETYPE, Relation.CONTENT_ITEM_TYPE);
    334         mValues4.put(Data.MIMETYPE, Relation.CONTENT_ITEM_TYPE);
    335 
    336         mValues1.put(Relation.NAME, name1);
    337         mValues2.put(Relation.NAME, name1);
    338         mValues3.put(Relation.NAME, name1);
    339         mValues4.put(Relation.NAME, name2);
    340 
    341         mValues1.put(Relation.TYPE, Relation.TYPE_CUSTOM);
    342         mValues2.put(Relation.TYPE, Relation.TYPE_CUSTOM);
    343         mValues3.put(Relation.TYPE, Relation.TYPE_CUSTOM);
    344         mValues4.put(Relation.TYPE, Relation.TYPE_BROTHER);
    345 
    346         mValues1.put(Relation.LABEL, customRelation1);
    347         mValues2.put(Relation.LABEL, customRelation1);
    348         mValues3.put(Relation.LABEL, customRelation2);
    349 
    350         final DataKind kind = mGoogleAccountType.getKindForMimetype(Relation.CONTENT_ITEM_TYPE);
    351 
    352         final List<DataItem> dataList =
    353                 createDataItemsAndCollapse(kind, mValues1, mValues2, mValues3, mValues4);
    354 
    355         assertEquals(3, dataList.size());
    356         assertEquals(name1, ((RelationDataItem) dataList.get(0)).getName());
    357         assertEquals(name2, ((RelationDataItem) dataList.get(2)).getName());
    358 
    359         assertEquals(customRelation1, ((RelationDataItem) dataList.get(0)).getLabel());
    360         assertEquals(customRelation2, ((RelationDataItem) dataList.get(1)).getLabel());
    361     }
    362 
    363     public void testDataItemCollapsing_sip() {
    364         final String sip1 = "sip 1";
    365         final String sip2 = "sip 2";
    366 
    367         mValues1.put(Data.MIMETYPE, SipAddress.CONTENT_ITEM_TYPE);
    368         mValues2.put(Data.MIMETYPE, SipAddress.CONTENT_ITEM_TYPE);
    369         mValues3.put(Data.MIMETYPE, SipAddress.CONTENT_ITEM_TYPE);
    370 
    371         mValues1.put(SipAddress.SIP_ADDRESS, sip1);
    372         mValues2.put(SipAddress.SIP_ADDRESS, sip1);
    373         mValues3.put(SipAddress.SIP_ADDRESS, sip2);
    374 
    375         mValues1.put(SipAddress.TYPE, SipAddress.TYPE_WORK);
    376         mValues2.put(SipAddress.TYPE, SipAddress.TYPE_HOME);
    377         mValues3.put(SipAddress.TYPE, SipAddress.TYPE_WORK);
    378 
    379         final DataKind kind = mGoogleAccountType.getKindForMimetype(SipAddress.CONTENT_ITEM_TYPE);
    380 
    381         final List<DataItem> dataList =
    382                 createDataItemsAndCollapse(kind, mValues1, mValues2, mValues3);
    383 
    384         assertEquals(2, dataList.size());
    385         assertEquals(sip1, ((SipAddressDataItem) dataList.get(0)).getSipAddress());
    386         assertEquals(sip2, ((SipAddressDataItem) dataList.get(1)).getSipAddress());
    387     }
    388 
    389     public void testDataItemCollapsing_structuredName() {
    390         final String displayName1 = "Display Name 1";
    391         final String displayName2 = "Display Name 2";
    392 
    393         mValues1.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
    394         mValues2.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
    395         mValues3.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
    396 
    397         mValues1.put(StructuredName.DISPLAY_NAME, displayName1);
    398         mValues2.put(StructuredName.DISPLAY_NAME, displayName1);
    399         mValues3.put(StructuredName.DISPLAY_NAME, displayName2);
    400 
    401         final DataKind kind =
    402                 mGoogleAccountType.getKindForMimetype(StructuredName.CONTENT_ITEM_TYPE);
    403 
    404         final List<DataItem> dataList =
    405                 createDataItemsAndCollapse(kind, mValues1, mValues2, mValues3);
    406 
    407         assertEquals(2, dataList.size());
    408         assertEquals(displayName1, ((StructuredNameDataItem) dataList.get(0)).getDisplayName());
    409         assertEquals(displayName2, ((StructuredNameDataItem) dataList.get(1)).getDisplayName());
    410     }
    411 
    412     public void testDataItemCollapsing_structuredPostal() {
    413         final String formattedAddress1 = "Formatted Address 1";
    414         final String formattedAddress2 = "Formatted Address 2";
    415 
    416         mValues1.put(Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE);
    417         mValues2.put(Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE);
    418         mValues3.put(Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE);
    419 
    420         mValues1.put(StructuredPostal.FORMATTED_ADDRESS, formattedAddress1);
    421         mValues2.put(StructuredPostal.FORMATTED_ADDRESS, formattedAddress1);
    422         mValues3.put(StructuredPostal.FORMATTED_ADDRESS, formattedAddress2);
    423 
    424         final DataKind kind =
    425                 mGoogleAccountType.getKindForMimetype(StructuredPostal.CONTENT_ITEM_TYPE);
    426 
    427         final List<DataItem> dataList =
    428                 createDataItemsAndCollapse(kind, mValues1, mValues2, mValues3);
    429 
    430         assertEquals(2, dataList.size());
    431         assertEquals(formattedAddress1,
    432                 ((StructuredPostalDataItem) dataList.get(0)).getFormattedAddress());
    433         assertEquals(formattedAddress2,
    434                 ((StructuredPostalDataItem) dataList.get(1)).getFormattedAddress());
    435     }
    436 
    437     public void testDataItemCollapsing_website() {
    438         final String url1 = "www.url1.com";
    439         final String url2 = "www.url2.com";
    440 
    441         mValues1.put(Data.MIMETYPE, Website.CONTENT_ITEM_TYPE);
    442         mValues2.put(Data.MIMETYPE, Website.CONTENT_ITEM_TYPE);
    443         mValues3.put(Data.MIMETYPE, Website.CONTENT_ITEM_TYPE);
    444 
    445         mValues1.put(Website.URL, url1);
    446         mValues2.put(Website.URL, url1);
    447         mValues3.put(Website.URL, url2);
    448 
    449         final DataKind kind = mGoogleAccountType.getKindForMimetype(Website.CONTENT_ITEM_TYPE);
    450 
    451         final List<DataItem> dataList =
    452                 createDataItemsAndCollapse(kind, mValues1, mValues2, mValues3);
    453 
    454         assertEquals(2, dataList.size());
    455         assertEquals(url1, ((WebsiteDataItem) dataList.get(0)).getUrl());
    456         assertEquals(url2, ((WebsiteDataItem) dataList.get(1)).getUrl());
    457     }
    458 }
    459