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