1
1
package com.github.lookupgroup27.lookup.ui.image
2
2
3
3
import androidx.compose.ui.test.assertIsDisplayed
4
+ import androidx.compose.ui.test.assertTextEquals
4
5
import androidx.compose.ui.test.junit4.createComposeRule
5
6
import androidx.compose.ui.test.onNodeWithTag
7
+ import androidx.compose.ui.test.onNodeWithText
6
8
import androidx.compose.ui.test.performClick
9
+ import androidx.compose.ui.test.performImeAction
10
+ import androidx.compose.ui.test.performTextClearance
11
+ import androidx.compose.ui.test.performTextInput
7
12
import androidx.test.ext.junit.runners.AndroidJUnit4
8
13
import com.github.lookupgroup27.lookup.model.collection.CollectionRepository
9
14
import com.github.lookupgroup27.lookup.model.image.EditImageRepository
@@ -17,7 +22,10 @@ import org.junit.Rule
17
22
import org.junit.Test
18
23
import org.junit.runner.RunWith
19
24
import org.mockito.Mockito
25
+ import org.mockito.kotlin.any
26
+ import org.mockito.kotlin.eq
20
27
import org.mockito.kotlin.mock
28
+ import org.mockito.kotlin.times
21
29
import org.mockito.kotlin.verify
22
30
23
31
/* *
@@ -65,6 +73,7 @@ class EditImageScreenTest {
65
73
postAverageStar = 0.0 ,
66
74
postRatedByNb = 0 ,
67
75
postUid = " mock_uid" ,
76
+ postDescription = " mock_description" ,
68
77
editImageViewModel = editImageViewModel,
69
78
collectionViewModel = collectionViewModel,
70
79
navigationActions = mockNavigationActions,
@@ -85,6 +94,7 @@ class EditImageScreenTest {
85
94
postAverageStar = 0.0 ,
86
95
postRatedByNb = 0 ,
87
96
postUid = " mock_uid" ,
97
+ postDescription = " mock_description" ,
88
98
editImageViewModel = editImageViewModel,
89
99
collectionViewModel = collectionViewModel,
90
100
navigationActions = mockNavigationActions,
@@ -106,6 +116,7 @@ class EditImageScreenTest {
106
116
postAverageStar = 0.0 ,
107
117
postRatedByNb = 0 ,
108
118
postUid = " mock_uid" ,
119
+ postDescription = " mock_description" ,
109
120
editImageViewModel = editImageViewModel,
110
121
collectionViewModel = collectionViewModel,
111
122
navigationActions = mockNavigationActions,
@@ -124,6 +135,7 @@ class EditImageScreenTest {
124
135
postAverageStar = 0.0 ,
125
136
postRatedByNb = 0 ,
126
137
postUid = " mock_uid" ,
138
+ postDescription = " mock_description" ,
127
139
editImageViewModel = editImageViewModel,
128
140
collectionViewModel = collectionViewModel,
129
141
navigationActions = mockNavigationActions,
@@ -146,6 +158,7 @@ class EditImageScreenTest {
146
158
postAverageStar = 0.0 ,
147
159
postRatedByNb = 0 ,
148
160
postUid = " mock_uid" ,
161
+ postDescription = " mock_description" ,
149
162
editImageViewModel = editImageViewModel,
150
163
collectionViewModel = collectionViewModel,
151
164
navigationActions = mockNavigationActions,
@@ -166,6 +179,7 @@ class EditImageScreenTest {
166
179
postAverageStar = 0.0 ,
167
180
postRatedByNb = 0 ,
168
181
postUid = " mock_uid" ,
182
+ postDescription = " mock_description" ,
169
183
editImageViewModel = editImageViewModel,
170
184
collectionViewModel = collectionViewModel,
171
185
navigationActions = mockNavigationActions,
@@ -187,6 +201,7 @@ class EditImageScreenTest {
187
201
postAverageStar = 0.0 ,
188
202
postRatedByNb = 0 ,
189
203
postUid = " mock_uid" ,
204
+ postDescription = " mock_description" ,
190
205
editImageViewModel = editImageViewModel,
191
206
collectionViewModel = collectionViewModel,
192
207
navigationActions = mockNavigationActions,
@@ -205,6 +220,7 @@ class EditImageScreenTest {
205
220
postAverageStar = 0.0 ,
206
221
postRatedByNb = 0 ,
207
222
postUid = " mock_uid" ,
223
+ postDescription = " mock_description" ,
208
224
editImageViewModel = editImageViewModel,
209
225
collectionViewModel = collectionViewModel,
210
226
navigationActions = mockNavigationActions,
@@ -223,6 +239,7 @@ class EditImageScreenTest {
223
239
postAverageStar = 0.0 ,
224
240
postRatedByNb = 0 ,
225
241
postUid = " mock_uid" ,
242
+ postDescription = " mock_description" ,
226
243
editImageViewModel = editImageViewModel,
227
244
collectionViewModel = collectionViewModel,
228
245
navigationActions = mockNavigationActions,
@@ -241,6 +258,7 @@ class EditImageScreenTest {
241
258
postAverageStar = 0.0 ,
242
259
postRatedByNb = 0 ,
243
260
postUid = " mock_uid" ,
261
+ postDescription = " mock_description" ,
244
262
editImageViewModel = editImageViewModel,
245
263
collectionViewModel = collectionViewModel,
246
264
navigationActions = mockNavigationActions,
@@ -249,4 +267,148 @@ class EditImageScreenTest {
249
267
250
268
composeTestRule.onNodeWithTag(" rated_by_collection" ).assertIsDisplayed()
251
269
}
270
+
271
+ @Test
272
+ fun testDescriptionBoxIsDisplayed () {
273
+ composeTestRule.setContent {
274
+ EditImageScreen (
275
+ postUri = " mock_image_url" ,
276
+ postAverageStar = 4.5 ,
277
+ postRatedByNb = 20 ,
278
+ postUid = " mock_uid" ,
279
+ postDescription = " mock_description" ,
280
+ editImageViewModel = editImageViewModel,
281
+ collectionViewModel = collectionViewModel,
282
+ navigationActions = mockNavigationActions,
283
+ postsViewModel = postsViewModel)
284
+ }
285
+
286
+ composeTestRule.onNodeWithTag(" description_text" ).assertIsDisplayed()
287
+ }
288
+
289
+ @Test
290
+ fun testEditFieldAppearsOnClick () {
291
+ composeTestRule.setContent {
292
+ EditImageScreen (
293
+ postUri = " mock_image_url" ,
294
+ postAverageStar = 4.5 ,
295
+ postRatedByNb = 20 ,
296
+ postUid = " mock_uid" ,
297
+ postDescription = " mock_description" ,
298
+ editImageViewModel = editImageViewModel,
299
+ collectionViewModel = collectionViewModel,
300
+ navigationActions = mockNavigationActions,
301
+ postsViewModel = postsViewModel)
302
+ }
303
+
304
+ // Simulate clicking the description box
305
+ composeTestRule.onNodeWithTag(" description_text" ).performClick()
306
+
307
+ // Verify that the edit field appears
308
+ composeTestRule.onNodeWithTag(" edit_description_field" ).assertIsDisplayed()
309
+ }
310
+
311
+ @Test
312
+ fun testOnDoneKeyboardActionDisplaysConfirmationDialogAndSavesDescription () {
313
+ val testPostUid = " mock_uid"
314
+ val initialDescription = " mock_description"
315
+ val updatedDescription = " updated_description"
316
+
317
+ composeTestRule.setContent {
318
+ EditImageScreen (
319
+ postUri = " mock_image_url" ,
320
+ postAverageStar = 4.5 ,
321
+ postRatedByNb = 20 ,
322
+ postUid = testPostUid,
323
+ postDescription = initialDescription,
324
+ editImageViewModel = editImageViewModel,
325
+ collectionViewModel = collectionViewModel,
326
+ navigationActions = mockNavigationActions,
327
+ postsViewModel = postsViewModel)
328
+ }
329
+
330
+ // Click on the description text to enter editing mode
331
+ composeTestRule.onNodeWithTag(" description_text" ).performClick()
332
+
333
+ // Verify the edit field appears
334
+ composeTestRule.onNodeWithTag(" edit_description_field" ).assertIsDisplayed()
335
+
336
+ // Input a new description (resetting any previous value first)
337
+ composeTestRule.onNodeWithTag(" edit_description_field" ).performTextClearance()
338
+ composeTestRule.onNodeWithTag(" edit_description_field" ).performTextInput(updatedDescription)
339
+
340
+ // Simulate the "Done" action
341
+ composeTestRule.onNodeWithTag(" edit_description_field" ).performImeAction()
342
+
343
+ // Verify that the confirmation dialog appears
344
+ composeTestRule.onNodeWithText(" Save Changes?" ).assertIsDisplayed()
345
+ composeTestRule.onNodeWithText(" Do you want to save the new description?" ).assertIsDisplayed()
346
+
347
+ // Click on the "Save" button in the dialog
348
+ composeTestRule.onNodeWithText(" Save" ).performClick()
349
+
350
+ // Verify that `updateDescription` is called with correct arguments
351
+ verify(postsRepository).updateDescription(eq(testPostUid), eq(updatedDescription), any(), any())
352
+
353
+ // Verify that the description text displays the updated description
354
+ composeTestRule.onNodeWithTag(" description_text" ).assertTextEquals(updatedDescription)
355
+
356
+ // Verify that the editing mode has exited
357
+ composeTestRule.onNodeWithTag(" edit_description_field" ).assertDoesNotExist()
358
+
359
+ // Ensure the confirmation dialog is dismissed
360
+ composeTestRule.onNodeWithText(" Save Changes?" ).assertDoesNotExist()
361
+ }
362
+
363
+ @Test
364
+ fun testOnDoneKeyboardActionDisplaysConfirmationDialogAndDiscardsDescription () {
365
+ val testPostUid = " mock_uid"
366
+ val initialDescription = " mock_description"
367
+ val updatedDescription = " updated_description"
368
+
369
+ composeTestRule.setContent {
370
+ EditImageScreen (
371
+ postUri = " mock_image_url" ,
372
+ postAverageStar = 4.5 ,
373
+ postRatedByNb = 20 ,
374
+ postUid = testPostUid,
375
+ postDescription = initialDescription,
376
+ editImageViewModel = editImageViewModel,
377
+ collectionViewModel = collectionViewModel,
378
+ navigationActions = mockNavigationActions,
379
+ postsViewModel = postsViewModel)
380
+ }
381
+
382
+ // Click on the description text to enter editing mode
383
+ composeTestRule.onNodeWithTag(" description_text" ).performClick()
384
+
385
+ // Verify the edit field appears
386
+ composeTestRule.onNodeWithTag(" edit_description_field" ).assertIsDisplayed()
387
+
388
+ // Input a new description (resetting any previous value first)
389
+ composeTestRule.onNodeWithTag(" edit_description_field" ).performTextClearance()
390
+ composeTestRule.onNodeWithTag(" edit_description_field" ).performTextInput(updatedDescription)
391
+
392
+ // Simulate the "Done" action
393
+ composeTestRule.onNodeWithTag(" edit_description_field" ).performImeAction()
394
+
395
+ // Verify that the confirmation dialog appears
396
+ composeTestRule.onNodeWithText(" Save Changes?" ).assertIsDisplayed()
397
+ composeTestRule.onNodeWithText(" Do you want to save the new description?" ).assertIsDisplayed()
398
+
399
+ // Click on the "Discard" button in the dialog
400
+ composeTestRule.onNodeWithText(" Discard" ).performClick()
401
+
402
+ // Verify that `updateDescription` is NOT called
403
+ verify(postsRepository, times(0 )).updateDescription(any(), any(), any(), any())
404
+
405
+ // Verify that the description text displays the initial description
406
+ composeTestRule.onNodeWithTag(" description_text" ).assertTextEquals(initialDescription)
407
+
408
+ // Verify that the editing mode has exited
409
+ composeTestRule.onNodeWithTag(" edit_description_field" ).assertDoesNotExist()
410
+
411
+ // Ensure the confirmation dialog is dismissed
412
+ composeTestRule.onNodeWithText(" Save Changes?" ).assertDoesNotExist()
413
+ }
252
414
}
0 commit comments