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