@josuelmm/cordova-background-geolocation 4.2.2 → 4.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. package/.npmignore +11 -0
  2. package/CHANGELOG.md +213 -0
  3. package/HISTORY.md +73 -0
  4. package/README.md +45 -74
  5. package/android/CDVBackgroundGeolocation/src/main/java/com/marianhello/bgloc/cordova/ConfigMapper.java +24 -0
  6. package/android/CDVBackgroundGeolocation/src/main/java/com/tenforwardconsulting/bgloc/cordova/BackgroundGeolocationPlugin.java +61 -1
  7. package/android/common/src/main/AndroidManifest.xml +1 -1
  8. package/android/common/src/main/java/com/marianhello/bgloc/BootCompletedReceiver.java +6 -3
  9. package/android/common/src/main/java/com/marianhello/bgloc/Config.java +65 -1
  10. package/android/common/src/main/java/com/marianhello/bgloc/PostLocationTask.java +1 -1
  11. package/android/common/src/main/java/com/marianhello/bgloc/data/BackgroundLocation.java +94 -0
  12. package/android/common/src/main/java/com/marianhello/bgloc/data/ConfigJsonMapper.java +205 -0
  13. package/android/common/src/main/java/com/marianhello/bgloc/data/LocationTemplateFactory.java +6 -0
  14. package/android/common/src/main/java/com/marianhello/bgloc/data/sqlite/SQLiteConfigurationContract.java +5 -1
  15. package/android/common/src/main/java/com/marianhello/bgloc/data/sqlite/SQLiteConfigurationDAO.java +32 -1
  16. package/android/common/src/main/java/com/marianhello/bgloc/data/sqlite/SQLiteLocationContract.java +12 -2
  17. package/android/common/src/main/java/com/marianhello/bgloc/data/sqlite/SQLiteLocationDAO.java +33 -2
  18. package/android/common/src/main/java/com/marianhello/bgloc/data/sqlite/SQLiteOpenHelper.java +15 -1
  19. package/android/common/src/main/java/com/marianhello/bgloc/provider/DistanceFilterLocationProvider.java +23 -8
  20. package/android/common/src/main/java/com/marianhello/bgloc/service/LocationServiceImpl.java +246 -21
  21. package/android/common/src/main/java/com/marianhello/bgloc/service/LocationServiceProxy.java +5 -2
  22. package/android/common/src/main/java/com/marianhello/bgloc/sync/BatchManager.java +46 -13
  23. package/ios/CDVBackgroundGeolocation/CDVBackgroundGeolocation.m +23 -1
  24. package/ios/common/BackgroundGeolocation/MAURBackgroundGeolocationFacade.m +111 -5
  25. package/ios/common/BackgroundGeolocation/MAURBackgroundSync.m +20 -0
  26. package/ios/common/BackgroundGeolocation/MAURConfig.h +2 -0
  27. package/ios/common/BackgroundGeolocation/MAURConfig.m +16 -2
  28. package/ios/common/BackgroundGeolocation/MAURConfigurationContract.h +3 -0
  29. package/ios/common/BackgroundGeolocation/MAURConfigurationContract.m +3 -1
  30. package/ios/common/BackgroundGeolocation/MAURGeolocationOpenHelper.m +15 -1
  31. package/ios/common/BackgroundGeolocation/MAURLocation.h +12 -0
  32. package/ios/common/BackgroundGeolocation/MAURLocation.m +33 -4
  33. package/ios/common/BackgroundGeolocation/MAURLocationContract.h +4 -0
  34. package/ios/common/BackgroundGeolocation/MAURLocationContract.m +5 -1
  35. package/ios/common/BackgroundGeolocation/MAURPostLocationTask.h +9 -0
  36. package/ios/common/BackgroundGeolocation/MAURPostLocationTask.m +59 -1
  37. package/ios/common/BackgroundGeolocation/MAURSQLiteConfigurationDAO.m +54 -4
  38. package/ios/common/BackgroundGeolocation/MAURSQLiteLocationDAO.h +12 -0
  39. package/ios/common/BackgroundGeolocation/MAURSQLiteLocationDAO.m +125 -5
  40. package/package.json +36 -1
  41. package/plugin.xml +3 -2
  42. package/www/BackgroundGeolocation.d.ts +114 -3
  43. package/www/BackgroundGeolocation.js +11 -4
  44. package/CLAUDE.md +0 -56
  45. package/android/CDVBackgroundGeolocation/src/test/java/com/marianhello/ConfigMapperTest.java +0 -220
  46. package/android/common/src/androidTest/java/com/marianhello/bgloc/BackgroundGeolocationFacadeTest.java +0 -45
  47. package/android/common/src/androidTest/java/com/marianhello/bgloc/BatchManagerTest.java +0 -570
  48. package/android/common/src/androidTest/java/com/marianhello/bgloc/ConfigTest.java +0 -76
  49. package/android/common/src/androidTest/java/com/marianhello/bgloc/ContentProviderLocationDAOTest.java +0 -437
  50. package/android/common/src/androidTest/java/com/marianhello/bgloc/DBLogReaderTest.java +0 -95
  51. package/android/common/src/androidTest/java/com/marianhello/bgloc/LocationContentProviderTest.java +0 -159
  52. package/android/common/src/androidTest/java/com/marianhello/bgloc/LocationServiceProxyTest.java +0 -161
  53. package/android/common/src/androidTest/java/com/marianhello/bgloc/LocationServiceTest.java +0 -247
  54. package/android/common/src/androidTest/java/com/marianhello/bgloc/SQLiteConfigurationDAOTest.java +0 -200
  55. package/android/common/src/androidTest/java/com/marianhello/bgloc/SQLiteLocationDAOTest.java +0 -457
  56. package/android/common/src/androidTest/java/com/marianhello/bgloc/SQLiteLocationDAOThreadTest.java +0 -96
  57. package/android/common/src/androidTest/java/com/marianhello/bgloc/SQLiteOpenHelperTest.java +0 -225
  58. package/android/common/src/androidTest/java/com/marianhello/bgloc/TestPluginDelegate.java +0 -46
  59. package/android/common/src/androidTest/java/com/marianhello/bgloc/TestResourceResolver.java +0 -14
  60. package/android/common/src/androidTest/java/com/marianhello/bgloc/provider/MockLocationProvider.java +0 -50
  61. package/android/common/src/androidTest/java/com/marianhello/bgloc/provider/TestLocationProviderFactory.java +0 -17
  62. package/android/common/src/androidTest/java/com/marianhello/bgloc/sqlite/SQLiteOpenHelper10.java +0 -92
  63. package/android/common/src/androidTest/java/com/marianhello/bgloc/test/LocationProviderTestCase.java +0 -107
  64. package/android/common/src/androidTest/java/com/marianhello/bgloc/test/TestConstants.java +0 -5
  65. package/android/common/src/test/java/com/marianhello/backgroundgeolocation/ArrayListLocationTemplateTest.java +0 -82
  66. package/android/common/src/test/java/com/marianhello/backgroundgeolocation/BackgroundLocationTest.java +0 -128
  67. package/android/common/src/test/java/com/marianhello/backgroundgeolocation/ConfigTest.java +0 -191
  68. package/android/common/src/test/java/com/marianhello/backgroundgeolocation/DBLogReaderTest.java +0 -37
  69. package/android/common/src/test/java/com/marianhello/backgroundgeolocation/HashMapLocationTemplateTest.java +0 -216
  70. package/android/common/src/test/java/com/marianhello/backgroundgeolocation/HttpPostServiceTest.java +0 -223
  71. package/android/common/src/test/java/com/marianhello/backgroundgeolocation/LocationTemplateFactoryTest.java +0 -50
  72. package/android/common/src/test/java/com/marianhello/backgroundgeolocation/PostLocationTaskTest.java +0 -180
  73. package/android/common/src/test/java/com/marianhello/backgroundgeolocation/TestHelper.java +0 -16
  74. package/ios/common/BackgroundGeolocationTests/Info.plist +0 -24
  75. package/ios/common/BackgroundGeolocationTests/MAURBackgroundLocationTest.m +0 -185
  76. package/ios/common/BackgroundGeolocationTests/MAURConfigTest.m +0 -161
  77. package/ios/common/BackgroundGeolocationTests/MAURGeolocationOpenHelperTest.m +0 -102
  78. package/ios/common/BackgroundGeolocationTests/MAURLocationTest.m +0 -216
  79. package/ios/common/BackgroundGeolocationTests/MAURLocationUploaderTest.m +0 -55
  80. package/ios/common/BackgroundGeolocationTests/MAURLogReaderTest.m +0 -43
  81. package/ios/common/BackgroundGeolocationTests/MAURSQLiteConfigurationDAOTest.m +0 -102
  82. package/ios/common/BackgroundGeolocationTests/MAURSQLiteHelperTest.m +0 -41
  83. package/ios/common/BackgroundGeolocationTests/MAURSQLiteLocationDAOTests.m +0 -240
  84. package/ios/common/BackgroundGeolocationTests/MAURSQLiteLocationDAOThreadTest.m +0 -84
  85. package/ios/common/BackgroundGeolocationTests/MAURSQLiteOpenHelperTest.m +0 -144
  86. package/ios/common/scripts/xcode-refactor.js +0 -184
