1 /*
2  * Copyright (C) 2023 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 
17 package com.android.cts.managedprofile.owner.app;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.app.admin.DeviceAdminReceiver;
22 import android.app.admin.DevicePolicyManager;
23 import android.app.admin.PackagePolicy;
24 import android.content.ComponentName;
25 import android.content.Context;
26 
27 import androidx.test.InstrumentationRegistry;
28 
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.junit.runners.JUnit4;
33 
34 @RunWith(JUnit4.class)
35 public class ManagedProfileOwnerAppTest {
36 
37     private Context mContext;
38     protected DevicePolicyManager mDevicePolicyManager;
39 
40     public static class BasicAdminReceiver extends DeviceAdminReceiver {
41     }
42 
43     public static final ComponentName ADMIN_RECEIVER_COMPONENT = new ComponentName(
44             BasicAdminReceiver.class.getPackage().getName(), BasicAdminReceiver.class.getName());
45 
46 
47     @Before
setUp()48     public void setUp() throws Exception {
49         mContext = InstrumentationRegistry.getContext();
50 
51         mDevicePolicyManager = (DevicePolicyManager)
52                 mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
53 
54         assertThat(mDevicePolicyManager).isNotNull();
55         assertThat(mDevicePolicyManager.isAdminActive(ADMIN_RECEIVER_COMPONENT)).isTrue();
56         assertThat(mDevicePolicyManager.isProfileOwnerApp(
57                 ADMIN_RECEIVER_COMPONENT.getPackageName())).isTrue();
58         assertThat(mDevicePolicyManager.isManagedProfile(ADMIN_RECEIVER_COMPONENT)).isTrue();
59     }
60 
61     @Test
testSetDisallowWorkContactsAccessPolicy()62     public void testSetDisallowWorkContactsAccessPolicy() {
63         PackagePolicy policy = new PackagePolicy(PackagePolicy.PACKAGE_POLICY_ALLOWLIST);
64         mDevicePolicyManager.setManagedProfileContactsAccessPolicy(policy);
65         assertThat(mDevicePolicyManager.getManagedProfileContactsAccessPolicy())
66                 .isEqualTo(policy);
67     }
68 
69     @Test
testEnableWorkContactsAccess()70     public void testEnableWorkContactsAccess() {
71         PackagePolicy policy = new PackagePolicy(PackagePolicy.PACKAGE_POLICY_BLOCKLIST);
72         mDevicePolicyManager.setManagedProfileContactsAccessPolicy(policy);
73         assertThat(mDevicePolicyManager.getManagedProfileContactsAccessPolicy())
74                 .isEqualTo(policy);
75     }
76 }
77