1/*
2 * Copyright (C) 2016 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.incallui;
18
19import android.test.AndroidTestCase;
20import android.test.suitebuilder.annotation.MediumTest;
21
22import com.android.contacts.common.preference.ContactsPreferences;
23import com.android.incallui.ContactInfoCache.ContactCacheEntry;
24
25import org.mockito.Mock;
26import org.mockito.Mockito;
27import org.mockito.MockitoAnnotations;
28
29@MediumTest
30public class CallCardPresenterTest extends AndroidTestCase {
31
32    private static final String NAME_PRIMARY = "Full Name";
33    private static final String NAME_ALTERNATIVE = "Name, Full";
34    private static final String LOCATION = "US";
35    private static final String NUMBER = "8006459001";
36
37    @Mock private ContactsPreferences mContactsPreferences;
38    private ContactCacheEntry mUnlockedContactInfo;
39    private ContactCacheEntry mLockedContactInfo;
40
41    @Override
42    public void setUp() throws Exception {
43        super.setUp();
44        MockitoAnnotations.initMocks(this);
45
46        Mockito.when(mContactsPreferences.getDisplayOrder())
47                .thenReturn(ContactsPreferences.DISPLAY_ORDER_PRIMARY);
48
49        // Unlocked all contact info is available
50        mUnlockedContactInfo = new ContactCacheEntry();
51        mUnlockedContactInfo.namePrimary = NAME_PRIMARY;
52        mUnlockedContactInfo.nameAlternative = NAME_ALTERNATIVE;
53        mUnlockedContactInfo.location = LOCATION;
54        mUnlockedContactInfo.number = NUMBER;
55
56        // Locked only number and location are available
57        mLockedContactInfo = new ContactCacheEntry();
58        mLockedContactInfo .location = LOCATION;
59        mLockedContactInfo .number = NUMBER;
60    }
61
62    @Override
63    public void tearDown() throws Exception {
64        super.tearDown();
65        ContactsPreferencesFactory.setTestInstance(null);
66    }
67
68    public void testGetNameForCall_Unlocked() {
69        ContactsPreferencesFactory.setTestInstance(mContactsPreferences);
70        CallCardPresenter presenter = new CallCardPresenter();
71        presenter.init(getContext(), null);
72
73        assertEquals(NAME_PRIMARY, presenter.getNameForCall(mUnlockedContactInfo));
74    }
75
76    public void testGetNameForCall_Locked() {
77        ContactsPreferencesFactory.setTestInstance(null);
78        CallCardPresenter presenter = new CallCardPresenter();
79        presenter.init(getContext(), null);
80
81        assertEquals(NUMBER, presenter.getNameForCall(mLockedContactInfo));
82    }
83
84    public void testGetNameForCall_EmptyPreferredName() {
85        ContactCacheEntry contactInfo = new ContactCacheEntry();
86        contactInfo.number = NUMBER;
87
88        ContactsPreferencesFactory.setTestInstance(null);
89        CallCardPresenter presenter = new CallCardPresenter();
90        presenter.init(getContext(), null);
91
92        assertEquals(NUMBER, presenter.getNameForCall(contactInfo));
93    }
94
95    public void testGetNumberForCall_Unlocked() {
96        ContactsPreferencesFactory.setTestInstance(mContactsPreferences);
97        CallCardPresenter presenter = new CallCardPresenter();
98        presenter.init(getContext(), null);
99
100        assertEquals(NUMBER, presenter.getNumberForCall(mUnlockedContactInfo));
101    }
102
103    public void testGetNumberForCall_Locked() {
104        ContactsPreferencesFactory.setTestInstance(null);
105        CallCardPresenter presenter = new CallCardPresenter();
106        presenter.init(getContext(), null);
107
108        assertEquals(LOCATION, presenter.getNumberForCall(mLockedContactInfo));
109    }
110
111    public void testGetNumberForCall_EmptyPreferredName() {
112        ContactCacheEntry contactInfo = new ContactCacheEntry();
113        contactInfo.location = LOCATION;
114
115        ContactsPreferencesFactory.setTestInstance(null);
116        CallCardPresenter presenter = new CallCardPresenter();
117        presenter.init(getContext(), null);
118
119        assertEquals(LOCATION, presenter.getNumberForCall(contactInfo));
120    }
121}
122