UiCollection.java revision 46d9444c7a39dc1c9fc60a5dcf4e79749d9b3859
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 */ 16package com.android.uiautomator.core; 17 18/** 19 * Used to enumerate a container's UI elements for the purpose of counting, 20 * or targeting a sub elements by a child's text or description. 21 */ 22public class UiCollection extends UiObject { 23 24 public UiCollection(UiSelector selector) { 25 super(selector); 26 } 27 28 /** 29 * Searches for child UI element within the constraints of this UiCollection {@link UiSelector} 30 * selector. 31 * 32 * It looks for any child matching the <code>childPattern</code> argument that has 33 * a child UI element anywhere within its sub hierarchy that has content-description text. 34 * The returned UiObject will point at the <code>childPattern</code> instance that matched the 35 * search and not at the identifying child element that matched the content description.</p> 36 * 37 * @param childPattern {@link UiSelector} selector of the child pattern to match and return 38 * @param text String of the identifying child contents of of the <code>childPattern</code> 39 * @return {@link UiObject} pointing at and instance of <code>childPattern</code> 40 * @throws UiObjectNotFoundException 41 */ 42 public UiObject getChildByDescription(UiSelector childPattern, String text) 43 throws UiObjectNotFoundException { 44 if (text != null) { 45 int count = getChildCount(childPattern); 46 for (int x = 0; x < count; x++) { 47 UiObject row = getChildByInstance(childPattern, x); 48 String nodeDesc = row.getContentDescription(); 49 if(nodeDesc != null && nodeDesc.contains(text)) { 50 return row; 51 } 52 UiObject item = row.getChild(new UiSelector().descriptionContains(text)); 53 if (item.exists()) { 54 return row; 55 } 56 } 57 } 58 throw new UiObjectNotFoundException("for description= \"" + text + "\""); 59 } 60 61 /** 62 * Searches for child UI element within the constraints of this UiCollection {@link UiSelector} 63 * selector. 64 * 65 * It looks for any child matching the <code>childPattern</code> argument that has 66 * a child UI element anywhere within its sub hierarchy that is at the <code>instance</code> 67 * specified. The operation is performed only on the visible items and no scrolling is performed 68 * in this case. 69 * 70 * @param childPattern {@link UiSelector} selector of the child pattern to match and return 71 * @param instance int the desired matched instance of this <code>childPattern</code> 72 * @return {@link UiObject} pointing at and instance of <code>childPattern</code> 73 */ 74 public UiObject getChildByInstance(UiSelector childPattern, int instance) 75 throws UiObjectNotFoundException { 76 UiSelector patternSelector = UiSelector.patternBuilder(getSelector(), 77 UiSelector.patternBuilder(childPattern).instance(instance)); 78 return new UiObject(patternSelector); 79 } 80 81 /** 82 * Searches for child UI element within the constraints of this UiCollection {@link UiSelector} 83 * selector. 84 * 85 * It looks for any child matching the <code>childPattern</code> argument that has 86 * a child UI element anywhere within its sub hierarchy that has text attribute = 87 * <code>text</code>. The returned UiObject will point at the <code>childPattern</code> 88 * instance that matched the search and not at the identifying child element that matched the 89 * text attribute.</p> 90 * 91 * @param childPattern {@link UiSelector} selector of the child pattern to match and return 92 * @param text String of the identifying child contents of of the <code>childPattern</code> 93 * @return {@link UiObject} pointing at and instance of <code>childPattern</code> 94 * @throws UiObjectNotFoundException 95 */ 96 public UiObject getChildByText(UiSelector childPattern, String text) 97 throws UiObjectNotFoundException { 98 99 if (text != null) { 100 int count = getChildCount(childPattern); 101 for (int x = 0; x < count; x++) { 102 UiObject row = getChildByInstance(childPattern, x); 103 String nodeText = row.getText(); 104 if(text.equals(nodeText)) { 105 return row; 106 } 107 UiObject item = row.getChild(new UiSelector().text(text)); 108 if (item.exists()) { 109 return row; 110 } 111 } 112 } 113 throw new UiObjectNotFoundException("for text= \"" + text + "\""); 114 } 115 116 /** 117 * Counts child UI element instances matching the <code>childPattern</code> 118 * argument. The method returns the number of matching UI elements that are 119 * currently visible. The count does not include items of a scrollable list 120 * that are off-screen. 121 * 122 * @param childPattern a {@link UiSelector} that represents the matching child UI 123 * elements to count 124 * @return the number of matched childPattern under the current {@link UiCollection} 125 */ 126 public int getChildCount(UiSelector childPattern) { 127 UiSelector patternSelector = 128 UiSelector.patternBuilder(getSelector(), UiSelector.patternBuilder(childPattern)); 129 return getQueryController().getPatternCount(patternSelector); 130 } 131} 132