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 android.content.pm; 18 19 import static org.mockito.ArgumentMatchers.anyInt; 20 import static org.mockito.ArgumentMatchers.nullable; 21 import static org.mockito.Mockito.verify; 22 import static org.mockito.Mockito.when; 23 24 import android.content.Context; 25 import android.content.res.Resources; 26 import android.graphics.drawable.Drawable; 27 import android.os.UserHandle; 28 import android.os.UserManager; 29 import android.platform.test.annotations.Presubmit; 30 31 import com.android.internal.R; 32 33 import org.junit.Before; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 import org.mockito.Answers; 37 import org.mockito.Mock; 38 import org.mockito.junit.MockitoJUnitRunner; 39 40 import java.util.ArrayList; 41 import java.util.List; 42 43 /** 44 * Build/Install/Run: 45 * atest frameworks/base/core/tests/coretests/src/android/content/pm/CrossProfileAppsTest.java 46 */ 47 @Presubmit 48 @RunWith(MockitoJUnitRunner.class) 49 public class CrossProfileAppsTest { 50 private static final UserHandle PERSONAL_PROFILE = UserHandle.of(0); 51 private static final UserHandle MANAGED_PROFILE = UserHandle.of(10); 52 private static final String MY_PACKAGE = "my.package"; 53 54 private List<UserHandle> mTargetProfiles; 55 56 @Mock 57 private Context mContext; 58 @Mock 59 private UserManager mUserManager; 60 @Mock 61 private ICrossProfileApps mService; 62 @Mock 63 private Resources mResources; 64 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 65 private Drawable mDrawable; 66 private CrossProfileApps mCrossProfileApps; 67 68 @Before 69 public void initCrossProfileApps() { 70 mCrossProfileApps = new CrossProfileApps(mContext, mService); 71 } 72 73 @Before 74 public void mockContext() { 75 when(mContext.getPackageName()).thenReturn(MY_PACKAGE); 76 when(mContext.getSystemServiceName(UserManager.class)).thenReturn(Context.USER_SERVICE); 77 when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); 78 } 79 80 @Before 81 public void mockResources() { 82 when(mContext.getResources()).thenReturn(mResources); 83 when(mResources.getDrawable(anyInt(), nullable(Resources.Theme.class))) 84 .thenReturn(mDrawable); 85 } 86 87 @Before 88 public void initUsers() throws Exception { 89 when(mUserManager.isManagedProfile(PERSONAL_PROFILE.getIdentifier())).thenReturn(false); 90 when(mUserManager.isManagedProfile(MANAGED_PROFILE.getIdentifier())).thenReturn(true); 91 92 mTargetProfiles = new ArrayList<>(); 93 when(mService.getTargetUserProfiles(MY_PACKAGE)).thenReturn(mTargetProfiles); 94 } 95 96 @Test 97 public void getProfileSwitchingLabel_managedProfile() { 98 setValidTargetProfile(MANAGED_PROFILE); 99 100 mCrossProfileApps.getProfileSwitchingLabel(MANAGED_PROFILE); 101 verify(mResources).getString(R.string.managed_profile_label); 102 } 103 104 @Test 105 public void getProfileSwitchingLabel_personalProfile() { 106 setValidTargetProfile(PERSONAL_PROFILE); 107 108 mCrossProfileApps.getProfileSwitchingLabel(PERSONAL_PROFILE); 109 verify(mResources).getString(R.string.user_owner_label); 110 } 111 112 @Test(expected = SecurityException.class) 113 public void getProfileSwitchingLabel_securityException() { 114 mCrossProfileApps.getProfileSwitchingLabel(PERSONAL_PROFILE); 115 } 116 117 @Test 118 public void getProfileSwitchingIcon_managedProfile() { 119 setValidTargetProfile(MANAGED_PROFILE); 120 121 mCrossProfileApps.getProfileSwitchingIconDrawable(MANAGED_PROFILE); 122 verify(mResources).getDrawable(R.drawable.ic_corp_badge, null); 123 } 124 125 @Test 126 public void getProfileSwitchingIcon_personalProfile() { 127 setValidTargetProfile(PERSONAL_PROFILE); 128 129 mCrossProfileApps.getProfileSwitchingIconDrawable(PERSONAL_PROFILE); 130 verify(mResources).getDrawable(R.drawable.ic_account_circle, null); 131 } 132 133 @Test(expected = SecurityException.class) 134 public void getProfileSwitchingIcon_securityException() { 135 mCrossProfileApps.getProfileSwitchingIconDrawable(PERSONAL_PROFILE); 136 } 137 138 private void setValidTargetProfile(UserHandle userHandle) { 139 mTargetProfiles.add(userHandle); 140 } 141 } 142