@capgo/background-geolocation 7.1.1 → 7.2.0

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.
package/README.md CHANGED
@@ -106,6 +106,10 @@ function guessLocation(callback, timeout) {
106
106
  }
107
107
  ```
108
108
 
109
+ ## Documentation
110
+
111
+ The most complete doc is available here: https://capgo.app/docs/plugins/background-geolocation/
112
+
109
113
  ## Installation
110
114
 
111
115
  This plugin supports Capacitor v7:
@@ -36,7 +36,7 @@ func formatLocation(_ location: CLLocation) -> PluginCallResultData {
36
36
 
37
37
  @objc(BackgroundGeolocation)
38
38
  public class BackgroundGeolocation: CAPPlugin, CLLocationManagerDelegate, CAPBridgedPlugin {
39
- private let PLUGIN_VERSION: String = "7.1.1"
39
+ private let PLUGIN_VERSION: String = "7.2.0"
40
40
  public let identifier = "BackgroundGeolocationPlugin"
41
41
  public let jsName = "BackgroundGeolocation"
42
42
  public let pluginMethods: [CAPPluginMethod] = [
@@ -1,15 +1,353 @@
1
1
  import XCTest
2
- @testable import CapgoAlarmPlugin
2
+ import CoreLocation
3
+ @testable import CapgoBackgroundGeolocationPlugin
3
4
 
4
- class CapgoAlarmTests: XCTestCase {
5
- func testEcho() {
6
- // This is an example of a functional test case for a plugin.
7
- // Use XCTAssert and related functions to verify your tests produce the correct results.
5
+ class CapgoBackgroundGeolocationTests: XCTestCase {
8
6
 
9
- let implementation = CapgoAlarm()
10
- let value = "Hello, World!"
11
- let result = implementation.echo(value)
7
+ var plugin: BackgroundGeolocation!
12
8
 
13
- XCTAssertEqual(value, result)
9
+ override func setUp() {
10
+ super.setUp()
11
+ plugin = BackgroundGeolocation()
12
+ }
13
+
14
+ override func tearDown() {
15
+ plugin = nil
16
+ super.tearDown()
17
+ }
18
+
19
+ // MARK: - Plugin Initialization Tests
20
+
21
+ func testPluginInitialization() {
22
+ XCTAssertNotNil(plugin)
23
+ XCTAssertEqual(plugin.identifier, "BackgroundGeolocationPlugin")
24
+ XCTAssertEqual(plugin.jsName, "BackgroundGeolocation")
25
+ }
26
+
27
+ func testPluginMethods() {
28
+ let methodNames = plugin.pluginMethods.map { $0.name }
29
+ XCTAssertTrue(methodNames.contains("start"))
30
+ XCTAssertTrue(methodNames.contains("stop"))
31
+ XCTAssertTrue(methodNames.contains("openSettings"))
32
+ XCTAssertTrue(methodNames.contains("setPlannedRoute"))
33
+ XCTAssertTrue(methodNames.contains("getPluginVersion"))
34
+ }
35
+
36
+ // MARK: - Format Location Tests
37
+
38
+ func testFormatLocationBasic() {
39
+ let location = CLLocation(
40
+ coordinate: CLLocationCoordinate2D(latitude: 37.7749, longitude: -122.4194),
41
+ altitude: 100.0,
42
+ horizontalAccuracy: 5.0,
43
+ verticalAccuracy: 3.0,
44
+ course: 45.0,
45
+ speed: 10.0,
46
+ timestamp: Date()
47
+ )
48
+
49
+ let formatted = formatLocation(location)
50
+
51
+ XCTAssertEqual(formatted["latitude"] as? Double, 37.7749)
52
+ XCTAssertEqual(formatted["longitude"] as? Double, -122.4194)
53
+ XCTAssertEqual(formatted["accuracy"] as? Double, 5.0)
54
+ XCTAssertEqual(formatted["altitude"] as? Double, 100.0)
55
+ XCTAssertEqual(formatted["altitudeAccuracy"] as? Double, 3.0)
56
+ XCTAssertEqual(formatted["speed"] as? Double, 10.0)
57
+ XCTAssertEqual(formatted["bearing"] as? Double, 45.0)
58
+ XCTAssertNotNil(formatted["time"])
59
+ }
60
+
61
+ func testFormatLocationNegativeSpeed() {
62
+ let location = CLLocation(
63
+ coordinate: CLLocationCoordinate2D(latitude: 37.7749, longitude: -122.4194),
64
+ altitude: 100.0,
65
+ horizontalAccuracy: 5.0,
66
+ verticalAccuracy: 3.0,
67
+ course: -1.0,
68
+ speed: -1.0,
69
+ timestamp: Date()
70
+ )
71
+
72
+ let formatted = formatLocation(location)
73
+
74
+ // Speed and bearing should be null when negative
75
+ XCTAssertNil(formatted["speed"] as? Double)
76
+ XCTAssertNil(formatted["bearing"] as? Double)
77
+ }
78
+
79
+ func testFormatLocationTimestamp() {
80
+ let timestamp = Date(timeIntervalSince1970: 1609459200) // 2021-01-01 00:00:00 UTC
81
+ let location = CLLocation(
82
+ coordinate: CLLocationCoordinate2D(latitude: 0, longitude: 0),
83
+ altitude: 0,
84
+ horizontalAccuracy: 0,
85
+ verticalAccuracy: 0,
86
+ timestamp: timestamp
87
+ )
88
+
89
+ let formatted = formatLocation(location)
90
+ let timeMs = (formatted["time"] as? NSNumber)?.int64Value
91
+
92
+ XCTAssertEqual(timeMs, 1609459200000)
93
+ }
94
+
95
+ // MARK: - Coordinate Tests
96
+
97
+ func testCoordinateEdgeCases() {
98
+ // North Pole
99
+ let northPole = CLLocation(
100
+ coordinate: CLLocationCoordinate2D(latitude: 90, longitude: 0),
101
+ altitude: 0,
102
+ horizontalAccuracy: 0,
103
+ verticalAccuracy: 0,
104
+ timestamp: Date()
105
+ )
106
+ let formattedNorth = formatLocation(northPole)
107
+ XCTAssertEqual(formattedNorth["latitude"] as? Double, 90)
108
+
109
+ // South Pole
110
+ let southPole = CLLocation(
111
+ coordinate: CLLocationCoordinate2D(latitude: -90, longitude: 0),
112
+ altitude: 0,
113
+ horizontalAccuracy: 0,
114
+ verticalAccuracy: 0,
115
+ timestamp: Date()
116
+ )
117
+ let formattedSouth = formatLocation(southPole)
118
+ XCTAssertEqual(formattedSouth["latitude"] as? Double, -90)
119
+
120
+ // Date line
121
+ let dateLine = CLLocation(
122
+ coordinate: CLLocationCoordinate2D(latitude: 0, longitude: 180),
123
+ altitude: 0,
124
+ horizontalAccuracy: 0,
125
+ verticalAccuracy: 0,
126
+ timestamp: Date()
127
+ )
128
+ let formattedDate = formatLocation(dateLine)
129
+ XCTAssertEqual(formattedDate["longitude"] as? Double, 180)
130
+ }
131
+
132
+ // MARK: - Altitude Tests
133
+
134
+ func testAltitudeVariations() {
135
+ // Sea level
136
+ let seaLevel = CLLocation(
137
+ coordinate: CLLocationCoordinate2D(latitude: 0, longitude: 0),
138
+ altitude: 0,
139
+ horizontalAccuracy: 0,
140
+ verticalAccuracy: 0,
141
+ timestamp: Date()
142
+ )
143
+ let formattedSea = formatLocation(seaLevel)
144
+ XCTAssertEqual(formattedSea["altitude"] as? Double, 0)
145
+
146
+ // Below sea level (Death Valley)
147
+ let belowSea = CLLocation(
148
+ coordinate: CLLocationCoordinate2D(latitude: 0, longitude: 0),
149
+ altitude: -85.0,
150
+ horizontalAccuracy: 0,
151
+ verticalAccuracy: 0,
152
+ timestamp: Date()
153
+ )
154
+ let formattedBelow = formatLocation(belowSea)
155
+ XCTAssertEqual(formattedBelow["altitude"] as? Double, -85.0)
156
+
157
+ // Mount Everest height
158
+ let mountain = CLLocation(
159
+ coordinate: CLLocationCoordinate2D(latitude: 0, longitude: 0),
160
+ altitude: 8849.0,
161
+ horizontalAccuracy: 0,
162
+ verticalAccuracy: 0,
163
+ timestamp: Date()
164
+ )
165
+ let formattedMountain = formatLocation(mountain)
166
+ XCTAssertEqual(formattedMountain["altitude"] as? Double, 8849.0)
167
+ }
168
+
169
+ // MARK: - Accuracy Tests
170
+
171
+ func testAccuracyValues() {
172
+ // High accuracy
173
+ let highAccuracy = CLLocation(
174
+ coordinate: CLLocationCoordinate2D(latitude: 0, longitude: 0),
175
+ altitude: 0,
176
+ horizontalAccuracy: 5.0,
177
+ verticalAccuracy: 3.0,
178
+ timestamp: Date()
179
+ )
180
+ let formattedHigh = formatLocation(highAccuracy)
181
+ XCTAssertEqual(formattedHigh["accuracy"] as? Double, 5.0)
182
+ XCTAssertEqual(formattedHigh["altitudeAccuracy"] as? Double, 3.0)
183
+
184
+ // Low accuracy
185
+ let lowAccuracy = CLLocation(
186
+ coordinate: CLLocationCoordinate2D(latitude: 0, longitude: 0),
187
+ altitude: 0,
188
+ horizontalAccuracy: 500.0,
189
+ verticalAccuracy: 300.0,
190
+ timestamp: Date()
191
+ )
192
+ let formattedLow = formatLocation(lowAccuracy)
193
+ XCTAssertEqual(formattedLow["accuracy"] as? Double, 500.0)
194
+ XCTAssertEqual(formattedLow["altitudeAccuracy"] as? Double, 300.0)
195
+ }
196
+
197
+ // MARK: - Speed and Bearing Tests
198
+
199
+ func testSpeedAndBearing() {
200
+ // Stationary
201
+ let stationary = CLLocation(
202
+ coordinate: CLLocationCoordinate2D(latitude: 0, longitude: 0),
203
+ altitude: 0,
204
+ horizontalAccuracy: 0,
205
+ verticalAccuracy: 0,
206
+ course: 0,
207
+ speed: 0,
208
+ timestamp: Date()
209
+ )
210
+ let formattedStationary = formatLocation(stationary)
211
+ XCTAssertEqual(formattedStationary["speed"] as? Double, 0)
212
+ XCTAssertEqual(formattedStationary["bearing"] as? Double, 0)
213
+
214
+ // Moving north
215
+ let movingNorth = CLLocation(
216
+ coordinate: CLLocationCoordinate2D(latitude: 0, longitude: 0),
217
+ altitude: 0,
218
+ horizontalAccuracy: 0,
219
+ verticalAccuracy: 0,
220
+ course: 0,
221
+ speed: 20.0,
222
+ timestamp: Date()
223
+ )
224
+ let formattedNorth = formatLocation(movingNorth)
225
+ XCTAssertEqual(formattedNorth["speed"] as? Double, 20.0)
226
+ XCTAssertEqual(formattedNorth["bearing"] as? Double, 0)
227
+
228
+ // Moving east
229
+ let movingEast = CLLocation(
230
+ coordinate: CLLocationCoordinate2D(latitude: 0, longitude: 0),
231
+ altitude: 0,
232
+ horizontalAccuracy: 0,
233
+ verticalAccuracy: 0,
234
+ course: 90,
235
+ speed: 15.0,
236
+ timestamp: Date()
237
+ )
238
+ let formattedEast = formatLocation(movingEast)
239
+ XCTAssertEqual(formattedEast["speed"] as? Double, 15.0)
240
+ XCTAssertEqual(formattedEast["bearing"] as? Double, 90)
241
+ }
242
+
243
+ // MARK: - Performance Tests
244
+
245
+ func testPerformanceFormatLocation() {
246
+ let location = CLLocation(
247
+ coordinate: CLLocationCoordinate2D(latitude: 37.7749, longitude: -122.4194),
248
+ altitude: 100.0,
249
+ horizontalAccuracy: 5.0,
250
+ verticalAccuracy: 3.0,
251
+ course: 45.0,
252
+ speed: 10.0,
253
+ timestamp: Date()
254
+ )
255
+
256
+ self.measure {
257
+ for _ in 0..<1000 {
258
+ _ = formatLocation(location)
259
+ }
260
+ }
261
+ }
262
+
263
+ func testPerformanceMultipleLocationUpdates() {
264
+ var locations: [CLLocation] = []
265
+ for index in 0..<100 {
266
+ let location = CLLocation(
267
+ coordinate: CLLocationCoordinate2D(
268
+ latitude: 37.7749 + Double(index) * 0.001,
269
+ longitude: -122.4194 + Double(index) * 0.001
270
+ ),
271
+ altitude: 100.0,
272
+ horizontalAccuracy: 5.0,
273
+ verticalAccuracy: 3.0,
274
+ course: 45.0,
275
+ speed: 10.0,
276
+ timestamp: Date()
277
+ )
278
+ locations.append(location)
279
+ }
280
+
281
+ self.measure {
282
+ for location in locations {
283
+ _ = formatLocation(location)
284
+ }
285
+ }
286
+ }
287
+
288
+ // MARK: - Data Type Tests
289
+
290
+ func testFormattedLocationDataTypes() {
291
+ let location = CLLocation(
292
+ coordinate: CLLocationCoordinate2D(latitude: 37.7749, longitude: -122.4194),
293
+ altitude: 100.0,
294
+ horizontalAccuracy: 5.0,
295
+ verticalAccuracy: 3.0,
296
+ course: 45.0,
297
+ speed: 10.0,
298
+ timestamp: Date()
299
+ )
300
+
301
+ let formatted = formatLocation(location)
302
+
303
+ // Verify all values are of expected types
304
+ XCTAssertTrue(formatted["latitude"] is Double)
305
+ XCTAssertTrue(formatted["longitude"] is Double)
306
+ XCTAssertTrue(formatted["accuracy"] is Double)
307
+ XCTAssertTrue(formatted["altitude"] is Double)
308
+ XCTAssertTrue(formatted["altitudeAccuracy"] is Double)
309
+ XCTAssertTrue(formatted["speed"] is Double)
310
+ XCTAssertTrue(formatted["bearing"] is Double)
311
+ XCTAssertTrue(formatted["time"] is NSNumber)
312
+ XCTAssertTrue(formatted["simulated"] is Bool)
313
+ }
314
+
315
+ // MARK: - Simulated Location Tests (iOS 15+)
316
+
317
+ func testSimulatedLocationFlag() {
318
+ let location = CLLocation(
319
+ coordinate: CLLocationCoordinate2D(latitude: 37.7749, longitude: -122.4194),
320
+ altitude: 100.0,
321
+ horizontalAccuracy: 5.0,
322
+ verticalAccuracy: 3.0,
323
+ timestamp: Date()
324
+ )
325
+
326
+ let formatted = formatLocation(location)
327
+
328
+ // The simulated flag should be present
329
+ XCTAssertNotNil(formatted["simulated"])
330
+ XCTAssertTrue(formatted["simulated"] is Bool)
331
+ }
332
+
333
+ // MARK: - Precision Tests
334
+
335
+ func testHighPrecisionCoordinates() {
336
+ // Test with high precision coordinates
337
+ let preciseLat = 37.77492830283847
338
+ let preciseLon = -122.41943529482831
339
+
340
+ let location = CLLocation(
341
+ coordinate: CLLocationCoordinate2D(latitude: preciseLat, longitude: preciseLon),
342
+ altitude: 0,
343
+ horizontalAccuracy: 0,
344
+ verticalAccuracy: 0,
345
+ timestamp: Date()
346
+ )
347
+
348
+ let formatted = formatLocation(location)
349
+
350
+ XCTAssertEqual(formatted["latitude"] as? Double, preciseLat)
351
+ XCTAssertEqual(formatted["longitude"] as? Double, preciseLon)
14
352
  }
15
353
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capgo/background-geolocation",
3
- "version": "7.1.1",
3
+ "version": "7.2.0",
4
4
  "description": "Receive accurate geolocation updates even while the app is in the background.",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",
@@ -29,6 +29,9 @@
29
29
  "verify:ios": "xcodebuild -scheme CapgoBackgroundGeolocation -destination generic/platform=iOS",
30
30
  "verify:android": "cd android && ./gradlew clean build test && cd ..",
31
31
  "verify:web": "npm run build",
32
+ "test": "npm run test:ios && npm run test:android",
33
+ "test:ios": "./scripts/test-ios.sh",
34
+ "test:android": "cd android && ./gradlew test && cd ..",
32
35
  "lint": "npm run eslint && npm run prettier -- --check && npm run swiftlint -- lint",
33
36
  "fmt": "npm run eslint -- --fix && npm run prettier -- --write && npm run swiftlint -- --fix --format",
34
37
  "eslint": "eslint . --ext ts",