[go: nahoru, domu]

Move tests related to WithConstraints to WithConstraintsTest file

Test: tests passed
Change-Id: I25e4d856f2af9351b63be9b67a0a577f8cf12c9a
diff --git a/ui/ui-framework/src/androidTest/java/androidx/ui/core/test/AndroidLayoutDrawTest.kt b/ui/ui-framework/src/androidTest/java/androidx/ui/core/test/AndroidLayoutDrawTest.kt
index 8a583b6..ab29ddc 100644
--- a/ui/ui-framework/src/androidTest/java/androidx/ui/core/test/AndroidLayoutDrawTest.kt
+++ b/ui/ui-framework/src/androidTest/java/androidx/ui/core/test/AndroidLayoutDrawTest.kt
@@ -53,7 +53,6 @@
 import androidx.ui.core.Ref
 import androidx.ui.core.RepaintBoundary
 import androidx.ui.core.VerticalAlignmentLine
-import androidx.ui.core.WithConstraints
 import androidx.ui.core.coerceAtLeast
 import androidx.ui.core.coerceIn
 import androidx.ui.core.ipx
@@ -364,102 +363,6 @@
         validateSquareColors(outerColor = green, innerColor = white, size = 20)
     }
 
-    @Test
-    fun withConstraintsTest() {
-        val size = 20.ipx
-
-        val countDownLatch = CountDownLatch(1)
-        val topConstraints = Ref<Constraints>()
-        val paddedConstraints = Ref<Constraints>()
-        val firstChildConstraints = Ref<Constraints>()
-        val secondChildConstraints = Ref<Constraints>()
-        activityTestRule.runOnUiThreadIR {
-            activity.setContentInFrameLayout {
-                WithConstraints { constraints ->
-                    topConstraints.value = constraints
-                    Padding(size = size) {
-                        WithConstraints { constraints ->
-                            paddedConstraints.value = constraints
-                            Layout(measureBlock = { _, childConstraints ->
-                                firstChildConstraints.value = childConstraints
-                                layout(size, size) { }
-                            }, children = { })
-                            Layout(measureBlock = { _, chilConstraints ->
-                                secondChildConstraints.value = chilConstraints
-                                layout(size, size) { }
-                            }, children = { })
-                            Draw { _, _ ->
-                                countDownLatch.countDown()
-                            }
-                        }
-                    }
-                }
-            }
-        }
-        assertTrue(countDownLatch.await(1, TimeUnit.SECONDS))
-
-        val expectedPaddedConstraints = Constraints(
-            0.ipx,
-            topConstraints.value!!.maxWidth - size * 2,
-            0.ipx,
-            topConstraints.value!!.maxHeight - size * 2
-        )
-        assertEquals(expectedPaddedConstraints, paddedConstraints.value)
-        assertEquals(paddedConstraints.value, firstChildConstraints.value)
-        assertEquals(paddedConstraints.value, secondChildConstraints.value)
-    }
-
-    @Suppress("UNUSED_ANONYMOUS_PARAMETER")
-    @SdkSuppress(minSdkVersion = Build.VERSION_CODES.O)
-    @Test
-    fun withConstraints_layoutListener() {
-        val green = Color.Green
-        val white = Color.White
-        val model = SquareModel(size = 20.ipx, outerColor = green, innerColor = white)
-
-        activityTestRule.runOnUiThreadIR {
-            activity.setContentInFrameLayout {
-                WithConstraints { constraints ->
-                    Layout(children = {
-                        Draw { canvas, parentSize ->
-                            val paint = Paint()
-                            paint.color = model.outerColor
-                            canvas.drawRect(parentSize.toRect(), paint)
-                        }
-                        Layout(children = {
-                            Draw { canvas, parentSize ->
-                                drawLatch.countDown()
-                                val paint = Paint()
-                                paint.color = model.innerColor
-                                canvas.drawRect(parentSize.toRect(), paint)
-                            }
-                        }) { measurables, constraints2 ->
-                            layout(model.size, model.size) {}
-                        }
-                    }) { measurables, constraints3 ->
-                        val placeable = measurables[0].measure(
-                            Constraints.tightConstraints(
-                                model.size,
-                                model.size
-                            )
-                        )
-                        layout(model.size * 3, model.size * 3) {
-                            placeable.place(model.size, model.size)
-                        }
-                    }
-                }
-            }
-        }
-        validateSquareColors(outerColor = green, innerColor = white, size = 20)
-
-        drawLatch = CountDownLatch(1)
-        activityTestRule.runOnUiThreadIR {
-            model.size = 10.ipx
-        }
-
-        validateSquareColors(outerColor = green, innerColor = white, size = 10)
-    }
-
     // Tests that calling measure multiple times on the same Measurable causes an exception
     @Test
     fun multipleMeasureCall() {
@@ -1512,18 +1415,6 @@
         assertEquals(8, childLayouts)
     }
 
