1 /* 2 * Copyright (C) 2022 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 android.devicepolicy.cts; 18 19 import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK; 20 import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK; 21 22 import static com.android.queryable.queries.ActivityQuery.activity; 23 24 import static com.google.common.truth.Truth.assertThat; 25 26 import static org.testng.Assert.assertThrows; 27 28 import android.content.Intent; 29 30 import com.android.bedstead.harrier.BedsteadJUnit4; 31 import com.android.bedstead.harrier.DeviceState; 32 import com.android.bedstead.harrier.annotations.NotificationsTest; 33 import com.android.bedstead.harrier.annotations.enterprise.CanSetPolicyTest; 34 import com.android.bedstead.harrier.annotations.enterprise.CannotSetPolicyTest; 35 import com.android.bedstead.harrier.annotations.enterprise.PolicyAppliesTest; 36 import com.android.bedstead.harrier.policies.MaximumTimeOff; 37 import com.android.bedstead.nene.TestApis; 38 import com.android.bedstead.nene.notifications.NotificationListener; 39 import com.android.bedstead.nene.notifications.NotificationListenerQuerySubject; 40 import com.android.bedstead.nene.utils.Poll; 41 import com.android.bedstead.testapp.TestApp; 42 import com.android.bedstead.testapp.TestAppActivityReference; 43 import com.android.bedstead.testapp.TestAppInstance; 44 import com.android.eventlib.EventLogs; 45 46 import com.google.common.truth.Truth; 47 48 import org.junit.ClassRule; 49 import org.junit.Rule; 50 import org.junit.runner.RunWith; 51 52 import java.time.Duration; 53 54 @RunWith(BedsteadJUnit4.class) 55 public final class MaximumTimeOffTest { 56 57 @ClassRule 58 @Rule 59 public static final DeviceState sDeviceState = new DeviceState(); 60 61 private static final TestApp sTestApp = sDeviceState.testApps().query() 62 .whereActivities().contains( 63 activity().where().exported().isTrue() 64 ).get(); 65 66 @PolicyAppliesTest(policy = MaximumTimeOff.class) setManagedProfileMaximumTimeOff_timesOut_personalAppsAreSuspended()67 public void setManagedProfileMaximumTimeOff_timesOut_personalAppsAreSuspended() 68 throws Exception { 69 long originalMaximumTimeOff = 70 sDeviceState.dpc().devicePolicyManager() 71 .getManagedProfileMaximumTimeOff( 72 sDeviceState.dpc().componentName()); 73 try (TestAppInstance personalInstance = sTestApp.install()) { 74 TestAppActivityReference activity = personalInstance.activities().any(); 75 sDeviceState.dpc().devicePolicyManager().setManagedProfileMaximumTimeOff( 76 sDeviceState.dpc().componentName(), /* timeoutMs= */ 1); 77 78 sDeviceState.workProfile().setQuietMode(true); 79 80 // TODO(264248238): We should create a more direct way of figuring out the launch 81 // was refused 82 Poll.forValue("Successfully launched", () -> { 83 // Ensure that we only see events from the current iteration 84 EventLogs.resetLogs(); 85 startActivityWithoutBlocking(activity); 86 87 try { 88 activity.events().activityStarted().waitForEvent(Duration.ofSeconds(2)); 89 return true; 90 } catch (AssertionError e) { 91 // No event 92 return false; 93 } 94 }).toBeEqualTo(false) 95 .errorOnFail() 96 .await(); 97 } finally { 98 sDeviceState.workProfile().setQuietMode(false); 99 sDeviceState.dpc().devicePolicyManager().setManagedProfileMaximumTimeOff( 100 sDeviceState.dpc().componentName(), /* timeoutMs= */ originalMaximumTimeOff); 101 } 102 } 103 startActivityWithoutBlocking(TestAppActivityReference activity)104 private void startActivityWithoutBlocking(TestAppActivityReference activity) { 105 Intent intent = new Intent(); 106 intent.setComponent(activity.component().componentName()); 107 intent.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK); 108 109 TestApis.context().instrumentedContext().startActivity(intent); 110 } 111 112 @PolicyAppliesTest(policy = MaximumTimeOff.class) 113 @NotificationsTest setManagedProfileMaximumTimeOff_timesOut_notificationIsShown()114 public void setManagedProfileMaximumTimeOff_timesOut_notificationIsShown() { 115 long originalMaximumTimeOff = 116 sDeviceState.dpc().devicePolicyManager() 117 .getManagedProfileMaximumTimeOff( 118 sDeviceState.dpc().componentName()); 119 try (NotificationListener notifications = TestApis.notifications().createListener()) { 120 sDeviceState.dpc().devicePolicyManager().setManagedProfileMaximumTimeOff( 121 sDeviceState.dpc().componentName(), /* timeoutMs= */ 1); 122 123 sDeviceState.workProfile().setQuietMode(true); 124 125 NotificationListenerQuerySubject.assertThat( 126 notifications.query() 127 .wherePackageName().isEqualTo("android") 128 .whereNotification().channelId().isEqualTo("DEVICE_ADMIN_ALERTS") 129 ).wasPosted(); 130 } finally { 131 sDeviceState.workProfile().setQuietMode(false); 132 sDeviceState.dpc().devicePolicyManager().setManagedProfileMaximumTimeOff( 133 sDeviceState.dpc().componentName(), /* timeoutMs= */ originalMaximumTimeOff); 134 } 135 } 136 137 @CannotSetPolicyTest(policy = MaximumTimeOff.class, includeNonDeviceAdminStates = false) setManagedProfileMaximumTimeOff_notAllowed_throwsException()138 public void setManagedProfileMaximumTimeOff_notAllowed_throwsException() { 139 assertThrows(SecurityException.class, () -> { 140 sDeviceState.dpc().devicePolicyManager().setManagedProfileMaximumTimeOff( 141 sDeviceState.dpc().componentName(), /* timeoutMs= */ 1); 142 }); 143 } 144 145 @CanSetPolicyTest(policy = MaximumTimeOff.class) getManagedProfileMaximumTimeOff_returnsSetValue()146 public void getManagedProfileMaximumTimeOff_returnsSetValue() { 147 long originalMaximumTimeOff = 148 sDeviceState.dpc().devicePolicyManager() 149 .getManagedProfileMaximumTimeOff( 150 sDeviceState.dpc().componentName()); 151 try { 152 sDeviceState.dpc().devicePolicyManager().setManagedProfileMaximumTimeOff( 153 sDeviceState.dpc().componentName(), /* timeoutMs= */ 12345); 154 155 assertThat(sDeviceState.dpc().devicePolicyManager().getManagedProfileMaximumTimeOff( 156 sDeviceState.dpc().componentName())).isEqualTo(12345); 157 } finally { 158 sDeviceState.dpc().devicePolicyManager().setManagedProfileMaximumTimeOff( 159 sDeviceState.dpc().componentName(), /* timeoutMs= */ originalMaximumTimeOff); 160 } 161 } 162 163 // TODO(264249662): Add missing coverage 164 } 165