Skip to content

Commit

Permalink
Implemented MC code conflict dialog.
Browse files Browse the repository at this point in the history
  • Loading branch information
surinder-tsys committed Jun 26, 2024
1 parent 0a642ee commit 1af7a97
Show file tree
Hide file tree
Showing 14 changed files with 775 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
/*
*
* Nextcloud Android client application
*
* @author Tobias Kaminsky
* Copyright (C) 2020 Tobias Kaminsky
* Copyright (C) 2020 Nextcloud GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.nmc.android.ui.conflict

import android.content.Intent
import androidx.test.espresso.Espresso
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.intent.rule.IntentsTestRule
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.platform.app.InstrumentationRegistry
import com.nextcloud.client.account.UserAccountManagerImpl
import com.nmc.android.ui.conflict.ConflictsResolveConsentDialog.Companion.newInstance
import com.owncloud.android.AbstractIT
import com.owncloud.android.R
import com.owncloud.android.datamodel.FileDataStorageManager
import com.owncloud.android.datamodel.OCFile
import com.owncloud.android.db.OCUpload
import com.owncloud.android.ui.activity.ConflictsResolveActivity
import com.owncloud.android.ui.activity.FileActivity
import com.owncloud.android.ui.dialog.ConflictsResolveDialog.Decision
import com.owncloud.android.ui.dialog.ConflictsResolveDialog.OnConflictDecisionMadeListener
import com.owncloud.android.utils.FileStorageUtils
import junit.framework.TestCase
import org.junit.After
import org.junit.Assert
import org.junit.Rule
import org.junit.Test

class ConflictsResolveConsentDialogIT : AbstractIT() {
@get:Rule
val activityRule = IntentsTestRule(ConflictsResolveActivity::class.java, true, false)

private var returnCode = false

@Test
fun replaceWithNewFile() {
returnCode = false

val newUpload = OCUpload(
FileStorageUtils.getSavePath(user.accountName) + "/nonEmpty.txt",
"/newFile.txt",
user.accountName
)

val existingFile = OCFile("/newFile.txt")
existingFile.fileLength = 1024000
existingFile.modificationTimestamp = 1582019340
existingFile.remoteId = "00000123abc"

val newFile = OCFile("/newFile.txt")
newFile.fileLength = 56000
newFile.modificationTimestamp = 1522019340
newFile.storagePath = FileStorageUtils.getSavePath(user.accountName) + "/nonEmpty.txt"

val storageManager = FileDataStorageManager(user, targetContext.contentResolver)
storageManager.saveNewFile(existingFile)

val intent = Intent(targetContext, ConflictsResolveActivity::class.java)
intent.putExtra(FileActivity.EXTRA_FILE, newFile)
intent.putExtra(ConflictsResolveActivity.EXTRA_EXISTING_FILE, existingFile)
intent.putExtra(ConflictsResolveActivity.EXTRA_CONFLICT_UPLOAD_ID, newUpload.uploadId)
intent.putExtra(ConflictsResolveActivity.EXTRA_LAUNCHED_FROM_TEST, true)

val sut = activityRule.launchActivity(intent)

val dialog = newInstance(
existingFile,
newFile,
UserAccountManagerImpl
.fromContext(targetContext)
.user
)
dialog.showDialog(sut)

sut.listener = OnConflictDecisionMadeListener { decision: Decision? ->
Assert.assertEquals(decision, Decision.KEEP_LOCAL)
returnCode = true
}

InstrumentationRegistry.getInstrumentation().waitForIdleSync()

Espresso.onView(ViewMatchers.withId(R.id.replace_btn)).perform(ViewActions.click())

TestCase.assertTrue(returnCode)
}

@Test
fun keepBothFiles() {
returnCode = false

val newUpload = OCUpload(
FileStorageUtils.getSavePath(user.accountName) + "/nonEmpty.txt",
"/newFile.txt",
user.accountName
)

val existingFile = OCFile("/newFile.txt")
existingFile.fileLength = 1024000
existingFile.modificationTimestamp = 1582019340

val newFile = OCFile("/newFile.txt")
newFile.fileLength = 56000
newFile.modificationTimestamp = 1522019340
newFile.storagePath = FileStorageUtils.getSavePath(user.accountName) + "/nonEmpty.txt"

val storageManager = FileDataStorageManager(user, targetContext.contentResolver)
storageManager.saveNewFile(existingFile)

val intent = Intent(targetContext, ConflictsResolveActivity::class.java)
intent.putExtra(FileActivity.EXTRA_FILE, newFile)
intent.putExtra(ConflictsResolveActivity.EXTRA_EXISTING_FILE, existingFile)
intent.putExtra(FileActivity.EXTRA_USER, user)
intent.putExtra(ConflictsResolveActivity.EXTRA_CONFLICT_UPLOAD_ID, newUpload.uploadId)
intent.putExtra(ConflictsResolveActivity.EXTRA_LAUNCHED_FROM_TEST, true)

val sut = activityRule.launchActivity(intent)

val dialog = newInstance(
existingFile,
newFile,
UserAccountManagerImpl
.fromContext(targetContext)
.user
)
dialog.showDialog(sut)

sut.listener = OnConflictDecisionMadeListener { decision: Decision? ->
Assert.assertEquals(decision, Decision.KEEP_BOTH)
returnCode = true
}

InstrumentationRegistry.getInstrumentation().waitForIdleSync()

Espresso.onView(ViewMatchers.withId(R.id.keep_both_btn)).perform(ViewActions.click())

TestCase.assertTrue(returnCode)
}

@Test
fun keepExistingFile() {
returnCode = false

val newUpload = OCUpload(
FileStorageUtils.getSavePath(user.accountName) + "/nonEmpty.txt",
"/newFile.txt",
user.accountName
)

val existingFile = OCFile("/newFile.txt")
existingFile.fileLength = 1024000
existingFile.modificationTimestamp = 1582019340

val newFile = OCFile("/newFile.txt")
newFile.fileLength = 56000
newFile.modificationTimestamp = 1522019340
newFile.storagePath = FileStorageUtils.getSavePath(user.accountName) + "/nonEmpty.txt"

val storageManager = FileDataStorageManager(user, targetContext.contentResolver)
storageManager.saveNewFile(existingFile)

val intent = Intent(targetContext, ConflictsResolveActivity::class.java)
intent.putExtra(FileActivity.EXTRA_FILE, newFile)
intent.putExtra(ConflictsResolveActivity.EXTRA_EXISTING_FILE, existingFile)
intent.putExtra(FileActivity.EXTRA_USER, user)
intent.putExtra(ConflictsResolveActivity.EXTRA_CONFLICT_UPLOAD_ID, newUpload.uploadId)
intent.putExtra(ConflictsResolveActivity.EXTRA_LAUNCHED_FROM_TEST, true)

val sut = activityRule.launchActivity(intent)

val dialog = newInstance(
existingFile,
newFile,
UserAccountManagerImpl
.fromContext(targetContext)
.user
)
dialog.showDialog(sut)

sut.listener = OnConflictDecisionMadeListener { decision: Decision? ->
Assert.assertEquals(decision, Decision.KEEP_SERVER)
returnCode = true
}

InstrumentationRegistry.getInstrumentation().waitForIdleSync()

Espresso.onView(ViewMatchers.withId(R.id.cancel_keep_existing_btn)).perform(ViewActions.click())

TestCase.assertTrue(returnCode)
}

@After
override fun after() {
storageManager.deleteAllFiles()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/*
* ownCloud Android client application
*
* @author Bartek Przybylski
* Copyright (C) 2012 Bartek Przybylski
* Copyright (C) 2015 ownCloud Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.nmc.android.ui.conflict

import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.DialogFragment
import com.nextcloud.client.account.User
import com.nextcloud.utils.extensions.getParcelableArgument
import com.nextcloud.utils.extensions.getSerializableArgument
import com.owncloud.android.R
import com.owncloud.android.databinding.ConflictResolveConsentDialogBinding
import com.owncloud.android.datamodel.OCFile
import com.owncloud.android.ui.dialog.ConflictsResolveDialog
import com.owncloud.android.ui.dialog.ConflictsResolveDialog.OnConflictDecisionMadeListener
import java.io.File

/**
* Dialog which will be displayed to user upon keep-in-sync file conflict.
*/
class ConflictsResolveConsentDialog : DialogFragment() {
private lateinit var binding: ConflictResolveConsentDialogBinding

private var existingFile: OCFile? = null
private var newFile: File? = null
private var listener: OnConflictDecisionMadeListener? = null
private var user: User? = null

override fun onAttach(context: Context) {
super.onAttach(context)
try {
listener = context as OnConflictDecisionMadeListener
} catch (e: ClassCastException) {
throw ClassCastException("Activity of this dialog must implement OnConflictDecisionMadeListener")
}
}

override fun onStart() {
super.onStart()

val alertDialog = dialog as AlertDialog?

if (alertDialog == null) {
Toast.makeText(context, "Failed to create conflict dialog", Toast.LENGTH_LONG).show()
return
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

if (savedInstanceState != null) {
existingFile = savedInstanceState.getParcelableArgument(KEY_EXISTING_FILE, OCFile::class.java)
newFile = savedInstanceState.getSerializableArgument(KEY_NEW_FILE, File::class.java)
user = savedInstanceState.getParcelableArgument(KEY_USER, User::class.java)
} else if (arguments != null) {
existingFile = arguments?.getParcelableArgument(KEY_EXISTING_FILE, OCFile::class.java)
newFile = arguments?.getSerializableArgument(KEY_NEW_FILE, File::class.java)
user = arguments?.getParcelableArgument(KEY_USER, User::class.java)
} else {
Toast.makeText(context, "Failed to create conflict dialog", Toast.LENGTH_LONG).show()
}
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)

outState.putParcelable(KEY_EXISTING_FILE, existingFile)
outState.putSerializable(KEY_NEW_FILE, newFile)
outState.putParcelable(KEY_USER, user)
}

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
// Inflate the layout for the dialog
binding = ConflictResolveConsentDialogBinding.inflate(requireActivity().layoutInflater)

// TODO: 26-05-2021 change replace and keep both button text for multiple files
binding.replaceBtn.setOnClickListener {
listener?.conflictDecisionMade(ConflictsResolveDialog.Decision.KEEP_LOCAL)
}

binding.keepBothBtn.setOnClickListener {
listener?.conflictDecisionMade(ConflictsResolveDialog.Decision.KEEP_BOTH)
}

binding.moreDetailsBtn.setOnClickListener { }

binding.cancelKeepExistingBtn.setOnClickListener {
listener?.conflictDecisionMade(ConflictsResolveDialog.Decision.KEEP_SERVER)
}

// Build the dialog
// TODO: 26-05-2021 Handle multiple dialog message
val dialogMessage = String.format(
getString(R.string.conflict_dialog_message),
existingFile?.decryptedFileName
)
val builder = AlertDialog.Builder(requireActivity())
builder.setView(binding.root) // TODO: 26-05-2021 handle multiple dialog title
.setTitle(getString(R.string.conflict_dialog_title))
.setMessage(dialogMessage)


return builder.create()
}

fun showDialog(activity: AppCompatActivity) {
val prev = activity.supportFragmentManager.findFragmentByTag("dialog")
val ft = activity.supportFragmentManager.beginTransaction()
if (prev != null) {
ft.remove(prev)
}
ft.addToBackStack(null)

this.show(ft, "dialog")
}

override fun onCancel(dialog: DialogInterface) {
listener?.conflictDecisionMade(ConflictsResolveDialog.Decision.CANCEL)
}

companion object {
private const val KEY_NEW_FILE = "file"
private const val KEY_EXISTING_FILE = "ocfile"
private const val KEY_USER = "user"

@JvmStatic
fun newInstance(existingFile: OCFile?, newFile: OCFile?, user: User?): ConflictsResolveConsentDialog {
val dialog = ConflictsResolveConsentDialog()

val args = Bundle()
args.putParcelable(KEY_EXISTING_FILE, existingFile)
newFile?.let {
args.putSerializable(KEY_NEW_FILE, File(it.storagePath))
}
args.putParcelable(KEY_USER, user)
dialog.arguments = args

return dialog
}
}
}
Loading

0 comments on commit 1af7a97

Please sign in to comment.