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 package com.android.messaging.datamodel; 17 18 import android.database.Cursor; 19 import android.test.suitebuilder.annotation.SmallTest; 20 21 import com.android.messaging.BugleTestCase; 22 import com.android.messaging.datamodel.data.TestDataFactory; 23 import com.android.messaging.util.ContactUtil; 24 25 @SmallTest 26 public class FrequentContactsCursorBuilderTest extends BugleTestCase { 27 verifyBuiltCursor(final Cursor expected, final Cursor actual)28 private void verifyBuiltCursor(final Cursor expected, final Cursor actual) { 29 final int rowCount = expected.getCount(); 30 final int columnCount = expected.getColumnCount(); 31 assertEquals(rowCount, actual.getCount()); 32 assertEquals(columnCount, actual.getColumnCount()); 33 for (int i = 0; i < rowCount; i++) { 34 expected.moveToPosition(i); 35 actual.moveToPosition(i); 36 assertEquals(expected.getLong(ContactUtil.INDEX_DATA_ID), 37 actual.getLong(ContactUtil.INDEX_DATA_ID)); 38 assertEquals(expected.getLong(ContactUtil.INDEX_CONTACT_ID), 39 actual.getLong(ContactUtil.INDEX_CONTACT_ID)); 40 assertEquals(expected.getString(ContactUtil.INDEX_LOOKUP_KEY), 41 actual.getString(ContactUtil.INDEX_LOOKUP_KEY)); 42 assertEquals(expected.getString(ContactUtil.INDEX_DISPLAY_NAME), 43 actual.getString(ContactUtil.INDEX_DISPLAY_NAME)); 44 assertEquals(expected.getString(ContactUtil.INDEX_PHOTO_URI), 45 actual.getString(ContactUtil.INDEX_PHOTO_URI)); 46 assertEquals(expected.getString(ContactUtil.INDEX_PHONE_EMAIL), 47 actual.getString(ContactUtil.INDEX_PHONE_EMAIL)); 48 assertEquals(expected.getInt(ContactUtil.INDEX_PHONE_EMAIL_TYPE), 49 actual.getInt(ContactUtil.INDEX_PHONE_EMAIL_TYPE)); 50 assertEquals(expected.getString(ContactUtil.INDEX_PHONE_EMAIL_LABEL), 51 actual.getString(ContactUtil.INDEX_PHONE_EMAIL_LABEL)); 52 } 53 } 54 testIncompleteBuild()55 public void testIncompleteBuild() { 56 final FrequentContactsCursorBuilder builder = new FrequentContactsCursorBuilder(); 57 assertNull(builder.build()); 58 assertNull(builder.setFrequents(TestDataFactory.getStrequentContactsCursor()).build()); 59 builder.resetBuilder(); 60 assertNull(builder.build()); 61 assertNull(builder.setAllContacts(TestDataFactory.getAllContactListCursor()).build()); 62 } 63 testBuildOnce()64 public void testBuildOnce() { 65 final Cursor cursor = new FrequentContactsCursorBuilder() 66 .setAllContacts(TestDataFactory.getAllContactListCursor()) 67 .setFrequents(TestDataFactory.getStrequentContactsCursor()) 68 .build(); 69 assertNotNull(cursor); 70 verifyBuiltCursor(TestDataFactory.getFrequentContactListCursor(), cursor); 71 } 72 testBuildTwice()73 public void testBuildTwice() { 74 final FrequentContactsCursorBuilder builder = new FrequentContactsCursorBuilder(); 75 final Cursor firstCursor = builder 76 .setAllContacts(TestDataFactory.getAllContactListCursor()) 77 .setFrequents(TestDataFactory.getStrequentContactsCursor()) 78 .build(); 79 assertNotNull(firstCursor); 80 builder.resetBuilder(); 81 assertNull(builder.build()); 82 83 final Cursor secondCursor = builder 84 .setAllContacts(TestDataFactory.getAllContactListCursor()) 85 .setFrequents(TestDataFactory.getStrequentContactsCursor()) 86 .build(); 87 assertNotNull(firstCursor); 88 verifyBuiltCursor(TestDataFactory.getFrequentContactListCursor(), secondCursor); 89 } 90 } 91