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