• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.android.uiautomator.core.UiObject;
19 import com.android.uiautomator.core.UiObjectNotFoundException;
20 import com.android.uiautomator.core.UiScrollable;
21 import com.android.uiautomator.core.UiSelector;
22 import com.android.uiautomator.testrunner.UiAutomatorTestCase;
23 
24 /**
25  * This demos how we read content-description to properly open the
26  * All Apps view and select and application to launch. Then we will
27  * use the package name to verify that the current window is actually
28  * from the expected package
29  */
30 public class LaunchSettings extends UiAutomatorTestCase {
31 
testDemo()32     public void testDemo() throws UiObjectNotFoundException {
33         // Good practice to start from a common place
34         getUiDevice().pressHome();
35 
36         // When we use the uiautomatorviewer in the DSK/tools we find that this
37         // button has the following content-description
38         UiObject allAppsButton = new UiObject(new UiSelector().description("Apps"));
39 
40         // ** NOTE **
41         // Any operation on a UiObject that is not currently present on the display
42         // will result in a UiObjectNotFoundException to be thrown. If we want to
43         // first check if the object exists we can use allAppsButton.exists() which
44         // return boolean.
45         // ** NOTE **
46 
47         //The operation below expects the click will result a new  window.
48         allAppsButton.clickAndWaitForNewWindow();
49 
50         // On the this view, we expect two tabs, one for APPS and another for
51         // WIDGETS. We will want to click the APPS just so we're sure apps and
52         // not widgets is our current display
53         UiObject appsTab = new UiObject(new UiSelector().text("Apps"));
54 
55         // ** NOTE **
56         // The above operation assumes that there is only one text "Apps" in the
57         // current view. If not, then the first "Apps" is selected. But if we
58         // want to be certain that we click on the tab "Apps", we can use the
59         // uiautomatorview from the SDK/tools and see if we can further narrow the
60         // selector. Comment the line above and uncomment the one bellow to use
61         // the more specific selector.
62         // ** NOTE **
63 
64         // This creates a selector hierarchy where the first selector is for the
65         // TabWidget and the second will search only inside the layout of TabWidget
66         // To use this instead, uncomment the lines bellow and comment the above appsTab
67         //UiSelector appsTabSelector =
68         //        new UiSelector().className(android.widget.TabWidget.class.getName())
69         //            .childSelector(new UiSelector().text("Apps"));
70         //UiObject appsTab = new UiObject(appsTabSelector);
71 
72 
73         // The operation below we only cause a content change so a click() is good
74         appsTab.click();
75 
76         // Since our device may have many apps on it spanning multiple views, we
77         // may need to scroll to find our app. Here we use UiScrollable to help.
78         // We declare the object with a selector to a scrollable view. Since in this
79         // case the firt scrollable view happens to be the one containing our apps
80         // list, we should be ok.
81         UiScrollable appViews = new UiScrollable(new UiSelector().scrollable(true));
82         // swipe horizontally when searching (default is vertical)
83         appViews.setAsHorizontalList();
84 
85         // the appsViews will perform horizontal scrolls to find the Settings app
86         UiObject settingsApp = appViews.getChildByText(
87                 new UiSelector().className(android.widget.TextView.class.getName()), "Settings");
88         settingsApp.clickAndWaitForNewWindow();
89 
90         // create a selector for anything on the display and check if the package name
91         // is the expected one
92         UiObject settingsValidation =
93                 new UiObject(new UiSelector().packageName("com.android.settings"));
94 
95         assertTrue("Unable to detect Settings", settingsValidation.exists());
96     }
97 }
98