blob: 8cf146ea801d2609d0d92e9694ca9b7ff144f905 [file] [log] [blame]
Tony Mak96d78f52017-05-03 18:53:21 +01001/*
2 * Copyright (C) 2017 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.internal.app;
18
Tony Mak96d78f52017-05-03 18:53:21 +010019import static junit.framework.Assert.assertEquals;
20import static junit.framework.Assert.assertNotNull;
21import static junit.framework.Assert.assertNull;
Alison Cichowlas3e340502018-08-07 17:15:01 -040022
Tony Mak96d78f52017-05-03 18:53:21 +010023import static org.mockito.ArgumentMatchers.any;
24import static org.mockito.ArgumentMatchers.anyInt;
25import static org.mockito.ArgumentMatchers.anyString;
26import static org.mockito.ArgumentMatchers.eq;
27import static org.mockito.ArgumentMatchers.nullable;
Susi Kharraz-Post0446fab2019-02-21 09:42:31 -050028import static org.mockito.Mockito.mock;
arangelov64439c1e2018-07-31 14:18:47 +010029import static org.mockito.Mockito.never;
30import static org.mockito.Mockito.spy;
Tony Mak96d78f52017-05-03 18:53:21 +010031import static org.mockito.Mockito.verify;
32import static org.mockito.Mockito.when;
33
arangelov64439c1e2018-07-31 14:18:47 +010034import android.annotation.Nullable;
35import android.content.ComponentName;
36import android.content.Context;
37import android.content.Intent;
38import android.content.pm.ActivityInfo;
39import android.content.pm.ApplicationInfo;
40import android.content.pm.IPackageManager;
41import android.content.pm.PackageManager;
42import android.content.pm.ResolveInfo;
43import android.content.pm.UserInfo;
Susi Kharraz-Post0446fab2019-02-21 09:42:31 -050044import android.metrics.LogMaker;
arangelov64439c1e2018-07-31 14:18:47 +010045import android.net.Uri;
46import android.os.Bundle;
Alison Cichowlas3e340502018-08-07 17:15:01 -040047import android.os.IBinder;
arangelov64439c1e2018-07-31 14:18:47 +010048import android.os.RemoteException;
49import android.os.UserHandle;
50import android.os.UserManager;
Eric Sandnesse89d0e12020-03-13 17:55:51 +000051import android.provider.Settings;
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090052
53import androidx.test.InstrumentationRegistry;
54import androidx.test.rule.ActivityTestRule;
55import androidx.test.runner.AndroidJUnit4;
Alison Cichowlas3e340502018-08-07 17:15:01 -040056
Susi Kharraz-Post0446fab2019-02-21 09:42:31 -050057import com.android.internal.logging.MetricsLogger;
58import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
59
Eric Sandnesse89d0e12020-03-13 17:55:51 +000060import org.junit.After;
arangelov64439c1e2018-07-31 14:18:47 +010061import org.junit.Before;
62import org.junit.Rule;
63import org.junit.Test;
64import org.junit.runner.RunWith;
65import org.mockito.ArgumentCaptor;
66import org.mockito.Mock;
67import org.mockito.MockitoAnnotations;
68
Alison Cichowlas3e340502018-08-07 17:15:01 -040069import java.util.ArrayList;
70import java.util.List;
71
Tony Mak96d78f52017-05-03 18:53:21 +010072@RunWith(AndroidJUnit4.class)
73public class IntentForwarderActivityTest {
arangelov64439c1e2018-07-31 14:18:47 +010074
Tony Mak96d78f52017-05-03 18:53:21 +010075 private static final ComponentName FORWARD_TO_MANAGED_PROFILE_COMPONENT_NAME =
76 new ComponentName(
77 "android",
78 IntentForwarderActivity.FORWARD_INTENT_TO_MANAGED_PROFILE
79 );
Susi Kharraz-Post0446fab2019-02-21 09:42:31 -050080 private static final ComponentName FORWARD_TO_PARENT_COMPONENT_NAME =
81 new ComponentName(
82 "android",
83 IntentForwarderActivity.FORWARD_INTENT_TO_PARENT
84 );
Tony Mak96d78f52017-05-03 18:53:21 +010085 private static final String TYPE_PLAIN_TEXT = "text/plain";
86
87 private static UserInfo MANAGED_PROFILE_INFO = new UserInfo();
arangelov38303742018-08-14 20:14:12 +010088
Tony Mak96d78f52017-05-03 18:53:21 +010089 static {
90 MANAGED_PROFILE_INFO.id = 10;
91 MANAGED_PROFILE_INFO.flags = UserInfo.FLAG_MANAGED_PROFILE;
Eric Sandnesse89d0e12020-03-13 17:55:51 +000092 MANAGED_PROFILE_INFO.userType = UserManager.USER_TYPE_PROFILE_MANAGED;
Tony Mak96d78f52017-05-03 18:53:21 +010093 }
94
95 private static UserInfo CURRENT_USER_INFO = new UserInfo();
arangelov38303742018-08-14 20:14:12 +010096
Tony Mak96d78f52017-05-03 18:53:21 +010097 static {
98 CURRENT_USER_INFO.id = UserHandle.myUserId();
99 CURRENT_USER_INFO.flags = 0;
100 }
101
102 private static IntentForwarderActivity.Injector sInjector;
103 private static ComponentName sComponentName;
arangelov64439c1e2018-07-31 14:18:47 +0100104 private static String sActivityName;
105 private static String sPackageName;
Tony Mak96d78f52017-05-03 18:53:21 +0100106
arangelov38303742018-08-14 20:14:12 +0100107 @Mock
108 private IPackageManager mIPm;
109 @Mock
110 private PackageManager mPm;
111 @Mock
112 private UserManager mUserManager;
113 @Mock
114 private ApplicationInfo mApplicationInfo;
Tony Mak96d78f52017-05-03 18:53:21 +0100115
116 @Rule
117 public ActivityTestRule<IntentForwarderWrapperActivity> mActivityRule =
118 new ActivityTestRule<>(IntentForwarderWrapperActivity.class, true, false);
119
120 private Context mContext;
arangelov64439c1e2018-07-31 14:18:47 +0100121 public static final String PHONE_NUMBER = "123-456-789";
Eric Sandnesse89d0e12020-03-13 17:55:51 +0000122 private int mDeviceProvisionedInitialValue;
Tony Mak96d78f52017-05-03 18:53:21 +0100123
124 @Before
125 public void setup() {
126 MockitoAnnotations.initMocks(this);
127 mContext = InstrumentationRegistry.getTargetContext();
arangelov64439c1e2018-07-31 14:18:47 +0100128 sInjector = spy(new TestInjector());
Eric Sandnesse89d0e12020-03-13 17:55:51 +0000129 mDeviceProvisionedInitialValue = Settings.Global.getInt(mContext.getContentResolver(),
130 Settings.Global.DEVICE_PROVISIONED, /* def= */ 0);
131 }
132
133 @After
134 public void tearDown() {
135 Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.DEVICE_PROVISIONED,
136 mDeviceProvisionedInitialValue);
Tony Mak96d78f52017-05-03 18:53:21 +0100137 }
138
139 @Test
140 public void forwardToManagedProfile_canForward_sendIntent() throws Exception {
141 sComponentName = FORWARD_TO_MANAGED_PROFILE_COMPONENT_NAME;
142
143 // Intent can be forwarded.
144 when(mIPm.canForwardTo(
145 any(Intent.class), nullable(String.class), anyInt(), anyInt())).thenReturn(true);
146
147 // Managed profile exists.
148 List<UserInfo> profiles = new ArrayList<>();
149 profiles.add(CURRENT_USER_INFO);
150 profiles.add(MANAGED_PROFILE_INFO);
151 when(mUserManager.getProfiles(anyInt())).thenReturn(profiles);
152
153 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class);
154 intent.setAction(Intent.ACTION_SEND);
155 intent.setType(TYPE_PLAIN_TEXT);
156 IntentForwarderWrapperActivity activity = mActivityRule.launchActivity(intent);
157
158 ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
159 verify(mIPm).canForwardTo(intentCaptor.capture(), eq(TYPE_PLAIN_TEXT), anyInt(), anyInt());
160 assertEquals(Intent.ACTION_SEND, intentCaptor.getValue().getAction());
161
162 assertEquals(Intent.ACTION_SEND, intentCaptor.getValue().getAction());
163 assertNotNull(activity.mStartActivityIntent);
164 assertEquals(Intent.ACTION_SEND, activity.mStartActivityIntent.getAction());
165 assertNull(activity.mStartActivityIntent.getPackage());
166 assertNull(activity.mStartActivityIntent.getComponent());
167 assertEquals(CURRENT_USER_INFO.id, activity.mStartActivityIntent.getContentUserHint());
168
169 assertEquals(MANAGED_PROFILE_INFO.id, activity.mUserIdActivityLaunchedIn);
170 }
171
172 @Test
173 public void forwardToManagedProfile_cannotForward_sendIntent() throws Exception {
174 sComponentName = FORWARD_TO_MANAGED_PROFILE_COMPONENT_NAME;
175
176 // Intent cannot be forwarded.
177 when(mIPm.canForwardTo(
178 any(Intent.class), nullable(String.class), anyInt(), anyInt())).thenReturn(false);
179
180 // Managed profile exists.
181 List<UserInfo> profiles = new ArrayList<>();
182 profiles.add(CURRENT_USER_INFO);
183 profiles.add(MANAGED_PROFILE_INFO);
184 when(mUserManager.getProfiles(anyInt())).thenReturn(profiles);
185
186 // Create ACTION_SEND intent.
187 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class);
188 intent.setAction(Intent.ACTION_SEND);
189 IntentForwarderWrapperActivity activity = mActivityRule.launchActivity(intent);
190
191 assertNull(activity.mStartActivityIntent);
192 }
193
194 @Test
195 public void forwardToManagedProfile_noManagedProfile_sendIntent() throws Exception {
196 sComponentName = FORWARD_TO_MANAGED_PROFILE_COMPONENT_NAME;
197
198 // Intent can be forwarded.
199 when(mIPm.canForwardTo(
200 any(Intent.class), anyString(), anyInt(), anyInt())).thenReturn(true);
201
202 // Managed profile does not exist.
203 List<UserInfo> profiles = new ArrayList<>();
204 profiles.add(CURRENT_USER_INFO);
205 when(mUserManager.getProfiles(anyInt())).thenReturn(profiles);
206
207 // Create ACTION_SEND intent.
208 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class);
209 intent.setAction(Intent.ACTION_SEND);
210 IntentForwarderWrapperActivity activity = mActivityRule.launchActivity(intent);
211
212 assertNull(activity.mStartActivityIntent);
213 }
214
215 @Test
216 public void forwardToManagedProfile_canForward_chooserIntent() throws Exception {
217 sComponentName = FORWARD_TO_MANAGED_PROFILE_COMPONENT_NAME;
218
219 // Intent can be forwarded.
220 when(mIPm.canForwardTo(
221 any(Intent.class), nullable(String.class), anyInt(), anyInt())).thenReturn(true);
222
223 // Manage profile exists.
224 List<UserInfo> profiles = new ArrayList<>();
225 profiles.add(CURRENT_USER_INFO);
226 profiles.add(MANAGED_PROFILE_INFO);
227 when(mUserManager.getProfiles(anyInt())).thenReturn(profiles);
228
229 // Create chooser Intent
230 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class);
231 intent.setAction(Intent.ACTION_CHOOSER);
232 Intent sendIntent = new Intent(Intent.ACTION_SEND);
233 sendIntent.setComponent(new ComponentName("xx", "yyy"));
234 sendIntent.setType(TYPE_PLAIN_TEXT);
235 intent.putExtra(Intent.EXTRA_INTENT, sendIntent);
236 IntentForwarderWrapperActivity activity = mActivityRule.launchActivity(intent);
237
238 ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
239 verify(mIPm).canForwardTo(intentCaptor.capture(), eq(TYPE_PLAIN_TEXT), anyInt(), anyInt());
240 assertEquals(Intent.ACTION_SEND, intentCaptor.getValue().getAction());
241
242 assertNotNull(activity.mStartActivityIntent);
243 assertEquals(Intent.ACTION_CHOOSER, activity.mStartActivityIntent.getAction());
244 assertNull(activity.mStartActivityIntent.getPackage());
245 assertNull(activity.mStartActivityIntent.getComponent());
246
247 Intent innerIntent = activity.mStartActivityIntent.getParcelableExtra(Intent.EXTRA_INTENT);
248 assertNotNull(innerIntent);
249 assertEquals(Intent.ACTION_SEND, innerIntent.getAction());
250 assertNull(innerIntent.getComponent());
251 assertNull(innerIntent.getPackage());
252 assertEquals(CURRENT_USER_INFO.id, innerIntent.getContentUserHint());
253
254 assertEquals(MANAGED_PROFILE_INFO.id, activity.mUserIdActivityLaunchedIn);
255 }
256
257 @Test
258 public void forwardToManagedProfile_canForward_selectorIntent() throws Exception {
259 sComponentName = FORWARD_TO_MANAGED_PROFILE_COMPONENT_NAME;
260
261 // Intent can be forwarded.
262 when(mIPm.canForwardTo(
263 any(Intent.class), nullable(String.class), anyInt(), anyInt())).thenReturn(true);
264
265 // Manage profile exists.
266 List<UserInfo> profiles = new ArrayList<>();
267 profiles.add(CURRENT_USER_INFO);
268 profiles.add(MANAGED_PROFILE_INFO);
269 when(mUserManager.getProfiles(anyInt())).thenReturn(profiles);
270
271 // Create selector intent.
272 Intent intent = Intent.makeMainSelectorActivity(
273 Intent.ACTION_VIEW, Intent.CATEGORY_BROWSABLE);
274 IntentForwarderWrapperActivity activity = mActivityRule.launchActivity(intent);
275
276 ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
277 verify(mIPm).canForwardTo(
278 intentCaptor.capture(), nullable(String.class), anyInt(), anyInt());
279 assertEquals(Intent.ACTION_VIEW, intentCaptor.getValue().getAction());
280
281 assertNotNull(activity.mStartActivityIntent);
282 assertEquals(Intent.ACTION_MAIN, activity.mStartActivityIntent.getAction());
283 assertNull(activity.mStartActivityIntent.getPackage());
284 assertNull(activity.mStartActivityIntent.getComponent());
285 assertEquals(CURRENT_USER_INFO.id, activity.mStartActivityIntent.getContentUserHint());
286
287 Intent innerIntent = activity.mStartActivityIntent.getSelector();
288 assertNotNull(innerIntent);
289 assertEquals(Intent.ACTION_VIEW, innerIntent.getAction());
290 assertNull(innerIntent.getComponent());
291 assertNull(innerIntent.getPackage());
292
293 assertEquals(MANAGED_PROFILE_INFO.id, activity.mUserIdActivityLaunchedIn);
294 }
295
arangelov64439c1e2018-07-31 14:18:47 +0100296 @Test
297 public void shouldSkipDisclosure_notWhitelisted() throws RemoteException {
298 setupShouldSkipDisclosureTest();
299 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
arangelov38303742018-08-14 20:14:12 +0100300 .setAction(Intent.ACTION_SEND)
301 .setType(TYPE_PLAIN_TEXT);
arangelov64439c1e2018-07-31 14:18:47 +0100302
303 mActivityRule.launchActivity(intent);
304
305 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
306 verify(sInjector).showToast(anyInt(), anyInt());
307 }
308
309 @Test
310 public void shouldSkipDisclosure_withResolverActivity() throws RemoteException {
311 setupShouldSkipDisclosureTest();
312 sActivityName = ResolverActivity.class.getName();
313 sPackageName = "android";
314 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
arangelov38303742018-08-14 20:14:12 +0100315 .setAction(Intent.ACTION_SEND)
316 .setType(TYPE_PLAIN_TEXT);
arangelov64439c1e2018-07-31 14:18:47 +0100317
318 mActivityRule.launchActivity(intent);
319
320 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
321 verify(sInjector, never()).showToast(anyInt(), anyInt());
322 }
323
324 @Test
325 public void shouldSkipDisclosure_callIntent_call() throws RemoteException {
326 setupShouldSkipDisclosureTest();
327 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
arangelov38303742018-08-14 20:14:12 +0100328 .setAction(Intent.ACTION_CALL);
329
330 mActivityRule.launchActivity(intent);
331
332 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
333 verify(sInjector, never()).showToast(anyInt(), anyInt());
334 }
335
336 @Test
337 public void shouldSkipDisclosure_callIntent_callPrivileged() throws RemoteException {
338 setupShouldSkipDisclosureTest();
339 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
340 .setAction(Intent.ACTION_CALL_PRIVILEGED);
341
342 mActivityRule.launchActivity(intent);
343
344 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
345 verify(sInjector, never()).showToast(anyInt(), anyInt());
346 }
347
348 @Test
349 public void shouldSkipDisclosure_callIntent_callEmergency() throws RemoteException {
350 setupShouldSkipDisclosureTest();
351 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
352 .setAction(Intent.ACTION_CALL_EMERGENCY);
arangelov64439c1e2018-07-31 14:18:47 +0100353
354 mActivityRule.launchActivity(intent);
355
356 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
357 verify(sInjector, never()).showToast(anyInt(), anyInt());
358 }
359
360 @Test
361 public void shouldSkipDisclosure_callIntent_dial() throws RemoteException {
362 setupShouldSkipDisclosureTest();
363 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
arangelov38303742018-08-14 20:14:12 +0100364 .setAction(Intent.ACTION_DIAL);
arangelov64439c1e2018-07-31 14:18:47 +0100365
366 mActivityRule.launchActivity(intent);
367
368 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
369 verify(sInjector, never()).showToast(anyInt(), anyInt());
370 }
371
372 @Test
373 public void shouldSkipDisclosure_callIntent_notCallOrDial() throws RemoteException {
374 setupShouldSkipDisclosureTest();
375 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
arangelov38303742018-08-14 20:14:12 +0100376 .setAction(Intent.ACTION_ALARM_CHANGED);
arangelov64439c1e2018-07-31 14:18:47 +0100377
378 mActivityRule.launchActivity(intent);
379
380 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
381 verify(sInjector).showToast(anyInt(), anyInt());
382 }
383
384 @Test
arangelov38303742018-08-14 20:14:12 +0100385 public void shouldSkipDisclosure_callIntent_actionViewTel() throws RemoteException {
386 setupShouldSkipDisclosureTest();
387 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
388 .setAction(Intent.ACTION_VIEW)
389 .addCategory(Intent.CATEGORY_BROWSABLE)
390 .setData(Uri.fromParts("tel", PHONE_NUMBER, null));
391
392 mActivityRule.launchActivity(intent);
393
394 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
395 verify(sInjector, never()).showToast(anyInt(), anyInt());
396 }
397
398 @Test
arangelov64439c1e2018-07-31 14:18:47 +0100399 public void shouldSkipDisclosure_textMessageIntent_sms() throws RemoteException {
400 setupShouldSkipDisclosureTest();
401 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
arangelov38303742018-08-14 20:14:12 +0100402 .setAction(Intent.ACTION_SENDTO)
403 .setData(Uri.fromParts("sms", PHONE_NUMBER, null));
arangelov64439c1e2018-07-31 14:18:47 +0100404
405 mActivityRule.launchActivity(intent);
406
407 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
408 verify(sInjector, never()).showToast(anyInt(), anyInt());
409 }
410
411 @Test
412 public void shouldSkipDisclosure_textMessageIntent_smsto() throws RemoteException {
413 setupShouldSkipDisclosureTest();
414 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
arangelov38303742018-08-14 20:14:12 +0100415 .setAction(Intent.ACTION_SENDTO)
416 .setData(Uri.fromParts("smsto", PHONE_NUMBER, null));
arangelov64439c1e2018-07-31 14:18:47 +0100417
418 mActivityRule.launchActivity(intent);
419
420 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
421 verify(sInjector, never()).showToast(anyInt(), anyInt());
422 }
423
424 @Test
425 public void shouldSkipDisclosure_textMessageIntent_mms() throws RemoteException {
426 setupShouldSkipDisclosureTest();
427 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
arangelov38303742018-08-14 20:14:12 +0100428 .setAction(Intent.ACTION_SENDTO)
429 .setData(Uri.fromParts("mms", PHONE_NUMBER, null));
arangelov64439c1e2018-07-31 14:18:47 +0100430
431 mActivityRule.launchActivity(intent);
432
433 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
434 verify(sInjector, never()).showToast(anyInt(), anyInt());
435 }
436
437 @Test
438 public void shouldSkipDisclosure_textMessageIntent_mmsto() throws RemoteException {
439 setupShouldSkipDisclosureTest();
440 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
arangelov38303742018-08-14 20:14:12 +0100441 .setAction(Intent.ACTION_SENDTO)
442 .setData(Uri.fromParts("mmsto", PHONE_NUMBER, null));
443
444 mActivityRule.launchActivity(intent);
445
446 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
447 verify(sInjector, never()).showToast(anyInt(), anyInt());
448 }
449
450 @Test
451 public void shouldSkipDisclosure_textMessageIntent_actionViewSms() throws RemoteException {
452 setupShouldSkipDisclosureTest();
453 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
454 .setAction(Intent.ACTION_VIEW)
455 .addCategory(Intent.CATEGORY_BROWSABLE)
456 .setData(Uri.fromParts("sms", PHONE_NUMBER, null));
457
458 mActivityRule.launchActivity(intent);
459
460 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
461 verify(sInjector, never()).showToast(anyInt(), anyInt());
462 }
463
464 @Test
465 public void shouldSkipDisclosure_textMessageIntent_actionViewSmsto() throws RemoteException {
466 setupShouldSkipDisclosureTest();
467 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
468 .setAction(Intent.ACTION_VIEW)
469 .addCategory(Intent.CATEGORY_BROWSABLE)
470 .setData(Uri.fromParts("smsto", PHONE_NUMBER, null));
471
472 mActivityRule.launchActivity(intent);
473
474 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
475 verify(sInjector, never()).showToast(anyInt(), anyInt());
476 }
477
478 @Test
479 public void shouldSkipDisclosure_textMessageIntent_actionViewMms() throws RemoteException {
480 setupShouldSkipDisclosureTest();
481 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
482 .setAction(Intent.ACTION_VIEW)
483 .addCategory(Intent.CATEGORY_BROWSABLE)
484 .setData(Uri.fromParts("mms", PHONE_NUMBER, null));
485
486 mActivityRule.launchActivity(intent);
487
488 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
489 verify(sInjector, never()).showToast(anyInt(), anyInt());
490 }
491
492 @Test
493 public void shouldSkipDisclosure_textMessageIntent_actionViewMmsto() throws RemoteException {
494 setupShouldSkipDisclosureTest();
495 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
496 .setAction(Intent.ACTION_VIEW)
497 .addCategory(Intent.CATEGORY_BROWSABLE)
498 .setData(Uri.fromParts("mmsto", PHONE_NUMBER, null));
arangelov64439c1e2018-07-31 14:18:47 +0100499
500 mActivityRule.launchActivity(intent);
501
502 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
503 verify(sInjector, never()).showToast(anyInt(), anyInt());
504 }
505
506 @Test
507 public void shouldSkipDisclosure_textMessageIntent_invalidUri() throws RemoteException {
508 setupShouldSkipDisclosureTest();
509 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
arangelov38303742018-08-14 20:14:12 +0100510 .setAction(Intent.ACTION_SENDTO)
511 .setData(Uri.fromParts("invalid", PHONE_NUMBER, null));
512
513 mActivityRule.launchActivity(intent);
514
515 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
516 verify(sInjector).showToast(anyInt(), anyInt());
517 }
518
519 @Test
520 public void shouldSkipDisclosure_viewBrowsableIntent_invalidUri() throws RemoteException {
521 setupShouldSkipDisclosureTest();
522 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
523 .setAction(Intent.ACTION_VIEW)
524 .addCategory(Intent.CATEGORY_BROWSABLE)
525 .setData(Uri.fromParts("invalid", PHONE_NUMBER, null));
526
527 mActivityRule.launchActivity(intent);
528
529 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
530 verify(sInjector).showToast(anyInt(), anyInt());
531 }
532
533 @Test
534 public void shouldSkipDisclosure_viewBrowsableIntent_normalUrl() throws RemoteException {
535 setupShouldSkipDisclosureTest();
536 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
537 .setAction(Intent.ACTION_VIEW)
538 .addCategory(Intent.CATEGORY_BROWSABLE)
539 .setData(Uri.fromParts("http", "apache.org", null));
arangelov64439c1e2018-07-31 14:18:47 +0100540
541 mActivityRule.launchActivity(intent);
542
543 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
544 verify(sInjector).showToast(anyInt(), anyInt());
545 }
546
Susi Kharraz-Post0446fab2019-02-21 09:42:31 -0500547 @Test
Eric Sandnesse89d0e12020-03-13 17:55:51 +0000548 public void shouldSkipDisclosure_duringDeviceSetup() throws RemoteException {
549 setupShouldSkipDisclosureTest();
550 Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.DEVICE_PROVISIONED,
551 /* value= */ 0);
552 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class)
553 .setAction(Intent.ACTION_VIEW)
554 .addCategory(Intent.CATEGORY_BROWSABLE)
555 .setData(Uri.fromParts("http", "apache.org", null));
556
557 mActivityRule.launchActivity(intent);
558
559 verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt());
560 verify(sInjector, never()).showToast(anyInt(), anyInt());
561 }
562
563 @Test
Susi Kharraz-Post0446fab2019-02-21 09:42:31 -0500564 public void forwardToManagedProfile_LoggingTest() throws Exception {
565 sComponentName = FORWARD_TO_MANAGED_PROFILE_COMPONENT_NAME;
566
567 // Intent can be forwarded.
568 when(mIPm.canForwardTo(
569 any(Intent.class), nullable(String.class), anyInt(), anyInt())).thenReturn(true);
570
571 // Managed profile exists.
572 List<UserInfo> profiles = new ArrayList<>();
573 profiles.add(CURRENT_USER_INFO);
574 profiles.add(MANAGED_PROFILE_INFO);
575 when(mUserManager.getProfiles(anyInt())).thenReturn(profiles);
576
577 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class);
578 intent.setAction(Intent.ACTION_SEND);
579 intent.setType(TYPE_PLAIN_TEXT);
580 IntentForwarderWrapperActivity activity = mActivityRule.launchActivity(intent);
581
582 ArgumentCaptor<LogMaker> logMakerCaptor = ArgumentCaptor.forClass(LogMaker.class);
583 verify(activity.getMetricsLogger()).write(logMakerCaptor.capture());
584 assertEquals(MetricsEvent.ACTION_SWITCH_SHARE_PROFILE,
585 logMakerCaptor.getValue().getCategory());
586 assertEquals(MetricsEvent.MANAGED_PROFILE,
587 logMakerCaptor.getValue().getSubtype());
588 }
589
590 @Test
591 public void forwardToParent_LoggingTest() throws Exception {
592 sComponentName = FORWARD_TO_PARENT_COMPONENT_NAME;
593
594 // Intent can be forwarded.
595 when(mIPm.canForwardTo(
596 any(Intent.class), nullable(String.class), anyInt(), anyInt())).thenReturn(true);
597
598 // Managed profile exists.
599 List<UserInfo> profiles = new ArrayList<>();
600 profiles.add(CURRENT_USER_INFO);
601 profiles.add(MANAGED_PROFILE_INFO);
602 when(mUserManager.getProfiles(anyInt())).thenReturn(profiles);
603
604 Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class);
605 intent.setAction(Intent.ACTION_SEND);
606 intent.setType(TYPE_PLAIN_TEXT);
607 IntentForwarderWrapperActivity activity = mActivityRule.launchActivity(intent);
608
609 ArgumentCaptor<LogMaker> logMakerCaptor = ArgumentCaptor.forClass(LogMaker.class);
610 verify(activity.getMetricsLogger()).write(logMakerCaptor.capture());
611 assertEquals(MetricsEvent.ACTION_SWITCH_SHARE_PROFILE,
612 logMakerCaptor.getValue().getCategory());
613 assertEquals(MetricsEvent.PARENT_PROFILE,
614 logMakerCaptor.getValue().getSubtype());
615 }
616
arangelov64439c1e2018-07-31 14:18:47 +0100617 private void setupShouldSkipDisclosureTest() throws RemoteException {
618 sComponentName = FORWARD_TO_MANAGED_PROFILE_COMPONENT_NAME;
619 sActivityName = "MyTestActivity";
620 sPackageName = "test.package.name";
Eric Sandnesse89d0e12020-03-13 17:55:51 +0000621 Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.DEVICE_PROVISIONED,
622 /* value= */ 1);
arangelov64439c1e2018-07-31 14:18:47 +0100623 when(mApplicationInfo.isSystemApp()).thenReturn(true);
624 // Managed profile exists.
625 List<UserInfo> profiles = new ArrayList<>();
626 profiles.add(CURRENT_USER_INFO);
627 profiles.add(MANAGED_PROFILE_INFO);
628 when(mUserManager.getProfiles(anyInt())).thenReturn(profiles);
629 // Intent can be forwarded.
630 when(mIPm.canForwardTo(
arangelov38303742018-08-14 20:14:12 +0100631 any(Intent.class), nullable(String.class), anyInt(), anyInt())).thenReturn(true);
arangelov64439c1e2018-07-31 14:18:47 +0100632 }
Tony Mak96d78f52017-05-03 18:53:21 +0100633
634 public static class IntentForwarderWrapperActivity extends IntentForwarderActivity {
arangelov38303742018-08-14 20:14:12 +0100635
Tony Mak96d78f52017-05-03 18:53:21 +0100636 private Intent mStartActivityIntent;
637 private int mUserIdActivityLaunchedIn;
Susi Kharraz-Post0446fab2019-02-21 09:42:31 -0500638 private MetricsLogger mMetricsLogger = mock(MetricsLogger.class);
Tony Mak96d78f52017-05-03 18:53:21 +0100639
640 @Override
641 public void onCreate(@Nullable Bundle savedInstanceState) {
642 getIntent().setComponent(sComponentName);
643 super.onCreate(savedInstanceState);
644 }
645
646 @Override
647 protected Injector createInjector() {
648 return sInjector;
649 }
650
651 @Override
Alison Cichowlas3e340502018-08-07 17:15:01 -0400652 public void startActivityAsCaller(Intent intent, @Nullable Bundle options,
653 IBinder permissionToken, boolean ignoreTargetSecurity, int userId) {
Tony Mak96d78f52017-05-03 18:53:21 +0100654 mStartActivityIntent = intent;
655 mUserIdActivityLaunchedIn = userId;
656 }
Susi Kharraz-Post0446fab2019-02-21 09:42:31 -0500657
658 @Override
659 protected MetricsLogger getMetricsLogger() {
660 return mMetricsLogger;
661 }
Tony Mak96d78f52017-05-03 18:53:21 +0100662 }
663
arangelov64439c1e2018-07-31 14:18:47 +0100664 public class TestInjector implements IntentForwarderActivity.Injector {
Tony Mak96d78f52017-05-03 18:53:21 +0100665
666 @Override
667 public IPackageManager getIPackageManager() {
668 return mIPm;
669 }
670
671 @Override
672 public UserManager getUserManager() {
673 return mUserManager;
674 }
675
676 @Override
677 public PackageManager getPackageManager() {
678 return mPm;
679 }
arangelov64439c1e2018-07-31 14:18:47 +0100680
681 @Override
682 public ResolveInfo resolveActivityAsUser(Intent intent, int flags, int userId) {
683 ActivityInfo activityInfo = new ActivityInfo();
684 activityInfo.packageName = sPackageName;
685 activityInfo.name = sActivityName;
686 activityInfo.applicationInfo = mApplicationInfo;
687
688 ResolveInfo resolveInfo = new ResolveInfo();
689 resolveInfo.activityInfo = activityInfo;
690
691 return resolveInfo;
692 }
693
694 @Override
695 public void showToast(int messageId, int duration) {}
Tony Mak96d78f52017-05-03 18:53:21 +0100696 }
Alison Cichowlas3e340502018-08-07 17:15:01 -0400697}