1 /*
2  * Copyright (C) 2019 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.deviceandprofileowner;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import java.util.concurrent.TimeUnit;
22 
23 public class TimeManagementTest extends BaseDeviceAdminTest {
24 
25     // Real world time to restore after the test.
26     private long mStartTimeWallClockMillis;
27     // Elapsed time to measure time taken by the test.
28     private long mStartTimeElapsedNanos;
29 
30     @Override
setUp()31     protected void setUp() throws Exception {
32         super.setUp();
33         saveTime();
34     }
35 
36     @Override
tearDown()37     protected void tearDown() throws Exception {
38         restoreTime();
39         super.tearDown();
40     }
41 
testSetAutoTimeZoneEnabled()42     public void testSetAutoTimeZoneEnabled() {
43         mDevicePolicyManager.setAutoTimeZoneEnabled(ADMIN_RECEIVER_COMPONENT, true);
44 
45         assertThat(mDevicePolicyManager.getAutoTimeZoneEnabled(ADMIN_RECEIVER_COMPONENT)).isTrue();
46 
47         mDevicePolicyManager.setAutoTimeZoneEnabled(ADMIN_RECEIVER_COMPONENT, false);
48 
49         assertThat(mDevicePolicyManager.getAutoTimeZoneEnabled(ADMIN_RECEIVER_COMPONENT)).isFalse();
50     }
51 
testSetTime()52     public void testSetTime() {
53         mDevicePolicyManager.setAutoTimeEnabled(ADMIN_RECEIVER_COMPONENT, false);
54 
55         final long estimatedNow = mStartTimeWallClockMillis +
56                 TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - mStartTimeElapsedNanos);
57         assertThat(mDevicePolicyManager.setTime(ADMIN_RECEIVER_COMPONENT,
58                 estimatedNow - TimeUnit.HOURS.toMillis(1))).isTrue();
59     }
60 
testSetTime_failWhenAutoTimeEnabled()61     public void testSetTime_failWhenAutoTimeEnabled() {
62         mDevicePolicyManager.setAutoTimeEnabled(ADMIN_RECEIVER_COMPONENT, true);
63 
64         assertThat(mDevicePolicyManager.setTime(ADMIN_RECEIVER_COMPONENT, 0)).isFalse();
65     }
66 
testSetTimeZone()67     public void testSetTimeZone() {
68         mDevicePolicyManager.setAutoTimeZoneEnabled(ADMIN_RECEIVER_COMPONENT, false);
69 
70         assertThat(
71                 mDevicePolicyManager.setTimeZone(ADMIN_RECEIVER_COMPONENT, "Singapore")).isTrue();
72     }
73 
testSetTimeZone_failIfAutoTimeZoneEnabled()74     public void testSetTimeZone_failIfAutoTimeZoneEnabled() {
75         mDevicePolicyManager.setAutoTimeZoneEnabled(ADMIN_RECEIVER_COMPONENT, true);
76 
77         assertThat(
78                 mDevicePolicyManager.setTimeZone(ADMIN_RECEIVER_COMPONENT, "Singapore")).isFalse();
79     }
80 
saveTime()81     private void saveTime() {
82         mStartTimeWallClockMillis = System.currentTimeMillis();
83         mStartTimeElapsedNanos = System.nanoTime();
84     }
85 
restoreTime()86     private void restoreTime() {
87         mDevicePolicyManager.setAutoTimeEnabled(ADMIN_RECEIVER_COMPONENT, false);
88 
89         final long estimatedNow = mStartTimeWallClockMillis +
90                 TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - mStartTimeElapsedNanos);
91         mDevicePolicyManager.setTime(ADMIN_RECEIVER_COMPONENT, estimatedNow);
92 
93         mDevicePolicyManager.setAutoTimeEnabled(ADMIN_RECEIVER_COMPONENT, true);
94     }
95 }
96