1 /*
2  * Copyright (C) 2021 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.bedstead.deviceadminapp;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.content.Context;
22 
23 import com.android.bedstead.harrier.DeviceState;
24 import com.android.bedstead.harrier.annotations.EnsureHasNoWorkProfile;
25 import com.android.bedstead.harrier.annotations.RequireRunOnPrimaryUser;
26 import com.android.bedstead.nene.TestApis;
27 import com.android.bedstead.nene.devicepolicy.DeviceOwner;
28 import com.android.bedstead.nene.users.UserReference;
29 import com.android.bedstead.nene.users.UserType;
30 import com.android.eventlib.EventLogs;
31 import com.android.eventlib.events.deviceadminreceivers.DeviceAdminEnabledEvent;
32 
33 import org.junit.Before;
34 import org.junit.ClassRule;
35 import org.junit.Rule;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.junit.runners.JUnit4;
39 
40 @RunWith(JUnit4.class)
41 public class DeviceAdminAppTest {
42 
43     private static final TestApis sTestApis = new TestApis();
44     private static final Context sContext = sTestApis.context().instrumentedContext();
45 
46     @ClassRule @Rule
47     public static final DeviceState sDeviceState = new DeviceState();
48 
49     // This test assumes that DeviceAdminApp is set as a dependency of the test
50 
51     @Before
setUp()52     public void setUp() {
53         EventLogs.resetLogs();
54     }
55 
56     @Test
57     @RequireRunOnPrimaryUser
58     // TODO(scottjonathan): Add annotations to ensure no accounts and no users
setAsDeviceOwner_isEnabled()59     public void setAsDeviceOwner_isEnabled() throws Exception {
60         try (DeviceOwner deviceOwner = sTestApis.devicePolicy().setDeviceOwner(
61                 sDeviceState.primaryUser(), DeviceAdminApp.deviceAdminComponentName(sContext))) {
62 
63             EventLogs<DeviceAdminEnabledEvent> logs =
64                     DeviceAdminEnabledEvent.queryPackage(sContext.getPackageName());
65             assertThat(logs.poll()).isNotNull();
66         }
67     }
68 
69     @Test
70     @RequireRunOnPrimaryUser
71     @EnsureHasNoWorkProfile
setAsProfileOwner_isEnabled()72     public void setAsProfileOwner_isEnabled() {
73         try (UserReference profile = sTestApis.users().createUser()
74                 .parent(sTestApis.users().instrumented())
75                 .type(sTestApis.users().supportedType(UserType.MANAGED_PROFILE_TYPE_NAME))
76                 .createAndStart()) {
77             sTestApis.packages().find(sContext.getPackageName()).install(profile);
78 
79             sTestApis.devicePolicy().setProfileOwner(
80                     profile, DeviceAdminApp.deviceAdminComponentName(sContext));
81 
82             EventLogs<DeviceAdminEnabledEvent> logs =
83                     DeviceAdminEnabledEvent.queryPackage(sContext.getPackageName())
84                     .onUser(profile);
85             assertThat(logs.poll()).isNotNull();
86         }
87     }
88 }
89