Merge pull request #7065 from vector-im/alfogrillo/increase_dm_code_coverage

Increase dm code coverage (PSG-925)
This commit is contained in:
Alfonso Grillo
2022-11-11 18:46:02 +01:00
committed by GitHub
17 changed files with 101 additions and 57 deletions
@@ -0,0 +1,202 @@
//
// 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.
//
@testable import RiotSwiftUI
import XCTest
class UserAgentParserTests: XCTestCase {
func testAndroidUserAgents() throws {
let uaStrings = [
// New User Agent Implementation
"Element dbg/1.5.0-dev (Xiaomi Mi 9T; Android 11; RKQ1.200826.002 test-keys; Flavour GooglePlay; MatrixAndroidSdk2 1.5.2)",
"Element/1.5.0 (Samsung SM-G960F; Android 6.0.1; RKQ1.200826.002; Flavour FDroid; MatrixAndroidSdk2 1.5.2)",
"Element/1.5.0 (Google Nexus 5; Android 7.0; RKQ1.200826.002 test test; Flavour FDroid; MatrixAndroidSdk2 1.5.2)",
// Legacy User Agent Implementation
"Element/1.0.0 (Linux; U; Android 6.0.1; SM-A510F Build/MMB29; Flavour GPlay; MatrixAndroidSdk2 1.0)",
"Element/1.0.0 (Linux; Android 7.0; SM-G610M Build/NRD90M; Flavour GPlay; MatrixAndroidSdk2 1.0)"
]
let userAgents = uaStrings.map { UserAgentParser.parse($0) }
let expected = [
UserAgent(deviceType: .mobile,
deviceModel: "Xiaomi Mi 9T",
deviceOS: "Android 11",
clientName: "Element dbg",
clientVersion: "1.5.0-dev"),
UserAgent(deviceType: .mobile,
deviceModel: "Samsung SM-G960F",
deviceOS: "Android 6.0.1",
clientName: "Element",
clientVersion: "1.5.0"),
UserAgent(deviceType: .mobile,
deviceModel: "Google Nexus 5",
deviceOS: "Android 7.0",
clientName: "Element",
clientVersion: "1.5.0"),
UserAgent(deviceType: .mobile,
deviceModel: "SM-A510F Build/MMB29",
deviceOS: "Android 6.0.1",
clientName: "Element",
clientVersion: "1.0.0"),
UserAgent(deviceType: .mobile,
deviceModel: "SM-G610M Build/NRD90M",
deviceOS: "Android 7.0",
clientName: "Element",
clientVersion: "1.0.0")
]
XCTAssertEqual(userAgents, expected)
}
func testIOSUserAgents() throws {
let uaStrings = [
// New User Agent Implementation
"Element/1.9.8 (iPhone X; iOS 15.2; Scale/3.00)",
"Element/1.9.9 (iPhone XS; iOS 15.5; Scale/3.00)",
"Element/1.9.7 (iPad Pro (12.9-inch) (3rd generation); iOS 15.5; Scale/3.00)",
// Legacy User Agent Implementation
"Element/1.8.21 (iPhone; iOS 15.0; Scale/2.00)",
"Element/1.8.19 (iPhone; iOS 15.2; Scale/3.00)",
// Simulator User Agent
"Element/1.9.7 (Simulator (iPhone 13 Pro Max); iOS 15.5; Scale/3.00)"
]
let userAgents = uaStrings.map { UserAgentParser.parse($0) }
let expected = [
UserAgent(deviceType: .mobile,
deviceModel: "iPhone X",
deviceOS: "iOS 15.2",
clientName: "Element",
clientVersion: "1.9.8"),
UserAgent(deviceType: .mobile,
deviceModel: "iPhone XS",
deviceOS: "iOS 15.5",
clientName: "Element",
clientVersion: "1.9.9"),
UserAgent(deviceType: .mobile,
deviceModel: "iPad Pro (12.9-inch) (3rd generation)",
deviceOS: "iOS 15.5",
clientName: "Element",
clientVersion: "1.9.7"),
UserAgent(deviceType: .mobile,
deviceModel: "iPhone",
deviceOS: "iOS 15.0",
clientName: "Element",
clientVersion: "1.8.21"),
UserAgent(deviceType: .mobile,
deviceModel: "iPhone",
deviceOS: "iOS 15.2",
clientName: "Element",
clientVersion: "1.8.19"),
UserAgent(deviceType: .mobile,
deviceModel: "Simulator (iPhone 13 Pro Max)",
deviceOS: "iOS 15.5",
clientName: "Element",
clientVersion: "1.9.7")
]
XCTAssertEqual(userAgents, expected)
}
func testDesktopUserAgents() {
let uaStrings = [
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) ElementNightly/2022091301 Chrome/104.0.5112.102 Electron/20.1.1 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) ElementNightly/2022091301 Chrome/104.0.5112.102 Electron/20.1.1 Safari/537.36"
]
let userAgents = uaStrings.map { UserAgentParser.parse($0) }
let expected = [
UserAgent(deviceType: .desktop,
deviceModel: nil,
deviceOS: "macOS",
clientName: "Electron",
clientVersion: "20.1.1"),
UserAgent(deviceType: .desktop,
deviceModel: nil,
deviceOS: "Windows",
clientName: "Electron",
clientVersion: "20.1.1")
]
XCTAssertEqual(userAgents, expected)
}
func testWebUserAgents() throws {
let uaStrings = [
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:39.0) Gecko/20100101 Firefox/39.0",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/600.3.18 (KHTML, like Gecko) Version/8.0.3 Safari/600.3.18",
"Mozilla/5.0 (Linux; Android 9; SM-G973U Build/PPR1.180610.011) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Mobile Safari/537.36"
]
let userAgents = uaStrings.map { UserAgentParser.parse($0) }
let expected = [
UserAgent(deviceType: .web,
deviceModel: nil,
deviceOS: "macOS",
clientName: "Chrome",
clientVersion: "104.0.5112.102"),
UserAgent(deviceType: .web,
deviceModel: nil,
deviceOS: "Windows",
clientName: "Chrome",
clientVersion: "104.0.5112.102"),
UserAgent(deviceType: .web,
deviceModel: nil,
deviceOS: "macOS",
clientName: "Firefox",
clientVersion: "39.0"),
UserAgent(deviceType: .web,
deviceModel: nil,
deviceOS: "macOS",
clientName: "Safari",
clientVersion: "8.0.3"),
UserAgent(deviceType: .web,
deviceModel: nil,
deviceOS: "Android 9",
clientName: "Chrome",
clientVersion: "69.0.3497.100")
]
XCTAssertEqual(userAgents, expected)
}
func testInvalidUserAgents() throws {
let uaStrings = [
"Element (iPhone X; OS 15.2; 3.00)",
"Element/1.9.9; iOS",
"Element/1.9.7 Android",
"some random string",
"Element/1.9.9; iOS "
]
let userAgents = uaStrings.map { UserAgentParser.parse($0) }
let expected = [
.unknown,
.unknown,
.unknown,
.unknown,
UserAgent(deviceType: .mobile,
deviceModel: nil,
deviceOS: nil,
clientName: "Element",
clientVersion: "1.9.9;")
]
XCTAssertEqual(userAgents, expected)
}
}
@@ -40,7 +40,7 @@ final class UserOtherSessionsCoordinator: Coordinator, Presentable {
let viewModel = UserOtherSessionsViewModel(sessionInfos: parameters.sessionInfos,
filter: parameters.filter,
title: parameters.title,
settingService: RiotSettings.shared)
settingsService: RiotSettings.shared)
let view = UserOtherSessions(viewModel: viewModel.context)
userOtherSessionsViewModel = viewModel
userOtherSessionsHostingController = VectorHostingController(rootView: view)
@@ -25,6 +25,7 @@ enum MockUserOtherSessionsScreenState: MockScreenState, CaseIterable {
// mock that screen.
case all
case none
case inactiveSessions
case unverifiedSessions
case verifiedSessions
@@ -37,7 +38,7 @@ enum MockUserOtherSessionsScreenState: MockScreenState, CaseIterable {
/// A list of screen state definitions
static var allCases: [MockUserOtherSessionsScreenState] {
// Each of the presence statuses
[.all, .inactiveSessions, .unverifiedSessions, .verifiedSessions]
[.all, .none, .inactiveSessions, .unverifiedSessions, .verifiedSessions]
}
/// Generate the view struct for the screen state.
@@ -48,22 +49,27 @@ enum MockUserOtherSessionsScreenState: MockScreenState, CaseIterable {
viewModel = UserOtherSessionsViewModel(sessionInfos: allSessions(),
filter: .all,
title: VectorL10n.userSessionsOverviewOtherSessionsSectionTitle,
settingService: MockUserSessionSettings())
settingsService: MockUserSessionSettings())
case .none:
viewModel = UserOtherSessionsViewModel(sessionInfos: [],
filter: .all,
title: VectorL10n.userSessionsOverviewOtherSessionsSectionTitle,
settingsService: MockUserSessionSettings())
case .inactiveSessions:
viewModel = UserOtherSessionsViewModel(sessionInfos: inactiveSessions(),
filter: .inactive,
title: VectorL10n.userOtherSessionSecurityRecommendationTitle,
settingService: MockUserSessionSettings())
settingsService: MockUserSessionSettings())
case .unverifiedSessions:
viewModel = UserOtherSessionsViewModel(sessionInfos: unverifiedSessions(),
filter: .unverified,
title: VectorL10n.userOtherSessionSecurityRecommendationTitle,
settingService: MockUserSessionSettings())
settingsService: MockUserSessionSettings())
case .verifiedSessions:
viewModel = UserOtherSessionsViewModel(sessionInfos: verifiedSessions(),
filter: .verified,
title: VectorL10n.userOtherSessionSecurityRecommendationTitle,
settingService: MockUserSessionSettings())
settingsService: MockUserSessionSettings())
}
// can simulate service and viewModel actions here if needs be.
@@ -114,4 +114,12 @@ class UserOtherSessionsUITests: MockScreenTestCase {
XCTAssertTrue(button.exists)
XCTAssertFalse(buttonLearnMore.exists)
}
func test_whenNoSessionAreShown_theLayoutIsCorrect() {
app.goToScreenWithIdentifier(MockUserOtherSessionsScreenState.none.title)
let button = app.buttons["UserOtherSessions.clearFilter"]
let text = app.staticTexts["UserOtherSessions.noItemsText"]
XCTAssertTrue(button.exists)
XCTAssertTrue(text.exists)
}
}
@@ -346,7 +346,7 @@ class UserOtherSessionsViewModelTests: XCTestCase {
UserOtherSessionsViewModel(sessionInfos: sessionInfos,
filter: filter,
title: title,
settingService: MockUserSessionSettings())
settingsService: MockUserSessionSettings())
}
private func createUserSessionInfo(sessionId: String,
@@ -28,12 +28,12 @@ class UserOtherSessionsViewModel: UserOtherSessionsViewModelType, UserOtherSessi
init(sessionInfos: [UserSessionInfo],
filter: UserOtherSessionsFilter,
title: String,
settingService: UserSessionSettingsProtocol) {
settingsService: UserSessionSettingsProtocol) {
self.sessionInfos = sessionInfos
defaultTitle = title
let bindings = UserOtherSessionsBindings(filter: filter, isEditModeEnabled: false)
let sessionItems = filter.filterSessionInfos(sessionInfos: sessionInfos, selectedSessions: selectedSessions)
self.settingsService = settingService
self.settingsService = settingsService
super.init(initialViewState: UserOtherSessionsViewState(bindings: bindings,
title: title,
sessionItems: sessionItems,
@@ -41,7 +41,7 @@ class UserOtherSessionsViewModel: UserOtherSessionsViewModelType, UserOtherSessi
emptyItemsTitle: filter.userOtherSessionsViewEmptyResultsTitle,
allItemsSelected: false,
enableSignOutButton: false,
showLocationInfo: settingService.showIPAddressesInSessionsManager))
showLocationInfo: settingsService.showIPAddressesInSessionsManager))
}
// MARK: - Public
@@ -73,6 +73,7 @@ struct UserOtherSessions: View {
.font(theme.fonts.footnote)
.foregroundColor(theme.colors.primaryContent)
.padding(.bottom, 20)
.accessibilityIdentifier("UserOtherSessions.noItemsText")
Button {
viewModel.send(viewAction: .clearFilter)
} label: {
@@ -87,6 +88,7 @@ struct UserOtherSessions: View {
}
.background(theme.colors.background)
}
.accessibilityIdentifier("UserOtherSessions.clearFilter")
}
}
@@ -62,13 +62,13 @@ enum MockUserSessionDetailsScreenState: MockScreenState, CaseIterable {
name: "Android",
deviceType: .mobile,
verificationState: .unverified,
lastSeenIP: "3.0.0.3",
lastSeenIP: nil,
lastSeenTimestamp: Date().timeIntervalSince1970 - 10,
applicationName: "Element Android",
applicationVersion: "1.0.0",
applicationName: "",
applicationVersion: "",
applicationURL: nil,
deviceModel: nil,
deviceOS: "Android 4.0",
deviceOS: nil,
lastSeenIPLocation: nil,
clientName: "Element",
clientVersion: "1.0.0",
@@ -18,18 +18,17 @@ import RiotSwiftUI
import XCTest
class UserSessionDetailsUITests: MockScreenTestCase {
func disabled_broken_xcode14_test_longPressDetailsCell_CopiesValueToClipboard() throws {
func test_screenWithAllTheContent() throws {
app.goToScreenWithIdentifier(MockUserSessionDetailsScreenState.allSections.title)
UIPasteboard.general.string = ""
let tables = app.tables
let sessionNameIosCell = tables.cells["Session name, iOS"]
sessionNameIosCell.press(forDuration: 0.5)
app.buttons["Copy"].tap()
let clipboard = try XCTUnwrap(UIPasteboard.general.string)
XCTAssertEqual(clipboard, "iOS")
let rows = app.staticTexts.matching(identifier: "UserSessionDetailsItem.title")
XCTAssertEqual(rows.count, 6)
}
func test_screenWithSessionSectionOnly() throws {
app.goToScreenWithIdentifier(MockUserSessionDetailsScreenState.sessionSectionOnly.title)
let rows = app.staticTexts.matching(identifier: "UserSessionDetailsItem.title")
XCTAssertEqual(rows.count, 3)
}
}
@@ -34,10 +34,12 @@ struct UserSessionDetailsItem: View {
.foregroundColor(theme.colors.secondaryContent)
.frame(maxWidth: .infinity, alignment: .leading)
.frame(maxHeight: .infinity, alignment: .top)
.accessibility(identifier: "UserSessionDetailsItem.title")
Text(viewData.value)
.font(theme.fonts.subheadline)
.foregroundColor(theme.colors.primaryContent)
.multilineTextAlignment(.trailing)
.accessibility(identifier: "UserSessionDetailsItem.value")
}
.contextMenu {
Button {
@@ -21,6 +21,7 @@ class UserSessionNameUITests: MockScreenTestCase {
func testUserSessionNameInitialState() {
app.goToScreenWithIdentifier(MockUserSessionNameScreenState.initialName.title)
assertButtonsExists()
let doneButton = app.buttons[VectorL10n.done]
XCTAssertTrue(doneButton.exists)
XCTAssertFalse(doneButton.isEnabled)
@@ -29,6 +30,7 @@ class UserSessionNameUITests: MockScreenTestCase {
func testUserSessionNameEmptyState() {
app.goToScreenWithIdentifier(MockUserSessionNameScreenState.empty.title)
assertButtonsExists()
let doneButton = app.buttons[VectorL10n.done]
XCTAssertTrue(doneButton.exists)
XCTAssertFalse(doneButton.isEnabled)
@@ -37,8 +39,20 @@ class UserSessionNameUITests: MockScreenTestCase {
func testUserSessionNameChangedState() {
app.goToScreenWithIdentifier(MockUserSessionNameScreenState.changedName.title)
assertButtonsExists()
let doneButton = app.buttons[VectorL10n.done]
XCTAssertTrue(doneButton.exists)
XCTAssertTrue(doneButton.isEnabled)
}
}
private extension UserSessionNameUITests {
func assertButtonsExists() {
let buttons = [VectorL10n.done, VectorL10n.cancel, "LearnMore"]
for buttonId in buttons {
let button = app.buttons[buttonId]
XCTAssertTrue(button.exists)
}
}
}
@@ -15,7 +15,6 @@
//
import XCTest
@testable import RiotSwiftUI
class UserSessionNameViewModelTests: XCTestCase {
@@ -48,4 +47,38 @@ class UserSessionNameViewModelTests: XCTestCase {
// Then the done button should be enabled.
XCTAssertTrue(context.viewState.canUpdateName, "The done button should be enabled when the name has been changed.")
}
func testCancelIsCalled() {
viewModel.completion = { result in
guard case .cancel = result else {
XCTFail()
return
}
}
viewModel.context.send(viewAction: .cancel)
}
func testLearnMoreIsCalled() {
viewModel.completion = { result in
guard case .learnMore = result else {
XCTFail()
return
}
}
viewModel.context.send(viewAction: .learnMore)
}
func testUpdateNameIsCalled() {
viewModel.completion = { result in
guard case let .updateName(name) = result else {
XCTFail()
return
}
XCTAssertEqual(name, "Element Mobile: iOS")
}
viewModel.context.send(viewAction: .done)
}
}
@@ -42,6 +42,7 @@ struct UserSessionName: View {
viewModel.send(viewAction: .learnMore)
}
.foregroundColor(theme.colors.secondaryContent)
.accessibility(identifier: "LearnMore")
}
}
@@ -28,19 +28,12 @@ protocol UserSessionsOverviewServiceProtocol {
var overviewDataPublisher: CurrentValueSubject<UserSessionsOverviewData, Never> { get }
func updateOverviewData(completion: @escaping (Result<UserSessionsOverviewData, Error>) -> Void) -> Void
func sessionForIdentifier(_ sessionId: String) -> UserSessionInfo?
}
extension UserSessionsOverviewServiceProtocol {
/// The user's current session.
var currentSession: UserSessionInfo? { overviewDataPublisher.value.currentSession }
/// Any unverified sessions on the user's account.
var unverifiedSessions: [UserSessionInfo] { overviewDataPublisher.value.unverifiedSessions }
/// Any inactive sessions on the user's account (not seen for a while).
var inactiveSessions: [UserSessionInfo] { overviewDataPublisher.value.inactiveSessions }
/// Any sessions that are verified and have been seen recently.
var otherSessions: [UserSessionInfo] { overviewDataPublisher.value.otherSessions }
/// Whether it is possible to link a new device via a QR code.
var linkDeviceEnabled: Bool { overviewDataPublisher.value.linkDeviceEnabled }
}
@@ -23,8 +23,6 @@ class UserSessionsOverviewUITests: MockScreenTestCase {
XCTAssertTrue(app.buttons["userSessionCardVerifyButton"].exists)
XCTAssertTrue(app.staticTexts["userSessionCardViewDetails"].exists)
verifyLinkDeviceButtonStatus(true)
}
func testCurrentSessionVerified() {
@@ -33,7 +31,6 @@ class UserSessionsOverviewUITests: MockScreenTestCase {
XCTAssertTrue(app.staticTexts["userSessionCardViewDetails"].exists)
app.buttons["MoreOptionsMenu"].tap()
XCTAssertTrue(app.buttons["Sign out of all other sessions"].exists)
verifyLinkDeviceButtonStatus(true)
}
func testOnlyUnverifiedSessions() {
@@ -41,8 +38,6 @@ class UserSessionsOverviewUITests: MockScreenTestCase {
XCTAssertTrue(app.staticTexts["userSessionsOverviewSecurityRecommendationsSection"].exists)
XCTAssertTrue(app.staticTexts["userSessionsOverviewOtherSection"].exists)
verifyLinkDeviceButtonStatus(false)
}
func testOnlyInactiveSessions() {
@@ -50,8 +45,6 @@ class UserSessionsOverviewUITests: MockScreenTestCase {
XCTAssertTrue(app.staticTexts["userSessionsOverviewSecurityRecommendationsSection"].exists)
XCTAssertTrue(app.staticTexts["userSessionsOverviewOtherSection"].exists)
verifyLinkDeviceButtonStatus(false)
}
func testNoOtherSessions() {
@@ -61,18 +54,6 @@ class UserSessionsOverviewUITests: MockScreenTestCase {
XCTAssertFalse(app.staticTexts["userSessionsOverviewOtherSection"].exists)
app.buttons["MoreOptionsMenu"].tap()
XCTAssertFalse(app.buttons["Sign out of all other sessions"].exists)
verifyLinkDeviceButtonStatus(false)
}
func verifyLinkDeviceButtonStatus(_ enabled: Bool) {
// if enabled {
// let linkDeviceButton = app.buttons["linkDeviceButton"]
// XCTAssertTrue(linkDeviceButton.exists)
// XCTAssertTrue(linkDeviceButton.isEnabled)
// } else {
// let linkDeviceButton = app.buttons["linkDeviceButton"]
// XCTAssertFalse(linkDeviceButton.exists)
// }
}
func testWhenMoreThan5OtherSessionsThenViewAllButtonVisible() {