|
| 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.app.Application; |
| 8 | +import androidx.test.core.app.ApplicationProvider; |
| 9 | +import android.database.Cursor; |
| 10 | +import android.database.MatrixCursor; |
| 11 | + |
| 12 | +import org.junit.Before; |
| 13 | +import org.junit.Test; |
| 14 | +import org.junit.runner.RunWith; |
| 15 | +import org.robolectric.Robolectric; |
| 16 | +import org.robolectric.RobolectricTestRunner; |
| 17 | +import org.robolectric.annotation.Config; |
| 18 | + |
| 19 | +import java.util.HashMap; |
| 20 | + |
| 21 | +@RunWith(RobolectricTestRunner.class) |
| 22 | +@Config(sdk = {28}, application = Global.class) |
| 23 | +public class MapItemsTest { |
| 24 | + |
| 25 | + SQLHandler mockSqlHandler; |
| 26 | + Cursor mockCursor; |
| 27 | + MapItems activity; |
| 28 | + |
| 29 | + @Before |
| 30 | + public void setup() { |
| 31 | + // Création des mocks avec la syntaxe moderne |
| 32 | + mockSqlHandler = mock(SQLHandler.class); |
| 33 | + mockCursor = mock(Cursor.class); |
| 34 | + |
| 35 | + activity = Robolectric.buildActivity(MapItems.class).create().get(); |
| 36 | + activity.sqlHandler = mockSqlHandler; |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void bindItemList_loadsItemsCorrectly() { |
| 41 | + doReturn(mockCursor).when(activity.sqlHandler).getMapping("I"); |
| 42 | + when(mockCursor.moveToFirst()).thenReturn(true); |
| 43 | + when(mockCursor.isAfterLast()).thenReturn(false, true); |
| 44 | + when(mockCursor.getString(0)).thenReturn("ITEM001"); |
| 45 | + when(mockCursor.getString(1)).thenReturn("Paracetamol"); |
| 46 | + when(mockCursor.getString(2)).thenReturn(null); |
| 47 | + |
| 48 | + activity.BindItemList(); |
| 49 | + |
| 50 | + assertEquals(1, activity.ItemsList.size()); |
| 51 | + HashMap<String, Object> item = activity.ItemsList.get(0); |
| 52 | + assertEquals("ITEM001", item.get("Code")); |
| 53 | + assertEquals("Paracetamol", item.get("Name")); |
| 54 | + assertFalse((Boolean) item.get("isMapped")); |
| 55 | + verify(mockCursor).close(); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void clickingItem_togglesIsMapped() { |
| 60 | + HashMap<String, Object> item = new HashMap<>(); |
| 61 | + item.put("Code", "ITEM001"); |
| 62 | + item.put("Name", "Test"); |
| 63 | + item.put("isMapped", false); |
| 64 | + activity.ItemsList.add(item); |
| 65 | + |
| 66 | + activity.alAdapter = activity.new ItemAdapter(activity, activity.ItemsList, |
| 67 | + R.layout.mappinglist, |
| 68 | + new String[]{"Code", "Name", "isMapped"}, |
| 69 | + new int[]{R.id.tvMapCode,R.id.tvMapName,R.id.chkMap}); |
| 70 | + |
| 71 | + activity.lvMapItems.setAdapter(activity.alAdapter); |
| 72 | + |
| 73 | + activity.lvMapItems.performItemClick(null, 0, 0); |
| 74 | + |
| 75 | + assertTrue((Boolean) activity.ItemsList.get(0).get("isMapped")); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void checkAll_setsAllItemsMapped() { |
| 80 | + for (int i = 0; i < 3; i++) { |
| 81 | + HashMap<String, Object> item = new HashMap<>(); |
| 82 | + item.put("Code", "ITEM00" + i); |
| 83 | + item.put("Name", "Item " + i); |
| 84 | + item.put("isMapped", false); |
| 85 | + activity.ItemsList.add(item); |
| 86 | + } |
| 87 | + |
| 88 | + activity.alAdapter = activity.new ItemAdapter(activity, activity.ItemsList, |
| 89 | + R.layout.mappinglist, |
| 90 | + new String[]{"Code", "Name", "isMapped"}, |
| 91 | + new int[]{R.id.tvMapCode,R.id.tvMapName,R.id.chkMap}); |
| 92 | + |
| 93 | + activity.CheckUncheckAll(true); |
| 94 | + |
| 95 | + for (HashMap<String, Object> item : activity.ItemsList) { |
| 96 | + assertTrue((Boolean)item.get("isMapped")); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void save_returnsCorrectCodes() { |
| 102 | + HashMap<String,Object> item = new HashMap<>(); |
| 103 | + item.put("Code","ITEM001"); |
| 104 | + item.put("Name","Test"); |
| 105 | + item.put("isMapped",false); |
| 106 | + activity.ItemsList.add(item); |
| 107 | + |
| 108 | + when(mockSqlHandler.InsertMapping(anyString(), anyString(), anyString())).thenReturn(true); |
| 109 | + |
| 110 | + int result = activity.Save(); |
| 111 | + assertEquals(1, result); |
| 112 | + |
| 113 | + item.put("isMapped", true); |
| 114 | + result = activity.Save(); |
| 115 | + assertEquals(0, result); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void save_returns2_whenInsertFails() { |
| 120 | + HashMap<String,Object> item = new HashMap<>(); |
| 121 | + item.put("Code","ITEM001"); |
| 122 | + item.put("Name","Test"); |
| 123 | + item.put("isMapped", true); |
| 124 | + activity.ItemsList.add(item); |
| 125 | + |
| 126 | + when(mockSqlHandler.InsertMapping(anyString(), anyString(), anyString())).thenReturn(false); |
| 127 | + doNothing().when(mockSqlHandler).ClearMapping(anyString()); |
| 128 | + |
| 129 | + |
| 130 | + int result = activity.Save(); |
| 131 | + assertEquals(2, result); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void filter_searchWorksCorrectly() { |
| 136 | + HashMap<String,Object> item1 = new HashMap<>(); |
| 137 | + item1.put("Code","ITEM001"); |
| 138 | + item1.put("Name","Paracetamol"); |
| 139 | + item1.put("isMapped", false); |
| 140 | + activity.ItemsList.add(item1); |
| 141 | + |
| 142 | + HashMap<String,Object> item2 = new HashMap<>(); |
| 143 | + item2.put("Code","ITEM002"); |
| 144 | + item2.put("Name","Aspirin"); |
| 145 | + item2.put("isMapped", false); |
| 146 | + activity.ItemsList.add(item2); |
| 147 | + |
| 148 | + activity.alAdapter = activity.new ItemAdapter(activity, activity.ItemsList, |
| 149 | + R.layout.mappinglist, |
| 150 | + new String[]{"Code","Name","isMapped"}, |
| 151 | + new int[]{R.id.tvMapCode,R.id.tvMapName,R.id.chkMap}); |
| 152 | + |
| 153 | + activity.alAdapter.getFilter().filter("para"); |
| 154 | + |
| 155 | + assertEquals(1, activity.ItemsList.size()); |
| 156 | + assertEquals("Paracetamol", activity.ItemsList.get(0).get("Name")); |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + public void adapter_getView_setsCorrectValues() { |
| 161 | + HashMap<String,Object> item = new HashMap<>(); |
| 162 | + item.put("Code","ITEM001"); |
| 163 | + item.put("Name","Test"); |
| 164 | + item.put("isMapped", true); |
| 165 | + activity.ItemsList.add(item); |
| 166 | + |
| 167 | + MapItems.ItemAdapter adapter = activity.new ItemAdapter(activity, activity.ItemsList, |
| 168 | + R.layout.mappinglist, |
| 169 | + new String[]{"Code","Name","isMapped"}, |
| 170 | + new int[]{R.id.tvMapCode,R.id.tvMapName,R.id.chkMap}); |
| 171 | + |
| 172 | + assertNotNull(adapter.getView(0, null, null)); |
| 173 | + } |
| 174 | + |
| 175 | + @Test |
| 176 | + public void showDialog_createsAlertDialog() { |
| 177 | + AlertDialog dialog = activity.ShowDialog("Test message"); |
| 178 | + assertNotNull(dialog); |
| 179 | + } |
| 180 | +} |
0 commit comments