@@ -1,570 +0,0 @@
1
- package com.marianhello.bgloc;
2
-
3
- import android.content.Context;
4
- import android.database.sqlite.SQLiteDatabase;
5
- import androidx.test.platform.app.InstrumentationRegistry;
6
- import androidx.test.ext.junit.runners.AndroidJUnit4;
7
- import androidx.test.filters.SmallTest;
8
- import android.util.JsonReader;
9
- import android.util.JsonToken;
10
-
11
- import com.marianhello.bgloc.data.ArrayListLocationTemplate;
12
- import com.marianhello.bgloc.data.BackgroundLocation;
13
- import com.marianhello.bgloc.data.HashMapLocationTemplate;
14
- import com.marianhello.bgloc.data.LocationTemplate;
15
- import com.marianhello.bgloc.data.LocationTemplateFactory;
16
- import com.marianhello.bgloc.data.sqlite.SQLiteLocationDAO;
17
- import com.marianhello.bgloc.data.sqlite.SQLiteOpenHelper;
18
- import com.marianhello.bgloc.sync.BatchManager;
19
-
20
- import junit.framework.Assert;
21
-
22
- import org.json.JSONArray;
23
- import org.json.JSONException;
24
- import org.json.JSONObject;
25
- import org.junit.Before;
26
- import org.junit.Test;
27
- import org.junit.runner.RunWith;
28
-
29
- import java.io.BufferedReader;
30
- import java.io.File;
31
- import java.io.FileReader;
32
- import java.io.IOException;
33
- import java.util.ArrayList;
34
- import java.util.HashMap;
35
- import java.util.List;
36
-
37
- import static com.marianhello.bgloc.data.sqlite.SQLiteLocationContract.LocationEntry.SQL_DROP_LOCATION_TABLE;
38
-
39
- /**
40
- * Created by finch on 22/07/16.
41
- */
42
- @RunWith(AndroidJUnit4.class)
43
- @SmallTest
44
- public class BatchManagerTest {
45
- Context mContext;
46
- SQLiteOpenHelper mDbHelper;
47
-
48
- public void prepareDatabase() {
49
- SQLiteDatabase db = mDbHelper.getWritableDatabase();
50
- mDbHelper.execAndLogSql(db, SQL_DROP_LOCATION_TABLE);
51
- mDbHelper.onCreate(db);
52
- }
53
-
54
- @Before
55
- public void setUp() {
56
- mContext = InstrumentationRegistry.getTargetContext();
57
- mDbHelper = SQLiteOpenHelper.getHelper(mContext);
58
- prepareDatabase();
59
- }
60
-
61
- private List<BackgroundLocation> readLocationsArray(JsonReader reader) throws IOException {
62
- List<BackgroundLocation> locations = new ArrayList<BackgroundLocation>();
63
- reader.beginArray();
64
- while (reader.hasNext()) {
65
- BackgroundLocation l = new BackgroundLocation();
66
- reader.beginObject();
67
- while (reader.hasNext()) {
68
- String name = reader.nextName();
69
- if (name.equals("id")) {
70
- l.setLocationId(reader.nextLong());
71
- } else if (name.equals("time")) {
72
- l.setTime(reader.nextLong());
73
- } else if (name.equals("latitude")) {
74
- l.setLatitude(reader.nextDouble());
75
- } else if (name.equals("longitude")) {
76
- l.setLongitude(reader.nextDouble());
77
- } else if (name.equals("accuracy")) {
78
- l.setAccuracy((float)reader.nextDouble());
79
- } else if (name.equals("speed")) {
80
- l.setSpeed((float)reader.nextDouble());
81
- } else if (name.equals("bearing")) {
82
- l.setBearing((float)reader.nextDouble());
83
- } else if (name.equals("altitude")) {
84
- l.setAltitude(reader.nextDouble());
85
- } else if (name.equals("radius")) {
86
- JsonToken token = reader.peek();
87
- if (token != JsonToken.NULL) {
88
- l.setRadius((float)reader.nextDouble());
89
- } else {
90
- reader.skipValue();
91
- }
92
- } else if (name.equals("provider")) {
93
- l.setProvider(reader.nextString());
94
- } else if (name.equals("locationProvider")) {
95
- l.setLocationProvider(reader.nextInt());
96
- } else {
97
- reader.skipValue();
98
- }
99
- }
100
- reader.endObject();
101
- locations.add(l);
102
- }
103
- reader.endArray();
104
- return locations;
105
- }
106
-
107
- @Test
108
- public void testCreateBatch() {
109
- SQLiteLocationDAO dao = new SQLiteLocationDAO(mDbHelper.getWritableDatabase());
110
-
111
- int i = 1;
112
- for (int j = i; j < 100; j++) {
113
- BackgroundLocation location = new BackgroundLocation();
114
- location.setTime(1000 + i);
115
- location.setLatitude(40.21 + i);
116
- location.setLongitude(23.45 + i);
117
- location.setAccuracy(200 +i);
118
- location.setAltitude(900 +i);
119
- location.setSpeed(20 + i);
120
- location.setBearing(2 + i);
121
- location.setProvider("test");
122
- location.setLocationProvider(1);
123
- if ((j % 3) == 0) {
124
- // exactly 33 locations (out of 99) should be eligible for sync for given batch id 1000
125
- location.setBatchStartMillis(1000L);
126
- } else if ((j % 2) == 0) {
127
- // exactly 33 locations as deleted
128
- location.setStatus(BackgroundLocation.DELETED);
129
- } else {
130
- location.setStatus(BackgroundLocation.SYNC_PENDING);
131
- i++;
132
- }
133
- dao.persistLocation(location);
134
- }
135
-
136
- List<BackgroundLocation> locations = null;
137
- BatchManager batchManager = new BatchManager(mContext);
138
- try {
139
- File batchFile = batchManager.createBatch(1000L, 0);
140
- JsonReader reader = new JsonReader(new FileReader(batchFile));
141
- locations = readLocationsArray(reader);
142
- } catch (Exception e) {
143
- Assert.fail(e.getMessage());
144
- }
145
-
146
- Assert.assertEquals(33, locations.size());
147
- i = 1;
148
- for (BackgroundLocation l : locations) {
149
- Assert.assertEquals(200 + i, l.getAccuracy(), 0);
150
- Assert.assertEquals(900 + i, l.getAltitude(), 0);
151
- Assert.assertEquals(2 + i, l.getBearing(), 0);
152
- Assert.assertEquals(40.21 + i, l.getLatitude(), 0);
153
- Assert.assertEquals(23.45 + i,l.getLongitude(), 0);
154
- Assert.assertEquals(20 + i, l.getSpeed(), 0);
155
- Assert.assertEquals("test", l.getProvider());
156
- Assert.assertEquals(1000 + i, l.getTime(), 0);
157
- i++;
158
- }
159
- }
160
-
161
- @Test
162
- public void testCreateBatchWithArrayListTemplate() {
163
- SQLiteLocationDAO dao = new SQLiteLocationDAO(mDbHelper.getWritableDatabase());
164
-
165
- for (int i = 1; i < 3; i++) {
166
- BackgroundLocation location = new BackgroundLocation();
167
- location.setTime(1000 * i);
168
- location.setLatitude(30.21 + i);
169
- location.setLongitude(13.45 + i);
170
- location.setBatchStartMillis(1000L);
171
- location.setIsFromMockProvider(false);
172
- location.setMockLocationsEnabled(true);
173
- location.setStatus(BackgroundLocation.SYNC_PENDING);
174
- dao.persistLocation(location);
175
- }
176
-
177
-
178
- ArrayList list = new ArrayList();
179
- list.add("@latitude");
180
- list.add("@longitude");
181
- list.add("foo");
182
- list.add("bar");
183
- list.add("@isFromMockProvider");
184
- list.add("@mockLocationsEnabled");
185
-
186
- LocationTemplate template = new ArrayListLocationTemplate(list);
187
-
188
- ArrayList<HashMap> locations = new ArrayList();
189
- BatchManager batchManager = new BatchManager(mContext);
190
-
191
- try {
192
- File batchFile = batchManager.createBatch(3000L, 0, template);
193
- JsonReader reader = new JsonReader(new FileReader(batchFile));
194
-
195
- reader.beginArray();
196
- while (reader.hasNext()) {
197
- HashMap hashLocation = new HashMap<String, Object>();
198
- reader.beginArray();
199
- hashLocation.put("latitude", reader.nextDouble());
200
- hashLocation.put("longitude", reader.nextDouble());
201
- hashLocation.put("foo", reader.nextString());
202
- hashLocation.put("bar", reader.nextString());
203
- hashLocation.put("isFromMockProvider", reader.nextBoolean());
204
- hashLocation.put("mockLocationsEnabled", reader.nextBoolean());
205
- reader.endArray();
206
- locations.add(hashLocation);
207
- }
208
- reader.endArray();
209
- } catch (Exception e) {
210
- Assert.fail(e.getMessage());
211
- }
212
-
213
- Assert.assertEquals(2, locations.size());
214
- int i = 1;
215
- for (HashMap l : locations) {
216
- Assert.assertEquals(30.21 + i, (Double) l.get("latitude"), 0);
217
- Assert.assertEquals(13.45 + i, (Double) l.get("longitude"), 0);
218
- Assert.assertEquals("foo", (String) l.get("foo"));
219
- Assert.assertEquals("bar", (String) l.get("bar"));
220
- Assert.assertEquals(false, l.get("isFromMockProvider"));
221
- Assert.assertEquals(true, l.get("mockLocationsEnabled"));
222
- i++;
223
- }
224
- }
225
-
226
- @Test
227
- public void testCreateBatchWithMapHashTemplate() {
228
- SQLiteLocationDAO dao = new SQLiteLocationDAO(mDbHelper.getWritableDatabase());
229
-
230
- for (int i = 1; i < 3; i++) {
231
- BackgroundLocation location = new BackgroundLocation();
232
- location.setTime(1000 * i);
233
- location.setLatitude(30.21 + i);
234
- location.setLongitude(13.45 + i);
235
- location.setBatchStartMillis(1000L);
236
- location.setIsFromMockProvider(true);
237
- location.setMockLocationsEnabled(false);
238
- location.setStatus(BackgroundLocation.SYNC_PENDING);
239
- dao.persistLocation(location);
240
- }
241
-
242
- HashMap map = new HashMap<String, String>();
243
- map.put("lat", "@latitude");
244
- map.put("lon", "@longitude");
245
- map.put("foo", "bar");
246
- map.put("pretzels", 123);
247
- map.put("isFromMockProvider", "@isFromMockProvider");
248
- map.put("mockLocationsEnabled", "@mockLocationsEnabled");
249
- LocationTemplate template = new HashMapLocationTemplate(map);
250
-
251
- ArrayList<HashMap> locations = new ArrayList();
252
- BatchManager batchManager = new BatchManager(mContext);
253
-
254
- try {
255
- File batchFile = batchManager.createBatch(3000L, 0, template);
256
- JsonReader reader = new JsonReader(new FileReader(batchFile));
257
-
258
- reader.beginArray();
259
- while (reader.hasNext()) {
260
- HashMap hashLocation = new HashMap<String, Object>();
261
- reader.beginObject();
262
- while (reader.hasNext()) {
263
- String name = reader.nextName();
264
- if ("lat".equals(name)) {
265
- hashLocation.put(name, reader.nextDouble());
266
- } else if ("lon".equals(name)) {
267
- hashLocation.put(name, reader.nextDouble());
268
- } else if ("foo".equals(name)) {
269
- hashLocation.put(name, reader.nextString());
270
- } else if ("isFromMockProvider".equals(name)) {
271
- hashLocation.put(name, reader.nextBoolean());
272
- } else if ("mockLocationsEnabled".equals(name)) {
273
- hashLocation.put(name, reader.nextBoolean());
274
- } else {
275
- reader.skipValue();
276
- }
277
- }
278
- reader.endObject();
279
- locations.add(hashLocation);
280
- }
281
- reader.endArray();
282
- } catch (Exception e) {
283
- Assert.fail(e.getMessage());
284
- }
285
-
286
- Assert.assertEquals(2, locations.size());
287
- int i = 1;
288
- for (HashMap l : locations) {
289
- Assert.assertEquals(30.21 + i, (Double) l.get("lat"), 0);
290
- Assert.assertEquals(13.45 + i, (Double) l.get("lon"), 0);
291
- Assert.assertEquals("bar", (String) l.get("foo"));
292
- Assert.assertEquals(true, l.get("isFromMockProvider"));
293
- Assert.assertEquals(false, l.get("mockLocationsEnabled"));
294
- i++;
295
- }
296
- }
297
-
298
- @Test
299
- public void testSetBatchCompleted() {
300
- SQLiteLocationDAO dao = new SQLiteLocationDAO(mDbHelper.getWritableDatabase());
301
-
302
- int i = 1;
303
- BackgroundLocation location;
304
- for (int j = i; j < 100; j++) {
305
- location = new BackgroundLocation();
306
- location.setProvider("test");
307
- location.setTime(1000 + i);
308
- location.setLatitude(40.21 + i);
309
- location.setLongitude(23.45 + i);
310
- location.setAccuracy(200 +i);
311
- location.setAltitude(900 +i);
312
- location.setSpeed(20 + i);
313
- location.setBearing(2 + i);
314
- location.setProvider("test");
315
- location.setLocationProvider(1);
316
- if ((j % 99) == 0) {
317
- location.setBatchStartMillis(1000L);
318
- } else if ((j % 2) == 0) {
319
- location.setStatus(BackgroundLocation.DELETED);
320
- } else {
321
- location.setStatus(BackgroundLocation.SYNC_PENDING);
322
- i++;
323
- }
324
- dao.persistLocation(location);
325
- }
326
-
327
- // distribution of location: 49 of invalid, (49 + 1 in sync) of valid, 1 in sync
328
-
329
- Assert.assertEquals(99, dao.getAllLocations().size());
330
- Assert.assertEquals(50, dao.getValidLocations().size());
331
- Assert.assertEquals(49, dao.getLocationsForSyncCount(1000L));
332
- BatchManager batchManager = new BatchManager(mContext);
333
- try {
334
- batchManager.createBatch(1000L, 0);
335
- batchManager.setBatchCompleted(1000L);
336
- Assert.assertEquals(0, dao.getValidLocations().size());
337
- Assert.assertEquals(0, dao.getLocationsForSyncCount(2000L));
338
- } catch (Exception e) {
339
- Assert.fail(e.getMessage());
340
- }
341
- }
342
-
343
- @Test
344
- public void testCreateBatchWithNestedTemplate() throws JSONException, IOException {
345
- SQLiteLocationDAO dao = new SQLiteLocationDAO(mDbHelper.getWritableDatabase());
346
- ArrayList<BackgroundLocation> testLocations = new ArrayList<BackgroundLocation>();
347
-
348
- for (int i = 0; i < 3; i++) {
349
- BackgroundLocation location = new BackgroundLocation();
350
- location.setProvider("test");
351
- location.setTime(1000 * i);
352
- location.setAltitude(999 + i);
353
- location.setLatitude(30.21 + i);
354
- location.setLongitude(13.45 + i);
355
- location.setAccuracy(9);
356
- location.setSpeed(66);
357
- location.setBearing(99);
358
- location.setBatchStartMillis(1000L);
359
- location.setIsFromMockProvider(true);
360
- location.setMockLocationsEnabled(false);
361
- location.setStatus(BackgroundLocation.SYNC_PENDING);
362
- dao.persistLocation(location);
363
- testLocations.add(location);
364
- }
365
-
366
- JSONObject templateJSON = new JSONObject(
367
- "{\"data\":{\"Id-Number\":\"@id\"," +
368
- "\"Provider-String\":\"@provider\"," +
369
- "\"Time-Number\":\"@time\"," +
370
- "\"Altitude-Number\":\"@altitude\"," +
371
- "\"Latitude-Number\":\"@latitude\"," +
372
- "\"Longitude-Number\":\"@longitude\"," +
373
- "\"Foo-String\":\"bar\"," +
374
- "\"LocationProvider-Number\":\"@locationProvider\"," +
375
- "\"Accuracy-Number\":\"@accuracy\"," +
376
- "\"Speed-Number\":\"@speed\"," +
377
- "\"Bearing-Number\":\"@bearing\"," +
378
- "\"FooNumber-Number\":111}" +
379
- "}"
380
- );
381
- LocationTemplate template = LocationTemplateFactory.fromJSON(templateJSON);
382
-
383
- BatchManager batchManager = new BatchManager(mContext);
384
- File batchFile = batchManager.createBatch(3000L, 0, template);
385
-
386
- ArrayList<HashMap<String, Object>> hashLocations = new ArrayList();
387
- JsonReader reader = new JsonReader(new FileReader(batchFile));
388
- reader.beginArray();
389
- while (reader.hasNext()) {
390
- reader.beginObject();
391
- if (reader.hasNext() && "data".equals(reader.nextName())) {
392
- HashMap hashLocation = new HashMap<String, Object>();
393
- reader.beginObject();
394
- while (reader.hasNext()) {
395
- String name = reader.nextName();
396
- if (name.endsWith("-String")) {
397
- hashLocation.put(name, reader.nextString());
398
- } else if (name.endsWith("-Number")) {
399
- hashLocation.put(name, reader.nextDouble());
400
- } else if (name.endsWith("-Boolean")) {
401
- hashLocation.put(name, reader.nextBoolean());
402
- } else {
403
- reader.skipValue();
404
- }
405
- }
406
- reader.endObject();
407
- hashLocations.add(hashLocation);
408
- }
409
- reader.endObject();
410
- }
411
- reader.endArray();
412
-
413
- Assert.assertEquals(3, hashLocations.size());
414
- int i = 0;
415
- for (HashMap l : hashLocations) {
416
- Assert.assertEquals("test", l.get("Provider-String"));
417
- Assert.assertEquals(0, (Double) l.get("LocationProvider-Number"), 0);
418
- Assert.assertEquals(9, (Double) l.get("Accuracy-Number"), 0);
419
- Assert.assertEquals(99, (Double) l.get("Bearing-Number"), 0);
420
- Assert.assertEquals(66, (Double) l.get("Speed-Number"), 0);
421
- Assert.assertEquals(testLocations.get(i).getAltitude(), (Double) l.get("Altitude-Number"), 0);
422
- Assert.assertEquals(testLocations.get(i).getLatitude(), (Double) l.get("Latitude-Number"), 0);
423
- Assert.assertEquals(testLocations.get(i).getLongitude(), (Double) l.get("Longitude-Number"), 0);
424
- Assert.assertEquals(testLocations.get(i).getTime(), (Double) l.get("Time-Number"), 0);
425
- Assert.assertEquals("bar", l.get("Foo-String"));
426
- Assert.assertEquals(111, (Double) l.get("FooNumber-Number"), 0);
427
- i++;
428
- }
429
- }
430
-
431
- @Test
432
- public void testCreateBatchWithNestedListTemplate() throws JSONException, IOException {
433
- SQLiteLocationDAO dao = new SQLiteLocationDAO(mDbHelper.getWritableDatabase());
434
- ArrayList<BackgroundLocation> testLocations = new ArrayList<BackgroundLocation>();
435
-
436
- for (int i = 0; i < 3; i++) {
437
- BackgroundLocation location = new BackgroundLocation();
438
- location.setProvider("test");
439
- location.setTime(1000 * i);
440
- location.setAltitude(999 + i);
441
- location.setLatitude(30.21 + i);
442
- location.setLongitude(13.45 + i);
443
- location.setAccuracy(9);
444
- location.setSpeed(66);
445
- location.setBearing(99);
446
- location.setBatchStartMillis(1000L);
447
- location.setIsFromMockProvider(true);
448
- location.setMockLocationsEnabled(false);
449
- location.setStatus(BackgroundLocation.SYNC_PENDING);
450
- dao.persistLocation(location);
451
- testLocations.add(location);
452
- }
453
-
454
- JSONArray templateJSON = new JSONArray(
455
- "[\"@id\"," +
456
- "{" +
457
- "\"Provider-String\":\"@provider\"," +
458
- "\"Time-Number\":\"@time\"," +
459
- "\"Altitude-Number\":\"@altitude\"," +
460
- "\"Latitude-Number\":\"@latitude\"," +
461
- "\"Longitude-Number\":\"@longitude\"," +
462
- "\"Foo-String\":\"bar\"," +
463
- "\"LocationProvider-Number\":\"@locationProvider\"," +
464
- "\"Accuracy-Number\":\"@accuracy\"," +
465
- "\"Speed-Number\":\"@speed\"," +
466
- "\"Bearing-Number\":\"@bearing\"," +
467
- "\"FooNumber-Number\":111" +
468
- "}]"
469
- );
470
- LocationTemplate template = LocationTemplateFactory.fromJSON(templateJSON);
471
- BatchManager batchManager = new BatchManager(mContext);
472
- File batchFile = batchManager.createBatch(3000L, 0, template);
473
-
474
- ArrayList<HashMap<String, Object>> hashLocations = new ArrayList();
475
- JsonReader reader = new JsonReader(new FileReader(batchFile));
476
- reader.beginArray();
477
- while (reader.hasNext()) {
478
- reader.beginArray();
479
- while(reader.hasNext()) {
480
- HashMap hashLocation = new HashMap<String, Object>();
481
- hashLocation.put("id", reader.nextInt());
482
- reader.beginObject();
483
- while(reader.hasNext()) {
484
- String name = reader.nextName();
485
- if (name.endsWith("-String")) {
486
- hashLocation.put(name, reader.nextString());
487
- } else if (name.endsWith("-Number")) {
488
- hashLocation.put(name, reader.nextDouble());
489
- } else if (name.endsWith("-Boolean")) {
490
- hashLocation.put(name, reader.nextBoolean());
491
- } else {
492
- reader.skipValue();
493
- }
494
- }
495
- reader.endObject();
496
- hashLocations.add(hashLocation);
497
- }
498
- reader.endArray();
499
- }
500
-
501
- Assert.assertEquals(3, hashLocations.size());
502
- int i = 0;
503
- for (HashMap l : hashLocations) {
504
- Assert.assertEquals(i+1, l.get("id"));
505
- Assert.assertEquals("test", l.get("Provider-String"));
506
- Assert.assertEquals(0, (Double) l.get("LocationProvider-Number"), 0);
507
- Assert.assertEquals(9, (Double) l.get("Accuracy-Number"), 0);
508
- Assert.assertEquals(99, (Double) l.get("Bearing-Number"), 0);
509
- Assert.assertEquals(66, (Double) l.get("Speed-Number"), 0);
510
- Assert.assertEquals(testLocations.get(i).getAltitude(), (Double) l.get("Altitude-Number"), 0);
511
- Assert.assertEquals(testLocations.get(i).getLatitude(), (Double) l.get("Latitude-Number"), 0);
512
- Assert.assertEquals(testLocations.get(i).getLongitude(), (Double) l.get("Longitude-Number"), 0);
513
- Assert.assertEquals(testLocations.get(i).getTime(), (Double) l.get("Time-Number"), 0);
514
- Assert.assertEquals("bar", l.get("Foo-String"));
515
- Assert.assertEquals(111, (Double) l.get("FooNumber-Number"), 0);
516
- i++;
517
- }
518
-
519
- }
520
-
521
- @Test
522
- public void testBatchWithNulls() throws JSONException, IOException {
523
-
524
- BackgroundLocation location = new BackgroundLocation();
525
- location.setBatchStartMillis(1000L);
526
- location.setStatus(BackgroundLocation.SYNC_PENDING);
527
- SQLiteLocationDAO dao = new SQLiteLocationDAO(mDbHelper.getWritableDatabase());
528
- dao.persistLocation(location);
529
-
530
- JSONObject templateJSON = new JSONObject("{\"Nullable\":null, \"NullRadius\": \"@radius\"}");
531
- LocationTemplate template = LocationTemplateFactory.fromJSON(templateJSON);
532
-
533
- BatchManager batchManager = new BatchManager(mContext);
534
- File batchFile = batchManager.createBatch(3000L, 0, template);
535
-
536
- HashMap hashLocation = new HashMap<String, Object>();
537
- JsonReader reader = new JsonReader(new FileReader(batchFile));
538
- reader.beginArray();
539
- while (reader.hasNext()) {
540
- reader.beginObject();
541
- while(reader.hasNext()) { ;
542
- hashLocation.put(reader.nextName(), null);
543
- reader.nextNull();
544
- }
545
- reader.endObject();
546
- }
547
- reader.endArray();
548
-
549
- Assert.assertTrue(hashLocation.containsKey("Nullable"));
550
- Assert.assertTrue(hashLocation.containsKey("NullRadius"));
551
- }
552
-
553
- public static String slurp (final File file) throws IOException {
554
- StringBuilder result = new StringBuilder();
555
- BufferedReader reader = null;
556
- try {
557
- reader = new BufferedReader(new FileReader(file));
558
- char[] buf = new char[1024];
559
- int r = 0;
560
- while ((r = reader.read(buf)) != -1) {
561
- result.append(buf, 0, r);
562
- }
563
- }
564
- finally {
565
- reader.close();
566
- }
567
-
568
- return result.toString();
569
- }
570
- }
@@ -1,76 +0,0 @@
1
- package com.marianhello.bgloc;
2
-
3
- import android.os.Parcel;
4
-
5
- import com.marianhello.bgloc.Config;
6
- import com.marianhello.bgloc.data.LocationTemplateFactory;
7
-
8
- import junit.framework.Assert;
9
-
10
- import org.junit.Test;
11
-
12
- import java.util.HashMap;
13
- import java.util.LinkedHashSet;
14
-
15
- /**
16
- * Created by finch on 9.12.2017.
17
- */
18
-
19
- public class ConfigTest {
20
- @Test
21
- public void testParcelable() {
22
- Config config = Config.getDefault();
23
-
24
- Parcel configParcel = config.toParcel();
25
- Config configFromParcel = Config.fromByteArray(configParcel.marshall());
26
-
27
- Assert.assertEquals(config.getStationaryRadius(), configFromParcel.getStationaryRadius());
28
- Assert.assertEquals(config.getDistanceFilter(), configFromParcel.getDistanceFilter());
29
- Assert.assertEquals(config.getDesiredAccuracy(), configFromParcel.getDesiredAccuracy());
30
- Assert.assertFalse(config.isDebugging());
31
- Assert.assertEquals(config.getNotificationTitle(), configFromParcel.getNotificationTitle());
32
- Assert.assertEquals(config.getNotificationText(), configFromParcel.getNotificationText());
33
- Assert.assertTrue(config.getStopOnTerminate());
34
- Assert.assertFalse(config.getStartOnBoot());
35
- Assert.assertEquals(config.getLocationProvider(), configFromParcel.getLocationProvider());
36
- Assert.assertEquals(config.getInterval(), configFromParcel.getInterval());
37
- Assert.assertEquals(config.getFastestInterval(), configFromParcel.getFastestInterval());
38
- Assert.assertEquals(config.getActivitiesInterval(), configFromParcel.getActivitiesInterval());
39
- Assert.assertEquals(config.getNotificationIconColor(), configFromParcel.getNotificationIconColor());
40
- Assert.assertEquals(config.getLargeNotificationIcon(), configFromParcel.getLargeNotificationIcon());
41
- Assert.assertEquals(config.getSmallNotificationIcon(), configFromParcel.getSmallNotificationIcon());
42
- Assert.assertTrue(config.getStartForeground());
43
- Assert.assertTrue(config.getStopOnStillActivity());
44
- Assert.assertEquals(config.getUrl(), configFromParcel.getUrl());
45
- Assert.assertEquals(config.getSyncUrl(), configFromParcel.getSyncUrl());
46
- Assert.assertEquals(config.getSyncThreshold(), configFromParcel.getSyncThreshold());
47
- Assert.assertTrue(config.getHttpHeaders().isEmpty());
48
- Assert.assertEquals(config.getMaxLocations(), configFromParcel.getMaxLocations());
49
- }
50
-
51
- @Test
52
- public void testTemplateParcelableFromHashMap() {
53
- Config config = Config.getDefault();
54
-
55
- HashMap map = new HashMap<String, String>();
56
- map.put("@id", "id");
57
- map.put("@provider", "provider");
58
- map.put("@time", "time");
59
- map.put("@altitude", "altitude");
60
- map.put("@latitude", "latitude");
61
- map.put("@longitude", "longitude");
62
- map.put("@foo", "foo");
63
- map.put("@locationProvider", "locationProvider");
64
- map.put("@accuracy", "accuracy");
65
- map.put("@speed", "speed");
66
- map.put("@bearing", "bearing");
67
-
68
- config.setTemplate(LocationTemplateFactory.fromHashMap(map));
69
-
70
- Parcel configParcel = config.toParcel();
71
- Config configFromParcel = Config.fromByteArray(configParcel.marshall());
72
-
73
- Assert.assertEquals(config.getTemplate(), configFromParcel.getTemplate());
74
- }
75
-
76
- }