blob: cef74ae39c60fe014ff23141af5cfadae8c0fa98 [file] [log] [blame]
Makoto Onukiff329c42017-01-05 11:19:56 -08001/*
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.shell;
17
18import static com.android.shell.BugreportProgressService.findSendToAccount;
19
20import static junit.framework.Assert.assertEquals;
21import static junit.framework.Assert.assertNull;
22
23import static org.mockito.Matchers.anyInt;
24import static org.mockito.Matchers.eq;
25import static org.mockito.Mockito.when;
26
27import android.accounts.Account;
28import android.accounts.AccountManager;
29import android.content.Context;
30import android.os.UserHandle;
31import android.os.UserManager;
32import android.support.test.filters.SmallTest;
33import android.support.test.runner.AndroidJUnit4;
34import android.test.mock.MockContext;
35import android.util.Pair;
36
37import org.junit.Before;
38import org.junit.Test;
39import org.junit.runner.RunWith;
40import org.mockito.Mock;
41import org.mockito.MockitoAnnotations;
42
43import java.util.Arrays;
44import java.util.List;
45
46/**
47 * Test for {@link BugreportProgressServiceTest}.
48 *
49 * Usage:
50 adb shell am instrument -w \
51 -e class com.android.shell.BugreportProgressServiceTest \
52 com.android.shell.tests
53 */
54@SmallTest
55@RunWith(AndroidJUnit4.class)
56public class BugreportProgressServiceTest {
57 private static class MyContext extends MockContext {
58 @Mock
59 public UserManager userManager;
60
61 @Mock
62 public AccountManager accountManager;
63
64 public MyContext() {
65 MockitoAnnotations.initMocks(this);
66 }
67
68 @Override
69 public Object getSystemService(String name) {
70 switch (name) {
71 case Context.USER_SERVICE:
72 return userManager;
73 case Context.ACCOUNT_SERVICE:
74 return accountManager;
75 default:
76 return super.getSystemService(name);
77 }
78 }
79
80 @Override
81 public String getSystemServiceName(Class<?> serviceClass) {
82 if (UserManager.class.equals(serviceClass)) {
83 return Context.USER_SERVICE;
84 }
85 if (AccountManager.class.equals(serviceClass)) {
86 return Context.ACCOUNT_SERVICE;
87 }
88 return super.getSystemServiceName(serviceClass);
89 }
90 }
91
92 private final MyContext mTestContext = new MyContext();
93
94 private static <T> List<T> list(T... array) {
95 return Arrays.asList(array);
96 }
97
98 private static <T> T[] array(T... array) {
99 return array;
100 }
101
102 private Account account(String email) {
103 return new Account(email, "test.com");
104 }
105
106 private void checkFindSendToAccount(
107 int expectedUserId, String expectedEmail, String preferredDomain) {
108 final Pair<UserHandle, Account> actual = findSendToAccount(mTestContext, preferredDomain);
109 assertEquals(UserHandle.of(expectedUserId), actual.first);
110 assertEquals(account(expectedEmail), actual.second);
111 }
112
113 @Test
114 public void findSendToAccount_noWorkProfile() {
115 when(mTestContext.userManager.getUserProfiles()).thenReturn(
116 list(UserHandle.of(UserHandle.USER_SYSTEM)));
117
118 // No accounts.
119 when(mTestContext.accountManager.getAccountsAsUser(eq(UserHandle.USER_SYSTEM))).thenReturn(
120 array());
121
122 assertNull(findSendToAccount(mTestContext, null));
123 assertNull(findSendToAccount(mTestContext, ""));
124 assertNull(findSendToAccount(mTestContext, "android.com"));
125 assertNull(findSendToAccount(mTestContext, "@android.com"));
126
127 // 1 account
128 when(mTestContext.accountManager.getAccountsAsUser(eq(UserHandle.USER_SYSTEM))).thenReturn(
129 array(account("abc@gmail.com")));
130
131 checkFindSendToAccount(0, "abc@gmail.com", null);
132 checkFindSendToAccount(0, "abc@gmail.com", "");
133 checkFindSendToAccount(0, "abc@gmail.com", "android.com");
134 checkFindSendToAccount(0, "abc@gmail.com", "@android.com");
135 checkFindSendToAccount(0, "abc@gmail.com", "gmail.com");
136 checkFindSendToAccount(0, "abc@gmail.com", "@gmail.com");
137
138 // 2 accounts, same domain
139 when(mTestContext.accountManager.getAccountsAsUser(eq(UserHandle.USER_SYSTEM))).thenReturn(
140 array(account("abc@gmail.com"), account("def@gmail.com")));
141
142 checkFindSendToAccount(0, "abc@gmail.com", null);
143 checkFindSendToAccount(0, "abc@gmail.com", "");
144 checkFindSendToAccount(0, "abc@gmail.com", "android.com");
145 checkFindSendToAccount(0, "abc@gmail.com", "@android.com");
146 checkFindSendToAccount(0, "abc@gmail.com", "gmail.com");
147 checkFindSendToAccount(0, "abc@gmail.com", "@gmail.com");
148
149 // 2 accounts, different domains
150 when(mTestContext.accountManager.getAccountsAsUser(eq(UserHandle.USER_SYSTEM))).thenReturn(
151 array(account("abc@gmail.com"), account("def@android.com")));
152
153 checkFindSendToAccount(0, "abc@gmail.com", null);
154 checkFindSendToAccount(0, "abc@gmail.com", "");
155 checkFindSendToAccount(0, "def@android.com", "android.com");
156 checkFindSendToAccount(0, "def@android.com", "@android.com");
157 checkFindSendToAccount(0, "abc@gmail.com", "gmail.com");
158 checkFindSendToAccount(0, "abc@gmail.com", "@gmail.com");
159
160 // Plut an account that doesn't look like an email address.
161 when(mTestContext.accountManager.getAccountsAsUser(eq(UserHandle.USER_SYSTEM))).thenReturn(
162 array(account("notemail"), account("abc@gmail.com"), account("def@android.com")));
163
164 checkFindSendToAccount(0, "abc@gmail.com", null);
165 checkFindSendToAccount(0, "abc@gmail.com", "");
166 checkFindSendToAccount(0, "def@android.com", "android.com");
167 checkFindSendToAccount(0, "def@android.com", "@android.com");
168 checkFindSendToAccount(0, "abc@gmail.com", "gmail.com");
169 checkFindSendToAccount(0, "abc@gmail.com", "@gmail.com");
170 }
171
172 /**
173 * Same as {@link #findSendToAccount_noWorkProfile()}, but with work profile, which has no
174 * accounts. The expected results are the same as the original.
175 */
176 @Test
177 public void findSendToAccount_withWorkProfile_noAccounts() {
178 when(mTestContext.userManager.getUserProfiles()).thenReturn(
179 list(UserHandle.of(UserHandle.USER_SYSTEM), UserHandle.of(10)));
180
181 // Work profile has no accounts
182 when(mTestContext.accountManager.getAccountsAsUser(eq(10))).thenReturn(
183 array());
184
185 // No accounts.
186 when(mTestContext.accountManager.getAccountsAsUser(eq(UserHandle.USER_SYSTEM))).thenReturn(
187 array());
188
189 assertNull(findSendToAccount(mTestContext, null));
190 assertNull(findSendToAccount(mTestContext, ""));
191 assertNull(findSendToAccount(mTestContext, "android.com"));
192 assertNull(findSendToAccount(mTestContext, "@android.com"));
193
194 // 1 account
195 when(mTestContext.accountManager.getAccountsAsUser(eq(UserHandle.USER_SYSTEM))).thenReturn(
196 array(account("abc@gmail.com")));
197
198 checkFindSendToAccount(0, "abc@gmail.com", null);
199 checkFindSendToAccount(0, "abc@gmail.com", "");
200 checkFindSendToAccount(0, "abc@gmail.com", "android.com");
201 checkFindSendToAccount(0, "abc@gmail.com", "@android.com");
202 checkFindSendToAccount(0, "abc@gmail.com", "gmail.com");
203 checkFindSendToAccount(0, "abc@gmail.com", "@gmail.com");
204
205 // 2 accounts, same domain
206 when(mTestContext.accountManager.getAccountsAsUser(eq(UserHandle.USER_SYSTEM))).thenReturn(
207 array(account("abc@gmail.com"), account("def@gmail.com")));
208
209 checkFindSendToAccount(0, "abc@gmail.com", null);
210 checkFindSendToAccount(0, "abc@gmail.com", "");
211 checkFindSendToAccount(0, "abc@gmail.com", "android.com");
212 checkFindSendToAccount(0, "abc@gmail.com", "@android.com");
213 checkFindSendToAccount(0, "abc@gmail.com", "gmail.com");
214 checkFindSendToAccount(0, "abc@gmail.com", "@gmail.com");
215
216 // 2 accounts, different domains
217 when(mTestContext.accountManager.getAccountsAsUser(eq(UserHandle.USER_SYSTEM))).thenReturn(
218 array(account("abc@gmail.com"), account("def@android.com")));
219
220 checkFindSendToAccount(0, "abc@gmail.com", null);
221 checkFindSendToAccount(0, "abc@gmail.com", "");
222 checkFindSendToAccount(0, "def@android.com", "android.com");
223 checkFindSendToAccount(0, "def@android.com", "@android.com");
224 checkFindSendToAccount(0, "abc@gmail.com", "gmail.com");
225 checkFindSendToAccount(0, "abc@gmail.com", "@gmail.com");
226
227 // Plut an account that doesn't look like an email address.
228 when(mTestContext.accountManager.getAccountsAsUser(eq(UserHandle.USER_SYSTEM))).thenReturn(
229 array(account("notemail"), account("abc@gmail.com"), account("def@android.com")));
230
231 checkFindSendToAccount(0, "abc@gmail.com", null);
232 checkFindSendToAccount(0, "abc@gmail.com", "");
233 checkFindSendToAccount(0, "def@android.com", "android.com");
234 checkFindSendToAccount(0, "def@android.com", "@android.com");
235 checkFindSendToAccount(0, "abc@gmail.com", "gmail.com");
236 checkFindSendToAccount(0, "abc@gmail.com", "@gmail.com");
237 }
238
239 /**
240 * Same as {@link #findSendToAccount_noWorkProfile()}, but with work profile, which has
241 * 1 account. The expected results are the same as the original, expect for the "no accounts
242 * on the primary profile" case.
243 */
244 @Test
245 public void findSendToAccount_withWorkProfile_1account() {
246 when(mTestContext.userManager.getUserProfiles()).thenReturn(
247 list(UserHandle.of(UserHandle.USER_SYSTEM), UserHandle.of(10)));
248
249 // Work profile has no accounts
250 when(mTestContext.accountManager.getAccountsAsUser(eq(10))).thenReturn(
251 array(account("xyz@gmail.com")));
252
253 // No accounts.
254 when(mTestContext.accountManager.getAccountsAsUser(eq(UserHandle.USER_SYSTEM))).thenReturn(
255 array());
256
257 checkFindSendToAccount(10, "xyz@gmail.com", null);
258 checkFindSendToAccount(10, "xyz@gmail.com", "");
259 checkFindSendToAccount(10, "xyz@gmail.com", "android.com");
260 checkFindSendToAccount(10, "xyz@gmail.com", "@android.com");
261
262 // 1 account
263 when(mTestContext.accountManager.getAccountsAsUser(eq(UserHandle.USER_SYSTEM))).thenReturn(
264 array(account("abc@gmail.com")));
265
266 checkFindSendToAccount(0, "abc@gmail.com", null);
267 checkFindSendToAccount(0, "abc@gmail.com", "");
268 checkFindSendToAccount(0, "abc@gmail.com", "android.com");
269 checkFindSendToAccount(0, "abc@gmail.com", "@android.com");
270 checkFindSendToAccount(0, "abc@gmail.com", "gmail.com");
271 checkFindSendToAccount(0, "abc@gmail.com", "@gmail.com");
272
273 // 2 accounts, same domain
274 when(mTestContext.accountManager.getAccountsAsUser(eq(UserHandle.USER_SYSTEM))).thenReturn(
275 array(account("abc@gmail.com"), account("def@gmail.com")));
276
277 checkFindSendToAccount(0, "abc@gmail.com", null);
278 checkFindSendToAccount(0, "abc@gmail.com", "");
279 checkFindSendToAccount(0, "abc@gmail.com", "android.com");
280 checkFindSendToAccount(0, "abc@gmail.com", "@android.com");
281 checkFindSendToAccount(0, "abc@gmail.com", "gmail.com");
282 checkFindSendToAccount(0, "abc@gmail.com", "@gmail.com");
283
284 // 2 accounts, different domains
285 when(mTestContext.accountManager.getAccountsAsUser(eq(UserHandle.USER_SYSTEM))).thenReturn(
286 array(account("abc@gmail.com"), account("def@android.com")));
287
288 checkFindSendToAccount(0, "abc@gmail.com", null);
289 checkFindSendToAccount(0, "abc@gmail.com", "");
290 checkFindSendToAccount(0, "def@android.com", "android.com");
291 checkFindSendToAccount(0, "def@android.com", "@android.com");
292 checkFindSendToAccount(0, "abc@gmail.com", "gmail.com");
293 checkFindSendToAccount(0, "abc@gmail.com", "@gmail.com");
294
295 // Plut an account that doesn't look like an email address.
296 when(mTestContext.accountManager.getAccountsAsUser(eq(UserHandle.USER_SYSTEM))).thenReturn(
297 array(account("notemail"), account("abc@gmail.com"), account("def@android.com")));
298
299 checkFindSendToAccount(0, "abc@gmail.com", null);
300 checkFindSendToAccount(0, "abc@gmail.com", "");
301 checkFindSendToAccount(0, "def@android.com", "android.com");
302 checkFindSendToAccount(0, "def@android.com", "@android.com");
303 checkFindSendToAccount(0, "abc@gmail.com", "gmail.com");
304 checkFindSendToAccount(0, "abc@gmail.com", "@gmail.com");
305 }
306
307 /**
308 * Same as {@link #findSendToAccount_noWorkProfile()}, but with work profile, with mixed
309 * domains.
310 */
311 @Test
312 public void findSendToAccount_withWorkProfile_mixedDomains() {
313 when(mTestContext.userManager.getUserProfiles()).thenReturn(
314 list(UserHandle.of(UserHandle.USER_SYSTEM), UserHandle.of(10)));
315
316 // Work profile has no accounts
317 when(mTestContext.accountManager.getAccountsAsUser(eq(10))).thenReturn(
318 array(account("xyz@android.com")));
319
320 // No accounts.
321 when(mTestContext.accountManager.getAccountsAsUser(eq(UserHandle.USER_SYSTEM))).thenReturn(
322 array());
323
324 checkFindSendToAccount(10, "xyz@android.com", null);
325 checkFindSendToAccount(10, "xyz@android.com", "");
326 checkFindSendToAccount(10, "xyz@android.com", "android.com");
327 checkFindSendToAccount(10, "xyz@android.com", "@android.com");
328
329 // 1 account
330 when(mTestContext.accountManager.getAccountsAsUser(eq(UserHandle.USER_SYSTEM))).thenReturn(
331 array(account("abc@gmail.com")));
332
333 checkFindSendToAccount(0, "abc@gmail.com", null);
334 checkFindSendToAccount(0, "abc@gmail.com", "");
335 checkFindSendToAccount(10, "xyz@android.com", "android.com");
336 checkFindSendToAccount(10, "xyz@android.com", "@android.com");
337 checkFindSendToAccount(0, "abc@gmail.com", "gmail.com");
338 checkFindSendToAccount(0, "abc@gmail.com", "@gmail.com");
339
340 // more accounts.
341 when(mTestContext.accountManager.getAccountsAsUser(eq(UserHandle.USER_SYSTEM))).thenReturn(
342 array(account("abc@gmail.com"), account("def@gmail.com")));
343
344 checkFindSendToAccount(0, "abc@gmail.com", null);
345 checkFindSendToAccount(0, "abc@gmail.com", "");
346 checkFindSendToAccount(10, "xyz@android.com", "android.com");
347 checkFindSendToAccount(10, "xyz@android.com", "@android.com");
348 checkFindSendToAccount(0, "abc@gmail.com", "gmail.com");
349 checkFindSendToAccount(0, "abc@gmail.com", "@gmail.com");
350 }
351}