Skip to content

Commit

Permalink
Added tests to verify that the error message is displayed when the an…
Browse files Browse the repository at this point in the history
…swer can not be loaded
  • Loading branch information
grzesiek2010 committed Feb 3, 2023
1 parent b9fa264 commit 1bf5052
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.odk.collect.shared.TempFiles;

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

import static java.util.Collections.singletonList;
import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -140,6 +141,30 @@ public void whenThereIsNoAnswer_hideImageViewAndErrorMessage() {
assertThat(widget.getErrorTextView().getVisibility(), is(View.GONE));
}

@Test
public void whenTheAnswerImageCanNotBeLoaded_hideImageViewAndShowErrorMessage() throws IOException {
CollectHelpers.overrideAppDependencyModule(new AppDependencyModule() {
@Override
public ImageLoader providesImageLoader() {
return new SynchronousImageLoader(true);
}
});

String imagePath = File.createTempFile("current", ".bmp").getAbsolutePath();
currentFile = new File(imagePath);

formEntryPrompt = new MockFormEntryPromptBuilder()
.withAnswerDisplayText(DrawWidgetTest.USER_SPECIFIED_IMAGE_ANSWER)
.build();

AnnotateWidget widget = createWidget();

assertThat(widget.getImageView().getVisibility(), is(View.GONE));
assertThat(widget.getImageView().getDrawable(), nullValue());

assertThat(widget.getErrorTextView().getVisibility(), is(View.VISIBLE));
}

@Test
public void whenPromptHasDefaultAnswer_showsInImageView() throws Exception {
String imagePath = File.createTempFile("default", ".bmp").getAbsolutePath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.odk.collect.shared.TempFiles;

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

import static java.util.Collections.singletonList;
import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -113,6 +114,30 @@ public void whenThereIsNoAnswer_hideImageViewAndErrorMessage() {
assertThat(widget.getErrorTextView().getVisibility(), is(View.GONE));
}

@Test
public void whenTheAnswerImageCanNotBeLoaded_hideImageViewAndShowErrorMessage() throws IOException {
CollectHelpers.overrideAppDependencyModule(new AppDependencyModule() {
@Override
public ImageLoader providesImageLoader() {
return new SynchronousImageLoader(true);
}
});

String imagePath = File.createTempFile("current", ".bmp").getAbsolutePath();
currentFile = new File(imagePath);

formEntryPrompt = new MockFormEntryPromptBuilder()
.withAnswerDisplayText(DrawWidgetTest.USER_SPECIFIED_IMAGE_ANSWER)
.build();

DrawWidget widget = createWidget();

assertThat(widget.getImageView().getVisibility(), is(View.GONE));
assertThat(widget.getImageView().getDrawable(), nullValue());

assertThat(widget.getErrorTextView().getVisibility(), is(View.VISIBLE));
}

@Test
public void whenPromptHasDefaultAnswer_showsInImageView() throws Exception {
String imagePath = File.createTempFile("default", ".bmp").getAbsolutePath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.odk.collect.shared.TempFiles;

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

import static java.util.Collections.singletonList;
import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -119,6 +120,30 @@ public void whenThereIsNoAnswer_hideImageViewAndErrorMessage() {
assertThat(widget.getErrorTextView().getVisibility(), is(View.GONE));
}

@Test
public void whenTheAnswerImageCanNotBeLoaded_hideImageViewAndShowErrorMessage() throws IOException {
CollectHelpers.overrideAppDependencyModule(new AppDependencyModule() {
@Override
public ImageLoader providesImageLoader() {
return new SynchronousImageLoader(true);
}
});

String imagePath = File.createTempFile("current", ".bmp").getAbsolutePath();
currentFile = new File(imagePath);

formEntryPrompt = new MockFormEntryPromptBuilder()
.withAnswerDisplayText(DrawWidgetTest.USER_SPECIFIED_IMAGE_ANSWER)
.build();

ImageWidget widget = createWidget();

assertThat(widget.getImageView().getVisibility(), is(View.GONE));
assertThat(widget.getImageView().getDrawable(), nullValue());

assertThat(widget.getErrorTextView().getVisibility(), is(View.VISIBLE));
}

@Test
public void whenPromptHasDefaultAnswer_doesNotShow() throws Exception {
String imagePath = File.createTempFile("default", ".bmp").getAbsolutePath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.odk.collect.shared.TempFiles;

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

import static java.util.Collections.singletonList;
import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -106,6 +107,30 @@ public void whenThereIsNoAnswer_hideImageViewAndErrorMessage() {
assertThat(widget.getErrorTextView().getVisibility(), is(View.GONE));
}

@Test
public void whenTheAnswerImageCanNotBeLoaded_hideImageViewAndShowErrorMessage() throws IOException {
CollectHelpers.overrideAppDependencyModule(new AppDependencyModule() {
@Override
public ImageLoader providesImageLoader() {
return new SynchronousImageLoader(true);
}
});

String imagePath = File.createTempFile("current", ".bmp").getAbsolutePath();
currentFile = new File(imagePath);

formEntryPrompt = new MockFormEntryPromptBuilder()
.withAnswerDisplayText(DrawWidgetTest.USER_SPECIFIED_IMAGE_ANSWER)
.build();

SignatureWidget widget = createWidget();

assertThat(widget.getImageView().getVisibility(), is(View.GONE));
assertThat(widget.getImageView().getDrawable(), nullValue());

assertThat(widget.getErrorTextView().getVisibility(), is(View.VISIBLE));
}

@Test
public void whenPromptHasDefaultAnswer_showsInImageView() throws Exception {
String imagePath = File.createTempFile("default", ".bmp").getAbsolutePath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ import org.odk.collect.imageloader.GlideImageLoader
import org.odk.collect.imageloader.ImageLoader
import java.io.File

class SynchronousImageLoader : ImageLoader {
class SynchronousImageLoader(private val fail: Boolean = false) : ImageLoader {
override fun loadImage(
imageView: ImageView,
imageFile: File?,
scaleType: ImageView.ScaleType,
requestListener: GlideImageLoader.ImageLoaderCallback?
) {
imageView.setImageBitmap(BitmapFactory.decodeFile(imageFile?.absolutePath))
requestListener?.onLoadSucceeded()
if (fail) {
requestListener?.onLoadFailed()
} else {
imageView.setImageBitmap(BitmapFactory.decodeFile(imageFile?.absolutePath))
requestListener?.onLoadSucceeded()
}
}
}

0 comments on commit 1bf5052

Please sign in to comment.