Skip to content

Commit b18e959

Browse files
committed
MXBeaconAggregationsTests: Handle stop location sharing.
1 parent 1225df0 commit b18e959

File tree

1 file changed

+109
-25
lines changed

1 file changed

+109
-25
lines changed

MatrixSDKTests/MXBeaconAggregationsTests.swift

Lines changed: 109 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -145,41 +145,125 @@ class MXBeaconAggregationsTests: XCTestCase {
145145
switch response {
146146
case .success(let eventId):
147147

148-
var localEcho: MXEvent?
148+
var firstUpdateListener: Any?
149149

150-
locationService.sendBeacon(withBeaconInfoEventId: eventId, latitude: expectedBeaconLatitude, longitude: expectedBeaconLongitude, description: expectedBeaconDescription, threadId: nil, inRoomWithId: roomId, localEcho: &localEcho) { response in
150+
firstUpdateListener = bobSession.aggregations.beaconAggegations.listenToBeaconInfoSummaryUpdateInRoom(withId: roomId) { beaconInfoSummary in
151151

152-
switch response {
153-
case .success:
152+
if let firstUpdateListener = firstUpdateListener {
153+
bobSession.aggregations.removeListener(firstUpdateListener)
154+
}
155+
156+
var localEcho: MXEvent?
157+
158+
locationService.sendLocation(withBeaconInfoEventId: eventId, latitude: expectedBeaconLatitude, longitude: expectedBeaconLongitude, description: expectedBeaconDescription, threadId: nil, inRoomWithId: roomId, localEcho: &localEcho) { response in
154159

155-
_ = bobSession.aggregations.beaconAggegations.listenToBeaconInfoSummaryUpdateInRoom(withId: initialRoom.roomId) { beaconInfoSummary in
156-
157-
XCTAssertEqual(beaconInfoSummary.id, eventId)
158-
XCTAssertEqual(beaconInfoSummary.userId, userId)
159-
160-
let beaconInfo = beaconInfoSummary.beaconInfo
160+
switch response {
161+
case .success:
161162

162-
XCTAssertEqual(beaconInfo.desc, expectedBeaconInfoDescription)
163-
XCTAssertEqual(beaconInfo.timeout, UInt64(expectedBeaconInfoTimeout))
164-
XCTAssertEqual(beaconInfo.isLive, expectedBeaconInfoIsLive)
165-
166-
let beacon = beaconInfoSummary.lastBeacon
163+
_ = bobSession.aggregations.beaconAggegations.listenToBeaconInfoSummaryUpdateInRoom(withId: initialRoom.roomId) { beaconInfoSummary in
164+
165+
XCTAssertEqual(beaconInfoSummary.id, eventId)
166+
XCTAssertEqual(beaconInfoSummary.userId, userId)
167+
168+
let beaconInfo = beaconInfoSummary.beaconInfo
169+
170+
XCTAssertEqual(beaconInfo.desc, expectedBeaconInfoDescription)
171+
XCTAssertEqual(beaconInfo.timeout, UInt64(expectedBeaconInfoTimeout))
172+
XCTAssertEqual(beaconInfo.isLive, expectedBeaconInfoIsLive)
173+
174+
let beacon = beaconInfoSummary.lastBeacon
175+
176+
XCTAssertNotNil(beacon)
177+
178+
if let beacon = beacon {
179+
XCTAssertEqual(beacon.location.desc, expectedBeaconDescription)
180+
XCTAssertEqual(beacon.location.latitude, expectedBeaconLatitude)
181+
XCTAssertEqual(beacon.location.longitude, expectedBeaconLongitude)
182+
}
183+
184+
expectation.fulfill()
185+
}
167186

168-
XCTAssertNotNil(beacon)
187+
case .failure(let error):
188+
XCTFail("Send beacon location fails with error: \(error)")
189+
expectation.fulfill()
190+
}
191+
}
192+
193+
}
194+
case .failure(let error):
195+
XCTFail("Start location sharing fails with error: \(error)")
196+
expectation.fulfill()
197+
}
198+
}
199+
}
200+
}
201+
202+
/// Test: Expect beacon info state event live property set to false after user has stopped to share is location
203+
/// - Create a Bob session
204+
/// - Create an initial room
205+
/// - Start location sharing
206+
/// - Expect a beacon info state event with live == false
207+
func testStopLiveLocationSharingSucceed() {
208+
let store = MXMemoryStore()
209+
testData.doMXSessionTest(withBobAndARoom: self, andStore: store) { bobSession, initialRoom, expectation in
210+
guard let bobSession = bobSession,
211+
let initialRoom = initialRoom,
212+
let expectation = expectation else {
213+
XCTFail("Failed to setup test conditions")
214+
return
215+
}
216+
217+
let locationService: MXLocationService = bobSession.locationService
218+
219+
let expectedBeaconInfoDescription = "Live location description"
220+
let expectedBeaconInfoTimeout: TimeInterval = 600000
221+
let expectedBeaconInfoIsLive = false
222+
223+
let roomId: String = initialRoom.roomId
224+
let userId: String = bobSession.myUserId
225+
var beaconInfoEventId: String?
226+
227+
locationService.startUserLocationSharing(withRoomId: roomId, description: expectedBeaconInfoDescription, timeout: expectedBeaconInfoTimeout) { response in
228+
229+
switch response {
230+
case .success(let eventId):
231+
232+
beaconInfoEventId = eventId
233+
234+
var firstUpdateListener: Any?
235+
236+
firstUpdateListener = bobSession.aggregations.beaconAggegations.listenToBeaconInfoSummaryUpdateInRoom(withId: roomId) { beaconInfoSummary in
237+
238+
if let firstUpdateListener = firstUpdateListener {
239+
bobSession.aggregations.removeListener(firstUpdateListener)
240+
}
241+
242+
locationService.stopUserLocationSharing(withBeaconInfoEventId: eventId, roomId: roomId) { response in
243+
244+
switch response {
245+
case .success:
169246

170-
if let beacon = beacon {
171-
XCTAssertEqual(beacon.location.desc, expectedBeaconDescription)
172-
XCTAssertEqual(beacon.location.latitude, expectedBeaconLatitude)
173-
XCTAssertEqual(beacon.location.longitude, expectedBeaconLongitude)
174-
}
247+
_ = bobSession.aggregations.beaconAggegations.listenToBeaconInfoSummaryUpdateInRoom(withId: roomId) { beaconInfoSummary in
248+
249+
XCTAssertEqual(beaconInfoSummary.id, beaconInfoEventId)
250+
XCTAssertEqual(beaconInfoSummary.userId, userId)
251+
252+
let beaconInfo = beaconInfoSummary.beaconInfo
253+
254+
XCTAssertEqual(beaconInfo.desc, expectedBeaconInfoDescription)
255+
XCTAssertEqual(beaconInfo.timeout, UInt64(expectedBeaconInfoTimeout))
256+
XCTAssertEqual(beaconInfo.isLive, expectedBeaconInfoIsLive)
175257

258+
expectation.fulfill()
259+
}
260+
261+
case .failure(let error):
262+
XCTFail("Stop location sharing fails with error: \(error)")
176263
expectation.fulfill()
177264
}
178-
179-
case .failure(let error):
180-
XCTFail("Send beacon location fails with error: \(error)")
181-
expectation.fulfill()
182265
}
266+
183267
}
184268

185269
case .failure(let error):

0 commit comments

Comments
 (0)