Add RoomChangePermissionsScreen (#2513)

This commit is contained in:
Doug
2024-03-01 16:11:22 +00:00
committed by GitHub
parent ce667eab13
commit bd15190b23
19 changed files with 919 additions and 99 deletions
@@ -0,0 +1,87 @@
//
// Copyright 2022 New Vector Ltd
//
// 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 XCTest
@testable import ElementX
@MainActor
class RoomChangePermissionsScreenViewModelTests: XCTestCase {
var roomProxy: RoomProxyMock!
var viewModel: RoomChangePermissionsScreenViewModelProtocol!
var context: RoomChangePermissionsScreenViewModelType.Context {
viewModel.context
}
override func setUp() {
roomProxy = RoomProxyMock(with: .init())
viewModel = RoomChangePermissionsScreenViewModel(currentPermissions: .init(),
group: .roomDetails,
roomProxy: roomProxy,
userIndicatorController: UserIndicatorControllerMock())
}
func testChangeSetting() {
// Given a screen with no changes.
guard let index = context.settings.firstIndex(where: { $0.keyPath == \.roomAvatar }) else {
XCTFail("There should be a setting for the room avatar.")
return
}
XCTAssertEqual(context.settings[index].value, .moderator)
XCTAssertFalse(context.viewState.hasChanges)
// When updating a setting.
let setting = RoomPermissionsSetting(keyPath: \.roomAvatar, value: .user, title: "")
context.settings[index] = setting
// Then the setting should update and the changes should be flagged.
XCTAssertEqual(context.settings[index].value, .user)
XCTAssertTrue(context.viewState.hasChanges)
}
func testSave() async throws {
// Given a screen with changes.
guard let index = context.settings.firstIndex(where: { $0.keyPath == \.roomAvatar }) else {
XCTFail("There should be a setting for the room avatar.")
return
}
context.settings[index] = RoomPermissionsSetting(keyPath: \.roomAvatar, value: .user, title: "")
XCTAssertEqual(context.settings[index].value, .user)
XCTAssertTrue(context.viewState.hasChanges)
// When saving changes.
context.send(viewAction: .save)
// Nothing to await right now,
try await Task.sleep(for: .milliseconds(100))
// Then the changes should be applied.
XCTAssertTrue(roomProxy.applyPowerLevelChangesCalled)
XCTAssertEqual(roomProxy.applyPowerLevelChangesReceivedChanges, .init(roomName: 50, roomAvatar: 0, roomTopic: 50),
"Only the changes for this screen should be applied, the others should be nil to remain unchanged.")
}
func testSaveNoChanges() async throws {
// Given a screen with no changes.
XCTAssertFalse(context.viewState.hasChanges)
// When saving changes.
context.send(viewAction: .save)
// Then nothing should happen.
XCTAssertFalse(roomProxy.applyPowerLevelChangesCalled)
}
}
@@ -0,0 +1,91 @@
//
// Copyright 2024 New Vector Ltd
//
// 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 MatrixRustSDK
import XCTest
@testable import ElementX
class RoomPermissionsTests: XCTestCase {
func testEmptyFromRust() {
// Given an empty set of power level changes.
let powerLevelChanges = RoomPowerLevelChanges()
// When creating room permissions from them.
let permissions = RoomPermissions(powerLevelChanges: powerLevelChanges)
// Then none of the permissions should be set.
XCTAssertNil(permissions.ban)
XCTAssertNil(permissions.invite)
XCTAssertNil(permissions.kick)
XCTAssertNil(permissions.redact)
XCTAssertNil(permissions.eventsDefault)
XCTAssertNil(permissions.stateDefault)
XCTAssertNil(permissions.usersDefault)
XCTAssertNil(permissions.roomName)
XCTAssertNil(permissions.roomAvatar)
XCTAssertNil(permissions.roomTopic)
}
func testCompleteFromRust() {
// Given a set of power level changes with all the values set to 100.
let powerLevelChanges = RoomPowerLevelChanges(ban: 100,
invite: 100,
kick: 100,
redact: 100,
eventsDefault: 100,
stateDefault: 100,
usersDefault: 100,
roomName: 100,
roomAvatar: 100,
roomTopic: 100)
// When creating room permissions from them.
let permissions = RoomPermissions(powerLevelChanges: powerLevelChanges)
// Then all of the permissions should be for an administrator.
XCTAssertEqual(permissions.ban, .administrator)
XCTAssertEqual(permissions.invite, .administrator)
XCTAssertEqual(permissions.kick, .administrator)
XCTAssertEqual(permissions.redact, .administrator)
XCTAssertEqual(permissions.eventsDefault, .administrator)
XCTAssertEqual(permissions.stateDefault, .administrator)
XCTAssertEqual(permissions.usersDefault, .administrator)
XCTAssertEqual(permissions.roomName, .administrator)
XCTAssertEqual(permissions.roomAvatar, .administrator)
XCTAssertEqual(permissions.roomTopic, .administrator)
}
func testToRust() {
// Given a set of permissions where on some of the values have been set.
let permissions = RoomPermissions(roomName: .administrator, roomAvatar: .administrator, roomTopic: .administrator)
// When creating power level changes from them.
let powerLevelChanges = permissions.makePowerLevelChanges()
// Then only the permissions that were set should be included.
XCTAssertNil(powerLevelChanges.ban, "Unset values should be nil for Rust to merge with the current value.")
XCTAssertNil(powerLevelChanges.invite, "Unset values should be nil for Rust to merge with the current value.")
XCTAssertNil(powerLevelChanges.kick, "Unset values should be nil for Rust to merge with the current value.")
XCTAssertNil(powerLevelChanges.redact, "Unset values should be nil for Rust to merge with the current value.")
XCTAssertNil(powerLevelChanges.eventsDefault, "Unset values should be nil for Rust to merge with the current value.")
XCTAssertNil(powerLevelChanges.stateDefault, "Unset values should be nil for Rust to merge with the current value.")
XCTAssertNil(powerLevelChanges.usersDefault, "Unset values should be nil for Rust to merge with the current value.")
XCTAssertEqual(powerLevelChanges.roomName, 100)
XCTAssertEqual(powerLevelChanges.roomAvatar, 100)
XCTAssertEqual(powerLevelChanges.roomTopic, 100)
}
}