ShadowView.java revision 4d709f74a8a630e677915cff8fba5e8b4888800f
1package com.xtremelabs.robolectric.shadows;
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;
12import com.xtremelabs.robolectric.util.RealObject;
13
14import java.io.PrintStream;
15import java.util.HashMap;
16import java.util.Map;
17
18import static com.xtremelabs.robolectric.Robolectric.shadowOf;
19
20@SuppressWarnings({"UnusedDeclaration"})
21@Implements(View.class)
22public class ShadowView {
23    @Deprecated
24    public static final int UNINITIALIZED_ATTRIBUTE = -1000;
25
26    @RealObject protected View realView;
27
28    private int id;
29    ShadowView parent;
30    private Context context;
31    private boolean selected;
32    private View.OnClickListener onClickListener;
33    private Object tag;
34    private boolean enabled = true;
35    private int visibility = View.VISIBLE;
36    int left;
37    int top;
38    int right;
39    int bottom;
40    private int paddingLeft;
41    private int paddingTop;
42    private int paddingRight;
43    private int paddingBottom;
44    private ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(0, 0);
45    private Map<Integer, Object> tags = new HashMap<Integer, Object>();
46    private boolean clickable;
47    protected boolean focusable;
48    boolean focusableInTouchMode;
49    private int backgroundResourceId = -1;
50    protected View.OnKeyListener onKeyListener;
51    private boolean isFocused;
52    private View.OnFocusChangeListener onFocusChangeListener;
53    private boolean wasInvalidated;
54    private View.OnTouchListener onTouchListener;
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 = ShadowLayoutInflater.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        ShadowView 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.isFocused = hasFocus;
298        if (onFocusChangeListener != null) {
299            onFocusChangeListener.onFocusChange(realView, hasFocus);
300        }
301    }
302
303    @Implementation
304    public boolean isFocused() {
305        return isFocused;
306    }
307
308    @Implementation
309    public boolean hasFocus() {
310        return isFocused;
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    public void dump() {
346        dump(System.out, 0);
347    }
348
349    public void dump(PrintStream out, int indent) {
350        dumpFirstPart(out, indent);
351        out.println("/>");
352    }
353
354    protected void dumpFirstPart(PrintStream out, int indent) {
355        dumpIndent(out, indent);
356
357        out.print("<" + realView.getClass().getSimpleName());
358        if (id > 0) {
359            out.print(" id=\"" + shadowOf(context).getResourceLoader().getNameForId(id) + "\"");
360        }
361    }
362
363    protected void dumpIndent(PrintStream out, int indent) {
364        for (int i = 0; i < indent; i++) out.print(" ");
365    }
366
367    public int getLeft() {
368        return left;
369    }
370
371    public int getTop() {
372        return top;
373    }
374
375    public int getRight() {
376        return right;
377    }
378
379    public int getBottom() {
380        return bottom;
381    }
382
383    public boolean isClickable() {
384        return clickable;
385    }
386
387    public int getBackgroundResourceId() {
388        return backgroundResourceId;
389    }
390
391    public boolean wasInvalidated() {
392        return wasInvalidated;
393    }
394
395    public void clearWasInvalidated() {
396        wasInvalidated = false;
397    }
398
399    public void setLeft(int left) {
400        this.left = left;
401    }
402
403    public void setTop(int top) {
404        this.top = top;
405    }
406
407    public void setRight(int right) {
408        this.right = right;
409    }
410
411    public void setBottom(int bottom) {
412        this.bottom = bottom;
413    }
414
415    public void setPaddingLeft(int paddingLeft) {
416        this.paddingLeft = paddingLeft;
417    }
418
419    public void setPaddingTop(int paddingTop) {
420        this.paddingTop = paddingTop;
421    }
422
423    public void setPaddingRight(int paddingRight) {
424        this.paddingRight = paddingRight;
425    }
426
427    public void setPaddingBottom(int paddingBottom) {
428        this.paddingBottom = paddingBottom;
429    }
430
431    public void setFocused(boolean focused) {
432        isFocused = focused;
433    }
434}
435