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