Add two helper functions to the uiautomator test library

Change-Id: Ic7da13ee0d46061eedc31cc2f9751380890a0b6d
diff --git a/uiautomator_test_libraries/src/com/android/uiautomator/common/helpers/DatePickerHelper.java b/uiautomator_test_libraries/src/com/android/uiautomator/common/helpers/DatePickerHelper.java
new file mode 100644
index 0000000..6c7db3a
--- /dev/null
+++ b/uiautomator_test_libraries/src/com/android/uiautomator/common/helpers/DatePickerHelper.java
@@ -0,0 +1,195 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.uiautomator.common.helpers;
+
+import com.android.uiautomator.core.UiObject;
+import com.android.uiautomator.core.UiObjectNotFoundException;
+import com.android.uiautomator.core.UiSelector;
+
+import java.util.Calendar;
+
+/**
+ * Use this helper anywhere there is a date picker to manage. This helper
+ * will set date specified in a Calendar object.
+ */
+public class DatePickerHelper {
+
+    public static final int MONTH = 0;
+    public static final int DAY = 1;
+    public static final int YEAR = 2;
+
+    public static String getCurrentMonth() throws UiObjectNotFoundException {
+        return getNumberPickerField(MONTH).getText();
+    }
+
+    public static String getCurrentDay() throws UiObjectNotFoundException {
+        return getNumberPickerField(DAY).getText();
+    }
+
+    public static String getCurrentYear() throws UiObjectNotFoundException {
+        return getNumberPickerField(YEAR).getText();
+    }
+
+    public static void incrementMonth() throws UiObjectNotFoundException {
+        incrementMonth(1);
+    }
+
+    public static void incrementMonth(int count) throws UiObjectNotFoundException {
+        for (int x = 0; x < count; x++)
+            getNumberPickerIncrementButton(MONTH).click();
+    }
+
+    public static void decrementMonth() throws UiObjectNotFoundException {
+        decrementMonth(1);
+    }
+
+    public static void decrementMonth(int count) throws UiObjectNotFoundException {
+        for (int x = 0; x < count; x++)
+            getNumberPickerDecrementButton(MONTH).click();
+    }
+
+    public static void incrementDay() throws UiObjectNotFoundException {
+        incrementDay(1);
+    }
+
+    public static void incrementDay(int count) throws UiObjectNotFoundException {
+        for (int x = 0; x < count; x++)
+            getNumberPickerIncrementButton(DAY).click();
+    }
+
+    public static void decrementDay() throws UiObjectNotFoundException {
+        decrementDay(1);
+    }
+
+    public static void decrementDay(int count) throws UiObjectNotFoundException {
+        for (int x = 0; x < count; x++)
+            getNumberPickerDecrementButton(DAY).click();
+    }
+
+    public static void incrementYear() throws UiObjectNotFoundException {
+        incrementYear(1);
+    }
+
+    public static void incrementYear(int count) throws UiObjectNotFoundException {
+        for (int x = 0; x < count; x++)
+            getNumberPickerIncrementButton(YEAR).click();
+    }
+
+    public static void decrementYear() throws UiObjectNotFoundException {
+        decrementYear(1);
+    }
+
+    public static void decrementYear(int count) throws UiObjectNotFoundException {
+        for (int x = 0; x < count; x++)
+            getNumberPickerDecrementButton(YEAR).click();
+    }
+
+    public static UiObject getNumberPicker(int instance) {
+        return new UiObject(new UiSelector().className(
+                android.widget.NumberPicker.class.getName()).instance(instance));
+    }
+
+    public static UiObject getNumberPickerField(int instance)
+            throws UiObjectNotFoundException {
+        return getNumberPicker(instance).getChild(
+                new UiSelector().className(android.widget.EditText.class.getName()));
+    }
+
+    public static UiObject getNumberPickerDecrementButton(int instance)
+            throws UiObjectNotFoundException {
+        return getNumberPicker(instance).getChild(
+                new UiSelector().className(android.widget.Button.class.getName()).instance(0));
+    }
+
+    public static UiObject getNumberPickerIncrementButton(int instance)
+            throws UiObjectNotFoundException {
+        return getNumberPicker(instance).getChild(
+                new UiSelector().className(android.widget.Button.class.getName()).instance(1));
+    }
+
+    public static void clickDone() throws UiObjectNotFoundException {
+        new UiObject(new UiSelector().text("Done")).click();
+    }
+
+    public static void setDate(Calendar cal) throws UiObjectNotFoundException {
+        int calYear = cal.get(Calendar.YEAR);
+        int calMonth = cal.get(Calendar.MONTH);
+        int calDay = cal.get(Calendar.DAY_OF_MONTH);
+
+        // Adjust day - increment or decrement using the shortest path
+        // while accounting for number of days in month and considering
+        // special case for Feb and leap years.
+        int dpDay = Integer.parseInt(getCurrentDay());
+        if (calDay > dpDay) {
+            if (calDay - dpDay < getDaysInMonth(calYear, calMonth) / 2)
+                incrementDay(calDay - dpDay);
+            else
+                decrementDay(dpDay - calDay + getDaysInMonth(calYear, calMonth));
+        } else if (dpDay > calDay) {
+            if (dpDay - calDay < getDaysInMonth(calYear, calMonth) / 2)
+                decrementDay(dpDay - calDay);
+            else
+                incrementDay(calDay - dpDay + getDaysInMonth(calYear, calMonth));
+        }
+
+        // Adjust month - increment or decrement using the shortest path
+        int dpMonth = toMonthNumber(getCurrentMonth());
+        if (calMonth > dpMonth) {
+            if (calMonth - dpMonth < 6)
+                incrementMonth(calMonth - dpMonth);
+            else
+                decrementMonth(dpMonth - calMonth + 12);
+        } else if (dpMonth > calMonth) {
+            if (dpMonth - calMonth < 6)
+                decrementMonth(dpMonth - calMonth);
+            else
+                incrementMonth(calMonth - dpMonth + 12);
+        }
+
+        // Adjust year
+        int dpYear = Integer.parseInt(getCurrentYear());
+        if (calYear > dpYear) {
+            incrementYear(calYear - dpYear);
+        } else if (dpYear > calYear) {
+            decrementYear(dpYear - calYear);
+        }
+    }
+
+    private static int toMonthNumber(String monthName) {
+        String months[] = new String[] {"January", "February", "March", "April", "May", "June",
+                "July", "August", "September", "October", "November", "December"};
+
+        for (int x = 0; x < months.length; x++) {
+            if (months[x].contains(monthName))
+                return x;
+        }
+
+        return 0;
+    }
+
+    /**
+     * Get the number of days in the month
+     * @param year
+     * @param month
+     * @return
+     */
+    private static int getDaysInMonth(int year, int month) {
+        Calendar cal = Calendar.getInstance();
+        cal.set(Calendar.YEAR, year);
+        cal.set(Calendar.MONTH, month);
+        return cal.getActualMaximum(Calendar.DAY_OF_MONTH);
+    }
+}
diff --git a/uiautomator_test_libraries/src/com/android/uiautomator/common/helpers/TimePickerHelper.java b/uiautomator_test_libraries/src/com/android/uiautomator/common/helpers/TimePickerHelper.java
new file mode 100644
index 0000000..c5e3add
--- /dev/null
+++ b/uiautomator_test_libraries/src/com/android/uiautomator/common/helpers/TimePickerHelper.java
@@ -0,0 +1,162 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.uiautomator.common.helpers;
+
+import com.android.uiautomator.core.UiObject;
+import com.android.uiautomator.core.UiObjectNotFoundException;
+import com.android.uiautomator.core.UiSelector;
+
+import java.util.Calendar;
+import java.util.Locale;
+
+/**
+ * Use this helper anywhere there is a time picker to manage. This helper
+ * will set time specified in a Calendar object.
+ */
+public class TimePickerHelper {
+
+    public static final int HOUR = 0;
+    public static final int MINUTE = 1;
+    public static final int MERIDIEM = 2;
+
+    public static String getCurrentHour() throws UiObjectNotFoundException {
+        return getNumberPickerField(HOUR).getText();
+    }
+
+    public static String getCurrentMinute() throws UiObjectNotFoundException {
+        return getNumberPickerField(MINUTE).getText();
+    }
+
+    public static String getCurrentMeridiem() throws UiObjectNotFoundException {
+        return getNumberPickerField(MERIDIEM).getText();
+    }
+
+
+    public static void incrementHour() throws UiObjectNotFoundException {
+        incrementHour(1);
+    }
+
+    public static void incrementHour(int count) throws UiObjectNotFoundException {
+        for (int x = 0; x < count; x++)
+            getNumberPickerIncrementButton(HOUR).click();
+    }
+
+    public static void decrementHour() throws UiObjectNotFoundException {
+        decrementHour(1);
+    }
+
+    public static void decrementHour(int count) throws UiObjectNotFoundException {
+        for (int x = 0; x < count; x++)
+            getNumberPickerDecrementButton(HOUR).click();
+    }
+
+    public static void incrementMinute() throws UiObjectNotFoundException {
+        incrementMinute(1);
+    }
+
+    public static void incrementMinute(int count) throws UiObjectNotFoundException {
+        for (int x = 0; x < count; x++)
+            getNumberPickerIncrementButton(MINUTE).click();
+    }
+
+    public static void decrementMinute() throws UiObjectNotFoundException {
+        decrementMinute(1);
+    }
+
+    public static void decrementMinute(int count) throws UiObjectNotFoundException {
+        for (int x = 0; x < count; x++)
+            getNumberPickerDecrementButton(MINUTE).click();
+    }
+
+    public static void selectPM() throws UiObjectNotFoundException {
+        getNumberPicker(MERIDIEM).getChild(new UiSelector().text("PM")).click();
+    }
+
+    public static void selectAM() throws UiObjectNotFoundException {
+        getNumberPicker(MERIDIEM).getChild(new UiSelector().text("AM")).click();
+    }
+
+    public static UiObject getNumberPicker(int instance) {
+        return new UiObject(new UiSelector().className(
+                android.widget.NumberPicker.class.getName()).instance(instance));
+    }
+
+    public static UiObject getNumberPickerField(int instance)
+            throws UiObjectNotFoundException {
+        return getNumberPicker(instance).getChild(
+                new UiSelector().className(android.widget.EditText.class.getName()));
+    }
+
+    public static UiObject getNumberPickerDecrementButton(int instance)
+            throws UiObjectNotFoundException {
+        return getNumberPicker(instance).getChild(
+                new UiSelector().className(android.widget.Button.class.getName()).instance(0));
+    }
+
+    public static UiObject getNumberPickerIncrementButton(int instance)
+            throws UiObjectNotFoundException {
+        return getNumberPicker(instance).getChild(
+                new UiSelector().className(android.widget.Button.class.getName()).instance(1));
+    }
+
+    public static void clickDone() throws UiObjectNotFoundException {
+        new UiObject(new UiSelector().text("Done")).click();
+    }
+
+    public static void setTime(Calendar cal) throws UiObjectNotFoundException {
+        // Adjust minutes - increment or decrement using the shortest path
+        int tpMinute = Integer.parseInt(getCurrentMinute());
+        int calMinute = cal.get(Calendar.MINUTE);
+        if (calMinute > tpMinute) {
+            if (calMinute - tpMinute < 30)
+                incrementMinute(calMinute - tpMinute);
+            else
+                decrementMinute(tpMinute - calMinute + 60);
+        } else if (tpMinute > calMinute) {
+            if (tpMinute - calMinute < 30)
+                decrementMinute(tpMinute - calMinute);
+            else
+                incrementMinute(calMinute - tpMinute + 60);
+        }
+
+        // Adjust hour - increment or decrement using the shortest path
+        int tpHour = Integer.parseInt(getCurrentHour());
+        int calHour = cal.get(Calendar.HOUR);
+        if (calHour > tpHour) {
+            if (calHour - tpHour < 6)
+                incrementHour(calHour - tpHour);
+            else
+                decrementHour(tpHour - calHour + 12);
+        } else if (tpHour > calHour) {
+            if (tpHour - calHour < 6)
+                decrementHour(tpHour - calHour);
+            else
+                incrementHour(calHour - tpHour + 12);
+        }
+
+        // Adjust meridiem
+        String calMer = cal.getDisplayName(Calendar.AM_PM, Calendar.SHORT, Locale.US);
+        String tpMer = getCurrentMeridiem();
+        if (tpMer.equalsIgnoreCase(calMer))
+            return;
+
+        if (!calMer.equalsIgnoreCase("AM")) {
+            selectPM();
+        } else {
+            selectAM();
+        }
+    }
+}
diff --git a/uiautomator_test_libraries/src/com/android/uiautomator/platform/JankTestBase.java b/uiautomator_test_libraries/src/com/android/uiautomator/platform/JankTestBase.java
index 42e4db1..9013ce6 100644
--- a/uiautomator_test_libraries/src/com/android/uiautomator/platform/JankTestBase.java
+++ b/uiautomator_test_libraries/src/com/android/uiautomator/platform/JankTestBase.java
@@ -44,7 +44,7 @@
     protected String mTestCaseName;
     protected int mSuccessTestRuns = 0;
 
-    private final static String TAG = "BasePerformance";
+    private final static String TAG = JankTestBase.class.getSimpleName();
     // holds all params for the derived tests
     private final static String PROPERTY_FILE_NAME = "UiJankinessTests.conf";
     private final static String PARAM_CONFIG = "conf";