-    /**
-     * WithConstraints will cause a requestLayout during layout in some circumstances.
-     * The test here is the minimal example from a bug.
-     */
-    @SdkSuppress(minSdkVersion = Build.VERSION_CODES.O)
-    @Test
-    fun requestLayoutDuringLayout() {
-        composeSquaresWithConstraints()
-
-        validateSquareColors(outerColor = Color.Yellow, innerColor = Color.Red, size = 10)
-    }
-
     @Test
     fun testLayoutBeforeDraw_forRecomposingNodesNotAffectingRootSize() {
         val model = OffsetModel(0.ipx)
@@ -1882,29 +1773,6 @@
         }
     }
 
-    private fun composeSquaresWithConstraints() {
-        val offset = OffsetModel(0.ipx)
-        activityTestRule.runOnUiThreadIR {
-            activity.setContentInFrameLayout {
-                Draw { canvas, parentSize ->
-                    val paint = Paint()
-                    paint.color = Color.Yellow
-                    canvas.drawRect(parentSize.toRect(), paint)
-                    drawLatch.countDown()
-                }
-                Scroller(
-                     position, _ ->
-                        offset.offset = position
-                    },
-                    offset = offset
-                ) {
-                    // Need to pass some param here to a separate function or else it works fine
-                    TestLayout(5)
-                }
-            }
-        }
-    }
-
     private fun validateSquareColors(
         outerColor: Color,
         innerColor: Color,
@@ -2139,35 +2007,7 @@
 }
 
 @Composable
-private fun TestLayout(@Suppress("UNUSED_PARAMETER") someInput: Int) {
-    Layout(children = {
-        WithConstraints {
-            NeedsOtherMeasurementComposable(10.ipx)
-        }
-    }) { measurables, constraints ->
-        val withConstraintsPlaceable = measurables[0].measure(constraints)
-
-        layout(30.ipx, 30.ipx) {
-            withConstraintsPlaceable.place(10.ipx, 10.ipx)
-        }
-    }
-}
-
-@Composable
-private fun NeedsOtherMeasurementComposable(foo: IntPx) {
-    Layout(children = {
-        Draw { canvas, parentSize ->
-            val paint = Paint()
-            paint.color = Color.Red
-            canvas.drawRect(parentSize.toRect(), paint)
-        }
-    }) { _, _ ->
-        layout(foo, foo) { }
-    }
-}
-
-@Composable
-private fun Scroller(
+fun Scroller(
     onScrollPositionChanged: (position: IntPx, maxPosition: IntPx) -> Unit,
     offset: OffsetModel,
     child: @Composable() () -> Unit
diff --git a/ui/ui-framework/src/androidTest/java/androidx/ui/core/test/WithConstraintsTest.kt b/ui/ui-framework/src/androidTest/java/androidx/ui/core/test/WithConstraintsTest.kt
index d1b8b95..dc557c0 100644
--- a/ui/ui-framework/src/androidTest/java/androidx/ui/core/test/WithConstraintsTest.kt
+++ b/ui/ui-framework/src/androidTest/java/androidx/ui/core/test/WithConstraintsTest.kt
@@ -16,15 +16,27 @@
 
 package androidx.ui.core.test
 
+import android.graphics.Bitmap
+import android.os.Build
+import androidx.compose.Composable
+import androidx.test.filters.SdkSuppress
 import androidx.test.filters.SmallTest
 import androidx.test.rule.ActivityTestRule
+import androidx.ui.core.Constraints
+import androidx.ui.core.Draw
+import androidx.ui.core.IntPx
 import androidx.ui.core.Layout
+import androidx.ui.core.Ref
 import androidx.ui.core.WithConstraints
 import androidx.ui.core.ipx
 import androidx.ui.core.px
 import androidx.ui.core.setContent
+import androidx.ui.core.toRect
 import androidx.ui.framework.test.TestActivity
+import androidx.ui.graphics.Color
+import androidx.ui.graphics.Paint
 import androidx.ui.graphics.vector.DrawVector
+import org.junit.Assert.assertEquals
 import org.junit.Assert.assertTrue
 import org.junit.Before
 import org.junit.Rule
@@ -43,11 +55,149 @@
         TestActivity::class.java
     )
     private lateinit var activity: TestActivity
+    private lateinit var drawLatch: CountDownLatch
 
     @Before
     fun setup() {
         activity = rule.activity
         activity.hasFocusLatch.await(5, TimeUnit.SECONDS)
+        drawLatch = CountDownLatch(1)
+    }
+
+    @Test
+    fun withConstraintsTest() {
+        val size = 20.ipx
+
+        val countDownLatch = CountDownLatch(1)
+        val topConstraints = Ref<Constraints>()
+        val paddedConstraints = Ref<Constraints>()
+        val firstChildConstraints = Ref<Constraints>()
+        val secondChildConstraints = Ref<Constraints>()
+        rule.runOnUiThreadIR {
+            activity.setContentInFrameLayout {
+                WithConstraints { constraints ->
+                    topConstraints.value = constraints
+                    Padding(size = size) {
+                        WithConstraints { constraints ->
+                            paddedConstraints.value = constraints
+                            Layout(measureBlock = { _, childConstraints ->
+                                firstChildConstraints.value = childConstraints
+                                layout(size, size) { }
+                            }, children = { })
+                            Layout(measureBlock = { _, chilConstraints ->
+                                secondChildConstraints.value = chilConstraints
+                                layout(size, size) { }
+                            }, children = { })
+                            Draw { _, _ ->
+                                countDownLatch.countDown()
+                            }
+                        }
+                    }
+                }
+            }
+        }
+        assertTrue(countDownLatch.await(1, TimeUnit.SECONDS))
+
+        val expectedPaddedConstraints = Constraints(
+            0.ipx,
+            topConstraints.value!!.maxWidth - size * 2,
+            0.ipx,
+            topConstraints.value!!.maxHeight - size * 2
+        )
+        assertEquals(expectedPaddedConstraints, paddedConstraints.value)
+        assertEquals(paddedConstraints.value, firstChildConstraints.value)
+        assertEquals(paddedConstraints.value, secondChildConstraints.value)
+    }
+
+    @Suppress("UNUSED_ANONYMOUS_PARAMETER")
+    @SdkSuppress(minSdkVersion = Build.VERSION_CODES.O)
+    @Test
+    fun withConstraints_layoutListener() {
+        val green = Color.Green
+        val white = Color.White
+        val model = SquareModel(size = 20.ipx, outerColor = green, innerColor = white)
+
+        rule.runOnUiThreadIR {
+            activity.setContentInFrameLayout {
+                WithConstraints { constraints ->
+                    Layout(children = {
+                        Draw { canvas, parentSize ->
+                            val paint = Paint()
+                            paint.color = model.outerColor
+                            canvas.drawRect(parentSize.toRect(), paint)
+                        }
+                        Layout(children = {
+                            Draw { canvas, parentSize ->
+                                drawLatch.countDown()
+                                val paint = Paint()
+                                paint.color = model.innerColor
+                                canvas.drawRect(parentSize.toRect(), paint)
+                            }
+                        }) { measurables, constraints2 ->
+                            layout(model.size, model.size) {}
+                        }
+                    }) { measurables, constraints3 ->
+                        val placeable = measurables[0].measure(
+                            Constraints.tightConstraints(
+                                model.size,
+                                model.size
+                            )
+                        )
+                        layout(model.size * 3, model.size * 3) {
+                            placeable.place(model.size, model.size)
+                        }
+                    }
+                }
+            }
+        }
+        takeScreenShot(60).apply {
+            assertRect(color = white, size = 20)
+            assertRect(color = green, holeSize = 20)
+        }
+
+        drawLatch = CountDownLatch(1)
+        rule.runOnUiThreadIR {
+            model.size = 10.ipx
+        }
+
+        takeScreenShot(30).apply {
+            assertRect(color = white, size = 10)
+            assertRect(color = green, holeSize = 10)
+        }
+    }
+
+    /**
+     * WithConstraints will cause a requestLayout during layout in some circumstances.
+     * The test here is the minimal example from a bug.
+     */
+    @SdkSuppress(minSdkVersion = Build.VERSION_CODES.O)
+    @Test
+    fun requestLayoutDuringLayout() {
+        val offset = OffsetModel(0.ipx)
+        rule.runOnUiThreadIR {
+            activity.setContentInFrameLayout {
+                Draw { canvas, parentSize ->
+                    val paint = Paint()
+                    paint.color = Color.Yellow
+                    canvas.drawRect(parentSize.toRect(), paint)
+                    drawLatch.countDown()
+                }
+                Scroller(
+                     position, _ ->
+                        offset.offset = position
+                    },
+                    offset = offset
+                ) {
+                    // Need to pass some param here to a separate function or else it works fine
+                    TestLayout(5)
+                }
+            }
+        }
+
+        takeScreenShot(30).apply {
+            assertRect(color = Color.Red, size = 10)
+            assertRect(color = Color.Yellow, holeSize = 10)
+        }
     }
 
     @Test
