Skip to content

Remove empty instanceDir if a user quits the identity dialog #6020

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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 @@ -7,13 +7,15 @@ import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
import org.junit.runner.RunWith
import org.odk.collect.android.support.StorageUtils
import org.odk.collect.android.support.StorageUtils.getAuditLogForFirstInstance
import org.odk.collect.android.support.pages.FormEntryPage
import org.odk.collect.android.support.pages.FormHierarchyPage
import org.odk.collect.android.support.pages.IdentifyUserPromptPage
import org.odk.collect.android.support.pages.MainMenuPage
import org.odk.collect.android.support.rules.CollectTestRule
import org.odk.collect.android.support.rules.TestRuleChain
import java.io.File
import java.io.IOException

@RunWith(AndroidJUnit4::class)
Expand Down Expand Up @@ -78,13 +80,15 @@ class IdentifyUserTest {
}

@Test
fun openingForm_andPressingBack_returnsToMainMenu() {
fun openingForm_andPressingBack_returnsToMainMenuAndDoesNotLeaveAnEmptyInstanceDir() {
rule.startAtMainMenu()
.copyForm(IDENTIFY_USER_AUDIT_FORM)
.clickFillBlankForm()
.clickOnFormWithIdentityPrompt("Identify User")
.closeSoftKeyboard()
.pressBack(MainMenuPage())

assertThat(File(StorageUtils.getInstancesDirPath()).listFiles().size, equalTo(0))
}

@Test
Expand All @@ -110,12 +114,14 @@ class IdentifyUserTest {
}

@Test
fun openingForm_andPressingCloseCross_returnsToMainMenu() {
fun openingForm_andPressingCloseCross_returnsToMainMenuAndDoesNotLeaveAnEmptyInstanceDir() {
rule.startAtMainMenu()
.copyForm(IDENTIFY_USER_AUDIT_FORM)
.clickFillBlankForm()
.clickOnFormWithIdentityPrompt("Identify User")
.pressClose()

assertThat(File(StorageUtils.getInstancesDirPath()).listFiles().size, equalTo(0))
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import androidx.lifecycle.ViewModel;

import org.odk.collect.android.javarosawrapper.FormController;
import org.odk.collect.android.utilities.FileUtils;

import java.io.File;

public class IdentityPromptViewModel extends ViewModel {

Expand All @@ -20,6 +23,7 @@ public class IdentityPromptViewModel extends ViewModel {

private String identity = "";
private String formName;
private String instanceFolder;

public IdentityPromptViewModel() {
updateRequiresIdentity();
Expand All @@ -28,6 +32,7 @@ public IdentityPromptViewModel() {
public void formLoaded(@NonNull FormController formController) {
this.formName = formController.getFormTitle();
this.auditEventLogger = formController.getAuditEventLogger();
this.instanceFolder = formController.getInstanceFile().getParent();
updateRequiresIdentity();
}

Expand Down Expand Up @@ -56,6 +61,14 @@ public void done() {
}

public void promptDismissed() {
File instanceFolderFile = new File(instanceFolder);
if (instanceFolderFile.isDirectory()) {
String[] instanceFolderContent = instanceFolderFile.list();
if (instanceFolderContent != null && instanceFolderContent.length == 0) {
FileUtils.purgeMediaPath(instanceFolder);
}
}

formEntryCancelled.setValue(true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.File;
import java.io.IOException;

@RunWith(AndroidJUnit4.class)
public class IdentityPromptViewModelTest {

@Test
public void done_setsUserOnAuditEventLogger() {
public void done_setsUserOnAuditEventLogger() throws IOException {
FormController formController = mock(FormController.class);
AuditEventLogger auditEventLogger = mock(AuditEventLogger.class);
when(formController.getAuditEventLogger()).thenReturn(auditEventLogger);
when(formController.getInstanceFile()).thenReturn(File.createTempFile("instance", ".xml"));

IdentityPromptViewModel viewModel = new IdentityPromptViewModel();

Expand Down