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