16
16
17
17
import org .json .JSONException ;
18
18
import org .json .JSONObject ;
19
+ import org .junit .After ;
19
20
import org .junit .Before ;
20
21
import org .junit .Test ;
21
22
import org .mockito .Mock ;
22
23
import org .mockito .MockitoAnnotations ;
24
+ import org .powermock .reflect .Whitebox ;
23
25
import org .robolectric .Robolectric ;
24
26
import org .robolectric .annotation .Config ;
25
27
import org .robolectric .util .ReflectionHelpers ;
30
32
31
33
import edu .washington .cs .ubicomplab .rdt_reader .activity .RDTCaptureActivity ;
32
34
35
+ import static android .app .Activity .RESULT_CANCELED ;
36
+ import static android .app .Activity .RESULT_OK ;
33
37
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 ;
34
39
import static org .junit .Assert .assertEquals ;
35
40
import static org .junit .Assert .assertFalse ;
36
41
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 ;
37
45
import static org .robolectric .Shadows .shadowOf ;
38
46
39
47
@ Config (shadows = {ShadowContextCompat .class })
@@ -51,7 +59,7 @@ public class BasicRDTCaptureFactoryTest extends BaseTest {
51
59
public void setUp () throws JSONException {
52
60
MockitoAnnotations .initMocks (this );
53
61
basicRDTCaptureFactory = new BasicRDTCaptureFactory ();
54
- jsonFormActivity = Robolectric .buildActivity (JsonFormActivity .class , getJsonFormActivityIntent ()).create ().get ();
62
+ jsonFormActivity = spy ( Robolectric .buildActivity (JsonFormActivity .class , getJsonFormActivityIntent ()).create ().get () );
55
63
}
56
64
57
65
@ Test
@@ -105,4 +113,46 @@ public void testGetCustomTranslatableWidgetFieldsShouldReturnNonNullSet() {
105
113
Set <String > translatableFields = basicRDTCaptureFactory .getCustomTranslatableWidgetFields ();
106
114
assertNotNull (translatableFields );
107
115
}
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
+ }
108
158
}
0 commit comments