1+ package org .openimis .imisclaims ;
2+
3+ import static org .junit .Assert .*;
4+ import static org .mockito .Mockito .*;
5+
6+ import android .app .AlertDialog ;
7+ import android .database .Cursor ;
8+
9+ import org .junit .Before ;
10+ import org .junit .Test ;
11+ import org .junit .runner .RunWith ;
12+ import org .robolectric .Robolectric ;
13+ import org .robolectric .RobolectricTestRunner ;
14+ import org .robolectric .annotation .Config ;
15+
16+ import java .util .HashMap ;
17+
18+ @ RunWith (RobolectricTestRunner .class )
19+ @ Config (sdk = {28 }, application = Global .class )
20+ public class MapServicesTest {
21+
22+ SQLHandler mockSqlHandler ;
23+ Cursor mockCursor ;
24+ MapServices activity ;
25+
26+ @ Before
27+ public void setup () {
28+ // Création des mocks avec la syntaxe moderne
29+ mockSqlHandler = mock (SQLHandler .class );
30+ mockCursor = mock (Cursor .class );
31+
32+ activity = Robolectric .buildActivity (MapServices .class ).create ().get ();
33+ activity .sqlHandler = mockSqlHandler ;
34+ }
35+
36+ @ Test
37+ public void bindServiceList_loadsServicesCorrectly () {
38+ doReturn (mockCursor ).when (activity .sqlHandler ).getMapping ("S" );
39+ when (mockCursor .moveToFirst ()).thenReturn (true );
40+ when (mockCursor .isAfterLast ()).thenReturn (false , true );
41+ when (mockCursor .getString (0 )).thenReturn ("SRV001" );
42+ when (mockCursor .getString (1 )).thenReturn ("Consultation" );
43+ when (mockCursor .getString (2 )).thenReturn (null );
44+
45+ activity .BindItemList ();
46+
47+ assertEquals (1 , activity .ServiceList .size ());
48+ HashMap <String , Object > service = activity .ServiceList .get (0 );
49+ assertEquals ("SRV001" , service .get ("Code" ));
50+ assertEquals ("Consultation" , service .get ("Name" ));
51+ assertFalse ((Boolean ) service .get ("isMapped" ));
52+ verify (mockCursor ).close ();
53+ }
54+
55+ @ Test
56+ public void clickingService_togglesIsMapped () {
57+ HashMap <String , Object > service = new HashMap <>();
58+ service .put ("Code" , "SRV001" );
59+ service .put ("Name" , "Test Service" );
60+ service .put ("isMapped" , false );
61+ activity .ServiceList .add (service );
62+
63+ activity .alAdapter = activity .new ServiceAdapter (activity , activity .ServiceList ,
64+ R .layout .mappinglist ,
65+ new String []{"Code" , "Name" , "isMapped" },
66+ new int []{R .id .tvMapCode , R .id .tvMapName , R .id .chkMap });
67+
68+ activity .lvMapServices .setAdapter (activity .alAdapter );
69+
70+ activity .lvMapServices .performItemClick (null , 0 , 0 );
71+
72+ assertTrue ((Boolean ) activity .ServiceList .get (0 ).get ("isMapped" ));
73+ }
74+
75+ @ Test
76+ public void checkAll_setsAllServicesMapped () {
77+ for (int i = 0 ; i < 3 ; i ++) {
78+ HashMap <String , Object > service = new HashMap <>();
79+ service .put ("Code" , "SRV00" + i );
80+ service .put ("Name" , "Service " + i );
81+ service .put ("isMapped" , false );
82+ activity .ServiceList .add (service );
83+ }
84+
85+ activity .alAdapter = activity .new ServiceAdapter (activity , activity .ServiceList ,
86+ R .layout .mappinglist ,
87+ new String []{"Code" , "Name" , "isMapped" },
88+ new int []{R .id .tvMapCode , R .id .tvMapName , R .id .chkMap });
89+
90+ activity .CheckUncheckAll (true );
91+
92+ for (HashMap <String , Object > service : activity .ServiceList ) {
93+ assertTrue ((Boolean ) service .get ("isMapped" ));
94+ }
95+ }
96+
97+ @ Test
98+ public void save_returnsCorrectCodes () {
99+ HashMap <String , Object > service = new HashMap <>();
100+ service .put ("Code" , "SRV001" );
101+ service .put ("Name" , "Test Service" );
102+ service .put ("isMapped" , false );
103+ activity .ServiceList .add (service );
104+
105+ when (mockSqlHandler .InsertMapping (anyString (), anyString (), anyString ())).thenReturn (true );
106+
107+ int result = activity .Save ();
108+ assertEquals (1 , result );
109+
110+ service .put ("isMapped" , true );
111+ result = activity .Save ();
112+ assertEquals (0 , result );
113+ }
114+
115+ @ Test
116+ public void save_returns2_whenInsertFails () {
117+ HashMap <String , Object > service = new HashMap <>();
118+ service .put ("Code" , "SRV001" );
119+ service .put ("Name" , "Test Service" );
120+ service .put ("isMapped" , true );
121+ activity .ServiceList .add (service );
122+
123+ when (mockSqlHandler .InsertMapping (anyString (), anyString (), anyString ())).thenReturn (false );
124+ doNothing ().when (mockSqlHandler ).ClearMapping (anyString ());
125+
126+ int result = activity .Save ();
127+ assertEquals (2 , result );
128+ }
129+
130+ @ Test
131+ public void filter_searchWorksCorrectly () {
132+ HashMap <String , Object > service1 = new HashMap <>();
133+ service1 .put ("Code" , "SRV001" );
134+ service1 .put ("Name" , "Consultation" );
135+ service1 .put ("isMapped" , false );
136+ activity .ServiceList .add (service1 );
137+
138+ HashMap <String , Object > service2 = new HashMap <>();
139+ service2 .put ("Code" , "SRV002" );
140+ service2 .put ("Name" , "Surgery" );
141+ service2 .put ("isMapped" , false );
142+ activity .ServiceList .add (service2 );
143+
144+ activity .alAdapter = activity .new ServiceAdapter (activity , activity .ServiceList ,
145+ R .layout .mappinglist ,
146+ new String []{"Code" , "Name" , "isMapped" },
147+ new int []{R .id .tvMapCode , R .id .tvMapName , R .id .chkMap });
148+
149+ activity .alAdapter .getFilter ().filter ("cons" );
150+
151+ assertEquals (1 , activity .ServiceList .size ());
152+ assertEquals ("Consultation" , activity .ServiceList .get (0 ).get ("Name" ));
153+ }
154+
155+ @ Test
156+ public void adapter_getView_setsCorrectValues () {
157+ HashMap <String , Object > service = new HashMap <>();
158+ service .put ("Code" , "SRV001" );
159+ service .put ("Name" , "Test Service" );
160+ service .put ("isMapped" , true );
161+ activity .ServiceList .add (service );
162+
163+ MapServices .ServiceAdapter adapter = activity .new ServiceAdapter (activity , activity .ServiceList ,
164+ R .layout .mappinglist ,
165+ new String []{"Code" , "Name" , "isMapped" },
166+ new int []{R .id .tvMapCode , R .id .tvMapName , R .id .chkMap });
167+
168+ assertNotNull (adapter .getView (0 , null , null ));
169+ }
170+
171+ @ Test
172+ public void showDialog_createsAlertDialog () {
173+ AlertDialog dialog = activity .ShowDialog ("Test message" );
174+ assertNotNull (dialog );
175+ }
176+
177+ @ Test
178+ public void uncheckAll_setsAllServicesUnmapped () {
179+ for (int i = 0 ; i < 3 ; i ++) {
180+ HashMap <String , Object > service = new HashMap <>();
181+ service .put ("Code" , "SRV00" + i );
182+ service .put ("Name" , "Service " + i );
183+ service .put ("isMapped" , true );
184+ activity .ServiceList .add (service );
185+ }
186+
187+ activity .alAdapter = activity .new ServiceAdapter (activity , activity .ServiceList ,
188+ R .layout .mappinglist ,
189+ new String []{"Code" , "Name" , "isMapped" },
190+ new int []{R .id .tvMapCode , R .id .tvMapName , R .id .chkMap });
191+
192+ activity .CheckUncheckAll (false );
193+
194+ for (HashMap <String , Object > service : activity .ServiceList ) {
195+ assertFalse ((Boolean ) service .get ("isMapped" ));
196+ }
197+ }
198+
199+ @ Test
200+ public void bindServiceList_loadsMultipleServicesCorrectly () {
201+ doReturn (mockCursor ).when (activity .sqlHandler ).getMapping ("S" );
202+ when (mockCursor .moveToFirst ()).thenReturn (true );
203+ when (mockCursor .isAfterLast ()).thenReturn (false , false , true );
204+ when (mockCursor .getString (0 )).thenReturn ("SRV001" , "SRV002" );
205+ when (mockCursor .getString (1 )).thenReturn ("Consultation" , "Surgery" );
206+ when (mockCursor .getString (2 )).thenReturn (null , "mapped" );
207+
208+ activity .BindItemList ();
209+
210+ assertEquals (2 , activity .ServiceList .size ());
211+ assertFalse ((Boolean ) activity .ServiceList .get (0 ).get ("isMapped" ));
212+ assertTrue ((Boolean ) activity .ServiceList .get (1 ).get ("isMapped" ));
213+ verify (mockCursor ).close ();
214+ }
215+
216+ @ Test
217+ public void save_clearsMapping_beforeInserting () {
218+ HashMap <String , Object > service = new HashMap <>();
219+ service .put ("Code" , "SRV001" );
220+ service .put ("Name" , "Test" );
221+ service .put ("isMapped" , true );
222+ activity .ServiceList .add (service );
223+
224+ when (mockSqlHandler .InsertMapping (anyString (), anyString (), anyString ())).thenReturn (true );
225+ doNothing ().when (mockSqlHandler ).ClearMapping ("S" );
226+
227+ activity .Save ();
228+
229+ verify (mockSqlHandler ).ClearMapping ("S" );
230+ verify (mockSqlHandler ).InsertMapping ("SRV001" , "Test" , "S" );
231+ }
232+ }
0 commit comments