Skip to content

Commit

Permalink
Merge pull request #5504 from grzesiek2010/COLLECT-5496
Browse files Browse the repository at this point in the history
Improved handling default images
  • Loading branch information
seadowg authored Mar 22, 2023
2 parents 605a6b9 + 0e1a779 commit 66ba346
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down Expand Up @@ -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;
Expand All @@ -294,7 +304,7 @@ private String getDefaultFilePath() {
Timber.w(e);
}

return "";
return null;
}

protected abstract boolean doesSupportDefaultValues();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public LiveData<Result<File>> 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) {
Expand Down

0 comments on commit 66ba346

Please sign in to comment.