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 17 package com.android.settings.enterprise; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.ArgumentMatchers.any; 22 import static org.mockito.ArgumentMatchers.anyInt; 23 import static org.mockito.ArgumentMatchers.argThat; 24 import static org.mockito.ArgumentMatchers.eq; 25 import static org.mockito.Mockito.mock; 26 import static org.mockito.Mockito.never; 27 import static org.mockito.Mockito.reset; 28 import static org.mockito.Mockito.verify; 29 import static org.mockito.Mockito.when; 30 31 import android.app.admin.DevicePolicyManager; 32 import android.content.ComponentName; 33 import android.content.Context; 34 import android.content.Intent; 35 import android.content.pm.ActivityInfo; 36 import android.content.pm.ApplicationInfo; 37 import android.content.pm.PackageManager; 38 import android.content.pm.ResolveInfo; 39 import android.content.pm.UserInfo; 40 import android.content.res.Resources; 41 import android.net.ConnectivityManager; 42 import android.net.VpnManager; 43 import android.os.UserHandle; 44 import android.os.UserManager; 45 import android.provider.Settings; 46 import android.text.SpannableStringBuilder; 47 48 import com.android.settings.R; 49 50 import com.google.common.collect.ImmutableList; 51 52 import org.junit.Before; 53 import org.junit.Ignore; 54 import org.junit.Test; 55 import org.junit.runner.RunWith; 56 import org.mockito.ArgumentMatcher; 57 import org.mockito.Mock; 58 import org.mockito.MockitoAnnotations; 59 import org.robolectric.RobolectricTestRunner; 60 import org.robolectric.RuntimeEnvironment; 61 62 import java.util.ArrayList; 63 import java.util.Arrays; 64 import java.util.Date; 65 import java.util.List; 66 67 68 @RunWith(RobolectricTestRunner.class) 69 public class EnterprisePrivacyFeatureProviderImplTest { 70 71 private static final String OWNER_ORGANIZATION = "ACME"; 72 private static final String VPN_PACKAGE_ID = "com.example.vpn"; 73 private static final String IME_PACKAGE_ID = "com.example.ime"; 74 private static final String IME_PACKAGE_LABEL = "Test IME"; 75 76 private final ComponentName mOwner = new ComponentName("mock", "component"); 77 private final ComponentName mAdmin1 = new ComponentName("mock", "admin1"); 78 private final ComponentName mAdmin2 = new ComponentName("mock", "admin2"); 79 private final Date mDate = new Date(2011, 11, 11); 80 private final int mUserId = UserHandle.myUserId(); 81 private final int mManagedProfileUserId = mUserId + 1; 82 83 private List<UserInfo> mProfiles = new ArrayList<>(); 84 85 @Mock 86 private Context mContext; 87 @Mock 88 private DevicePolicyManager mDevicePolicyManager; 89 @Mock 90 private PackageManager mPackageManager; 91 @Mock 92 private UserManager mUserManager; 93 @Mock 94 private ConnectivityManager mConnectivityManger; 95 @Mock 96 private VpnManager mVpnManager; 97 private Resources mResources; 98 99 private EnterprisePrivacyFeatureProvider mProvider; 100 101 @Before setUp()102 public void setUp() { 103 MockitoAnnotations.initMocks(this); 104 105 when(mContext.getApplicationContext()).thenReturn(mContext); 106 resetAndInitializePackageManager(); 107 when(mUserManager.getProfiles(mUserId)).thenReturn(mProfiles); 108 when(mContext.getSystemService(Context.DEVICE_POLICY_SERVICE)) 109 .thenReturn(mDevicePolicyManager); 110 when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); 111 when(mContext.getPackageManager()).thenReturn(mPackageManager); 112 mProfiles.add(new UserInfo(mUserId, "", "", 0 /* flags */)); 113 mResources = RuntimeEnvironment.application.getResources(); 114 115 mProvider = new EnterprisePrivacyFeatureProviderImpl(mContext, mDevicePolicyManager, 116 mPackageManager, mUserManager, mConnectivityManger, mVpnManager, mResources); 117 } 118 119 @Test testHasDeviceOwner()120 public void testHasDeviceOwner() { 121 when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(null); 122 assertThat(mProvider.hasDeviceOwner()).isFalse(); 123 124 when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(mOwner); 125 assertThat(mProvider.hasDeviceOwner()).isTrue(); 126 } 127 128 @Test testIsInCompMode()129 public void testIsInCompMode() { 130 when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(mOwner); 131 assertThat(mProvider.isInCompMode()).isFalse(); 132 133 mProfiles.add(new UserInfo(mManagedProfileUserId, "", "", UserInfo.FLAG_MANAGED_PROFILE)); 134 assertThat(mProvider.isInCompMode()).isTrue(); 135 } 136 137 @Test testGetDeviceOwnerOrganizationName()138 public void testGetDeviceOwnerOrganizationName() { 139 when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn(null); 140 assertThat(mProvider.getDeviceOwnerOrganizationName()).isNull(); 141 142 when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn(OWNER_ORGANIZATION); 143 assertThat(mProvider.getDeviceOwnerOrganizationName()).isEqualTo(OWNER_ORGANIZATION); 144 } 145 146 @Test 147 @Ignore testGetDeviceOwnerDisclosure()148 public void testGetDeviceOwnerDisclosure() { 149 when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(null); 150 assertThat(mProvider.getDeviceOwnerDisclosure()).isNull(); 151 152 SpannableStringBuilder disclosure = new SpannableStringBuilder(); 153 disclosure.append(mResources.getString(R.string.do_disclosure_generic)); 154 when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(mOwner); 155 when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn(null); 156 assertThat(mProvider.getDeviceOwnerDisclosure()).isEqualTo(disclosure); 157 158 disclosure = new SpannableStringBuilder(); 159 disclosure.append(mResources.getString(R.string.do_disclosure_with_name, 160 OWNER_ORGANIZATION)); 161 when(mDevicePolicyManager.getDeviceOwnerOrganizationName()).thenReturn(OWNER_ORGANIZATION); 162 assertThat(mProvider.getDeviceOwnerDisclosure()).isEqualTo(disclosure); 163 } 164 165 @Test testGetLastSecurityLogRetrievalTime()166 public void testGetLastSecurityLogRetrievalTime() { 167 when(mDevicePolicyManager.getLastSecurityLogRetrievalTime()).thenReturn(-1L); 168 assertThat(mProvider.getLastSecurityLogRetrievalTime()).isNull(); 169 170 when(mDevicePolicyManager.getLastSecurityLogRetrievalTime()) 171 .thenReturn(mDate.getTime()); 172 assertThat(mProvider.getLastSecurityLogRetrievalTime()).isEqualTo(mDate); 173 } 174 175 @Test testGetLastBugReportRequestTime()176 public void testGetLastBugReportRequestTime() { 177 when(mDevicePolicyManager.getLastBugReportRequestTime()).thenReturn(-1L); 178 assertThat(mProvider.getLastBugReportRequestTime()).isNull(); 179 180 when(mDevicePolicyManager.getLastBugReportRequestTime()).thenReturn(mDate.getTime()); 181 assertThat(mProvider.getLastBugReportRequestTime()).isEqualTo(mDate); 182 } 183 184 @Test testGetLastNetworkLogRetrievalTime()185 public void testGetLastNetworkLogRetrievalTime() { 186 when(mDevicePolicyManager.getLastNetworkLogRetrievalTime()).thenReturn(-1L); 187 assertThat(mProvider.getLastNetworkLogRetrievalTime()).isNull(); 188 189 when(mDevicePolicyManager.getLastNetworkLogRetrievalTime()).thenReturn(mDate.getTime()); 190 assertThat(mProvider.getLastNetworkLogRetrievalTime()).isEqualTo(mDate); 191 } 192 193 @Test testIsSecurityLoggingEnabled()194 public void testIsSecurityLoggingEnabled() { 195 when(mDevicePolicyManager.isSecurityLoggingEnabled(null)).thenReturn(false); 196 assertThat(mProvider.isSecurityLoggingEnabled()).isFalse(); 197 198 when(mDevicePolicyManager.isSecurityLoggingEnabled(null)).thenReturn(true); 199 assertThat(mProvider.isSecurityLoggingEnabled()).isTrue(); 200 } 201 202 @Test testIsNetworkLoggingEnabled()203 public void testIsNetworkLoggingEnabled() { 204 when(mDevicePolicyManager.isNetworkLoggingEnabled(null)).thenReturn(false); 205 assertThat(mProvider.isNetworkLoggingEnabled()).isFalse(); 206 207 when(mDevicePolicyManager.isNetworkLoggingEnabled(null)).thenReturn(true); 208 assertThat(mProvider.isNetworkLoggingEnabled()).isTrue(); 209 } 210 211 @Test testIsAlwaysOnVpnSetInCurrentUser()212 public void testIsAlwaysOnVpnSetInCurrentUser() { 213 when(mVpnManager.getAlwaysOnVpnPackageForUser(mUserId)).thenReturn(null); 214 assertThat(mProvider.isAlwaysOnVpnSetInCurrentUser()).isFalse(); 215 216 when(mVpnManager.getAlwaysOnVpnPackageForUser(mUserId)).thenReturn(VPN_PACKAGE_ID); 217 assertThat(mProvider.isAlwaysOnVpnSetInCurrentUser()).isTrue(); 218 } 219 220 @Test testIsAlwaysOnVpnSetInManagedProfileProfile()221 public void testIsAlwaysOnVpnSetInManagedProfileProfile() { 222 assertThat(mProvider.isAlwaysOnVpnSetInManagedProfile()).isFalse(); 223 224 mProfiles.add(new UserInfo(mManagedProfileUserId, "", "", UserInfo.FLAG_MANAGED_PROFILE)); 225 226 when(mVpnManager.getAlwaysOnVpnPackageForUser(mManagedProfileUserId)).thenReturn(null); 227 assertThat(mProvider.isAlwaysOnVpnSetInManagedProfile()).isFalse(); 228 229 when(mVpnManager.getAlwaysOnVpnPackageForUser(mManagedProfileUserId)) 230 .thenReturn(VPN_PACKAGE_ID); 231 assertThat(mProvider.isAlwaysOnVpnSetInManagedProfile()).isTrue(); 232 } 233 234 @Test testGetMaximumFailedPasswordsForWipeInCurrentUser()235 public void testGetMaximumFailedPasswordsForWipeInCurrentUser() { 236 when(mDevicePolicyManager.getDeviceOwnerComponentOnCallingUser()).thenReturn(null); 237 when(mDevicePolicyManager.getProfileOwnerAsUser(mUserId)).thenReturn(null); 238 when(mDevicePolicyManager.getMaximumFailedPasswordsForWipe(mOwner, mUserId)) 239 .thenReturn(10); 240 assertThat(mProvider.getMaximumFailedPasswordsBeforeWipeInCurrentUser()).isEqualTo(0); 241 242 when(mDevicePolicyManager.getProfileOwnerAsUser(mUserId)).thenReturn(mOwner); 243 assertThat(mProvider.getMaximumFailedPasswordsBeforeWipeInCurrentUser()).isEqualTo(10); 244 245 when(mDevicePolicyManager.getDeviceOwnerComponentOnCallingUser()).thenReturn(mOwner); 246 when(mDevicePolicyManager.getProfileOwnerAsUser(mUserId)).thenReturn(null); 247 assertThat(mProvider.getMaximumFailedPasswordsBeforeWipeInCurrentUser()).isEqualTo(10); 248 } 249 250 @Test testGetMaximumFailedPasswordsForWipeInManagedProfile()251 public void testGetMaximumFailedPasswordsForWipeInManagedProfile() { 252 when(mDevicePolicyManager.getProfileOwnerAsUser(mManagedProfileUserId)).thenReturn(mOwner); 253 when(mDevicePolicyManager.getMaximumFailedPasswordsForWipe(mOwner, mManagedProfileUserId)) 254 .thenReturn(10); 255 assertThat(mProvider.getMaximumFailedPasswordsBeforeWipeInManagedProfile()).isEqualTo(0); 256 257 mProfiles.add(new UserInfo(mManagedProfileUserId, "", "", UserInfo.FLAG_MANAGED_PROFILE)); 258 assertThat(mProvider.getMaximumFailedPasswordsBeforeWipeInManagedProfile()).isEqualTo(10); 259 } 260 261 @Test testGetImeLabelIfOwnerSet()262 public void testGetImeLabelIfOwnerSet() throws Exception { 263 final ApplicationInfo applicationInfo = mock(ApplicationInfo.class); 264 when(applicationInfo.loadLabel(mPackageManager)).thenReturn(IME_PACKAGE_LABEL); 265 266 Settings.Secure.putString(null, Settings.Secure.DEFAULT_INPUT_METHOD, IME_PACKAGE_ID); 267 when(mPackageManager.getApplicationInfoAsUser(IME_PACKAGE_ID, 0, mUserId)) 268 .thenReturn(applicationInfo); 269 270 // IME not set by Device Owner. 271 when(mDevicePolicyManager.isCurrentInputMethodSetByOwner()).thenReturn(false); 272 assertThat(mProvider.getImeLabelIfOwnerSet()).isNull(); 273 274 // Device Owner set IME to empty string. 275 when(mDevicePolicyManager.isCurrentInputMethodSetByOwner()).thenReturn(true); 276 Settings.Secure.putString(null, Settings.Secure.DEFAULT_INPUT_METHOD, null); 277 assertThat(mProvider.getImeLabelIfOwnerSet()).isNull(); 278 279 // Device Owner set IME to nonexistent package. 280 Settings.Secure.putString(null, Settings.Secure.DEFAULT_INPUT_METHOD, IME_PACKAGE_ID); 281 when(mPackageManager.getApplicationInfoAsUser(IME_PACKAGE_ID, 0, mUserId)) 282 .thenThrow(new PackageManager.NameNotFoundException()); 283 assertThat(mProvider.getImeLabelIfOwnerSet()).isNull(); 284 285 // Device Owner set IME to existent package. 286 resetAndInitializePackageManager(); 287 when(mPackageManager.getApplicationInfoAsUser(IME_PACKAGE_ID, 0, mUserId)) 288 .thenReturn(applicationInfo); 289 assertThat(mProvider.getImeLabelIfOwnerSet()).isEqualTo(IME_PACKAGE_LABEL); 290 } 291 292 @Test testGetNumberOfOwnerInstalledCaCertsForCurrent()293 public void testGetNumberOfOwnerInstalledCaCertsForCurrent() { 294 final UserHandle userHandle = new UserHandle(UserHandle.USER_SYSTEM); 295 final UserHandle managedProfileUserHandle = new UserHandle(mManagedProfileUserId); 296 297 when(mDevicePolicyManager.getOwnerInstalledCaCerts(managedProfileUserHandle)) 298 .thenReturn(Arrays.asList("ca1", "ca2")); 299 300 when(mDevicePolicyManager.getOwnerInstalledCaCerts(userHandle)) 301 .thenReturn(null); 302 assertThat(mProvider.getNumberOfOwnerInstalledCaCertsForCurrentUser()) 303 .isEqualTo(0); 304 when(mDevicePolicyManager.getOwnerInstalledCaCerts(userHandle)) 305 .thenReturn(new ArrayList<>()); 306 assertThat(mProvider.getNumberOfOwnerInstalledCaCertsForCurrentUser()) 307 .isEqualTo(0); 308 when(mDevicePolicyManager.getOwnerInstalledCaCerts(userHandle)) 309 .thenReturn(Arrays.asList("ca1", "ca2")); 310 assertThat(mProvider.getNumberOfOwnerInstalledCaCertsForCurrentUser()) 311 .isEqualTo(2); 312 } 313 314 @Test testGetNumberOfOwnerInstalledCaCertsForManagedProfile()315 public void testGetNumberOfOwnerInstalledCaCertsForManagedProfile() { 316 final UserHandle userHandle = new UserHandle(UserHandle.USER_SYSTEM); 317 final UserHandle managedProfileUserHandle = new UserHandle(mManagedProfileUserId); 318 final UserInfo managedProfile = 319 new UserInfo(mManagedProfileUserId, "", "", UserInfo.FLAG_MANAGED_PROFILE); 320 321 // Without a profile 322 when(mDevicePolicyManager.getOwnerInstalledCaCerts(managedProfileUserHandle)) 323 .thenReturn(Arrays.asList("ca1", "ca2")); 324 assertThat(mProvider.getNumberOfOwnerInstalledCaCertsForManagedProfile()) 325 .isEqualTo(0); 326 327 // With a profile 328 mProfiles.add(managedProfile); 329 when(mDevicePolicyManager.getOwnerInstalledCaCerts(managedProfileUserHandle)) 330 .thenReturn(null); 331 assertThat(mProvider.getNumberOfOwnerInstalledCaCertsForManagedProfile()) 332 .isEqualTo(0); 333 when(mDevicePolicyManager.getOwnerInstalledCaCerts(userHandle)) 334 .thenReturn(new ArrayList<>()); 335 assertThat(mProvider.getNumberOfOwnerInstalledCaCertsForManagedProfile()) 336 .isEqualTo(0); 337 when(mDevicePolicyManager.getOwnerInstalledCaCerts(managedProfileUserHandle)) 338 .thenReturn(Arrays.asList("ca1", "ca2")); 339 assertThat(mProvider.getNumberOfOwnerInstalledCaCertsForManagedProfile()) 340 .isEqualTo(2); 341 } 342 343 @Test testGetNumberOfActiveDeviceAdminsForCurrentUserAndManagedProfile()344 public void testGetNumberOfActiveDeviceAdminsForCurrentUserAndManagedProfile() { 345 when(mDevicePolicyManager.getActiveAdminsAsUser(mUserId)) 346 .thenReturn(Arrays.asList(mAdmin1, mAdmin2)); 347 when(mDevicePolicyManager.getActiveAdminsAsUser(mManagedProfileUserId)) 348 .thenReturn(Arrays.asList(mAdmin1)); 349 350 assertThat(mProvider.getNumberOfActiveDeviceAdminsForCurrentUserAndManagedProfile()) 351 .isEqualTo(2); 352 353 mProfiles.add(new UserInfo(mManagedProfileUserId, "", "", UserInfo.FLAG_MANAGED_PROFILE)); 354 assertThat(mProvider.getNumberOfActiveDeviceAdminsForCurrentUserAndManagedProfile()) 355 .isEqualTo(3); 356 } 357 358 @Test workPolicyInfo_unmanagedDevice_shouldDoNothing()359 public void workPolicyInfo_unmanagedDevice_shouldDoNothing() { 360 // Even if we have the intent resolved, don't show it if there's no DO or PO 361 when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(null); 362 addWorkPolicyInfoIntent(mOwner.getPackageName(), true, false); 363 assertThat(mProvider.hasWorkPolicyInfo()).isFalse(); 364 365 assertThat(mProvider.showWorkPolicyInfo(mContext)).isFalse(); 366 verify(mContext, never()).startActivity(any()); 367 } 368 369 @Test workPolicyInfo_deviceOwner_shouldResolveIntent()370 public void workPolicyInfo_deviceOwner_shouldResolveIntent() { 371 // If the intent is not resolved, then there's no info to show for DO 372 when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(mOwner); 373 assertThat(mProvider.hasWorkPolicyInfo()).isFalse(); 374 assertThat(mProvider.showWorkPolicyInfo(mContext)).isFalse(); 375 376 // If the intent is resolved, then we can use it to launch the activity 377 Intent intent = addWorkPolicyInfoIntent(mOwner.getPackageName(), true, false); 378 assertThat(mProvider.hasWorkPolicyInfo()).isTrue(); 379 assertThat(mProvider.showWorkPolicyInfo(mContext)).isTrue(); 380 verify(mContext).startActivity(intentEquals(intent)); 381 } 382 383 @Test workPolicyInfo_profileOwner_shouldResolveIntent()384 public void workPolicyInfo_profileOwner_shouldResolveIntent() 385 throws PackageManager.NameNotFoundException { 386 when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(null); 387 List<UserHandle> mAllProfiles = new ArrayList<>(); 388 mAllProfiles.add(new UserHandle(mManagedProfileUserId)); 389 when(mUserManager.getAllProfiles()).thenReturn(mAllProfiles); 390 when(mUserManager.isManagedProfile(mManagedProfileUserId)).thenReturn(true); 391 when(mContext.getPackageName()).thenReturn("somePackageName"); 392 when(mContext.createPackageContextAsUser( 393 eq(mContext.getPackageName()), 394 anyInt(), 395 any(UserHandle.class)) 396 ).thenReturn(mContext); 397 when(mContext.getSystemService(Context.DEVICE_POLICY_SERVICE)) 398 .thenReturn(mDevicePolicyManager); 399 when(mDevicePolicyManager.getProfileOwner()).thenReturn(mOwner); 400 401 // If the intent is not resolved, then there's no info to show for PO 402 assertThat(mProvider.hasWorkPolicyInfo()).isFalse(); 403 assertThat(mProvider.showWorkPolicyInfo(mContext)).isFalse(); 404 405 // If the intent is resolved, then we can use it to launch the activity in managed profile 406 Intent intent = addWorkPolicyInfoIntent(mOwner.getPackageName(), false, true); 407 assertThat(mProvider.hasWorkPolicyInfo()).isTrue(); 408 assertThat(mProvider.showWorkPolicyInfo(mContext)).isTrue(); 409 verify(mContext) 410 .startActivityAsUser( 411 intentEquals(intent), 412 argThat(handle -> handle.getIdentifier() == mManagedProfileUserId)); 413 } 414 415 @Test workPolicyInfo_comp_shouldUseDeviceOwnerIntent()416 public void workPolicyInfo_comp_shouldUseDeviceOwnerIntent() { 417 when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(mOwner); 418 mProfiles.add(new UserInfo(mManagedProfileUserId, "", "", UserInfo.FLAG_MANAGED_PROFILE)); 419 when(mDevicePolicyManager.getProfileOwnerAsUser(mUserId)).thenReturn(mOwner); 420 421 // If the intent is not resolved, then there's no info to show for COMP 422 assertThat(mProvider.hasWorkPolicyInfo()).isFalse(); 423 assertThat(mProvider.showWorkPolicyInfo(mContext)).isFalse(); 424 425 // If the intent is resolved, then we can use it to launch the activity for device owner 426 Intent intent = addWorkPolicyInfoIntent(mOwner.getPackageName(), true, true); 427 assertThat(mProvider.hasWorkPolicyInfo()).isTrue(); 428 assertThat(mProvider.showWorkPolicyInfo(mContext)).isTrue(); 429 verify(mContext).startActivity(intentEquals(intent)); 430 } 431 432 @Test testShowParentalControls()433 public void testShowParentalControls() { 434 when(mDevicePolicyManager.getProfileOwnerOrDeviceOwnerSupervisionComponent(any())) 435 .thenReturn(mOwner); 436 437 // If the intent is resolved, then we can use it to launch the activity 438 Intent intent = addParentalControlsIntent(mOwner.getPackageName()); 439 assertThat(mProvider.showParentalControls()).isTrue(); 440 verify(mContext).startActivity(intentEquals(intent)); 441 } 442 addWorkPolicyInfoIntent( String packageName, boolean deviceOwner, boolean profileOwner)443 private Intent addWorkPolicyInfoIntent( 444 String packageName, boolean deviceOwner, boolean profileOwner) { 445 Intent intent = new Intent(Settings.ACTION_SHOW_WORK_POLICY_INFO); 446 intent.setPackage(packageName); 447 ResolveInfo resolveInfo = new ResolveInfo(); 448 resolveInfo.resolvePackageName = packageName; 449 resolveInfo.activityInfo = new ActivityInfo(); 450 resolveInfo.activityInfo.name = "activityName"; 451 resolveInfo.activityInfo.packageName = packageName; 452 453 List<ResolveInfo> activities = ImmutableList.of(resolveInfo); 454 if (deviceOwner) { 455 when(mPackageManager.queryIntentActivities(intentEquals(intent), anyInt())) 456 .thenReturn(activities); 457 } 458 if (profileOwner) { 459 when(mPackageManager.queryIntentActivitiesAsUser( 460 intentEquals(intent), anyInt(), eq(UserHandle.of(mManagedProfileUserId)))) 461 .thenReturn(activities); 462 } 463 464 return intent; 465 } 466 addParentalControlsIntent(String packageName)467 private Intent addParentalControlsIntent(String packageName) { 468 Intent intent = new Intent(EnterprisePrivacyFeatureProviderImpl.ACTION_PARENTAL_CONTROLS); 469 intent.setPackage(packageName); 470 ResolveInfo resolveInfo = new ResolveInfo(); 471 resolveInfo.resolvePackageName = packageName; 472 resolveInfo.activityInfo = new ActivityInfo(); 473 resolveInfo.activityInfo.name = "activityName"; 474 resolveInfo.activityInfo.packageName = packageName; 475 476 List<ResolveInfo> activities = ImmutableList.of(resolveInfo); 477 when(mPackageManager.queryIntentActivities(intentEquals(intent), anyInt())) 478 .thenReturn(activities); 479 when(mPackageManager.queryIntentActivitiesAsUser(intentEquals(intent), anyInt(), anyInt())) 480 .thenReturn(activities); 481 return intent; 482 } 483 484 private static class IntentMatcher implements ArgumentMatcher<Intent> { 485 private final Intent mExpectedIntent; 486 IntentMatcher(Intent expectedIntent)487 public IntentMatcher(Intent expectedIntent) { 488 mExpectedIntent = expectedIntent; 489 } 490 491 @Override matches(Intent actualIntent)492 public boolean matches(Intent actualIntent) { 493 // filterEquals() compares only the action, data, type, class, and categories. 494 return actualIntent != null && mExpectedIntent.filterEquals(actualIntent); 495 } 496 } 497 intentEquals(Intent intent)498 private static Intent intentEquals(Intent intent) { 499 return argThat(new IntentMatcher(intent)); 500 } 501 resetAndInitializePackageManager()502 private void resetAndInitializePackageManager() { 503 reset(mPackageManager); 504 when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN)) 505 .thenReturn(true); 506 } 507 } 508