@@ -79,4 +229,40 @@
         rule.runOnUiThread { model.value++ }
         assertTrue(latch.await(1, TimeUnit.SECONDS))
     }
+
+    private fun takeScreenShot(size: Int): Bitmap {
+        assertTrue(drawLatch.await(1, TimeUnit.SECONDS))
+        val bitmap = rule.waitAndScreenShot()
+        assertEquals(size, bitmap.width)
+        assertEquals(size, bitmap.height)
+        return bitmap
+    }
+}
+
+@Composable
+private fun TestLayout(@Suppress("UNUSED_PARAMETER") someInput: Int) {
+    Layout(children = {
+        WithConstraints {
+            NeedsOtherMeasurementComposable(10.ipx)
+        }
+    }) { measurables, constraints ->
+        val withConstraintsPlaceable = measurables[0].measure(constraints)
+
+        layout(30.ipx, 30.ipx) {
+            withConstraintsPlaceable.place(10.ipx, 10.ipx)
+        }
+    }
+}
+
+@Composable
+private fun NeedsOtherMeasurementComposable(foo: IntPx) {
+    Layout(children = {
+        Draw { canvas, parentSize ->
+            val paint = Paint()
+            paint.color = Color.Red
+            canvas.drawRect(parentSize.toRect(), paint)
+        }
+    }) { _, _ ->
+        layout(foo, foo) { }
+    }
 }