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 package com.android.contacts.util;
17 
18 import android.graphics.Canvas;
19 import android.graphics.ColorFilter;
20 import android.graphics.PixelFormat;
21 import android.graphics.drawable.Drawable;
22 import android.test.AndroidTestCase;
23 
24 import androidx.test.filters.SmallTest;
25 
26 import com.android.contacts.model.AccountTypeManager;
27 import com.android.contacts.model.account.AccountDisplayInfo;
28 import com.android.contacts.model.account.AccountDisplayInfoFactory;
29 import com.android.contacts.model.account.AccountType;
30 import com.android.contacts.model.account.AccountWithDataSet;
31 import com.android.contacts.test.mocks.MockAccountTypeManager;
32 import com.android.contacts.tests.FakeAccountType;
33 import com.android.contacts.tests.FakeDeviceAccountTypeFactory;
34 
35 import java.util.ArrayList;
36 import java.util.HashMap;
37 import java.util.Map;
38 
39 @SmallTest
40 public class AccountDisplayInfoFactoryTests extends AndroidTestCase {
41 
42     private Map<AccountWithDataSet, AccountType> mKnownTypes;
43 
44     @Override
setUp()45     protected void setUp() throws Exception {
46         super.setUp();
47         mKnownTypes = new HashMap<>();
48     }
49 
test_displayableAccount_hasIconFromAccountType()50     public void test_displayableAccount_hasIconFromAccountType() {
51         final Drawable comExampleIcon = someDrawable();
52 
53         addTypeMapping(account("user", "com.example"), "title", comExampleIcon);
54         addTypeMapping(account(null, null), "device", someDrawable());
55         addTypeMapping(account("foo", "bar.type"), "bar", someDrawable());
56         addTypeMapping(account("user2", "com.example"), "title", comExampleIcon);
57 
58         final AccountDisplayInfoFactory sut = createFactoryForKnownTypes();
59 
60         final AccountDisplayInfo displayable = sut.getAccountDisplayInfo(
61                 account("user", "com.example"));
62         assertEquals(comExampleIcon, displayable.getIcon());
63     }
64 
test_displayableAccount_hasNameFromAccount()65     public void test_displayableAccount_hasNameFromAccount() {
66         final Drawable comExampleIcon = someDrawable();
67 
68         addTypeMapping(account("user@example.com", "com.example"), "title", comExampleIcon);
69         addTypeMapping(account(null, null), "device", someDrawable());
70         addTypeMapping(account("foo", "bar.type"), "bar", someDrawable());
71         addTypeMapping(account("user2@example.com", "com.example"), "title", comExampleIcon);
72 
73         final AccountDisplayInfoFactory sut = createFactoryForKnownTypes();
74 
75         final AccountDisplayInfo displayable = sut.getAccountDisplayInfo(
76                 account("user@example.com", "com.example"));
77         assertEquals("user@example.com", displayable.getNameLabel());
78     }
79 
test_displayableAccountForNullAccount_hasNameFromAccountType()80     public void test_displayableAccountForNullAccount_hasNameFromAccountType() {
81         addSomeKnownAccounts();
82         addTypeMapping(account(null, null), "Device Display Label", someDrawable());
83 
84         final AccountDisplayInfoFactory sut = createFactoryForKnownTypes();
85 
86         final AccountDisplayInfo displayable = sut.getAccountDisplayInfo(
87                 account(null, null));
88         assertEquals("Device Display Label", displayable.getNameLabel());
89     }
90 
test_displayableAccountForDeviceAccount_hasNameFromAccountType()91     public void test_displayableAccountForDeviceAccount_hasNameFromAccountType() {
92         addSomeKnownAccounts();
93         addTypeMapping(account("some.device.account.name", "device.account.type"), "Device Label",
94                 someDrawable());
95 
96         final AccountDisplayInfoFactory sut = createFactoryForKnownTypes(
97                 new FakeDeviceAccountTypeFactory().withDeviceTypes("device.account.type"));
98 
99         final AccountDisplayInfo displayable = sut.getAccountDisplayInfo(
100                 account("some.device.account.name", "device.account.type"));
101         assertEquals("Device Label", displayable.getNameLabel());
102     }
103 
test_displayableAccountForDeviceAccountWhenMultiple_hasNameFromAccount()104     public void test_displayableAccountForDeviceAccountWhenMultiple_hasNameFromAccount() {
105         addSomeKnownAccounts();
106         addTypeMapping(account("first.device.account.name", "a.device.account.type"),
107                 "Device Display Label", someDrawable());
108         addTypeMapping(account("second.device.account.name", "b.device.account.type"),
109                 "Device Display Label", someDrawable());
110         addTypeMapping(account("another.device.account.name", "a.device.account.type"),
111                 "Device Display Label", someDrawable());
112 
113         final AccountDisplayInfoFactory sut = createFactoryForKnownTypes(
114                 new FakeDeviceAccountTypeFactory().withDeviceTypes("a.device.account.type",
115                         "b.device.account.type"));
116 
117         final AccountDisplayInfo displayable = sut.getAccountDisplayInfo(
118                 account("first.device.account.name", "a.device.account.type"));
119         assertEquals("first.device.account.name", displayable.getNameLabel());
120 
121         final AccountDisplayInfo displayable2 = sut.getAccountDisplayInfo(
122                 account("second.device.account.name", "b.device.account.type"));
123         assertEquals("second.device.account.name", displayable2.getNameLabel());
124     }
125 
test_displayableAccountForSimAccount_hasNameFromAccountType()126     public void test_displayableAccountForSimAccount_hasNameFromAccountType() {
127         addSomeKnownAccounts();
128         addTypeMapping(account("sim.account.name", "sim.account.type"), "SIM", someDrawable());
129 
130         final AccountDisplayInfoFactory sut = createFactoryForKnownTypes(
131                 new FakeDeviceAccountTypeFactory().withSimTypes("sim.account.type"));
132 
133         final AccountDisplayInfo displayable = sut.getAccountDisplayInfo(
134                 account("sim.account.name", "sim.account.type"));
135         assertEquals("SIM", displayable.getNameLabel());
136     }
137 
test_displayableAccountForSimAccountWhenMultiple_hasNameFromAccount()138     public void test_displayableAccountForSimAccountWhenMultiple_hasNameFromAccount() {
139         addSomeKnownAccounts();
140         addTypeMapping(account("sim.account.name", "sim.account.type"), "SIM", someDrawable());
141         addTypeMapping(account("sim2.account.name", "sim.account.type"), "SIM", someDrawable());
142 
143         final AccountDisplayInfoFactory sut = createFactoryForKnownTypes(
144                 new FakeDeviceAccountTypeFactory().withSimTypes("sim.account.type"));
145 
146         final AccountDisplayInfo displayable = sut.getAccountDisplayInfo(
147                 account("sim.account.name", "sim.account.type"));
148         assertEquals("sim.account.name", displayable.getNameLabel());
149     }
150 
addSomeKnownAccounts()151     private void addSomeKnownAccounts() {
152         final Drawable comExampleIcon = someDrawable();
153         addTypeMapping(account("user@example.com", "com.example"), "Example Title", comExampleIcon);
154         addTypeMapping(account("foo", "bar.type"), "Bar", someDrawable());
155         addTypeMapping(account("user2@example.com", "com.example"), "Example Title", comExampleIcon);
156         addTypeMapping(account("user", "com.example.two"), "Some Account", someDrawable());
157     }
158 
createFactoryForKnownTypes()159     private AccountDisplayInfoFactory createFactoryForKnownTypes() {
160         return createFactoryForKnownTypes(new DeviceLocalAccountTypeFactory.Default(getContext()));
161     }
162 
createFactoryForKnownTypes(DeviceLocalAccountTypeFactory typeFactory)163     private AccountDisplayInfoFactory createFactoryForKnownTypes(DeviceLocalAccountTypeFactory
164             typeFactory) {
165         return new AccountDisplayInfoFactory(getContext(),
166                 createFakeAccountTypeManager(mKnownTypes), typeFactory,
167                 new ArrayList<>(mKnownTypes.keySet()));
168     }
169 
account(String name, String type)170     private AccountWithDataSet account(String name, String type) {
171         return new AccountWithDataSet(name, type, /* dataSet */ null);
172     }
173 
addTypeMapping(AccountWithDataSet account, String label, Drawable icon)174     private void addTypeMapping(AccountWithDataSet account, String label, Drawable icon) {
175         mKnownTypes.put(account, FakeAccountType.create(account, label, icon));
176     }
177 
createFakeAccountTypeManager( final Map<AccountWithDataSet, AccountType> mapping)178     private AccountTypeManager createFakeAccountTypeManager(
179             final Map<AccountWithDataSet, AccountType> mapping) {
180         return new MockAccountTypeManager(mapping.values().toArray(new AccountType[mapping.size()]),
181                 mapping.keySet().toArray(new AccountWithDataSet[mapping.size()]));
182     }
183 
someDrawable()184     private Drawable someDrawable() {
185         return new Drawable() {
186             @Override
187             public void draw(Canvas canvas) {
188             }
189 
190             @Override
191             public void setAlpha(int i) {
192             }
193 
194             @Override
195             public void setColorFilter(ColorFilter colorFilter) {
196             }
197 
198             @Override
199             public int getOpacity() {
200                 return PixelFormat.OPAQUE;
201             }
202         };
203     }
204 
205 }
206