Add missing tests for ui-foundation
This CL adds tests that were missing since basic stuff moved to
ui-foundation. Some of this tests require sizes testing infrastructure,
though it's moved to androidTest/ui-foundation as well.
Bug: 136805286
Bug: 136805263
Bug: 136804057
Bug: 136804594
Test: this is tests
Change-Id: Icde24d5d48c20566575a8c8836652b0af808fa16
diff --git a/ui/ui-foundation/src/androidTest/java/androidx/ui/foundation/ClickableTest.kt b/ui/ui-foundation/src/androidTest/java/androidx/ui/foundation/ClickableTest.kt
new file mode 100644
index 0000000..920868f
--- /dev/null
+++ b/ui/ui-foundation/src/androidTest/java/androidx/ui/foundation/ClickableTest.kt
@@ -0,0 +1,113 @@
+/*
+ * 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.foundation
+
+import androidx.compose.composer
+import androidx.test.filters.MediumTest
+import androidx.ui.core.TestTag
+import androidx.ui.core.Text
+import androidx.ui.layout.Center
+import androidx.ui.test.assertSemanticsIsEqualTo
+import androidx.ui.test.createComposeRule
+import androidx.ui.test.createFullSemantics
+import androidx.ui.test.doClick
+import androidx.ui.test.findByTag
+import com.google.common.truth.Truth
+import org.junit.Rule
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.junit.runners.JUnit4
+
+@MediumTest
+@RunWith(JUnit4::class)
+class ClickableTest {
+
+ @get:Rule
+ val composeTestRule = createComposeRule()
+
+ @Test
+ fun clickableTest_defaultSemantics() {
+ composeTestRule.setContent {
+ Center {
+ TestTag(tag = "myClickable") {
+ Clickable( {
+ Text("ClickableText")
+ }
+ }
+ }
+ }
+
+ findByTag("myClickable")
+ .assertSemanticsIsEqualTo(
+ createFullSemantics(
+ isEnabled = true,
+ isButton = true
+ )
+ )
+ }
+
+ @Test
+ fun clickableTest_disabledSemantics() {
+ composeTestRule.setContent {
+ Center {
+ TestTag(tag = "myClickable") {
+ Clickable {
+ Text("ClickableText")
+ }
+ }
+ }
+ }
+
+ findByTag("myClickable")
+ .assertSemanticsIsEqualTo(
+ createFullSemantics(
+ isEnabled = false,
+ isButton = true
+ )
+ )
+ }
+
+ @Test
+ fun clickableTest_click() {
+ var counter = 0
+ val onClick: () -> Unit = { ++counter }
+
+ composeTestRule.setContent {
+ Center {
+ TestTag(tag = "myClickable") {
+ Clickable( {
+ Text("ClickableText")
+ }
+ }
+ }
+ }
+
+ findByTag("myClickable")
+ .doClick()
+
+ Truth
+ .assertThat(counter)
+ .isEqualTo(1)
+
+ findByTag("myClickable")
+ .doClick()
+
+ Truth
+ .assertThat(counter)
+ .isEqualTo(2)
+ }
+}
\ No newline at end of file
diff --git a/ui/ui-material/src/androidTest/java/androidx/ui/material/ColoredRectUiTest.kt b/ui/ui-foundation/src/androidTest/java/androidx/ui/foundation/ColoredRectTest.kt
similarity index 89%
rename from ui/ui-material/src/androidTest/java/androidx/ui/material/ColoredRectUiTest.kt
rename to ui/ui-foundation/src/androidTest/java/androidx/ui/foundation/ColoredRectTest.kt
index 98b43f9..7360545 100644
--- a/ui/ui-material/src/androidTest/java/androidx/ui/material/ColoredRectUiTest.kt
+++ b/ui/ui-foundation/src/androidTest/java/androidx/ui/foundation/ColoredRectTest.kt
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-package androidx.ui.material
+package androidx.ui.foundation
import androidx.compose.composer
import androidx.test.filters.MediumTest
@@ -24,6 +24,7 @@
import androidx.ui.graphics.Color
import androidx.ui.layout.DpConstraints
import androidx.ui.test.createComposeRule
+import androidx.ui.test.setContentAndCollectSizes
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
@@ -31,7 +32,7 @@
@MediumTest
@RunWith(JUnit4::class)
-class ColoredRectUiTest {
+class ColoredRectTest {
@get:Rule
val composeTestRule = createComposeRule()
@@ -43,7 +44,7 @@
val width = 40.dp
val height = 71.dp
composeTestRule
- .setMaterialContentAndTestSizes {
+ .setContentAndCollectSizes {
ColoredRect(width = width, height = height, color = color)
}
.assertWidthEqualsTo(width)
@@ -55,7 +56,7 @@
val width = 40.dp
val height = 71.dp
composeTestRule
- .setMaterialContentAndTestSizes(
+ .setContentAndCollectSizes(
parentConstraints = DpConstraints.tightConstraints(
width,
height
@@ -71,7 +72,7 @@
fun coloredRect_expand_WholeScreenSizes() {
val dm = composeTestRule.displayMetrics
composeTestRule
- .setMaterialContentAndTestSizes {
+ .setContentAndCollectSizes {
ColoredRect(color = color)
}
.assertWidthEqualsTo { dm.widthPixels.ipx }
diff --git a/ui/ui-foundation/src/androidTest/java/androidx/ui/foundation/DeterminateProgressTest.kt b/ui/ui-foundation/src/androidTest/java/androidx/ui/foundation/DeterminateProgressTest.kt
new file mode 100644
index 0000000..9bb254d
--- /dev/null
+++ b/ui/ui-foundation/src/androidTest/java/androidx/ui/foundation/DeterminateProgressTest.kt
@@ -0,0 +1,73 @@
+/*
+ * 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.foundation
+
+import androidx.compose.Model
+import androidx.compose.State
+import androidx.compose.composer
+import androidx.test.filters.MediumTest
+import androidx.ui.core.TestTag
+import androidx.ui.core.dp
+import androidx.ui.graphics.Color
+import androidx.ui.test.assertIsVisible
+import androidx.ui.test.assertValueEquals
+import androidx.ui.test.createComposeRule
+import androidx.ui.test.findByTag
+import org.junit.Rule
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.junit.runners.JUnit4
+
+@Model
+private class State {
+ var progress = 0f
+}
+
+@MediumTest
+@RunWith(JUnit4::class)
+class DeterminateProgressTest {
+
+ @get:Rule
+ val composeTestRule = createComposeRule()
+
+ @Test
+ fun determinateProgress_testSemantics() {
+ val tag = "linear"
+ val state = State()
+
+ composeTestRule
+ .setContent {
+ TestTag(tag = tag) {
+ DeterminateProgressIndicator(progress = state.progress) {
+ ColoredRect(Color.Cyan, 50.dp, 50.dp)
+ }
+ }
+ }
+
+ findByTag(tag)
+ .assertIsVisible()
+ .assertValueEquals("0.0")
+
+ composeTestRule.runOnUiThread {
+ state.progress = 0.5f
+ }
+
+ findByTag(tag)
+ .assertIsVisible()
+ .assertValueEquals("0.5")
+ }
+}
\ No newline at end of file
diff --git a/ui/ui-foundation/src/androidTest/java/androidx/ui/foundation/MutuallyExclusiveSetItemTest.kt b/ui/ui-foundation/src/androidTest/java/androidx/ui/foundation/MutuallyExclusiveSetItemTest.kt
new file mode 100644
index 0000000..7351089
--- /dev/null
+++ b/ui/ui-foundation/src/androidTest/java/androidx/ui/foundation/MutuallyExclusiveSetItemTest.kt
@@ -0,0 +1,95 @@
+/*
+ * 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.foundation
+
+import androidx.compose.composer
+import androidx.compose.state
+import androidx.compose.unaryPlus
+import androidx.test.filters.MediumTest
+import androidx.ui.core.Text
+import androidx.ui.foundation.selection.MutuallyExclusiveSetItem
+import androidx.ui.test.assertIsNotSelected
+import androidx.ui.test.assertIsSelected
+import androidx.ui.test.assertSemanticsIsEqualTo
+import androidx.ui.test.createComposeRule
+import androidx.ui.test.createFullSemantics
+import androidx.ui.test.doClick
+import androidx.ui.test.find
+import androidx.ui.test.findAll
+import com.google.common.truth.Truth
+import org.junit.Rule
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.junit.runners.JUnit4
+
+@MediumTest
+@RunWith(JUnit4::class)
+class MutuallyExclusiveSetItemTest {
+
+ @get:Rule
+ val composeTestRule = createComposeRule()
+
+ @Test
+ fun mutuallyExclusiveItem_defaultSemantics() {
+ composeTestRule.setContent {
+ MutuallyExclusiveSetItem(selected = true, {
+ Text("Text in item")
+ }
+ }
+
+ Truth.assertThat(findAll { isInMutuallyExclusiveGroup }.size).isEqualTo(1)
+ findAll { isInMutuallyExclusiveGroup }.first()
+ .assertSemanticsIsEqualTo(
+ createFullSemantics(
+ inMutuallyExclusiveGroup = true,
+ isSelected = true
+ )
+ )
+ }
+
+ @Test
+ fun mutuallyExclusiveItem_defaultClicks() {
+ composeTestRule.setContent {
+ val (selected, onSelected) = +state { false }
+ MutuallyExclusiveSetItem(selected, onSelected(!selected) }) {
+ Text("Text in item")
+ }
+ }
+
+ find { isInMutuallyExclusiveGroup }
+ .assertIsNotSelected()
+ .doClick()
+ .assertIsSelected()
+ .doClick()
+ .assertIsNotSelected()
+ }
+
+ @Test
+ fun mutuallyExclusiveItem_noClicksNoChanges() {
+ composeTestRule.setContent {
+ val (selected, _) = +state { false }
+ MutuallyExclusiveSetItem(selected, {
+ Text("Text in item")
+ }
+ }
+
+ find { isInMutuallyExclusiveGroup }
+ .assertIsNotSelected()
+ .doClick()
+ .assertIsNotSelected()
+ }
+}
\ No newline at end of file
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 e33ff72..3ce14b6 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
@@ -60,7 +60,7 @@
fun topAppBar_expandsToScreen() {
val dm = composeTestRule.displayMetrics
composeTestRule
- .setMaterialContentAndTestSizes {
+ .setMaterialContentAndCollectSizes {
TopAppBar<Nothing>()
}
.assertHeightEqualsTo(appBarHeight)
@@ -192,7 +192,7 @@
fun bottomAppBar_expandsToScreen() {
val dm = composeTestRule.displayMetrics
composeTestRule
- .setMaterialContentAndTestSizes {
+ .setMaterialContentAndCollectSizes {
BottomAppBar<Nothing>()
}
.assertHeightEqualsTo(appBarHeight)
diff --git a/ui/ui-material/src/androidTest/java/androidx/ui/material/ButtonUiTest.kt b/ui/ui-material/src/androidTest/java/androidx/ui/material/ButtonUiTest.kt
index fcb2e35..fb9cc63 100644
--- a/ui/ui-material/src/androidTest/java/androidx/ui/material/ButtonUiTest.kt
+++ b/ui/ui-material/src/androidTest/java/androidx/ui/material/ButtonUiTest.kt
@@ -161,7 +161,7 @@
return
}
composeTestRule
- .setMaterialContentAndTestSizes {
+ .setMaterialContentAndCollectSizes {
Button( text = "Test button")
}
.assertHeightEqualsTo(36.dp)
@@ -169,7 +169,7 @@
@Test
fun buttonTest_ButtonWithLargeFontSizeIsLargerThenMinHeight() {
- val realSize: PxSize = composeTestRule.setMaterialContentAndCollectPixelSize {
+ val realSize: PxSize = composeTestRule.setMaterialContentAndGetPixelSize {
Button(
>
text = "Test button",
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 0ea5b02..cf168d3 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
@@ -144,7 +144,7 @@
private fun materialSizeTestForValue(checkboxValue: ToggleableState) {
composeTestRule
- .setMaterialContentAndTestSizes {
+ .setMaterialContentAndCollectSizes {
TriStateCheckbox(value = checkboxValue, >
}
.assertIsSquareWithSize { 2.dp.toIntPx() * 2 + 20.dp.toIntPx() }
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 a7a06ec..122c314 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
@@ -39,7 +39,7 @@
fun divider_DefaultSizes() {
val dm = composeTestRule.displayMetrics
composeTestRule
- .setMaterialContentAndTestSizes {
+ .setMaterialContentAndCollectSizes {
Divider()
}
.assertHeightEqualsTo(defaultHeight)
@@ -51,7 +51,7 @@
val height = 20.dp
val dm = composeTestRule.displayMetrics
composeTestRule
- .setMaterialContentAndTestSizes {
+ .setMaterialContentAndCollectSizes {
Divider(height = height)
}
.assertWidthEqualsTo { dm.widthPixels.ipx }
@@ -65,7 +65,7 @@
val height = 21.dp
composeTestRule
- .setMaterialContentAndTestSizes {
+ .setMaterialContentAndCollectSizes {
Divider(indent = indent, height = height)
}
.assertHeightEqualsTo(height)
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 fa3267c..32e5cca 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
@@ -129,7 +129,7 @@
@Test
fun staticDrawer_testWidth_whenOpened() {
composeTestRule
- .setMaterialContentAndTestSizes {
+ .setMaterialContentAndCollectSizes {
StaticDrawer {
Container(expanded = true) {}
}
diff --git a/ui/ui-material/src/androidTest/java/androidx/ui/material/FloatingActionButtonUiTest.kt b/ui/ui-material/src/androidTest/java/androidx/ui/material/FloatingActionButtonUiTest.kt
index 05bca1d..dd2bf51 100644
--- a/ui/ui-material/src/androidTest/java/androidx/ui/material/FloatingActionButtonUiTest.kt
+++ b/ui/ui-material/src/androidTest/java/androidx/ui/material/FloatingActionButtonUiTest.kt
@@ -40,7 +40,7 @@
@Test
fun defaultFabHasSizeFromSpec() {
composeTestRule
- .setMaterialContentAndTestSizes {
+ .setMaterialContentAndCollectSizes {
FloatingActionButton(icon = createImage())
}
.assertIsSquareWithSize(56.dp)
@@ -49,7 +49,7 @@
@Test
fun extendedFabHasHeightFromSpec() {
val size = composeTestRule
- .setMaterialContentAndCollectPixelSize {
+ .setMaterialContentAndGetPixelSize {
FloatingActionButton(icon = createImage(), text = "Extended")
}
withDensity(composeTestRule.density) {
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 e2e84f1..825a817 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
@@ -18,21 +18,13 @@
import androidx.compose.Composable
import androidx.compose.composer
-import androidx.ui.core.Density
-import androidx.ui.core.DensityReceiver
-import androidx.ui.core.Dp
-import androidx.ui.core.IntPx
-import androidx.ui.core.OnChildPositioned
import androidx.ui.core.PxSize
-import androidx.ui.core.dp
-import androidx.ui.core.round
-import androidx.ui.core.withDensity
-import androidx.ui.layout.ConstrainedBox
+import androidx.ui.test.BigTestConstraints
+import androidx.ui.test.CollectedSizes
+import androidx.ui.test.setContentAndGetPixelSize
import androidx.ui.layout.DpConstraints
-import androidx.ui.layout.Wrap
import androidx.ui.material.surface.Surface
import androidx.ui.test.ComposeTestRule
-import com.google.common.truth.Truth
fun ComposeTestRule.setMaterialContent(composable: @Composable() () -> Unit) {
setContent {
@@ -44,77 +36,24 @@
}
}
-private val BigConstraints = DpConstraints(maxWidth = 5000.dp, maxHeight = 5000.dp)
-
-fun ComposeTestRule.setMaterialContentAndTestSizes(
- parentConstraints: DpConstraints = BigConstraints,
+fun ComposeTestRule.setMaterialContentAndCollectSizes(
+ parentConstraints: DpConstraints = BigTestConstraints,
children: @Composable() () -> Unit
-): SizeTestSpec {
- var realSize: PxSize? = null
- setMaterialContent {
- Wrap {
- ConstrainedBox(constraints = parentConstraints) {
- OnChildPositioned( coordinates ->
- realSize = coordinates.size
- }) {
- children()
- }
- }
- }
- }
- return SizeTestSpec(realSize!!, density)
+): CollectedSizes {
+ val sizes = setMaterialContentAndGetPixelSize(parentConstraints, children)
+ return CollectedSizes(sizes, density)
}
-fun ComposeTestRule.setMaterialContentAndCollectPixelSize(
- parentConstraints: DpConstraints = BigConstraints,
+fun ComposeTestRule.setMaterialContentAndGetPixelSize(
+ parentConstraints: DpConstraints = BigTestConstraints,
children: @Composable() () -> Unit
-): PxSize {
- var realSize: PxSize? = null
- setMaterialContent {
- Wrap {
- ConstrainedBox(constraints = parentConstraints) {
- OnChildPositioned( coordinates ->
- realSize = coordinates.size
- }) {
- children()
- }
- }
+): PxSize = setContentAndGetPixelSize(
+ parentConstraints,
+ { setMaterialContent(it) }
+) {
+ MaterialTheme {
+ Surface {
+ children()
}
}
- return realSize!!
-}
-
-class SizeTestSpec(private val size: PxSize, private val density: Density) {
- fun assertHeightEqualsTo(expectedHeight: Dp) =
- assertHeightEqualsTo { expectedHeight.toIntPx() }
-
- fun assertWidthEqualsTo(expectedWidth: Dp): SizeTestSpec =
- assertWidthEqualsTo { expectedWidth.toIntPx() }
-
- fun assertIsSquareWithSize(expectedSize: Dp) = assertIsSquareWithSize { expectedSize.toIntPx() }
-
- fun assertWidthEqualsTo(expectedWidthPx: DensityReceiver.() -> IntPx): SizeTestSpec {
- val widthPx = withDensity(density) {
- expectedWidthPx()
- }
- Truth.assertThat(size.width.round()).isEqualTo(widthPx)
- return this
- }
-
- fun assertHeightEqualsTo(expectedHeightPx: DensityReceiver.() -> IntPx): SizeTestSpec {
- val heightPx = withDensity(density) {
- expectedHeightPx()
- }
- Truth.assertThat(size.height.round()).isEqualTo(heightPx)
- return this
- }
-
- fun assertIsSquareWithSize(expectedSquarePx: DensityReceiver.() -> IntPx): SizeTestSpec {
- val squarePx = withDensity(density) {
- expectedSquarePx()
- }
- Truth.assertThat(size.width.round()).isEqualTo(squarePx)
- Truth.assertThat(size.height.round()).isEqualTo(squarePx)
- return this
- }
}
\ No newline at end of file
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 28d5233..389f109 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
@@ -72,7 +72,7 @@
@Test
fun determinateLinearProgressIndicator_Size() {
composeTestRule
- .setMaterialContentAndTestSizes {
+ .setMaterialContentAndCollectSizes {
LinearProgressIndicator(progress = 0f)
}
.assertWidthEqualsTo(ExpectedLinearWidth)
@@ -82,7 +82,7 @@
@Test
fun indeterminateLinearProgressIndicator_Size() {
composeTestRule
- .setMaterialContentAndTestSizes {
+ .setMaterialContentAndCollectSizes {
LinearProgressIndicator()
}
.assertWidthEqualsTo(ExpectedLinearWidth)
@@ -117,7 +117,7 @@
@Test
fun determinateCircularProgressIndicator_Size() {
composeTestRule
- .setMaterialContentAndTestSizes {
+ .setMaterialContentAndCollectSizes {
CircularProgressIndicator(progress = 0f)
}
.assertIsSquareWithSize { 4.dp.toIntPx() * 2 + 40.dp.toIntPx() }
@@ -126,7 +126,7 @@
@Test
fun indeterminateCircularProgressIndicator_Size() {
composeTestRule
- .setMaterialContentAndTestSizes {
+ .setMaterialContentAndCollectSizes {
CircularProgressIndicator()
}
.assertIsSquareWithSize { 4.dp.toIntPx() * 2 + 40.dp.toIntPx() }
diff --git a/ui/ui-material/src/androidTest/java/androidx/ui/material/RadioGroupUiTest.kt b/ui/ui-material/src/androidTest/java/androidx/ui/material/RadioGroupUiTest.kt
index 1fcb8fa..d43cd7a 100644
--- a/ui/ui-material/src/androidTest/java/androidx/ui/material/RadioGroupUiTest.kt
+++ b/ui/ui-material/src/androidTest/java/androidx/ui/material/RadioGroupUiTest.kt
@@ -208,7 +208,7 @@
private fun materialSizesTestForValue(selected: Boolean) {
composeTestRule
- .setMaterialContentAndTestSizes {
+ .setMaterialContentAndCollectSizes {
RadioButton(selected = selected, >
}
.assertIsSquareWithSize { 2.dp.toIntPx() * 2 + 20.dp.toIntPx() }
diff --git a/ui/ui-material/src/androidTest/java/androidx/ui/material/SwitchUiTest.kt b/ui/ui-material/src/androidTest/java/androidx/ui/material/SwitchUiTest.kt
index 8e56d4c..855bf30 100644
--- a/ui/ui-material/src/androidTest/java/androidx/ui/material/SwitchUiTest.kt
+++ b/ui/ui-material/src/androidTest/java/androidx/ui/material/SwitchUiTest.kt
@@ -126,7 +126,7 @@
private fun materialSizesTestForValue(checked: Boolean) {
composeTestRule
- .setMaterialContentAndTestSizes {
+ .setMaterialContentAndCollectSizes {
Switch(checked = checked, >
}
.assertWidthEqualsTo { 34.dp.toIntPx() + 2.dp.toIntPx() * 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 3f8fba7..d6906c6 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
@@ -54,7 +54,7 @@
@Test
fun textTab_Height() {
composeTestRule
- .setMaterialContentAndTestSizes {
+ .setMaterialContentAndCollectSizes {
Container {
Surface {
Tab(text = "Text", selected = true, >
@@ -67,7 +67,7 @@
@Test
fun iconTab_Height() {
composeTestRule
- .setMaterialContentAndTestSizes {
+ .setMaterialContentAndCollectSizes {
Container {
Surface {
Tab(icon = image, selected = true, >
@@ -80,7 +80,7 @@
@Test
fun textAndIconTab_Height() {
composeTestRule
- .setMaterialContentAndTestSizes {
+ .setMaterialContentAndCollectSizes {
Container {
Surface {
Tab(text = "Text And Icon", icon = image, selected = true, >
diff --git a/ui/ui-test/api/1.0.0-alpha01.txt b/ui/ui-test/api/1.0.0-alpha01.txt
index cedc190..89f2433 100644
--- a/ui/ui-test/api/1.0.0-alpha01.txt
+++ b/ui/ui-test/api/1.0.0-alpha01.txt
@@ -23,6 +23,16 @@
method public static androidx.ui.test.SemanticsNodeInteraction assertValueEquals(androidx.ui.test.SemanticsNodeInteraction, String value);
}
+ public final class CollectedSizes {
+ ctor public CollectedSizes(androidx.ui.core.PxSize size, androidx.ui.core.Density density);
+ method public androidx.ui.test.CollectedSizes assertHeightEqualsTo(androidx.ui.core.Dp expectedHeight);
+ method public androidx.ui.test.CollectedSizes assertHeightEqualsTo(kotlin.jvm.functions.Function1<? super androidx.ui.core.DensityReceiver,androidx.ui.core.IntPx> expectedHeightPx);
+ method public androidx.ui.test.CollectedSizes assertIsSquareWithSize(androidx.ui.core.Dp expectedSize);
+ method public androidx.ui.test.CollectedSizes assertIsSquareWithSize(kotlin.jvm.functions.Function1<? super androidx.ui.core.DensityReceiver,androidx.ui.core.IntPx> expectedSquarePx);
+ method public androidx.ui.test.CollectedSizes assertWidthEqualsTo(androidx.ui.core.Dp expectedWidth);
+ method public androidx.ui.test.CollectedSizes assertWidthEqualsTo(kotlin.jvm.functions.Function1<? super androidx.ui.core.DensityReceiver,androidx.ui.core.IntPx> expectedWidthPx);
+ }
+
public interface ComposeTestRule extends org.junit.rules.TestRule {
method public androidx.ui.core.Density getDensity();
method public android.util.DisplayMetrics getDisplayMetrics();
@@ -79,6 +89,13 @@
ctor public SemanticsTreeInteractionKt();
}
+ public final class SizesTestingKt {
+ ctor public 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.core.PxSize 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);
+ }
+
}
package androidx.ui.test.android {
diff --git a/ui/ui-test/api/current.txt b/ui/ui-test/api/current.txt
index cedc190..89f2433 100644
--- a/ui/ui-test/api/current.txt
+++ b/ui/ui-test/api/current.txt
@@ -23,6 +23,16 @@
method public static androidx.ui.test.SemanticsNodeInteraction assertValueEquals(androidx.ui.test.SemanticsNodeInteraction, String value);
}
+ public final class CollectedSizes {
+ ctor public CollectedSizes(androidx.ui.core.PxSize size, androidx.ui.core.Density density);
+ method public androidx.ui.test.CollectedSizes assertHeightEqualsTo(androidx.ui.core.Dp expectedHeight);
+ method public androidx.ui.test.CollectedSizes assertHeightEqualsTo(kotlin.jvm.functions.Function1<? super androidx.ui.core.DensityReceiver,androidx.ui.core.IntPx> expectedHeightPx);
+ method public androidx.ui.test.CollectedSizes assertIsSquareWithSize(androidx.ui.core.Dp expectedSize);
+ method public androidx.ui.test.CollectedSizes assertIsSquareWithSize(kotlin.jvm.functions.Function1<? super androidx.ui.core.DensityReceiver,androidx.ui.core.IntPx> expectedSquarePx);
+ method public androidx.ui.test.CollectedSizes assertWidthEqualsTo(androidx.ui.core.Dp expectedWidth);
+ method public androidx.ui.test.CollectedSizes assertWidthEqualsTo(kotlin.jvm.functions.Function1<? super androidx.ui.core.DensityReceiver,androidx.ui.core.IntPx> expectedWidthPx);
+ }
+
public interface ComposeTestRule extends org.junit.rules.TestRule {
method public androidx.ui.core.Density getDensity();
method public android.util.DisplayMetrics getDisplayMetrics();
@@ -79,6 +89,13 @@
ctor public SemanticsTreeInteractionKt();
}
+ public final class SizesTestingKt {
+ ctor public 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.core.PxSize 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);
+ }
+
}
package androidx.ui.test.android {
diff --git a/ui/ui-test/api/restricted_1.0.0-alpha01.txt b/ui/ui-test/api/restricted_1.0.0-alpha01.txt
index cedc190..89f2433 100644
--- a/ui/ui-test/api/restricted_1.0.0-alpha01.txt
+++ b/ui/ui-test/api/restricted_1.0.0-alpha01.txt
@@ -23,6 +23,16 @@
method public static androidx.ui.test.SemanticsNodeInteraction assertValueEquals(androidx.ui.test.SemanticsNodeInteraction, String value);
}
+ public final class CollectedSizes {
+ ctor public CollectedSizes(androidx.ui.core.PxSize size, androidx.ui.core.Density density);
+ method public androidx.ui.test.CollectedSizes assertHeightEqualsTo(androidx.ui.core.Dp expectedHeight);
+ method public androidx.ui.test.CollectedSizes assertHeightEqualsTo(kotlin.jvm.functions.Function1<? super androidx.ui.core.DensityReceiver,androidx.ui.core.IntPx> expectedHeightPx);
+ method public androidx.ui.test.CollectedSizes assertIsSquareWithSize(androidx.ui.core.Dp expectedSize);
+ method public androidx.ui.test.CollectedSizes assertIsSquareWithSize(kotlin.jvm.functions.Function1<? super androidx.ui.core.DensityReceiver,androidx.ui.core.IntPx> expectedSquarePx);
+ method public androidx.ui.test.CollectedSizes assertWidthEqualsTo(androidx.ui.core.Dp expectedWidth);
+ method public androidx.ui.test.CollectedSizes assertWidthEqualsTo(kotlin.jvm.functions.Function1<? super androidx.ui.core.DensityReceiver,androidx.ui.core.IntPx> expectedWidthPx);
+ }
+
public interface ComposeTestRule extends org.junit.rules.TestRule {
method public androidx.ui.core.Density getDensity();
method public android.util.DisplayMetrics getDisplayMetrics();
@@ -79,6 +89,13 @@
ctor public SemanticsTreeInteractionKt();
}
+ public final class SizesTestingKt {
+ ctor public 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.core.PxSize 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);
+ }
+
}
package androidx.ui.test.android {
diff --git a/ui/ui-test/api/restricted_current.txt b/ui/ui-test/api/restricted_current.txt
index cedc190..89f2433 100644
--- a/ui/ui-test/api/restricted_current.txt
+++ b/ui/ui-test/api/restricted_current.txt
@@ -23,6 +23,16 @@
method public static androidx.ui.test.SemanticsNodeInteraction assertValueEquals(androidx.ui.test.SemanticsNodeInteraction, String value);
}
+ public final class CollectedSizes {
+ ctor public CollectedSizes(androidx.ui.core.PxSize size, androidx.ui.core.Density density);
+ method public androidx.ui.test.CollectedSizes assertHeightEqualsTo(androidx.ui.core.Dp expectedHeight);
+ method public androidx.ui.test.CollectedSizes assertHeightEqualsTo(kotlin.jvm.functions.Function1<? super androidx.ui.core.DensityReceiver,androidx.ui.core.IntPx> expectedHeightPx);
+ method public androidx.ui.test.CollectedSizes assertIsSquareWithSize(androidx.ui.core.Dp expectedSize);
+ method public androidx.ui.test.CollectedSizes assertIsSquareWithSize(kotlin.jvm.functions.Function1<? super androidx.ui.core.DensityReceiver,androidx.ui.core.IntPx> expectedSquarePx);
+ method public androidx.ui.test.CollectedSizes assertWidthEqualsTo(androidx.ui.core.Dp expectedWidth);
+ method public androidx.ui.test.CollectedSizes assertWidthEqualsTo(kotlin.jvm.functions.Function1<? super androidx.ui.core.DensityReceiver,androidx.ui.core.IntPx> expectedWidthPx);
+ }
+
public interface ComposeTestRule extends org.junit.rules.TestRule {
method public androidx.ui.core.Density getDensity();
method public android.util.DisplayMetrics getDisplayMetrics();
@@ -79,6 +89,13 @@
ctor public SemanticsTreeInteractionKt();
}
+ public final class SizesTestingKt {
+ ctor public 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.core.PxSize 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);
+ }
+
}
package androidx.ui.test.android {
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
new file mode 100644
index 0000000..aa62d7c
--- /dev/null
+++ b/ui/ui-test/src/main/java/androidx/ui/test/SizesTesting.kt
@@ -0,0 +1,128 @@
+/*
+ * 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.compose.composer
+import androidx.ui.core.Density
+import androidx.ui.core.DensityReceiver
+import androidx.ui.core.Dp
+import androidx.ui.core.IntPx
+import androidx.ui.core.OnChildPositioned
+import androidx.ui.core.PxSize
+import androidx.ui.core.dp
+import androidx.ui.core.round
+import androidx.ui.core.withDensity
+import androidx.ui.layout.ConstrainedBox
+import androidx.ui.layout.DpConstraints
+import androidx.ui.layout.Wrap
+
+/**
+ * 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
+): PxSize {
+ var realSize: PxSize? = null
+ performSetContent {
+ Wrap {
+ ConstrainedBox(constraints = parentConstraints) {
+ OnChildPositioned( coordinates ->
+ realSize = coordinates.size
+ }) {
+ 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 [PxSize].
+ * It also provides [DensityReceiver] while asserting.
+ *
+ * @see ComposeTestRule.setContentAndCollectSizes
+ */
+class CollectedSizes(private val size: PxSize, 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: DensityReceiver.() -> IntPx): CollectedSizes {
+ val widthPx = withDensity(density) {
+ expectedWidthPx()
+ }
+ assertSize(size.width.round(), widthPx)
+ return this
+ }
+
+ fun assertHeightEqualsTo(expectedHeightPx: DensityReceiver.() -> IntPx): CollectedSizes {
+ val heightPx = withDensity(density) {
+ expectedHeightPx()
+ }
+ assertSize(size.height.round(), heightPx)
+ return this
+ }
+
+ fun assertIsSquareWithSize(expectedSquarePx: DensityReceiver.() -> IntPx): CollectedSizes {
+ val squarePx = withDensity(density) {
+ expectedSquarePx()
+ }
+ assertSize(size.width.round(), squarePx)
+ assertSize(size.height.round(), squarePx)
+ return this
+ }
+}
+
+private fun assertSize(actual: IntPx, expected: IntPx) {
+ if (actual != expected) {
+ throw AssertionError("Found size: $actual pixels.\nExpected size $expected pixels")
+ }
+}
\ No newline at end of file
diff --git a/ui/ui-test/src/test/java/androidx/ui/test/AssertsTests.kt b/ui/ui-test/src/test/java/androidx/ui/test/AssertsTests.kt
index d983719..9ea85c0 100644
--- a/ui/ui-test/src/test/java/androidx/ui/test/AssertsTests.kt
+++ b/ui/ui-test/src/test/java/androidx/ui/test/AssertsTests.kt
@@ -16,6 +16,10 @@
package androidx.ui.test
+import androidx.ui.core.Density
+import androidx.ui.core.PxSize
+import androidx.ui.core.dp
+import androidx.ui.core.ipx
import androidx.ui.core.semantics.SemanticsConfiguration
import androidx.ui.test.helpers.FakeSemanticsTreeInteraction
import org.junit.Test
@@ -189,4 +193,28 @@
findByTag("test")
.assertIsInMutuallyExclusiveGroup()
}
+
+ @Test
+ fun assertSizesTest_testPixelAssertion() {
+ val size = PxSize(50.ipx, 31.ipx)
+ val spec = CollectedSizes(size, Density(0f))
+ spec.assertWidthEqualsTo { 50.ipx }
+ spec.assertHeightEqualsTo { 31.ipx }
+ }
+
+ @Test
+ fun assertSizesTest_testDpAssertion() {
+ val size = PxSize(50.ipx, 30.ipx)
+ val spec = CollectedSizes(size, Density(2f))
+ spec.assertWidthEqualsTo(25.dp)
+ spec.assertHeightEqualsTo(15.dp)
+ }
+
+ @Test
+ fun assertSizesTest_testSquare() {
+ val size = PxSize(50.ipx, 50.ipx)
+ val spec = CollectedSizes(size, Density(2f))
+ spec.assertIsSquareWithSize(25.dp)
+ spec.assertIsSquareWithSize { 50.ipx }
+ }
}
\ No newline at end of file