Skip to content

Commit cb660d7

Browse files
Merge pull request #464 from OpenSRP/tt-11-08-2020
Add BasicRDTCaptureFactory tests
2 parents 542d619 + 8ceac05 commit cb660d7

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

android-json-form-wizard/src/test/java/com/vijay/jsonwizard/widgets/BasicRDTCaptureFactoryTest.java

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616

1717
import org.json.JSONException;
1818
import org.json.JSONObject;
19+
import org.junit.After;
1920
import org.junit.Before;
2021
import org.junit.Test;
2122
import org.mockito.Mock;
2223
import org.mockito.MockitoAnnotations;
24+
import org.powermock.reflect.Whitebox;
2325
import org.robolectric.Robolectric;
2426
import org.robolectric.annotation.Config;
2527
import org.robolectric.util.ReflectionHelpers;
@@ -30,10 +32,16 @@
3032

3133
import edu.washington.cs.ubicomplab.rdt_reader.activity.RDTCaptureActivity;
3234

35+
import static android.app.Activity.RESULT_CANCELED;
36+
import static android.app.Activity.RESULT_OK;
3337
import static com.vijay.jsonwizard.constants.JsonFormConstants.RDT_CAPTURE_CODE;
38+
import static edu.washington.cs.ubicomplab.rdt_reader.core.Constants.SAVED_IMAGE_FILE_PATH;
3439
import static org.junit.Assert.assertEquals;
3540
import static org.junit.Assert.assertFalse;
3641
import static org.junit.Assert.assertNotNull;
42+
import static org.mockito.ArgumentMatchers.eq;
43+
import static org.mockito.Mockito.spy;
44+
import static org.mockito.Mockito.verify;
3745
import static org.robolectric.Shadows.shadowOf;
3846

3947
@Config(shadows = {ShadowContextCompat.class})
@@ -51,7 +59,7 @@ public class BasicRDTCaptureFactoryTest extends BaseTest {
5159
public void setUp() throws JSONException {
5260
MockitoAnnotations.initMocks(this);
5361
basicRDTCaptureFactory = new BasicRDTCaptureFactory();
54-
jsonFormActivity = Robolectric.buildActivity(JsonFormActivity.class, getJsonFormActivityIntent()).create().get();
62+
jsonFormActivity = spy(Robolectric.buildActivity(JsonFormActivity.class, getJsonFormActivityIntent()).create().get());
5563
}
5664

5765
@Test
@@ -105,4 +113,46 @@ public void testGetCustomTranslatableWidgetFieldsShouldReturnNonNullSet() {
105113
Set<String> translatableFields = basicRDTCaptureFactory.getCustomTranslatableWidgetFields();
106114
assertNotNull(translatableFields);
107115
}
116+
117+
@Test
118+
public void testCaptureActivityIsClosedOnBackPress() {
119+
Whitebox.setInternalState(basicRDTCaptureFactory, "widgetArgs", getWidgetArgs());
120+
basicRDTCaptureFactory.onActivityResult(1, RESULT_CANCELED, null);
121+
verify(jsonFormActivity).finish();
122+
}
123+
124+
@Test
125+
public void testOnActivityResultShouldCorrectlyExtractCaptureValues() throws JSONException {
126+
Whitebox.setInternalState(basicRDTCaptureFactory, "widgetArgs", getWidgetArgs());
127+
Intent intent = new Intent();
128+
intent.putExtra(SAVED_IMAGE_FILE_PATH, "file_path");
129+
130+
View view = new View(jsonFormActivity);
131+
view.setTag(R.id.key, "key");
132+
view.setTag(R.id.openmrs_entity_parent, "entity_parent");
133+
view.setTag(R.id.openmrs_entity, "entity");
134+
view.setTag(R.id.openmrs_entity_id, "entity_id");
135+
Whitebox.setInternalState(basicRDTCaptureFactory, "rootLayout", view);
136+
137+
basicRDTCaptureFactory.onActivityResult(RDT_CAPTURE_CODE, RESULT_OK, intent);
138+
verify(jsonFormActivity).writeValue(eq("step1"), eq("key"), eq("file_path"),
139+
eq("entity_parent"), eq("entity"), eq("entity_id"), eq(false));
140+
verify(formFragment).next();
141+
verify(formFragment).save(eq(true));
142+
}
143+
144+
@After
145+
public void tearDown() {
146+
jsonFormActivity.finish();
147+
}
148+
149+
private WidgetArgs getWidgetArgs() {
150+
WidgetArgs widgetArgs = new WidgetArgs();
151+
widgetArgs.withFormFragment(formFragment)
152+
.withContext(jsonFormActivity)
153+
.withStepName("step1")
154+
.withPopup(false)
155+
.withJsonObject(new JSONObject());
156+
return widgetArgs;
157+
}
108158
}

0 commit comments

Comments
 (0)