1 /* 2 * Copyright (C) 2018 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 org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertNotNull; 21 import static org.junit.Assert.assertNull; 22 import static org.junit.Assert.assertTrue; 23 import static org.mockito.ArgumentMatchers.any; 24 import static org.mockito.ArgumentMatchers.anyInt; 25 import static org.mockito.Mockito.mock; 26 import static org.mockito.Mockito.never; 27 import static org.mockito.Mockito.verify; 28 29 import android.app.Activity; 30 import android.app.admin.DevicePolicyManager; 31 import android.content.ComponentName; 32 import android.content.Intent; 33 import android.content.pm.UserInfo; 34 import android.os.Process; 35 import android.os.UserHandle; 36 import android.os.UserManager; 37 import android.view.ViewGroup; 38 import android.widget.FrameLayout; 39 import android.widget.TextView; 40 41 import androidx.appcompat.app.AlertDialog; 42 43 import com.android.settings.R; 44 import com.android.settings.Settings; 45 import com.android.settings.applications.specialaccess.deviceadmin.DeviceAdminAdd; 46 import com.android.settings.testutils.CustomActivity; 47 import com.android.settings.testutils.shadow.ShadowActivity; 48 import com.android.settings.testutils.shadow.ShadowDevicePolicyManager; 49 import com.android.settings.testutils.shadow.ShadowUserManager; 50 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; 51 52 import org.junit.Before; 53 import org.junit.Test; 54 import org.junit.runner.RunWith; 55 import org.robolectric.Robolectric; 56 import org.robolectric.RobolectricTestRunner; 57 import org.robolectric.RuntimeEnvironment; 58 import org.robolectric.Shadows; 59 import org.robolectric.annotation.Config; 60 import org.robolectric.shadow.api.Shadow; 61 import org.robolectric.shadows.ShadowProcess; 62 63 @RunWith(RobolectricTestRunner.class) 64 @Config(shadows = { 65 ShadowDevicePolicyManager.class, 66 ShadowUserManager.class, 67 ShadowActivity.class 68 }) 69 public class ActionDisabledByAdminDialogHelperTest { 70 private ActionDisabledByAdminDialogHelper mHelper; 71 private Activity mActivity; 72 private org.robolectric.shadows.ShadowActivity mActivityShadow; 73 74 @Before setUp()75 public void setUp() { 76 mActivity = Robolectric.setupActivity(CustomActivity.class); 77 mActivityShadow = Shadow.extract(mActivity); 78 mHelper = new ActionDisabledByAdminDialogHelper(mActivity); 79 } 80 81 @Test testShowAdminPoliciesWithComponent()82 public void testShowAdminPoliciesWithComponent() { 83 final int userId = 123; 84 final ComponentName component = new ComponentName("some.package.name", 85 "some.package.name.SomeClass"); 86 final EnforcedAdmin admin = new EnforcedAdmin(component, UserHandle.of(userId)); 87 88 mHelper.showAdminPolicies(admin, mActivity); 89 90 final Intent intent = mActivityShadow.getNextStartedActivity(); 91 assertTrue( 92 intent.getBooleanExtra(DeviceAdminAdd.EXTRA_CALLED_FROM_SUPPORT_DIALOG, false)); 93 assertEquals(component, 94 intent.getParcelableExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN)); 95 } 96 97 @Test testShowAdminPoliciesWithoutComponent()98 public void testShowAdminPoliciesWithoutComponent() { 99 final int userId = 123; 100 final EnforcedAdmin admin = new EnforcedAdmin(null, UserHandle.of(userId)); 101 mHelper.showAdminPolicies(admin, mActivity); 102 final Intent intent = mActivityShadow.getNextStartedActivity(); 103 assertEquals(intent.getComponent(), new ComponentName(mActivity, 104 Settings.DeviceAdminSettingsActivity.class.getName())); 105 } 106 107 @Test testSetAdminSupportTitle()108 public void testSetAdminSupportTitle() { 109 final ViewGroup view = new FrameLayout(mActivity); 110 final TextView textView = new TextView(mActivity); 111 textView.setId(R.id.admin_support_dialog_title); 112 view.addView(textView); 113 114 mHelper.setAdminSupportTitle(view, UserManager.DISALLOW_ADJUST_VOLUME); 115 assertEquals(Shadows.shadowOf(textView).innerText(), 116 mActivity.getString(R.string.disabled_by_policy_title_adjust_volume)); 117 118 mHelper.setAdminSupportTitle(view, UserManager.DISALLOW_OUTGOING_CALLS); 119 assertEquals(Shadows.shadowOf(textView).innerText(), 120 mActivity.getString(R.string.disabled_by_policy_title_outgoing_calls)); 121 122 mHelper.setAdminSupportTitle(view, UserManager.DISALLOW_SMS); 123 assertEquals(Shadows.shadowOf(textView).innerText(), 124 mActivity.getString(R.string.disabled_by_policy_title_sms)); 125 126 mHelper.setAdminSupportTitle(view, DevicePolicyManager.POLICY_DISABLE_CAMERA); 127 assertEquals(Shadows.shadowOf(textView).innerText(), 128 mActivity.getString(R.string.disabled_by_policy_title_camera)); 129 130 mHelper.setAdminSupportTitle(view, DevicePolicyManager.POLICY_DISABLE_SCREEN_CAPTURE); 131 assertEquals(Shadows.shadowOf(textView).innerText(), 132 mActivity.getString(R.string.disabled_by_policy_title_screen_capture)); 133 134 mHelper.setAdminSupportTitle(view, DevicePolicyManager.POLICY_SUSPEND_PACKAGES); 135 assertEquals(Shadows.shadowOf(textView).innerText(), 136 mActivity.getString(R.string.disabled_by_policy_title_suspend_packages)); 137 138 mHelper.setAdminSupportTitle(view, "another restriction"); 139 assertEquals(Shadows.shadowOf(textView).innerText(), 140 mActivity.getString(R.string.disabled_by_policy_title)); 141 142 mHelper.setAdminSupportTitle(view, null); 143 assertEquals(Shadows.shadowOf(textView).innerText(), 144 mActivity.getString(R.string.disabled_by_policy_title)); 145 } 146 147 @Test testSetAdminSupportDetails()148 public void testSetAdminSupportDetails() { 149 final ShadowDevicePolicyManager dpmShadow = ShadowDevicePolicyManager.getShadow(); 150 final UserManager userManager = RuntimeEnvironment.application.getSystemService( 151 UserManager.class); 152 final ShadowUserManager userManagerShadow = Shadow.extract(userManager); 153 final ViewGroup view = new FrameLayout(mActivity); 154 final ComponentName component = new ComponentName("some.package.name", 155 "some.package.name.SomeClass"); 156 final EnforcedAdmin admin = new EnforcedAdmin(component, UserHandle.of(123)); 157 final TextView textView = new TextView(mActivity); 158 159 textView.setId(R.id.admin_support_msg); 160 view.addView(textView); 161 dpmShadow.setShortSupportMessageForUser(component, 123, "some message"); 162 dpmShadow.setIsAdminActiveAsUser(true); 163 userManagerShadow.addProfile(new UserInfo(123, null, 0)); 164 userManagerShadow.addUserProfile(new UserHandle(123)); 165 ShadowProcess.setUid(Process.SYSTEM_UID); 166 167 mHelper.setAdminSupportDetails(mActivity, view, admin); 168 assertNotNull(admin.component); 169 assertEquals("some message", Shadows.shadowOf(textView).innerText()); 170 } 171 172 @Test testSetAdminSupportDetailsNotAdmin()173 public void testSetAdminSupportDetailsNotAdmin() { 174 final ShadowDevicePolicyManager dpmShadow = ShadowDevicePolicyManager.getShadow(); 175 final UserManager userManager = RuntimeEnvironment.application.getSystemService( 176 UserManager.class); 177 final ShadowUserManager userManagerShadow = Shadow.extract(userManager); 178 final ComponentName component = new ComponentName("some.package.name", 179 "some.package.name.SomeClass"); 180 final EnforcedAdmin admin = new EnforcedAdmin(component, UserHandle.of(123)); 181 182 dpmShadow.setShortSupportMessageForUser(component, 123, "some message"); 183 dpmShadow.setIsAdminActiveAsUser(false); 184 userManagerShadow.addProfile(new UserInfo(123, null, 0)); 185 186 mHelper.setAdminSupportDetails(mActivity, null, admin); 187 assertNull(admin.component); 188 } 189 190 @Test testMaybeSetLearnMoreButton()191 public void testMaybeSetLearnMoreButton() { 192 final UserManager userManager = RuntimeEnvironment.application.getSystemService( 193 UserManager.class); 194 final ShadowUserManager userManagerShadow = Shadow.extract(userManager); 195 final ComponentName component = new ComponentName("some.package.name", 196 "some.package.name.SomeClass"); 197 mHelper.mEnforcedAdmin = new EnforcedAdmin(component, UserHandle.of(123)); 198 199 // Set up for shadow call. 200 userManagerShadow.getSameProfileGroupIds().put(123, 0); 201 202 // Test that the button is shown when user IDs are in the same profile group 203 AlertDialog.Builder builder = mock(AlertDialog.Builder.class); 204 mHelper.maybeSetLearnMoreButton(builder); 205 verify(builder).setNeutralButton(anyInt(), any()); 206 207 // Test that the button is not shown when user IDs are not in the same profile group 208 userManagerShadow.getSameProfileGroupIds().clear(); 209 builder = mock(AlertDialog.Builder.class); 210 mHelper.maybeSetLearnMoreButton(builder); 211 verify(builder, never()).setNeutralButton(anyInt(), any()); 212 } 213 } 214