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