Skip to content

Commit

Permalink
Merge pull request #479 from OpenSRP/tt-09-2020
Browse files Browse the repository at this point in the history
Add unit tests
  • Loading branch information
vincent-karuri authored Sep 15, 2020
2 parents 2c89570 + a426fc4 commit 8c62e5c
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.vijay.jsonwizard.listeners;

import android.app.AlertDialog;
import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;

import com.vijay.jsonwizard.R;
import com.vijay.jsonwizard.constants.JsonFormConstants;
import com.vijay.jsonwizard.shadow.ShadowUtils;
import com.vijay.jsonwizard.utils.FormUtils;
import com.vijay.jsonwizard.widgets.FactoryTest;

Expand All @@ -17,45 +20,114 @@
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowAlertDialog;
import org.robolectric.util.ReflectionHelpers;

/**
* Created by Vincent Karuri on 07/09/2020
*/

@Config(shadows = {ShadowUtils.class})
public class ExpansionPanelUndoButtonClickListenerTest extends FactoryTest {

private ExpansionPanelUndoButtonClickListener expansionPanelUndoButtonClickListener;
private JSONObject field;
private JSONObject mJsonObject;
private JSONObject global;

private final String KEY_1 = "key1";
private final String KEY_2 = "key2";
private final String KEY_3 = "key3";

private final String VALUE_1 = "value1";
private final String VALUE_3 = "value3";

@Before
public void setUp() {
super.setUp();
expansionPanelUndoButtonClickListener = new ExpansionPanelUndoButtonClickListener();
bootStrapPanel();
}

@Test
public void testOnClickShouldShowExpansionPanel() {
callOnClick();

AlertDialog alertDialog = Mockito.spy(ShadowAlertDialog.getLatestAlertDialog());
Assert.assertTrue(alertDialog.isShowing());
}

@Test
public void onClickShouldSetUpExpansionPanel() throws JSONException {
final String KEY = "key";
final String VALUE = "value";
public void testExpansionPanelButtonsShouldPerformCorrectAction() throws JSONException {
Assert.assertEquals(VALUE_1, global.get(KEY_1));
Assert.assertEquals(VALUE_3, global.get(KEY_3));

callOnClick();
AlertDialog alertDialog = (AlertDialog) Mockito.spy(ShadowAlertDialog.getShownDialogs().get(0));
alertDialog.findViewById(R.id.undo_button).performClick();

Mockito.verify(field).remove(ArgumentMatchers.eq(JsonFormConstants.VALUE));
Mockito.verify(field).remove(ArgumentMatchers.eq(JsonFormConstants.REQUIRED_FIELDS));
Mockito.verify(jsonFormActivity).setmJSONObject(ArgumentMatchers.eq(mJsonObject));

Assert.assertEquals("", global.get(KEY_1));
Assert.assertEquals(VALUE_3, global.get(KEY_3));
}

private void bootStrapPanel() {
try {
expansionPanelUndoButtonClickListener = new ExpansionPanelUndoButtonClickListener();

mJsonObject = new JSONObject();

global = new JSONObject();
global.put(KEY_1, VALUE_1);
global.put(KEY_3, VALUE_3);
mJsonObject.put(JsonFormConstants.GLOBAL, global);

Mockito.doReturn(mJsonObject).when(jsonFormActivity).getmJSONObject();

FormUtils formUtils = Mockito.mock(FormUtils.class);
JSONArray fields = new JSONArray();
field = Mockito.spy(new JSONObject());
field.put(JsonFormConstants.KEY, JsonFormConstants.KEY);
field.put(JsonFormConstants.VALUE, JsonFormConstants.VALUE);
field.put(JsonFormConstants.TEXT, "header");

JSONArray valueItemJSONArray = new JSONArray();
valueItemJSONArray.put(new JSONObject());

JSONArray selectedValues = new JSONArray();
JSONObject selectedValue = new JSONObject();
selectedValue.put(JsonFormConstants.KEY, KEY_1);
selectedValue.put(JsonFormConstants.VALUES, valueItemJSONArray);
selectedValue.put(JsonFormConstants.TYPE, "");
selectedValues.put(selectedValue);

selectedValue = new JSONObject();
selectedValue.put(JsonFormConstants.KEY, KEY_2);
selectedValue.put(JsonFormConstants.VALUES, valueItemJSONArray);
selectedValue.put(JsonFormConstants.TYPE, "");
selectedValues.put(selectedValue);

field.put(JsonFormConstants.VALUE, selectedValues);

fields.put(field);
Mockito.doReturn(fields).when(formUtils).getFormFields(ArgumentMatchers.anyString(),
ArgumentMatchers.any(Context.class));

ReflectionHelpers.setField(expansionPanelUndoButtonClickListener, "formUtils", formUtils);
} catch (Exception e) {
// do nothing
}
}

private void callOnClick() {
View view = new View(RuntimeEnvironment.application);
view.setTag(R.id.key, KEY);
view.setTag(R.id.key, JsonFormConstants.KEY);
view.setTag(R.id.specify_context, jsonFormActivity);
view.setTag(R.id.specify_step_name, "step1");

FormUtils formUtils = Mockito.mock(FormUtils.class);
JSONArray fields = new JSONArray();
JSONObject field = new JSONObject();
field.put(JsonFormConstants.KEY, KEY);
field.put(JsonFormConstants.VALUE, VALUE);
field.put(JsonFormConstants.TEXT, "header");
fields.put(field);
Mockito.doReturn(fields).when(formUtils).getFormFields(ArgumentMatchers.anyString(),
ArgumentMatchers.any(Context.class));

ReflectionHelpers.setField(expansionPanelUndoButtonClickListener, "formUtils", formUtils);
view.setTag(R.id.linearLayout, new LinearLayout(RuntimeEnvironment.application));
expansionPanelUndoButtonClickListener.onClick(view);

Assert.assertNotNull(ShadowAlertDialog.getLatestAlertDialog());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.vijay.jsonwizard.shadow;

import com.vijay.jsonwizard.event.BaseEvent;
import com.vijay.jsonwizard.utils.Utils;

import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.shadow.api.Shadow;

/**
* Created by Vincent Karuri on 08/09/2020
*/

@Implements(Utils.class)
public class ShadowUtils extends Shadow {

@Implementation
public static void postEvent(BaseEvent event) {
// do nothing
}
}

0 comments on commit 8c62e5c

Please sign in to comment.