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