[go: nahoru, domu]

Improve code coverage for Bounds

Add tests for Bounds.
Enforce non-negative dimensions for Bounds

Bug: 238767712
Test: ./gradlew window:window:test
Test: Run code coverage and see that Bounds has coverage
Change-Id: I102931e2f3a971174b974d7d83919ce0336a71e3
diff --git a/window/window/src/main/java/androidx/window/core/Bounds.kt b/window/window/src/main/java/androidx/window/core/Bounds.kt
index 94f2137..4d67a07 100644
--- a/window/window/src/main/java/androidx/window/core/Bounds.kt
+++ b/window/window/src/main/java/androidx/window/core/Bounds.kt
@@ -33,35 +33,44 @@
     public val right: Int,
     public val bottom: Int
 ) {
-    public constructor(rect: Rect) : this(rect.left, rect.top, rect.right, rect.bottom)
+    constructor(rect: Rect) : this(rect.left, rect.top, rect.right, rect.bottom)
+
+    init {
+        require(left <= right) {
+            "Left must be less than or equal to right, left: $left, right: $right"
+        }
+        require(top <= bottom) {
+            "top must be less than or equal to bottom, top: $top, bottom: $bottom"
+        }
+    }
 
     /**
      * Return the [Rect] representation of the bounds
      */
-    public fun toRect(): Rect = Rect(left, top, right, bottom)
+    fun toRect(): Rect = Rect(left, top, right, bottom)
 
     /**
      * The width of the bounds, may be negative.
      */
-    public val width: Int
+    val width: Int
         get() = right - left
 
     /**
      * The height of the bounds, may be negative.
      */
-    public val height: Int
+    val height: Int
         get() = bottom - top
 
     /**
      * Determines if the bounds has empty area.
      */
-    public val isEmpty: Boolean
+    val isEmpty: Boolean
         get() = height == 0 || width == 0
 
     /**
      * Returns if the dimensions of the bounds is 0.
      */
-    public val isZero: Boolean
+    val isZero: Boolean
         get() = height == 0 && width == 0
 
     override fun toString(): String {
diff --git a/window/window/src/test/java/androidx/window/core/BoundsTest.kt b/window/window/src/test/java/androidx/window/core/BoundsTest.kt
new file mode 100644
index 0000000..0d19d4a
--- /dev/null
+++ b/window/window/src/test/java/androidx/window/core/BoundsTest.kt
@@ -0,0 +1,103 @@
+/*
+ * Copyright 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package androidx.window.core
+
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertNotEquals
+import org.junit.Assert.assertTrue
+import org.junit.Test
+
+/**
+ * Tests for [Bounds] to check the public API and that some methods
+ * match [android.graphics.Rect].
+ */
+class BoundsTest {
+
+    @Test
+    fun sameBounds_equals() {
+        val first = Bounds(0, 0, dimension, doubleDimension)
+        val second = Bounds(0, 0, dimension, doubleDimension)
+        assertEquals(first, second)
+        assertEquals(first.hashCode(), second.hashCode())
+    }
+
+    @Test
+    fun differentBounds_equals() {
+        val base = Bounds(0, 0, dimension, doubleDimension)
+        val diffLeft = Bounds(dimension, 0, dimension, doubleDimension)
+        val diffTop = Bounds(0, dimension, dimension, doubleDimension)
+        val diffRight = Bounds(0, 0, doubleDimension, doubleDimension)
+        val diffBottom = Bounds(0, 0, dimension, dimension)
+
+        assertNotEquals(base, diffLeft)
+        assertNotEquals(base, diffTop)
+        assertNotEquals(base, diffRight)
+        assertNotEquals(base, diffBottom)
+
+        assertNotEquals(base.hashCode(), diffLeft.hashCode())
+        assertNotEquals(base.hashCode(), diffTop.hashCode())
+        assertNotEquals(base.hashCode(), diffRight.hashCode())
+        assertNotEquals(base.hashCode(), diffBottom.hashCode())
+    }
+
+    @Test
+    fun testWidthCalculation() {
+        val bounds = Bounds(0, 0, dimension, doubleDimension)
+
+        assertEquals(dimension, bounds.width)
+    }
+
+    @Test
+    fun testHeightCalculation() {
+        val bounds = Bounds(0, 0, doubleDimension, dimension)
+
+        assertEquals(dimension, bounds.height)
+    }
+
+    @Test
+    fun zeroBounds_isZero() {
+        val zero = Bounds(dimension, dimension, dimension, dimension)
+        assertTrue(zero.isZero)
+    }
+
+    @Test
+    fun zeroWidth_isEmpty() {
+        val zeroWidth = Bounds(dimension, dimension, dimension, doubleDimension)
+        assertTrue(zeroWidth.isEmpty)
+    }
+
+    @Test
+    fun zeroHeight_isEmpty() {
+        val zeroHeight = Bounds(dimension, dimension, doubleDimension, dimension)
+        assertTrue(zeroHeight.isEmpty)
+    }
+
+    @Test(expected = IllegalArgumentException::class)
+    fun negativeWidth_throwsException() {
+        Bounds(0, 0, -dimension, 0)
+    }
+
+    @Test(expected = IllegalArgumentException::class)
+    fun negativeHeight_throwsException() {
+        Bounds(0, 0, 0, -dimension)
+    }
+
+    companion object {
+        const val dimension = 100
+        const val doubleDimension = 200
+    }
+}
\ No newline at end of file