@@ -8,15 +8,21 @@ import androidx.compose.material3.MaterialTheme
88import androidx.compose.ui.geometry.Offset
99import androidx.compose.ui.semantics.SemanticsProperties
1010import androidx.compose.ui.test.assertIsDisplayed
11+ import androidx.compose.ui.test.assertIsEnabled
12+ import androidx.compose.ui.test.assertIsNotEnabled
13+ import androidx.compose.ui.test.assertIsOn
1114import androidx.compose.ui.test.assertTextEquals
15+ import androidx.compose.ui.test.click
1216import androidx.compose.ui.test.doubleClick
1317import androidx.compose.ui.test.junit4.createComposeRule
1418import androidx.compose.ui.test.onAllNodesWithTag
1519import androidx.compose.ui.test.onNodeWithContentDescription
1620import androidx.compose.ui.test.onNodeWithTag
1721import androidx.compose.ui.test.performClick
22+ import androidx.compose.ui.test.performTextInput
1823import androidx.compose.ui.test.performTouchInput
1924import androidx.compose.ui.test.pinch
25+ import androidx.compose.ui.test.swipe
2026import androidx.compose.ui.test.swipeDown
2127import androidx.test.ext.junit.runners.AndroidJUnit4
2228import androidx.test.platform.app.InstrumentationRegistry
@@ -35,14 +41,19 @@ class NoteImageViewerTest {
3541
3642 private var dismissed = false
3743
38- private fun showViewer (description : String = "Whiteboard ") {
44+ private fun showViewer (
45+ description : String = "Whiteboard ",
46+ onSaveAnnotation : (suspend (Bitmap , Boolean ) -> Unit )? = null
47+ ) {
3948 val bitmap = Bitmap .createBitmap(300 , 200 , Bitmap .Config .ARGB_8888 ).apply {
4049 eraseColor(Color .GREEN )
4150 }
4251 composeRule.setContent {
4352 MaterialTheme {
4453 if (! dismissed) {
45- NoteImageViewer (NoteImage (bitmap, description)) { dismissed = true }
54+ NoteImageViewer (NoteImage (bitmap, description), onSaveAnnotation) {
55+ dismissed = true
56+ }
4657 }
4758 }
4859 }
@@ -117,6 +128,90 @@ class NoteImageViewerTest {
117128 assertEquals(1 , File (context.cacheDir, " copied-images" ).listFiles()!! .size)
118129 }
119130
131+ @Test
132+ fun annotatingDrawsUndoesRedoesAndSavesAsANewImage () {
133+ var saved: Pair <Bitmap , Boolean >? = null
134+ showViewer { annotated, replace -> saved = annotated to replace }
135+ composeRule.onNodeWithTag(" image-viewer-annotate" ).performClick()
136+ composeRule.onNodeWithTag(" image-annotation-done" ).assertIsNotEnabled()
137+ composeRule.onNodeWithTag(" image-annotation-tool-pen" ).assertIsOn()
138+ // Two fingers zoom rather than draw; zooming back out keeps the screen-to-image mapping.
139+ composeRule.onNodeWithTag(" image-annotation-canvas" ).performTouchInput {
140+ pinch(
141+ center - Offset (20f , 0f ),
142+ center - Offset (250f , 0f ),
143+ center + Offset (20f , 0f ),
144+ center + Offset (250f , 0f )
145+ )
146+ }
147+ composeRule.onNodeWithTag(" image-annotation-done" ).assertIsNotEnabled()
148+ composeRule.onNodeWithTag(" image-annotation-canvas" ).performTouchInput {
149+ pinch(
150+ center - Offset (250f , 0f ),
151+ center - Offset (20f , 0f ),
152+ center + Offset (250f , 0f ),
153+ center + Offset (20f , 0f )
154+ )
155+ }
156+ composeRule.onNodeWithTag(" image-annotation-done" ).assertIsNotEnabled()
157+
158+ // The image fills the screen width, so its middle row runs across the screen's middle.
159+ drawAcrossTheMiddle()
160+ composeRule.onNodeWithTag(" image-annotation-done" ).assertIsEnabled()
161+ composeRule.onNodeWithTag(" image-annotation-undo" ).performClick()
162+ composeRule.onNodeWithTag(" image-annotation-done" ).assertIsNotEnabled()
163+ composeRule.onNodeWithTag(" image-annotation-redo" ).performClick()
164+
165+ composeRule.onNodeWithTag(" image-annotation-done" ).performClick()
166+ composeRule.onNodeWithTag(" image-annotation-add" ).performClick()
167+ composeRule.waitUntil(5_000 ) { dismissed }
168+
169+ val (annotated, replace) = saved!!
170+ assertFalse(replace)
171+ assertEquals(300 , annotated.width)
172+ val marked = annotated.getPixel(150 , 100 )
173+ assertTrue(Color .red(marked) > 200 && Color .green(marked) < 100 )
174+ // Away from the stroke the image is unchanged.
175+ assertEquals(Color .GREEN , annotated.getPixel(150 , 20 ))
176+ }
177+
178+ @Test
179+ fun annotationsCanBeErasedAndClosingAsksBeforeDiscardingThem () {
180+ showViewer()
181+ composeRule.onNodeWithTag(" image-viewer-annotate" ).performClick()
182+ drawAcrossTheMiddle()
183+ composeRule.onNodeWithTag(" image-annotation-tool-eraser" ).performClick()
184+ composeRule.onNodeWithTag(" image-annotation-canvas" ).performTouchInput { click(center) }
185+ composeRule.onNodeWithTag(" image-annotation-done" ).assertIsNotEnabled()
186+ composeRule.onNodeWithTag(" image-annotation-undo" ).performClick()
187+
188+ composeRule.onNodeWithTag(" image-annotation-close" ).performClick()
189+ composeRule.onNodeWithTag(" image-annotation-discard" ).performClick()
190+ waitUntilGone(" image-annotation-editor" )
191+ composeRule.onNodeWithTag(" image-viewer-image" ).assertIsDisplayed()
192+ assertFalse(dismissed)
193+ }
194+
195+ @Test
196+ fun textToolAddsALabelAndImagesOutsideEditableNotesCanOnlyBeCopied () {
197+ showViewer()
198+ composeRule.onNodeWithTag(" image-viewer-annotate" ).performClick()
199+ composeRule.onNodeWithTag(" image-annotation-tool-text" ).performClick()
200+ composeRule.onNodeWithTag(" image-annotation-canvas" ).performTouchInput { click(center) }
201+ composeRule.onNodeWithTag(" image-annotation-text-field" ).performTextInput(" Look here" )
202+ composeRule.onNodeWithTag(" image-annotation-text-done" ).performClick()
203+ composeRule.onNodeWithTag(" image-annotation-done" ).assertIsEnabled().performClick()
204+
205+ composeRule.onNodeWithTag(" image-annotation-add" ).assertIsNotEnabled()
206+ composeRule.onNodeWithTag(" image-annotation-replace" ).assertIsNotEnabled()
207+ composeRule.onNodeWithTag(" image-annotation-copy" ).assertIsEnabled()
208+ }
209+
210+ private fun drawAcrossTheMiddle () =
211+ composeRule.onNodeWithTag(" image-annotation-canvas" ).performTouchInput {
212+ swipe(Offset (width * 0.2f , centerY), Offset (width * 0.8f , centerY), 300 )
213+ }
214+
120215 private fun zoomText (): String? = composeRule.onAllNodesWithTag(" image-viewer-zoom" )
121216 .fetchSemanticsNodes().firstOrNull()?.config
122217 ?.getOrElseNullable(SemanticsProperties .Text ) { null }
0 commit comments