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