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