1 /*
2  * Copyright 2022 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 androidx.window.core
18 
19 import org.junit.Assert.assertEquals
20 import org.junit.Assert.assertNotEquals
21 import org.junit.Assert.assertTrue
22 import org.junit.Test
23 
24 /**
25  * Tests for [Bounds] to check the public API and that some methods match [android.graphics.Rect].
26  */
27 class BoundsTest {
28 
29     @Test
sameBounds_equalsnull30     fun sameBounds_equals() {
31         val first = Bounds(0, 0, dimension, doubleDimension)
32         val second = Bounds(0, 0, dimension, doubleDimension)
33         assertEquals(first, second)
34         assertEquals(first.hashCode(), second.hashCode())
35     }
36 
37     @Test
differentBounds_equalsnull38     fun differentBounds_equals() {
39         val base = Bounds(0, 0, dimension, doubleDimension)
40         val diffLeft = Bounds(dimension, 0, dimension, doubleDimension)
41         val diffTop = Bounds(0, dimension, dimension, doubleDimension)
42         val diffRight = Bounds(0, 0, doubleDimension, doubleDimension)
43         val diffBottom = Bounds(0, 0, dimension, dimension)
44 
45         assertNotEquals(base, diffLeft)
46         assertNotEquals(base, diffTop)
47         assertNotEquals(base, diffRight)
48         assertNotEquals(base, diffBottom)
49 
50         assertNotEquals(base.hashCode(), diffLeft.hashCode())
51         assertNotEquals(base.hashCode(), diffTop.hashCode())
52         assertNotEquals(base.hashCode(), diffRight.hashCode())
53         assertNotEquals(base.hashCode(), diffBottom.hashCode())
54     }
55 
56     @Test
testWidthCalculationnull57     fun testWidthCalculation() {
58         val bounds = Bounds(0, 0, dimension, doubleDimension)
59 
60         assertEquals(dimension, bounds.width)
61     }
62 
63     @Test
testHeightCalculationnull64     fun testHeightCalculation() {
65         val bounds = Bounds(0, 0, doubleDimension, dimension)
66 
67         assertEquals(dimension, bounds.height)
68     }
69 
70     @Test
zeroBounds_isZeronull71     fun zeroBounds_isZero() {
72         val zero = Bounds(dimension, dimension, dimension, dimension)
73         assertTrue(zero.isZero)
74     }
75 
76     @Test
zeroWidth_isEmptynull77     fun zeroWidth_isEmpty() {
78         val zeroWidth = Bounds(dimension, dimension, dimension, doubleDimension)
79         assertTrue(zeroWidth.isEmpty)
80     }
81 
82     @Test
zeroHeight_isEmptynull83     fun zeroHeight_isEmpty() {
84         val zeroHeight = Bounds(dimension, dimension, doubleDimension, dimension)
85         assertTrue(zeroHeight.isEmpty)
86     }
87 
88     @Test(expected = IllegalArgumentException::class)
negativeWidth_throwsExceptionnull89     fun negativeWidth_throwsException() {
90         Bounds(0, 0, -dimension, 0)
91     }
92 
93     @Test(expected = IllegalArgumentException::class)
negativeHeight_throwsExceptionnull94     fun negativeHeight_throwsException() {
95         Bounds(0, 0, 0, -dimension)
96     }
97 
98     companion object {
99         const val dimension = 100
100         const val doubleDimension = 200
101     }
102 }
103