Adding UiModeManager Custom Tests

Added tests for setting custom time

Fixes: 152548753
Fixes: 152549245
Fixes: 152549114
Fixes: 152549934

Test: atest UiModeManagerTest
Change-Id: Id25e57d752779f3ab6a9d76fb4a8aa425967c316
diff --git a/tests/app/src/android/app/cts/UiModeManagerTest.java b/tests/app/src/android/app/cts/UiModeManagerTest.java
index 25ec9a3..1d443f6 100644
--- a/tests/app/src/android/app/cts/UiModeManagerTest.java
+++ b/tests/app/src/android/app/cts/UiModeManagerTest.java
@@ -34,6 +34,7 @@
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.time.LocalTime;
 
 public class UiModeManagerTest extends AndroidTestCase {
     private static final String TAG = "UiModeManagerTest";
@@ -84,6 +85,28 @@
         }
     }
 
+    public void testSetAndGetCustomTimeStart() {
+        LocalTime time = mUiModeManager.getCustomNightModeStart();
+        // decrease time
+        LocalTime timeNew = LocalTime.of(
+                (time.getHour() + 1) % 12,
+                (time.getMinute() + 30) % 60);
+        setStartTime(timeNew);
+        assertNotSame(time, timeNew);
+        assertEquals(timeNew, mUiModeManager.getCustomNightModeStart());
+    }
+
+    public void testSetAndGetCustomTimeEnd() {
+        LocalTime time = mUiModeManager.getCustomNightModeEnd();
+        // decrease time
+        LocalTime timeNew = LocalTime.of(
+                (time.getHour() + 1) % 12,
+                (time.getMinute() + 30) % 60);
+        setEndTime(timeNew);
+        assertNotSame(time, timeNew);
+        assertEquals(timeNew, mUiModeManager.getCustomNightModeEnd());
+    }
+
     public void testNightModeYesPersisted() throws InterruptedException {
         // Reset the mode to no if it is set to another value
         setNightMode(UiModeManager.MODE_NIGHT_NO);
@@ -327,7 +350,6 @@
     }
 
     private void setNightMode(int mode) {
-        final UiAutomation uiAutomation = getInstrumentation().getUiAutomation();
         String modeString = "unknown";
         switch (mode) {
             case UiModeManager.MODE_NIGHT_AUTO:
@@ -341,6 +363,21 @@
                 break;
         }
         final String command = " cmd uimode night " + modeString;
+        applyCommand(command);
+    }
+
+    private void setStartTime(LocalTime t) {
+        final String command = " cmd uimode time start " + t.toString();
+        applyCommand(command);
+    }
+
+    private void setEndTime(LocalTime t) {
+        final String command = " cmd uimode time end " + t.toString();
+        applyCommand(command);
+    }
+
+    private void applyCommand(String command) {
+        final UiAutomation uiAutomation = getInstrumentation().getUiAutomation();
         try (ParcelFileDescriptor fd = uiAutomation.executeShellCommand(command)) {
             Assert.assertNotNull("Failed to execute shell command: " + command, fd);
             // Wait for the command to finish by reading until EOF
@@ -356,4 +393,5 @@
             uiAutomation.destroy();
         }
     }
+
 }