1 /* 2 * Copyright (C) 2024 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.server.usbtest; 18 19 import static android.provider.Settings.Secure.USER_SETUP_COMPLETE; 20 21 import static com.android.server.usb.UsbProfileGroupSettingsManager.PROPERTY_RESTRICT_USB_OVERLAY_ACTIVITIES; 22 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.Mockito.times; 28 import static org.mockito.Mockito.verify; 29 import static org.mockito.Mockito.when; 30 31 import android.app.ActivityManager; 32 import android.content.Context; 33 import android.content.Intent; 34 import android.content.pm.ApplicationInfo; 35 import android.content.pm.PackageInfo; 36 import android.content.pm.PackageManager; 37 import android.content.pm.PackageManager.NameNotFoundException; 38 import android.content.pm.PackageManager.Property; 39 import android.content.pm.UserInfo; 40 import android.content.res.Resources; 41 import android.hardware.usb.UsbDevice; 42 import android.os.UserHandle; 43 import android.os.UserManager; 44 import android.provider.Settings; 45 import android.test.mock.MockContentResolver; 46 47 import androidx.test.runner.AndroidJUnit4; 48 49 import com.android.dx.mockito.inline.extended.ExtendedMockito; 50 import com.android.internal.util.test.FakeSettingsProvider; 51 import com.android.server.usb.UsbHandlerManager; 52 import com.android.server.usb.UsbProfileGroupSettingsManager; 53 import com.android.server.usb.UsbSettingsManager; 54 import com.android.server.usb.UsbUserSettingsManager; 55 import com.android.server.usb.flags.Flags; 56 57 import org.junit.After; 58 import org.junit.Before; 59 import org.junit.Test; 60 import org.junit.runner.RunWith; 61 import org.mockito.Mock; 62 import org.mockito.Mockito; 63 import org.mockito.MockitoAnnotations; 64 import org.mockito.MockitoSession; 65 import org.mockito.quality.Strictness; 66 67 import java.util.ArrayList; 68 import java.util.List; 69 70 /** 71 * Unit tests for {@link com.android.server.usb.UsbProfileGroupSettingsManager}. 72 * Note: MUST claim MANAGE_USB permission in Manifest 73 */ 74 @RunWith(AndroidJUnit4.class) 75 public class UsbProfileGroupSettingsManagerTest { 76 77 private static final String TEST_PACKAGE_NAME = "testPkg"; 78 79 @Mock 80 private Context mContext; 81 @Mock 82 private PackageManager mPackageManager; 83 @Mock 84 private ActivityManager mActivityManager; 85 @Mock 86 private UserHandle mUserHandle; 87 @Mock 88 private UsbSettingsManager mUsbSettingsManager; 89 @Mock 90 private UsbHandlerManager mUsbHandlerManager; 91 @Mock 92 private UserManager mUserManager; 93 @Mock 94 private UsbUserSettingsManager mUsbUserSettingsManager; 95 @Mock 96 private Property mRestrictUsbOverlayActivitiesProperty; 97 @Mock 98 private UsbDevice mUsbDevice; 99 100 private MockContentResolver mContentResolver; 101 private MockitoSession mStaticMockSession; 102 103 private UsbProfileGroupSettingsManager mUsbProfileGroupSettingsManager; 104 105 @Before setUp()106 public void setUp() throws Exception { 107 MockitoAnnotations.initMocks(this); 108 mStaticMockSession = ExtendedMockito.mockitoSession() 109 .mockStatic(Flags.class) 110 .strictness(Strictness.WARN) 111 .startMocking(); 112 113 when(mUsbSettingsManager.getSettingsForUser(anyInt())).thenReturn(mUsbUserSettingsManager); 114 when(mUserManager.getEnabledProfiles(anyInt())) 115 .thenReturn(List.of(Mockito.mock(UserInfo.class))); 116 117 mContentResolver = new MockContentResolver(); 118 mContentResolver.addProvider(Settings.AUTHORITY, new FakeSettingsProvider()); 119 120 when(mContext.getContentResolver()).thenReturn(mContentResolver); 121 when(mContext.getPackageManager()).thenReturn(mPackageManager); 122 when(mContext.getResources()).thenReturn(Mockito.mock(Resources.class)); 123 when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); 124 when(mContext.getSystemService(ActivityManager.class)).thenReturn(mActivityManager); 125 when(mContext.createPackageContextAsUser(anyString(), anyInt(), any(UserHandle.class))) 126 .thenReturn(mContext); 127 128 mUsbProfileGroupSettingsManager = new UsbProfileGroupSettingsManager( 129 mContext, mUserHandle, mUsbSettingsManager, mUsbHandlerManager); 130 131 setupDefaultConfiguration(); 132 } 133 134 /** 135 * Setups the following configuration 136 * 137 * <ul> 138 * <li>Flag is enabled 139 * <li>Device setup has completed 140 * <li>There is a foreground activity with MANAGE_USB permission 141 * <li>The foreground activity has PROPERTY_RESTRICT_USB_OVERLAY_ACTIVITIES enabled 142 * </ul> 143 */ setupDefaultConfiguration()144 private void setupDefaultConfiguration() throws NameNotFoundException { 145 when(Flags.allowRestrictionOfOverlayActivities()).thenReturn(true); 146 147 Settings.Secure.putInt(mContentResolver, USER_SETUP_COMPLETE, 1); 148 149 ActivityManager.RunningAppProcessInfo mRunningAppProcessInfo = 150 new ActivityManager.RunningAppProcessInfo(); 151 mRunningAppProcessInfo.pkgList = new String[] { TEST_PACKAGE_NAME }; 152 when(mActivityManager.getRunningAppProcesses()).thenReturn(List.of(mRunningAppProcessInfo)); 153 154 PackageInfo mPackageInfo = new PackageInfo(); 155 mPackageInfo.packageName = TEST_PACKAGE_NAME; 156 mPackageInfo.applicationInfo = Mockito.mock(ApplicationInfo.class); 157 when(mPackageManager.getPackageInfo(TEST_PACKAGE_NAME, 0)).thenReturn(mPackageInfo); 158 when(mPackageManager.getPackagesHoldingPermissions( 159 new String[] { android.Manifest.permission.MANAGE_USB }, 160 PackageManager.MATCH_SYSTEM_ONLY)) 161 .thenReturn(List.of(mPackageInfo)); 162 163 when(mRestrictUsbOverlayActivitiesProperty.getBoolean()).thenReturn(true); 164 when(mPackageManager.getProperty( 165 eq(PROPERTY_RESTRICT_USB_OVERLAY_ACTIVITIES), eq(TEST_PACKAGE_NAME))) 166 .thenReturn(mRestrictUsbOverlayActivitiesProperty); 167 } 168 169 @After tearDown()170 public void tearDown() throws Exception { 171 mStaticMockSession.finishMocking(); 172 } 173 174 @Test testDeviceAttached_foregroundActivityWithManifestField_resolveActivityNotCalled()175 public void testDeviceAttached_foregroundActivityWithManifestField_resolveActivityNotCalled() { 176 mUsbProfileGroupSettingsManager.deviceAttached(mUsbDevice); 177 178 verify(mUsbUserSettingsManager, times(0)).queryIntentActivities(any(Intent.class)); 179 } 180 181 @Test testDeviceAttached_noForegroundActivity_resolveActivityCalled()182 public void testDeviceAttached_noForegroundActivity_resolveActivityCalled() { 183 when(mActivityManager.getRunningAppProcesses()).thenReturn(new ArrayList<>()); 184 185 mUsbProfileGroupSettingsManager.deviceAttached(mUsbDevice); 186 187 verify(mUsbUserSettingsManager).queryIntentActivities(any(Intent.class)); 188 } 189 190 @Test testDeviceAttached_noForegroundActivityWithUsbPermission_resolveActivityCalled()191 public void testDeviceAttached_noForegroundActivityWithUsbPermission_resolveActivityCalled() { 192 when(mPackageManager.getPackagesHoldingPermissions( 193 new String[] { android.Manifest.permission.MANAGE_USB }, 194 PackageManager.MATCH_SYSTEM_ONLY)) 195 .thenReturn(new ArrayList<>()); 196 197 mUsbProfileGroupSettingsManager.deviceAttached(mUsbDevice); 198 199 verify(mUsbUserSettingsManager).queryIntentActivities(any(Intent.class)); 200 } 201 202 @Test testDeviceAttached_restricUsbOverlayPropertyDisabled_resolveActivityCalled()203 public void testDeviceAttached_restricUsbOverlayPropertyDisabled_resolveActivityCalled() { 204 when(mRestrictUsbOverlayActivitiesProperty.getBoolean()).thenReturn(false); 205 206 mUsbProfileGroupSettingsManager.deviceAttached(mUsbDevice); 207 208 verify(mUsbUserSettingsManager).queryIntentActivities(any(Intent.class)); 209 } 210 211 @Test testDeviceAttached_flagFalse_resolveActivityCalled()212 public void testDeviceAttached_flagFalse_resolveActivityCalled() { 213 when(Flags.allowRestrictionOfOverlayActivities()).thenReturn(false); 214 215 mUsbProfileGroupSettingsManager.deviceAttached(mUsbDevice); 216 217 verify(mUsbUserSettingsManager).queryIntentActivities(any(Intent.class)); 218 } 219 220 @Test 221 public void testDeviceAttached_setupNotCompleteAndNoBlockingActivities_resolveActivityNotCalled()222 testDeviceAttached_setupNotCompleteAndNoBlockingActivities_resolveActivityNotCalled() { 223 when(mRestrictUsbOverlayActivitiesProperty.getBoolean()).thenReturn(false); 224 Settings.Secure.putInt(mContentResolver, USER_SETUP_COMPLETE, 0); 225 226 mUsbProfileGroupSettingsManager.deviceAttached(mUsbDevice); 227 228 verify(mUsbUserSettingsManager, times(0)).queryIntentActivities(any(Intent.class)); 229 } 230 } 231