1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0 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 android.graphics.cts; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertFalse; 21 import static org.junit.Assert.assertNotNull; 22 import static org.junit.Assert.assertTrue; 23 24 import android.graphics.Rect; 25 import android.graphics.RectF; 26 import android.os.Parcel; 27 28 import androidx.test.filters.SmallTest; 29 import androidx.test.runner.AndroidJUnit4; 30 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 34 @SmallTest 35 @RunWith(AndroidJUnit4.class) 36 public class RectFTest { 37 private RectF mRectF; 38 39 @Test testConstructor()40 public void testConstructor() { 41 mRectF = new RectF(); 42 mRectF = new RectF(1.5f, 2.5f, 20.3f, 40.9f); 43 44 RectF rectF = new RectF(1.5f, 2.5f, 20.3f, 40.9f); 45 mRectF = new RectF(rectF); 46 47 Rect rect = new Rect(0, 0, 10, 10); 48 mRectF = new RectF(rect); 49 } 50 51 @Test testSort()52 public void testSort() { 53 mRectF = new RectF(10, 10, 5, 5); 54 assertEquals(10.0f, mRectF.left, 0.0f); 55 assertEquals(10.0f, mRectF.top, 0.0f); 56 assertEquals(5.0f, mRectF.right, 0.0f); 57 assertEquals(5.0f, mRectF.bottom, 0.0f); 58 59 mRectF.sort(); 60 assertEquals(5.0f, mRectF.left, 0.0f); 61 assertEquals(5.0f, mRectF.top, 0.0f); 62 assertEquals(10.0f, mRectF.right, 0.0f); 63 assertEquals(10.0f, mRectF.bottom, 0.0f); 64 } 65 66 @Test testSet1()67 public void testSet1() { 68 mRectF = new RectF(); 69 mRectF.set(1.0f, 2.0f, 3.0f, 4.0f); 70 assertEquals(1.0f, mRectF.left, 0.0f); 71 assertEquals(2.0f, mRectF.top, 0.0f); 72 assertEquals(3.0f, mRectF.right, 0.0f); 73 assertEquals(4.0f, mRectF.bottom, 0.0f); 74 } 75 76 @Test testSet2()77 public void testSet2() { 78 RectF rectF = new RectF(1.0f, 2.0f, 3.0f, 4.0f); 79 mRectF = new RectF(); 80 mRectF.set(rectF); 81 assertEquals(1.0f, mRectF.left, 0.0f); 82 assertEquals(2.0f, mRectF.top, 0.0f); 83 assertEquals(3.0f, mRectF.right, 0.0f); 84 assertEquals(4.0f, mRectF.bottom, 0.0f); 85 } 86 87 @Test testSet3()88 public void testSet3() { 89 Rect rect = new Rect(1, 2, 3, 4); 90 mRectF = new RectF(); 91 mRectF.set(rect); 92 assertEquals(1.0f, mRectF.left, 0.0f); 93 assertEquals(2.0f, mRectF.top, 0.0f); 94 assertEquals(3.0f, mRectF.right, 0.0f); 95 assertEquals(4.0f, mRectF.bottom, 0.0f); 96 } 97 98 @Test testIntersects1()99 public void testIntersects1() { 100 mRectF = new RectF(0, 0, 10, 10); 101 assertTrue(mRectF.intersects(5, 5, 15, 15)); 102 assertEquals(0.0f, mRectF.left, 0.0f); 103 assertEquals(0.0f, mRectF.top, 0.0f); 104 assertEquals(10.0f, mRectF.right, 0.0f); 105 assertEquals(10.0f, mRectF.bottom, 0.0f); 106 107 mRectF = new RectF(0, 0, 10, 10); 108 assertFalse(mRectF.intersects(15, 15, 25, 25)); 109 assertEquals(0.0f, mRectF.left, 0.0f); 110 assertEquals(0.0f, mRectF.top, 0.0f); 111 assertEquals(10.0f, mRectF.right, 0.0f); 112 assertEquals(10.0f, mRectF.bottom, 0.0f); 113 } 114 115 @Test testIntersects2()116 public void testIntersects2() { 117 RectF rectF1; 118 RectF rectF2; 119 120 rectF1 = new RectF(0, 0, 10, 10); 121 rectF2 = new RectF(5, 5, 15, 15); 122 assertTrue(RectF.intersects(rectF1, rectF2)); 123 124 rectF1 = new RectF(0, 0, 10, 10); 125 rectF2 = new RectF(15, 15, 25, 25); 126 assertFalse(RectF.intersects(rectF1, rectF2)); 127 } 128 129 @Test testIntersect1()130 public void testIntersect1() { 131 mRectF = new RectF(0, 0, 10, 10); 132 assertTrue(mRectF.intersect(5, 5, 15, 15)); 133 assertEquals(5.0f, mRectF.left, 0.0f); 134 assertEquals(5.0f, mRectF.top, 0.0f); 135 assertEquals(10.0f, mRectF.right, 0.0f); 136 assertEquals(10.0f, mRectF.bottom, 0.0f); 137 138 mRectF = new RectF(0, 0, 10, 10); 139 assertFalse(mRectF.intersect(15, 15, 25, 25)); 140 assertEquals(0.0f, mRectF.left, 0.0f); 141 assertEquals(0.0f, mRectF.top, 0.0f); 142 assertEquals(10.0f, mRectF.right, 0.0f); 143 assertEquals(10.0f, mRectF.bottom, 0.0f); 144 } 145 146 @Test testIntersect2()147 public void testIntersect2() { 148 RectF rectF; 149 150 mRectF = new RectF(0, 0, 10, 10); 151 rectF= new RectF(5, 5, 15, 15); 152 assertTrue(mRectF.intersect(rectF)); 153 assertEquals(5.0f, mRectF.left, 0.0f); 154 assertEquals(5.0f, mRectF.top, 0.0f); 155 assertEquals(10.0f, mRectF.right, 0.0f); 156 assertEquals(10.0f, mRectF.bottom, 0.0f); 157 158 mRectF = new RectF(0, 0, 10, 10); 159 rectF= new RectF(15, 15, 25, 25); 160 assertFalse(mRectF.intersect(rectF)); 161 assertEquals(0.0f, mRectF.left, 0.0f); 162 assertEquals(0.0f, mRectF.top, 0.0f); 163 assertEquals(10.0f, mRectF.right, 0.0f); 164 assertEquals(10.0f, mRectF.bottom, 0.0f); 165 } 166 167 @Test testUnion1()168 public void testUnion1() { 169 // Both rect1 and rect2 are not empty. 170 // 1. left < right, top < bottom 171 // this.left < this.right, this.top < this.bottom 172 mRectF = new RectF(0.0f, 0.0f, 1.0f, 1.0f); 173 mRectF.union(1.0f, 1.0f, 2.0f, 2.0f); 174 assertEquals(0.0f, mRectF.top, 0.0f); 175 assertEquals(0.0f, mRectF.left, 0.0f); 176 assertEquals(2.0f, mRectF.right, 0.0f); 177 assertEquals(2.0f, mRectF.bottom, 0.0f); 178 179 // 2. left < right, top < bottom 180 // this.left > this.right, this.top > this.bottom 181 // New rectangle will be set to the new arguments 182 mRectF = new RectF(1.0f, 1.0f, 0.0f, 0.0f); 183 mRectF.union(1.0f, 1.0f, 2.0f, 2.0f); 184 assertEquals(1.0f, mRectF.top, 0.0f); 185 assertEquals(1.0f, mRectF.left, 0.0f); 186 assertEquals(2.0f, mRectF.right, 0.0f); 187 assertEquals(2.0f, mRectF.bottom, 0.0f); 188 189 // 3. left > right, top > bottom 190 // this.left < this.right, this.top < this.bottom 191 // Nothing will be done. 192 mRectF = new RectF(0.0f, 0.0f, 1.0f, 1.0f); 193 mRectF.union(2.0f, 2.0f, 1.5f, 1.5f); 194 assertEquals(0.0f, mRectF.top, 0.0f); 195 assertEquals(0.0f, mRectF.left, 0.0f); 196 assertEquals(1.0f, mRectF.right, 0.0f); 197 assertEquals(1.0f, mRectF.bottom, 0.0f); 198 199 // rect1 is empty, update to rect2. 200 mRectF = new RectF(); 201 mRectF.union(1.0f, 1.0f, 2.0f, 2.0f); 202 assertEquals(1.0f, mRectF.top, 0.0f); 203 assertEquals(1.0f, mRectF.left, 0.0f); 204 assertEquals(2.0f, mRectF.right, 0.0f); 205 assertEquals(2.0f, mRectF.bottom, 0.0f); 206 207 // rect2 is empty, nothing changed. 208 mRectF = new RectF(0.0f, 0.0f, 1.0f, 1.0f); 209 mRectF.union(2.0f, 2.0f, 2.0f, 2.0f); 210 assertEquals(0.0f, mRectF.top, 0.0f); 211 assertEquals(0.0f, mRectF.left, 0.0f); 212 assertEquals(1.0f, mRectF.right, 0.0f); 213 assertEquals(1.0f, mRectF.bottom, 0.0f); 214 } 215 216 @Test testUnion2()217 public void testUnion2() { 218 RectF rectF; 219 220 // Both rect1 and rect2 are not empty. 221 // 1. left < right, top < bottom 222 // this.left < this.right, this.top < this.bottom 223 mRectF = new RectF(0.0f, 0.0f, 1.0f, 1.0f); 224 rectF = new RectF(1.0f, 1.0f, 2.0f, 2.0f); 225 mRectF.union(rectF); 226 assertEquals(0.0f, mRectF.top, 0.0f); 227 assertEquals(0.0f, mRectF.left, 0.0f); 228 assertEquals(2.0f, mRectF.right, 0.0f); 229 assertEquals(2.0f, mRectF.bottom, 0.0f); 230 231 // 2. left < right, top < bottom 232 // this.left > this.right, this.top > this.bottom 233 // New rectangle will be set to the new arguments 234 mRectF = new RectF(1.0f, 1.0f, 0.0f, 0.0f); 235 rectF = new RectF(1.0f, 1.0f, 2.0f, 2.0f); 236 mRectF.union(rectF); 237 assertEquals(1.0f, mRectF.top, 0.0f); 238 assertEquals(1.0f, mRectF.left, 0.0f); 239 assertEquals(2.0f, mRectF.right, 0.0f); 240 assertEquals(2.0f, mRectF.bottom, 0.0f); 241 242 // 3. left > right, top > bottom 243 // this.left < this.right, this.top < this.bottom 244 // Nothing will be done. 245 mRectF = new RectF(0.0f, 0.0f, 1.0f, 1.0f); 246 rectF = new RectF(2.0f, 2.0f, 1.5f, 1.5f); 247 mRectF.union(rectF); 248 assertEquals(0.0f, mRectF.top, 0.0f); 249 assertEquals(0.0f, mRectF.left, 0.0f); 250 assertEquals(1.0f, mRectF.right, 0.0f); 251 assertEquals(1.0f, mRectF.bottom, 0.0f); 252 253 // rect1 is empty, update to rect2. 254 mRectF = new RectF(0.0f, 0.0f, 0.0f, 0.0f); 255 rectF = new RectF(1.0f, 1.0f, 2.0f, 2.0f); 256 mRectF.union(rectF); 257 assertEquals(1.0f, mRectF.top, 0.0f); 258 assertEquals(1.0f, mRectF.left, 0.0f); 259 assertEquals(2.0f, mRectF.right, 0.0f); 260 assertEquals(2.0f, mRectF.bottom, 0.0f); 261 262 // rect2 is empty, nothing changed. 263 mRectF = new RectF(0.0f, 0.0f, 1.0f, 1.0f); 264 rectF = new RectF(2.0f, 2.0f, 2.0f, 2.0f); 265 mRectF.union(rectF); 266 assertEquals(0.0f, mRectF.top, 0.0f); 267 assertEquals(0.0f, mRectF.left, 0.0f); 268 assertEquals(1.0f, mRectF.right, 0.0f); 269 assertEquals(1.0f, mRectF.bottom, 0.0f); 270 } 271 272 @Test testUnion3()273 public void testUnion3() { 274 // rect1 is not empty (x > right, y > bottom). 275 mRectF = new RectF(0.0f, 0.0f, 1.0f, 1.0f); 276 mRectF.union(2.0f, 2.0f); 277 assertEquals(0.0f, mRectF.top, 0.0f); 278 assertEquals(0.0f, mRectF.left, 0.0f); 279 assertEquals(2.0f, mRectF.right, 0.0f); 280 assertEquals(2.0f, mRectF.bottom, 0.0f); 281 282 // rect1 is not empty (x < left, y < top). 283 mRectF = new RectF(1.0f, 1.0f, 2.0f, 2.0f); 284 mRectF.union(0.0f, 0.0f); 285 assertEquals(0.0f, mRectF.top, 0.0f); 286 assertEquals(0.0f, mRectF.left, 0.0f); 287 assertEquals(2.0f, mRectF.right, 0.0f); 288 assertEquals(2.0f, mRectF.bottom, 0.0f); 289 290 // rect1 is not empty(point is inside of the rectangle). 291 mRectF = new RectF(1.0f, 1.0f, 2.0f, 2.0f); 292 mRectF.union(1.5f, 1.5f); 293 assertEquals(1.0f, mRectF.top, 0.0f); 294 assertEquals(1.0f, mRectF.left, 0.0f); 295 assertEquals(2.0f, mRectF.right, 0.0f); 296 assertEquals(2.0f, mRectF.bottom, 0.0f); 297 298 // rect1 is empty. 299 mRectF = new RectF(); 300 mRectF.union(2.0f, 2.0f); 301 assertEquals(0.0f, mRectF.top, 0.0f); 302 assertEquals(0.0f, mRectF.left, 0.0f); 303 assertEquals(2.0f, mRectF.right, 0.0f); 304 assertEquals(2.0f, mRectF.bottom, 0.0f); 305 } 306 307 @Test testContains1()308 public void testContains1() { 309 mRectF = new RectF(1.0f, 1.0f, 20.0f, 20.0f); 310 assertFalse(mRectF.contains(0.9f, 0.9f)); 311 assertTrue(mRectF.contains(1.0f, 1.0f)); 312 assertTrue(mRectF.contains(19.9f, 19.9f)); 313 assertFalse(mRectF.contains(20.0f, 20.0f)); 314 } 315 316 @Test testContains2()317 public void testContains2() { 318 mRectF = new RectF(1.0f, 1.0f, 20.0f, 20.0f); 319 assertTrue(mRectF.contains(1.0f, 1.0f, 20.0f, 20.0f)); 320 assertTrue(mRectF.contains(2.0f, 2.0f, 19.0f, 19.0f)); 321 assertFalse(mRectF.contains(21.0f, 21.0f, 22.0f, 22.0f)); 322 assertFalse(mRectF.contains(0.0f, 0.0f, 19.0f, 19.0f)); 323 } 324 325 @Test testContains3()326 public void testContains3() { 327 RectF rectF; 328 mRectF = new RectF(1.0f, 1.0f, 20.0f, 20.0f); 329 rectF = new RectF(1.0f, 1.0f, 20.0f, 20.0f); 330 assertTrue(mRectF.contains(rectF)); 331 rectF = new RectF(2.0f, 2.0f, 19.0f, 19.0f); 332 assertTrue(mRectF.contains(rectF)); 333 rectF = new RectF(21.0f, 21.0f, 22.0f, 22.0f); 334 assertFalse(mRectF.contains(rectF)); 335 rectF = new RectF(0.0f, 0.0f, 19.0f, 19.0f); 336 assertFalse(mRectF.contains(rectF)); 337 } 338 339 @Test testOffset()340 public void testOffset() { 341 mRectF = new RectF(5, 5, 10, 10); 342 mRectF.offset(1.0f, 1.0f); 343 assertEquals(6.0f, mRectF.left, 0.0f); 344 assertEquals(6.0f, mRectF.top, 0.0f); 345 assertEquals(11.0f, mRectF.right, 0.0f); 346 assertEquals(11.0f, mRectF.bottom, 0.0f); 347 } 348 349 @Test testInset()350 public void testInset() { 351 mRectF = new RectF(5.0f, 5.0f, 10.0f, 10.0f); 352 mRectF.inset(1.0f, 1.0f); 353 assertEquals(6.0f, mRectF.left, 0.0f); 354 assertEquals(6.0f, mRectF.top, 0.0f); 355 assertEquals(9.0f, mRectF.right, 0.0f); 356 assertEquals(9.0f, mRectF.bottom, 0.0f); 357 358 mRectF = new RectF(5.0f, 5.0f, 10.0f, 10.0f); 359 mRectF.inset(-1.0f, -1.0f); 360 assertEquals(4.0f, mRectF.left, 0.0f); 361 assertEquals(4.0f, mRectF.top, 0.0f); 362 assertEquals(11.0f, mRectF.right, 0.0f); 363 assertEquals(11.0f, mRectF.bottom, 0.0f); 364 } 365 366 @Test testHeight()367 public void testHeight() { 368 mRectF = new RectF(1.0f, 1.0f, 20.5f, 20.5f); 369 assertEquals(19.5f, mRectF.height(), 0.0f); 370 } 371 372 @Test testWidth()373 public void testWidth() { 374 mRectF = new RectF(1.0f, 1.0f, 20.5f, 20.5f); 375 assertEquals(19.5f, mRectF.width(), 0.0f); 376 } 377 378 @Test testOffsetTo()379 public void testOffsetTo() { 380 mRectF = new RectF(5, 5, 10, 10); 381 mRectF.offsetTo(1.0f, 1.0f); 382 assertEquals(1.0f, mRectF.left, 0.0f); 383 assertEquals(1.0f, mRectF.top, 0.0f); 384 assertEquals(6.0f, mRectF.right, 0.0f); 385 assertEquals(6.0f, mRectF.bottom, 0.0f); 386 } 387 388 @Test testSetEmpty()389 public void testSetEmpty() { 390 // Before setEmpty() 391 mRectF = new RectF(1, 2, 3, 4); 392 assertEquals(1.0f, mRectF.left, 0.0f); 393 assertEquals(2.0f, mRectF.top, 0.0f); 394 assertEquals(3.0f, mRectF.right, 0.0f); 395 assertEquals(4.0f, mRectF.bottom, 0.0f); 396 397 // After setEmpty() 398 mRectF.setEmpty(); 399 assertEquals(0.0f, mRectF.left, 0.0f); 400 assertEquals(0.0f, mRectF.top, 0.0f); 401 assertEquals(0.0f, mRectF.right, 0.0f); 402 assertEquals(0.0f, mRectF.bottom, 0.0f); 403 } 404 405 @Test testIsEmpty()406 public void testIsEmpty() { 407 mRectF = new RectF(); 408 assertTrue(mRectF.isEmpty()); 409 mRectF = new RectF(1.0f, 1.0f, 1.0f, 1.0f); 410 assertTrue(mRectF.isEmpty()); 411 mRectF = new RectF(0.0f, 1.0f, 2.0f, 1.0f); 412 assertTrue(mRectF.isEmpty()); 413 mRectF = new RectF(1.0f, 1.0f, 20.0f, 20.0f); 414 assertFalse(mRectF.isEmpty()); 415 } 416 417 @Test testCenterX()418 public void testCenterX() { 419 mRectF = new RectF(10.0f, 10.0f, 20.0f, 20.0f); 420 assertEquals(15.0f, mRectF.centerX(), 0.0f); 421 mRectF = new RectF(10.5f, 10.0f, 20.0f, 20.0f); 422 assertEquals(15.25f, mRectF.centerX(), 0.0f); 423 mRectF = new RectF(10.4f, 10.0f, 20.0f, 20.0f); 424 assertEquals(15.2f, mRectF.centerX(), 0.0f); 425 } 426 427 @Test testCenterY()428 public void testCenterY() { 429 mRectF = new RectF(10.0f, 10.0f, 20.0f, 20.0f); 430 assertEquals(15.0f, mRectF.centerY(), 0.0f); 431 mRectF = new RectF(10.0f, 10.5f, 20.0f, 20.0f); 432 assertEquals(15.25f, mRectF.centerY(), 0.0f); 433 mRectF = new RectF(10.0f, 10.4f, 20.0f, 20.0f); 434 assertEquals(15.2f, mRectF.centerY(), 0.0f); 435 } 436 437 @Test testToString()438 public void testToString() { 439 mRectF = new RectF(10.0f, 20.0f, 30.0f, 40.0f); 440 assertEquals("RectF(10.0, 20.0, 30.0, 40.0)", mRectF.toString()); 441 } 442 443 @Test testToShortString()444 public void testToShortString() { 445 mRectF = new RectF(10.0f, 20.0f, 30.0f, 40.0f); 446 assertEquals("[10.0,20.0][30.0,40.0]", mRectF.toShortString()); 447 } 448 449 @Test testSetIntersect()450 public void testSetIntersect() { 451 RectF rectF1 = new RectF(0, 0, 10, 10); 452 RectF rectF2 = new RectF(5, 5, 15, 15); 453 454 // Empty RectF 455 mRectF = new RectF(); 456 assertTrue(mRectF.setIntersect(rectF1, rectF2)); 457 assertEquals(5.0f, mRectF.left, 0.0f); 458 assertEquals(5.0f, mRectF.top, 0.0f); 459 assertEquals(10.0f, mRectF.right, 0.0f); 460 assertEquals(10.0f, mRectF.bottom, 0.0f); 461 462 // Not Empty RectF 463 mRectF = new RectF(0, 0, 15, 15); 464 assertTrue(mRectF.setIntersect(rectF1, rectF2)); 465 assertEquals(5.0f, mRectF.left, 0.0f); 466 assertEquals(5.0f, mRectF.top, 0.0f); 467 assertEquals(10.0f, mRectF.right, 0.0f); 468 assertEquals(10.0f, mRectF.bottom, 0.0f); 469 } 470 471 @Test testRoundOut()472 public void testRoundOut() { 473 Rect rect = new Rect(); 474 mRectF = new RectF(1.2f, 1.8f, 5.2f, 5.8f); 475 mRectF.roundOut(rect); 476 assertEquals(1, rect.left, 0.0f); 477 assertEquals(1, rect.top, 0.0f); 478 assertEquals(6, rect.right, 0.0f); 479 assertEquals(6, rect.bottom, 0.0f); 480 } 481 482 @Test testRound()483 public void testRound() { 484 Rect rect = new Rect(); 485 mRectF = new RectF(1.2f, 1.8f, 5.2f, 5.8f); 486 mRectF.round(rect); 487 assertEquals(1, rect.left, 0.0f); 488 assertEquals(2, rect.top, 0.0f); 489 assertEquals(5, rect.right, 0.0f); 490 assertEquals(6, rect.bottom, 0.0f); 491 } 492 493 @Test testWriteReadParcel()494 public void testWriteReadParcel() { 495 RectF rectOut = new RectF(0.0f,0.0f,10.0f,10.0f); 496 Parcel out = Parcel.obtain(); 497 rectOut.writeToParcel(out, 0); 498 out.setDataPosition(0); 499 RectF rectIn = new RectF(1.0f,1.0f,1.0f,1.0f); 500 rectIn.readFromParcel(out); 501 assertEquals(rectOut.left, rectIn.left, 0.0f); 502 assertEquals(rectOut.bottom, rectIn.bottom, 0.0f); 503 assertEquals(rectOut.right, rectIn.right, 0.0f); 504 assertEquals(rectOut.top, rectIn.top, 0.0f); 505 assertNotNull(rectIn.toString()); 506 507 assertEquals(0, rectIn.describeContents()); 508 509 out.recycle(); 510 } 511 } 512