Skip to content

Commit

Permalink
Merge pull request #7065 from vector-im/alfogrillo/increase_dm_code_c…
Browse files Browse the repository at this point in the history
…overage

Increase dm code coverage (PSG-925)
  • Loading branch information
Alfonso Grillo committed Nov 11, 2022
2 parents 3b66145 + f575c97 commit 01326dc
Show file tree
Hide file tree
Showing 17 changed files with 101 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@
// limitations under the License.
//

@testable import RiotSwiftUI
import XCTest
@testable import Element

class UserAgentParserTests: XCTestCase {

func testAndroidUserAgents() throws {
let uaStrings = [
// New User Agent Implementation
Expand Down Expand Up @@ -182,7 +181,7 @@ class UserAgentParserTests: XCTestCase {
"Element/1.9.9; iOS",
"Element/1.9.7 Android",
"some random string",
"Element/1.9.9; iOS ",
"Element/1.9.9; iOS "
]
let userAgents = uaStrings.map { UserAgentParser.parse($0) }

Expand All @@ -200,5 +199,4 @@ class UserAgentParserTests: XCTestCase {

XCTAssertEqual(userAgents, expected)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ enum MockUserOtherSessionsScreenState: MockScreenState, CaseIterable {
// mock that screen.

case all
case none
case inactiveSessions
case unverifiedSessions
case verifiedSessions
Expand All @@ -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.
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ class UserOtherSessionsViewModelTests: XCTestCase {
UserOtherSessionsViewModel(sessionInfos: sessionInfos,
filter: filter,
title: title,
settingService: MockUserSessionSettings())
settingsService: MockUserSessionSettings())
}

private func createUserSessionInfo(sessionId: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,20 @@ 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,
header: filter.userOtherSessionsViewHeader,
emptyItemsTitle: filter.userOtherSessionsViewEmptyResultsTitle,
allItemsSelected: false,
enableSignOutButton: false,
showLocationInfo: settingService.showIPAddressesInSessionsManager))
showLocationInfo: settingsService.showIPAddressesInSessionsManager))
}

// MARK: - Public
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -87,6 +88,7 @@ struct UserOtherSessions: View {
}
.background(theme.colors.background)
}
.accessibilityIdentifier("UserOtherSessions.clearFilter")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
//

import XCTest

@testable import RiotSwiftUI

class UserSessionNameViewModelTests: XCTestCase {
Expand Down Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ struct UserSessionName: View {
viewModel.send(viewAction: .learnMore)
}
.foregroundColor(theme.colors.secondaryContent)
.accessibility(identifier: "LearnMore")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ class UserSessionsOverviewUITests: MockScreenTestCase {

XCTAssertTrue(app.buttons["userSessionCardVerifyButton"].exists)
XCTAssertTrue(app.staticTexts["userSessionCardViewDetails"].exists)

verifyLinkDeviceButtonStatus(true)
}

func testCurrentSessionVerified() {
Expand All @@ -33,25 +31,20 @@ 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() {
app.goToScreenWithIdentifier(MockUserSessionsOverviewScreenState.onlyUnverifiedSessions.title)

XCTAssertTrue(app.staticTexts["userSessionsOverviewSecurityRecommendationsSection"].exists)
XCTAssertTrue(app.staticTexts["userSessionsOverviewOtherSection"].exists)

verifyLinkDeviceButtonStatus(false)
}

func testOnlyInactiveSessions() {
app.goToScreenWithIdentifier(MockUserSessionsOverviewScreenState.onlyInactiveSessions.title)

XCTAssertTrue(app.staticTexts["userSessionsOverviewSecurityRecommendationsSection"].exists)
XCTAssertTrue(app.staticTexts["userSessionsOverviewOtherSection"].exists)

verifyLinkDeviceButtonStatus(false)
}

func testNoOtherSessions() {
Expand All @@ -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() {
Expand Down
6 changes: 6 additions & 0 deletions RiotTests/UserSessionsOverviewServiceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ class UserSessionsOverviewServiceTests: XCTestCase {
}
}

private extension UserSessionsOverviewServiceProtocol {
var unverifiedSessions: [UserSessionInfo] { overviewDataPublisher.value.unverifiedSessions }
var inactiveSessions: [UserSessionInfo] { overviewDataPublisher.value.inactiveSessions }
var linkDeviceEnabled: Bool { overviewDataPublisher.value.linkDeviceEnabled }
}

private class MockUserSessionsDataProvider: UserSessionsDataProviderProtocol {
enum Mode {
case currentSessionUnverified
Expand Down
1 change: 1 addition & 0 deletions changelog.d/pr-7065.change
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve device manager code coverage.

0 comments on commit 01326dc

Please sign in to comment.