Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.internal.app; |
| 18 | |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 19 | import static junit.framework.Assert.assertEquals; |
| 20 | import static junit.framework.Assert.assertNotNull; |
| 21 | import static junit.framework.Assert.assertNull; |
Alison Cichowlas | 3e34050 | 2018-08-07 17:15:01 -0400 | [diff] [blame] | 22 | |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 23 | import static org.mockito.ArgumentMatchers.any; |
| 24 | import static org.mockito.ArgumentMatchers.anyInt; |
| 25 | import static org.mockito.ArgumentMatchers.anyString; |
| 26 | import static org.mockito.ArgumentMatchers.eq; |
| 27 | import static org.mockito.ArgumentMatchers.nullable; |
Susi Kharraz-Post | 0446fab | 2019-02-21 09:42:31 -0500 | [diff] [blame] | 28 | import static org.mockito.Mockito.mock; |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 29 | import static org.mockito.Mockito.never; |
| 30 | import static org.mockito.Mockito.spy; |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 31 | import static org.mockito.Mockito.verify; |
| 32 | import static org.mockito.Mockito.when; |
| 33 | |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 34 | import android.annotation.Nullable; |
| 35 | import android.content.ComponentName; |
| 36 | import android.content.Context; |
| 37 | import android.content.Intent; |
| 38 | import android.content.pm.ActivityInfo; |
| 39 | import android.content.pm.ApplicationInfo; |
| 40 | import android.content.pm.IPackageManager; |
| 41 | import android.content.pm.PackageManager; |
| 42 | import android.content.pm.ResolveInfo; |
| 43 | import android.content.pm.UserInfo; |
Susi Kharraz-Post | 0446fab | 2019-02-21 09:42:31 -0500 | [diff] [blame] | 44 | import android.metrics.LogMaker; |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 45 | import android.net.Uri; |
| 46 | import android.os.Bundle; |
Alison Cichowlas | 3e34050 | 2018-08-07 17:15:01 -0400 | [diff] [blame] | 47 | import android.os.IBinder; |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 48 | import android.os.RemoteException; |
| 49 | import android.os.UserHandle; |
| 50 | import android.os.UserManager; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 51 | |
| 52 | import androidx.test.InstrumentationRegistry; |
| 53 | import androidx.test.rule.ActivityTestRule; |
| 54 | import androidx.test.runner.AndroidJUnit4; |
Alison Cichowlas | 3e34050 | 2018-08-07 17:15:01 -0400 | [diff] [blame] | 55 | |
Susi Kharraz-Post | 0446fab | 2019-02-21 09:42:31 -0500 | [diff] [blame] | 56 | import com.android.internal.logging.MetricsLogger; |
| 57 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
| 58 | |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 59 | import org.junit.Before; |
| 60 | import org.junit.Rule; |
| 61 | import org.junit.Test; |
| 62 | import org.junit.runner.RunWith; |
| 63 | import org.mockito.ArgumentCaptor; |
| 64 | import org.mockito.Mock; |
| 65 | import org.mockito.MockitoAnnotations; |
| 66 | |
Alison Cichowlas | 3e34050 | 2018-08-07 17:15:01 -0400 | [diff] [blame] | 67 | import java.util.ArrayList; |
| 68 | import java.util.List; |
| 69 | |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 70 | @RunWith(AndroidJUnit4.class) |
| 71 | public class IntentForwarderActivityTest { |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 72 | |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 73 | private static final ComponentName FORWARD_TO_MANAGED_PROFILE_COMPONENT_NAME = |
| 74 | new ComponentName( |
| 75 | "android", |
| 76 | IntentForwarderActivity.FORWARD_INTENT_TO_MANAGED_PROFILE |
| 77 | ); |
Susi Kharraz-Post | 0446fab | 2019-02-21 09:42:31 -0500 | [diff] [blame] | 78 | private static final ComponentName FORWARD_TO_PARENT_COMPONENT_NAME = |
| 79 | new ComponentName( |
| 80 | "android", |
| 81 | IntentForwarderActivity.FORWARD_INTENT_TO_PARENT |
| 82 | ); |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 83 | private static final String TYPE_PLAIN_TEXT = "text/plain"; |
| 84 | |
| 85 | private static UserInfo MANAGED_PROFILE_INFO = new UserInfo(); |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame] | 86 | |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 87 | static { |
| 88 | MANAGED_PROFILE_INFO.id = 10; |
| 89 | MANAGED_PROFILE_INFO.flags = UserInfo.FLAG_MANAGED_PROFILE; |
| 90 | } |
| 91 | |
| 92 | private static UserInfo CURRENT_USER_INFO = new UserInfo(); |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame] | 93 | |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 94 | static { |
| 95 | CURRENT_USER_INFO.id = UserHandle.myUserId(); |
| 96 | CURRENT_USER_INFO.flags = 0; |
| 97 | } |
| 98 | |
| 99 | private static IntentForwarderActivity.Injector sInjector; |
| 100 | private static ComponentName sComponentName; |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 101 | private static String sActivityName; |
| 102 | private static String sPackageName; |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 103 | |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame] | 104 | @Mock |
| 105 | private IPackageManager mIPm; |
| 106 | @Mock |
| 107 | private PackageManager mPm; |
| 108 | @Mock |
| 109 | private UserManager mUserManager; |
| 110 | @Mock |
| 111 | private ApplicationInfo mApplicationInfo; |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 112 | |
| 113 | @Rule |
| 114 | public ActivityTestRule<IntentForwarderWrapperActivity> mActivityRule = |
| 115 | new ActivityTestRule<>(IntentForwarderWrapperActivity.class, true, false); |
| 116 | |
| 117 | private Context mContext; |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 118 | public static final String PHONE_NUMBER = "123-456-789"; |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 119 | |
| 120 | @Before |
| 121 | public void setup() { |
| 122 | MockitoAnnotations.initMocks(this); |
| 123 | mContext = InstrumentationRegistry.getTargetContext(); |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 124 | sInjector = spy(new TestInjector()); |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | @Test |
| 128 | public void forwardToManagedProfile_canForward_sendIntent() throws Exception { |
| 129 | sComponentName = FORWARD_TO_MANAGED_PROFILE_COMPONENT_NAME; |
| 130 | |
| 131 | // Intent can be forwarded. |
| 132 | when(mIPm.canForwardTo( |
| 133 | any(Intent.class), nullable(String.class), anyInt(), anyInt())).thenReturn(true); |
| 134 | |
| 135 | // Managed profile exists. |
| 136 | List<UserInfo> profiles = new ArrayList<>(); |
| 137 | profiles.add(CURRENT_USER_INFO); |
| 138 | profiles.add(MANAGED_PROFILE_INFO); |
| 139 | when(mUserManager.getProfiles(anyInt())).thenReturn(profiles); |
| 140 | |
| 141 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class); |
| 142 | intent.setAction(Intent.ACTION_SEND); |
| 143 | intent.setType(TYPE_PLAIN_TEXT); |
| 144 | IntentForwarderWrapperActivity activity = mActivityRule.launchActivity(intent); |
| 145 | |
| 146 | ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); |
| 147 | verify(mIPm).canForwardTo(intentCaptor.capture(), eq(TYPE_PLAIN_TEXT), anyInt(), anyInt()); |
| 148 | assertEquals(Intent.ACTION_SEND, intentCaptor.getValue().getAction()); |
| 149 | |
| 150 | assertEquals(Intent.ACTION_SEND, intentCaptor.getValue().getAction()); |
| 151 | assertNotNull(activity.mStartActivityIntent); |
| 152 | assertEquals(Intent.ACTION_SEND, activity.mStartActivityIntent.getAction()); |
| 153 | assertNull(activity.mStartActivityIntent.getPackage()); |
| 154 | assertNull(activity.mStartActivityIntent.getComponent()); |
| 155 | assertEquals(CURRENT_USER_INFO.id, activity.mStartActivityIntent.getContentUserHint()); |
| 156 | |
| 157 | assertEquals(MANAGED_PROFILE_INFO.id, activity.mUserIdActivityLaunchedIn); |
| 158 | } |
| 159 | |
| 160 | @Test |
| 161 | public void forwardToManagedProfile_cannotForward_sendIntent() throws Exception { |
| 162 | sComponentName = FORWARD_TO_MANAGED_PROFILE_COMPONENT_NAME; |
| 163 | |
| 164 | // Intent cannot be forwarded. |
| 165 | when(mIPm.canForwardTo( |
| 166 | any(Intent.class), nullable(String.class), anyInt(), anyInt())).thenReturn(false); |
| 167 | |
| 168 | // Managed profile exists. |
| 169 | List<UserInfo> profiles = new ArrayList<>(); |
| 170 | profiles.add(CURRENT_USER_INFO); |
| 171 | profiles.add(MANAGED_PROFILE_INFO); |
| 172 | when(mUserManager.getProfiles(anyInt())).thenReturn(profiles); |
| 173 | |
| 174 | // Create ACTION_SEND intent. |
| 175 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class); |
| 176 | intent.setAction(Intent.ACTION_SEND); |
| 177 | IntentForwarderWrapperActivity activity = mActivityRule.launchActivity(intent); |
| 178 | |
| 179 | assertNull(activity.mStartActivityIntent); |
| 180 | } |
| 181 | |
| 182 | @Test |
| 183 | public void forwardToManagedProfile_noManagedProfile_sendIntent() throws Exception { |
| 184 | sComponentName = FORWARD_TO_MANAGED_PROFILE_COMPONENT_NAME; |
| 185 | |
| 186 | // Intent can be forwarded. |
| 187 | when(mIPm.canForwardTo( |
| 188 | any(Intent.class), anyString(), anyInt(), anyInt())).thenReturn(true); |
| 189 | |
| 190 | // Managed profile does not exist. |
| 191 | List<UserInfo> profiles = new ArrayList<>(); |
| 192 | profiles.add(CURRENT_USER_INFO); |
| 193 | when(mUserManager.getProfiles(anyInt())).thenReturn(profiles); |
| 194 | |
| 195 | // Create ACTION_SEND intent. |
| 196 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class); |
| 197 | intent.setAction(Intent.ACTION_SEND); |
| 198 | IntentForwarderWrapperActivity activity = mActivityRule.launchActivity(intent); |
| 199 | |
| 200 | assertNull(activity.mStartActivityIntent); |
| 201 | } |
| 202 | |
| 203 | @Test |
| 204 | public void forwardToManagedProfile_canForward_chooserIntent() throws Exception { |
| 205 | sComponentName = FORWARD_TO_MANAGED_PROFILE_COMPONENT_NAME; |
| 206 | |
| 207 | // Intent can be forwarded. |
| 208 | when(mIPm.canForwardTo( |
| 209 | any(Intent.class), nullable(String.class), anyInt(), anyInt())).thenReturn(true); |
| 210 | |
| 211 | // Manage profile exists. |
| 212 | List<UserInfo> profiles = new ArrayList<>(); |
| 213 | profiles.add(CURRENT_USER_INFO); |
| 214 | profiles.add(MANAGED_PROFILE_INFO); |
| 215 | when(mUserManager.getProfiles(anyInt())).thenReturn(profiles); |
| 216 | |
| 217 | // Create chooser Intent |
| 218 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class); |
| 219 | intent.setAction(Intent.ACTION_CHOOSER); |
| 220 | Intent sendIntent = new Intent(Intent.ACTION_SEND); |
| 221 | sendIntent.setComponent(new ComponentName("xx", "yyy")); |
| 222 | sendIntent.setType(TYPE_PLAIN_TEXT); |
| 223 | intent.putExtra(Intent.EXTRA_INTENT, sendIntent); |
| 224 | IntentForwarderWrapperActivity activity = mActivityRule.launchActivity(intent); |
| 225 | |
| 226 | ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); |
| 227 | verify(mIPm).canForwardTo(intentCaptor.capture(), eq(TYPE_PLAIN_TEXT), anyInt(), anyInt()); |
| 228 | assertEquals(Intent.ACTION_SEND, intentCaptor.getValue().getAction()); |
| 229 | |
| 230 | assertNotNull(activity.mStartActivityIntent); |
| 231 | assertEquals(Intent.ACTION_CHOOSER, activity.mStartActivityIntent.getAction()); |
| 232 | assertNull(activity.mStartActivityIntent.getPackage()); |
| 233 | assertNull(activity.mStartActivityIntent.getComponent()); |
| 234 | |
| 235 | Intent innerIntent = activity.mStartActivityIntent.getParcelableExtra(Intent.EXTRA_INTENT); |
| 236 | assertNotNull(innerIntent); |
| 237 | assertEquals(Intent.ACTION_SEND, innerIntent.getAction()); |
| 238 | assertNull(innerIntent.getComponent()); |
| 239 | assertNull(innerIntent.getPackage()); |
| 240 | assertEquals(CURRENT_USER_INFO.id, innerIntent.getContentUserHint()); |
| 241 | |
| 242 | assertEquals(MANAGED_PROFILE_INFO.id, activity.mUserIdActivityLaunchedIn); |
| 243 | } |
| 244 | |
| 245 | @Test |
| 246 | public void forwardToManagedProfile_canForward_selectorIntent() throws Exception { |
| 247 | sComponentName = FORWARD_TO_MANAGED_PROFILE_COMPONENT_NAME; |
| 248 | |
| 249 | // Intent can be forwarded. |
| 250 | when(mIPm.canForwardTo( |
| 251 | any(Intent.class), nullable(String.class), anyInt(), anyInt())).thenReturn(true); |
| 252 | |
| 253 | // Manage profile exists. |
| 254 | List<UserInfo> profiles = new ArrayList<>(); |
| 255 | profiles.add(CURRENT_USER_INFO); |
| 256 | profiles.add(MANAGED_PROFILE_INFO); |
| 257 | when(mUserManager.getProfiles(anyInt())).thenReturn(profiles); |
| 258 | |
| 259 | // Create selector intent. |
| 260 | Intent intent = Intent.makeMainSelectorActivity( |
| 261 | Intent.ACTION_VIEW, Intent.CATEGORY_BROWSABLE); |
| 262 | IntentForwarderWrapperActivity activity = mActivityRule.launchActivity(intent); |
| 263 | |
| 264 | ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); |
| 265 | verify(mIPm).canForwardTo( |
| 266 | intentCaptor.capture(), nullable(String.class), anyInt(), anyInt()); |
| 267 | assertEquals(Intent.ACTION_VIEW, intentCaptor.getValue().getAction()); |
| 268 | |
| 269 | assertNotNull(activity.mStartActivityIntent); |
| 270 | assertEquals(Intent.ACTION_MAIN, activity.mStartActivityIntent.getAction()); |
| 271 | assertNull(activity.mStartActivityIntent.getPackage()); |
| 272 | assertNull(activity.mStartActivityIntent.getComponent()); |
| 273 | assertEquals(CURRENT_USER_INFO.id, activity.mStartActivityIntent.getContentUserHint()); |
| 274 | |
| 275 | Intent innerIntent = activity.mStartActivityIntent.getSelector(); |
| 276 | assertNotNull(innerIntent); |
| 277 | assertEquals(Intent.ACTION_VIEW, innerIntent.getAction()); |
| 278 | assertNull(innerIntent.getComponent()); |
| 279 | assertNull(innerIntent.getPackage()); |
| 280 | |
| 281 | assertEquals(MANAGED_PROFILE_INFO.id, activity.mUserIdActivityLaunchedIn); |
| 282 | } |
| 283 | |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 284 | @Test |
| 285 | public void shouldSkipDisclosure_notWhitelisted() throws RemoteException { |
| 286 | setupShouldSkipDisclosureTest(); |
| 287 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class) |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame] | 288 | .setAction(Intent.ACTION_SEND) |
| 289 | .setType(TYPE_PLAIN_TEXT); |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 290 | |
| 291 | mActivityRule.launchActivity(intent); |
| 292 | |
| 293 | verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt()); |
| 294 | verify(sInjector).showToast(anyInt(), anyInt()); |
| 295 | } |
| 296 | |
| 297 | @Test |
| 298 | public void shouldSkipDisclosure_withResolverActivity() throws RemoteException { |
| 299 | setupShouldSkipDisclosureTest(); |
| 300 | sActivityName = ResolverActivity.class.getName(); |
| 301 | sPackageName = "android"; |
| 302 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class) |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame] | 303 | .setAction(Intent.ACTION_SEND) |
| 304 | .setType(TYPE_PLAIN_TEXT); |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 305 | |
| 306 | mActivityRule.launchActivity(intent); |
| 307 | |
| 308 | verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt()); |
| 309 | verify(sInjector, never()).showToast(anyInt(), anyInt()); |
| 310 | } |
| 311 | |
| 312 | @Test |
| 313 | public void shouldSkipDisclosure_callIntent_call() throws RemoteException { |
| 314 | setupShouldSkipDisclosureTest(); |
| 315 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class) |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame] | 316 | .setAction(Intent.ACTION_CALL); |
| 317 | |
| 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_callPrivileged() throws RemoteException { |
| 326 | setupShouldSkipDisclosureTest(); |
| 327 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class) |
| 328 | .setAction(Intent.ACTION_CALL_PRIVILEGED); |
| 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_callEmergency() throws RemoteException { |
| 338 | setupShouldSkipDisclosureTest(); |
| 339 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class) |
| 340 | .setAction(Intent.ACTION_CALL_EMERGENCY); |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 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_dial() throws RemoteException { |
| 350 | setupShouldSkipDisclosureTest(); |
| 351 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class) |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame] | 352 | .setAction(Intent.ACTION_DIAL); |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 353 | |
| 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_notCallOrDial() throws RemoteException { |
| 362 | setupShouldSkipDisclosureTest(); |
| 363 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class) |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame] | 364 | .setAction(Intent.ACTION_ALARM_CHANGED); |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 365 | |
| 366 | mActivityRule.launchActivity(intent); |
| 367 | |
| 368 | verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt()); |
| 369 | verify(sInjector).showToast(anyInt(), anyInt()); |
| 370 | } |
| 371 | |
| 372 | @Test |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame] | 373 | public void shouldSkipDisclosure_callIntent_actionViewTel() throws RemoteException { |
| 374 | setupShouldSkipDisclosureTest(); |
| 375 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class) |
| 376 | .setAction(Intent.ACTION_VIEW) |
| 377 | .addCategory(Intent.CATEGORY_BROWSABLE) |
| 378 | .setData(Uri.fromParts("tel", PHONE_NUMBER, null)); |
| 379 | |
| 380 | mActivityRule.launchActivity(intent); |
| 381 | |
| 382 | verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt()); |
| 383 | verify(sInjector, never()).showToast(anyInt(), anyInt()); |
| 384 | } |
| 385 | |
| 386 | @Test |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 387 | public void shouldSkipDisclosure_textMessageIntent_sms() throws RemoteException { |
| 388 | setupShouldSkipDisclosureTest(); |
| 389 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class) |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame] | 390 | .setAction(Intent.ACTION_SENDTO) |
| 391 | .setData(Uri.fromParts("sms", PHONE_NUMBER, null)); |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 392 | |
| 393 | mActivityRule.launchActivity(intent); |
| 394 | |
| 395 | verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt()); |
| 396 | verify(sInjector, never()).showToast(anyInt(), anyInt()); |
| 397 | } |
| 398 | |
| 399 | @Test |
| 400 | public void shouldSkipDisclosure_textMessageIntent_smsto() throws RemoteException { |
| 401 | setupShouldSkipDisclosureTest(); |
| 402 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class) |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame] | 403 | .setAction(Intent.ACTION_SENDTO) |
| 404 | .setData(Uri.fromParts("smsto", PHONE_NUMBER, null)); |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 405 | |
| 406 | mActivityRule.launchActivity(intent); |
| 407 | |
| 408 | verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt()); |
| 409 | verify(sInjector, never()).showToast(anyInt(), anyInt()); |
| 410 | } |
| 411 | |
| 412 | @Test |
| 413 | public void shouldSkipDisclosure_textMessageIntent_mms() throws RemoteException { |
| 414 | setupShouldSkipDisclosureTest(); |
| 415 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class) |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame] | 416 | .setAction(Intent.ACTION_SENDTO) |
| 417 | .setData(Uri.fromParts("mms", PHONE_NUMBER, null)); |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 418 | |
| 419 | mActivityRule.launchActivity(intent); |
| 420 | |
| 421 | verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt()); |
| 422 | verify(sInjector, never()).showToast(anyInt(), anyInt()); |
| 423 | } |
| 424 | |
| 425 | @Test |
| 426 | public void shouldSkipDisclosure_textMessageIntent_mmsto() throws RemoteException { |
| 427 | setupShouldSkipDisclosureTest(); |
| 428 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class) |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame] | 429 | .setAction(Intent.ACTION_SENDTO) |
| 430 | .setData(Uri.fromParts("mmsto", PHONE_NUMBER, null)); |
| 431 | |
| 432 | mActivityRule.launchActivity(intent); |
| 433 | |
| 434 | verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt()); |
| 435 | verify(sInjector, never()).showToast(anyInt(), anyInt()); |
| 436 | } |
| 437 | |
| 438 | @Test |
| 439 | public void shouldSkipDisclosure_textMessageIntent_actionViewSms() throws RemoteException { |
| 440 | setupShouldSkipDisclosureTest(); |
| 441 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class) |
| 442 | .setAction(Intent.ACTION_VIEW) |
| 443 | .addCategory(Intent.CATEGORY_BROWSABLE) |
| 444 | .setData(Uri.fromParts("sms", PHONE_NUMBER, null)); |
| 445 | |
| 446 | mActivityRule.launchActivity(intent); |
| 447 | |
| 448 | verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt()); |
| 449 | verify(sInjector, never()).showToast(anyInt(), anyInt()); |
| 450 | } |
| 451 | |
| 452 | @Test |
| 453 | public void shouldSkipDisclosure_textMessageIntent_actionViewSmsto() throws RemoteException { |
| 454 | setupShouldSkipDisclosureTest(); |
| 455 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class) |
| 456 | .setAction(Intent.ACTION_VIEW) |
| 457 | .addCategory(Intent.CATEGORY_BROWSABLE) |
| 458 | .setData(Uri.fromParts("smsto", PHONE_NUMBER, null)); |
| 459 | |
| 460 | mActivityRule.launchActivity(intent); |
| 461 | |
| 462 | verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt()); |
| 463 | verify(sInjector, never()).showToast(anyInt(), anyInt()); |
| 464 | } |
| 465 | |
| 466 | @Test |
| 467 | public void shouldSkipDisclosure_textMessageIntent_actionViewMms() throws RemoteException { |
| 468 | setupShouldSkipDisclosureTest(); |
| 469 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class) |
| 470 | .setAction(Intent.ACTION_VIEW) |
| 471 | .addCategory(Intent.CATEGORY_BROWSABLE) |
| 472 | .setData(Uri.fromParts("mms", PHONE_NUMBER, null)); |
| 473 | |
| 474 | mActivityRule.launchActivity(intent); |
| 475 | |
| 476 | verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt()); |
| 477 | verify(sInjector, never()).showToast(anyInt(), anyInt()); |
| 478 | } |
| 479 | |
| 480 | @Test |
| 481 | public void shouldSkipDisclosure_textMessageIntent_actionViewMmsto() throws RemoteException { |
| 482 | setupShouldSkipDisclosureTest(); |
| 483 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class) |
| 484 | .setAction(Intent.ACTION_VIEW) |
| 485 | .addCategory(Intent.CATEGORY_BROWSABLE) |
| 486 | .setData(Uri.fromParts("mmsto", PHONE_NUMBER, null)); |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 487 | |
| 488 | mActivityRule.launchActivity(intent); |
| 489 | |
| 490 | verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt()); |
| 491 | verify(sInjector, never()).showToast(anyInt(), anyInt()); |
| 492 | } |
| 493 | |
| 494 | @Test |
| 495 | public void shouldSkipDisclosure_textMessageIntent_invalidUri() throws RemoteException { |
| 496 | setupShouldSkipDisclosureTest(); |
| 497 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class) |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame] | 498 | .setAction(Intent.ACTION_SENDTO) |
| 499 | .setData(Uri.fromParts("invalid", PHONE_NUMBER, null)); |
| 500 | |
| 501 | mActivityRule.launchActivity(intent); |
| 502 | |
| 503 | verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt()); |
| 504 | verify(sInjector).showToast(anyInt(), anyInt()); |
| 505 | } |
| 506 | |
| 507 | @Test |
| 508 | public void shouldSkipDisclosure_viewBrowsableIntent_invalidUri() throws RemoteException { |
| 509 | setupShouldSkipDisclosureTest(); |
| 510 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class) |
| 511 | .setAction(Intent.ACTION_VIEW) |
| 512 | .addCategory(Intent.CATEGORY_BROWSABLE) |
| 513 | .setData(Uri.fromParts("invalid", PHONE_NUMBER, null)); |
| 514 | |
| 515 | mActivityRule.launchActivity(intent); |
| 516 | |
| 517 | verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt()); |
| 518 | verify(sInjector).showToast(anyInt(), anyInt()); |
| 519 | } |
| 520 | |
| 521 | @Test |
| 522 | public void shouldSkipDisclosure_viewBrowsableIntent_normalUrl() throws RemoteException { |
| 523 | setupShouldSkipDisclosureTest(); |
| 524 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class) |
| 525 | .setAction(Intent.ACTION_VIEW) |
| 526 | .addCategory(Intent.CATEGORY_BROWSABLE) |
| 527 | .setData(Uri.fromParts("http", "apache.org", null)); |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 528 | |
| 529 | mActivityRule.launchActivity(intent); |
| 530 | |
| 531 | verify(mIPm).canForwardTo(any(), any(), anyInt(), anyInt()); |
| 532 | verify(sInjector).showToast(anyInt(), anyInt()); |
| 533 | } |
| 534 | |
Susi Kharraz-Post | 0446fab | 2019-02-21 09:42:31 -0500 | [diff] [blame] | 535 | @Test |
| 536 | public void forwardToManagedProfile_LoggingTest() throws Exception { |
| 537 | sComponentName = FORWARD_TO_MANAGED_PROFILE_COMPONENT_NAME; |
| 538 | |
| 539 | // Intent can be forwarded. |
| 540 | when(mIPm.canForwardTo( |
| 541 | any(Intent.class), nullable(String.class), anyInt(), anyInt())).thenReturn(true); |
| 542 | |
| 543 | // Managed profile exists. |
| 544 | List<UserInfo> profiles = new ArrayList<>(); |
| 545 | profiles.add(CURRENT_USER_INFO); |
| 546 | profiles.add(MANAGED_PROFILE_INFO); |
| 547 | when(mUserManager.getProfiles(anyInt())).thenReturn(profiles); |
| 548 | |
| 549 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class); |
| 550 | intent.setAction(Intent.ACTION_SEND); |
| 551 | intent.setType(TYPE_PLAIN_TEXT); |
| 552 | IntentForwarderWrapperActivity activity = mActivityRule.launchActivity(intent); |
| 553 | |
| 554 | ArgumentCaptor<LogMaker> logMakerCaptor = ArgumentCaptor.forClass(LogMaker.class); |
| 555 | verify(activity.getMetricsLogger()).write(logMakerCaptor.capture()); |
| 556 | assertEquals(MetricsEvent.ACTION_SWITCH_SHARE_PROFILE, |
| 557 | logMakerCaptor.getValue().getCategory()); |
| 558 | assertEquals(MetricsEvent.MANAGED_PROFILE, |
| 559 | logMakerCaptor.getValue().getSubtype()); |
| 560 | } |
| 561 | |
| 562 | @Test |
| 563 | public void forwardToParent_LoggingTest() throws Exception { |
| 564 | sComponentName = FORWARD_TO_PARENT_COMPONENT_NAME; |
| 565 | |
| 566 | // Intent can be forwarded. |
| 567 | when(mIPm.canForwardTo( |
| 568 | any(Intent.class), nullable(String.class), anyInt(), anyInt())).thenReturn(true); |
| 569 | |
| 570 | // Managed profile exists. |
| 571 | List<UserInfo> profiles = new ArrayList<>(); |
| 572 | profiles.add(CURRENT_USER_INFO); |
| 573 | profiles.add(MANAGED_PROFILE_INFO); |
| 574 | when(mUserManager.getProfiles(anyInt())).thenReturn(profiles); |
| 575 | |
| 576 | Intent intent = new Intent(mContext, IntentForwarderWrapperActivity.class); |
| 577 | intent.setAction(Intent.ACTION_SEND); |
| 578 | intent.setType(TYPE_PLAIN_TEXT); |
| 579 | IntentForwarderWrapperActivity activity = mActivityRule.launchActivity(intent); |
| 580 | |
| 581 | ArgumentCaptor<LogMaker> logMakerCaptor = ArgumentCaptor.forClass(LogMaker.class); |
| 582 | verify(activity.getMetricsLogger()).write(logMakerCaptor.capture()); |
| 583 | assertEquals(MetricsEvent.ACTION_SWITCH_SHARE_PROFILE, |
| 584 | logMakerCaptor.getValue().getCategory()); |
| 585 | assertEquals(MetricsEvent.PARENT_PROFILE, |
| 586 | logMakerCaptor.getValue().getSubtype()); |
| 587 | } |
| 588 | |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 589 | private void setupShouldSkipDisclosureTest() throws RemoteException { |
| 590 | sComponentName = FORWARD_TO_MANAGED_PROFILE_COMPONENT_NAME; |
| 591 | sActivityName = "MyTestActivity"; |
| 592 | sPackageName = "test.package.name"; |
| 593 | when(mApplicationInfo.isSystemApp()).thenReturn(true); |
| 594 | // Managed profile exists. |
| 595 | List<UserInfo> profiles = new ArrayList<>(); |
| 596 | profiles.add(CURRENT_USER_INFO); |
| 597 | profiles.add(MANAGED_PROFILE_INFO); |
| 598 | when(mUserManager.getProfiles(anyInt())).thenReturn(profiles); |
| 599 | // Intent can be forwarded. |
| 600 | when(mIPm.canForwardTo( |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame] | 601 | any(Intent.class), nullable(String.class), anyInt(), anyInt())).thenReturn(true); |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 602 | } |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 603 | |
| 604 | public static class IntentForwarderWrapperActivity extends IntentForwarderActivity { |
arangelov | 3830374 | 2018-08-14 20:14:12 +0100 | [diff] [blame] | 605 | |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 606 | private Intent mStartActivityIntent; |
| 607 | private int mUserIdActivityLaunchedIn; |
Susi Kharraz-Post | 0446fab | 2019-02-21 09:42:31 -0500 | [diff] [blame] | 608 | private MetricsLogger mMetricsLogger = mock(MetricsLogger.class); |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 609 | |
| 610 | @Override |
| 611 | public void onCreate(@Nullable Bundle savedInstanceState) { |
| 612 | getIntent().setComponent(sComponentName); |
| 613 | super.onCreate(savedInstanceState); |
| 614 | } |
| 615 | |
| 616 | @Override |
| 617 | protected Injector createInjector() { |
| 618 | return sInjector; |
| 619 | } |
| 620 | |
| 621 | @Override |
Alison Cichowlas | 3e34050 | 2018-08-07 17:15:01 -0400 | [diff] [blame] | 622 | public void startActivityAsCaller(Intent intent, @Nullable Bundle options, |
| 623 | IBinder permissionToken, boolean ignoreTargetSecurity, int userId) { |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 624 | mStartActivityIntent = intent; |
| 625 | mUserIdActivityLaunchedIn = userId; |
| 626 | } |
Susi Kharraz-Post | 0446fab | 2019-02-21 09:42:31 -0500 | [diff] [blame] | 627 | |
| 628 | @Override |
| 629 | protected MetricsLogger getMetricsLogger() { |
| 630 | return mMetricsLogger; |
| 631 | } |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 632 | } |
| 633 | |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 634 | public class TestInjector implements IntentForwarderActivity.Injector { |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 635 | |
| 636 | @Override |
| 637 | public IPackageManager getIPackageManager() { |
| 638 | return mIPm; |
| 639 | } |
| 640 | |
| 641 | @Override |
| 642 | public UserManager getUserManager() { |
| 643 | return mUserManager; |
| 644 | } |
| 645 | |
| 646 | @Override |
| 647 | public PackageManager getPackageManager() { |
| 648 | return mPm; |
| 649 | } |
arangelov | 64439c1e | 2018-07-31 14:18:47 +0100 | [diff] [blame] | 650 | |
| 651 | @Override |
| 652 | public ResolveInfo resolveActivityAsUser(Intent intent, int flags, int userId) { |
| 653 | ActivityInfo activityInfo = new ActivityInfo(); |
| 654 | activityInfo.packageName = sPackageName; |
| 655 | activityInfo.name = sActivityName; |
| 656 | activityInfo.applicationInfo = mApplicationInfo; |
| 657 | |
| 658 | ResolveInfo resolveInfo = new ResolveInfo(); |
| 659 | resolveInfo.activityInfo = activityInfo; |
| 660 | |
| 661 | return resolveInfo; |
| 662 | } |
| 663 | |
| 664 | @Override |
| 665 | public void showToast(int messageId, int duration) {} |
Tony Mak | 96d78f5 | 2017-05-03 18:53:21 +0100 | [diff] [blame] | 666 | } |
Alison Cichowlas | 3e34050 | 2018-08-07 17:15:01 -0400 | [diff] [blame] | 667 | } |