1 /*
2  * Copyright (C) 2016 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.profileowner;
17 
18 import android.app.admin.DeviceAdminReceiver;
19 import android.app.admin.DevicePolicyManager;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.test.AndroidTestCase;
24 import android.util.Log;
25 
26 import androidx.localbroadcastmanager.content.LocalBroadcastManager;
27 
28 import com.android.cts.devicepolicy.OperationSafetyChangedCallback;
29 import com.android.cts.devicepolicy.OperationSafetyChangedEvent;
30 
31 public abstract class BaseProfileOwnerTest extends AndroidTestCase {
32 
33     public static class BasicAdminReceiver extends DeviceAdminReceiver {
34 
35         @Override
onOperationSafetyStateChanged(Context context, int reason, boolean isSafe)36         public void onOperationSafetyStateChanged(Context context, int reason, boolean isSafe) {
37             OperationSafetyChangedEvent event = new OperationSafetyChangedEvent(reason, isSafe);
38             Log.d(TAG, "onOperationSafetyStateChanged() on user " + context.getUserId() + ": "
39                     + event);
40 
41             Intent intent = OperationSafetyChangedCallback.intentFor(event);
42             LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
43         }
44     }
45 
46     private static final String TAG = BaseProfileOwnerTest.class.getSimpleName();
47 
48     public static final String PACKAGE_NAME = BaseProfileOwnerTest.class.getPackage().getName();
49 
50     protected DevicePolicyManager mDevicePolicyManager;
51 
52     @Override
setUp()53     protected void setUp() throws Exception {
54         super.setUp();
55 
56         mDevicePolicyManager = (DevicePolicyManager)
57                 mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
58         assertProfileOwner(mDevicePolicyManager);
59     }
60 
assertProfileOwner(DevicePolicyManager dpm)61     static void assertProfileOwner(DevicePolicyManager dpm) {
62         assertNotNull(dpm);
63         assertTrue(dpm.isAdminActive(getWho()));
64         assertTrue(dpm.isProfileOwnerApp(PACKAGE_NAME));
65     }
66 
getWho()67     protected static ComponentName getWho() {
68         return new ComponentName(PACKAGE_NAME, BasicAdminReceiver.class.getName());
69     }
70 }
71