@@ -9,6 +9,8 @@ import android.widget.TextView
99import androidx.compose.ui.test.assertIsDisplayed
1010import androidx.compose.ui.test.assertIsEnabled
1111import androidx.compose.ui.test.assertIsNotEnabled
12+ import androidx.compose.ui.test.assertIsOff
13+ import androidx.compose.ui.test.assertIsOn
1214import androidx.compose.ui.test.junit4.createAndroidComposeRule
1315import androidx.compose.ui.test.longClick
1416import androidx.compose.ui.test.onAllNodesWithTag
@@ -42,7 +44,6 @@ import kotlinx.coroutines.flow.first
4244import kotlinx.coroutines.runBlocking
4345import org.hamcrest.Matchers.containsString
4446import org.junit.Assert.assertEquals
45- import org.junit.Assert.assertFalse
4647import org.junit.Assert.assertTrue
4748import org.junit.Before
4849import org.junit.Rule
@@ -348,8 +349,8 @@ class AppLaunchTest {
348349 composeRule.waitUntil(timeoutMillis = 10_000 ) {
349350 application.fakeBackend.deletedRemoteIds.toSet() == setOf (42L , 43L )
350351 }
351- composeRule.onNodeWithText (" First note" ).assertDoesNotExist( )
352- composeRule.onNodeWithText (" Second note" ).assertDoesNotExist( )
352+ composeRule.waitForTextToGo (" First note" )
353+ composeRule.waitForTextToGo (" Second note" )
353354 }
354355
355356 @Test
@@ -363,7 +364,7 @@ class AppLaunchTest {
363364 composeRule.onNodeWithTag(" confirm-delete-note" ).performClick()
364365
365366 composeRule.waitForTag(" note-list" )
366- composeRule.onNodeWithText (" Existing note" ).assertDoesNotExist( )
367+ composeRule.waitForTextToGo (" Existing note" )
367368 composeRule.waitUntil(timeoutMillis = 10_000 ) {
368369 application.fakeBackend.deletedRemoteIds == listOf (42L )
369370 }
@@ -435,7 +436,7 @@ class AppLaunchTest {
435436 }
436437 composeRule.waitForText(" Filtered note" )
437438 composeRule.onNodeWithTag(" note-search" ).performTextInput(" Visible" )
438- composeRule.onNodeWithText (" Filtered note" ).assertDoesNotExist( )
439+ composeRule.waitForTextToGo (" Filtered note" )
439440
440441 composeRule.onNodeWithTag(" remote-trash" ).performClick()
441442
@@ -636,18 +637,12 @@ class AppLaunchTest {
636637 importAccount(" alice" , " Existing note" , " etag-1" , 10 )
637638 composeRule.onNodeWithText(" Existing note" ).performClick()
638639 composeRule.enterEditMode()
639- lateinit var editor: TextView
640- onView(withId(R .id.markdown_editor)).check { view, _ ->
641- editor = view as TextView
642- assertTrue(editor.hasFocus())
643- }
640+ awaitEditorFocus(focused = true )
644641
645642 composeRule.onNodeWithTag(" cancel-editing" ).performClick()
646643
647644 composeRule.waitForTag(" markdown-view" )
648- composeRule.runOnIdle {
649- assertFalse(" leaving edit mode must release editor focus" , editor.hasFocus())
650- }
645+ awaitEditorFocus(focused = false )
651646 composeRule.onNodeWithTag(" confirm-discard-changes" ).assertDoesNotExist()
652647 composeRule.onNodeWithTag(" edit-note" ).assertIsDisplayed()
653648 }
@@ -908,33 +903,44 @@ class AppLaunchTest {
908903 composeRule.onNodeWithTag(" delete-note" ).assertIsDisplayed()
909904 }
910905
911- /* * The name of a note is the name of the file holding it, so a rename has to be uploaded. */
906+ /* *
907+ * The name of a note is the name of the file holding it, so a rename has to be uploaded.
908+ * Renaming without the heading option touches nothing but that name.
909+ */
912910 @Test
913911 fun renamingANoteShowsAndUploadsTheNewFileName () {
914912 importAccount(" alice" , " Existing note" , " etag-1" , 10 )
915913 composeRule.onNodeWithText(" Existing note" ).performClick()
916914
917- // Opening a note loads it from the repository, so its actions appear a recomposition later.
918- composeRule.openNoteMenu()
919- composeRule.onNodeWithTag(" rename-note" ).performClick()
920- composeRule.waitForTag(" note-name-field" )
921- composeRule.onNodeWithTag(" note-name-field" ).performTextReplacement(" Grocery list" )
922- composeRule.onNodeWithTag(" confirm-rename-note" ).performClick()
915+ renameNote(" Grocery list" , updateHeading = false )
923916
924917 composeRule.waitForText(" Grocery list" )
925918 composeRule.onNodeWithTag(" back-to-note-list" ).performClick()
926919 composeRule.waitForText(" Grocery list" )
927920 composeRule.waitUntil(timeoutMillis = 10_000 ) {
928921 application.fakeBackend.pushedNotes.any { it.title == " Grocery list" }
929922 }
930- assertTrue(
931- runBlocking {
932- application.component.noteRepository
933- .observeNotes(testAccount(" alice" ).localAccountId())
934- .first()
935- .all { it.content == " # Existing note" }
923+ assertEquals(listOf (" # Existing note" ), noteContents(" alice" ))
924+ }
925+
926+ /* *
927+ * The heading a note opens with usually repeats its file name, so the rename dialog offers to
928+ * carry the new name into that heading, and does so unless the writer says otherwise.
929+ */
930+ @Test
931+ fun renamingANoteAlsoRewritesTheFirstHeadingByDefault () {
932+ importAccount(" alice" , " Existing note" , " etag-1" , 10 , content = " # Existing note\n\n Body" )
933+ composeRule.onNodeWithText(" Existing note" ).performClick()
934+
935+ renameNote(" Grocery list" )
936+
937+ composeRule.waitForText(" Grocery list" )
938+ composeRule.waitUntil(timeoutMillis = 10_000 ) {
939+ application.fakeBackend.pushedNotes.any {
940+ it.title == " Grocery list" && it.content == " # Grocery list\n\n Body"
936941 }
937- )
942+ }
943+ assertEquals(listOf (" # Grocery list\n\n Body" ), noteContents(" alice" ))
938944 }
939945
940946 /* * A name that holds nothing a file system accepts would leave the note unreachable. */
@@ -1053,24 +1059,59 @@ class AppLaunchTest {
10531059 composeRule.onNodeWithTag(" note-find-field" ).assertDoesNotExist()
10541060 }
10551061
1062+ /* *
1063+ * An editing session that never pauses must still reach storage. Every round of typing
1064+ * restarts the idle save, so it never completes and whatever the database ends up holding was
1065+ * written by the periodic checkpoint. What is stored is only checked for the shape of the
1066+ * typed text, not for the newest of it, because a checkpoint is a snapshot of a moment that
1067+ * the writer has already typed past by the time it can be read back.
1068+ */
10561069 @Test
1057- fun editorDraftIsPeriodicallyCheckpointedWithoutStartingNetworkWork () {
1070+ fun editorDraftIsPeriodicallyCheckpointedWhileTypingNeverPauses () {
10581071 importAccount(" alice" , " Existing note" , " etag-1" , 10 )
10591072 val note = runBlocking { notesOf(" alice" ).single() }
10601073 composeRule.onNodeWithText(" Existing note" ).performClick()
10611074 composeRule.enterEditMode()
1062- onView(withId(R .id.markdown_editor)).perform(
1063- click(),
1064- replaceText(" # Edited\n\n Periodic checkpoint" )
1065- )
1075+ onView(withId(R .id.markdown_editor)).perform(click())
10661076
1067- composeRule.waitUntil(timeoutMillis = 2_000 ) {
1068- runBlocking {
1069- application.component.noteRepository.get(note.localId)?.content ==
1070- " # Edited\n\n Periodic checkpoint"
1071- }
1077+ var round = 0
1078+ composeRule.waitUntil(timeoutMillis = 30_000 ) {
1079+ onView(withId(R .id.markdown_editor))
1080+ .perform(replaceText(" # Edited\n\n Periodic checkpoint ${round++ } " ))
1081+ runBlocking { application.component.noteRepository.get(note.localId)?.content }
1082+ ?.startsWith(" # Edited\n\n Periodic checkpoint" ) == true
1083+ }
1084+ }
1085+
1086+ /* *
1087+ * A checkpoint is a local safety net, not an edit the writer finished, so it must not start
1088+ * network work of its own. Waiting past the synchronization delay is what makes the absence of
1089+ * an upload mean anything.
1090+ */
1091+ @Test
1092+ fun checkpointingADraftStartsNoNetworkWork () {
1093+ importAccount(" alice" , " Existing note" , " etag-1" , 10 )
1094+ val note = runBlocking { notesOf(" alice" ).single() }
1095+ val pullsBefore = application.fakeBackend.checkpoints.size
1096+
1097+ runBlocking {
1098+ application.component.checkpointDraft(note.localId, " # Edited\n\n Checkpoint" )
10721099 }
1073- assertTrue(application.fakeBackend.pushedNotes.isEmpty())
1100+
1101+ assertEquals(
1102+ " # Edited\n\n Checkpoint" ,
1103+ runBlocking { application.component.noteRepository.get(note.localId)?.content }
1104+ )
1105+ Thread .sleep(SYNC_DELAY_MILLIS * 2 )
1106+ assertTrue(
1107+ " a checkpoint must not upload the note" ,
1108+ application.fakeBackend.pushedNotes.isEmpty()
1109+ )
1110+ assertEquals(
1111+ " a checkpoint must not refresh the account" ,
1112+ pullsBefore,
1113+ application.fakeBackend.checkpoints.size
1114+ )
10741115 }
10751116
10761117 @Test
@@ -1166,6 +1207,31 @@ class AppLaunchTest {
11661207 onNodeWithTag(" note-menu" ).performClick()
11671208 }
11681209
1210+ /* *
1211+ * Renames the open note. Opening a note loads it from the repository, so its actions appear a
1212+ * recomposition later, and the dialog is only reachable through the note menu.
1213+ */
1214+ private fun renameNote (name : String , updateHeading : Boolean = true) {
1215+ composeRule.openNoteMenu()
1216+ composeRule.onNodeWithTag(" rename-note" ).performClick()
1217+ composeRule.waitForTag(" note-name-field" )
1218+ composeRule.onNodeWithTag(" note-name-field" ).performTextReplacement(name)
1219+ composeRule.onNodeWithTag(" update-heading-checkbox" ).assertIsOn()
1220+ if (! updateHeading) {
1221+ composeRule.onNodeWithTag(" update-heading-checkbox" ).performClick()
1222+ composeRule.onNodeWithTag(" update-heading-checkbox" ).assertIsOff()
1223+ }
1224+ composeRule.onNodeWithTag(" confirm-rename-note" ).performClick()
1225+ }
1226+
1227+ /* * What the notes of an account hold, read from the repository rather than from the screen. */
1228+ private fun noteContents (user : String ): List <String > = runBlocking {
1229+ application.component.noteRepository
1230+ .observeNotes(testAccount(user).localAccountId())
1231+ .first()
1232+ .map { it.content }
1233+ }
1234+
11691235 /* *
11701236 * Pull to refresh reacts to how far a drag has travelled by the time it is released. The
11711237 * injected gesture and the state that measures it advance on separate coroutines, so on a
@@ -1189,6 +1255,20 @@ class AppLaunchTest {
11891255 throw AssertionError (" pulling the note list down never reached the backend" )
11901256 }
11911257
1258+ /* *
1259+ * Waits for the editor to hold or release input focus. Focus is requested once the view has
1260+ * been attached and released as edit mode is left, so neither is true the moment the action
1261+ * that causes it returns. A released editor is gone from the hierarchy and holds no focus.
1262+ */
1263+ private fun awaitEditorFocus (focused : Boolean ) {
1264+ composeRule.waitUntil(timeoutMillis = 10_000 ) {
1265+ composeRule.runOnIdle {
1266+ composeRule.activity.findViewById<TextView >(R .id.markdown_editor)
1267+ ?.hasFocus() == true
1268+ } == focused
1269+ }
1270+ }
1271+
11921272 /* *
11931273 * Espresso injects key events into the input method, which commits the resulting characters
11941274 * back through an asynchronous input connection. Looping the main thread until it is idle does
@@ -1295,4 +1375,22 @@ class AppLaunchTest {
12951375 onAllNodesWithText(text, substring = substring).fetchSemanticsNodes().isNotEmpty()
12961376 }
12971377 }
1378+
1379+ /* *
1380+ * Waits for text to go, for lists that are answered by a query rather than filtered in place.
1381+ * Searching starts a new database flow and the previous result stays on screen until that flow
1382+ * emits, so the screen is idle while it still shows what the search is about to replace.
1383+ */
1384+ private fun androidx.compose.ui.test.junit4.AndroidComposeTestRule <* , * >.waitForTextToGo (
1385+ text : String
1386+ ) {
1387+ waitUntil(timeoutMillis = 10_000 ) {
1388+ onAllNodesWithText(text).fetchSemanticsNodes().isEmpty()
1389+ }
1390+ }
1391+
1392+ private companion object {
1393+ /* * How long `ApplicationComponent` lets a scheduled synchronization wait before it runs. */
1394+ const val SYNC_DELAY_MILLIS = 1_500L
1395+ }
12981396}
0 commit comments