1+ package org .openimis .imisclaims ;
2+
3+ import static org .junit .Assert .*;
4+ import static org .mockito .Mockito .*;
5+
6+ import android .content .Intent ;
7+ import android .widget .AutoCompleteTextView ;
8+ import android .widget .Button ;
9+ import android .widget .EditText ;
10+
11+ import org .junit .Before ;
12+ import org .junit .Test ;
13+ import org .junit .runner .RunWith ;
14+ import org .robolectric .Robolectric ;
15+ import org .robolectric .RobolectricTestRunner ;
16+ import org .robolectric .annotation .Config ;
17+
18+ import java .util .HashMap ;
19+
20+ @ RunWith (RobolectricTestRunner .class )
21+ @ Config (sdk = {28 }, application = Global .class )
22+ public class AddServicesTest {
23+
24+ private AddServices activity ;
25+ private AutoCompleteTextView etServices ;
26+ private EditText etSQuantity ;
27+ private EditText etSAmount ;
28+ private Button btnAdd ;
29+ private SQLHandler mockSqlHandler ;
30+
31+ @ Before
32+ public void setup () {
33+ mockSqlHandler = mock (SQLHandler .class );
34+
35+ // Initialize static list BEFORE creating activity
36+ ClaimActivity .lvServiceList = new java .util .ArrayList <>();
37+
38+ activity = Robolectric .buildActivity (AddServices .class ).create ().get ();
39+ activity .sqlHandler = mockSqlHandler ;
40+
41+ etServices = activity .findViewById (R .id .etService );
42+ etSQuantity = activity .findViewById (R .id .etSQuantity );
43+ etSAmount = activity .findViewById (R .id .etSAmount );
44+ btnAdd = activity .findViewById (R .id .btnAdd );
45+ }
46+
47+ @ Test
48+ public void onCreate_disablesFieldsWhenReadonly () {
49+ Intent intent = new Intent ();
50+ intent .putExtra (ClaimActivity .EXTRA_READONLY , true );
51+
52+ activity = Robolectric .buildActivity (AddServices .class , intent ).create ().get ();
53+
54+ etServices = activity .findViewById (R .id .etService );
55+ etSQuantity = activity .findViewById (R .id .etSQuantity );
56+ etSAmount = activity .findViewById (R .id .etSAmount );
57+ btnAdd = activity .findViewById (R .id .btnAdd );
58+
59+ assertFalse (etServices .isEnabled ());
60+ assertFalse (etSQuantity .isEnabled ());
61+ assertFalse (etSAmount .isEnabled ());
62+ assertFalse (btnAdd .isEnabled ());
63+ }
64+
65+ @ Test
66+ public void addButton_isDisabledInitially () {
67+ assertFalse (btnAdd .isEnabled ());
68+ }
69+
70+ @ Test
71+ public void addButton_enablesWhenAllFieldsFilled () {
72+ etServices .setText ("SRV001" );
73+ etSQuantity .setText ("2" );
74+ etSAmount .setText ("5000" );
75+
76+ assertTrue (btnAdd .isEnabled ());
77+ }
78+
79+ @ Test
80+ public void addButton_disablesWhenServiceEmpty () {
81+ etServices .setText ("" );
82+ etSQuantity .setText ("2" );
83+ etSAmount .setText ("5000" );
84+
85+ assertFalse (btnAdd .isEnabled ());
86+ }
87+
88+ @ Test
89+ public void addButton_disablesWhenQuantityEmpty () {
90+ etServices .setText ("SRV001" );
91+ etSQuantity .setText ("" );
92+ etSAmount .setText ("5000" );
93+
94+ assertFalse (btnAdd .isEnabled ());
95+ }
96+
97+ @ Test
98+ public void addButton_disablesWhenAmountEmpty () {
99+ etServices .setText ("SRV001" );
100+ etSQuantity .setText ("2" );
101+ etSAmount .setText ("" );
102+
103+ assertFalse (btnAdd .isEnabled ());
104+ }
105+
106+ @ Test
107+ public void addButton_addsServiceToList () {
108+ activity .oService = new HashMap <>();
109+ activity .oService .put ("Code" , "SRV001" );
110+ activity .oService .put ("Name" , "Consultation" );
111+ activity .oService .put ("PackageType" , "S" );
112+
113+ etSQuantity .setText ("1" );
114+ etSAmount .setText ("2000" );
115+
116+ int initialSize = ClaimActivity .lvServiceList .size ();
117+ btnAdd .performClick ();
118+
119+ assertEquals (initialSize + 1 , ClaimActivity .lvServiceList .size ());
120+
121+ HashMap <String , String > addedService = ClaimActivity .lvServiceList .get (initialSize );
122+ assertEquals ("SRV001" , addedService .get ("Code" ));
123+ assertEquals ("Consultation" , addedService .get ("Name" ));
124+ assertEquals ("2000" , addedService .get ("Price" ));
125+ assertEquals ("1" , addedService .get ("Quantity" ));
126+ assertEquals ("S" , addedService .get ("PackageType" ));
127+ }
128+
129+ @ Test
130+ public void addButton_usesDefaultQuantityWhenEmpty () {
131+ activity .oService = new HashMap <>();
132+ activity .oService .put ("Code" , "SRV001" );
133+ activity .oService .put ("Name" , "Consultation" );
134+ activity .oService .put ("PackageType" , "S" );
135+
136+ etSQuantity .setText ("" );
137+ etSAmount .setText ("2000" );
138+
139+ btnAdd .performClick ();
140+
141+ HashMap <String , String > addedService = ClaimActivity .lvServiceList .get (0 );
142+ assertEquals ("1" , addedService .get ("Quantity" ));
143+ }
144+
145+ @ Test
146+ public void addButton_clearsFieldsAfterAdding () {
147+ activity .oService = new HashMap <>();
148+ activity .oService .put ("Code" , "SRV001" );
149+ activity .oService .put ("Name" , "Consultation" );
150+ activity .oService .put ("PackageType" , "S" );
151+
152+ etServices .setText ("TEST" );
153+ etSQuantity .setText ("1" );
154+ etSAmount .setText ("2000" );
155+
156+ btnAdd .performClick ();
157+
158+ assertEquals ("" , etServices .getText ().toString ());
159+ assertEquals ("" , etSQuantity .getText ().toString ());
160+ assertEquals ("" , etSAmount .getText ().toString ());
161+ }
162+
163+ @ Test
164+ public void addButton_doesNothingWhenServiceNotSelected () {
165+ activity .oService = null ;
166+
167+ etSQuantity .setText ("1" );
168+ etSAmount .setText ("2000" );
169+
170+ int initialSize = ClaimActivity .lvServiceList .size ();
171+ btnAdd .performClick ();
172+
173+ assertEquals (initialSize , ClaimActivity .lvServiceList .size ());
174+ }
175+ }
0 commit comments