Skip to content

Commit

Permalink
PAINTROID-323: Fixed overwrite adding suffix before file name.
Browse files Browse the repository at this point in the history
  • Loading branch information
AneiMakovec committed Jun 15, 2022
1 parent 5a2c023 commit 4d4c413
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,7 @@ import org.hamcrest.Matchers.`is`
import org.hamcrest.core.AllOf.allOf
import org.hamcrest.core.IsNot
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNotSame
import org.junit.Assert.assertTrue
import org.junit.Assert.*
import org.junit.Before
import org.junit.ClassRule
import org.junit.Rule
Expand All @@ -84,6 +80,7 @@ import org.junit.runner.RunWith
import java.io.File
import java.io.IOException
import java.lang.AssertionError
import java.lang.NumberFormatException

@RunWith(AndroidJUnit4::class)
class MenuFileActivityIntegrationTest {
Expand Down Expand Up @@ -414,7 +411,8 @@ class MenuFileActivityIntegrationTest {

@Test
fun testCheckSaveImageDialogShowPNGSpinnerText() {
FileIO.compressFormat = Bitmap.CompressFormat.PNG
//FileIO.compressFormat = Bitmap.CompressFormat.PNG
FileIO.fileType = FileIO.FileType.PNG
onDrawingSurfaceView().perform(touchAt(MIDDLE))
onTopBarView().performOpenMoreOptions()
onView(withText(R.string.menu_save_image)).perform(click())
Expand All @@ -424,7 +422,8 @@ class MenuFileActivityIntegrationTest {

@Test
fun testCheckSaveImageDialogShowORASpinnerText() {
FileIO.isCatrobatImage = true
//FileIO.isCatrobatImage = true
FileIO.fileType = FileIO.FileType.ORA
onDrawingSurfaceView().perform(touchAt(MIDDLE))
onTopBarView().performOpenMoreOptions()
onView(withText(R.string.menu_save_image)).perform(click())
Expand Down Expand Up @@ -516,6 +515,108 @@ class MenuFileActivityIntegrationTest {
onDrawingSurfaceView().checkPixelColor(Color.TRANSPARENT, BitmapLocationProvider.MIDDLE)
}

@Test
fun testSameFileNameAfterOverwritePng() {
val name = "testPNG"
FileIO.filename = name
FileIO.fileType = FileIO.FileType.PNG
FileIO.compressFormat = Bitmap.CompressFormat.PNG
onTopBarView().performOpenMoreOptions()
onView(withText(R.string.menu_save_image)).perform(click())
onView(withText(R.string.save_button_text)).perform(click())
onView(isRoot()).perform(waitFor(200))
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(200))
assertEquals(uri, activity.model.savedPictureUri)
addUriToDeletionFileList(activity.model.savedPictureUri)
}

@Test
fun testSameFileNameAfterOverwriteJpg() {
val name = "testJPG"
FileIO.filename = name
FileIO.fileType = FileIO.FileType.JPG
FileIO.compressFormat = Bitmap.CompressFormat.JPEG
onTopBarView().performOpenMoreOptions()
onView(withText(R.string.menu_save_image)).perform(click())
onView(withText(R.string.save_button_text)).perform(click())
onView(isRoot()).perform(waitFor(200))
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(200))
assertEquals(uri, activity.model.savedPictureUri)
addUriToDeletionFileList(activity.model.savedPictureUri)
}

@Test
fun testSameFileNameAfterOverwriteOra() {
val name = "testORA"
FileIO.filename = name
FileIO.fileType = FileIO.FileType.ORA
FileIO.compressFormat = Bitmap.CompressFormat.PNG
onTopBarView().performOpenMoreOptions()
onView(withText(R.string.menu_save_image)).perform(click())
onView(withText(R.string.save_button_text)).perform(click())
onView(isRoot()).perform(waitFor(200))
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(200))
try {
val oldFileId = uri?.path?.let {
File(it).name.toInt() + 1
}
val newFileId = activity.model.savedPictureUri?.path?.let {
File(it).name.toInt()
}
assertEquals(oldFileId, newFileId)
} catch (e : NumberFormatException) {
fail()
}
addUriToDeletionFileList(activity.model.savedPictureUri)
}

@Test
fun testSameFileNameAfterOverwriteCatrobatImage() {
val name = "testCI"
FileIO.filename = name
FileIO.fileType = FileIO.FileType.CATROBAT
FileIO.compressFormat = Bitmap.CompressFormat.PNG
onTopBarView().performOpenMoreOptions()
onView(withText(R.string.menu_save_image)).perform(click())
onView(withText(R.string.save_button_text)).perform(click())
onView(isRoot()).perform(waitFor(200))
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(200))
assertEquals(uri, activity.model.savedPictureUri)
addUriToDeletionFileList(activity.model.savedPictureUri)
}

private fun addUriToDeletionFileList(uri: Uri?) {
uri?.path?.let {
deletionFileList.add(File(it))
Expand Down
3 changes: 0 additions & 3 deletions Paintroid/src/main/java/org/catrobat/paintroid/FileIO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,6 @@ object FileIO {

var catroidFlag = false

@JvmField
var isCatrobatImage = false

@JvmField
var storeImageUri: Uri? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import com.esotericsoftware.kryo.Kryo
import com.esotericsoftware.kryo.KryoException
import com.esotericsoftware.kryo.io.Input
import com.esotericsoftware.kryo.io.Output
import org.catrobat.paintroid.FileIO
import org.catrobat.paintroid.command.Command
import org.catrobat.paintroid.command.CommandManager
import org.catrobat.paintroid.command.implementation.AddLayerCommand
Expand Down Expand Up @@ -155,14 +156,23 @@ class CommandSerializationUtilities(private val activityContext: Context, privat
val contentResolver = activityContext.contentResolver

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val contentValues = ContentValues().apply {
put(MediaStore.Images.Media.DISPLAY_NAME, fileName)
put(MediaStore.Images.Media.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS)
}
contentResolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues)?.let { uri ->
contentResolver.openOutputStream(uri)?.use { stream ->
writeToStream(stream)
returnUri = uri
if (FileIO.storeImageUri == null) {
val contentValues = ContentValues().apply {
put(MediaStore.Images.Media.DISPLAY_NAME, fileName)
put(MediaStore.Images.Media.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS)
}
contentResolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues)?.let { uri ->
contentResolver.openOutputStream(uri)?.use { stream ->
writeToStream(stream)
returnUri = uri
}
}
} else {
FileIO.storeImageUri?.let { uri ->
contentResolver.openOutputStream(uri)?.use { stream ->
writeToStream(stream)
returnUri = uri
}
}
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ class SaveInformationDialog :
isExport: Boolean
): SaveInformationDialog {
if (isStandard) {
FileIO.isCatrobatImage = false
FileIO.filename = STANDARD_FILE_NAME
FileIO.compressFormat = Bitmap.CompressFormat.PNG
FileIO.fileType = PNG
Expand Down Expand Up @@ -168,10 +167,10 @@ class SaveInformationDialog :
private fun initInfoButton(view: View) {
val infoButton: AppCompatImageButton = view.findViewById(R.id.pocketpaint_btn_save_info)
infoButton.setOnClickListener {
when {
FileIO.isCatrobatImage -> presenter.showOraInformationDialog()
FileIO.compressFormat == Bitmap.CompressFormat.JPEG -> presenter.showJpgInformationDialog()
FileIO.fileType == CATROBAT -> presenter.showCatrobatInformationDialog()
when (FileIO.fileType) {
JPG -> presenter.showJpgInformationDialog()
ORA -> presenter.showOraInformationDialog()
CATROBAT -> presenter.showCatrobatInformationDialog()
else -> presenter.showPngInformationDialog()
}
}
Expand All @@ -193,33 +192,31 @@ class SaveInformationDialog :

private fun setFileDetails(
compressFormat: Bitmap.CompressFormat,
isCatrobatImage: Boolean,
fileType: FileType,
isJpg: Boolean = false
fileType: FileType
) {
specificFormatLayout.removeAllViews()
if (isJpg) {
if (fileType == JPG) {
specificFormatLayout.addView(jpgView)
}
FileIO.compressFormat = compressFormat
FileIO.isCatrobatImage = isCatrobatImage
FileIO.fileType = fileType
}

private fun setSpinnerSelection() {
when {
FileIO.isCatrobatImage -> spinner.setSelection(2)
FileIO.compressFormat == Bitmap.CompressFormat.PNG -> spinner.setSelection(0)
else -> spinner.setSelection(1)
when (FileIO.fileType) {
JPG -> spinner.setSelection(1)
ORA -> spinner.setSelection(2)
CATROBAT -> spinner.setSelection(3)
else -> spinner.setSelection(0)
}
}

override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
when (parent?.getItemAtPosition(position).toString().toLowerCase(Locale.getDefault())) {
JPG.value -> setFileDetails(Bitmap.CompressFormat.JPEG, false, JPG, true)
PNG.value -> setFileDetails(Bitmap.CompressFormat.PNG, false, PNG)
ORA.value -> setFileDetails(Bitmap.CompressFormat.PNG, true, ORA)
CATROBAT.value -> setFileDetails(Bitmap.CompressFormat.PNG, false, CATROBAT)
JPG.value -> setFileDetails(Bitmap.CompressFormat.JPEG, JPG)
PNG.value -> setFileDetails(Bitmap.CompressFormat.PNG, PNG)
ORA.value -> setFileDetails(Bitmap.CompressFormat.PNG, ORA)
CATROBAT.value -> setFileDetails(Bitmap.CompressFormat.PNG, CATROBAT)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class SaveImage(
try {
val bitmap = workspace.bitmapOfAllLayers
val filename = FileIO.defaultFileName
currentUri = if (FileIO.isCatrobatImage) {
currentUri = if (FileIO.fileType == FileIO.FileType.ORA) {
val bitmapList = workspace.bitmapLisOfAllLayers
if (uri != null && filename.endsWith(FileIO.FileType.ORA.toExtension())) {
uri?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,6 @@ open class MainActivityPresenter(
FileIO.filename = "image"
FileIO.compressFormat = Bitmap.CompressFormat.PNG
FileIO.fileType = FileIO.FileType.PNG
FileIO.isCatrobatImage = false
FileIO.deleteTempFile(internalMemoryPath)
val initCommand =
commandFactory.createInitCommand(metrics.widthPixels, metrics.heightPixels)
Expand Down Expand Up @@ -766,6 +765,7 @@ open class MainActivityPresenter(
if (result.model != null) {
commandManager.loadCommandsCatrobatImage(result.model)
resetPerspectiveAfterNextCommand = true
FileIO.fileType = FileIO.FileType.CATROBAT
return
}
if (result.toBeScaled) {
Expand Down Expand Up @@ -796,14 +796,11 @@ open class MainActivityPresenter(
if (name.endsWith(FileIO.FileType.JPG.value) || name.endsWith("jpeg")) {
FileIO.compressFormat = Bitmap.CompressFormat.JPEG
FileIO.fileType = FileIO.FileType.JPG
FileIO.isCatrobatImage = false
} else if (name.endsWith(FileIO.FileType.PNG.value)) {
FileIO.compressFormat = Bitmap.CompressFormat.PNG
FileIO.fileType = FileIO.FileType.PNG
FileIO.isCatrobatImage = false
} else {
FileIO.fileType = FileIO.FileType.ORA
FileIO.isCatrobatImage = true
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,6 @@ class MainActivityNavigator(
}
FileIO.filename = "image$imageNumber"
FileIO.catroidFlag = true
FileIO.isCatrobatImage = false
mainActivity.presenter.switchBetweenVersions(permissionCode, isExport)
return
}
Expand Down

0 comments on commit 4d4c413

Please sign in to comment.