ViewElement.java revision 6316362de61fca700d7d5a455ad5c0ac9717c365
1/* 2 * Copyright (C) 2013 DroidDriver committers 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 17package com.google.android.droiddriver.instrumentation; 18 19import static com.google.android.droiddriver.util.TextUtils.charSequenceToString; 20 21import android.content.res.Resources; 22import android.graphics.Rect; 23import android.view.View; 24import android.view.ViewGroup; 25import android.view.ViewParent; 26import android.widget.Checkable; 27import android.widget.TextView; 28 29import com.google.android.droiddriver.InputInjector; 30import com.google.android.droiddriver.base.AbstractUiElement; 31import com.google.common.base.Preconditions; 32 33/** 34 * A UiElement that is backed by a View. 35 */ 36public class ViewElement extends AbstractUiElement { 37 private final InstrumentationContext context; 38 private final View view; 39 40 public ViewElement(InstrumentationContext context, View view) { 41 this.context = Preconditions.checkNotNull(context); 42 this.view = Preconditions.checkNotNull(view); 43 } 44 45 @Override 46 public String getText() { 47 if (!(view instanceof TextView)) { 48 return null; 49 } 50 return charSequenceToString(((TextView) view).getText()); 51 } 52 53 @Override 54 public String getContentDescription() { 55 return charSequenceToString(view.getContentDescription()); 56 } 57 58 @Override 59 public String getClassName() { 60 return view.getClass().getCanonicalName(); 61 } 62 63 @Override 64 public String getResourceId() { 65 if (view.getId() != View.NO_ID) { 66 try { 67 return charSequenceToString(view.getResources().getResourceName(view.getId())); 68 } catch (Resources.NotFoundException nfe) { 69 /* ignore */ 70 } 71 } 72 return null; 73 } 74 75 @Override 76 public String getPackageName() { 77 return view.getContext().getPackageName(); 78 } 79 80 @Override 81 public InputInjector getInjector() { 82 return context.getInjector(); 83 } 84 85 @Override 86 public boolean isVisible() { 87 return view.getGlobalVisibleRect(new Rect()); 88 } 89 90 @Override 91 public boolean isCheckable() { 92 return view instanceof Checkable; 93 } 94 95 @Override 96 public boolean isChecked() { 97 if (!isCheckable()) { 98 return false; 99 } 100 return ((Checkable) view).isChecked(); 101 } 102 103 @Override 104 public boolean isClickable() { 105 return view.isClickable(); 106 } 107 108 @Override 109 public boolean isEnabled() { 110 return view.isEnabled(); 111 } 112 113 @Override 114 public boolean isFocusable() { 115 return view.isFocusable(); 116 } 117 118 @Override 119 public boolean isFocused() { 120 return view.isFocused(); 121 } 122 123 @Override 124 public boolean isScrollable() { 125 // TODO: find a meaningful implementation 126 return true; 127 } 128 129 @Override 130 public boolean isLongClickable() { 131 return view.isLongClickable(); 132 } 133 134 @Override 135 public boolean isPassword() { 136 // TODO: find a meaningful implementation 137 return false; 138 } 139 140 @Override 141 public boolean isSelected() { 142 return view.isSelected(); 143 } 144 145 @Override 146 public Rect getBounds() { 147 Rect rect = new Rect(); 148 int[] xy = new int[2]; 149 view.getLocationOnScreen(xy); 150 rect.set(xy[0], xy[1], xy[0] + view.getWidth(), xy[1] + view.getHeight()); 151 return rect; 152 } 153 154 @Override 155 public int getChildCount() { 156 if (!(view instanceof ViewGroup)) { 157 return 0; 158 } 159 return ((ViewGroup) view).getChildCount(); 160 } 161 162 @Override 163 public ViewElement getChild(int index) { 164 if (!(view instanceof ViewGroup)) { 165 return null; 166 } 167 View child = ((ViewGroup) view).getChildAt(index); 168 return child == null ? null : context.getUiElement(child); 169 } 170 171 @Override 172 public ViewElement getParent() { 173 ViewParent parent = view.getParent(); 174 if (!(parent instanceof View)) { 175 return null; 176 } 177 return context.getUiElement((View) parent); 178 } 179} 180