aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/xtremelabs/robolectric/shadows/ShadowRect.java
blob: 3573aafd01f1a4aa78c2a009653cd985743ad145 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.xtremelabs.robolectric.shadows;

import android.graphics.Rect;
import com.xtremelabs.robolectric.internal.Implementation;
import com.xtremelabs.robolectric.internal.Implements;
import com.xtremelabs.robolectric.internal.RealObject;

import static com.xtremelabs.robolectric.Robolectric.shadowOf_;

@Implements(Rect.class)
public class ShadowRect {
    @RealObject Rect realRect;

    public void __constructor__(int left, int top, int right, int bottom) {
        realRect.left = left;
        realRect.top = top;
        realRect.right = right;
        realRect.bottom = bottom;
    }

    public void __constructor__(Rect otherRect) {
        realRect.left = otherRect.left;
        realRect.top = otherRect.top;
        realRect.right = otherRect.right;
        realRect.bottom = otherRect.bottom;
    }

    @Implementation    
    public void set(Rect rect) {
        set(rect.left, rect.top, rect.right, rect.bottom);
    }
    
    @Implementation
    public void set(int left, int top, int right, int bottom) {
        realRect.left = left;
        realRect.top = top;
        realRect.right = right;
        realRect.bottom = bottom;
    }

    @Implementation
    public int width() {
        return realRect.right - realRect.left;
    }

    @Implementation
    public int height() {
        return realRect.bottom - realRect.top;
    }

    @Implementation
    public boolean equals(Object obj) {
        if (obj == null) return false;
        Object o = shadowOf_(obj);
        if (o == null) return false;
        if (getClass() != o.getClass()) return false;
        if (this == o) return true;

        Rect r = (Rect) obj;
        return realRect.left == r.left && realRect.top == r.top && realRect.right == r.right
                && realRect.bottom == r.bottom;
    }

    @Implementation
    public String toString() {
        StringBuilder sb = new StringBuilder(32);
        sb.append("Rect(");
        sb.append(realRect.left);
        sb.append(", ");
        sb.append(realRect.top);
        sb.append(" - ");
        sb.append(realRect.right);
        sb.append(", ");
        sb.append(realRect.bottom);
        sb.append(")");
        return sb.toString();
    }
    
    @Implementation
    public boolean contains(int x, int y) {
    	return x > realRect.left && x < realRect.right
    			&& y >= realRect.top && y <= realRect.bottom;
    } 

    @Implementation
    public boolean contains(Rect r) {
    	return equals(r)
    			|| (contains(r.left, r.top) && contains(r.right, r.top)
    					&& contains(r.left, r.bottom) && contains(r.right, r.bottom));
    }
    
    @Implementation
	public static boolean intersects(Rect a, Rect b) {
    	return a.left < b.right && b.left < a.right
    			&& a.top < b.bottom && b.top < a.bottom;
    }
    
    @Implementation
    public boolean intersect(Rect r) {
    	return intersects(realRect, r);
    }
    
    @Implementation
    public boolean intersect(int left, int top, int right, int bottom) {
    	return intersect(new Rect(left, top, right, bottom));
    }
    
    @Implementation
    public void offset(int dx, int dy) {
      realRect.left += dx;
      realRect.right += dx;
      realRect.top += dy;
      realRect.bottom += dy;
    }
}