Avoid changing timezone to the same one and look for the right broadcast.

Currently if the timezone is NY, the test fails because it
attempts to set it to NY and since it is the same timezone,
no broadcast is sent.

To avoid getting the wrong broadcast (e.g. originating from previous
test tearDonw), we now check time-zone extra in the receiver.

+ increased broadcast wait timeout.
+ don't change auto_time_zone in the teardown - there is no need,
  just causes more broadcasts.
+ sleep/retry loop for checking timezone change.

Bug: 110036709
Test: atest CtsDevicePolicyManagerTestCases:com.android.cts.devicepolicy.DeviceOwnerTest#testSetTime
Change-Id: I9400875ea26b4a3fffe9b5d2a1fe4eedeb4fe9a1
diff --git a/hostsidetests/devicepolicy/app/DeviceOwner/src/com/android/cts/deviceowner/SetTimeTest.java b/hostsidetests/devicepolicy/app/DeviceOwner/src/com/android/cts/deviceowner/SetTimeTest.java
index ca2769b..acf142f 100644
--- a/hostsidetests/devicepolicy/app/DeviceOwner/src/com/android/cts/deviceowner/SetTimeTest.java
+++ b/hostsidetests/devicepolicy/app/DeviceOwner/src/com/android/cts/deviceowner/SetTimeTest.java
@@ -23,6 +23,7 @@
 import android.provider.Settings;
 
 import java.util.Calendar;
+import java.util.TimeZone;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 
@@ -35,7 +36,7 @@
     private static final long TEST_TIME_2 = 100000000;
     private static final String TEST_TIME_ZONE_1 = "America/New_York";
     private static final String TEST_TIME_ZONE_2 = "America/Los_Angeles";
-    private static final long TIMEOUT_SEC = 10;
+    private static final long TIMEOUT_SEC = 60;
 
     // Real world time to restore after the test.
     private long mStartTimeWallClockMillis;
@@ -65,9 +66,11 @@
         mContext.registerReceiver(receiver, new IntentFilter(Intent.ACTION_TIME_CHANGED));
 
         try {
-            assertTrue(mDevicePolicyManager.setTime(getWho(), testTime));
-            assertTrue(latch.await(TIMEOUT_SEC, TimeUnit.SECONDS));
-            assertTrue(System.currentTimeMillis() <= testTime + (TIMEOUT_SEC + 1) * 1000);
+            assertTrue("failed to set time", mDevicePolicyManager.setTime(getWho(), testTime));
+            assertTrue("timed out waiting for time change broadcast",
+                latch.await(TIMEOUT_SEC, TimeUnit.SECONDS));
+            assertTrue("time is different from what was set",
+                System.currentTimeMillis() <= testTime + (TIMEOUT_SEC + 1) * 1000);
         } finally {
             mContext.unregisterReceiver(receiver);
         }
@@ -86,18 +89,30 @@
 
     private void testSetTimeZoneWithValue(String testTimeZone) throws Exception {
         final CountDownLatch latch = new CountDownLatch(1);
-        BroadcastReceiver receiver = new BroadcastReceiver() {
+        final BroadcastReceiver receiver = new BroadcastReceiver() {
             @Override
             public void onReceive(Context context, Intent intent) {
-                latch.countDown();
+                if (testTimeZone.equals(intent.getStringExtra("time-zone"))) {
+                    latch.countDown();
+                }
             }
         };
         mContext.registerReceiver(receiver, new IntentFilter(Intent.ACTION_TIMEZONE_CHANGED));
 
         try {
-            assertTrue(mDevicePolicyManager.setTimeZone(getWho(), testTimeZone));
-            assertTrue(latch.await(TIMEOUT_SEC, TimeUnit.SECONDS));
-            assertEquals(testTimeZone, Calendar.getInstance().getTimeZone().getID());
+            assertTrue("failed to set timezone",
+                mDevicePolicyManager.setTimeZone(getWho(), testTimeZone));
+            assertTrue("timed out waiting for timezone change broadcast",
+                latch.await(TIMEOUT_SEC, TimeUnit.SECONDS));
+
+            // There might be a delay in timezone setting propagation, so we retry for 5 seconds.
+            int retries = 0;
+            while (!testTimeZone.equals(TimeZone.getDefault().getID())) {
+                if (retries++ > 5) {
+                    fail("timezone wasn't updated");
+                }
+                Thread.sleep(1000);
+            }
         } finally {
             mContext.unregisterReceiver(receiver);
         }
@@ -105,8 +120,19 @@
 
     public void testSetTimeZone() throws Exception {
         mDevicePolicyManager.setGlobalSetting(getWho(), Settings.Global.AUTO_TIME_ZONE, "0");
-        testSetTimeZoneWithValue(TEST_TIME_ZONE_1);
-        testSetTimeZoneWithValue(TEST_TIME_ZONE_2);
+
+        try {
+            // If we are already in test time zone, use another one first.
+            if (TEST_TIME_ZONE_1.equals(TimeZone.getDefault().getID())) {
+                testSetTimeZoneWithValue(TEST_TIME_ZONE_2);
+                testSetTimeZoneWithValue(TEST_TIME_ZONE_1);
+            } else {
+                testSetTimeZoneWithValue(TEST_TIME_ZONE_1);
+                testSetTimeZoneWithValue(TEST_TIME_ZONE_2);
+            }
+        } finally {
+            mDevicePolicyManager.setGlobalSetting(getWho(), Settings.Global.AUTO_TIME_ZONE, "1");
+        }
     }
 
     public void testSetTimeZoneFailWithAutoTimezoneOn() {
@@ -121,13 +147,12 @@
 
     private void restoreTime() {
         mDevicePolicyManager.setGlobalSetting(getWho(), Settings.Global.AUTO_TIME, "0");
-        mDevicePolicyManager.setGlobalSetting(getWho(), Settings.Global.AUTO_TIME_ZONE, "0");
 
         final long estimatedNow = mStartTimeWallClockMillis +
                 TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - mStartTimeElapsedNanos);
         mDevicePolicyManager.setTime(getWho(), estimatedNow);
 
         mDevicePolicyManager.setGlobalSetting(getWho(), Settings.Global.AUTO_TIME, "1");
-        mDevicePolicyManager.setGlobalSetting(getWho(), Settings.Global.AUTO_TIME_ZONE, "1");
+
     }
 }