CallLogAdapterTest.java revision e0b2f1e2d01d1ac52ba207dc7ce76971d853298e
1/*
2 * Copyright (C) 2011 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
17package com.android.contacts.calllog;
18
19import android.content.Context;
20import android.database.MatrixCursor;
21import android.test.AndroidTestCase;
22import android.test.suitebuilder.annotation.SmallTest;
23import android.view.View;
24
25import com.google.common.collect.Lists;
26
27import java.util.List;
28
29/**
30 * Unit tests for {@link CallLogAdapter}.
31 */
32@SmallTest
33public class CallLogAdapterTest extends AndroidTestCase {
34    private static final String TEST_NUMBER = "12345678";
35    private static final String TEST_NAME = "name";
36    private static final String TEST_NUMBER_LABEL = "label";
37    private static final int TEST_NUMBER_TYPE = 1;
38    private static final String TEST_COUNTRY_ISO = "US";
39
40    /** The object under test. */
41    private TestCallLogAdapter mAdapter;
42
43    private MatrixCursor mCursor;
44    private View mView;
45
46    @Override
47    protected void setUp() throws Exception {
48        super.setUp();
49        // Use a call fetcher that does not do anything.
50        CallLogAdapter.CallFetcher fakeCallFetcher = new CallLogAdapter.CallFetcher() {
51            @Override
52            public void fetchCalls() {}
53        };
54
55        ContactInfoHelper fakeContactInfoHelper =
56                new ContactInfoHelper(getContext(), TEST_COUNTRY_ISO) {
57                    @Override
58                    public ContactInfo lookupNumber(String number, String countryIso) {
59                        ContactInfo info = new ContactInfo();
60                        info.number = number;
61                        info.formattedNumber = number;
62                        return info;
63                    }
64                };
65
66        mAdapter = new TestCallLogAdapter(getContext(), fakeCallFetcher, fakeContactInfoHelper);
67        // The cursor used in the tests to store the entries to display.
68        mCursor = new MatrixCursor(CallLogQuery.EXTENDED_PROJECTION);
69        mCursor.moveToFirst();
70        // The views into which to store the data.
71        mView = new View(getContext());
72        mView.setTag(CallLogListItemViews.createForTest(getContext()));
73    }
74
75    @Override
76    protected void tearDown() throws Exception {
77        mAdapter = null;
78        mCursor = null;
79        mView = null;
80        super.tearDown();
81    }
82
83    public void testBindView_NoCallLogCacheNorMemoryCache_EnqueueRequest() {
84        mCursor.addRow(createCallLogEntry());
85
86        // Bind the views of a single row.
87        mAdapter.bindStandAloneView(mView, getContext(), mCursor);
88
89        // There is one request for contact details.
90        assertEquals(1, mAdapter.requests.size());
91
92        TestCallLogAdapter.Request request = mAdapter.requests.get(0);
93        // It is for the number we need to show.
94        assertEquals(TEST_NUMBER, request.number);
95        // It has the right country.
96        assertEquals(TEST_COUNTRY_ISO, request.countryIso);
97        // Since there is nothing in the cache, it is an immediate request.
98        assertTrue("should be immediate", request.immediate);
99    }
100
101    public void testBindView_CallLogCacheButNoMemoryCache_EnqueueRequest() {
102        mCursor.addRow(createCallLogEntryWithCachedValues());
103
104        // Bind the views of a single row.
105        mAdapter.bindStandAloneView(mView, getContext(), mCursor);
106
107        // There is one request for contact details.
108        assertEquals(1, mAdapter.requests.size());
109
110        TestCallLogAdapter.Request request = mAdapter.requests.get(0);
111        // The values passed to the request, match the ones in the call log cache.
112        assertEquals(TEST_NAME, request.callLogInfo.name);
113        assertEquals(1, request.callLogInfo.type);
114        assertEquals(TEST_NUMBER_LABEL, request.callLogInfo.label);
115    }
116
117
118    public void testBindView_NoCallLogButMemoryCache_EnqueueRequest() {
119        mCursor.addRow(createCallLogEntry());
120        mAdapter.injectContactInfoForTest(TEST_NUMBER, TEST_COUNTRY_ISO, createContactInfo());
121
122        // Bind the views of a single row.
123        mAdapter.bindStandAloneView(mView, getContext(), mCursor);
124
125        // There is one request for contact details.
126        assertEquals(1, mAdapter.requests.size());
127
128        TestCallLogAdapter.Request request = mAdapter.requests.get(0);
129        // Since there is something in the cache, it is not an immediate request.
130        assertFalse("should not be immediate", request.immediate);
131    }
132
133    public void testBindView_BothCallLogAndMemoryCache_NoEnqueueRequest() {
134        mCursor.addRow(createCallLogEntryWithCachedValues());
135        mAdapter.injectContactInfoForTest(TEST_NUMBER, TEST_COUNTRY_ISO, createContactInfo());
136
137        // Bind the views of a single row.
138        mAdapter.bindStandAloneView(mView, getContext(), mCursor);
139
140        // Cache and call log are up-to-date: no need to request update.
141        assertEquals(0, mAdapter.requests.size());
142    }
143
144    public void testBindView_MismatchBetwenCallLogAndMemoryCache_EnqueueRequest() {
145        mCursor.addRow(createCallLogEntryWithCachedValues());
146
147        // Contact info contains a different name.
148        ContactInfo info = createContactInfo();
149        info.name = "new name";
150        mAdapter.injectContactInfoForTest(TEST_NUMBER, TEST_COUNTRY_ISO, info);
151
152        // Bind the views of a single row.
153        mAdapter.bindStandAloneView(mView, getContext(), mCursor);
154
155        // There is one request for contact details.
156        assertEquals(1, mAdapter.requests.size());
157
158        TestCallLogAdapter.Request request = mAdapter.requests.get(0);
159        // Since there is something in the cache, it is not an immediate request.
160        assertFalse("should not be immediate", request.immediate);
161    }
162
163    /** Returns a contact info with default values. */
164    private ContactInfo createContactInfo() {
165        ContactInfo info = new ContactInfo();
166        info.number = TEST_NUMBER;
167        info.name = TEST_NAME;
168        info.type = TEST_NUMBER_TYPE;
169        info.label = TEST_NUMBER_LABEL;
170        return info;
171    }
172
173    /** Returns a call log entry without cached values. */
174    private Object[] createCallLogEntry() {
175        Object[] values = CallLogQueryTestUtils.createTestExtendedValues();
176        values[CallLogQuery.NUMBER] = TEST_NUMBER;
177        values[CallLogQuery.COUNTRY_ISO] = TEST_COUNTRY_ISO;
178        return values;
179    }
180
181    /** Returns a call log entry with a cached values. */
182    private Object[] createCallLogEntryWithCachedValues() {
183        Object[] values = createCallLogEntry();
184        values[CallLogQuery.CACHED_NAME] = TEST_NAME;
185        values[CallLogQuery.CACHED_NUMBER_TYPE] = TEST_NUMBER_TYPE;
186        values[CallLogQuery.CACHED_NUMBER_LABEL] = TEST_NUMBER_LABEL;
187        return values;
188    }
189
190    /**
191     * Subclass of {@link CallLogAdapter} used in tests to intercept certain calls.
192     */
193    // TODO: This would be better done by splitting the contact lookup into a collaborator class
194    // instead.
195    private static final class TestCallLogAdapter extends CallLogAdapter {
196        public static class Request {
197            public final String number;
198            public final String countryIso;
199            public final ContactInfo callLogInfo;
200            public final boolean immediate;
201
202            public Request(String number, String countryIso, ContactInfo callLogInfo,
203                    boolean immediate) {
204                this.number = number;
205                this.countryIso = countryIso;
206                this.callLogInfo = callLogInfo;
207                this.immediate = immediate;
208            }
209        }
210
211        public final List<Request> requests = Lists.newArrayList();
212
213        public TestCallLogAdapter(Context context, CallFetcher callFetcher,
214                ContactInfoHelper contactInfoHelper) {
215            super(context, callFetcher, contactInfoHelper);
216        }
217
218        @Override
219        void enqueueRequest(String number, String countryIso, ContactInfo callLogInfo,
220                boolean immediate) {
221            requests.add(new Request(number, countryIso, callLogInfo, immediate));
222        }
223    }
224}
225