|
| 1 | +package com.codename1.location; |
| 2 | + |
| 3 | +import com.codename1.io.Storage; |
| 4 | +import com.codename1.test.UITestBase; |
| 5 | +import org.junit.jupiter.api.AfterEach; |
| 6 | +import org.junit.jupiter.api.BeforeEach; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +import java.lang.reflect.Field; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.HashMap; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Map; |
| 14 | + |
| 15 | +import static org.junit.jupiter.api.Assertions.*; |
| 16 | +import static org.mockito.Mockito.when; |
| 17 | + |
| 18 | +class GeofenceManagerTest extends UITestBase { |
| 19 | + private Storage originalStorage; |
| 20 | + private InMemoryStorage storage; |
| 21 | + private RecordingLocationManager locationManager; |
| 22 | + private GeofenceManager manager; |
| 23 | + |
| 24 | + @BeforeEach |
| 25 | + void setupManager() throws Exception { |
| 26 | + originalStorage = Storage.getInstance(); |
| 27 | + storage = new InMemoryStorage(); |
| 28 | + Storage.setStorageInstance(storage); |
| 29 | + locationManager = new RecordingLocationManager(); |
| 30 | + Location origin = new Location(0.0, 0.0); |
| 31 | + locationManager.setCurrentLocation(origin); |
| 32 | + locationManager.setLastLocation(origin); |
| 33 | + when(implementation.getLocationManager()).thenReturn(locationManager); |
| 34 | + resetSingleton(); |
| 35 | + manager = GeofenceManager.getInstance(); |
| 36 | + locationManager.clearRecords(); |
| 37 | + } |
| 38 | + |
| 39 | + @AfterEach |
| 40 | + void tearDownManager() throws Exception { |
| 41 | + if (manager != null) { |
| 42 | + manager.clear(); |
| 43 | + } |
| 44 | + Storage.setStorageInstance(originalStorage); |
| 45 | + resetSingleton(); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void addStoresGeofencesAndUpdatesSize() { |
| 50 | + Geofence first = createGeofence("first", 0.001, 0.0, 120, -1L); |
| 51 | + Geofence second = createGeofence("second", 0.002, 0.0, 80, -1L); |
| 52 | + |
| 53 | + manager.add(first, second); |
| 54 | + |
| 55 | + assertEquals(2, manager.size()); |
| 56 | + assertTrue(manager.asMap().containsKey("first")); |
| 57 | + assertTrue(manager.asMap().containsKey("second")); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void removeAndClearDeleteTrackedGeofences() { |
| 62 | + Geofence first = createGeofence("one", 0.0, 0.001, 100, -1L); |
| 63 | + Geofence second = createGeofence("two", 0.0, 0.002, 100, -1L); |
| 64 | + manager.add(first, second); |
| 65 | + |
| 66 | + manager.remove("one"); |
| 67 | + assertEquals(1, manager.size()); |
| 68 | + assertFalse(manager.asMap().containsKey("one")); |
| 69 | + |
| 70 | + manager.clear(); |
| 71 | + assertEquals(0, manager.size()); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void updateActivatesGeofencesWithinBubble() { |
| 76 | + Geofence near = createGeofence("near", 0.001, 0.001, 50, -1L); |
| 77 | + Geofence far = createGeofence("far", 2.0, 2.0, 100, -1L); |
| 78 | + manager.add(near, far); |
| 79 | + locationManager.clearRecords(); |
| 80 | + |
| 81 | + manager.update(1000); |
| 82 | + |
| 83 | + assertTrue(locationManager.addedIds.contains("near")); |
| 84 | + assertTrue(manager.isCurrentlyActive("near")); |
| 85 | + assertFalse(manager.isCurrentlyActive("far")); |
| 86 | + assertFalse(locationManager.removedIds.contains("near")); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void updateWithNullLocationRegistersBackgroundListener() { |
| 91 | + locationManager.setCurrentLocation(null); |
| 92 | + locationManager.clearRecords(); |
| 93 | + |
| 94 | + manager.update(5000); |
| 95 | + |
| 96 | + assertEquals(GeofenceManager.Listener.class, locationManager.lastBackgroundListener); |
| 97 | + assertTrue(locationManager.backgroundBound); |
| 98 | + assertTrue(locationManager.addedIds.isEmpty()); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + void listenerClassPersistsAndClears() { |
| 103 | + manager.setListenerClass(TestGeofenceListener.class); |
| 104 | + assertSame(TestGeofenceListener.class, manager.getListenerClass()); |
| 105 | + |
| 106 | + manager.setListenerClass(null); |
| 107 | + assertNull(manager.getListenerClass()); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + void asSortedListOrdersByProximity() { |
| 112 | + Location reference = new Location(0.0, 0.0); |
| 113 | + locationManager.setLastLocation(reference); |
| 114 | + Geofence close = createGeofence("close", 0.001, 0.0, 30, -1L); |
| 115 | + Geofence far = createGeofence("farther", 0.01, 0.01, 30, -1L); |
| 116 | + manager.add(close, far); |
| 117 | + |
| 118 | + List<Geofence> sorted = manager.asSortedList(); |
| 119 | + |
| 120 | + assertEquals("close", sorted.get(0).getId()); |
| 121 | + assertEquals(2, sorted.size()); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + void isBubbleRecognizesBubbleId() { |
| 126 | + assertTrue(manager.isBubble("$AsyncGeoStreamer.bubble")); |
| 127 | + assertFalse(manager.isBubble("not-bubble")); |
| 128 | + } |
| 129 | + |
| 130 | + private Geofence createGeofence(String id, double lat, double lng, int radius, long expiration) { |
| 131 | + Location location = new Location(lat, lng); |
| 132 | + return new Geofence(id, location, radius, expiration); |
| 133 | + } |
| 134 | + |
| 135 | + private void resetSingleton() throws Exception { |
| 136 | + Field field = GeofenceManager.class.getDeclaredField("instance"); |
| 137 | + field.setAccessible(true); |
| 138 | + field.set(null, null); |
| 139 | + } |
| 140 | + |
| 141 | + private static class InMemoryStorage extends Storage { |
| 142 | + private final Map<String, Object> values = new HashMap<String, Object>(); |
| 143 | + |
| 144 | + @Override |
| 145 | + public Object readObject(String name) { |
| 146 | + return values.get(name); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public boolean writeObject(String name, Object value) { |
| 151 | + values.put(name, value); |
| 152 | + return true; |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public void deleteStorageFile(String name) { |
| 157 | + values.remove(name); |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public boolean exists(String name) { |
| 162 | + return values.containsKey(name); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + private static class RecordingLocationManager extends LocationManager { |
| 167 | + private Location currentLocation; |
| 168 | + private Location lastLocation; |
| 169 | + private final List<String> addedIds = new ArrayList<String>(); |
| 170 | + private final List<String> removedIds = new ArrayList<String>(); |
| 171 | + private final List<Class> backgroundChanges = new ArrayList<Class>(); |
| 172 | + private Class lastBackgroundListener; |
| 173 | + private boolean backgroundBound; |
| 174 | + private boolean backgroundCleared; |
| 175 | + |
| 176 | + void setCurrentLocation(Location currentLocation) { |
| 177 | + this.currentLocation = currentLocation; |
| 178 | + } |
| 179 | + |
| 180 | + void setLastLocation(Location lastLocation) { |
| 181 | + this.lastLocation = lastLocation; |
| 182 | + } |
| 183 | + |
| 184 | + void clearRecords() { |
| 185 | + addedIds.clear(); |
| 186 | + removedIds.clear(); |
| 187 | + backgroundChanges.clear(); |
| 188 | + backgroundBound = false; |
| 189 | + backgroundCleared = false; |
| 190 | + lastBackgroundListener = null; |
| 191 | + } |
| 192 | + |
| 193 | + @Override |
| 194 | + public Location getCurrentLocation() { |
| 195 | + return currentLocation; |
| 196 | + } |
| 197 | + |
| 198 | + @Override |
| 199 | + public Location getCurrentLocationSync(long timeout) { |
| 200 | + return currentLocation; |
| 201 | + } |
| 202 | + |
| 203 | + @Override |
| 204 | + public Location getLastKnownLocation() { |
| 205 | + return lastLocation; |
| 206 | + } |
| 207 | + |
| 208 | + @Override |
| 209 | + protected void bindListener() { |
| 210 | + } |
| 211 | + |
| 212 | + @Override |
| 213 | + protected void clearListener() { |
| 214 | + } |
| 215 | + |
| 216 | + @Override |
| 217 | + public void addGeoFencing(Class GeofenceListenerClass, Geofence gf) { |
| 218 | + addedIds.add(gf.getId()); |
| 219 | + } |
| 220 | + |
| 221 | + @Override |
| 222 | + public void removeGeoFencing(String id) { |
| 223 | + removedIds.add(id); |
| 224 | + } |
| 225 | + |
| 226 | + @Override |
| 227 | + public void setBackgroundLocationListener(Class locationListener) { |
| 228 | + super.setBackgroundLocationListener(locationListener); |
| 229 | + backgroundChanges.add(locationListener); |
| 230 | + lastBackgroundListener = locationListener; |
| 231 | + } |
| 232 | + |
| 233 | + @Override |
| 234 | + protected void bindBackgroundListener() { |
| 235 | + backgroundBound = true; |
| 236 | + backgroundCleared = false; |
| 237 | + } |
| 238 | + |
| 239 | + @Override |
| 240 | + protected void clearBackgroundListener() { |
| 241 | + backgroundCleared = true; |
| 242 | + backgroundBound = false; |
| 243 | + } |
| 244 | + } |
| 245 | + |
| 246 | + public static class TestGeofenceListener implements GeofenceListener { |
| 247 | + @Override |
| 248 | + public void onExit(String id) { |
| 249 | + } |
| 250 | + |
| 251 | + @Override |
| 252 | + public void onEntered(String id) { |
| 253 | + } |
| 254 | + } |
| 255 | +} |
0 commit comments