Skip to content

Commit

Permalink
PAINTROID-762 Update to Android 14
Browse files Browse the repository at this point in the history
  • Loading branch information
foenabua committed Oct 27, 2024
1 parent 87082dd commit 07b8aee
Showing 1 changed file with 43 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ import android.content.ContentValues
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Color
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.provider.MediaStore
import androidx.core.net.toUri
import androidx.test.espresso.Espresso.onData
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.Espresso.pressBack
Expand Down Expand Up @@ -75,10 +77,12 @@ import org.hamcrest.core.IsNot
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNotSame
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.BeforeClass
import org.junit.ClassRule
import org.junit.Rule
import org.junit.Test
Expand Down Expand Up @@ -116,26 +120,28 @@ class MenuFileActivityIntegrationTest {

@After
fun tearDown() {
for (file in deletionFileList) {
if (file != null && file.exists()) {
assertTrue(file.delete())
for (uri in deletionFileList) {
if (uri != null && uri.exists()) {
assertTrue(uri.delete())
}
}
IdlingRegistry.getInstance().unregister(idlingResource)
}

@Test
fun testNewEmptyDrawingWithSave() {
FileIO.fileType = FileIO.FileType.PNG
onDrawingSurfaceView().perform(touchAt(MIDDLE))
onDrawingSurfaceView().checkPixelColor(Color.BLACK, BitmapLocationProvider.MIDDLE)
onTopBarView().performOpenMoreOptions()
onView(withText(R.string.menu_new_image)).perform(click())
onView(withText(R.string.save_button_text)).perform(click())
onView(isRoot()).perform(waitFor(100))
val fileName = "test987654"
onView(withId(R.id.pocketpaint_image_name_save_text))
.perform(replaceText("test987654"))
.perform(replaceText(fileName))
onView(withText(R.string.save_button_text)).perform(click())
onView(isRoot()).perform(waitFor(100))
onView(isRoot()).perform(waitFor(500))
addFileToDeletionFileList(fileName, "png", Environment.DIRECTORY_PICTURES)
onDrawingSurfaceView().checkPixelColor(Color.TRANSPARENT, BitmapLocationProvider.MIDDLE)
}

Expand Down Expand Up @@ -239,7 +245,6 @@ class MenuFileActivityIntegrationTest {
onView(withId(R.id.pocketpaint_image_name_save_text))
.perform(replaceText("test98765"))
onView(withText(R.string.save_button_text)).perform(click())
onView(isRoot()).perform(waitFor(100))
assertNotNull(activity.model.savedPictureUri)
addUriToDeletionFileList(activity.model.savedPictureUri)
assertTrue(activity.model.isSaved)
Expand Down Expand Up @@ -268,21 +273,26 @@ class MenuFileActivityIntegrationTest {
.edit()
.clear()
.commit()

onDrawingSurfaceView().perform(touchAt(MIDDLE))
onTopBarView().performOpenMoreOptions()
onView(withText(R.string.menu_save_image)).perform(click())
onView(withId(R.id.pocketpaint_image_name_save_text))
.perform(replaceText("testSaveCopy"))
onView(withText(R.string.save_button_text)).perform(click())

assertNotNull(activity.model.savedPictureUri)
if (!activity.model.isOpenedFromCatroid) {
assertNotSame(
"null",
MainActivityPresenter.getPathFromUri(activity, activity.model.savedPictureUri!!)
)
}

addUriToDeletionFileList(activity.model.savedPictureUri)
val oldFile = File(activity.model.savedPictureUri.toString())
val savedImageFile =
activity.model.savedPictureUri?.let {MainActivityPresenter.getPathFromUri(activity, it) }
?.let { File(it) }
onView(withText(R.string.pocketpaint_no)).perform(click())
onView(withText(R.string.pocketpaint_ok)).perform(click())
onDrawingSurfaceView().perform(touchAt(HALFWAY_BOTTOM_MIDDLE))
Expand All @@ -291,17 +301,13 @@ class MenuFileActivityIntegrationTest {
onView(withId(R.id.pocketpaint_image_name_save_text))
.perform(replaceText("copy1"))
onView(withText(R.string.save_button_text)).perform(click())
onView(isRoot()).perform(waitFor(100))
val newFile = File(activity.model.savedPictureUri.toString())
assertNotSame("Changes to saved", oldFile, newFile)
assertNotNull(activity.model.savedPictureUri)
if (!activity.model.isOpenedFromCatroid) {
assertNotSame(
"null",
MainActivityPresenter.getPathFromUri(activity, activity.model.savedPictureUri!!)
)
}
addUriToDeletionFileList(activity.model.savedPictureUri)

val savedCopyFile = createFileFromFileName("copy1", "png", Environment.DIRECTORY_PICTURES)
val bitmap1 = BitmapFactory.decodeFile(savedImageFile?.absolutePath)
val bitmap2 = BitmapFactory.decodeFile(savedCopyFile.absolutePath)
assertNotEquals("Bitmaps should not be the same", bitmap1, bitmap2)

addFileToDeletionFileList("copy1","png", Environment.DIRECTORY_PICTURES)
}

@Test
Expand All @@ -328,13 +334,12 @@ class MenuFileActivityIntegrationTest {
onView(withId(R.id.pocketpaint_image_name_save_text))
.perform(replaceText("12345test12345"))
onView(withText(R.string.save_button_text)).perform(click())
onView(isRoot()).perform(waitFor(100))
onView(isRoot()).perform(waitFor(300))
assertNotNull(activity.model.savedPictureUri)
addUriToDeletionFileList(activity.model.savedPictureUri)
onTopBarView().performOpenMoreOptions()
onView(withText(R.string.menu_save_image)).perform(click())
onView(withText(R.string.save_button_text)).perform(click())
onView(isRoot()).perform(waitFor(100))
onView(withText(R.string.overwrite_button_text)).check(matches(isDisplayed()))
}

Expand All @@ -351,7 +356,6 @@ class MenuFileActivityIntegrationTest {
onDrawingSurfaceView().perform(touchAt(MIDDLE))
onTopBarView().performOpenMoreOptions()
onView(withText(R.string.menu_save_image)).perform(click())
onView(isRoot()).perform(waitFor(200))
val newImageNumber = launchActivityRule.activity.presenter.imageNumber
assertEquals((imageNumber + 1).toLong(), newImageNumber.toLong())
}
Expand All @@ -365,7 +369,6 @@ class MenuFileActivityIntegrationTest {
onView(withId(R.id.pocketpaint_image_name_save_text))
.perform(replaceText("test9876"))
onView(withText(R.string.save_button_text)).perform(click())
onView(isRoot()).perform(waitFor(100))
assertNotNull(activity.model.savedPictureUri)
addUriToDeletionFileList(activity.model.savedPictureUri)
val newImageNumber = launchActivityRule.activity.presenter.imageNumber
Expand All @@ -386,7 +389,6 @@ class MenuFileActivityIntegrationTest {
onView(withId(R.id.pocketpaint_image_name_save_text))
.perform(replaceText(defaultFileName))
onView(withText(R.string.save_button_text)).perform(click())
onView(isRoot()).perform(waitFor(100))
assertNotNull(activity.model.savedPictureUri)
addUriToDeletionFileList(activity.model.savedPictureUri)
val oldFile = File(activity.model.savedPictureUri.toString())
Expand All @@ -398,7 +400,6 @@ class MenuFileActivityIntegrationTest {
onView(withId(R.id.pocketpaint_image_name_save_text))
.perform(replaceText(defaultFileName))
onView(withText(R.string.save_button_text)).perform(click())
onView(isRoot()).perform(waitFor(100))
assertNotNull(activity.model.savedPictureUri)
addUriToDeletionFileList(activity.model.savedPictureUri)
val newFile = File(activity.model.savedPictureUri.toString())
Expand Down Expand Up @@ -450,7 +451,6 @@ class MenuFileActivityIntegrationTest {
onData(allOf(`is`(instanceOf<Any>(String::class.java)), `is`("png")))
.inRoot(RootMatchers.isPlatformPopup()).perform(click())
onView(withText(R.string.save_button_text)).perform(click())
onView(isRoot()).perform(waitFor(100))
assertNotNull(activity.model.savedPictureUri)
addUriToDeletionFileList(activity.model.savedPictureUri)
onTopBarView().performOpenMoreOptions()
Expand All @@ -472,6 +472,7 @@ class MenuFileActivityIntegrationTest {
.inRoot(RootMatchers.isPlatformPopup()).perform(click())
onView(withText(R.string.save_button_text)).perform(click())
onView(isRoot()).perform(waitFor(100))
addFileToDeletionFileList("image$imageNumber", "png", Environment.DIRECTORY_PICTURES)
onTopBarView().performOpenMoreOptions()
onView(withText(R.string.menu_save_copy)).perform(click())
imageNumber = launchActivityRule.activity.presenter.imageNumber
Expand Down Expand Up @@ -504,8 +505,7 @@ class MenuFileActivityIntegrationTest {
} catch (e: IOException) {
throw AssertionError("Picture file could not be created.", e)
}
val imageFile = File(imageUri?.path, "testfile.jpg")
deletionFileList.add(imageFile)
addUriToDeletionFileList(imageUri)
return imageUri
}

Expand Down Expand Up @@ -534,15 +534,14 @@ class MenuFileActivityIntegrationTest {
onView(withText(R.string.menu_save_image)).perform(click())
onView(withText(R.string.save_button_text)).perform(click())
onView(isRoot()).perform(waitFor(200))
addUriToDeletionFileList(activity.model.savedPictureUri)
val uri = activity.model.savedPictureUri
onDrawingSurfaceView().perform(touchAt(MIDDLE))
onTopBarView().performOpenMoreOptions()
onView(withText(R.string.menu_save_image)).perform(click())
onView(withText(R.string.save_button_text)).perform(click())
onView(isRoot()).perform(waitFor(100))
onView(withText(R.string.overwrite_button_text)).check(matches(isDisplayed()))
onView(withText(R.string.overwrite_button_text)).perform(click())
onView(isRoot()).perform(waitFor(500))

val oldFileName = uri?.path?.let { File(it).name }
val newFileName = activity.model.savedPictureUri?.path?.let { File(it).name }
Expand All @@ -561,12 +560,14 @@ class MenuFileActivityIntegrationTest {
onView(withText(R.string.menu_save_image)).perform(click())
onView(withText(R.string.save_button_text)).perform(click())
onView(isRoot()).perform(waitFor(200))
addUriToDeletionFileList(activity.model.savedPictureUri)
val uri = activity.model.savedPictureUri
onDrawingSurfaceView().perform(touchAt(MIDDLE))
onTopBarView().performOpenMoreOptions()
onView(withText(R.string.menu_save_image)).perform(click())
onView(withText(R.string.save_button_text)).perform(click())
onView(isRoot()).perform(waitFor(100))
addUriToDeletionFileList(activity.model.savedPictureUri)
onView(withText(R.string.overwrite_button_text)).check(matches(isDisplayed()))
onView(withText(R.string.overwrite_button_text)).perform(click())
onView(isRoot()).perform(waitFor(500))
Expand Down Expand Up @@ -645,21 +646,27 @@ class MenuFileActivityIntegrationTest {
assertEquals(oldFileName, newFileName)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
addFileToDeletionFileList(name, FileIO.fileType.value)
addFileToDeletionFileList(name, FileIO.fileType.value, Environment.DIRECTORY_DOWNLOADS)
} else {
addUriToDeletionFileList(activity.model.savedPictureUri)
}
}

private fun addUriToDeletionFileList(uri: Uri?) {
uri?.path?.let {
deletionFileList.add(File(it))
uri?.let {
val path = MainActivityPresenter.getPathFromUri(activity, it)
deletionFileList.add(File(path))
}
}

private fun addFileToDeletionFileList(fileName: String?, extension: String?) {
val dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
val file = File(dir, "$fileName.$extension")
private fun addFileToDeletionFileList(fileName: String?, extension: String?, directory: String) {
val file = createFileFromFileName(fileName, extension, directory)
deletionFileList.add(file)
}

private fun createFileFromFileName(fileName: String?, extension: String?, directory: String): File {
val dir = Environment.getExternalStoragePublicDirectory(directory)
val file = File(dir, "$fileName.$extension")
return file
}
}

0 comments on commit 07b8aee

Please sign in to comment.