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 Context sContext = TestApis.context().instrumentedContext();
44 
45     @ClassRule @Rule
46     public static final DeviceState sDeviceState = new DeviceState();
47 
48     // This test assumes that DeviceAdminApp is set as a dependency of the test
49 
50     @Before
setUp()51     public void setUp() {
52         EventLogs.resetLogs();
53     }
54 
55     @Test
56     @RequireRunOnPrimaryUser
57     // TODO(scottjonathan): Add annotations to ensure no accounts and no users
setAsDeviceOwner_isEnabled()58     public void setAsDeviceOwner_isEnabled() throws Exception {
59         try (DeviceOwner deviceOwner = TestApis.devicePolicy().setDeviceOwner(
60                 DeviceAdminApp.deviceAdminComponentName(sContext))) {
61 
62             EventLogs<DeviceAdminEnabledEvent> logs =
63                     DeviceAdminEnabledEvent.queryPackage(sContext.getPackageName());
64             assertThat(logs.poll()).isNotNull();
65         }
66     }
67 
68     @Test
69     @RequireRunOnPrimaryUser
70     @EnsureHasNoWorkProfile
setAsProfileOwner_isEnabled()71     public void setAsProfileOwner_isEnabled() {
72         try (UserReference profile = TestApis.users().createUser()
73                 .parent(TestApis.users().instrumented())
74                 .type(TestApis.users().supportedType(UserType.MANAGED_PROFILE_TYPE_NAME))
75                 .createAndStart()) {
76             TestApis.packages().find(sContext.getPackageName()).installExisting(profile);
77 
78             TestApis.devicePolicy().setProfileOwner(
79                     profile, DeviceAdminApp.deviceAdminComponentName(sContext));
80 
81             EventLogs<DeviceAdminEnabledEvent> logs =
82                     DeviceAdminEnabledEvent.queryPackage(sContext.getPackageName())
83                     .onUser(profile);
84             assertThat(logs.poll()).isNotNull();
85         }
86     }
87 }
88