1package com.xtremelabs.robolectric.shadows;
2
3import android.graphics.Rect;
4import com.xtremelabs.robolectric.internal.Implementation;
5import com.xtremelabs.robolectric.internal.Implements;
6import com.xtremelabs.robolectric.internal.RealObject;
7
8import static com.xtremelabs.robolectric.Robolectric.shadowOf_;
9
10@Implements(Rect.class)
11public class ShadowRect {
12    @RealObject Rect realRect;
13
14    public void __constructor__(int left, int top, int right, int bottom) {
15        realRect.left = left;
16        realRect.top = top;
17        realRect.right = right;
18        realRect.bottom = bottom;
19    }
20
21    public void __constructor__(Rect otherRect) {
22        realRect.left = otherRect.left;
23        realRect.top = otherRect.top;
24        realRect.right = otherRect.right;
25        realRect.bottom = otherRect.bottom;
26    }
27
28    @Implementation
29    public void set(Rect rect) {
30        set(rect.left, rect.top, rect.right, rect.bottom);
31    }
32
33    @Implementation
34    public void set(int left, int top, int right, int bottom) {
35        realRect.left = left;
36        realRect.top = top;
37        realRect.right = right;
38        realRect.bottom = bottom;
39    }
40
41    @Implementation
42    public int width() {
43        return realRect.right - realRect.left;
44    }
45
46    @Implementation
47    public int height() {
48        return realRect.bottom - realRect.top;
49    }
50
51    @Implementation
52    public boolean equals(Object obj) {
53        if (obj == null) return false;
54        Object o = shadowOf_(obj);
55        if (o == null) return false;
56        if (getClass() != o.getClass()) return false;
57        if (this == o) return true;
58
59        Rect r = (Rect) obj;
60        return realRect.left == r.left && realRect.top == r.top && realRect.right == r.right
61                && realRect.bottom == r.bottom;
62    }
63
64    @Implementation
65    public String toString() {
66        StringBuilder sb = new StringBuilder(32);
67        sb.append("Rect(");
68        sb.append(realRect.left);
69        sb.append(", ");
70        sb.append(realRect.top);
71        sb.append(" - ");
72        sb.append(realRect.right);
73        sb.append(", ");
74        sb.append(realRect.bottom);
75        sb.append(")");
76        return sb.toString();
77    }
78
79    @Implementation
80    public boolean contains(int x, int y) {
81    	return x > realRect.left && x < realRect.right
82    			&& y >= realRect.top && y <= realRect.bottom;
83    }
84
85    @Implementation
86    public boolean contains(Rect r) {
87    	return equals(r)
88    			|| (contains(r.left, r.top) && contains(r.right, r.top)
89    					&& contains(r.left, r.bottom) && contains(r.right, r.bottom));
90    }
91
92    @Implementation
93	public static boolean intersects(Rect a, Rect b) {
94    	return a.left < b.right && b.left < a.right
95    			&& a.top < b.bottom && b.top < a.bottom;
96    }
97
98    @Implementation
99    public boolean intersect(Rect r) {
100    	return intersects(realRect, r);
101    }
102
103    @Implementation
104    public boolean intersect(int left, int top, int right, int bottom) {
105    	return intersect(new Rect(left, top, right, bottom));
106    }
107
108    @Implementation
109    public void offset(int dx, int dy) {
110      realRect.left += dx;
111      realRect.right += dx;
112      realRect.top += dy;
113      realRect.bottom += dy;
114    }
115}
116