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