11package org.qownnotes.mobile
22
33import androidx.compose.foundation.layout.Box
4- import androidx.compose.foundation.layout.fillMaxSize
4+ import androidx.compose.foundation.layout.Column
5+ import androidx.compose.foundation.layout.fillMaxWidth
56import androidx.compose.foundation.layout.size
7+ import androidx.compose.foundation.rememberScrollState
8+ import androidx.compose.foundation.verticalScroll
69import androidx.compose.material3.MaterialTheme
7- import androidx.compose.runtime.mutableIntStateOf
810import androidx.compose.runtime.mutableStateOf
911import androidx.compose.ui.Alignment
1012import androidx.compose.ui.Modifier
13+ import androidx.compose.ui.platform.testTag
1114import androidx.compose.ui.test.junit4.createComposeRule
1215import androidx.compose.ui.test.onNodeWithTag
1316import androidx.compose.ui.test.performTouchInput
1417import androidx.compose.ui.test.swipeDown
18+ import androidx.compose.ui.test.swipeUp
1519import androidx.compose.ui.unit.dp
1620import androidx.compose.ui.viewinterop.AndroidView
1721import org.junit.Assert.assertTrue
@@ -22,50 +26,73 @@ import org.qownnotes.mobile.markdown.MarkdownEditText
2226class EditorFastScrollerTest {
2327 @get:Rule val composeRule = createComposeRule()
2428
29+ private val scrollState = mutableStateOf(0 )
30+ private val maximumScroll = mutableStateOf(0 )
31+
32+ /* *
33+ * A flick with the thumb, which is what reading through a long note actually uses. An
34+ * `EditText` scrolls its own text without any fling, so this only travels far when the editor
35+ * is hosted in a scrolling container.
36+ */
2537 @Test
26- fun draggingTheRailMovesThroughMostOfALongEditor () {
27- val editor = mutableStateOf<MarkdownEditText ?>(null )
28- val scrollY = mutableIntStateOf(0 )
29- val scrollRange = mutableIntStateOf(0 )
30- val viewportHeight = mutableIntStateOf(0 )
38+ fun flingingTheEditorCoastsThroughTheNote () {
39+ showEditor()
40+
41+ composeRule.onNodeWithTag(" editor-host" ).performTouchInput { swipeUp() }
42+ composeRule.waitForIdle()
43+
44+ // A drag of roughly one viewport that only moved the text by a few lines has no fling.
45+ assertTrue(
46+ " expected the flick to coast, but it stopped at ${scrollState.value} " ,
47+ scrollState.value > 1_000
48+ )
49+ }
50+
51+ @Test
52+ fun draggingTheRailMovesThroughMostOfTheNote () {
53+ showEditor()
54+
55+ composeRule.onNodeWithTag(" editor-fast-scroll" ).performTouchInput { swipeDown() }
56+ composeRule.waitForIdle()
3157
58+ assertTrue(
59+ " expected the rail to move most of the note, but it reached ${scrollState.value} " ,
60+ scrollState.value > maximumScroll.value / 2
61+ )
62+ }
63+
64+ private fun showEditor () {
65+ val editor = mutableStateOf<MarkdownEditText ?>(null )
3266 composeRule.setContent {
3367 MaterialTheme {
3468 Box (modifier = Modifier .size(width = 320 .dp, height = 480 .dp)) {
35- AndroidView (
36- factory = { context ->
37- MarkdownEditText (context).also { view ->
38- fun updateMetrics () {
39- scrollY.intValue = view.scrollY
40- scrollRange.intValue = editorMaximumScroll(view)
41- viewportHeight.intValue = view.height
42- }
43- editor.value = view
44- view.setText((1 .. 300 ).joinToString(" \n " ) { " Long editor line $it " })
45- view.setOnScrollChangeListener { _, _, _, _, _ -> updateMetrics() }
46- view.addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ ->
47- updateMetrics()
69+ val editorScrollState = rememberScrollState()
70+ scrollState.value = editorScrollState.value
71+ maximumScroll.value = editorScrollState.maxValue
72+ Column (
73+ modifier = Modifier .size(width = 320 .dp, height = 480 .dp)
74+ .verticalScroll(editorScrollState)
75+ .testTag(" editor-host" )
76+ ) {
77+ AndroidView (
78+ factory = { context ->
79+ MarkdownEditText (context).also { view ->
80+ editor.value = view
81+ view.setText(
82+ (1 .. 300 ).joinToString(" \n " ) { " Long editor line $it " }
83+ )
4884 }
49- }
50- },
51- modifier = Modifier .fillMaxSize( )
52- )
85+ },
86+ modifier = Modifier .fillMaxWidth()
87+ )
88+ }
5389 EditorFastScroller (
54- editor = editor.value,
55- scrollY = scrollY.intValue,
56- scrollRange = scrollRange.intValue,
57- viewportHeight = viewportHeight.intValue,
90+ scrollState = editorScrollState,
5891 modifier = Modifier .align(Alignment .CenterEnd )
5992 )
6093 }
6194 }
6295 }
63- composeRule.waitUntil(timeoutMillis = 10_000 ) { scrollRange.intValue > 2_000 }
64- composeRule.onNodeWithTag(" editor-fast-scroll" ).performTouchInput { swipeDown() }
65-
66- composeRule.waitUntil(timeoutMillis = 10_000 ) {
67- editor.value!! .scrollY > scrollRange.intValue / 2
68- }
69- assertTrue(editor.value!! .scrollY > scrollRange.intValue / 2 )
96+ composeRule.waitUntil(timeoutMillis = 10_000 ) { maximumScroll.value > 2_000 }
7097 }
7198}
0 commit comments