Updates snippets for Gestures and testing docs
Test: ./gradlew :compose:integration-tests:docs-snippets:ktlint
Change-Id: I7d727adc18c2485d56fbd2536b9a66e0f2568837
diff --git a/compose/integration-tests/docs-snippets/src/main/java/androidx/compose/integration/docs/gestures/Gestures.kt b/compose/integration-tests/docs-snippets/src/main/java/androidx/compose/integration/docs/gestures/Gestures.kt
index 6ef6d29..2b55af8 100644
--- a/compose/integration-tests/docs-snippets/src/main/java/androidx/compose/integration/docs/gestures/Gestures.kt
+++ b/compose/integration-tests/docs-snippets/src/main/java/androidx/compose/integration/docs/gestures/Gestures.kt
@@ -58,7 +58,6 @@
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
-import androidx.compose.ui.graphics.TileMode
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.input.pointer.consumeAllChanges
import androidx.compose.ui.input.pointer.pointerInput
@@ -177,12 +176,12 @@
fun ScrollableSample() {
// actual composable state
var offset by remember { mutableStateOf(0f) }
- // state for Scrollable, describes how to consume scrolling delta and update offset
Box(
Modifier
.size(150.dp)
.scrollable(
orientation = Orientation.Vertical,
+ // Scrollable state: describes how to consume scrolling delta and update offset
state = rememberScrollableState { delta ->
offset += delta
delta
@@ -216,9 +215,7 @@
private object GesturesSnippet6 {
@Composable
fun NestedSample() {
- val gradient = Brush.verticalGradient(
- listOf(Color.Gray, Color.White), 0.0f, 1000.0f, TileMode.Repeated
- )
+ val gradient = Brush.verticalGradient(0f to Color.Gray, 1000f to Color.White)
Box(
modifier = Modifier
.background(Color.LightGray)
@@ -280,19 +277,19 @@
}
@Composable private fun GesturesSnippet8() {
Box(modifier = Modifier.fillMaxSize()) {
- val offsetX = remember { mutableStateOf(0f) }
- val offsetY = remember { mutableStateOf(0f) }
+ var offsetX by remember { mutableStateOf(0f) }
+ var offsetY by remember { mutableStateOf(0f) }
Box(
Modifier
- .offset { IntOffset(offsetX.value.roundToInt(), offsetY.value.roundToInt()) }
+ .offset { IntOffset(offsetX.roundToInt(), offsetY.roundToInt()) }
.background(Color.Blue)
.size(50.dp)
.pointerInput(Unit) {
detectDragGestures { change, dragAmount ->
change.consumeAllChanges()
- offsetX.value = (offsetX.value + dragAmount.x)
- offsetY.value = (offsetY.value + dragAmount.y)
+ offsetX += dragAmount.x
+ offsetY += dragAmount.y
}
}
)
@@ -314,7 +311,7 @@
val swipeableState = rememberSwipeableState(0)
val sizePx = with(LocalDensity.current) { squareSize.toPx() }
- val anchors = mapOf(0f to 0, sizePx to 1)
+ val anchors = mapOf(0f to 0, sizePx to 1) // Maps anchor points (in px) to states
Box(
modifier = Modifier
diff --git a/compose/integration-tests/docs-snippets/src/main/java/androidx/compose/integration/docs/testing/CheatSheet.kt b/compose/integration-tests/docs-snippets/src/main/java/androidx/compose/integration/docs/testing/CheatSheet.kt
index 72b3300..3feae6b 100644
--- a/compose/integration-tests/docs-snippets/src/main/java/androidx/compose/integration/docs/testing/CheatSheet.kt
+++ b/compose/integration-tests/docs-snippets/src/main/java/androidx/compose/integration/docs/testing/CheatSheet.kt
@@ -28,6 +28,7 @@
import androidx.compose.ui.layout.FirstBaseline
import androidx.compose.ui.semantics.ProgressBarRangeInfo
import androidx.compose.ui.semantics.SemanticsActions
+import androidx.compose.ui.test.IdlingResource
import androidx.compose.ui.test.assert
import androidx.compose.ui.test.assertAll
import androidx.compose.ui.test.assertAny
@@ -163,6 +164,8 @@
import androidx.compose.ui.test.width
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.unit.dp
+import kotlinx.coroutines.GlobalScope
+import kotlinx.coroutines.launch
import android.view.KeyEvent as AndroidKeyEvent
import android.view.KeyEvent.ACTION_DOWN as ActionDown
import android.view.KeyEvent.KEYCODE_A as KeyCodeA
@@ -378,6 +381,7 @@
runOnIdle { }
runOnUiThread { }
waitForIdle()
+ waitUntil { true }
mainClock.apply {
autoAdvance
currentTime
@@ -385,6 +389,11 @@
advanceTimeByFrame()
advanceTimeUntil { true }
}
+ registerIdlingResource(idlingResource)
+ unregisterIdlingResource(idlingResource)
+ }
+ GlobalScope.launch {
+ nonAndroidComposeTestRule.awaitIdle()
}
// ANDROID COMPOSE TEST RULE
@@ -410,4 +419,8 @@
private val nonAndroidComposeTestRule = createComposeRule()
private val keyEvent2 = KeyEvent(AndroidKeyEvent(ActionDown, KeyCodeA))
private val offset = Offset(0f, 0f)
-private val rangeInfo = ProgressBarRangeInfo(0f, 0f..1f)
\ No newline at end of file
+private val rangeInfo = ProgressBarRangeInfo(0f, 0f..1f)
+private val idlingResource = object : IdlingResource {
+ override val isIdleNow: Boolean
+ get() = TODO("Stub!")
+}
diff --git a/compose/integration-tests/docs-snippets/src/main/java/androidx/compose/integration/docs/testing/Testing.kt b/compose/integration-tests/docs-snippets/src/main/java/androidx/compose/integration/docs/testing/Testing.kt
index f69f42d..9060916 100644
--- a/compose/integration-tests/docs-snippets/src/main/java/androidx/compose/integration/docs/testing/Testing.kt
+++ b/compose/integration-tests/docs-snippets/src/main/java/androidx/compose/integration/docs/testing/Testing.kt
@@ -224,6 +224,20 @@
composeTestRule.unregisterIdlingResource(idlingResource)
}
+private fun TestingSyncSnippets5() {
+ composeTestRule.mainClock.autoAdvance = true // default
+ composeTestRule.waitForIdle() // Advances the clock until Compose is idle
+
+ composeTestRule.mainClock.autoAdvance = false
+ composeTestRule.waitForIdle() // Only waits for Idling Resources to become idle
+}
+
+private fun TestingSyncSnippets6and7() {
+ composeTestRule.mainClock.advanceTimeUntil(timeoutMs) { condition }
+
+ composeTestRule.waitUntil(timeoutMs) { condition }
+}
+
private object TestingSemanticsSnippets1 {
// Creates a Semantics property of type boolean
val PickedDateKey = SemanticsPropertyKey<Long>("PickedDate")
@@ -295,7 +309,9 @@
private lateinit var key: SemanticsPropertyKey<AccessibilityAction<() -> Boolean>>
private var keyEvent = KeyEvent(AndroidKeyEvent(ActionDown, KeyCodeA))
private const val milliseconds = 10L
-val idlingResource = object : IdlingResource {
+private const val timeoutMs = 10L
+private val idlingResource = object : IdlingResource {
override val isIdleNow: Boolean
get() = TODO("Stub!")
-}
\ No newline at end of file
+}
+private val condition = true
\ No newline at end of file