Skip to content

Commit

Permalink
PAINTROID-323: Fixed adding suffix when overwritting files.
Browse files Browse the repository at this point in the history
  • Loading branch information
AneiMakovec committed Jul 25, 2022
1 parent cf3859b commit 38c7b13
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ class MenuFileActivityIntegrationTest {

@Test
fun testCheckSaveImageDialogShowPNGSpinnerText() {
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 @@ -431,7 +431,7 @@ class MenuFileActivityIntegrationTest {

@Test
fun testCheckSaveImageDialogShowORASpinnerText() {
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 @@ -524,6 +524,138 @@ 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(500))

val oldFileName = uri?.path?.let { File(it).name }
val newFileName = activity.model.savedPictureUri?.path?.let { File(it).name }

assertEquals(oldFileName, newFileName)
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(500))

val oldFileName = uri?.path?.let { File(it).name }
val newFileName = activity.model.savedPictureUri?.path?.let { File(it).name }

assertEquals(oldFileName, newFileName)
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(500))
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(500))
onView(withText(R.string.overwrite_button_text)).check(matches(isDisplayed()))
onView(withText(R.string.overwrite_button_text)).perform(click())
onView(isRoot()).perform(waitFor(500))

var newFileName = "new"
val uri = activity.model.savedPictureUri
if (uri != null) {
val cursor = activity.contentResolver.query(
uri,
arrayOf(MediaStore.Images.ImageColumns.DISPLAY_NAME),
null, null, null
)
cursor?.use {
if (cursor.moveToFirst()) {
newFileName =
cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DISPLAY_NAME))
}
}
}

assertEquals(newFileName, "testORA.ora")
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(500))
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(500))
onView(withText(R.string.overwrite_button_text)).check(matches(isDisplayed()))
onView(withText(R.string.overwrite_button_text)).perform(click())
onView(isRoot()).perform(waitFor(500))

var newFileName = "new"
val uri = activity.model.savedPictureUri
if (uri != null) {
val cursor = activity.contentResolver.query(
uri,
arrayOf(MediaStore.Images.ImageColumns.DISPLAY_NAME),
null, null, null
)
cursor?.use {
if (cursor.moveToFirst()) {
newFileName =
cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DISPLAY_NAME))
}
}
}

assertEquals(newFileName, "testCI.catrobat-image")
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 @@ -90,9 +90,6 @@ object FileIO {

var navigator: MainActivityContracts.Navigator? = null

@JvmField
var isCatrobatImage = false

@JvmField
var storeImageUri: Uri? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ interface MainActivityContracts {

fun saveImageConfirmClicked(requestCode: Int, uri: Uri?)

fun saveCopyConfirmClicked(requestCode: Int)
fun saveCopyConfirmClicked(requestCode: Int, uri: Uri?)

fun undoClicked()

Expand Down Expand Up @@ -311,7 +311,13 @@ interface MainActivityContracts {
}

interface Interactor {
fun saveCopy(callback: SaveImageCallback, requestCode: Int, workspace: Workspace, context: Context)
fun saveCopy(
callback: SaveImageCallback,
requestCode: Int,
workspace: Workspace,
uri: Uri?,
context: Context
)

fun createFile(callback: CreateFileCallback, requestCode: Int, filename: String)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import org.catrobat.paintroid.FileIO.FileType.JPG
import org.catrobat.paintroid.FileIO.FileType.CATROBAT
import org.catrobat.paintroid.FileIO.FileType.ORA
import org.catrobat.paintroid.R
import org.catrobat.paintroid.common.PERMISSION_EXTERNAL_STORAGE_SAVE_COPY
import java.util.Locale

private const val STANDARD_FILE_NAME = "image"
Expand Down Expand Up @@ -72,7 +71,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 @@ -118,9 +116,7 @@ class SaveInformationDialog :
.setPositiveButton(R.string.save_button_text) { _, _ ->
FileIO.filename = imageName.text.toString()
FileIO.storeImageUri = null
if (permission != PERMISSION_EXTERNAL_STORAGE_SAVE_COPY &&
FileIO.checkFileExists(FileIO.fileType, FileIO.defaultFileName, requireContext().contentResolver)
) {
if (FileIO.checkFileExists(FileIO.fileType, FileIO.defaultFileName, requireContext().contentResolver)) {
presenter.showOverwriteDialog(permission, isExport)
} else {
presenter.switchBetweenVersions(permission, isExport)
Expand Down Expand Up @@ -168,10 +164,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 +189,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(JPG.ordinal)
ORA -> spinner.setSelection(ORA.ordinal)
CATROBAT -> spinner.setSelection(CATROBAT.ordinal)
else -> spinner.setSelection(PNG.ordinal)
}
}

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 @@ -113,7 +113,7 @@ class SaveImage(
idlingResource.increment()
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 @@ -315,7 +315,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)
commandManager.setInitialStateCommand(initCommand)
Expand Down Expand Up @@ -491,7 +490,10 @@ open class MainActivityPresenter(
checkForDefaultFilename()
}
PERMISSION_EXTERNAL_STORAGE_SAVE_COPY -> {
saveCopyConfirmClicked(SAVE_IMAGE_DEFAULT)
saveCopyConfirmClicked(
SAVE_IMAGE_DEFAULT,
FileIO.storeImageUri
)
checkForDefaultFilename()
}
PERMISSION_REQUEST_CODE_REPLACE_PICTURE ->
Expand Down Expand Up @@ -544,10 +546,10 @@ open class MainActivityPresenter(
interactor.saveImage(this, requestCode, workspace, uri, context)
}

override fun saveCopyConfirmClicked(requestCode: Int) {
override fun saveCopyConfirmClicked(requestCode: Int, uri: Uri?) {
checkIfClippingToolNeedsAdjustment()
view.refreshDrawingSurface()
interactor.saveCopy(this, requestCode, workspace, context)
interactor.saveCopy(this, requestCode, workspace, uri, context)
}

override fun undoClicked() {
Expand Down Expand Up @@ -833,6 +835,13 @@ open class MainActivityPresenter(
if (result.model != null) {
commandManager.loadCommandsCatrobatImage(result.model)
resetPerspectiveAfterNextCommand = true
FileIO.fileType = FileIO.FileType.CATROBAT
if (uri != null) {
val name = getFileName(uri)
if (name != null) {
FileIO.filename = name.substring(0, name.length - FileIO.fileType.toExtension().length)
}
}
return
}
if (result.toBeScaled) {
Expand Down Expand Up @@ -863,15 +872,13 @@ 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
}
FileIO.filename = name.substring(0, name.length - FileIO.fileType.toExtension().length)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ class MainActivityInteractor(private val idlingResource: CountingIdlingResource)
callback: SaveImageCallback,
requestCode: Int,
workspace: Workspace,
uri: Uri?,
context: Context
) {
SaveImage(callback, requestCode, workspace, null, true, context, scopeIO, idlingResource).execute()
SaveImage(callback, requestCode, workspace, uri, true, context, scopeIO, idlingResource).execute()
}

override fun createFile(callback: CreateFileCallback, requestCode: Int, filename: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,6 @@ class MainActivityNavigator(
}
FileIO.filename = "image$imageNumber"
FileIO.catroidFlag = true
FileIO.isCatrobatImage = false
mainActivity.presenter.switchBetweenVersions(permissionCode, isExport)
return
}
Expand Down
Loading

0 comments on commit 38c7b13

Please sign in to comment.