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
+ }
0 commit comments