1 package org.robolectric.shadows; 2 3 import static com.google.common.truth.Truth.assertThat; 4 5 import android.graphics.Rect; 6 import androidx.test.ext.junit.runners.AndroidJUnit4; 7 import org.junit.Before; 8 import org.junit.Test; 9 import org.junit.runner.RunWith; 10 11 @RunWith(AndroidJUnit4.class) 12 public class ShadowRectTest { 13 @Before setUp()14 public void setUp() { 15 } 16 17 @Test constructorSetsCoordinates()18 public void constructorSetsCoordinates() { 19 Rect r = new Rect(1, 2, 3, 4); 20 assertThat(r.left).isEqualTo(1); 21 assertThat(r.top).isEqualTo(2); 22 assertThat(r.right).isEqualTo(3); 23 assertThat(r.bottom).isEqualTo(4); 24 } 25 26 @Test secondConstructorSetsCoordinates()27 public void secondConstructorSetsCoordinates() { 28 Rect existingRect = new Rect(1, 2, 3, 4); 29 Rect r = new Rect(existingRect); 30 assertThat(r.left).isEqualTo(1); 31 assertThat(r.top).isEqualTo(2); 32 assertThat(r.right).isEqualTo(3); 33 assertThat(r.bottom).isEqualTo(4); 34 } 35 36 @Test width()37 public void width() { 38 Rect r = new Rect(0, 0, 10, 10); 39 assertThat(r.width()).isEqualTo(10); 40 } 41 42 @Test height()43 public void height() { 44 Rect r = new Rect(0, 0, 10, 10); 45 assertThat(r.height()).isEqualTo(10); 46 } 47 48 @Test doesntEqual()49 public void doesntEqual() { 50 Rect a = new Rect(1, 2, 3, 4); 51 Rect b = new Rect(2, 3, 4, 5); 52 assertThat(a.equals(b)).isFalse(); 53 } 54 55 @Test equals()56 public void equals() { 57 Rect a = new Rect(1, 2, 3, 4); 58 Rect b = new Rect(1, 2, 3, 4); 59 assertThat(a.equals(b)).isTrue(); 60 } 61 62 @Test doesntContainPoint()63 public void doesntContainPoint() { 64 Rect r = new Rect(0, 0, 10, 10); 65 assertThat(r.contains(11, 11)).isFalse(); 66 } 67 68 @Test containsPoint()69 public void containsPoint() { 70 Rect r = new Rect(0, 0, 10, 10); 71 assertThat(r.contains(5, 5)).isTrue(); 72 } 73 74 @Test doesntContainPointOnLeftEdge()75 public void doesntContainPointOnLeftEdge() { 76 Rect r = new Rect(0, 0, 10, 10); 77 assertThat(r.contains(0, 10)).isFalse(); 78 } 79 80 @Test doesntContainPointOnRightEdge()81 public void doesntContainPointOnRightEdge() { 82 Rect r = new Rect(0, 0, 10, 10); 83 assertThat(r.contains(10, 5)).isFalse(); 84 } 85 86 @Test containsPointOnTopEdge()87 public void containsPointOnTopEdge() { 88 Rect r = new Rect(0, 0, 10, 10); 89 assertThat(r.contains(5, 0)).isTrue(); 90 } 91 92 @Test containsPointOnBottomEdge()93 public void containsPointOnBottomEdge() { 94 Rect r = new Rect(0, 0, 10, 10); 95 assertThat(r.contains(5, 10)).isFalse(); 96 } 97 98 @Test doesntContainRect()99 public void doesntContainRect() { 100 Rect a = new Rect(0, 0, 10, 10); 101 Rect b = new Rect(11, 11, 12, 12); 102 assertThat(a.contains(b)).isFalse(); 103 } 104 105 @Test containsRect()106 public void containsRect() { 107 Rect a = new Rect(0, 0, 10, 10); 108 Rect b = new Rect(8, 8, 9, 9); 109 assertThat(a.contains(b)).isTrue(); 110 } 111 112 @Test containsEqualRect()113 public void containsEqualRect() { 114 Rect a = new Rect(0, 0, 10, 10); 115 Rect b = new Rect(0, 0, 10, 10); 116 assertThat(a.contains(b)).isTrue(); 117 } 118 119 @Test intersectsButDoesntContainRect()120 public void intersectsButDoesntContainRect() { 121 Rect a = new Rect(0, 0, 10, 10); 122 Rect b = new Rect(5, 5, 15, 15); 123 assertThat(a.contains(b)).isFalse(); 124 } 125 126 @Test doesntIntersect()127 public void doesntIntersect() { 128 Rect a = new Rect(0, 0, 10, 10); 129 Rect b = new Rect(11, 11, 21, 21); 130 assertThat(Rect.intersects(a, b)).isFalse(); 131 } 132 133 @Test intersects()134 public void intersects() { 135 Rect a = new Rect(0, 0, 10, 10); 136 Rect b = new Rect(5, 0, 15, 10); 137 assertThat(Rect.intersects(a, b)).isTrue(); 138 } 139 140 @Test almostIntersects()141 public void almostIntersects() { 142 Rect a = new Rect(3, 0, 4, 2); 143 Rect b = new Rect(1, 0, 3, 1); 144 assertThat(Rect.intersects(a, b)).isFalse(); 145 } 146 147 @Test intersectRect()148 public void intersectRect() { 149 Rect a = new Rect(0, 0, 10, 10); 150 Rect b = new Rect(5, 0, 15, 10); 151 assertThat(a.intersect(b)).isTrue(); 152 } 153 154 @Test intersectCoordinates()155 public void intersectCoordinates() { 156 Rect r = new Rect(0, 0, 10, 10); 157 assertThat(r.intersect(5, 0, 15, 10)).isTrue(); 158 } 159 160 @Test setWithIntsSetsCoordinates()161 public void setWithIntsSetsCoordinates() { 162 Rect r = new Rect(); 163 r.set(1, 2, 3, 4); 164 assertThat(r.left).isEqualTo(1); 165 assertThat(r.top).isEqualTo(2); 166 assertThat(r.right).isEqualTo(3); 167 assertThat(r.bottom).isEqualTo(4); 168 } 169 170 @Test setWithRectSetsCoordinates()171 public void setWithRectSetsCoordinates() { 172 Rect rSrc = new Rect(1, 2, 3, 4); 173 Rect r = new Rect(); 174 r.set(rSrc); 175 assertThat(r.left).isEqualTo(1); 176 assertThat(r.top).isEqualTo(2); 177 assertThat(r.right).isEqualTo(3); 178 assertThat(r.bottom).isEqualTo(4); 179 } 180 181 @Test offsetModifiesRect()182 public void offsetModifiesRect() { 183 Rect r = new Rect(1, 2, 3, 4); 184 r.offset(10, 20); 185 assertThat(r.left).isEqualTo(11); 186 assertThat(r.top).isEqualTo(22); 187 assertThat(r.right).isEqualTo(13); 188 assertThat(r.bottom).isEqualTo(24); 189 } 190 } 191