@@ -24,6 +24,7 @@ import android.content.ContentValues
2424import android.content.Context
2525import android.content.Intent
2626import android.graphics.Bitmap
27+ import android.graphics.BitmapFactory
2728import android.graphics.Color
2829import android.net.Uri
2930import android.os.Build
@@ -75,6 +76,7 @@ import org.hamcrest.core.IsNot
7576import org.junit.After
7677import org.junit.Assert.assertEquals
7778import org.junit.Assert.assertFalse
79+ import org.junit.Assert.assertNotEquals
7880import org.junit.Assert.assertNotNull
7981import org.junit.Assert.assertNotSame
8082import org.junit.Assert.assertTrue
@@ -116,26 +118,28 @@ class MenuFileActivityIntegrationTest {
116118
117119 @After
118120 fun tearDown () {
119- for (file in deletionFileList) {
120- if (file != null && file .exists()) {
121- assertTrue(file .delete())
121+ for (uri in deletionFileList) {
122+ if (uri != null && uri .exists()) {
123+ assertTrue(uri .delete())
122124 }
123125 }
124126 IdlingRegistry .getInstance().unregister(idlingResource)
125127 }
126128
127129 @Test
128130 fun testNewEmptyDrawingWithSave () {
131+ FileIO .fileType = FileIO .FileType .PNG
129132 onDrawingSurfaceView().perform(touchAt(MIDDLE ))
130133 onDrawingSurfaceView().checkPixelColor(Color .BLACK , BitmapLocationProvider .MIDDLE )
131134 onTopBarView().performOpenMoreOptions()
132135 onView(withText(R .string.menu_new_image)).perform(click())
133136 onView(withText(R .string.save_button_text)).perform(click())
134- onView(isRoot()).perform(waitFor( 100 ))
137+ val fileName = " test987654 "
135138 onView(withId(R .id.pocketpaint_image_name_save_text))
136- .perform(replaceText(" test987654 " ))
139+ .perform(replaceText(fileName ))
137140 onView(withText(R .string.save_button_text)).perform(click())
138- onView(isRoot()).perform(waitFor(100 ))
141+ onView(isRoot()).perform(waitFor(500 ))
142+ addFileToDeletionFileList(fileName, " png" , Environment .DIRECTORY_PICTURES )
139143 onDrawingSurfaceView().checkPixelColor(Color .TRANSPARENT , BitmapLocationProvider .MIDDLE )
140144 }
141145
@@ -239,7 +243,6 @@ class MenuFileActivityIntegrationTest {
239243 onView(withId(R .id.pocketpaint_image_name_save_text))
240244 .perform(replaceText(" test98765" ))
241245 onView(withText(R .string.save_button_text)).perform(click())
242- onView(isRoot()).perform(waitFor(100 ))
243246 assertNotNull(activity.model.savedPictureUri)
244247 addUriToDeletionFileList(activity.model.savedPictureUri)
245248 assertTrue(activity.model.isSaved)
@@ -268,21 +271,31 @@ class MenuFileActivityIntegrationTest {
268271 .edit()
269272 .clear()
270273 .commit()
274+
271275 onDrawingSurfaceView().perform(touchAt(MIDDLE ))
272276 onTopBarView().performOpenMoreOptions()
273277 onView(withText(R .string.menu_save_image)).perform(click())
274278 onView(withId(R .id.pocketpaint_image_name_save_text))
275279 .perform(replaceText(" testSaveCopy" ))
276280 onView(withText(R .string.save_button_text)).perform(click())
281+
277282 assertNotNull(activity.model.savedPictureUri)
278283 if (! activity.model.isOpenedFromCatroid) {
279284 assertNotSame(
280285 " null" ,
281286 MainActivityPresenter .getPathFromUri(activity, activity.model.savedPictureUri!! )
282287 )
283288 }
289+
284290 addUriToDeletionFileList(activity.model.savedPictureUri)
285- val oldFile = File (activity.model.savedPictureUri.toString())
291+ val savedImageFile =
292+ activity.model.savedPictureUri?.let {
293+ MainActivityPresenter .getPathFromUri(
294+ activity,
295+ it
296+ )
297+ }
298+ ?.let { File (it) }
286299 onView(withText(R .string.pocketpaint_no)).perform(click())
287300 onView(withText(R .string.pocketpaint_ok)).perform(click())
288301 onDrawingSurfaceView().perform(touchAt(HALFWAY_BOTTOM_MIDDLE ))
@@ -291,17 +304,13 @@ class MenuFileActivityIntegrationTest {
291304 onView(withId(R .id.pocketpaint_image_name_save_text))
292305 .perform(replaceText(" copy1" ))
293306 onView(withText(R .string.save_button_text)).perform(click())
294- onView(isRoot()).perform(waitFor(100 ))
295- val newFile = File (activity.model.savedPictureUri.toString())
296- assertNotSame(" Changes to saved" , oldFile, newFile)
297- assertNotNull(activity.model.savedPictureUri)
298- if (! activity.model.isOpenedFromCatroid) {
299- assertNotSame(
300- " null" ,
301- MainActivityPresenter .getPathFromUri(activity, activity.model.savedPictureUri!! )
302- )
303- }
304- addUriToDeletionFileList(activity.model.savedPictureUri)
307+
308+ val savedCopyFile = createFileFromFileName(" copy1" , " png" , Environment .DIRECTORY_PICTURES )
309+ val bitmap1 = BitmapFactory .decodeFile(savedImageFile?.absolutePath)
310+ val bitmap2 = BitmapFactory .decodeFile(savedCopyFile.absolutePath)
311+ assertNotEquals(" Bitmaps should not be the same" , bitmap1, bitmap2)
312+
313+ addFileToDeletionFileList(" copy1" , " png" , Environment .DIRECTORY_PICTURES )
305314 }
306315
307316 @Test
@@ -328,13 +337,12 @@ class MenuFileActivityIntegrationTest {
328337 onView(withId(R .id.pocketpaint_image_name_save_text))
329338 .perform(replaceText(" 12345test12345" ))
330339 onView(withText(R .string.save_button_text)).perform(click())
331- onView(isRoot()).perform(waitFor(100 ))
340+ onView(isRoot()).perform(waitFor(300 ))
332341 assertNotNull(activity.model.savedPictureUri)
333342 addUriToDeletionFileList(activity.model.savedPictureUri)
334343 onTopBarView().performOpenMoreOptions()
335344 onView(withText(R .string.menu_save_image)).perform(click())
336345 onView(withText(R .string.save_button_text)).perform(click())
337- onView(isRoot()).perform(waitFor(100 ))
338346 onView(withText(R .string.overwrite_button_text)).check(matches(isDisplayed()))
339347 }
340348
@@ -351,7 +359,6 @@ class MenuFileActivityIntegrationTest {
351359 onDrawingSurfaceView().perform(touchAt(MIDDLE ))
352360 onTopBarView().performOpenMoreOptions()
353361 onView(withText(R .string.menu_save_image)).perform(click())
354- onView(isRoot()).perform(waitFor(200 ))
355362 val newImageNumber = launchActivityRule.activity.presenter.imageNumber
356363 assertEquals((imageNumber + 1 ).toLong(), newImageNumber.toLong())
357364 }
@@ -365,7 +372,6 @@ class MenuFileActivityIntegrationTest {
365372 onView(withId(R .id.pocketpaint_image_name_save_text))
366373 .perform(replaceText(" test9876" ))
367374 onView(withText(R .string.save_button_text)).perform(click())
368- onView(isRoot()).perform(waitFor(100 ))
369375 assertNotNull(activity.model.savedPictureUri)
370376 addUriToDeletionFileList(activity.model.savedPictureUri)
371377 val newImageNumber = launchActivityRule.activity.presenter.imageNumber
@@ -386,7 +392,6 @@ class MenuFileActivityIntegrationTest {
386392 onView(withId(R .id.pocketpaint_image_name_save_text))
387393 .perform(replaceText(defaultFileName))
388394 onView(withText(R .string.save_button_text)).perform(click())
389- onView(isRoot()).perform(waitFor(100 ))
390395 assertNotNull(activity.model.savedPictureUri)
391396 addUriToDeletionFileList(activity.model.savedPictureUri)
392397 val oldFile = File (activity.model.savedPictureUri.toString())
@@ -398,7 +403,6 @@ class MenuFileActivityIntegrationTest {
398403 onView(withId(R .id.pocketpaint_image_name_save_text))
399404 .perform(replaceText(defaultFileName))
400405 onView(withText(R .string.save_button_text)).perform(click())
401- onView(isRoot()).perform(waitFor(100 ))
402406 assertNotNull(activity.model.savedPictureUri)
403407 addUriToDeletionFileList(activity.model.savedPictureUri)
404408 val newFile = File (activity.model.savedPictureUri.toString())
@@ -450,7 +454,6 @@ class MenuFileActivityIntegrationTest {
450454 onData(allOf(`is `(instanceOf<Any >(String ::class .java)), `is `(" png" )))
451455 .inRoot(RootMatchers .isPlatformPopup()).perform(click())
452456 onView(withText(R .string.save_button_text)).perform(click())
453- onView(isRoot()).perform(waitFor(100 ))
454457 assertNotNull(activity.model.savedPictureUri)
455458 addUriToDeletionFileList(activity.model.savedPictureUri)
456459 onTopBarView().performOpenMoreOptions()
@@ -472,6 +475,7 @@ class MenuFileActivityIntegrationTest {
472475 .inRoot(RootMatchers .isPlatformPopup()).perform(click())
473476 onView(withText(R .string.save_button_text)).perform(click())
474477 onView(isRoot()).perform(waitFor(100 ))
478+ addFileToDeletionFileList(" image$imageNumber " , " png" , Environment .DIRECTORY_PICTURES )
475479 onTopBarView().performOpenMoreOptions()
476480 onView(withText(R .string.menu_save_copy)).perform(click())
477481 imageNumber = launchActivityRule.activity.presenter.imageNumber
@@ -498,14 +502,14 @@ class MenuFileActivityIntegrationTest {
498502 val imageUri = resolver.insert(MediaStore .Images .Media .EXTERNAL_CONTENT_URI , contentValues)
499503 try {
500504 val fos = imageUri?.let { resolver.openOutputStream(it) }
501- assertTrue(bitmap.compress(Bitmap .CompressFormat .JPEG , 100 , fos))
505+ fos?.let { bitmap.compress(Bitmap .CompressFormat .JPEG , 100 , it) }
506+ ?.let { assertTrue(it) }
502507 assert (fos != null )
503508 fos?.close()
504509 } catch (e: IOException ) {
505510 throw AssertionError (" Picture file could not be created." , e)
506511 }
507- val imageFile = File (imageUri?.path, " testfile.jpg" )
508- deletionFileList.add(imageFile)
512+ addUriToDeletionFileList(imageUri)
509513 return imageUri
510514 }
511515
@@ -534,15 +538,14 @@ class MenuFileActivityIntegrationTest {
534538 onView(withText(R .string.menu_save_image)).perform(click())
535539 onView(withText(R .string.save_button_text)).perform(click())
536540 onView(isRoot()).perform(waitFor(200 ))
541+ addUriToDeletionFileList(activity.model.savedPictureUri)
537542 val uri = activity.model.savedPictureUri
538543 onDrawingSurfaceView().perform(touchAt(MIDDLE ))
539544 onTopBarView().performOpenMoreOptions()
540545 onView(withText(R .string.menu_save_image)).perform(click())
541546 onView(withText(R .string.save_button_text)).perform(click())
542- onView(isRoot()).perform(waitFor(100 ))
543547 onView(withText(R .string.overwrite_button_text)).check(matches(isDisplayed()))
544548 onView(withText(R .string.overwrite_button_text)).perform(click())
545- onView(isRoot()).perform(waitFor(500 ))
546549
547550 val oldFileName = uri?.path?.let { File (it).name }
548551 val newFileName = activity.model.savedPictureUri?.path?.let { File (it).name }
@@ -561,12 +564,14 @@ class MenuFileActivityIntegrationTest {
561564 onView(withText(R .string.menu_save_image)).perform(click())
562565 onView(withText(R .string.save_button_text)).perform(click())
563566 onView(isRoot()).perform(waitFor(200 ))
567+ addUriToDeletionFileList(activity.model.savedPictureUri)
564568 val uri = activity.model.savedPictureUri
565569 onDrawingSurfaceView().perform(touchAt(MIDDLE ))
566570 onTopBarView().performOpenMoreOptions()
567571 onView(withText(R .string.menu_save_image)).perform(click())
568572 onView(withText(R .string.save_button_text)).perform(click())
569573 onView(isRoot()).perform(waitFor(100 ))
574+ addUriToDeletionFileList(activity.model.savedPictureUri)
570575 onView(withText(R .string.overwrite_button_text)).check(matches(isDisplayed()))
571576 onView(withText(R .string.overwrite_button_text)).perform(click())
572577 onView(isRoot()).perform(waitFor(500 ))
@@ -645,21 +650,35 @@ class MenuFileActivityIntegrationTest {
645650 assertEquals(oldFileName, newFileName)
646651
647652 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q ) {
648- addFileToDeletionFileList(name, FileIO .fileType.value)
653+ addFileToDeletionFileList(name, FileIO .fileType.value, Environment . DIRECTORY_DOWNLOADS )
649654 } else {
650655 addUriToDeletionFileList(activity.model.savedPictureUri)
651656 }
652657 }
653658
654659 private fun addUriToDeletionFileList (uri : Uri ? ) {
655- uri?.path?.let {
656- deletionFileList.add(File (it))
660+ uri?.let {
661+ val path = MainActivityPresenter .getPathFromUri(activity, it)
662+ deletionFileList.add(File (path))
657663 }
658664 }
659665
660- private fun addFileToDeletionFileList (fileName : String? , extension : String? ) {
661- val dir = Environment .getExternalStoragePublicDirectory(Environment .DIRECTORY_DOWNLOADS )
662- val file = File (dir, " $fileName .$extension " )
666+ private fun addFileToDeletionFileList (
667+ fileName : String? ,
668+ extension : String? ,
669+ directory : String
670+ ) {
671+ val file = createFileFromFileName(fileName, extension, directory)
663672 deletionFileList.add(file)
664673 }
674+
675+ private fun createFileFromFileName (
676+ fileName : String? ,
677+ extension : String? ,
678+ directory : String
679+ ): File {
680+ val dir = Environment .getExternalStoragePublicDirectory(directory)
681+ val file = File (dir, " $fileName .$extension " )
682+ return file
683+ }
665684}
0 commit comments