Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent bulk finalization on instances with save points #5766

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,30 @@ class BulkFinalizationTest {
}

@Test
fun doesNotFinalizeOtherTypesOfInstance() {
fun doesNotFinalizeInstancesWithSavePoints() {
rule.startAtMainMenu()
.copyForm("one-question.xml")
.startBlankForm("One Question")
.swipeToEndScreen()
.clickSaveAsDraft()

.clickDrafts()
.clickOnForm("One Question")
.killAndReopenApp(MainMenuPage())

.clickDrafts()
.clickOptionsIcon(string.finalize_all_forms)
.clickOnString(string.finalize_all_forms)
.checkIsSnackbarWithQuantityDisplayed(plurals.bulk_finalize_failure, 1)
.assertText("One Question")
.pressBack(MainMenuPage())

.assertNumberOfEditableForms(1)
.assertNumberOfFinalizedForms(0)
}

@Test
fun doesNotFinalizeAlreadyFinalizedInstances() {
rule.startAtMainMenu()
.copyForm("one-question.xml")
.startBlankForm("One Question")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,20 @@ import androidx.test.espresso.matcher.ViewMatchers.withEffectiveVisibility
import androidx.test.espresso.matcher.ViewMatchers.withHint
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.UiDevice
import androidx.test.uiautomator.UiSelector
import org.hamcrest.CoreMatchers.not
import org.hamcrest.Matchers.allOf
import org.hamcrest.core.StringContains.containsString
import org.hamcrest.core.StringEndsWith.endsWith
import org.junit.Assert
import org.odk.collect.android.BuildConfig
import org.odk.collect.android.R
import org.odk.collect.android.application.Collect
import org.odk.collect.android.storage.StoragePathProvider
import org.odk.collect.android.support.ActivityHelpers
import org.odk.collect.android.support.CollectHelpers
import org.odk.collect.android.support.WaitFor.wait250ms
import org.odk.collect.android.support.WaitFor.waitFor
import org.odk.collect.android.support.actions.RotateAction
Expand Down Expand Up @@ -450,6 +455,25 @@ abstract class Page<T : Page<T>> {
return this as T
}

fun <D : Page<D>?> killAndReopenApp(destination: D): D {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is copied over from master.

val device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())

// kill
device.pressRecentApps()
device
.findObject(UiSelector().descriptionContains("Collect"))
.swipeUp(10).also {
CollectHelpers.simulateProcessRestart() // the process is not restarted automatically (probably to keep the test running) so we have simulate it
}

// reopen
InstrumentationRegistry.getInstrumentation().targetContext.apply {
val intent = packageManager.getLaunchIntentForPackage(BuildConfig.APPLICATION_ID)!!
startActivity(intent)
}
return destination!!.assertOnPage()
}

companion object {
private fun rotateToLandscape(): ViewAction {
return RotateAction(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ object FormEntryUseCases {

@JvmStatic
fun loadFormDef(xForm: File, projectRootDir: File, formMediaDir: File): FormDef? {
FormUtils.setupReferenceManagerForForm(ReferenceManager.instance(), projectRootDir, formMediaDir)
FormUtils.setupReferenceManagerForForm(
ReferenceManager.instance(),
projectRootDir,
formMediaDir
)
return createFormDefFromCacheOrXml(xForm)
}

Expand Down Expand Up @@ -62,6 +66,17 @@ object FormEntryUseCases {
)
}

fun getSavePoint(formController: FormController, cacheDir: File): File? {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be able to use this in other places we grab save point files, but I'll leave out that refactor for the moment to avoid the risk of merge conflicts that it'd introduce.

val instanceXml = formController.getInstanceFile()!!
val savepointFile = File(cacheDir, "${instanceXml.name}.save")

return if (savepointFile.exists() && savepointFile.lastModified() > instanceXml.lastModified()) {
savepointFile
} else {
null
}
}

fun saveDraft(
formController: JavaRosaFormController,
instancesRepository: InstancesRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.lifecycle.LiveData
import org.odk.collect.android.entities.EntitiesRepositoryProvider
import org.odk.collect.android.formentry.FormEntryUseCases
import org.odk.collect.android.storage.StoragePathProvider
import org.odk.collect.android.storage.StorageSubdirectory
import org.odk.collect.android.utilities.FileUtils
import org.odk.collect.android.utilities.FormsRepositoryProvider
import org.odk.collect.android.utilities.InstancesRepositoryProvider
Expand Down Expand Up @@ -70,16 +71,21 @@ class InstancesDataService(
val formController =
FormEntryUseCases.loadDraft(formEntryController, formMediaDir, instanceFile)

val instance = FormEntryUseCases.finalizeDraft(
formController,
instancesRepository,
entitiesRepository
)
val cacheDir = storagePathProvider.getOdkDirPath(StorageSubdirectory.CACHE)
if (FormEntryUseCases.getSavePoint(formController, File(cacheDir)) == null) {
val finalizedInstance = FormEntryUseCases.finalizeDraft(
formController,
instancesRepository,
entitiesRepository
)

if (instance == null) {
failCount + 1
if (finalizedInstance == null) {
failCount + 1
} else {
failCount
}
} else {
failCount
failCount + 1
}
}

Expand Down