Skip to content

Commit

Permalink
element-hq/element-ios/issues/5114 - Added multi user and gappy sync …
Browse files Browse the repository at this point in the history
…intregration tests. Added simple poll aggregator.
  • Loading branch information
stefanceriu committed Nov 11, 2021
1 parent 780cf6b commit 6686dda
Show file tree
Hide file tree
Showing 8 changed files with 479 additions and 185 deletions.
58 changes: 37 additions & 21 deletions MatrixSDK.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion MatrixSDK/Data/MXRoom.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#import "MXEvent.h"
#import "MXJSONModels.h"
#import "MXEventContentPollStart.h"
#import "MXRoomSummary.h"
#import "MXRoomMember.h"
#import "MXReceiptData.h"
Expand All @@ -44,7 +45,6 @@
@class MXRoom;
@class MXSession;
@class MXUsersTrustLevelSummary;
@class MXEventContentPollStart;

#pragma mark - Notifications

Expand Down
55 changes: 55 additions & 0 deletions MatrixSDK/Room/Polls/MXPollAggregator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// Copyright 2021 The Matrix.org Foundation C.I.C
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import Foundation

struct MXPollAnswerOption {
let id: String
let text: String
}

struct MXPoll {
let text: String
let answerOptions: [MXPollAnswerOption]
}

class MXPollAggregator {

private let session: MXSession
private let room: MXRoom
private let pollStartEvent: MXEvent
private let eventListener: Any

deinit {
room.removeListener(eventListener)
}

init(session: MXSession, room: MXRoom, pollStartEvent: MXEvent) {
self.session = session
self.room = room
self.pollStartEvent = pollStartEvent

let pollContent = MXEventContentPollStart(fromJSON: pollStartEvent.content)!

let poll = MXPoll(text: pollContent.question, answerOptions: pollContent.answerOptions.compactMap({ answerOption in
MXPollAnswerOption(id: answerOption.uuid, text: answerOption.text)
}))

eventListener = room.listen(toEventsOfTypes: [kMXEventTypeStringPollResponse, kMXEventTypeStringPollEnd]) { event, direction, state in

} as Any
}
}
163 changes: 0 additions & 163 deletions MatrixSDKTests/MXAggregatedPollTests.m

This file was deleted.

81 changes: 81 additions & 0 deletions MatrixSDKTests/MXPollAggregatorTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//
// Copyright 2021 The Matrix.org Foundation C.I.C
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import Foundation

class PollAggregatorTest: XCTestCase {
private var matrixSDKTestsData: MatrixSDKTestsData!

private var matrixSDKTestsE2EData: MatrixSDKTestsE2EData!

private var pollAggregator: MXPollAggregator!

override func setUp() {
matrixSDKTestsData = MatrixSDKTestsData()
matrixSDKTestsE2EData = MatrixSDKTestsE2EData(matrixSDKTestsData: matrixSDKTestsData)
}

override func tearDown() {
matrixSDKTestsData = nil
matrixSDKTestsE2EData = nil
}

func testAggregator() {
self.createScenarioForBobAndAlice { bobSession, bobRoom, event, expectation in
self.pollAggregator = MXPollAggregator(session: bobSession, room: bobRoom, pollStartEvent: event)

DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
expectation.fulfill()
}

}
}

func createScenarioForBobAndAlice(_ readyToTest: @escaping (MXSession, MXRoom, MXEvent, XCTestExpectation)->Void) {

self.matrixSDKTestsData.doTestWithAliceAndBob(inARoom: self, aliceStore: MXMemoryStore(), bobStore: MXMemoryStore()) { aliceSession, bobSession, roomId, expectation in

let room = bobSession?.room(withRoomId: roomId)

let answerOptions = [MXEventContentPollStartAnswerOption(uuid: UUID().uuidString, text: "First answer"),
MXEventContentPollStartAnswerOption(uuid: UUID().uuidString, text: "Second answer")]

let pollStartContent = MXEventContentPollStart(question: "Question", kind: kMXMessageContentKeyExtensiblePollKindDisclosed, maxSelections: 1, answerOptions: answerOptions)

room?.sendPollStart(withContent: pollStartContent, localEcho: nil, success: { pollStartEventId in

bobSession?.event(withEventId: pollStartEventId, inRoom: roomId, success: { pollStartEvent in
readyToTest(bobSession!, room!, pollStartEvent!, expectation!)

let aliceRoom = aliceSession?.room(withRoomId: roomId)
aliceRoom?.sendPollResponse(for: pollStartEvent, withAnswerIdentifiers: [pollStartContent.answerOptions.first!.uuid], localEcho: nil, success: { _ in

}, failure: { error in
XCTFail("The operation should not fail - NSError: \(String(describing: error))")
expectation?.fulfill()
})

}, failure: { error in
XCTFail("The operation should not fail - NSError: \(String(describing: error))")
expectation?.fulfill()
})
}, failure: { error in
XCTFail("The operation should not fail - NSError: \(String(describing: error))")
expectation?.fulfill()
})
}
}
}
Loading

0 comments on commit 6686dda

Please sign in to comment.