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 WifiConfigCreator mWifiConfigCreator;
56     protected Instrumentation mInstrumentation;
57     protected UiDevice mDevice;
58     protected boolean mHasSecureLockScreen;
59     protected boolean mHasTelephonyFeature;
60     protected boolean mIsAutomotive;
61     /** User running the test (obtained from {@code mContext}). */
62     protected @UserIdInt int mUserId;
63 
64     @Override
setUp()65     protected void setUp() throws Exception {
66         super.setUp();
67 
68         mInstrumentation = InstrumentationRegistry.getInstrumentation();
69         mDevice = UiDevice.getInstance(mInstrumentation);
70         mDevicePolicyManager = TestAppSystemServiceFactory.getDevicePolicyManager(mContext,
71                 BasicAdminReceiver.class);
72         mWifiManager = TestAppSystemServiceFactory.getWifiManager(mContext,
73                 BasicAdminReceiver.class);
74         WifiManager currentUserWifiManager = mContext.getSystemService(WifiManager.class);
75         mWifiConfigCreator = new WifiConfigCreator(mContext, mWifiManager) {
76             @Override
77             public List<WifiConfiguration> getConfiguredNetworks() {
78                 // Must always use the current user's wifi manager, otherwise it would fail on
79                 // headless system user (as the device owner is not the current user).
80                 return currentUserWifiManager.getConfiguredNetworks();
81             }
82         };
83 
84         mHasSecureLockScreen = mContext.getPackageManager().hasSystemFeature(
85                 PackageManager.FEATURE_SECURE_LOCK_SCREEN);
86         mHasTelephonyFeature = mContext.getPackageManager().hasSystemFeature(
87                 PackageManager.FEATURE_TELEPHONY);
88         mIsAutomotive = mContext.getPackageManager().hasSystemFeature(
89                 PackageManager.FEATURE_AUTOMOTIVE);
90         mUserId = mContext.getUserId();
91 
92         Log.v(TAG, getClass() + ".setUp(): userId=" + mUserId + ", dpm=" + mDevicePolicyManager
93                 + ", wifiManager=" + mWifiManager);
94 
95         assertDeviceOwner();
96     }
97 
assertDeviceOwner()98     private void assertDeviceOwner() {
99         int myUserId = UserHandle.myUserId();
100         assertWithMessage("DPM for user %s", myUserId).that(mDevicePolicyManager).isNotNull();
101 
102         ComponentName admin = getWho();
103         assertWithMessage("Component %s is admin for user %s", admin, myUserId)
104                 .that(mDevicePolicyManager.isAdminActive(admin)).isTrue();
105 
106         String pkgName = mContext.getPackageName();
107         assertWithMessage("Component %s is device owner for user %s", admin, myUserId)
108                 .that(mDevicePolicyManager.isDeviceOwnerApp(pkgName)).isTrue();
109         assertWithMessage("Component %s is profile owner for user %s", admin, myUserId)
110                 .that(mDevicePolicyManager.isManagedProfile(admin)).isFalse();
111     }
112 
getWho()113     protected ComponentName getWho() {
114         return BasicAdminReceiver.getComponentName(mContext);
115     }
116 
executeShellCommand(String... command)117     protected String executeShellCommand(String... command) throws Exception {
118         return mDevice.executeShellCommand(String.join(" ", command));
119     }
120 
isHeadlessSystemUserMode()121     protected static boolean isHeadlessSystemUserMode() {
122         return UserManager.isHeadlessSystemUserMode();
123     }
124 
getCurrentUser()125     protected final UserHandle getCurrentUser() {
126         return UserHandle.of(ActivityManager.getCurrentUser());
127     }
128 }
129