Home | History | Annotate | Download | only in demos
      1 /*
      2  * Copyright (C) 2012 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.test.uiautomator.demos;
     17 
     18 import android.widget.TextView;
     19 
     20 import com.android.uiautomator.core.UiObject;
     21 import com.android.uiautomator.core.UiObjectNotFoundException;
     22 import com.android.uiautomator.core.UiScrollable;
     23 import com.android.uiautomator.core.UiSelector;
     24 import com.android.uiautomator.testrunner.UiAutomatorTestCase;
     25 
     26 /**
     27  * Test demonstrates using the UiAutomator APIs to set an alarm to
     28  * go off in 2 minutes
     29  */
     30 public class SetTwoMinuteAlarm extends UiAutomatorTestCase {
     31 
     32     /**
     33      * For the purpose of this demo, we're declaring the Launcher signatures here.
     34      * It may be more appropriate to declare signatures and methods related
     35      * to Launcher in their own reusable Launcher helper file.
     36      */
     37     public static class LauncherHelper {
     38         public static final UiSelector ALL_APPS_BUTTON = new UiSelector().description("Apps");
     39         public static final UiSelector LAUNCHER_CONTAINER = new UiSelector().scrollable(true);
     40         public static final UiSelector LAUNCHER_ITEM =
     41                 new UiSelector().className(android.widget.TextView.class.getName());
     42     }
     43 
     44     /**
     45      * Set an alarm 2 minutes from now and verify it goes off. Also check the notification
     46      * shades for an alarm. (Crude test but it demos the use of Android resources)
     47      * @throws UiObjectNotFoundException
     48      */
     49     public void testDemo() throws UiObjectNotFoundException {
     50         // The following code is documented in the LaunchSettings demo. For detailed description
     51         // of how this code works, look at the demo LaunchSettings
     52 
     53         // Good to start from here
     54         getUiDevice().pressHome();
     55 
     56         // open the All Apps view
     57         UiObject allAppsButton = new UiObject(LauncherHelper.ALL_APPS_BUTTON);
     58         allAppsButton.click();
     59 
     60         // clicking the APPS tab
     61         UiSelector appsTabSelector =
     62                 new UiSelector().className(android.widget.TabWidget.class.getName())
     63                     .childSelector(new UiSelector().text("Apps"));
     64         UiObject appsTab = new UiObject(appsTabSelector);
     65         appsTab.click();
     66 
     67         // Clicking the Settings
     68         UiScrollable allAppsScreen = new UiScrollable(LauncherHelper.LAUNCHER_CONTAINER);
     69         allAppsScreen.setAsHorizontalList();
     70         UiObject clockApp =
     71                 allAppsScreen.getChildByText(LauncherHelper.LAUNCHER_ITEM, "Clock");
     72         clockApp.click();
     73 
     74         // Set an alarm to go off in about 2 minutes
     75         setAlarm(2);
     76 
     77         // wait for the alarm alert dialog
     78         UiObject alarmAlert =
     79                 new UiObject(new UiSelector().packageName("com.google.android.deskclock")
     80                         .className(TextView.class.getName()).text("Alarm"));
     81 
     82         assertTrue("Timeout while waiting for alarm to go off",
     83                 alarmAlert.waitForExists(2 * 60 * 1000));
     84 
     85         clickByText("Dismiss");
     86     }
     87 
     88     /**
     89      * Helper function to set an alarm
     90      * @param minutesFromNow
     91      * @throws UiObjectNotFoundException
     92      */
     93     private void setAlarm(int minutesFromNow) throws UiObjectNotFoundException {
     94         UiObject setAlarm = new UiObject(new UiSelector().textStartsWith("Alarm set"));
     95         if (!setAlarm.exists())
     96             setAlarm = new UiObject(new UiSelector().textStartsWith("Set alarm"));
     97         setAlarm.click();
     98 
     99         // let's add an alarm
    100         clickByDescription("Add alarm");
    101         // let's set the time
    102         clickByText("Time");
    103 
    104         // we want the minutes only
    105         UiSelector minuteAreaSelector = new UiSelector().className(
    106                 android.widget.NumberPicker.class.getName()).instance(1);
    107         UiSelector minuteIncreaseButtonSelector = minuteAreaSelector.childSelector(
    108                 new UiSelector().className(android.widget.Button.class.getName()).instance(1));
    109 
    110         // increment minutes a couple of times
    111         for (int x = 0; x < minutesFromNow; x++)
    112             new UiObject(minuteIncreaseButtonSelector).click();
    113         clickByText("Done");
    114 
    115         // few confirmations to click thru
    116         UiObject doneButton = new UiObject(new UiSelector().text("Done"));
    117         UiObject okButton = new UiObject(new UiSelector().text("OK"));
    118         // working around some inconsistencies in phone vs tablet UI
    119         if (doneButton.exists()) {
    120             doneButton.click();
    121         } else {
    122             okButton.click(); // let it fail if neither exists
    123         }
    124 
    125         // we're done. Let's return to home screen
    126         clickByText("Done");
    127         getUiDevice().pressHome();
    128     }
    129 
    130     /**
    131      * Helper to click on objects that match the content-description text
    132      * @param text
    133      * @throws UiObjectNotFoundException
    134      */
    135     private void clickByDescription(String text) throws UiObjectNotFoundException {
    136         UiObject obj = new UiObject(new UiSelector().description(text));
    137         obj.clickAndWaitForNewWindow();
    138     }
    139 
    140     /**
    141      * Helper to click on object that match the text value
    142      * @param text
    143      * @throws UiObjectNotFoundException
    144      */
    145     private void clickByText(String text) throws UiObjectNotFoundException {
    146         UiObject obj = new UiObject(new UiSelector().text(text));
    147         obj.clickAndWaitForNewWindow();
    148     }
    149 }