Final migration of material size tests.
Also removes the old sizes testing infra.
It turned out that having a utility that sets a content and returns it
for measuring in a safe way (by using parent) is quite less error prone
and requires less code so I have kept that patten but hooked it to the
new APIs.
I couldn't migrate for instance text position tests as that would
require access to unmerged semantics. But I still at least fixed the
positions used, so it won't cause issues on cutouts anymore.
Bug: b/157657482
Test: N/A
Relnote: Removed obsolete size testing APIs.
Change-Id: Iba0a086e8c88cf44684cba56766792614201ba30
diff --git a/ui/ui-foundation/src/androidTest/java/androidx/ui/foundation/CanvasTest.kt b/ui/ui-foundation/src/androidTest/java/androidx/ui/foundation/CanvasTest.kt
index 7fb6c89..670b858 100644
--- a/ui/ui-foundation/src/androidTest/java/androidx/ui/foundation/CanvasTest.kt
+++ b/ui/ui-foundation/src/androidTest/java/androidx/ui/foundation/CanvasTest.kt
@@ -17,40 +17,30 @@
package androidx.ui.foundation
import android.os.Build
-import androidx.compose.Composable
import androidx.test.filters.MediumTest
import androidx.test.filters.SdkSuppress
import androidx.ui.core.Alignment
import androidx.ui.core.DensityAmbient
import androidx.ui.core.Modifier
-import androidx.ui.core.onPositioned
-import androidx.ui.core.testTag
import androidx.ui.foundation.shape.corner.CircleShape
import androidx.ui.geometry.Offset
import androidx.ui.graphics.Color
import androidx.ui.graphics.RectangleShape
import androidx.ui.graphics.toArgb
-import androidx.ui.layout.Stack
import androidx.ui.layout.preferredSize
import androidx.ui.layout.wrapContentSize
+import androidx.ui.test.assertHeightIsEqualTo
import androidx.ui.test.assertShape
+import androidx.ui.test.assertWidthIsEqualTo
import androidx.ui.test.captureToBitmap
import androidx.ui.test.createComposeRule
import androidx.ui.test.findRoot
-import androidx.ui.test.findByTag
-import androidx.ui.test.setContentAndCollectSizes
-import androidx.ui.unit.Density
-import androidx.ui.unit.IntSize
import androidx.ui.unit.dp
-import com.google.common.truth.Truth
import org.junit.Assert
-import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
-import java.util.concurrent.CountDownLatch
-import java.util.concurrent.TimeUnit
@MediumTest
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.O)
@@ -137,93 +127,55 @@
@Test
fun canvas_noSize_emptyCanvas() {
- composeTestRule.setContentAndCollectSizes {
+ composeTestRule.setContentForSizeAssertions {
Canvas(modifier = Modifier) {
drawRect(Color.Black)
}
}
- .assertHeightEqualsTo(0.dp)
- .assertWidthEqualsTo(0.dp)
+ .assertHeightIsEqualTo(0.dp)
+ .assertWidthIsEqualTo(0.dp)
}
@Test
fun canvas_exactSizes() {
- var canvasSize: IntSize? = null
- val latch = CountDownLatch(1)
- composeTestRule.setContentAndCollectSizes {
- SemanticParent {
- Canvas(
- Modifier.preferredSize(100.dp)
- .onPositioned { position -> canvasSize = position.size }
- ) {
- drawRect(Color.Red)
-
- latch.countDown()
- }
+ composeTestRule.setContentForSizeAssertions {
+ Canvas(Modifier.preferredSize(100.dp)) {
+ drawRect(Color.Red)
}
}
-
- assertTrue(latch.await(5, TimeUnit.SECONDS))
-
- with(composeTestRule.density) {
- Truth.assertThat(canvasSize!!.width).isEqualTo(100.dp.toIntPx())
- Truth.assertThat(canvasSize!!.height).isEqualTo(100.dp.toIntPx())
- }
-
- val bitmap = findByTag(contentTag).captureToBitmap()
- bitmap.assertShape(
- density = composeTestRule.density,
- backgroundColor = Color.Red,
- shapeColor = Color.Red,
- shape = RectangleShape
- )
+ .assertWidthIsEqualTo(100.dp)
+ .assertHeightIsEqualTo(100.dp)
+ .captureToBitmap()
+ .assertShape(
+ density = composeTestRule.density,
+ backgroundColor = Color.Red,
+ shapeColor = Color.Red,
+ shape = RectangleShape
+ )
}
@Test
fun canvas_exactSizes_drawCircle() {
- var canvasSize: IntSize? = null
- val latch = CountDownLatch(1)
- composeTestRule.setContentAndCollectSizes {
- SemanticParent {
- Canvas(
- Modifier.preferredSize(100.dp)
- .onPositioned { position -> canvasSize = position.size }
- ) {
- drawRect(Color.Red)
- drawCircle(
- Color.Blue,
- radius = 10.0f
- )
- latch.countDown()
- }
+ composeTestRule.setContentForSizeAssertions {
+ Canvas(Modifier.preferredSize(100.dp)) {
+ drawRect(Color.Red)
+ drawCircle(
+ Color.Blue,
+ radius = 10.0f
+ )
}
}
-
- assertTrue(latch.await(5, TimeUnit.SECONDS))
-
- with(composeTestRule.density) {
- Truth.assertThat(canvasSize!!.width).isEqualTo(100.dp.toIntPx())
- Truth.assertThat(canvasSize!!.height).isEqualTo(100.dp.toIntPx())
- }
-
- val bitmap = findByTag(contentTag).captureToBitmap()
- bitmap.assertShape(
- density = composeTestRule.density,
- backgroundColor = Color.Red,
- shapeColor = Color.Blue,
- shape = CircleShape,
- shapeSizeX = 20.0f,
- shapeSizeY = 20.0f,
- shapeOverlapPixelCount = 2.0f
- )
- }
-
- @Composable
- fun SemanticParent(children: @Composable Density.() -> Unit) {
- Stack(Modifier.testTag(contentTag)) {
- Box {
- DensityAmbient.current.children()
- }
- }
+ .assertWidthIsEqualTo(100.dp)
+ .assertHeightIsEqualTo(100.dp)
+ .captureToBitmap()
+ .assertShape(
+ density = composeTestRule.density,
+ backgroundColor = Color.Red,
+ shapeColor = Color.Blue,
+ shape = CircleShape,
+ shapeSizeX = 20.0f,
+ shapeSizeY = 20.0f,
+ shapeOverlapPixelCount = 2.0f
+ )
}
}
\ No newline at end of file
diff --git a/ui/ui-foundation/src/androidTest/java/androidx/ui/foundation/FoundationTest.kt b/ui/ui-foundation/src/androidTest/java/androidx/ui/foundation/FoundationTest.kt
new file mode 100644
index 0000000..5ed7fc0
--- /dev/null
+++ b/ui/ui-foundation/src/androidTest/java/androidx/ui/foundation/FoundationTest.kt
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2020 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.ui.foundation
+
+import androidx.compose.Composable
+import androidx.ui.core.Modifier
+import androidx.ui.core.testTag
+import androidx.ui.layout.DpConstraints
+import androidx.ui.layout.Stack
+import androidx.ui.layout.preferredSizeIn
+import androidx.ui.test.ComposeTestRule
+import androidx.ui.test.SemanticsNodeInteraction
+import androidx.ui.test.findByTag
+import androidx.ui.unit.dp
+
+/**
+ * Constant to emulate very big but finite constraints
+ */
+val BigTestConstraints = DpConstraints(maxWidth = 5000.dp, maxHeight = 5000.dp)
+
+fun ComposeTestRule.setContentForSizeAssertions(
+ parentConstraints: DpConstraints = BigTestConstraints,
+ children: @Composable () -> Unit
+): SemanticsNodeInteraction {
+ setContent {
+ Stack {
+ Stack(
+ Modifier.preferredSizeIn(parentConstraints)
+ .testTag("containerForSizeAssertion")) {
+ children()
+ }
+ }
+ }
+
+ return findByTag("containerForSizeAssertion")
+}
diff --git a/ui/ui-material/src/androidTest/java/androidx/ui/material/AppBarTest.kt b/ui/ui-material/src/androidTest/java/androidx/ui/material/AppBarTest.kt
index e6d6c1e..a3a3c71 100644
--- a/ui/ui-material/src/androidTest/java/androidx/ui/material/AppBarTest.kt
+++ b/ui/ui-material/src/androidTest/java/androidx/ui/material/AppBarTest.kt
@@ -18,29 +18,32 @@
import androidx.compose.Composable
import androidx.test.filters.SmallTest
-import androidx.ui.core.LayoutCoordinates
import androidx.ui.core.Modifier
-import androidx.ui.core.globalPosition
-import androidx.ui.core.onChildPositioned
-import androidx.ui.core.onPositioned
+import androidx.ui.core.testTag
import androidx.ui.foundation.Box
import androidx.ui.foundation.Icon
import androidx.ui.foundation.Text
import androidx.ui.foundation.currentTextStyle
import androidx.ui.graphics.Color
import androidx.ui.graphics.painter.ColorPainter
+import androidx.ui.test.assertHeightIsEqualTo
import androidx.ui.test.assertIsDisplayed
+import androidx.ui.test.assertLeftPositionInRootIsEqualTo
+import androidx.ui.test.assertTopPositionInRootIsEqualTo
import androidx.ui.test.createComposeRule
+import androidx.ui.test.findByTag
import androidx.ui.test.findByText
+import androidx.ui.test.getBoundsInRoot
import androidx.ui.text.TextStyle
-import androidx.ui.unit.Density
+import androidx.ui.unit.Dp
import androidx.ui.unit.dp
+import androidx.ui.unit.height
+import androidx.ui.unit.width
import com.google.common.truth.Truth.assertThat
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
-import kotlin.math.roundToInt
@SmallTest
@RunWith(JUnit4::class)
@@ -53,13 +56,12 @@
@Test
fun topAppBar_expandsToScreen() {
- val dm = composeTestRule.displayMetrics
composeTestRule
- .setMaterialContentAndCollectSizes {
+ .setMaterialContentForSizeAssertions {
TopAppBar(title = { Text("Title") })
}
- .assertHeightEqualsTo(appBarHeight)
- .assertWidthEqualsTo { dm.widthPixels }
+ .assertHeightIsEqualTo(appBarHeight)
+ .assertWidthFillsRoot()
}
@Test
@@ -73,101 +75,73 @@
@Test
fun topAppBar_default_positioning() {
- var appBarCoords: LayoutCoordinates? = null
- var navigationIconCoords: LayoutCoordinates? = null
- var titleCoords: LayoutCoordinates? = null
- var actionCoords: LayoutCoordinates? = null
composeTestRule.setMaterialContent {
- Box(Modifier.onChildPositioned { appBarCoords = it }) {
+ Box(Modifier.testTag("bar")) {
TopAppBar(
navigationIcon = {
- FakeIcon(Modifier.onPositioned { navigationIconCoords = it })
+ FakeIcon(Modifier.testTag("navigationIcon"))
},
title = {
- Text("title", Modifier.onPositioned { titleCoords = it })
+ Text("title", Modifier.testTag("title"))
},
actions = {
- FakeIcon(Modifier.onPositioned { actionCoords = it })
+ FakeIcon(Modifier.testTag("action"))
}
)
}
}
- composeTestRule.runOnIdleComposeWithDensity {
- val appBarBottomEdgeY = appBarCoords!!.globalPosition.y +
- appBarCoords!!.size.height
+ val appBarBounds = findByTag("bar").getBoundsInRoot()
+ val titleBounds = findByTag("title").getBoundsInRoot()
+ val appBarBottomEdgeY = appBarBounds.top + appBarBounds.height
+ findByTag("navigationIcon")
// Navigation icon should be 4.dp from the start
- val navigationIconPositionX = navigationIconCoords!!.globalPosition.x
- val navigationIconExpectedPositionX = AppBarStartAndEndPadding.toIntPx().toFloat()
- assertThat(navigationIconPositionX).isEqualTo(navigationIconExpectedPositionX)
-
+ .assertLeftPositionInRootIsEqualTo(AppBarStartAndEndPadding)
// Navigation icon should be 4.dp from the bottom
- val navigationIconPositionY = navigationIconCoords!!.globalPosition.y
- val navigationIconExpectedPositionY = (appBarBottomEdgeY -
- AppBarStartAndEndPadding.toPx() - FakeIconSize.toPx()
- ).roundToInt().toFloat()
- assertThat(navigationIconPositionY).isEqualTo(navigationIconExpectedPositionY)
+ .assertTopPositionInRootIsEqualTo(
+ appBarBottomEdgeY - AppBarStartAndEndPadding - FakeIconSize)
+ findByTag("title")
// Title should be 72.dp from the start
- val titlePositionX = titleCoords!!.globalPosition.x
// 4.dp padding for the whole app bar + 68.dp inset
- val titleExpectedPositionX = (4.dp.toIntPx() + 68.dp.toIntPx()).toFloat()
- assertThat(titlePositionX).isEqualTo(titleExpectedPositionX)
-
+ .assertLeftPositionInRootIsEqualTo(4.dp + 68.dp)
// Title should be vertically centered
- val titlePositionY = titleCoords!!.globalPosition.y
- val titleHeight = titleCoords!!.size.height
- val appBarHeight = appBarCoords!!.size.height
- val titleExpectedPositionY = (appBarHeight - titleHeight) / 2
- assertThat(titlePositionY).isEqualTo(titleExpectedPositionY)
+ .assertTopPositionInRootIsEqualTo((appBarBounds.height - titleBounds.height) / 2)
+ findByTag("action")
// Action should be placed at the end
- val actionPositionX = actionCoords!!.globalPosition.x
- val actionExpectedPositionX =
- expectedActionPosition(appBarCoords!!.size.width.toFloat())
- assertThat(actionPositionX).isEqualTo(actionExpectedPositionX)
-
+ .assertLeftPositionInRootIsEqualTo(expectedActionPosition(appBarBounds.width))
// Action should be 4.dp from the bottom
- val actionPositionY = actionCoords!!.globalPosition.y
- val actionExpectedPositionY = (appBarBottomEdgeY - AppBarStartAndEndPadding.toPx() -
- FakeIconSize.toPx()
- ).roundToInt().toFloat()
- assertThat(actionPositionY).isEqualTo(actionExpectedPositionY)
- }
+ .assertTopPositionInRootIsEqualTo(
+ appBarBottomEdgeY - AppBarStartAndEndPadding - FakeIconSize)
}
@Test
fun topAppBar_noNavigationIcon_positioning() {
- var appBarCoords: LayoutCoordinates? = null
- var titleCoords: LayoutCoordinates? = null
- var actionCoords: LayoutCoordinates? = null
composeTestRule.setMaterialContent {
- Box(Modifier.onChildPositioned { appBarCoords = it }) {
+ Box(Modifier.testTag("bar")) {
TopAppBar(
title = {
- Text("title", Modifier.onPositioned { titleCoords = it })
+ Text("title", Modifier.testTag("title"))
},
actions = {
- FakeIcon(Modifier.onPositioned { actionCoords = it })
+ FakeIcon(Modifier.testTag("action"))
}
)
}
}
- composeTestRule.runOnIdleComposeWithDensity {
- // Title should now be placed 16.dp from the start, as there is no navigation icon
- val titlePositionX = titleCoords!!.globalPosition.x
- // 4.dp padding for the whole app bar + 12.dp inset
- val titleExpectedPositionX = (4.dp.toIntPx() + 12.dp.toIntPx()).toFloat()
- assertThat(titlePositionX).isEqualTo(titleExpectedPositionX)
+ val appBarBounds = findByTag("bar").getBoundsInRoot()
+ findByTag("title")
+ // Title should now be placed 16.dp from the start, as there is no navigation icon
+ // 4.dp padding for the whole app bar + 12.dp inset
+ .assertLeftPositionInRootIsEqualTo(4.dp + 12.dp)
+
+ findByTag("action")
// Action should still be placed at the end
- val actionPositionX = actionCoords!!.globalPosition.x
- val actionExpectedPositionX =
- expectedActionPosition(appBarCoords!!.size.width.toFloat())
- assertThat(actionPositionX).isEqualTo(actionExpectedPositionX)
- }
+ .assertLeftPositionInRootIsEqualTo(expectedActionPosition(appBarBounds.width))
}
@Test
@@ -191,43 +165,31 @@
@Test
fun bottomAppBar_expandsToScreen() {
- val dm = composeTestRule.displayMetrics
composeTestRule
- .setMaterialContentAndCollectSizes {
+ .setMaterialContentForSizeAssertions {
BottomAppBar {}
}
- .assertHeightEqualsTo(appBarHeight)
- .assertWidthEqualsTo { dm.widthPixels }
+ .assertHeightIsEqualTo(appBarHeight)
+ .assertWidthFillsRoot()
}
@Test
fun bottomAppBar_default_positioning() {
- var appBarCoords: LayoutCoordinates? = null
- var childCoords: LayoutCoordinates? = null
composeTestRule.setMaterialContent {
- Box(Modifier.onChildPositioned { appBarCoords = it }) {
- BottomAppBar {
- FakeIcon(Modifier.onPositioned { childCoords = it })
- }
+ BottomAppBar(Modifier.testTag("bar")) {
+ FakeIcon(Modifier.testTag("icon"))
}
}
- composeTestRule.runOnIdleComposeWithDensity {
+ val appBarBounds = findByTag("bar").getBoundsInRoot()
+ val appBarBottomEdgeY = appBarBounds.top + appBarBounds.height
+
+ findByTag("icon")
// Child icon should be 4.dp from the start
- val childIconPositionX = childCoords!!.globalPosition.x
- val childIconExpectedPositionX = AppBarStartAndEndPadding.toIntPx().toFloat()
- assertThat(childIconPositionX).isEqualTo(childIconExpectedPositionX)
-
- val appBarBottomEdgeY = appBarCoords!!.globalPosition.y +
- appBarCoords!!.size.height
-
+ .assertLeftPositionInRootIsEqualTo(AppBarStartAndEndPadding)
// Child icon should be 4.dp from the bottom
- val childIconPositionY = childCoords!!.globalPosition.y
- val childIconExpectedPositionY = (appBarBottomEdgeY - AppBarStartAndEndPadding.toPx() -
- FakeIconSize.toPx()
- ).roundToInt().toFloat()
- assertThat(childIconPositionY).isEqualTo(childIconExpectedPositionY)
- }
+ .assertTopPositionInRootIsEqualTo(
+ appBarBottomEdgeY - AppBarStartAndEndPadding - FakeIconSize)
}
/**
@@ -239,10 +201,8 @@
}
}
- private fun Density.expectedActionPosition(appBarWidth: Float): Float {
- return appBarWidth - AppBarStartAndEndPadding.toIntPx() -
- FakeIconSize.toIntPx()
- }
+ private fun expectedActionPosition(appBarWidth: Dp): Dp =
+ appBarWidth - AppBarStartAndEndPadding - FakeIconSize
private val AppBarStartAndEndPadding = 4.dp
diff --git a/ui/ui-material/src/androidTest/java/androidx/ui/material/BottomNavigationTest.kt b/ui/ui-material/src/androidTest/java/androidx/ui/material/BottomNavigationTest.kt
index 9deec05..449c150 100644
--- a/ui/ui-material/src/androidTest/java/androidx/ui/material/BottomNavigationTest.kt
+++ b/ui/ui-material/src/androidTest/java/androidx/ui/material/BottomNavigationTest.kt
@@ -20,6 +20,7 @@
import androidx.ui.core.Modifier
import androidx.ui.core.globalPosition
import androidx.ui.core.onPositioned
+import androidx.ui.core.testTag
import androidx.ui.foundation.Box
import androidx.ui.foundation.Icon
import androidx.ui.foundation.Text
@@ -27,16 +28,23 @@
import androidx.ui.material.icons.filled.Favorite
import androidx.ui.material.samples.BottomNavigationSample
import androidx.ui.test.assertCountEquals
+import androidx.ui.test.assertHeightIsEqualTo
+import androidx.ui.test.assertIsEqualTo
import androidx.ui.test.assertIsNotDisplayed
import androidx.ui.test.assertIsSelected
import androidx.ui.test.assertIsUnselected
+import androidx.ui.test.assertLeftPositionInRootIsEqualTo
+import androidx.ui.test.assertTopPositionInRootIsEqualTo
import androidx.ui.test.createComposeRule
import androidx.ui.test.doClick
import androidx.ui.test.findAll
+import androidx.ui.test.findByTag
import androidx.ui.test.findByText
+import androidx.ui.test.getBoundsInRoot
import androidx.ui.test.isInMutuallyExclusiveGroup
-import androidx.ui.text.LastBaseline
import androidx.ui.unit.dp
+import androidx.ui.unit.height
+import androidx.ui.unit.width
import com.google.common.truth.Truth
import org.junit.Rule
import org.junit.Test
@@ -54,17 +62,12 @@
@Test
fun bottomNavigation_size() {
- lateinit var parentCoords: LayoutCoordinates
val height = 56.dp
- composeTestRule.setMaterialContentAndCollectSizes(
- modifier = Modifier.onPositioned { coords: LayoutCoordinates ->
- parentCoords = coords
- }
- ) {
+ composeTestRule.setMaterialContentForSizeAssertions {
BottomNavigationSample()
}
- .assertWidthEqualsTo { parentCoords.size.width }
- .assertHeightEqualsTo(height)
+ .assertWidthFillsRoot()
+ .assertHeightIsEqualTo(height)
}
@Test
@@ -110,25 +113,18 @@
@Test
fun bottomNavigationItemContent_withLabel_sizeAndPosition() {
- lateinit var itemCoords: LayoutCoordinates
- lateinit var iconCoords: LayoutCoordinates
- lateinit var textCoords: LayoutCoordinates
composeTestRule.setMaterialContent {
Box {
BottomNavigation {
BottomNavigationItem(
- modifier = Modifier.onPositioned { coords: LayoutCoordinates ->
- itemCoords = coords
- },
+ modifier = Modifier.testTag("item"),
icon = {
Icon(Icons.Filled.Favorite,
- Modifier.onPositioned { iconCoords = it }
+ Modifier.testTag("icon")
)
},
text = {
- Text("Item",
- Modifier.onPositioned { textCoords = it }
- )
+ Text("ItemText")
},
selected = true,
>
@@ -137,51 +133,44 @@
}
}
- composeTestRule.runOnIdleComposeWithDensity {
- // Distance from the bottom to the text baseline and from the text baseline to the
- // bottom of the icon
- val textBaseline = 12.dp.toIntPx().toFloat()
+ val itemBounds = findByTag("item").getBoundsInRoot()
+ val iconBounds = findByTag("icon").getBoundsInRoot()
+ val textBounds = findByText("ItemText").getBoundsInRoot()
- // Relative position of the baseline to the top of text
- val relativeTextBaseline = textCoords[LastBaseline].toFloat()
- // Absolute y position of the text baseline
- val absoluteTextBaseline = textCoords.globalPosition.y + relativeTextBaseline
+ // Distance from the bottom to the text baseline and from the text baseline to the
+ // bottom of the icon
+ val textBaseline = 12.dp
- val itemBottom = itemCoords.size.height.toFloat() + itemCoords.globalPosition.y
- // Text baseline should be 12.dp from the bottom of the item
- Truth.assertThat(absoluteTextBaseline).isEqualTo(itemBottom - textBaseline)
+ // Relative position of the baseline to the top of text
+ val relativeTextBaseline = findByText("ItemText").getLastBaselinePosition()
+ // Absolute y position of the text baseline
+ val absoluteTextBaseline = textBounds.top + relativeTextBaseline
+ val itemBottom = itemBounds.height + itemBounds.top
+ // Text baseline should be 12.dp from the bottom of the item
+ absoluteTextBaseline.assertIsEqualTo(itemBottom - textBaseline)
+
+ findByTag("icon")
// The icon should be centered in the item
- val iconExpectedX = (itemCoords.size.width.toFloat() - iconCoords.size.width
- .toFloat()) / 2
+ .assertLeftPositionInRootIsEqualTo((itemBounds.width - iconBounds.width) / 2)
// The bottom of the icon is 12.dp above the text baseline
- val iconExpectedY =
- absoluteTextBaseline - 12.dp.toIntPx().toFloat() -
- iconCoords.size.height
-
- Truth.assertThat(iconCoords.globalPosition.x).isWithin(1f).of(iconExpectedX)
- Truth.assertThat(iconCoords.globalPosition.y).isEqualTo(iconExpectedY)
- }
+ .assertTopPositionInRootIsEqualTo(absoluteTextBaseline - 12.dp - iconBounds.height)
}
@Test
fun bottomNavigationItemContent_withLabel_unselected_sizeAndPosition() {
- lateinit var itemCoords: LayoutCoordinates
- lateinit var iconCoords: LayoutCoordinates
composeTestRule.setMaterialContent {
Box {
BottomNavigation {
BottomNavigationItem(
- modifier = Modifier.onPositioned { coords: LayoutCoordinates ->
- itemCoords = coords
- },
+ modifier = Modifier.testTag("item"),
icon = {
Icon(Icons.Filled.Favorite,
- Modifier.onPositioned { iconCoords = it }
+ Modifier.testTag("icon")
)
},
text = {
- Text("Item")
+ Text("ItemText")
},
selected = false,
>
@@ -193,34 +182,26 @@
// The text should not be placed, since the item is not selected and alwaysShowLabels
// is false
- findByText("Item").assertIsNotDisplayed()
+ findByText("ItemText").assertIsNotDisplayed()
- composeTestRule.runOnIdleComposeWithDensity {
- // The icon should be centered in the item
- val iconExpectedX = (itemCoords.size.width.toFloat() -
- iconCoords.size.width.toFloat()) / 2
- val iconExpectedY = ((itemCoords.size.height - iconCoords.size.height) / 2)
- .toFloat()
+ val itemBounds = findByTag("item").getBoundsInRoot()
+ val iconBounds = findByTag("icon").getBoundsInRoot()
- Truth.assertThat(iconCoords.globalPosition.x).isWithin(1f).of(iconExpectedX)
- Truth.assertThat(iconCoords.globalPosition.y).isEqualTo(iconExpectedY)
- }
+ findByTag("icon")
+ .assertLeftPositionInRootIsEqualTo((itemBounds.width - iconBounds.width) / 2)
+ .assertTopPositionInRootIsEqualTo((itemBounds.height - iconBounds.height) / 2)
}
@Test
fun bottomNavigationItemContent_withoutLabel_sizeAndPosition() {
- lateinit var itemCoords: LayoutCoordinates
- lateinit var iconCoords: LayoutCoordinates
composeTestRule.setMaterialContent {
Box {
BottomNavigation {
BottomNavigationItem(
- modifier = Modifier.onPositioned { coords: LayoutCoordinates ->
- itemCoords = coords
- },
+ modifier = Modifier.testTag("item"),
icon = {
Icon(Icons.Filled.Favorite,
- Modifier.onPositioned { iconCoords = it }
+ Modifier.testTag("icon")
)
},
text = {},
@@ -231,16 +212,13 @@
}
}
- composeTestRule.runOnIdleComposeWithDensity {
- // The icon should be centered in the item, as there is no text placeable provided
- val iconExpectedX = (itemCoords.size.width - iconCoords.size.width) / 2
- val iconExpectedY = (itemCoords.size.height - iconCoords.size.height) / 2
+ val itemBounds = findByTag("item").getBoundsInRoot()
+ val iconBounds = findByTag("icon").getBoundsInRoot()
- Truth.assertThat(iconCoords.globalPosition.x).isWithin(1f).of(
- iconExpectedX.toFloat()
- )
- Truth.assertThat(iconCoords.globalPosition.y).isEqualTo(iconExpectedY.toFloat())
- }
+ // The icon should be centered in the item, as there is no text placeable provided
+ findByTag("icon")
+ .assertLeftPositionInRootIsEqualTo((itemBounds.width - iconBounds.width) / 2)
+ .assertTopPositionInRootIsEqualTo((itemBounds.height - iconBounds.height) / 2)
}
@Test
diff --git a/ui/ui-material/src/androidTest/java/androidx/ui/material/CheckboxUiTest.kt b/ui/ui-material/src/androidTest/java/androidx/ui/material/CheckboxUiTest.kt
index 55aaab4..41c052f 100644
--- a/ui/ui-material/src/androidTest/java/androidx/ui/material/CheckboxUiTest.kt
+++ b/ui/ui-material/src/androidTest/java/androidx/ui/material/CheckboxUiTest.kt
@@ -125,9 +125,9 @@
private fun materialSizeTestForValue(checkboxValue: ToggleableState) {
composeTestRule
- .setMaterialContentAndCollectSizes {
+ .setMaterialContentForSizeAssertions {
TriStateCheckbox(state = checkboxValue, enabled = false)
}
- .assertIsSquareWithSize { 2.dp.toIntPx() * 2 + 20.dp.toIntPx() }
+ .assertIsSquareWithSize(2.dp * 2 + 20.dp)
}
}
\ No newline at end of file
diff --git a/ui/ui-material/src/androidTest/java/androidx/ui/material/DividerUiTest.kt b/ui/ui-material/src/androidTest/java/androidx/ui/material/DividerUiTest.kt
index 3c4ee77..02c81e8 100644
--- a/ui/ui-material/src/androidTest/java/androidx/ui/material/DividerUiTest.kt
+++ b/ui/ui-material/src/androidTest/java/androidx/ui/material/DividerUiTest.kt
@@ -17,6 +17,7 @@
package androidx.ui.material
import androidx.test.filters.MediumTest
+import androidx.ui.test.assertHeightIsEqualTo
import androidx.ui.test.createComposeRule
import androidx.ui.unit.dp
import org.junit.Rule
@@ -35,38 +36,35 @@
@Test
fun divider_DefaultSizes() {
- val dm = composeTestRule.displayMetrics
composeTestRule
- .setMaterialContentAndCollectSizes {
+ .setMaterialContentForSizeAssertions {
Divider()
}
- .assertHeightEqualsTo(defaultHeight)
- .assertWidthEqualsTo { dm.widthPixels }
+ .assertHeightIsEqualTo(defaultHeight)
+ .assertWidthFillsRoot()
}
@Test
fun divider_CustomSizes() {
val height = 20.dp
- val dm = composeTestRule.displayMetrics
composeTestRule
- .setMaterialContentAndCollectSizes {
+ .setMaterialContentForSizeAssertions {
Divider(thickness = height)
}
- .assertWidthEqualsTo { dm.widthPixels }
- .assertHeightEqualsTo(height)
+ .assertWidthFillsRoot()
+ .assertHeightIsEqualTo(height)
}
@Test
fun divider_SizesWithIndent_DoesNotChanged() {
- val dm = composeTestRule.displayMetrics
val indent = 75.dp
val height = 21.dp
composeTestRule
- .setMaterialContentAndCollectSizes {
+ .setMaterialContentForSizeAssertions {
Divider(startIndent = indent, thickness = height)
}
- .assertHeightEqualsTo(height)
- .assertWidthEqualsTo { dm.widthPixels }
+ .assertHeightIsEqualTo(height)
+ .assertWidthFillsRoot()
}
}
\ No newline at end of file
diff --git a/ui/ui-material/src/androidTest/java/androidx/ui/material/DrawerTest.kt b/ui/ui-material/src/androidTest/java/androidx/ui/material/DrawerTest.kt
index 2f2da2f..510382b 100644
--- a/ui/ui-material/src/androidTest/java/androidx/ui/material/DrawerTest.kt
+++ b/ui/ui-material/src/androidTest/java/androidx/ui/material/DrawerTest.kt
@@ -31,6 +31,10 @@
import androidx.ui.graphics.Color
import androidx.ui.layout.fillMaxSize
import androidx.ui.layout.rtl
+import androidx.ui.test.assertIsEqualTo
+import androidx.ui.test.assertLeftPositionInRootIsEqualTo
+import androidx.ui.test.assertTopPositionInRootIsEqualTo
+import androidx.ui.test.assertWidthIsEqualTo
import androidx.ui.test.createComposeRule
import androidx.ui.test.doGesture
import androidx.ui.test.findByTag
@@ -42,7 +46,6 @@
import androidx.ui.test.sendSwipeLeft
import androidx.ui.test.sendSwipeRight
import androidx.ui.test.sendSwipeUp
-import androidx.ui.unit.IntSize
import androidx.ui.unit.dp
import androidx.ui.unit.height
import androidx.ui.unit.width
@@ -65,17 +68,14 @@
@Test
fun modalDrawer_testOffset_whenOpened() {
- var position: Offset? = null
composeTestRule.setMaterialContent {
ModalDrawerLayout(DrawerState.Opened, {}, drawerContent = {
- Box(Modifier.fillMaxSize().onPositioned { coords: LayoutCoordinates ->
- position = coords.localToGlobal(Offset.Zero)
- })
+ Box(Modifier.fillMaxSize().testTag("content"))
}, bodyContent = emptyContent())
}
- runOnIdleCompose {
- assertThat(position!!.x).isEqualTo(0f)
- }
+
+ findByTag("content")
+ .assertLeftPositionInRootIsEqualTo(0.dp)
}
@Test
@@ -84,52 +84,42 @@
composeTestRule.setMaterialContent {
ModalDrawerLayout(DrawerState.Closed, {}, drawerContent = {
Box(Modifier.fillMaxSize().onPositioned { coords: LayoutCoordinates ->
- position = coords.localToGlobal(Offset.Zero)
+ position = coords.localToRoot(Offset.Zero)
})
}, bodyContent = emptyContent())
}
- val width = composeTestRule.displayMetrics.widthPixels
- runOnIdleCompose {
- assertThat(position!!.x.roundToInt()).isEqualTo(-width)
+
+ val width = rootWidth()
+ composeTestRule.runOnIdleComposeWithDensity {
+ position!!.x.toDp().assertIsEqualTo(-width)
}
}
@Test
fun modalDrawer_testEndPadding_whenOpened() {
- var size: IntSize? = null
composeTestRule.setMaterialContent {
ModalDrawerLayout(DrawerState.Opened, {}, drawerContent = {
- Box(Modifier.fillMaxSize().onPositioned { coords: LayoutCoordinates ->
- size = coords.size
- })
+ Box(Modifier.fillMaxSize().testTag("content"))
}, bodyContent = emptyContent())
}
- val width = composeTestRule.displayMetrics.widthPixels
- composeTestRule.runOnIdleComposeWithDensity {
- assertThat(size!!.width)
- .isEqualTo(width - 56.dp.toPx().roundToInt())
- }
+ findByTag("content")
+ .assertWidthIsEqualTo(rootWidth() - 56.dp)
}
@Test
fun bottomDrawer_testOffset_whenOpened() {
- var position: Offset? = null
composeTestRule.setMaterialContent {
BottomDrawerLayout(DrawerState.Opened, {}, drawerContent = {
- Box(Modifier.fillMaxSize().onPositioned { coords: LayoutCoordinates ->
- position = coords.localToGlobal(Offset.Zero)
- })
+ Box(Modifier.fillMaxSize().testTag("content"))
}, bodyContent = emptyContent())
}
- val width = composeTestRule.displayMetrics.widthPixels
- val height = composeTestRule.displayMetrics.heightPixels
- // temporary calculation of landscape screen
- val expectedHeight = if (width > height) 0 else (height / 2f).roundToInt()
- runOnIdleCompose {
- assertThat(position!!.y.roundToInt()).isEqualTo(expectedHeight)
- }
+ val width = rootWidth()
+ val height = rootHeight()
+ val expectedHeight = if (width > height) 0.dp else (height / 2)
+ findByTag("content")
+ .assertTopPositionInRootIsEqualTo(expectedHeight)
}
@Test
@@ -138,13 +128,14 @@
composeTestRule.setMaterialContent {
BottomDrawerLayout(DrawerState.Closed, {}, drawerContent = {
Box(Modifier.fillMaxSize().onPositioned { coords: LayoutCoordinates ->
- position = coords.localToGlobal(Offset.Zero)
+ position = coords.localToRoot(Offset.Zero)
})
}, bodyContent = emptyContent())
}
- val height = composeTestRule.displayMetrics.heightPixels
- runOnIdleCompose {
- assertThat(position!!.y.roundToInt()).isEqualTo(height)
+
+ val height = rootHeight()
+ composeTestRule.runOnIdleComposeWithDensity {
+ position!!.y.toDp().assertIsEqualTo(height)
}
}
diff --git a/ui/ui-material/src/androidTest/java/androidx/ui/material/FloatingActionButtonTest.kt b/ui/ui-material/src/androidTest/java/androidx/ui/material/FloatingActionButtonTest.kt
index f8e81e7..99fcc38 100644
--- a/ui/ui-material/src/androidTest/java/androidx/ui/material/FloatingActionButtonTest.kt
+++ b/ui/ui-material/src/androidTest/java/androidx/ui/material/FloatingActionButtonTest.kt
@@ -99,7 +99,7 @@
@Test
fun defaultFabHasSizeFromSpec() {
composeTestRule
- .setMaterialContentAndCollectSizes {
+ .setMaterialContentForSizeAssertions {
FloatingActionButton( {
Icon(Icons.Filled.Favorite)
}
diff --git a/ui/ui-material/src/androidTest/java/androidx/ui/material/IconButtonTest.kt b/ui/ui-material/src/androidTest/java/androidx/ui/material/IconButtonTest.kt
index e061541..904c874 100644
--- a/ui/ui-material/src/androidTest/java/androidx/ui/material/IconButtonTest.kt
+++ b/ui/ui-material/src/androidTest/java/androidx/ui/material/IconButtonTest.kt
@@ -17,27 +17,28 @@
import androidx.compose.emptyContent
import androidx.test.filters.LargeTest
-import androidx.ui.core.LayoutCoordinates
import androidx.ui.core.Modifier
-import androidx.ui.core.onPositioned
-import androidx.ui.core.positionInParent
+import androidx.ui.core.testTag
import androidx.ui.foundation.Box
import androidx.ui.layout.preferredSize
import androidx.ui.material.samples.IconButtonSample
import androidx.ui.material.samples.IconToggleButtonSample
+import androidx.ui.test.assertHeightIsEqualTo
import androidx.ui.test.assertIsOff
import androidx.ui.test.assertIsOn
+import androidx.ui.test.assertLeftPositionInRootIsEqualTo
+import androidx.ui.test.assertTopPositionInRootIsEqualTo
+import androidx.ui.test.assertWidthIsEqualTo
import androidx.ui.test.createComposeRule
import androidx.ui.test.doClick
import androidx.ui.test.find
+import androidx.ui.test.findByTag
import androidx.ui.test.isToggleable
import androidx.ui.unit.dp
-import com.google.common.truth.Truth
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
-import kotlin.math.roundToInt
@LargeTest
@RunWith(JUnit4::class)
@@ -53,63 +54,52 @@
val width = 48.dp
val height = 48.dp
composeTestRule
- .setMaterialContentAndCollectSizes {
+ .setMaterialContentForSizeAssertions {
IconButtonSample()
}
- .assertWidthEqualsTo(width)
- .assertHeightEqualsTo(height)
+ .assertWidthIsEqualTo(width)
+ .assertHeightIsEqualTo(height)
}
@Test
fun iconButton_materialIconSize_iconPositioning() {
val diameter = 24.dp
- lateinit var iconCoords: LayoutCoordinates
composeTestRule.setMaterialContent {
Box {
IconButton( {
Box(
- Modifier.preferredSize(diameter).onPositioned { iconCoords = it },
+ Modifier.preferredSize(diameter).testTag("icon"),
children = emptyContent()
)
}
}
}
- composeTestRule.runOnIdleComposeWithDensity {
- val iconX = iconCoords.positionInParent.x
- val iconY = iconCoords.positionInParent.y
- // Icon should be centered inside the IconButton
- Truth.assertThat(iconX).isEqualTo((24.dp.toIntPx() / 2f).roundToInt().toFloat())
- Truth.assertThat(iconY).isEqualTo((24.dp.toIntPx() / 2f).roundToInt().toFloat())
- }
+ // Icon should be centered inside the IconButton
+ findByTag("icon")
+ .assertLeftPositionInRootIsEqualTo(24.dp / 2)
+ .assertTopPositionInRootIsEqualTo(24.dp / 2)
}
@Test
fun iconButton_customIconSize_iconPositioning() {
val width = 36.dp
val height = 14.dp
- lateinit var iconCoords: LayoutCoordinates
composeTestRule.setMaterialContent {
Box {
IconButton( {
Box(
- Modifier.preferredSize(width, height).onPositioned { iconCoords = it },
+ Modifier.preferredSize(width, height).testTag("icon"),
children = emptyContent()
)
}
}
}
- composeTestRule.runOnIdleComposeWithDensity {
- val iconX = iconCoords.positionInParent.x
- val iconY = iconCoords.positionInParent.y
-
- val expectedX = ((48.dp.toIntPx() - width.toIntPx()) / 2f).roundToInt().toFloat()
- val expectedY = ((48.dp.toIntPx() - height.toIntPx()) / 2f).roundToInt().toFloat()
- // Icon should be centered inside the IconButton
- Truth.assertThat(iconX).isEqualTo(expectedX)
- Truth.assertThat(iconY).isEqualTo(expectedY)
- }
+ // Icon should be centered inside the IconButton
+ findByTag("icon")
+ .assertLeftPositionInRootIsEqualTo((48.dp - width) / 2)
+ .assertTopPositionInRootIsEqualTo((48.dp - height) / 2)
}
@Test
@@ -117,62 +107,51 @@
val width = 48.dp
val height = 48.dp
composeTestRule
- .setMaterialContentAndCollectSizes {
+ .setMaterialContentForSizeAssertions {
IconToggleButtonSample()
}
- .assertWidthEqualsTo(width)
- .assertHeightEqualsTo(height)
+ .assertWidthIsEqualTo(width)
+ .assertHeightIsEqualTo(height)
}
@Test
fun iconToggleButton_materialIconSize_iconPositioning() {
val diameter = 24.dp
- lateinit var iconCoords: LayoutCoordinates
composeTestRule.setMaterialContent {
Box {
IconToggleButton(checked = false, {
Box(
- Modifier.preferredSize(diameter).onPositioned { iconCoords = it },
+ Modifier.preferredSize(diameter).testTag("icon"),
children = emptyContent()
)
}
}
}
- composeTestRule.runOnIdleComposeWithDensity {
- val iconX = iconCoords.positionInParent.x
- val iconY = iconCoords.positionInParent.y
- // Icon should be centered inside the IconButton
- Truth.assertThat(iconX).isEqualTo((24.dp.toIntPx() / 2f).roundToInt().toFloat())
- Truth.assertThat(iconY).isEqualTo((24.dp.toIntPx() / 2f).roundToInt().toFloat())
- }
+ // Icon should be centered inside the IconButton
+ findByTag("icon")
+ .assertLeftPositionInRootIsEqualTo(24.dp / 2)
+ .assertTopPositionInRootIsEqualTo(24.dp / 2)
}
@Test
fun iconToggleButton_customIconSize_iconPositioning() {
val width = 36.dp
val height = 14.dp
- lateinit var iconCoords: LayoutCoordinates
composeTestRule.setMaterialContent {
Box {
IconToggleButton(checked = false, {
Box(
- Modifier.preferredSize(width, height).onPositioned { iconCoords = it },
+ Modifier.preferredSize(width, height).testTag("icon"),
children = emptyContent())
}
}
}
- composeTestRule.runOnIdleComposeWithDensity {
- val iconX = iconCoords.positionInParent.x
- val iconY = iconCoords.positionInParent.y
-
- val expectedX = ((48.dp.toIntPx() - width.toIntPx()) / 2f).roundToInt().toFloat()
- val expectedY = ((48.dp.toIntPx() - height.toIntPx()) / 2f).roundToInt().toFloat()
- // Icon should be centered inside the IconButton
- Truth.assertThat(iconX).isEqualTo(expectedX)
- Truth.assertThat(iconY).isEqualTo(expectedY)
- }
+ // Icon should be centered inside the IconButton
+ findByTag("icon")
+ .assertLeftPositionInRootIsEqualTo((48.dp - width) / 2)
+ .assertTopPositionInRootIsEqualTo((48.dp - height) / 2)
}
@Test
diff --git a/ui/ui-material/src/androidTest/java/androidx/ui/material/IconTest.kt b/ui/ui-material/src/androidTest/java/androidx/ui/material/IconTest.kt
index 6bf1b54..df17451 100644
--- a/ui/ui-material/src/androidTest/java/androidx/ui/material/IconTest.kt
+++ b/ui/ui-material/src/androidTest/java/androidx/ui/material/IconTest.kt
@@ -25,6 +25,8 @@
import androidx.ui.graphics.vector.VectorAssetBuilder
import androidx.ui.material.icons.Icons
import androidx.ui.material.icons.filled.Menu
+import androidx.ui.test.assertHeightIsEqualTo
+import androidx.ui.test.assertWidthIsEqualTo
import androidx.ui.test.createComposeRule
import androidx.ui.unit.dp
import org.junit.Rule
@@ -44,11 +46,11 @@
val height = 24.dp
val vector = Icons.Filled.Menu
composeTestRule
- .setMaterialContentAndCollectSizes {
+ .setMaterialContentForSizeAssertions {
Icon(vector)
}
- .assertWidthEqualsTo(width)
- .assertHeightEqualsTo(height)
+ .assertWidthIsEqualTo(width)
+ .assertHeightIsEqualTo(height)
}
@Test
@@ -58,11 +60,11 @@
val vector = VectorAssetBuilder(defaultWidth = width, defaultHeight = height,
viewportWidth = width.value, viewportHeight = height.value).build()
composeTestRule
- .setMaterialContentAndCollectSizes {
+ .setMaterialContentForSizeAssertions {
Icon(vector)
}
- .assertWidthEqualsTo(width)
- .assertHeightEqualsTo(height)
+ .assertWidthIsEqualTo(width)
+ .assertHeightIsEqualTo(height)
}
@Test
@@ -70,15 +72,15 @@
val width = 24.dp
val height = 24.dp
composeTestRule
- .setMaterialContentAndCollectSizes {
+ .setMaterialContentForSizeAssertions {
val dummyImage = with(DensityAmbient.current) {
ImageAsset(width.toIntPx(), height.toIntPx())
}
Icon(dummyImage)
}
- .assertWidthEqualsTo(width)
- .assertHeightEqualsTo(height)
+ .assertWidthIsEqualTo(width)
+ .assertHeightIsEqualTo(height)
}
@Test
@@ -87,15 +89,15 @@
val height = 83.dp
composeTestRule
- .setMaterialContentAndCollectSizes {
+ .setMaterialContentForSizeAssertions {
val dummyImage = with(DensityAmbient.current) {
ImageAsset(width.toIntPx(), height.toIntPx())
}
Icon(dummyImage)
}
- .assertWidthEqualsTo(width)
- .assertHeightEqualsTo(height)
+ .assertWidthIsEqualTo(width)
+ .assertHeightIsEqualTo(height)
}
@Test
@@ -104,11 +106,11 @@
val height = 24.dp
val painter = ColorPainter(Color.Red)
composeTestRule
- .setMaterialContentAndCollectSizes {
+ .setMaterialContentForSizeAssertions {
Icon(painter)
}
- .assertWidthEqualsTo(width)
- .assertHeightEqualsTo(height)
+ .assertWidthIsEqualTo(width)
+ .assertHeightIsEqualTo(height)
}
@Test
@@ -117,7 +119,7 @@
val height = 83.dp
composeTestRule
- .setMaterialContentAndCollectSizes {
+ .setMaterialContentForSizeAssertions {
val dummyImage = with(DensityAmbient.current) {
ImageAsset(width.toIntPx(), height.toIntPx())
}
@@ -125,7 +127,7 @@
val imagePainter = ImagePainter(dummyImage)
Icon(imagePainter)
}
- .assertWidthEqualsTo(width)
- .assertHeightEqualsTo(height)
+ .assertWidthIsEqualTo(width)
+ .assertHeightIsEqualTo(height)
}
}
diff --git a/ui/ui-material/src/androidTest/java/androidx/ui/material/ListItemTest.kt b/ui/ui-material/src/androidTest/java/androidx/ui/material/ListItemTest.kt
index 0d8c976..2cad68a 100644
--- a/ui/ui-material/src/androidTest/java/androidx/ui/material/ListItemTest.kt
+++ b/ui/ui-material/src/androidTest/java/androidx/ui/material/ListItemTest.kt
@@ -27,6 +27,7 @@
import androidx.ui.foundation.Text
import androidx.ui.geometry.Offset
import androidx.ui.graphics.ImageAsset
+import androidx.ui.test.assertHeightIsEqualTo
import androidx.ui.test.createComposeRule
import androidx.ui.unit.Dp
import androidx.ui.unit.IntSize
@@ -51,107 +52,99 @@
@Test
fun listItem_oneLine_size() {
- val dm = composeTestRule.displayMetrics
val expectedHeightNoIcon = 48.dp
composeTestRule
- .setMaterialContentAndCollectSizes {
+ .setMaterialContentForSizeAssertions {
ListItem(text = "Primary text")
}
- .assertHeightEqualsTo(expectedHeightNoIcon)
- .assertWidthEqualsTo { dm.widthPixels }
+ .assertHeightIsEqualTo(expectedHeightNoIcon)
+ .assertWidthFillsRoot()
}
@Test
fun listItem_oneLine_withIcon24_size() {
- val dm = composeTestRule.displayMetrics
val expectedHeightSmallIcon = 56.dp
composeTestRule
- .setMaterialContentAndCollectSizes {
+ .setMaterialContentForSizeAssertions {
ListItem(text = "Primary text", icon = icon24x24)
}
- .assertHeightEqualsTo(expectedHeightSmallIcon)
- .assertWidthEqualsTo { dm.widthPixels }
+ .assertHeightIsEqualTo(expectedHeightSmallIcon)
+ .assertWidthFillsRoot()
}
@Test
fun listItem_oneLine_withIcon56_size() {
- val dm = composeTestRule.displayMetrics
val expectedHeightLargeIcon = 72.dp
composeTestRule
- .setMaterialContentAndCollectSizes {
+ .setMaterialContentForSizeAssertions {
ListItem(text = "Primary text", icon = icon56x56)
}
- .assertHeightEqualsTo(expectedHeightLargeIcon)
- .assertWidthEqualsTo { dm.widthPixels }
+ .assertHeightIsEqualTo(expectedHeightLargeIcon)
+ .assertWidthFillsRoot()
}
@Test
fun listItem_twoLine_size() {
- val dm = composeTestRule.displayMetrics
val expectedHeightNoIcon = 64.dp
composeTestRule
- .setMaterialContentAndCollectSizes {
+ .setMaterialContentForSizeAssertions {
ListItem(text = "Primary text", secondaryText = "Secondary text")
}
- .assertHeightEqualsTo(expectedHeightNoIcon)
- .assertWidthEqualsTo { dm.widthPixels }
+ .assertHeightIsEqualTo(expectedHeightNoIcon)
+ .assertWidthFillsRoot()
}
@Test
fun listItem_twoLine_withIcon_size() {
- val dm = composeTestRule.displayMetrics
val expectedHeightWithIcon = 72.dp
composeTestRule
- .setMaterialContentAndCollectSizes {
+ .setMaterialContentForSizeAssertions {
ListItem(
text = "Primary text",
secondaryText = "Secondary text",
icon = icon24x24
)
}
- .assertHeightEqualsTo(expectedHeightWithIcon)
- .assertWidthEqualsTo { dm.widthPixels }
+ .assertHeightIsEqualTo(expectedHeightWithIcon)
+ .assertWidthFillsRoot()
}
@Test
fun listItem_threeLine_size() {
- val dm = composeTestRule.displayMetrics
val expectedHeight = 88.dp
composeTestRule
- .setMaterialContentAndCollectSizes {
+ .setMaterialContentForSizeAssertions {
ListItem(
overlineText = "OVERLINE",
text = "Primary text",
secondaryText = "Secondary text"
)
}
- .assertHeightEqualsTo(expectedHeight)
- .assertWidthEqualsTo { dm.widthPixels }
+ .assertHeightIsEqualTo(expectedHeight)
+ .assertWidthFillsRoot()
}
@Test
fun listItem_threeLine_noSingleLine_size() {
- val dm = composeTestRule.displayMetrics
val expectedHeight = 88.dp
composeTestRule
- .setMaterialContentAndCollectSizes {
+ .setMaterialContentForSizeAssertions {
ListItem(
text = "Primary text",
secondaryText = "Secondary text with long text",
singleLineSecondaryText = false
)
}
- .assertHeightEqualsTo(expectedHeight)
- .assertWidthEqualsTo { dm.widthPixels }
+ .assertHeightIsEqualTo(expectedHeight)
+ .assertWidthFillsRoot()
}
@Test
fun listItem_threeLine_metaText_size() {
- val dm = composeTestRule.displayMetrics
val expectedHeight = 88.dp
composeTestRule
- .setMaterialContentAndCollectSizes {
+ .setMaterialContentForSizeAssertions {
ListItem(
overlineText = "OVERLINE",
text = "Primary text",
@@ -159,16 +152,15 @@
metaText = "meta"
)
}
- .assertHeightEqualsTo(expectedHeight)
- .assertWidthEqualsTo { dm.widthPixels }
+ .assertHeightIsEqualTo(expectedHeight)
+ .assertWidthFillsRoot()
}
@Test
fun listItem_threeLine_noSingleLine_metaText_size() {
- val dm = composeTestRule.displayMetrics
val expectedHeight = 88.dp
composeTestRule
- .setMaterialContentAndCollectSizes {
+ .setMaterialContentForSizeAssertions {
ListItem(
text = "Primary text",
secondaryText = "Secondary text with long text",
@@ -176,8 +168,8 @@
metaText = "meta"
)
}
- .assertHeightEqualsTo(expectedHeight)
- .assertWidthEqualsTo { dm.widthPixels }
+ .assertHeightIsEqualTo(expectedHeight)
+ .assertWidthFillsRoot()
}
@Test
@@ -657,7 +649,7 @@
size: Ref<IntSize>,
baseline: Ref<Float> = Ref()
): Modifier = Modifier.onPositioned { coordinates: LayoutCoordinates ->
- coords.value = coordinates.localToGlobal(Offset.Zero)
+ coords.value = coordinates.localToRoot(Offset.Zero)
baseline.value = coordinates[FirstBaseline].toFloat() + coords.value!!.y
size.value = coordinates.size
}
diff --git a/ui/ui-material/src/androidTest/java/androidx/ui/material/MaterialTest.kt b/ui/ui-material/src/androidTest/java/androidx/ui/material/MaterialTest.kt
index f6bbaf81..b9cf4aa 100644
--- a/ui/ui-material/src/androidTest/java/androidx/ui/material/MaterialTest.kt
+++ b/ui/ui-material/src/androidTest/java/androidx/ui/material/MaterialTest.kt
@@ -17,21 +17,28 @@
package androidx.ui.material
import androidx.compose.Composable
+import androidx.ui.core.AndroidOwner
+import androidx.ui.core.ExperimentalLayoutNodeApi
import androidx.ui.core.Modifier
-import androidx.ui.geometry.Size
+import androidx.ui.core.testTag
import androidx.ui.layout.DpConstraints
import androidx.ui.layout.Stack
import androidx.ui.layout.preferredSizeIn
-import androidx.ui.test.BigTestConstraints
-import androidx.ui.test.CollectedSizes
import androidx.ui.test.ComposeTestRule
import androidx.ui.test.SemanticsNodeInteraction
+import androidx.ui.test.assertHeightIsEqualTo
+import androidx.ui.test.assertIsEqualTo
+import androidx.ui.test.assertWidthIsEqualTo
+import androidx.ui.test.findByTag
+import androidx.ui.test.findRoot
import androidx.ui.test.getAlignmentLinePosition
import androidx.ui.test.runOnIdleCompose
-import androidx.ui.test.setContentAndGetPixelSize
import androidx.ui.text.FirstBaseline
import androidx.ui.text.LastBaseline
import androidx.ui.unit.Density
+import androidx.ui.unit.Dp
+import androidx.ui.unit.dp
+import androidx.ui.unit.width
fun ComposeTestRule.setMaterialContent(
modifier: Modifier = Modifier,
@@ -43,47 +50,76 @@
}
}
}
-fun ComposeTestRule.setMaterialContentWithConstraints(
- parentConstraints: DpConstraints,
- composable: @Composable () -> Unit
-) {
- setContent {
- MaterialTheme {
- Stack {
- Stack(Modifier.preferredSizeIn(parentConstraints)) {
- composable()
- }
- }
- }
- }
-}
+
fun <T> ComposeTestRule.runOnIdleComposeWithDensity(action: Density.() -> T): T {
return runOnIdleCompose {
density.action()
}
}
-fun ComposeTestRule.setMaterialContentAndCollectSizes(
- modifier: Modifier = Modifier,
- parentConstraints: DpConstraints = BigTestConstraints,
- children: @Composable () -> Unit
-): CollectedSizes {
- val sizes = setMaterialContentAndGetPixelSize(modifier, parentConstraints, children)
- return CollectedSizes(sizes, density)
-}
-
-private fun ComposeTestRule.setMaterialContentAndGetPixelSize(
- modifier: Modifier = Modifier,
- parentConstraints: DpConstraints = BigTestConstraints,
- children: @Composable () -> Unit
-): Size = setContentAndGetPixelSize(
- parentConstraints,
- { setMaterialContent(composable = it) }
-) {
- MaterialTheme {
- Surface(modifier = modifier, content = children)
- }
-}
fun SemanticsNodeInteraction.getFirstBaselinePosition() = getAlignmentLinePosition(FirstBaseline)
-fun SemanticsNodeInteraction.getLastBaselinePosition() = getAlignmentLinePosition(LastBaseline)
\ No newline at end of file
+fun SemanticsNodeInteraction.getLastBaselinePosition() = getAlignmentLinePosition(LastBaseline)
+
+fun SemanticsNodeInteraction.assertIsSquareWithSize(expectedSize: Dp) =
+ assertWidthIsEqualTo(expectedSize).assertHeightIsEqualTo(expectedSize)
+
+fun SemanticsNodeInteraction.assertWidthFillsRoot(): SemanticsNodeInteraction {
+ val node = fetchSemanticsNode("Failed to assertWidthFillsScreen")
+ @OptIn(ExperimentalLayoutNodeApi::class)
+ val owner = node.componentNode.owner as AndroidOwner
+ val rootViewWidth = owner.view.width
+
+ with(owner.density) {
+ node.boundsInRoot.width.toDp().assertIsEqualTo(rootViewWidth.toDp())
+ }
+ return this
+}
+
+fun rootWidth(): Dp {
+ val nodeInteraction = findRoot()
+ val node = nodeInteraction.fetchSemanticsNode("Failed to get screen width")
+ @OptIn(ExperimentalLayoutNodeApi::class)
+ val owner = node.componentNode.owner as AndroidOwner
+
+ return with(owner.density) {
+ owner.view.width.toDp()
+ }
+}
+
+fun rootHeight(): Dp {
+ val nodeInteraction = findRoot()
+ val node = nodeInteraction.fetchSemanticsNode("Failed to get screen height")
+ @OptIn(ExperimentalLayoutNodeApi::class)
+ val owner = node.componentNode.owner as AndroidOwner
+
+ return with(owner.density) {
+ owner.view.height.toDp()
+ }
+}
+
+/**
+ * Constant to emulate very big but finite constraints
+ */
+val BigTestConstraints = DpConstraints(maxWidth = 5000.dp, maxHeight = 5000.dp)
+
+fun ComposeTestRule.setMaterialContentForSizeAssertions(
+ parentConstraints: DpConstraints = BigTestConstraints,
+ // TODO : figure out better way to make it flexible
+ children: @Composable () -> Unit
+): SemanticsNodeInteraction {
+ setContent {
+ MaterialTheme {
+ Surface {
+ Stack {
+ Stack(Modifier.preferredSizeIn(parentConstraints)
+ .testTag("containerForSizeAssertion")) {
+ children()
+ }
+ }
+ }
+ }
+ }
+
+ return findByTag("containerForSizeAssertion")
+}
diff --git a/ui/ui-material/src/androidTest/java/androidx/ui/material/ProgressIndicatorTest.kt b/ui/ui-material/src/androidTest/java/androidx/ui/material/ProgressIndicatorTest.kt
index 5c4f208..095ad0e 100644
--- a/ui/ui-material/src/androidTest/java/androidx/ui/material/ProgressIndicatorTest.kt
+++ b/ui/ui-material/src/androidTest/java/androidx/ui/material/ProgressIndicatorTest.kt
@@ -21,9 +21,11 @@
import androidx.ui.core.testTag
import androidx.ui.foundation.Strings
import androidx.ui.semantics.AccessibilityRangeInfo
+import androidx.ui.test.assertHeightIsEqualTo
import androidx.ui.test.assertIsDisplayed
import androidx.ui.test.assertRangeInfoEquals
import androidx.ui.test.assertValueEquals
+import androidx.ui.test.assertWidthIsEqualTo
import androidx.ui.test.createComposeRule
import androidx.ui.test.findByTag
import androidx.ui.test.runOnUiThread
@@ -72,11 +74,11 @@
@Test
fun determinateLinearProgressIndicator_Size() {
composeTestRule
- .setMaterialContentAndCollectSizes {
+ .setMaterialContentForSizeAssertions {
LinearProgressIndicator(progress = 0f)
}
- .assertWidthEqualsTo(ExpectedLinearWidth)
- .assertHeightEqualsTo(ExpectedLinearHeight)
+ .assertWidthIsEqualTo(ExpectedLinearWidth)
+ .assertHeightIsEqualTo(ExpectedLinearHeight)
}
@Test
@@ -97,11 +99,11 @@
@Ignore("b/154757752")
fun indeterminateLinearProgressIndicator_Size() {
composeTestRule
- .setMaterialContentAndCollectSizes {
+ .setMaterialContentForSizeAssertions {
LinearProgressIndicator()
}
- .assertWidthEqualsTo(ExpectedLinearWidth)
- .assertHeightEqualsTo(ExpectedLinearHeight)
+ .assertWidthIsEqualTo(ExpectedLinearWidth)
+ .assertHeightIsEqualTo(ExpectedLinearHeight)
}
@Test
@@ -135,10 +137,10 @@
@Test
fun determinateCircularProgressIndicator_Size() {
composeTestRule
- .setMaterialContentAndCollectSizes {
+ .setMaterialContentForSizeAssertions {
CircularProgressIndicator(progress = 0f)
}
- .assertIsSquareWithSize { 4.dp.toIntPx() * 2 + 40.dp.toIntPx() }
+ .assertIsSquareWithSize(4.dp * 2 + 40.dp)
}
@Test
@@ -159,9 +161,9 @@
@Ignore("b/154757752")
fun indeterminateCircularProgressIndicator_Size() {
composeTestRule
- .setMaterialContentAndCollectSizes {
+ .setMaterialContentForSizeAssertions {
CircularProgressIndicator()
}
- .assertIsSquareWithSize { 4.dp.toIntPx() * 2 + 40.dp.toIntPx() }
+ .assertIsSquareWithSize(4.dp * 2 + 40.dp)
}
}
diff --git a/ui/ui-material/src/androidTest/java/androidx/ui/material/RadioButtonTest.kt b/ui/ui-material/src/androidTest/java/androidx/ui/material/RadioButtonTest.kt
index 4e72037..cd48273 100644
--- a/ui/ui-material/src/androidTest/java/androidx/ui/material/RadioButtonTest.kt
+++ b/ui/ui-material/src/androidTest/java/androidx/ui/material/RadioButtonTest.kt
@@ -182,9 +182,9 @@
private fun materialSizesTestForValue(selected: Boolean) {
composeTestRule
- .setMaterialContentAndCollectSizes {
+ .setMaterialContentForSizeAssertions {
RadioButton(selected = selected, >
}
- .assertIsSquareWithSize { 2.dp.toIntPx() * 2 + 20.dp.toIntPx() }
+ .assertIsSquareWithSize(2.dp * 2 + 20.dp)
}
}
\ No newline at end of file
diff --git a/ui/ui-material/src/androidTest/java/androidx/ui/material/ScaffoldTest.kt b/ui/ui-material/src/androidTest/java/androidx/ui/material/ScaffoldTest.kt
index bec8633..3c55c35 100644
--- a/ui/ui-material/src/androidTest/java/androidx/ui/material/ScaffoldTest.kt
+++ b/ui/ui-material/src/androidTest/java/androidx/ui/material/ScaffoldTest.kt
@@ -42,6 +42,8 @@
import androidx.ui.layout.size
import androidx.ui.material.icons.Icons
import androidx.ui.material.icons.filled.Favorite
+import androidx.ui.test.assertHeightIsEqualTo
+import androidx.ui.test.assertWidthIsEqualTo
import androidx.ui.test.captureToBitmap
import androidx.ui.test.createComposeRule
import androidx.ui.test.doGesture
@@ -72,15 +74,15 @@
@Test
fun scaffold_onlyContent_takesWholeScreen() {
- composeTestRule.setMaterialContentAndCollectSizes(
+ composeTestRule.setMaterialContentForSizeAssertions(
parentConstraints = DpConstraints(maxWidth = 100.dp, maxHeight = 100.dp)
) {
Scaffold {
Text("Scaffold body")
}
}
- .assertWidthEqualsTo(100.dp)
- .assertHeightEqualsTo(100.dp)
+ .assertWidthIsEqualTo(100.dp)
+ .assertHeightIsEqualTo(100.dp)
}
@Test
diff --git a/ui/ui-material/src/androidTest/java/androidx/ui/material/SliderTest.kt b/ui/ui-material/src/androidTest/java/androidx/ui/material/SliderTest.kt
index 7134415..4fc7d44 100644
--- a/ui/ui-material/src/androidTest/java/androidx/ui/material/SliderTest.kt
+++ b/ui/ui-material/src/androidTest/java/androidx/ui/material/SliderTest.kt
@@ -31,7 +31,9 @@
import androidx.ui.layout.DpConstraints
import androidx.ui.layout.ltr
import androidx.ui.layout.rtl
+import androidx.ui.test.assertHeightIsEqualTo
import androidx.ui.test.assertValueEquals
+import androidx.ui.test.assertWidthIsEqualTo
import androidx.ui.test.captureToBitmap
import androidx.ui.test.center
import androidx.ui.test.centerX
@@ -246,11 +248,11 @@
fun slider_sizes() {
val state = mutableStateOf(0f)
composeTestRule
- .setMaterialContentAndCollectSizes(
+ .setMaterialContentForSizeAssertions(
parentConstraints = DpConstraints(maxWidth = 100.dp, maxHeight = 100.dp)
) { Slider(value = state.value, state.value = it }) }
- .assertHeightEqualsTo(48.dp)
- .assertWidthEqualsTo(100.dp)
+ .assertHeightIsEqualTo(48.dp)
+ .assertWidthIsEqualTo(100.dp)
}
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.O)
diff --git a/ui/ui-material/src/androidTest/java/androidx/ui/material/SnackbarTest.kt b/ui/ui-material/src/androidTest/java/androidx/ui/material/SnackbarTest.kt
index 9e52a90..d91be77 100644
--- a/ui/ui-material/src/androidTest/java/androidx/ui/material/SnackbarTest.kt
+++ b/ui/ui-material/src/androidTest/java/androidx/ui/material/SnackbarTest.kt
@@ -90,16 +90,15 @@
@Test
fun snackbar_shortTextOnly_defaultSizes() {
- composeTestRule.setMaterialContentWithConstraints(DpConstraints(maxWidth = 300.dp)) {
+ val snackbar = composeTestRule.setMaterialContentForSizeAssertions(
+ DpConstraints(maxWidth = 300.dp)
+ ) {
Snackbar(
- modifier = Modifier.testTag("snackbar"),
text = {
Text("Message")
}
)
}
-
- findByTag("snackbar")
.assertWidthIsEqualTo(300.dp)
.assertHeightIsEqualTo(48.dp)
@@ -108,7 +107,7 @@
firstBaseLine.assertIsNotEqualTo(0.dp, "first baseline")
firstBaseLine.assertIsEqualTo(lastBaseLine, "first baseline")
- val snackBounds = findByTag("snackbar").getBoundsInRoot()
+ val snackBounds = snackbar.getBoundsInRoot()
val textBounds = findByText("Message").getBoundsInRoot()
val textTopOffset = textBounds.top - snackBounds.top
@@ -119,16 +118,15 @@
@Test
fun snackbar_shortTextOnly_bigFont_centered() {
- composeTestRule.setMaterialContentWithConstraints(DpConstraints(maxWidth = 300.dp)) {
+ val snackbar = composeTestRule.setMaterialContentForSizeAssertions(
+ DpConstraints(maxWidth = 300.dp)
+ ) {
Snackbar(
- modifier = Modifier.testTag("snackbar"),
text = {
Text("Message", fontSize = 30.sp)
}
)
}
-
- findByTag("snackbar")
.assertWidthIsEqualTo(300.dp)
val firstBaseLine = findByText("Message").getAlignmentLinePosition(FirstBaseline)
@@ -136,7 +134,7 @@
firstBaseLine.assertIsNotEqualTo(0.dp, "first baseline")
firstBaseLine.assertIsEqualTo(lastBaseLine, "first baseline")
- val snackBounds = findByTag("snackbar").getBoundsInRoot()
+ val snackBounds = snackbar.getBoundsInRoot()
val textBounds = findByText("Message").getBoundsInRoot()
val textTopOffset = textBounds.top - snackBounds.top
@@ -147,9 +145,10 @@
@Test
fun snackbar_shortTextAndButton_alignment() {
- composeTestRule.setMaterialContentWithConstraints(DpConstraints(maxWidth = 300.dp)) {
+ val snackbar = composeTestRule.setMaterialContentForSizeAssertions(
+ DpConstraints(maxWidth = 300.dp)
+ ) {
Snackbar(
- modifier = Modifier.testTag("snackbar"),
text = {
Text("Message")
},
@@ -163,8 +162,6 @@
}
)
}
-
- findByTag("snackbar")
.assertWidthIsEqualTo(300.dp)
.assertHeightIsEqualTo(48.dp)
@@ -173,7 +170,7 @@
textBaseLine.assertIsNotEqualTo(0.dp, "text baseline")
buttonBaseLine.assertIsNotEqualTo(0.dp, "button baseline")
- val snackBounds = findByTag("snackbar").getBoundsInRoot()
+ val snackBounds = snackbar.getBoundsInRoot()
val textBounds = findByText("Message").getBoundsInRoot()
val buttonBounds = findByText("Undo").getBoundsInRoot()
@@ -187,10 +184,11 @@
@Test
fun snackbar_shortTextAndButton_bigFont_alignment() {
- composeTestRule.setMaterialContentWithConstraints(DpConstraints(maxWidth = 400.dp)) {
+ val snackbar = composeTestRule.setMaterialContentForSizeAssertions(
+ DpConstraints(maxWidth = 400.dp)
+ ) {
val fontSize = 30.sp
Snackbar(
- modifier = Modifier.testTag("snackbar"),
text = {
Text("Message", fontSize = fontSize)
},
@@ -210,7 +208,7 @@
textBaseLine.assertIsNotEqualTo(0.dp, "text baseline")
buttonBaseLine.assertIsNotEqualTo(0.dp, "button baseline")
- val snackBounds = findByTag("snackbar").getBoundsInRoot()
+ val snackBounds = snackbar.getBoundsInRoot()
val textBounds = findByText("Message").getBoundsInRoot()
val buttonBounds = findByText("Undo").getBoundsInRoot()
@@ -224,16 +222,15 @@
@Test
fun snackbar_longText_sizes() {
- composeTestRule.setMaterialContentWithConstraints(DpConstraints(maxWidth = 300.dp)) {
+ val snackbar = composeTestRule.setMaterialContentForSizeAssertions(
+ DpConstraints(maxWidth = 300.dp)
+ ) {
Snackbar(
- modifier = Modifier.testTag("snackbar"),
text = {
Text(longText, Modifier.testTag("text"), maxLines = 2)
}
)
}
-
- findByTag("snackbar")
.assertWidthIsEqualTo(300.dp)
.assertHeightIsEqualTo(68.dp)
@@ -244,7 +241,7 @@
lastBaseline.assertIsNotEqualTo(0.dp, "last baseline")
firstBaseline.assertIsNotEqualTo(lastBaseline, "first baseline")
- val snackBounds = findByTag("snackbar").getBoundsInRoot()
+ val snackBounds = snackbar.getBoundsInRoot()
val textBounds = findByTag("text").getBoundsInRoot()
val textTopOffset = textBounds.top - snackBounds.top
@@ -255,9 +252,10 @@
@Test
fun snackbar_longTextAndButton_alignment() {
- composeTestRule.setMaterialContentWithConstraints(DpConstraints(maxWidth = 300.dp)) {
+ val snackbar = composeTestRule.setMaterialContentForSizeAssertions(
+ DpConstraints(maxWidth = 300.dp)
+ ) {
Snackbar(
- modifier = Modifier.testTag("snackbar"),
text = {
Text(longText, Modifier.testTag("text"), maxLines = 2)
},
@@ -271,8 +269,6 @@
}
)
}
-
- findByTag("snackbar")
.assertWidthIsEqualTo(300.dp)
.assertHeightIsEqualTo(68.dp)
@@ -287,7 +283,7 @@
.assertTopPositionInRootIsEqualTo(30.dp - textFirstBaseLine)
val buttonBounds = findByTag("button").getBoundsInRoot()
- val snackBounds = findByTag("snackbar").getBoundsInRoot()
+ val snackBounds = snackbar.getBoundsInRoot()
val buttonCenter = buttonBounds.top + (buttonBounds.height / 2)
buttonCenter.assertIsEqualTo(snackBounds.height / 2, "button center")
@@ -295,9 +291,10 @@
@Test
fun snackbar_textAndButtonOnSeparateLine_alignment() {
- composeTestRule.setMaterialContentWithConstraints(DpConstraints(maxWidth = 300.dp)) {
+ val snackbar = composeTestRule.setMaterialContentForSizeAssertions(
+ DpConstraints(maxWidth = 300.dp)
+ ) {
Snackbar(
- modifier = Modifier.testTag("snackbar"),
text = {
Text("Message")
},
@@ -324,7 +321,7 @@
findByTag("button")
.assertTopPositionInRootIsEqualTo(18.dp + textBounds.top + textLastBaseLine)
- findByTag("snackbar")
+ snackbar
.assertHeightIsEqualTo(8.dp + buttonBounds.top + buttonBounds.height)
.assertWidthIsEqualTo(8.dp + buttonBounds.left + buttonBounds.width)
}
diff --git a/ui/ui-material/src/androidTest/java/androidx/ui/material/SwitchTest.kt b/ui/ui-material/src/androidTest/java/androidx/ui/material/SwitchTest.kt
index c3f4c57..19f3247 100644
--- a/ui/ui-material/src/androidTest/java/androidx/ui/material/SwitchTest.kt
+++ b/ui/ui-material/src/androidTest/java/androidx/ui/material/SwitchTest.kt
@@ -26,10 +26,12 @@
import androidx.ui.layout.Stack
import androidx.ui.layout.rtl
import androidx.ui.test.assertHasNoClickAction
+import androidx.ui.test.assertHeightIsEqualTo
import androidx.ui.test.assertIsEnabled
import androidx.ui.test.assertIsOff
import androidx.ui.test.assertIsOn
import androidx.ui.test.assertValueEquals
+import androidx.ui.test.assertWidthIsEqualTo
import androidx.ui.test.createComposeRule
import androidx.ui.test.doClick
import androidx.ui.test.doGesture
@@ -204,11 +206,10 @@
}
private fun materialSizesTestForValue(checked: Boolean) {
- composeTestRule
- .setMaterialContentAndCollectSizes {
- Switch(checked = checked, enabled = false)
- }
- .assertWidthEqualsTo { 34.dp.toIntPx() + 2.dp.toIntPx() * 2 }
- .assertHeightEqualsTo { 20.dp.toIntPx() + 2.dp.toIntPx() * 2 }
+ composeTestRule.setMaterialContentForSizeAssertions {
+ Switch(checked = checked, enabled = false)
+ }
+ .assertWidthIsEqualTo(34.dp + 2.dp * 2)
+ .assertHeightIsEqualTo(20.dp + 2.dp * 2)
}
}
diff --git a/ui/ui-material/src/androidTest/java/androidx/ui/material/TabTest.kt b/ui/ui-material/src/androidTest/java/androidx/ui/material/TabTest.kt
index 6adacc9..27958ae 100644
--- a/ui/ui-material/src/androidTest/java/androidx/ui/material/TabTest.kt
+++ b/ui/ui-material/src/androidTest/java/androidx/ui/material/TabTest.kt
@@ -67,32 +67,26 @@
@Test
fun textTab_height() {
- composeTestRule.setMaterialContent {
- Box(Modifier.testTag("tab")) {
+ composeTestRule
+ .setMaterialContentForSizeAssertions {
Tab(text = { Text("Text") }, selected = true, >
}
- }
-
- findByTag("tab")
.assertHeightIsEqualTo(ExpectedSmallTabHeight)
}
@Test
fun iconTab_height() {
- composeTestRule.setMaterialContent {
- Box(Modifier.testTag("tab")) {
+ composeTestRule
+ .setMaterialContentForSizeAssertions {
Tab(icon = { Icon(icon) }, selected = true, >
}
- }
-
- findByTag("tab")
.assertHeightIsEqualTo(ExpectedSmallTabHeight)
}
@Test
fun textAndIconTab_height() {
- composeTestRule.setMaterialContent {
- Box(Modifier.testTag("tab")) {
+ composeTestRule
+ .setMaterialContentForSizeAssertions {
Surface {
Tab(
text = { Text("Text and Icon") },
@@ -102,9 +96,6 @@
)
}
}
- }
-
- findByTag("tab")
.assertHeightIsEqualTo(ExpectedLargeTabHeight)
}
diff --git a/ui/ui-material/src/androidTest/java/androidx/ui/material/textfield/TextFieldTest.kt b/ui/ui-material/src/androidTest/java/androidx/ui/material/textfield/TextFieldTest.kt
index 4ce7f55..1c4a808 100644
--- a/ui/ui-material/src/androidTest/java/androidx/ui/material/textfield/TextFieldTest.kt
+++ b/ui/ui-material/src/androidTest/java/androidx/ui/material/textfield/TextFieldTest.kt
@@ -25,7 +25,6 @@
import androidx.ui.core.Modifier
import androidx.ui.core.Ref
import androidx.ui.core.TextInputServiceAmbient
-import androidx.ui.core.globalPosition
import androidx.ui.core.onPositioned
import androidx.ui.core.positionInRoot
import androidx.ui.core.testTag
@@ -49,8 +48,18 @@
import androidx.ui.layout.height
import androidx.ui.layout.preferredHeight
import androidx.ui.layout.preferredSize
+import androidx.ui.layout.preferredWidth
+import androidx.ui.material.FilledTextField
+import androidx.ui.material.MaterialTheme
+import androidx.ui.material.OutlinedTextField
+import androidx.ui.material.TextFieldScroller
+import androidx.ui.material.TextFieldScrollerPosition
+import androidx.ui.material.runOnIdleComposeWithDensity
+import androidx.ui.material.setMaterialContent
+import androidx.ui.material.setMaterialContentForSizeAssertions
import androidx.ui.savedinstancestate.rememberSavedInstanceState
import androidx.ui.test.StateRestorationTester
+import androidx.ui.test.assertHeightIsEqualTo
import androidx.ui.test.assertPixels
import androidx.ui.test.assertShape
import androidx.ui.test.captureToBitmap
@@ -82,15 +91,6 @@
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import kotlin.math.roundToInt
-import androidx.ui.layout.preferredWidth
-import androidx.ui.material.FilledTextField
-import androidx.ui.material.MaterialTheme
-import androidx.ui.material.OutlinedTextField
-import androidx.ui.material.TextFieldScroller
-import androidx.ui.material.TextFieldScrollerPosition
-import androidx.ui.material.runOnIdleComposeWithDensity
-import androidx.ui.material.setMaterialContent
-import androidx.ui.material.setMaterialContentAndCollectSizes
@MediumTest
@RunWith(JUnit4::class)
@@ -115,16 +115,15 @@
@Test
fun testFilledTextField_minimumHeight() {
- testRule
- .setMaterialContentAndCollectSizes {
- FilledTextField(
- value = "input",
- >
- label = {},
- modifier = Modifier.preferredHeight(20.dp)
- )
- }
- .assertHeightEqualsTo(ExpectedMinimumTextFieldHeight)
+ testRule.setMaterialContentForSizeAssertions {
+ FilledTextField(
+ value = "input",
+ >
+ label = {},
+ modifier = Modifier.preferredHeight(20.dp)
+ )
+ }
+ .assertHeightIsEqualTo(ExpectedMinimumTextFieldHeight)
}
@Test
@@ -254,7 +253,7 @@
fontSize = 10.sp,
modifier = Modifier
.onPositioned {
- labelPosition.value = it.globalPosition
+ labelPosition.value = it.positionInRoot
labelSize.value = it.size
}
)
@@ -291,7 +290,7 @@
>
label = {
Text(text = "label", modifier = Modifier.onPositioned {
- labelPosition.value = it.globalPosition
+ labelPosition.value = it.positionInRoot
labelSize.value = it.size
})
}
@@ -328,7 +327,7 @@
modifier = Modifier.preferredHeight(height),
label = {
Text(text = "label", modifier = Modifier.onPositioned {
- labelPosition.value = it.globalPosition
+ labelPosition.value = it.positionInRoot
labelSize.value = it.size
})
}
@@ -403,7 +402,7 @@
>
label = {
Text(text = "label", modifier = Modifier.onPositioned {
- labelPosition.value = it.globalPosition
+ labelPosition.value = it.positionInRoot
labelSize.value = it.size
})
}
@@ -473,7 +472,7 @@
>
label = {
Text(text = "label", modifier = Modifier.onPositioned {
- labelPosition.value = it.globalPosition
+ labelPosition.value = it.positionInRoot
labelSize.value = it.size
})
}
@@ -548,7 +547,7 @@
label = { Text("label") },
placeholder = {
Text(text = "placeholder", modifier = Modifier.onPositioned {
- placeholderPosition.value = it.globalPosition
+ placeholderPosition.value = it.positionInRoot
placeholderSize.value = it.size
})
}
@@ -631,7 +630,7 @@
label = {},
placeholder = {
Text(text = "placeholder", modifier = Modifier.onPositioned {
- placeholderPosition.value = it.globalPosition
+ placeholderPosition.value = it.positionInRoot
placeholderSize.value = it.size
})
}
@@ -673,7 +672,7 @@
label = {},
placeholder = {
Text(text = "placeholder", modifier = Modifier.onPositioned {
- placeholderPosition.value = it.globalPosition
+ placeholderPosition.value = it.positionInRoot
placeholderSize.value = it.size
})
}
@@ -703,7 +702,7 @@
label = {},
placeholder = {
Text(text = "placeholder", modifier = Modifier.onPositioned {
- placeholderPosition.value = it.globalPosition
+ placeholderPosition.value = it.positionInRoot
placeholderSize.value = it.size
})
}
@@ -788,13 +787,13 @@
label = {},
leadingIcon = {
Box(Modifier.preferredSize(size).onPositioned {
- leadingPosition.value = it.globalPosition
+ leadingPosition.value = it.positionInRoot
leadingSize.value = it.size
})
},
trailingIcon = {
Box(Modifier.preferredSize(size).onPositioned {
- trailingPosition.value = it.globalPosition
+ trailingPosition.value = it.positionInRoot
trailingSize.value = it.size
})
}
@@ -840,13 +839,13 @@
label = {},
leadingIcon = {
Box(Modifier.preferredSize(size).onPositioned {
- leadingPosition.value = it.globalPosition
+ leadingPosition.value = it.positionInRoot
leadingSize.value = it.size
})
},
trailingIcon = {
Box(Modifier.preferredSize(size).onPositioned {
- trailingPosition.value = it.globalPosition
+ trailingPosition.value = it.positionInRoot
trailingSize.value = it.size
})
}
@@ -886,7 +885,7 @@
modifier = Modifier.preferredHeight(height),
label = {
Text(text = "label", modifier = Modifier.onPositioned {
- labelPosition.value = it.globalPosition
+ labelPosition.value = it.positionInRoot
})
},
trailingIcon = { Box(Modifier.preferredSize(iconSize)) },
@@ -914,7 +913,7 @@
>
label = {
Text(text = "label", modifier = Modifier.onPositioned {
- labelPosition.value = it.globalPosition
+ labelPosition.value = it.positionInRoot
})
},
trailingIcon = { Box(Modifier.preferredSize(iconSize)) },
@@ -943,7 +942,7 @@
modifier = Modifier.preferredHeight(height),
label = {
Text(text = "label", modifier = Modifier.onPositioned {
- labelPosition.value = it.globalPosition
+ labelPosition.value = it.positionInRoot
})
},
trailingIcon = {},
@@ -969,7 +968,7 @@
>
label = {
Text(text = "label", modifier = Modifier.onPositioned {
- labelPosition.value = it.globalPosition
+ labelPosition.value = it.positionInRoot
})
},
trailingIcon = {},
diff --git a/ui/ui-test/api/0.1.0-dev15.txt b/ui/ui-test/api/0.1.0-dev15.txt
index eb16d6d..ee16986 100644
--- a/ui/ui-test/api/0.1.0-dev15.txt
+++ b/ui/ui-test/api/0.1.0-dev15.txt
@@ -75,16 +75,6 @@
method public static androidx.ui.unit.Bounds getBoundsInRoot(androidx.ui.test.SemanticsNodeInteraction);
}
- public final class CollectedSizes {
- ctor public CollectedSizes(androidx.ui.geometry.Size size, androidx.ui.unit.Density density);
- method public androidx.ui.test.CollectedSizes assertHeightEqualsTo(kotlin.jvm.functions.Function1<? super androidx.ui.unit.Density,java.lang.Integer> expectedHeightPx);
- method public androidx.ui.test.CollectedSizes assertHeightEqualsTo-ipo6vVg(float expectedHeight);
- method public androidx.ui.test.CollectedSizes assertIsSquareWithSize(kotlin.jvm.functions.Function1<? super androidx.ui.unit.Density,java.lang.Integer> expectedSquarePx);
- method public androidx.ui.test.CollectedSizes assertIsSquareWithSize-ipo6vVg(float expectedSize);
- method public androidx.ui.test.CollectedSizes assertWidthEqualsTo(kotlin.jvm.functions.Function1<? super androidx.ui.unit.Density,java.lang.Integer> expectedWidthPx);
- method public androidx.ui.test.CollectedSizes assertWidthEqualsTo-ipo6vVg(float expectedWidth);
- }
-
public interface ComposeBenchmarkScope<T> extends androidx.ui.test.ComposeExecutionControl {
method public void createTestCase();
method public void disposeContent();
@@ -336,12 +326,6 @@
public final class SemanticsSelectorKt {
}
- public final class SizesTestingKt {
- method public static androidx.ui.layout.DpConstraints getBigTestConstraints();
- method public static androidx.ui.test.CollectedSizes setContentAndCollectSizes(androidx.ui.test.ComposeTestRule, androidx.ui.layout.DpConstraints parentConstraints = BigTestConstraints, kotlin.jvm.functions.Function0<kotlin.Unit> children);
- method public static androidx.ui.geometry.Size setContentAndGetPixelSize(androidx.ui.test.ComposeTestRule, androidx.ui.layout.DpConstraints parentConstraints = BigTestConstraints, kotlin.jvm.functions.Function1<? super kotlin.jvm.functions.Function0<kotlin.Unit>,kotlin.Unit> performSetContent = { setContent(it) }, kotlin.jvm.functions.Function0<kotlin.Unit> children);
- }
-
public final class StateRestorationTester {
ctor public StateRestorationTester(androidx.ui.test.ComposeTestRule composeTestRule);
method public void emulateSavedInstanceStateRestore();
diff --git a/ui/ui-test/api/current.txt b/ui/ui-test/api/current.txt
index eb16d6d..ee16986 100644
--- a/ui/ui-test/api/current.txt
+++ b/ui/ui-test/api/current.txt
@@ -75,16 +75,6 @@
method public static androidx.ui.unit.Bounds getBoundsInRoot(androidx.ui.test.SemanticsNodeInteraction);
}
- public final class CollectedSizes {
- ctor public CollectedSizes(androidx.ui.geometry.Size size, androidx.ui.unit.Density density);
- method public androidx.ui.test.CollectedSizes assertHeightEqualsTo(kotlin.jvm.functions.Function1<? super androidx.ui.unit.Density,java.lang.Integer> expectedHeightPx);
- method public androidx.ui.test.CollectedSizes assertHeightEqualsTo-ipo6vVg(float expectedHeight);
- method public androidx.ui.test.CollectedSizes assertIsSquareWithSize(kotlin.jvm.functions.Function1<? super androidx.ui.unit.Density,java.lang.Integer> expectedSquarePx);
- method public androidx.ui.test.CollectedSizes assertIsSquareWithSize-ipo6vVg(float expectedSize);
- method public androidx.ui.test.CollectedSizes assertWidthEqualsTo(kotlin.jvm.functions.Function1<? super androidx.ui.unit.Density,java.lang.Integer> expectedWidthPx);
- method public androidx.ui.test.CollectedSizes assertWidthEqualsTo-ipo6vVg(float expectedWidth);
- }
-
public interface ComposeBenchmarkScope<T> extends androidx.ui.test.ComposeExecutionControl {
method public void createTestCase();
method public void disposeContent();
@@ -336,12 +326,6 @@
public final class SemanticsSelectorKt {
}
- public final class SizesTestingKt {
- method public static androidx.ui.layout.DpConstraints getBigTestConstraints();
- method public static androidx.ui.test.CollectedSizes setContentAndCollectSizes(androidx.ui.test.ComposeTestRule, androidx.ui.layout.DpConstraints parentConstraints = BigTestConstraints, kotlin.jvm.functions.Function0<kotlin.Unit> children);
- method public static androidx.ui.geometry.Size setContentAndGetPixelSize(androidx.ui.test.ComposeTestRule, androidx.ui.layout.DpConstraints parentConstraints = BigTestConstraints, kotlin.jvm.functions.Function1<? super kotlin.jvm.functions.Function0<kotlin.Unit>,kotlin.Unit> performSetContent = { setContent(it) }, kotlin.jvm.functions.Function0<kotlin.Unit> children);
- }
-
public final class StateRestorationTester {
ctor public StateRestorationTester(androidx.ui.test.ComposeTestRule composeTestRule);
method public void emulateSavedInstanceStateRestore();
diff --git a/ui/ui-test/api/public_plus_experimental_0.1.0-dev15.txt b/ui/ui-test/api/public_plus_experimental_0.1.0-dev15.txt
index eb16d6d..ee16986 100644
--- a/ui/ui-test/api/public_plus_experimental_0.1.0-dev15.txt
+++ b/ui/ui-test/api/public_plus_experimental_0.1.0-dev15.txt
@@ -75,16 +75,6 @@
method public static androidx.ui.unit.Bounds getBoundsInRoot(androidx.ui.test.SemanticsNodeInteraction);
}
- public final class CollectedSizes {
- ctor public CollectedSizes(androidx.ui.geometry.Size size, androidx.ui.unit.Density density);
- method public androidx.ui.test.CollectedSizes assertHeightEqualsTo(kotlin.jvm.functions.Function1<? super androidx.ui.unit.Density,java.lang.Integer> expectedHeightPx);
- method public androidx.ui.test.CollectedSizes assertHeightEqualsTo-ipo6vVg(float expectedHeight);
- method public androidx.ui.test.CollectedSizes assertIsSquareWithSize(kotlin.jvm.functions.Function1<? super androidx.ui.unit.Density,java.lang.Integer> expectedSquarePx);
- method public androidx.ui.test.CollectedSizes assertIsSquareWithSize-ipo6vVg(float expectedSize);
- method public androidx.ui.test.CollectedSizes assertWidthEqualsTo(kotlin.jvm.functions.Function1<? super androidx.ui.unit.Density,java.lang.Integer> expectedWidthPx);
- method public androidx.ui.test.CollectedSizes assertWidthEqualsTo-ipo6vVg(float expectedWidth);
- }
-
public interface ComposeBenchmarkScope<T> extends androidx.ui.test.ComposeExecutionControl {
method public void createTestCase();
method public void disposeContent();
@@ -336,12 +326,6 @@
public final class SemanticsSelectorKt {
}
- public final class SizesTestingKt {
- method public static androidx.ui.layout.DpConstraints getBigTestConstraints();
- method public static androidx.ui.test.CollectedSizes setContentAndCollectSizes(androidx.ui.test.ComposeTestRule, androidx.ui.layout.DpConstraints parentConstraints = BigTestConstraints, kotlin.jvm.functions.Function0<kotlin.Unit> children);
- method public static androidx.ui.geometry.Size setContentAndGetPixelSize(androidx.ui.test.ComposeTestRule, androidx.ui.layout.DpConstraints parentConstraints = BigTestConstraints, kotlin.jvm.functions.Function1<? super kotlin.jvm.functions.Function0<kotlin.Unit>,kotlin.Unit> performSetContent = { setContent(it) }, kotlin.jvm.functions.Function0<kotlin.Unit> children);
- }
-
public final class StateRestorationTester {
ctor public StateRestorationTester(androidx.ui.test.ComposeTestRule composeTestRule);
method public void emulateSavedInstanceStateRestore();
diff --git a/ui/ui-test/api/public_plus_experimental_current.txt b/ui/ui-test/api/public_plus_experimental_current.txt
index eb16d6d..ee16986 100644
--- a/ui/ui-test/api/public_plus_experimental_current.txt
+++ b/ui/ui-test/api/public_plus_experimental_current.txt
@@ -75,16 +75,6 @@
method public static androidx.ui.unit.Bounds getBoundsInRoot(androidx.ui.test.SemanticsNodeInteraction);
}
- public final class CollectedSizes {
- ctor public CollectedSizes(androidx.ui.geometry.Size size, androidx.ui.unit.Density density);
- method public androidx.ui.test.CollectedSizes assertHeightEqualsTo(kotlin.jvm.functions.Function1<? super androidx.ui.unit.Density,java.lang.Integer> expectedHeightPx);
- method public androidx.ui.test.CollectedSizes assertHeightEqualsTo-ipo6vVg(float expectedHeight);
- method public androidx.ui.test.CollectedSizes assertIsSquareWithSize(kotlin.jvm.functions.Function1<? super androidx.ui.unit.Density,java.lang.Integer> expectedSquarePx);
- method public androidx.ui.test.CollectedSizes assertIsSquareWithSize-ipo6vVg(float expectedSize);
- method public androidx.ui.test.CollectedSizes assertWidthEqualsTo(kotlin.jvm.functions.Function1<? super androidx.ui.unit.Density,java.lang.Integer> expectedWidthPx);
- method public androidx.ui.test.CollectedSizes assertWidthEqualsTo-ipo6vVg(float expectedWidth);
- }
-
public interface ComposeBenchmarkScope<T> extends androidx.ui.test.ComposeExecutionControl {
method public void createTestCase();
method public void disposeContent();
@@ -336,12 +326,6 @@
public final class SemanticsSelectorKt {
}
- public final class SizesTestingKt {
- method public static androidx.ui.layout.DpConstraints getBigTestConstraints();
- method public static androidx.ui.test.CollectedSizes setContentAndCollectSizes(androidx.ui.test.ComposeTestRule, androidx.ui.layout.DpConstraints parentConstraints = BigTestConstraints, kotlin.jvm.functions.Function0<kotlin.Unit> children);
- method public static androidx.ui.geometry.Size setContentAndGetPixelSize(androidx.ui.test.ComposeTestRule, androidx.ui.layout.DpConstraints parentConstraints = BigTestConstraints, kotlin.jvm.functions.Function1<? super kotlin.jvm.functions.Function0<kotlin.Unit>,kotlin.Unit> performSetContent = { setContent(it) }, kotlin.jvm.functions.Function0<kotlin.Unit> children);
- }
-
public final class StateRestorationTester {
ctor public StateRestorationTester(androidx.ui.test.ComposeTestRule composeTestRule);
method public void emulateSavedInstanceStateRestore();
diff --git a/ui/ui-test/api/restricted_0.1.0-dev15.txt b/ui/ui-test/api/restricted_0.1.0-dev15.txt
index eb16d6d..ee16986 100644
--- a/ui/ui-test/api/restricted_0.1.0-dev15.txt
+++ b/ui/ui-test/api/restricted_0.1.0-dev15.txt
@@ -75,16 +75,6 @@
method public static androidx.ui.unit.Bounds getBoundsInRoot(androidx.ui.test.SemanticsNodeInteraction);
}
- public final class CollectedSizes {
- ctor public CollectedSizes(androidx.ui.geometry.Size size, androidx.ui.unit.Density density);
- method public androidx.ui.test.CollectedSizes assertHeightEqualsTo(kotlin.jvm.functions.Function1<? super androidx.ui.unit.Density,java.lang.Integer> expectedHeightPx);
- method public androidx.ui.test.CollectedSizes assertHeightEqualsTo-ipo6vVg(float expectedHeight);
- method public androidx.ui.test.CollectedSizes assertIsSquareWithSize(kotlin.jvm.functions.Function1<? super androidx.ui.unit.Density,java.lang.Integer> expectedSquarePx);
- method public androidx.ui.test.CollectedSizes assertIsSquareWithSize-ipo6vVg(float expectedSize);
- method public androidx.ui.test.CollectedSizes assertWidthEqualsTo(kotlin.jvm.functions.Function1<? super androidx.ui.unit.Density,java.lang.Integer> expectedWidthPx);
- method public androidx.ui.test.CollectedSizes assertWidthEqualsTo-ipo6vVg(float expectedWidth);
- }
-
public interface ComposeBenchmarkScope<T> extends androidx.ui.test.ComposeExecutionControl {
method public void createTestCase();
method public void disposeContent();
@@ -336,12 +326,6 @@
public final class SemanticsSelectorKt {
}
- public final class SizesTestingKt {
- method public static androidx.ui.layout.DpConstraints getBigTestConstraints();
- method public static androidx.ui.test.CollectedSizes setContentAndCollectSizes(androidx.ui.test.ComposeTestRule, androidx.ui.layout.DpConstraints parentConstraints = BigTestConstraints, kotlin.jvm.functions.Function0<kotlin.Unit> children);
- method public static androidx.ui.geometry.Size setContentAndGetPixelSize(androidx.ui.test.ComposeTestRule, androidx.ui.layout.DpConstraints parentConstraints = BigTestConstraints, kotlin.jvm.functions.Function1<? super kotlin.jvm.functions.Function0<kotlin.Unit>,kotlin.Unit> performSetContent = { setContent(it) }, kotlin.jvm.functions.Function0<kotlin.Unit> children);
- }
-
public final class StateRestorationTester {
ctor public StateRestorationTester(androidx.ui.test.ComposeTestRule composeTestRule);
method public void emulateSavedInstanceStateRestore();
diff --git a/ui/ui-test/api/restricted_current.txt b/ui/ui-test/api/restricted_current.txt
index eb16d6d..ee16986 100644
--- a/ui/ui-test/api/restricted_current.txt
+++ b/ui/ui-test/api/restricted_current.txt
@@ -75,16 +75,6 @@
method public static androidx.ui.unit.Bounds getBoundsInRoot(androidx.ui.test.SemanticsNodeInteraction);
}
- public final class CollectedSizes {
- ctor public CollectedSizes(androidx.ui.geometry.Size size, androidx.ui.unit.Density density);
- method public androidx.ui.test.CollectedSizes assertHeightEqualsTo(kotlin.jvm.functions.Function1<? super androidx.ui.unit.Density,java.lang.Integer> expectedHeightPx);
- method public androidx.ui.test.CollectedSizes assertHeightEqualsTo-ipo6vVg(float expectedHeight);
- method public androidx.ui.test.CollectedSizes assertIsSquareWithSize(kotlin.jvm.functions.Function1<? super androidx.ui.unit.Density,java.lang.Integer> expectedSquarePx);
- method public androidx.ui.test.CollectedSizes assertIsSquareWithSize-ipo6vVg(float expectedSize);
- method public androidx.ui.test.CollectedSizes assertWidthEqualsTo(kotlin.jvm.functions.Function1<? super androidx.ui.unit.Density,java.lang.Integer> expectedWidthPx);
- method public androidx.ui.test.CollectedSizes assertWidthEqualsTo-ipo6vVg(float expectedWidth);
- }
-
public interface ComposeBenchmarkScope<T> extends androidx.ui.test.ComposeExecutionControl {
method public void createTestCase();
method public void disposeContent();
@@ -336,12 +326,6 @@
public final class SemanticsSelectorKt {
}
- public final class SizesTestingKt {
- method public static androidx.ui.layout.DpConstraints getBigTestConstraints();
- method public static androidx.ui.test.CollectedSizes setContentAndCollectSizes(androidx.ui.test.ComposeTestRule, androidx.ui.layout.DpConstraints parentConstraints = BigTestConstraints, kotlin.jvm.functions.Function0<kotlin.Unit> children);
- method public static androidx.ui.geometry.Size setContentAndGetPixelSize(androidx.ui.test.ComposeTestRule, androidx.ui.layout.DpConstraints parentConstraints = BigTestConstraints, kotlin.jvm.functions.Function1<? super kotlin.jvm.functions.Function0<kotlin.Unit>,kotlin.Unit> performSetContent = { setContent(it) }, kotlin.jvm.functions.Function0<kotlin.Unit> children);
- }
-
public final class StateRestorationTester {
ctor public StateRestorationTester(androidx.ui.test.ComposeTestRule composeTestRule);
method public void emulateSavedInstanceStateRestore();
diff --git a/ui/ui-test/src/androidTest/java/androidx/ui/test/AssertsTest.kt b/ui/ui-test/src/androidTest/java/androidx/ui/test/AssertsTest.kt
index da5e522..9eb6a66 100644
--- a/ui/ui-test/src/androidTest/java/androidx/ui/test/AssertsTest.kt
+++ b/ui/ui-test/src/androidTest/java/androidx/ui/test/AssertsTest.kt
@@ -23,13 +23,10 @@
import androidx.ui.foundation.semantics.inMutuallyExclusiveGroup
import androidx.ui.foundation.semantics.selected
import androidx.ui.foundation.semantics.toggleableState
-import androidx.ui.geometry.Size
import androidx.ui.layout.Column
import androidx.ui.semantics.SemanticsPropertyReceiver
import androidx.ui.semantics.hidden
import androidx.ui.semantics.testTag
-import androidx.ui.unit.Density
-import androidx.ui.unit.dp
import org.junit.Rule
import org.junit.Test
@@ -228,30 +225,6 @@
.assertIsInMutuallyExclusiveGroup()
}
- @Test
- fun assertSizesTest_testPixelAssertion() {
- val size = Size(50f, 31f)
- val spec = CollectedSizes(size, Density(0f))
- spec.assertWidthEqualsTo { 50 }
- spec.assertHeightEqualsTo { 31 }
- }
-
- @Test
- fun assertSizesTest_testDpAssertion() {
- val size = Size(50f, 30f)
- val spec = CollectedSizes(size, Density(2f))
- spec.assertWidthEqualsTo(25.dp)
- spec.assertHeightEqualsTo(15.dp)
- }
-
- @Test
- fun assertSizesTest_testSquare() {
- val size = Size(50f, 50f)
- val spec = CollectedSizes(size, Density(2f))
- spec.assertIsSquareWithSize(25.dp)
- spec.assertIsSquareWithSize { 50 }
- }
-
@Composable
fun BoundaryNode(props: (SemanticsPropertyReceiver.() -> Unit)? = null) {
Column(Modifier.semantics(properties = props)) {}
diff --git a/ui/ui-test/src/main/java/androidx/ui/test/SizesTesting.kt b/ui/ui-test/src/main/java/androidx/ui/test/SizesTesting.kt
deleted file mode 100644
index c3790d0..0000000
--- a/ui/ui-test/src/main/java/androidx/ui/test/SizesTesting.kt
+++ /dev/null
@@ -1,127 +0,0 @@
-/*
- * Copyright 2019 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.ui.test
-
-import androidx.compose.Composable
-import androidx.ui.core.Modifier
-import androidx.ui.core.onChildPositioned
-import androidx.ui.geometry.Size
-import androidx.ui.layout.DpConstraints
-import androidx.ui.layout.Stack
-import androidx.ui.layout.preferredSizeIn
-import androidx.ui.unit.Density
-import androidx.ui.unit.Dp
-import androidx.ui.unit.dp
-import androidx.ui.unit.toSize
-import kotlin.math.abs
-import kotlin.math.roundToInt
-
-/**
- * Constant to emulate very big but finite constraints
- */
-val BigTestConstraints = DpConstraints(maxWidth = 5000.dp, maxHeight = 5000.dp)
-
-/**
- * Set content as with [ComposeTestRule.setContent], but return sizes of this content
- *
- * @param parentConstraints desired parent constraints for content
- * @param performSetContent lambda that should be performed to set content.
- * Defaults to [ComposeTestRule.setContent] and usually don't need to be changed
- * @param children content to set
- */
-fun ComposeTestRule.setContentAndGetPixelSize(
- parentConstraints: DpConstraints = BigTestConstraints,
- // TODO : figure out better way to make it flexible
- performSetContent: (@Composable () -> Unit) -> Unit = { setContent(it) },
- children: @Composable () -> Unit
-): Size {
- var realSize: Size? = null
- performSetContent {
- Stack {
- Stack(
- Modifier.preferredSizeIn(parentConstraints)
- .onChildPositioned { coordinates -> realSize = coordinates.size.toSize() }
- ) {
- children()
- }
- }
- }
- return realSize!!
-}
-
-/**
- * Set content as with [ComposeTestRule.setContent], but return [CollectedSizes] to assert
- * width and height of this content
- *
- * @param parentConstraints desired parent constraints for content
- * @param children content to set
- */
-fun ComposeTestRule.setContentAndCollectSizes(
- parentConstraints: DpConstraints = BigTestConstraints,
- children: @Composable () -> Unit
-): CollectedSizes {
- val size = setContentAndGetPixelSize(parentConstraints, { setContent(it) }, children)
- return CollectedSizes(size, density)
-}
-
-/**
- * Small utility class to provide convenient assertion for width and height for some [Size].
- * It also provides [Density] while asserting.
- *
- * @see ComposeTestRule.setContentAndCollectSizes
- */
-class CollectedSizes(private val size: Size, private val density: Density) {
- fun assertHeightEqualsTo(expectedHeight: Dp) =
- assertHeightEqualsTo { expectedHeight.toIntPx() }
-
- fun assertWidthEqualsTo(expectedWidth: Dp): CollectedSizes =
- assertWidthEqualsTo { expectedWidth.toIntPx() }
-
- fun assertIsSquareWithSize(expectedSize: Dp) = assertIsSquareWithSize { expectedSize.toIntPx() }
-
- fun assertWidthEqualsTo(expectedWidthPx: Density.() -> Int): CollectedSizes {
- val widthPx = with(density) {
- expectedWidthPx()
- }
- assertSize(size.width.roundToInt(), widthPx)
- return this
- }
-
- fun assertHeightEqualsTo(expectedHeightPx: Density.() -> Int): CollectedSizes {
- val heightPx = with(density) {
- expectedHeightPx()
- }
- assertSize(size.height.roundToInt(), heightPx)
- return this
- }
-
- fun assertIsSquareWithSize(expectedSquarePx: Density.() -> Int): CollectedSizes {
- val squarePx = with(density) {
- expectedSquarePx()
- }
- assertSize(size.width.roundToInt(), squarePx)
- assertSize(size.height.roundToInt(), squarePx)
- return this
- }
-}
-
-private fun assertSize(actual: Int, expected: Int) {
- // TODO: because if dp and ipx collision. Remove dp assertion later
- if (abs(actual - expected) > 1) {
- throw AssertionError("Found size: $actual pixels.\nExpected size $expected pixels")
- }
-}