Home | History | Annotate | Download | only in helpers
      1 /*
      2  * Copyright (C) 2013 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.uiautomator.common.helpers;
     17 
     18 import com.android.uiautomator.core.UiObject;
     19 import com.android.uiautomator.core.UiObjectNotFoundException;
     20 import com.android.uiautomator.core.UiSelector;
     21 
     22 import java.util.Calendar;
     23 
     24 /**
     25  * Use this helper anywhere there is a date picker to manage. This helper
     26  * will set date specified in a Calendar object.
     27  */
     28 public class DatePickerHelper {
     29 
     30     public static final int MONTH = 0;
     31     public static final int DAY = 1;
     32     public static final int YEAR = 2;
     33 
     34     public static String getCurrentMonth() throws UiObjectNotFoundException {
     35         return getNumberPickerField(MONTH).getText();
     36     }
     37 
     38     public static String getCurrentDay() throws UiObjectNotFoundException {
     39         return getNumberPickerField(DAY).getText();
     40     }
     41 
     42     public static String getCurrentYear() throws UiObjectNotFoundException {
     43         return getNumberPickerField(YEAR).getText();
     44     }
     45 
     46     public static void incrementMonth() throws UiObjectNotFoundException {
     47         incrementMonth(1);
     48     }
     49 
     50     public static void incrementMonth(int count) throws UiObjectNotFoundException {
     51         for (int x = 0; x < count; x++)
     52             getNumberPickerIncrementButton(MONTH).click();
     53     }
     54 
     55     public static void decrementMonth() throws UiObjectNotFoundException {
     56         decrementMonth(1);
     57     }
     58 
     59     public static void decrementMonth(int count) throws UiObjectNotFoundException {
     60         for (int x = 0; x < count; x++)
     61             getNumberPickerDecrementButton(MONTH).click();
     62     }
     63 
     64     public static void incrementDay() throws UiObjectNotFoundException {
     65         incrementDay(1);
     66     }
     67 
     68     public static void incrementDay(int count) throws UiObjectNotFoundException {
     69         for (int x = 0; x < count; x++)
     70             getNumberPickerIncrementButton(DAY).click();
     71     }
     72 
     73     public static void decrementDay() throws UiObjectNotFoundException {
     74         decrementDay(1);
     75     }
     76 
     77     public static void decrementDay(int count) throws UiObjectNotFoundException {
     78         for (int x = 0; x < count; x++)
     79             getNumberPickerDecrementButton(DAY).click();
     80     }
     81 
     82     public static void incrementYear() throws UiObjectNotFoundException {
     83         incrementYear(1);
     84     }
     85 
     86     public static void incrementYear(int count) throws UiObjectNotFoundException {
     87         for (int x = 0; x < count; x++)
     88             getNumberPickerIncrementButton(YEAR).click();
     89     }
     90 
     91     public static void decrementYear() throws UiObjectNotFoundException {
     92         decrementYear(1);
     93     }
     94 
     95     public static void decrementYear(int count) throws UiObjectNotFoundException {
     96         for (int x = 0; x < count; x++)
     97             getNumberPickerDecrementButton(YEAR).click();
     98     }
     99 
    100     public static UiObject getNumberPicker(int instance) {
    101         return new UiObject(new UiSelector().className(
    102                 android.widget.NumberPicker.class.getName()).instance(instance));
    103     }
    104 
    105     public static UiObject getNumberPickerField(int instance)
    106             throws UiObjectNotFoundException {
    107         return getNumberPicker(instance).getChild(
    108                 new UiSelector().className(android.widget.EditText.class.getName()));
    109     }
    110 
    111     public static UiObject getNumberPickerDecrementButton(int instance)
    112             throws UiObjectNotFoundException {
    113         return getNumberPicker(instance).getChild(
    114                 new UiSelector().className(android.widget.Button.class.getName()).instance(0));
    115     }
    116 
    117     public static UiObject getNumberPickerIncrementButton(int instance)
    118             throws UiObjectNotFoundException {
    119         return getNumberPicker(instance).getChild(
    120                 new UiSelector().className(android.widget.Button.class.getName()).instance(1));
    121     }
    122 
    123     public static void clickDone() throws UiObjectNotFoundException {
    124         new UiObject(new UiSelector().text("Done")).click();
    125     }
    126 
    127     public static void setDate(Calendar cal) throws UiObjectNotFoundException {
    128         int calYear = cal.get(Calendar.YEAR);
    129         int calMonth = cal.get(Calendar.MONTH);
    130         int calDay = cal.get(Calendar.DAY_OF_MONTH);
    131 
    132         // Adjust day - increment or decrement using the shortest path
    133         // while accounting for number of days in month and considering
    134         // special case for Feb and leap years.
    135         int dpDay = Integer.parseInt(getCurrentDay());
    136         if (calDay > dpDay) {
    137             if (calDay - dpDay < getDaysInMonth(calYear, calMonth) / 2)
    138                 incrementDay(calDay - dpDay);
    139             else
    140                 decrementDay(dpDay - calDay + getDaysInMonth(calYear, calMonth));
    141         } else if (dpDay > calDay) {
    142             if (dpDay - calDay < getDaysInMonth(calYear, calMonth) / 2)
    143                 decrementDay(dpDay - calDay);
    144             else
    145                 incrementDay(calDay - dpDay + getDaysInMonth(calYear, calMonth));
    146         }
    147 
    148         // Adjust month - increment or decrement using the shortest path
    149         int dpMonth = toMonthNumber(getCurrentMonth());
    150         if (calMonth > dpMonth) {
    151             if (calMonth - dpMonth < 6)
    152                 incrementMonth(calMonth - dpMonth);
    153             else
    154                 decrementMonth(dpMonth - calMonth + 12);
    155         } else if (dpMonth > calMonth) {
    156             if (dpMonth - calMonth < 6)
    157                 decrementMonth(dpMonth - calMonth);
    158             else
    159                 incrementMonth(calMonth - dpMonth + 12);
    160         }
    161 
    162         // Adjust year
    163         int dpYear = Integer.parseInt(getCurrentYear());
    164         if (calYear > dpYear) {
    165             incrementYear(calYear - dpYear);
    166         } else if (dpYear > calYear) {
    167             decrementYear(dpYear - calYear);
    168         }
    169     }
    170 
    171     private static int toMonthNumber(String monthName) {
    172         String months[] = new String[] {"January", "February", "March", "April", "May", "June",
    173                 "July", "August", "September", "October", "November", "December"};
    174 
    175         for (int x = 0; x < months.length; x++) {
    176             if (months[x].contains(monthName))
    177                 return x;
    178         }
    179 
    180         return 0;
    181     }
    182 
    183     /**
    184      * Get the number of days in the month
    185      * @param year
    186      * @param month
    187      * @return
    188      */
    189     private static int getDaysInMonth(int year, int month) {
    190         Calendar cal = Calendar.getInstance();
    191         cal.set(Calendar.YEAR, year);
    192         cal.set(Calendar.MONTH, month);
    193         return cal.getActualMaximum(Calendar.DAY_OF_MONTH);
    194     }
    195 }
    196