1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Eclipse Public License, Version 1.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.eclipse.org/org/documents/epl-v10.php 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.ide.common.api; 18 19 import junit.framework.TestCase; 20 21 public class RectTest extends TestCase { 22 23 @Override setUp()24 protected void setUp() throws Exception { 25 super.setUp(); 26 } 27 testRect()28 public final void testRect() { 29 Rect r = new Rect(); 30 assertEquals(0, r.x); 31 assertEquals(0, r.y); 32 assertEquals(0, r.w); 33 assertEquals(0, r.h); 34 } 35 testRectIntIntIntInt()36 public final void testRectIntIntIntInt() { 37 Rect r = new Rect(1, 2, 3, 4); 38 assertEquals(1, r.x); 39 assertEquals(2, r.y); 40 assertEquals(3, r.w); 41 assertEquals(4, r.h); 42 } 43 testRectRect()44 public final void testRectRect() { 45 Rect r = new Rect(1, 2, 3, 4); 46 Rect r2 = new Rect(r); 47 48 assertNotSame(r2, r); 49 assertEquals(r2, r); 50 } 51 testSetIntIntIntInt()52 public final void testSetIntIntIntInt() { 53 Rect r = new Rect(1, 2, 3, 4); 54 Rect r2 = r.set(3, 4, 20, 30); 55 56 assertSame(r2, r); 57 assertEquals(3, r2.x); 58 assertEquals(4, r2.y); 59 assertEquals(20, r2.w); 60 assertEquals(30, r2.h); 61 } 62 testSetRect()63 public final void testSetRect() { 64 Rect r = new Rect(1, 2, 3, 4); 65 Rect r2 = new Rect(3, 4, 20, 30); 66 Rect r3 = r.set(r2); 67 68 assertSame(r3, r); 69 assertNotSame(r3, r2); 70 assertEquals(3, r.x); 71 assertEquals(4, r.y); 72 assertEquals(20, r.w); 73 assertEquals(30, r.h); 74 } 75 testCopy()76 public final void testCopy() { 77 Rect r = new Rect(1, 2, 3, 4); 78 Rect r2 = r.copy(); 79 80 assertNotSame(r2, r); 81 assertEquals(r2, r); 82 } 83 testIsValid()84 public final void testIsValid() { 85 Rect r = new Rect(); 86 assertFalse(r.isValid()); 87 88 r = new Rect(1, 2, 3, 4); 89 assertTrue(r.isValid()); 90 91 // Rectangles must have a width > 0 to be valid 92 r = new Rect(1, 2, 0, 4); 93 assertFalse(r.isValid()); 94 r = new Rect(1, 2, -5, 4); 95 assertFalse(r.isValid()); 96 97 // Rectangles must have a height > 0 to be valid 98 r = new Rect(1, 2, 3, 0); 99 assertFalse(r.isValid()); 100 r = new Rect(1, 2, 3, -5); 101 assertFalse(r.isValid()); 102 103 r = new Rect(1, 2, 0, 0); 104 assertFalse(r.isValid()); 105 r = new Rect(1, 2, -20, -5); 106 assertFalse(r.isValid()); 107 } 108 testContainsIntInt()109 public final void testContainsIntInt() { 110 Rect r = new Rect(3, 4, 20, 30); 111 112 assertTrue(r.contains(3, 4)); 113 assertTrue(r.contains(3+19, 4)); 114 assertTrue(r.contains(3+19, 4+29)); 115 assertTrue(r.contains(3, 4+29)); 116 117 assertFalse(r.contains(3-1, 4)); 118 assertFalse(r.contains(3, 4-1)); 119 assertFalse(r.contains(3-1, 4-1)); 120 121 assertFalse(r.contains(3+20, 4)); 122 assertFalse(r.contains(3+20, 4+30)); 123 assertFalse(r.contains(3, 4+30)); 124 } 125 testContainsIntInt_Invalid()126 public final void testContainsIntInt_Invalid() { 127 // Invalid rects always return false 128 Rect r = new Rect(3, 4, -20, -30); 129 assertFalse(r.contains(3, 4)); 130 } 131 testContainsPoint_Null()132 public final void testContainsPoint_Null() { 133 // contains(null) returns false rather than an NPE 134 Rect r = new Rect(3, 4, -20, -30); 135 assertFalse(r.contains((Point) null)); 136 } 137 testContainsRect_Null()138 public final void testContainsRect_Null() { 139 // contains(null) returns false rather than an NPE 140 Rect r = new Rect(3, 4, -20, -30); 141 assertFalse(r.contains((Rect) null)); 142 } 143 testContainsPoint()144 public final void testContainsPoint() { 145 Rect r = new Rect(3, 4, 20, 30); 146 147 assertTrue(r.contains(new Point(3, 4))); 148 assertTrue(r.contains(new Point(3+19, 4))); 149 assertTrue(r.contains(new Point(3+19, 4+29))); 150 assertTrue(r.contains(new Point(3, 4+29))); 151 152 assertFalse(r.contains(new Point(3-1, 4))); 153 assertFalse(r.contains(new Point(3, 4-1))); 154 assertFalse(r.contains(new Point(3-1, 4-1))); 155 156 assertFalse(r.contains(new Point(3+20, 4))); 157 assertFalse(r.contains(new Point(3+20, 4+30))); 158 assertFalse(r.contains(new Point(3, 4+30))); 159 } 160 testContainsRect()161 public final void testContainsRect() { 162 Rect r = new Rect(3, 4, 20, 30); 163 164 assertTrue(r.contains(new Rect(3, 4, 5, 10))); 165 assertFalse(r.contains(new Rect(3 - 1, 4, 5, 10))); 166 } 167 testIntersects()168 public final void testIntersects() { 169 Rect r1 = new Rect(0, 0, 10, 10); 170 Rect r2 = new Rect(1, 1, 5, 5); 171 Rect r3 = new Rect(10, 0, 1, 1); 172 Rect r4 = new Rect(5, 5, 10, 10); 173 Rect r5 = new Rect(-1, 0, 1, 1); 174 Rect r6 = new Rect(0, 10, 1, 1); 175 176 assertTrue(r1.intersects(r2)); 177 assertTrue(r2.intersects(r1)); 178 assertTrue(r1.intersects(r4)); 179 assertFalse(r1.intersects(r3)); 180 assertFalse(r1.intersects(r5)); 181 assertFalse(r1.intersects(r6)); 182 } 183 testMoveTo()184 public final void testMoveTo() { 185 Rect r = new Rect(3, 4, 20, 30); 186 Rect r2 = r.moveTo(100, 200); 187 188 assertSame(r2, r); 189 assertEquals(100, r.x); 190 assertEquals(200, r.y); 191 assertEquals(20, r.w); 192 assertEquals(30, r.h); 193 } 194 testOffsetBy()195 public final void testOffsetBy() { 196 Rect r = new Rect(3, 4, 20, 30); 197 Rect r2 = r.offsetBy(100, 200); 198 199 assertSame(r2, r); 200 assertEquals(103, r.x); 201 assertEquals(204, r.y); 202 assertEquals(20, r.w); 203 assertEquals(30, r.h); 204 } 205 testGetCenter()206 public final void testGetCenter() { 207 Rect r = new Rect(3, 4, 20, 30); 208 Point p = r.getCenter(); 209 210 assertEquals(3+20/2, p.x); 211 assertEquals(4+30/2, p.y); 212 } 213 testGetTopLeft()214 public final void testGetTopLeft() { 215 Rect r = new Rect(3, 4, 20, 30); 216 Point p = r.getTopLeft(); 217 218 assertEquals(3, p.x); 219 assertEquals(4, p.y); 220 } 221 testGetBottomLeft()222 public final void testGetBottomLeft() { 223 Rect r = new Rect(3, 4, 20, 30); 224 Point p = r.getBottomLeft(); 225 226 assertEquals(3, p.x); 227 assertEquals(4+30, p.y); 228 } 229 testGetTopRight()230 public final void testGetTopRight() { 231 Rect r = new Rect(3, 4, 20, 30); 232 Point p = r.getTopRight(); 233 234 assertEquals(3+20, p.x); 235 assertEquals(4, p.y); 236 } 237 testGetBottomRight()238 public final void testGetBottomRight() { 239 Rect r = new Rect(3, 4, 20, 30); 240 Point p = r.getBottomRight(); 241 242 assertEquals(3+20, p.x); 243 assertEquals(4+30, p.y); 244 } 245 testToString()246 public final void testToString() { 247 Rect r = new Rect(3, 4, 20, 30); 248 249 assertEquals("Rect [(3,4)-(23,34): 20x30]", r.toString()); 250 } 251 testEqualsObject()252 public final void testEqualsObject() { 253 Rect r = new Rect(3, 4, 20, 30); 254 255 assertFalse(r.equals(null)); 256 assertFalse(r.equals(new Object())); 257 assertTrue(r.equals(new Rect(3, 4, 20, 30))); 258 } 259 testEqualsObject_Invalid()260 public final void testEqualsObject_Invalid() { 261 Rect r = new Rect(3, 4, 20, 30); 262 assertTrue(r.isValid()); 263 264 Rect i1 = new Rect(3, 4, 0, 0); 265 assertFalse(i1.isValid()); 266 Rect i2 = new Rect(10, 20, 0, 0); 267 assertFalse(i2.isValid()); 268 269 // valid rects can't be equal to invalid rects 270 assertFalse(r.equals(i1)); 271 assertFalse(r.equals(i2)); 272 273 // invalid rects are equal to each other whatever their content is 274 assertEquals(i2, i1); 275 } 276 testHashCode()277 public final void testHashCode() { 278 Rect r = new Rect(1, 2, 3, 4); 279 Rect r1 = new Rect(3, 4, 20, 30); 280 Rect r2 = new Rect(3, 4, 20, 30); 281 282 assertFalse(r1.hashCode() == r.hashCode()); 283 assertEquals(r2.hashCode(), r1.hashCode()); 284 } 285 286 testCenter()287 public final void testCenter() { 288 Rect r = new Rect(10, 20, 30, 40); 289 Point center = r.center(); 290 assertEquals(25, center.x); 291 assertEquals(40, center.y); 292 assertEquals(25, r.centerX()); 293 assertEquals(40, r.centerY()); 294 } 295 testX2Y2()296 public final void testX2Y2() { 297 Rect r = new Rect(1, 2, 3, 4); 298 assertEquals(4, r.x2()); 299 assertEquals(6, r.y2()); 300 } 301 } 302