diff --git a/collect_app/src/main/java/org/odk/collect/android/widgets/BaseImageWidget.java b/collect_app/src/main/java/org/odk/collect/android/widgets/BaseImageWidget.java index 02db449ac3e..ec2b33db0e6 100644 --- a/collect_app/src/main/java/org/odk/collect/android/widgets/BaseImageWidget.java +++ b/collect_app/src/main/java/org/odk/collect/android/widgets/BaseImageWidget.java @@ -221,8 +221,9 @@ public void clickImage(String context) { private void launchDrawActivity() { Intent i = new Intent(getContext(), DrawActivity.class); i.putExtra(DrawActivity.OPTION, drawOption); - if (binaryName != null) { - i.putExtra(DrawActivity.REF_IMAGE, Uri.fromFile(getFile())); + File file = getFile(); + if (file != null) { + i.putExtra(DrawActivity.REF_IMAGE, Uri.fromFile(file)); } i.putExtra(DrawActivity.EXTRA_OUTPUT, Uri.fromFile(new File(tmpImageFilePath))); @@ -279,9 +280,18 @@ protected void launchActivityForResult(Intent intent, final int resourceCode, fi @Nullable private File getFile() { + if (binaryName == null) { + return null; + } + File file = questionMediaManager.getAnswerFile(binaryName); if ((file == null || !file.exists()) && doesSupportDefaultValues()) { - file = new File(getDefaultFilePath()); + String filePath = getDefaultFilePath(); + if (filePath != null) { + return new File(getDefaultFilePath()); + } else { + return null; + } } return file; @@ -294,7 +304,7 @@ private String getDefaultFilePath() { Timber.w(e); } - return ""; + return null; } protected abstract boolean doesSupportDefaultValues(); diff --git a/collect_app/src/test/java/org/odk/collect/android/widgets/AnnotateWidgetTest.java b/collect_app/src/test/java/org/odk/collect/android/widgets/AnnotateWidgetTest.java index f47b0622371..0663b0f1d78 100644 --- a/collect_app/src/test/java/org/odk/collect/android/widgets/AnnotateWidgetTest.java +++ b/collect_app/src/test/java/org/odk/collect/android/widgets/AnnotateWidgetTest.java @@ -3,6 +3,7 @@ import android.content.Intent; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; +import android.net.Uri; import android.provider.MediaStore; import android.view.View; import android.widget.ImageView; @@ -243,4 +244,54 @@ public void markupButtonShouldBeDisabledIfImageAbsent() throws Exception { assertThat(getWidget().annotateButton.isEnabled(), is(false)); } + + @Test + public void whenPromptHasDefaultAnswer_passUriToDrawActivity() throws Exception { + File file = File.createTempFile("default", ".bmp"); + String imagePath = file.getAbsolutePath(); + + ReferenceManager referenceManager = setupFakeReferenceManager(singletonList( + new Pair<>(DrawWidgetTest.DEFAULT_IMAGE_ANSWER, imagePath) + )); + CollectHelpers.overrideAppDependencyModule(new AppDependencyModule() { + @Override + public ReferenceManager providesReferenceManager() { + return referenceManager; + } + + @Override + public ImageLoader providesImageLoader() { + return new SynchronousImageLoader(); + } + }); + + formEntryPrompt = new MockFormEntryPromptBuilder() + .withAnswerDisplayText(DrawWidgetTest.DEFAULT_IMAGE_ANSWER) + .build(); + + Intent intent = getIntentLaunchedByClick(R.id.markup_image); + assertComponentEquals(activity, DrawActivity.class, intent); + assertExtraEquals(DrawActivity.OPTION, DrawActivity.OPTION_ANNOTATE, intent); + assertExtraEquals(DrawActivity.REF_IMAGE, Uri.fromFile(file), intent); + } + + @Test + public void whenPromptHasDefaultAnswerThatDoesNotExist_doNotPassUriToDrawActivity() { + formEntryPrompt = new MockFormEntryPromptBuilder() + .withAnswerDisplayText(DrawWidgetTest.DEFAULT_IMAGE_ANSWER) + .build(); + + Intent intent = getIntentLaunchedByClick(R.id.markup_image); + assertComponentEquals(activity, DrawActivity.class, intent); + assertExtraEquals(DrawActivity.OPTION, DrawActivity.OPTION_ANNOTATE, intent); + assertThat(intent.hasExtra(DrawActivity.REF_IMAGE), is(false)); + } + + @Test + public void whenThereIsNoAnswer_doNotPassUriToDrawActivity() { + Intent intent = getIntentLaunchedByClick(R.id.markup_image); + assertComponentEquals(activity, DrawActivity.class, intent); + assertExtraEquals(DrawActivity.OPTION, DrawActivity.OPTION_ANNOTATE, intent); + assertThat(intent.hasExtra(DrawActivity.REF_IMAGE), is(false)); + } } diff --git a/collect_app/src/test/java/org/odk/collect/android/widgets/DrawWidgetTest.java b/collect_app/src/test/java/org/odk/collect/android/widgets/DrawWidgetTest.java index 7c426500e7c..ddb063ce8e1 100644 --- a/collect_app/src/test/java/org/odk/collect/android/widgets/DrawWidgetTest.java +++ b/collect_app/src/test/java/org/odk/collect/android/widgets/DrawWidgetTest.java @@ -3,6 +3,7 @@ import android.content.Intent; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; +import android.net.Uri; import android.view.View; import android.widget.ImageView; @@ -80,15 +81,6 @@ public StringData getNextAnswer() { return new StringData(RandomString.make()); } - @Test - public void buttonsShouldLaunchCorrectIntents() { - stubAllRuntimePermissionsGranted(true); - - Intent intent = getIntentLaunchedByClick(R.id.simple_button); - assertComponentEquals(activity, DrawActivity.class, intent); - assertExtraEquals(DrawActivity.OPTION, DrawActivity.OPTION_DRAW, intent); - } - @Test public void usingReadOnlyOptionShouldMakeAllClickableElementsDisabled() { when(formEntryPrompt.isReadOnly()).thenReturn(true); @@ -195,4 +187,54 @@ public ImageLoader providesImageLoader() { String loadedPath = shadowOf(((BitmapDrawable) drawable).getBitmap()).getCreatedFromPath(); assertThat(loadedPath, equalTo(imagePath)); } + + @Test + public void whenPromptHasDefaultAnswer_passUriToDrawActivity() throws Exception { + File file = File.createTempFile("default", ".bmp"); + String imagePath = file.getAbsolutePath(); + + ReferenceManager referenceManager = setupFakeReferenceManager(singletonList( + new Pair<>(DrawWidgetTest.DEFAULT_IMAGE_ANSWER, imagePath) + )); + CollectHelpers.overrideAppDependencyModule(new AppDependencyModule() { + @Override + public ReferenceManager providesReferenceManager() { + return referenceManager; + } + + @Override + public ImageLoader providesImageLoader() { + return new SynchronousImageLoader(); + } + }); + + formEntryPrompt = new MockFormEntryPromptBuilder() + .withAnswerDisplayText(DrawWidgetTest.DEFAULT_IMAGE_ANSWER) + .build(); + + Intent intent = getIntentLaunchedByClick(R.id.simple_button); + assertComponentEquals(activity, DrawActivity.class, intent); + assertExtraEquals(DrawActivity.OPTION, DrawActivity.OPTION_DRAW, intent); + assertExtraEquals(DrawActivity.REF_IMAGE, Uri.fromFile(file), intent); + } + + @Test + public void whenPromptHasDefaultAnswerThatDoesNotExist_doNotPassUriToDrawActivity() { + formEntryPrompt = new MockFormEntryPromptBuilder() + .withAnswerDisplayText(DrawWidgetTest.DEFAULT_IMAGE_ANSWER) + .build(); + + Intent intent = getIntentLaunchedByClick(R.id.simple_button); + assertComponentEquals(activity, DrawActivity.class, intent); + assertExtraEquals(DrawActivity.OPTION, DrawActivity.OPTION_DRAW, intent); + assertThat(intent.hasExtra(DrawActivity.REF_IMAGE), is(false)); + } + + @Test + public void whenThereIsNoAnswer_doNotPassUriToDrawActivity() { + Intent intent = getIntentLaunchedByClick(R.id.simple_button); + assertComponentEquals(activity, DrawActivity.class, intent); + assertExtraEquals(DrawActivity.OPTION, DrawActivity.OPTION_DRAW, intent); + assertThat(intent.hasExtra(DrawActivity.REF_IMAGE), is(false)); + } } diff --git a/collect_app/src/test/java/org/odk/collect/android/widgets/SignatureWidgetTest.java b/collect_app/src/test/java/org/odk/collect/android/widgets/SignatureWidgetTest.java index 13c18c1ffbe..2b03f861401 100644 --- a/collect_app/src/test/java/org/odk/collect/android/widgets/SignatureWidgetTest.java +++ b/collect_app/src/test/java/org/odk/collect/android/widgets/SignatureWidgetTest.java @@ -3,6 +3,7 @@ import android.content.Intent; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; +import android.net.Uri; import android.view.View; import android.widget.ImageView; @@ -73,15 +74,6 @@ public StringData getNextAnswer() { return new StringData(RandomString.make()); } - @Test - public void buttonsShouldLaunchCorrectIntents() { - stubAllRuntimePermissionsGranted(true); - - Intent intent = getIntentLaunchedByClick(R.id.simple_button); - assertComponentEquals(activity, DrawActivity.class, intent); - assertExtraEquals(DrawActivity.OPTION, DrawActivity.OPTION_SIGNATURE, intent); - } - @Test public void usingReadOnlyOptionShouldMakeAllClickableElementsDisabled() { when(formEntryPrompt.isReadOnly()).thenReturn(true); @@ -189,4 +181,54 @@ public ImageLoader providesImageLoader() { String loadedPath = shadowOf(((BitmapDrawable) drawable).getBitmap()).getCreatedFromPath(); assertThat(loadedPath, equalTo(imagePath)); } + + @Test + public void whenPromptHasDefaultAnswer_passUriToDrawActivity() throws Exception { + File file = File.createTempFile("default", ".bmp"); + String imagePath = file.getAbsolutePath(); + + ReferenceManager referenceManager = setupFakeReferenceManager(singletonList( + new Pair<>(DrawWidgetTest.DEFAULT_IMAGE_ANSWER, imagePath) + )); + CollectHelpers.overrideAppDependencyModule(new AppDependencyModule() { + @Override + public ReferenceManager providesReferenceManager() { + return referenceManager; + } + + @Override + public ImageLoader providesImageLoader() { + return new SynchronousImageLoader(); + } + }); + + formEntryPrompt = new MockFormEntryPromptBuilder() + .withAnswerDisplayText(DrawWidgetTest.DEFAULT_IMAGE_ANSWER) + .build(); + + Intent intent = getIntentLaunchedByClick(R.id.simple_button); + assertComponentEquals(activity, DrawActivity.class, intent); + assertExtraEquals(DrawActivity.OPTION, DrawActivity.OPTION_SIGNATURE, intent); + assertExtraEquals(DrawActivity.REF_IMAGE, Uri.fromFile(file), intent); + } + + @Test + public void whenPromptHasDefaultAnswerThatDoesNotExist_doNotPassUriToDrawActivity() { + formEntryPrompt = new MockFormEntryPromptBuilder() + .withAnswerDisplayText(DrawWidgetTest.DEFAULT_IMAGE_ANSWER) + .build(); + + Intent intent = getIntentLaunchedByClick(R.id.simple_button); + assertComponentEquals(activity, DrawActivity.class, intent); + assertExtraEquals(DrawActivity.OPTION, DrawActivity.OPTION_SIGNATURE, intent); + assertThat(intent.hasExtra(DrawActivity.REF_IMAGE), is(false)); + } + + @Test + public void whenThereIsNoAnswer_doNotPassUriToDrawActivity() { + Intent intent = getIntentLaunchedByClick(R.id.simple_button); + assertComponentEquals(activity, DrawActivity.class, intent); + assertExtraEquals(DrawActivity.OPTION, DrawActivity.OPTION_SIGNATURE, intent); + assertThat(intent.hasExtra(DrawActivity.REF_IMAGE), is(false)); + } } diff --git a/collect_app/src/test/java/org/odk/collect/android/widgets/support/FakeQuestionMediaManager.java b/collect_app/src/test/java/org/odk/collect/android/widgets/support/FakeQuestionMediaManager.java index cab5cbe2a61..416ce40f433 100644 --- a/collect_app/src/test/java/org/odk/collect/android/widgets/support/FakeQuestionMediaManager.java +++ b/collect_app/src/test/java/org/odk/collect/android/widgets/support/FakeQuestionMediaManager.java @@ -30,6 +30,10 @@ public LiveData> createAnswerFile(File file) { @Override public File getAnswerFile(String fileName) { + if (fileName == null) { + return null; + } + File existing = answerFiles.stream().filter(f -> f.getName().equals(fileName)).findFirst().orElse(null); if (existing != null) {