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