Skip to content

Commit 3fee953

Browse files
Merge pull request #469 from OpenSRP/tt-25-08-2020
Add unit tests
2 parents c8b78eb + bbe42ce commit 3fee953

File tree

4 files changed

+126
-17
lines changed

4 files changed

+126
-17
lines changed

android-json-form-wizard/src/main/java/com/vijay/jsonwizard/widgets/ButtonFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public List<View> getViewsFromJson(String stepName, final Context context,
4949

5050
@Override
5151
public List<View> getViewsFromJson(String stepName, Context context, JsonFormFragment formFragment, JSONObject jsonObject, CommonListener listener) throws Exception {
52-
return attachJson(stepName, context, formFragment, jsonObject, listener, false);
52+
return getViewsFromJson(stepName, context, formFragment, jsonObject, listener, false);
5353
}
5454

5555
private List<View> attachJson(String stepName, final Context context, final JsonFormFragment formFragment, JSONObject jsonObject, CommonListener listener, boolean popup) throws JSONException {

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

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
import android.content.pm.PackageManager;
55
import android.view.View;
66

7-
import com.vijay.jsonwizard.BaseTest;
87
import com.vijay.jsonwizard.R;
9-
import com.vijay.jsonwizard.activities.JsonFormActivity;
108
import com.vijay.jsonwizard.constants.JsonFormConstants;
119
import com.vijay.jsonwizard.domain.WidgetArgs;
1210
import com.vijay.jsonwizard.fragments.JsonFormFragment;
@@ -16,13 +14,10 @@
1614

1715
import org.json.JSONException;
1816
import org.json.JSONObject;
19-
import org.junit.After;
2017
import org.junit.Before;
2118
import org.junit.Test;
2219
import org.mockito.Mock;
23-
import org.mockito.MockitoAnnotations;
2420
import org.powermock.reflect.Whitebox;
25-
import org.robolectric.Robolectric;
2621
import org.robolectric.annotation.Config;
2722
import org.robolectric.util.ReflectionHelpers;
2823

@@ -40,26 +35,23 @@
4035
import static org.junit.Assert.assertFalse;
4136
import static org.junit.Assert.assertNotNull;
4237
import static org.mockito.ArgumentMatchers.eq;
43-
import static org.mockito.Mockito.spy;
4438
import static org.mockito.Mockito.verify;
4539
import static org.robolectric.Shadows.shadowOf;
4640

4741
@Config(shadows = {ShadowContextCompat.class})
48-
public class BasicRDTCaptureFactoryTest extends BaseTest {
42+
public class BasicRDTCaptureFactoryTest extends FactoryTest {
4943

5044
private BasicRDTCaptureFactory basicRDTCaptureFactory;
51-
private JsonFormActivity jsonFormActivity;
5245

5346
@Mock
5447
private JsonFormFragment formFragment;
5548
@Mock
5649
private CommonListener listener;
5750

5851
@Before
59-
public void setUp() throws JSONException {
60-
MockitoAnnotations.initMocks(this);
52+
public void setUp() {
53+
super.setUp();
6154
basicRDTCaptureFactory = new BasicRDTCaptureFactory();
62-
jsonFormActivity = spy(Robolectric.buildActivity(JsonFormActivity.class, getJsonFormActivityIntent()).create().get());
6355
}
6456

6557
@Test
@@ -141,11 +133,6 @@ public void testOnActivityResultShouldCorrectlyExtractCaptureValues() throws JSO
141133
verify(formFragment).save(eq(true));
142134
}
143135

144-
@After
145-
public void tearDown() {
146-
jsonFormActivity.finish();
147-
}
148-
149136
private WidgetArgs getWidgetArgs() {
150137
WidgetArgs widgetArgs = new WidgetArgs();
151138
widgetArgs.withFormFragment(formFragment)
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.vijay.jsonwizard.widgets;
2+
3+
import com.rey.material.widget.Button;
4+
import com.vijay.jsonwizard.R;
5+
import com.vijay.jsonwizard.constants.JsonFormConstants;
6+
import com.vijay.jsonwizard.fragments.JsonFormFragment;
7+
import com.vijay.jsonwizard.interfaces.CommonListener;
8+
9+
import org.json.JSONException;
10+
import org.json.JSONObject;
11+
import org.junit.Assert;
12+
import org.junit.Before;
13+
import org.junit.Test;
14+
import org.mockito.ArgumentMatchers;
15+
import org.mockito.Mock;
16+
import org.mockito.Mockito;
17+
18+
import static com.vijay.jsonwizard.constants.JsonFormConstants.STEP1;
19+
20+
/**
21+
* Created by Vincent Karuri on 25/08/2020
22+
*/
23+
public class ButtonFactoryTest extends FactoryTest {
24+
25+
@Mock
26+
private JsonFormFragment jsonFormFragment;
27+
@Mock
28+
private CommonListener commonListener;
29+
30+
private ButtonFactory buttonFactory;
31+
32+
@Before
33+
public void setUp() {
34+
super.setUp();
35+
buttonFactory = new ButtonFactory();
36+
}
37+
38+
@Test
39+
public void testGetViewsFromJsonShouldCorrectlyInitializeWidget() throws Exception {
40+
JSONObject jsonObject = getJsonObject();
41+
Button button = (Button) buttonFactory.getViewsFromJson(STEP1, jsonFormActivity, jsonFormFragment,
42+
jsonObject, commonListener).get(0);
43+
Assert.assertEquals(getJsonObject().get(JsonFormConstants.KEY), button.getTag(R.id.key));
44+
Assert.assertEquals(jsonObject.get(JsonFormConstants.OPENMRS_ENTITY_PARENT), button.getTag(R.id.openmrs_entity_parent));
45+
Assert.assertEquals(jsonObject.get(JsonFormConstants.OPENMRS_ENTITY), button.getTag(R.id.openmrs_entity));
46+
Assert.assertEquals(jsonObject.get(JsonFormConstants.OPENMRS_ENTITY_ID), button.getTag(R.id.openmrs_entity_id));
47+
Assert.assertEquals(jsonObject.getString(JsonFormConstants.TYPE), button.getTag(R.id.type));
48+
Assert.assertEquals(STEP1 + ":" + jsonObject.getString(JsonFormConstants.KEY), button.getTag(R.id.address));
49+
Assert.assertEquals(false, button.getTag(R.id.extraPopup));
50+
Assert.assertEquals(JsonFormConstants.VALUE, button.getTag(R.id.raw_value));
51+
Assert.assertEquals(jsonObject.optString(JsonFormConstants.RELEVANCE), button.getTag(R.id.relevance));
52+
Assert.assertTrue(button.isEnabled());
53+
Assert.assertTrue(button.isFocusable());
54+
Assert.assertEquals(button.getText(), jsonObject.get(JsonFormConstants.HINT));
55+
56+
// default action
57+
Assert.assertFalse(jsonObject.getJSONObject(JsonFormConstants.ACTION).getBoolean(JsonFormConstants.RESULT));
58+
Assert.assertEquals(jsonObject.optString(JsonFormConstants.VALUE), Boolean.FALSE.toString());
59+
60+
// action BEHAVIOUR_FINISH_FORM
61+
jsonObject.getJSONObject(JsonFormConstants.ACTION).put(JsonFormConstants.BEHAVIOUR, JsonFormConstants.BEHAVIOUR_FINISH_FORM);
62+
button = (Button) buttonFactory.getViewsFromJson(STEP1, jsonFormActivity, jsonFormFragment,
63+
jsonObject, commonListener).get(0);
64+
button.performClick();
65+
Mockito.verify(jsonFormFragment).save(ArgumentMatchers.eq(false));
66+
Assert.assertEquals(Boolean.TRUE.toString(), button.getTag(R.id.raw_value));
67+
68+
// action BEHAVIOUR_NEXT_STEP
69+
jsonObject.getJSONObject(JsonFormConstants.ACTION).put(JsonFormConstants.BEHAVIOUR, JsonFormConstants.BEHAVIOUR_NEXT_STEP);
70+
button = (Button) buttonFactory.getViewsFromJson(STEP1, jsonFormActivity, jsonFormFragment,
71+
jsonObject, commonListener).get(0);
72+
button.performClick();
73+
Mockito.verify(jsonFormFragment).next();
74+
}
75+
76+
private JSONObject getJsonObject() throws JSONException {
77+
JSONObject jsonObject = new JSONObject();
78+
jsonObject.put(JsonFormConstants.OPENMRS_ENTITY_PARENT, "entity_parent");
79+
jsonObject.put(JsonFormConstants.OPENMRS_ENTITY, "entity");
80+
jsonObject.put(JsonFormConstants.OPENMRS_ENTITY_ID, "entity_id");
81+
jsonObject.put(JsonFormConstants.RELEVANCE, JsonFormConstants.RELEVANCE);
82+
jsonObject.put(JsonFormConstants.HINT, JsonFormConstants.HINT);
83+
jsonObject.put(JsonFormConstants.KEY, JsonFormConstants.KEY);
84+
jsonObject.put(JsonFormConstants.TYPE, JsonFormConstants.TYPE);
85+
jsonObject.put(JsonFormConstants.VALUE, JsonFormConstants.VALUE);
86+
jsonObject.put(JsonFormConstants.READ_ONLY, false);
87+
88+
JSONObject action = new JSONObject();
89+
jsonObject.put(JsonFormConstants.ACTION, action);
90+
return jsonObject;
91+
}
92+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.vijay.jsonwizard.widgets;
2+
3+
import com.vijay.jsonwizard.BaseTest;
4+
import com.vijay.jsonwizard.activities.JsonFormActivity;
5+
6+
import org.junit.After;
7+
import org.junit.Before;
8+
import org.mockito.Mockito;
9+
import org.mockito.MockitoAnnotations;
10+
import org.robolectric.Robolectric;
11+
12+
/**
13+
* Created by Vincent Karuri on 25/08/2020
14+
*/
15+
public abstract class FactoryTest extends BaseTest {
16+
17+
protected JsonFormActivity jsonFormActivity;
18+
19+
@Before
20+
public void setUp() {
21+
MockitoAnnotations.initMocks(this);
22+
jsonFormActivity = Robolectric.buildActivity(JsonFormActivity.class, getJsonFormActivityIntent()).create().get();
23+
jsonFormActivity = Mockito.spy(jsonFormActivity);
24+
}
25+
26+
@After
27+
public void tearDown() {
28+
jsonFormActivity.finish();
29+
}
30+
}

0 commit comments

Comments
 (0)