ShadowView.java revision 415f3fdab8162ece7185268f71b577f7faa0ba38
1package com.xtremelabs.robolectric.fakes; 2 3import android.content.Context; 4import android.content.res.Resources; 5import android.util.AttributeSet; 6import android.view.MotionEvent; 7import android.view.View; 8import android.view.ViewGroup; 9import android.view.ViewParent; 10import com.xtremelabs.robolectric.util.Implementation; 11import com.xtremelabs.robolectric.util.Implements; 12 13import java.util.HashMap; 14import java.util.Map; 15 16@SuppressWarnings({"UnusedDeclaration"}) 17@Implements(View.class) 18public class FakeView { 19 @Deprecated 20 public static final int UNINITIALIZED_ATTRIBUTE = -1000; 21 22 protected View realView; 23 24 private int id; 25 FakeView parent; 26 private Context context; 27 public boolean selected; 28 private View.OnClickListener onClickListener; 29 private Object tag; 30 private boolean enabled = true; 31 public int visibility = View.VISIBLE; 32 public int left; 33 public int top; 34 public int right; 35 public int bottom; 36 public int paddingLeft; 37 public int paddingTop; 38 public int paddingRight; 39 public int paddingBottom; 40 public ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(0, 0); 41 private Map<Integer, Object> tags = new HashMap<Integer, Object>(); 42 public boolean clickable; 43 protected boolean focusable; 44 public int backgroundResourceId = -1; 45 protected View.OnKeyListener onKeyListener; 46 public boolean hasFocus; 47 private View.OnFocusChangeListener onFocusChangeListener; 48 public boolean wasInvalidated; 49 private View.OnTouchListener onTouchListener; 50 private boolean focusableInTouchMode; 51 52 public FakeView(View view) { 53 this.realView = view; 54 } 55 56 public void __constructor__(Context context) { 57 this.context = context; 58 } 59 60 public void __constructor__(Context context, AttributeSet attrs) { 61 __constructor__(context); 62 } 63 64 @Implementation 65 public void setId(int id) { 66 this.id = id; 67 } 68 69 @Implementation 70 public void setClickable(boolean clickable) { 71 this.clickable = clickable; 72 } 73 74 @Implementation 75 public void setFocusable(boolean focusable) { 76 this.focusable = focusable; 77 if(!focusable) { 78 setFocusableInTouchMode(false); 79 } 80 } 81 82 @Implementation 83 public final boolean isFocusableInTouchMode() { 84 return focusableInTouchMode; 85 } 86 87 @Implementation 88 public void setFocusableInTouchMode(boolean focusableInTouchMode) { 89 this.focusableInTouchMode = focusableInTouchMode; 90 if(focusableInTouchMode) { 91 setFocusable(true); 92 } 93 } 94 95 @Implementation 96 public boolean isFocusable() { 97 return focusable; 98 } 99 100 @Implementation 101 public int getId() { 102 return id; 103 } 104 105 @Implementation 106 public static View inflate(Context context, int resource, ViewGroup root) { 107 View view = FakeLayoutInflater.from(context).inflate(resource, root); 108 if (root != null) { 109 root.addView(view); 110 } 111 return view; 112 } 113 114 @Implementation 115 public View findViewById(int id) { 116 if (id == this.id) { 117 return realView; 118 } 119 120 return null; 121 } 122 123 @Implementation 124 public View getRootView() { 125 FakeView root = this; 126 while(root.parent != null) { 127 root = root.parent; 128 } 129 return root.realView; 130 } 131 132 @Implementation 133 public ViewGroup.LayoutParams getLayoutParams() { 134 return layoutParams; 135 } 136 137 @Implementation 138 public void setLayoutParams(ViewGroup.LayoutParams params) { 139 layoutParams = params; 140 } 141 142 @Implementation 143 public final ViewParent getParent() { 144 return parent == null ? null : (ViewParent) parent.realView; 145 } 146 147 @Implementation 148 public final Context getContext() { 149 return context; 150 } 151 152 @Implementation 153 public Resources getResources() { 154 return context.getResources(); 155 } 156 157 @Implementation 158 public void setBackgroundResource(int backgroundResourceId) { 159 this.backgroundResourceId = backgroundResourceId; 160 } 161 162 @Implementation 163 public int getVisibility() { 164 return visibility; 165 } 166 167 @Implementation 168 public void setVisibility(int visibility) { 169 this.visibility = visibility; 170 } 171 172 @Implementation 173 public void setSelected(boolean selected) { 174 this.selected = selected; 175 } 176 177 @Implementation 178 public boolean isSelected() { 179 return this.selected; 180 } 181 182 @Implementation 183 public boolean isEnabled() { 184 return this.enabled; 185 } 186 187 @Implementation 188 public void setEnabled(boolean enabled) { 189 this.enabled = enabled; 190 } 191 192 @Implementation 193 public void setOnClickListener(View.OnClickListener onClickListener) { 194 this.onClickListener = onClickListener; 195 } 196 197 @Implementation 198 public boolean performClick() { 199 if (onClickListener != null) { 200 onClickListener.onClick(realView); 201 return true; 202 } else { 203 return false; 204 } 205 } 206 207 @Implementation 208 public void setOnKeyListener(View.OnKeyListener onKeyListener) { 209 this.onKeyListener = onKeyListener; 210 } 211 212 @Implementation 213 public Object getTag() { 214 return this.tag; 215 } 216 217 @Implementation 218 public void setTag(Object tag) { 219 this.tag = tag; 220 } 221 222 @Implementation 223 public final int getHeight() { 224 return bottom - top; 225 } 226 227 @Implementation 228 public final int getWidth() { 229 return right - left; 230 } 231 232 @Implementation 233 public final int getMeasuredWidth() { 234 return getWidth(); 235 } 236 237 @Implementation 238 public final void layout(int l, int t, int r, int b) { 239 left = l; 240 top = t; 241 right = r; 242 bottom = b; 243 244// todo: realView.onLayout(); 245 } 246 247 @Implementation 248 public void setPadding(int left, int top, int right, int bottom) { 249 paddingLeft = left; 250 paddingTop = top; 251 paddingRight = right; 252 paddingBottom = bottom; 253 } 254 255 @Implementation 256 public int getPaddingTop() { 257 return paddingTop; 258 } 259 260 @Implementation 261 public int getPaddingLeft() { 262 return paddingLeft; 263 } 264 265 @Implementation 266 public int getPaddingRight() { 267 return paddingRight; 268 } 269 270 @Implementation 271 public int getPaddingBottom() { 272 return paddingBottom; 273 } 274 275 @Implementation 276 public Object getTag(int key) { 277 return tags.get(key); 278 } 279 280 @Implementation 281 public void setTag(int key, Object value) { 282 tags.put(key, value); 283 } 284 285 @Implementation 286 public final boolean requestFocus() { 287 return requestFocus(View.FOCUS_DOWN); 288 } 289 290 @Implementation 291 public final boolean requestFocus(int direction) { 292 setViewFocus(true); 293 return true; 294 } 295 296 public void setViewFocus(boolean hasFocus) { 297 this.hasFocus = hasFocus; 298 if (onFocusChangeListener != null) { 299 onFocusChangeListener.onFocusChange(realView, hasFocus); 300 } 301 } 302 303 @Implementation 304 public boolean isFocused() { 305 return hasFocus; 306 } 307 308 @Implementation 309 public boolean hasFocus() { 310 return hasFocus; 311 } 312 313 @Implementation 314 public void clearFocus() { 315 setViewFocus(false); 316 } 317 318 @Implementation 319 public void setOnFocusChangeListener(View.OnFocusChangeListener listener) { 320 onFocusChangeListener = listener; 321 } 322 323 @Implementation 324 public void invalidate() { 325 wasInvalidated = true; 326 } 327 328 @Implementation 329 public void setOnTouchListener(View.OnTouchListener onTouchListener) { 330 this.onTouchListener = onTouchListener; 331 } 332 333 @Implementation 334 public boolean dispatchTouchEvent(MotionEvent event) { 335 if (onTouchListener != null) { 336 return onTouchListener.onTouch(realView, event); 337 } 338 return false; 339 } 340 341 public String innerText() { 342 return ""; 343 } 344} 345