1 /* 2 * Copyright (C) 2014 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 package com.android.cts.deviceowner; 17 18 import static android.Manifest.permission.INTERACT_ACROSS_USERS; 19 20 import static com.google.common.truth.Truth.assertWithMessage; 21 22 import android.annotation.Nullable; 23 import android.annotation.UserIdInt; 24 import android.app.ActivityManager; 25 import android.app.Instrumentation; 26 import android.app.UiAutomation; 27 import android.app.admin.DevicePolicyManager; 28 import android.content.ComponentName; 29 import android.content.pm.PackageManager; 30 import android.net.TetheringManager; 31 import android.net.wifi.WifiConfiguration; 32 import android.net.wifi.WifiManager; 33 import android.os.UserHandle; 34 import android.os.UserManager; 35 import android.test.AndroidTestCase; 36 import android.util.Log; 37 38 import androidx.test.InstrumentationRegistry; 39 import androidx.test.uiautomator.UiDevice; 40 41 import com.android.bedstead.dpmwrapper.TestAppSystemServiceFactory; 42 import com.android.compatibility.common.util.WifiConfigCreator; 43 44 import java.util.List; 45 46 /** 47 * Base class for device-owner based tests. 48 * 49 * This class handles making sure that the test is the device owner 50 * and that it has an active admin registered, so that all tests may 51 * assume these are done. The admin component can be accessed through 52 * {@link #getWho()}. 53 */ 54 public abstract class BaseDeviceOwnerTest extends AndroidTestCase { 55 56 private static final String TAG = BaseDeviceOwnerTest.class.getSimpleName(); 57 58 protected DevicePolicyManager mDevicePolicyManager; 59 @Nullable 60 protected WifiManager mWifiManager; 61 protected WifiManager mCurrentUserWifiManager; 62 @Nullable 63 protected WifiConfigCreator mWifiConfigCreator; 64 @Nullable protected TetheringManager mTetheringManager; 65 protected Instrumentation mInstrumentation; 66 protected UiDevice mDevice; 67 protected boolean mHasSecureLockScreen; 68 protected boolean mHasTelephonyFeature; 69 protected boolean mIsAutomotive; 70 /** User running the test (obtained from {@code mContext}). */ 71 protected @UserIdInt int mUserId; 72 private UiAutomation mUiAutomation; 73 74 @Override setUp()75 protected void setUp() throws Exception { 76 super.setUp(); 77 78 mUserId = mContext.getUserId(); 79 Log.v(TAG, getClass().getSimpleName() + ".setUp(): test=" + getClass() + ", userId=" 80 + mUserId); 81 82 mInstrumentation = InstrumentationRegistry.getInstrumentation(); 83 mDevice = UiDevice.getInstance(mInstrumentation); 84 mDevicePolicyManager = TestAppSystemServiceFactory.getDevicePolicyManager(mContext, 85 BasicAdminReceiver.class, /* forDeviceOwner= */ true); 86 mWifiManager = TestAppSystemServiceFactory.getWifiManager(mContext, 87 BasicAdminReceiver.class); 88 mTetheringManager = 89 TestAppSystemServiceFactory.getTetheringManager(mContext, BasicAdminReceiver.class); 90 mCurrentUserWifiManager = mContext.getSystemService(WifiManager.class); 91 mWifiConfigCreator = mWifiManager == null ? null : new WifiConfigCreator(mContext, 92 mWifiManager); 93 mHasSecureLockScreen = mContext.getPackageManager().hasSystemFeature( 94 PackageManager.FEATURE_SECURE_LOCK_SCREEN); 95 mHasTelephonyFeature = mContext.getPackageManager().hasSystemFeature( 96 PackageManager.FEATURE_TELEPHONY); 97 mIsAutomotive = mContext.getPackageManager().hasSystemFeature( 98 PackageManager.FEATURE_AUTOMOTIVE); 99 mUiAutomation = InstrumentationRegistry.getInstrumentation().getUiAutomation(); 100 101 Log.v(TAG, "dpm=" + mDevicePolicyManager + ", wifiManager=" + mWifiManager); 102 103 assertDeviceOwner(); 104 } 105 assertDeviceOwner()106 private void assertDeviceOwner() { 107 int myUserId = UserHandle.myUserId(); 108 assertWithMessage("DPM for user %s", myUserId).that(mDevicePolicyManager).isNotNull(); 109 110 ComponentName admin = getWho(); 111 assertWithMessage("Component %s is admin for user %s", admin, myUserId) 112 .that(mDevicePolicyManager.isAdminActive(admin)).isTrue(); 113 114 String pkgName = mContext.getPackageName(); 115 assertWithMessage("Component %s is device owner for user %s", admin, myUserId) 116 .that(mDevicePolicyManager.isDeviceOwnerApp(pkgName)).isTrue(); 117 assertWithMessage("Component %s is profile owner for user %s", admin, myUserId) 118 .that(mDevicePolicyManager.isManagedProfile(admin)).isFalse(); 119 } 120 getWho()121 protected ComponentName getWho() { 122 return BasicAdminReceiver.getComponentName(mContext); 123 } 124 executeShellCommand(String... command)125 protected String executeShellCommand(String... command) throws Exception { 126 return mDevice.executeShellCommand(String.join(" ", command)); 127 } 128 isHeadlessSystemUserMode()129 protected static boolean isHeadlessSystemUserMode() { 130 return UserManager.isHeadlessSystemUserMode(); 131 } 132 getCurrentUser()133 protected final UserHandle getCurrentUser() { 134 try { 135 mUiAutomation.adoptShellPermissionIdentity(INTERACT_ACROSS_USERS); 136 UserHandle currentUser = UserHandle.of(ActivityManager.getCurrentUser()); 137 Log.v(TAG, "getCurrentUser(): returning " + currentUser); 138 return currentUser; 139 } finally { 140 mUiAutomation.dropShellPermissionIdentity(); 141 } 142 } 143 getConfiguredNetworks()144 protected final List<WifiConfiguration> getConfiguredNetworks() { 145 // Must use a the WifiManager of the current user to list networks, as 146 // getConfiguredNetworks() would return empty on systems using headless system 147 // mode as that method "Return a list of all the networks configured for the current 148 // foreground user", and the system user is running in the background in this case. 149 return mCurrentUserWifiManager.getConfiguredNetworks(); 150 } 151 } 152