Home | History | Annotate | Download | only in deviceowner
      1 /*
      2  * Copyright (C) 2018 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 package com.android.cts.deviceowner;
     17 
     18 import android.app.admin.DevicePolicyManager;
     19 import android.content.BroadcastReceiver;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.content.IntentFilter;
     23 import android.provider.Settings;
     24 
     25 import java.util.Calendar;
     26 import java.util.TimeZone;
     27 import java.util.concurrent.CountDownLatch;
     28 import java.util.concurrent.TimeUnit;
     29 
     30 /**
     31  * Test {@link DevicePolicyManager#setTime} and @link {DevicePolicyManager#setTimeZone}
     32  */
     33 public class SetTimeTest extends BaseDeviceOwnerTest {
     34 
     35     private static final long TEST_TIME_1 = 10000000;
     36     private static final long TEST_TIME_2 = 100000000;
     37     private static final String TEST_TIME_ZONE_1 = "America/New_York";
     38     private static final String TEST_TIME_ZONE_2 = "America/Los_Angeles";
     39     private static final long TIMEOUT_SEC = 60;
     40 
     41     // Real world time to restore after the test.
     42     private long mStartTimeWallClockMillis;
     43     // Elapsed time to measure time taken by the test.
     44     private long mStartTimeElapsedNanos;
     45 
     46     @Override
     47     protected void setUp() throws Exception {
     48         super.setUp();
     49         saveTime();
     50     }
     51 
     52     @Override
     53     protected void tearDown() throws Exception {
     54         restoreTime();
     55         super.tearDown();
     56     }
     57 
     58     private void testSetTimeWithValue(long testTime) throws Exception {
     59         final CountDownLatch latch = new CountDownLatch(1);
     60         BroadcastReceiver receiver = new BroadcastReceiver() {
     61             @Override
     62             public void onReceive(Context context, Intent intent) {
     63                 latch.countDown();
     64             }
     65         };
     66         mContext.registerReceiver(receiver, new IntentFilter(Intent.ACTION_TIME_CHANGED));
     67 
     68         try {
     69             assertTrue("failed to set time", mDevicePolicyManager.setTime(getWho(), testTime));
     70             assertTrue("timed out waiting for time change broadcast",
     71                 latch.await(TIMEOUT_SEC, TimeUnit.SECONDS));
     72             assertTrue("time is different from what was set",
     73                 System.currentTimeMillis() <= testTime + (TIMEOUT_SEC + 1) * 1000);
     74         } finally {
     75             mContext.unregisterReceiver(receiver);
     76         }
     77     }
     78 
     79     public void testSetTime() throws Exception {
     80         mDevicePolicyManager.setGlobalSetting(getWho(), Settings.Global.AUTO_TIME, "0");
     81         testSetTimeWithValue(TEST_TIME_1);
     82         testSetTimeWithValue(TEST_TIME_2);
     83     }
     84 
     85     public void testSetTimeFailWithAutoTimeOn() {
     86         mDevicePolicyManager.setGlobalSetting(getWho(), Settings.Global.AUTO_TIME, "1");
     87         assertFalse(mDevicePolicyManager.setTime(getWho(), TEST_TIME_1));
     88     }
     89 
     90     private void testSetTimeZoneWithValue(String testTimeZone) throws Exception {
     91         final CountDownLatch latch = new CountDownLatch(1);
     92         final BroadcastReceiver receiver = new BroadcastReceiver() {
     93             @Override
     94             public void onReceive(Context context, Intent intent) {
     95                 if (testTimeZone.equals(intent.getStringExtra("time-zone"))) {
     96                     latch.countDown();
     97                 }
     98             }
     99         };
    100         mContext.registerReceiver(receiver, new IntentFilter(Intent.ACTION_TIMEZONE_CHANGED));
    101 
    102         try {
    103             assertTrue("failed to set timezone",
    104                 mDevicePolicyManager.setTimeZone(getWho(), testTimeZone));
    105             assertTrue("timed out waiting for timezone change broadcast",
    106                 latch.await(TIMEOUT_SEC, TimeUnit.SECONDS));
    107 
    108             // There might be a delay in timezone setting propagation, so we retry for 5 seconds.
    109             int retries = 0;
    110             while (!testTimeZone.equals(TimeZone.getDefault().getID())) {
    111                 if (retries++ > 5) {
    112                     fail("timezone wasn't updated");
    113                 }
    114                 Thread.sleep(1000);
    115             }
    116         } finally {
    117             mContext.unregisterReceiver(receiver);
    118         }
    119     }
    120 
    121     public void testSetTimeZone() throws Exception {
    122         mDevicePolicyManager.setGlobalSetting(getWho(), Settings.Global.AUTO_TIME_ZONE, "0");
    123 
    124         try {
    125             // If we are already in test time zone, use another one first.
    126             if (TEST_TIME_ZONE_1.equals(TimeZone.getDefault().getID())) {
    127                 testSetTimeZoneWithValue(TEST_TIME_ZONE_2);
    128                 testSetTimeZoneWithValue(TEST_TIME_ZONE_1);
    129             } else {
    130                 testSetTimeZoneWithValue(TEST_TIME_ZONE_1);
    131                 testSetTimeZoneWithValue(TEST_TIME_ZONE_2);
    132             }
    133         } finally {
    134             mDevicePolicyManager.setGlobalSetting(getWho(), Settings.Global.AUTO_TIME_ZONE, "1");
    135         }
    136     }
    137 
    138     public void testSetTimeZoneFailWithAutoTimezoneOn() {
    139         mDevicePolicyManager.setGlobalSetting(getWho(), Settings.Global.AUTO_TIME_ZONE, "1");
    140         assertFalse(mDevicePolicyManager.setTimeZone(getWho(), TEST_TIME_ZONE_1));
    141     }
    142 
    143     private void saveTime() {
    144         mStartTimeWallClockMillis = System.currentTimeMillis();
    145         mStartTimeElapsedNanos = System.nanoTime();
    146     }
    147 
    148     private void restoreTime() {
    149         mDevicePolicyManager.setGlobalSetting(getWho(), Settings.Global.AUTO_TIME, "0");
    150 
    151         final long estimatedNow = mStartTimeWallClockMillis +
    152                 TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - mStartTimeElapsedNanos);
    153         mDevicePolicyManager.setTime(getWho(), estimatedNow);
    154 
    155         mDevicePolicyManager.setGlobalSetting(getWho(), Settings.Global.AUTO_TIME, "1");
    156 
    157     }
    158 }
    159