Add utility to run UI tests for Screen states, add screen states for template and finish unit test.

This commit is contained in:
David Langley
2021-09-10 16:43:31 +01:00
parent 59a54654b1
commit 4fb59260d4
15 changed files with 332 additions and 148 deletions

View File

@@ -2,6 +2,8 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>UILaunchScreen</key>
<dict/>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>

View File

@@ -0,0 +1,24 @@
//
// Copyright 2021 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 Foundation
/// The static list of mocked screens in RiotSwiftUI
@available(iOS 14.0, *)
enum MockAppScreens {
static let appScreens = [MockTemplateProfileUserScreenState.self]
}

View File

@@ -1,58 +0,0 @@
//
// Copyright 2021 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 SwiftUI
/*
Used for mocking top level screens and their various state.
*/
@available(iOS 14.0, *)
protocol MockScreen {
associatedtype ScreenType: View
static func screen(for state: Self) -> ScreenType
static var screenStates: [Self] { get }
}
@available(iOS 14.0, *)
extension MockScreen {
/*
Get a list of the screens for every screen state.
*/
static var screens: [ScreenType] {
Self.screenStates.map(screen(for:))
}
/*
Render each of the screen states in a group applying
any optional environment variables.
*/
static func screenGroup(
themeId: ThemeIdentifier = .light,
locale: Locale = Locale.current,
sizeCategory: ContentSizeCategory = ContentSizeCategory.medium
) -> some View {
Group {
ForEach(0..<screens.count) { index in
screens[index]
}
}
.theme(themeId)
.environment(\.locale, locale)
.environment(\.sizeCategory, sizeCategory)
}
}

View File

@@ -0,0 +1,79 @@
//
// Copyright 2021 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 SwiftUI
/// Used for mocking top level screens and their various states.
@available(iOS 14.0, *)
protocol MockScreenState {
static var screenStates: [MockScreenState] { get }
var screenType: Any.Type { get }
var screenView: AnyView { get }
var stateTitle: String { get }
}
@available(iOS 14.0, *)
extension MockScreenState {
/// Get a list of the screens for every screen state.
static var screensViews: [AnyView] {
screenStates.map(\.screenView)
}
/// A unique key to identify each screen state.
static var screenStateKeys: [String] {
return Array(0..<screenStates.count).map(String.init)
}
/// Render each of the screen states in a group applying
/// any optional environment variables.
/// - Parameters:
/// - themeId: id of theme to render the screens with
/// - locale: Locale to render the screens with
/// - sizeCategory: type sizeCategory to render the screens with
/// - Returns: The group of screens
static func screenGroup(
themeId: ThemeIdentifier = .light,
locale: Locale = Locale.current,
sizeCategory: ContentSizeCategory = ContentSizeCategory.medium
) -> some View {
Group {
ForEach(0..<screensViews.count) { index in
screensViews[index]
}
}
.theme(themeId)
.environment(\.locale, locale)
.environment(\.sizeCategory, sizeCategory)
}
/// A title to represent the screen and it's screen state
var screenTitle: String {
"\(String(describing: screenType.self)): \(stateTitle)"
}
/// A title to represent this screen state
var stateTitle: String {
String(describing: self)
}
}
@available(iOS 14.0, *)
extension MockScreenState where Self: CaseIterable {
static var screenStates: [MockScreenState] {
return Array(self.allCases)
}
}

View File

@@ -0,0 +1,49 @@
//
// Copyright 2021 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 SwiftUI
@available(iOS 14.0, *)
struct ScreenList: View {
private var allStates: [MockScreenState]
init(screens: [MockScreenState.Type]) {
self.allStates = screens.flatMap{ $0.screenStates }
}
var body: some View {
NavigationView {
List {
ForEach(0..<allStates.count) { i in
let state = allStates[i]
NavigationLink(destination: state.screenView) {
Text(state.screenTitle)
.accessibilityIdentifier(String(i))
}
}
}
}
.navigationTitle("Screen States")
}
}
@available(iOS 14.0, *)
struct ScreenList_Previews: PreviewProvider {
static var previews: some View {
ScreenList(screens: [MockTemplateProfileUserScreenState.self])
}
}

View File

@@ -0,0 +1,72 @@
//
// Copyright 2021 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
import RiotSwiftUI
/// XCTestCase subclass to easy testing of `MockScreenState`'s.
/// Creates a test case for each screen state, launches the app,
/// goes to the correct screen and
@available(iOS 14.0, *)
class MockScreenTest: XCTestCase {
enum Constants {
static let defaultTimeout: TimeInterval = 3
}
class var screenType: MockScreenState.Type? {
return nil
}
var screenState: MockScreenState?
var screenStateKey: String?
let app = XCUIApplication()
override class var defaultTestSuite: XCTestSuite {
let testSuite = XCTestSuite(name: NSStringFromClass(self))
guard let screenType = screenType else {
return testSuite
}
// Create a test case for each screen state
screenType.screenStates.enumerated().forEach { index, screenState in
let key = screenType.screenStateKeys[index]
addTestFor(screenState: screenState, screenStateKey: key, toTestSuite: testSuite)
}
return testSuite
}
private class func addTestFor(screenState: MockScreenState, screenStateKey: String, toTestSuite testSuite: XCTestSuite) {
testInvocations.forEach { invocation in
let testCase = TestUserProfileUITests(invocation: invocation)
testCase.screenState = screenState
testCase.screenStateKey = screenStateKey
testSuite.addTest(testCase)
}
}
open override func setUpWithError() throws {
// For every test case launch the app and go to the relevant screen
continueAfterFailure = false
app.launch()
goToScreen()
}
private func goToScreen() {
guard let screenKey = screenStateKey else { fatalError("no screen") }
let link = app.buttons[screenKey]
link.tap()
}
}

View File

@@ -1,45 +0,0 @@
//
// Copyright 2021 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 Foundation
import SwiftUI
/**
Using an enum for the screen allows you define the different state cases with
the relevant associated data for each case.
*/
@available(iOS 14.0, *)
enum MockTemplateProfileUserScreenStates: MockScreen {
case mockPresenceStates(TemplateUserProfilePresence)
case mockLongDisplayName(String)
static var screenStates: [MockTemplateProfileUserScreenStates] = TemplateUserProfilePresence.allCases.map(MockTemplateProfileUserScreenStates.mockPresenceStates)
+ [.mockLongDisplayName("Somebody with a super long name we would like to test")]
static func screen(for state: MockTemplateProfileUserScreenStates) -> some View {
let service: MockTemplateUserProfileService
switch state {
case .mockPresenceStates(let presence):
service = MockTemplateUserProfileService(presence: presence)
case .mockLongDisplayName(let displayName):
service = MockTemplateUserProfileService(displayName: displayName)
}
let viewModel = TemplateUserProfileViewModel(userService: service)
return TemplateUserProfile(viewModel: viewModel)
.addDependency(MockAvatarService.example)
}
}

View File

@@ -0,0 +1,60 @@
//
// Copyright 2021 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 Foundation
import SwiftUI
/// Using an enum for the screen allows you define the different state cases with
/// the relevant associated data for each case.
@available(iOS 14.0, *)
enum MockTemplateProfileUserScreenState: MockScreenState, CaseIterable {
// A case for each state you want to represent
// with specific, minimal associated data that will allow you
// mock that screen.
case presence(TemplateUserProfilePresence)
case longDisplayName(String)
/// The associated screen
var screenType: Any.Type {
TemplateUserProfile.self
}
/// A list of screen state definitions
static var allCases: [MockTemplateProfileUserScreenState] {
// Each of the presence statuses
TemplateUserProfilePresence.allCases.map(MockTemplateProfileUserScreenState.presence)
// A long display name
+ [.longDisplayName("Somebody with a super long name we would like to test")]
}
/// Generate the view struct for the screen state.
var screenView: AnyView {
let service: MockTemplateUserProfileService
switch self {
case .presence(let presence):
service = MockTemplateUserProfileService(presence: presence)
case .longDisplayName(let displayName):
service = MockTemplateUserProfileService(displayName: displayName)
}
let viewModel = TemplateUserProfileViewModel(userService: service)
// can simulate service and viewModel actions here if needs be.
return AnyView(TemplateUserProfile(viewModel: viewModel)
.addDependency(MockAvatarService.example))
}
}

View File

@@ -15,33 +15,35 @@
//
import XCTest
@testable import RiotSwiftUI
import RiotSwiftUI
@available(iOS 14.0, *)
class TestUserProfileUITests: XCTestCase {
let app = XCUIApplication()
class TestUserProfileUITests: MockScreenTest {
override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
// In UI tests it is usually best to stop immediately when a failure occurs.
continueAfterFailure = false
// UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
app.launch()
// In UI tests its important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
override class var screenType: MockScreenState.Type {
return MockTemplateProfileUserScreenState.self
}
override func tearDownWithError() throws {
// Put teardown code here. This method is called after the invocation of each test method in the class.
func testTemplateUserProfileScreen() throws {
guard let screenState = screenState as? MockTemplateProfileUserScreenState else { fatalError("no screen") }
switch screenState {
case .presence(let presence):
testTemplateUserProfilePresence(presence: presence)
case .longDisplayName(let name):
testTemplateUserProfileLongName(name: name)
}
}
func testUserContentTextDisplayed() throws {
let userContentText = app.staticTexts["More great user content!"]
XCTAssert(userContentText.exists)
func testTemplateUserProfilePresence(presence: TemplateUserProfilePresence) {
let presenceText = app.staticTexts["presenceText"]
XCTAssert(presenceText.exists)
XCTAssert(presenceText.label == presence.title)
}
func testTemplateUserProfileLongName(name: String) {
let displayNameText = app.staticTexts["displayNameText"]
XCTAssert(displayNameText.exists)
XCTAssert(displayNameText.label == name)
}
}

View File

@@ -38,12 +38,12 @@ class TemplateUserProfileViewModelTests: XCTestCase {
XCTAssertEqual(viewModel.viewState.presence, Constants.presenceInitialValue)
}
func testFirstPresenceRecieved() throws {
func testFirstPresenceReceived() throws {
let presencePublisher = viewModel.$viewState.map(\.presence).removeDuplicates().collect(1).first()
XCTAssertEqual(try xcAwait(presencePublisher), [Constants.presenceInitialValue])
}
func testPresenceUpdatesRecieved() throws {
func testPresenceUpdatesReceived() throws {
let presencePublisher = viewModel.$viewState.map(\.presence).removeDuplicates().collect(3).first()
let newPresenceValue1: TemplateUserProfilePresence = .online
let newPresenceValue2: TemplateUserProfilePresence = .idle

View File

@@ -67,6 +67,6 @@ struct TemplateUserProfile: View {
@available(iOS 14.0, *)
struct TemplateUserProfile_Previews: PreviewProvider {
static var previews: some View {
MockTemplateProfileUserScreenStates.screenGroup()
MockTemplateProfileUserScreenState.screenGroup()
}
}

View File

@@ -38,6 +38,9 @@ struct TemplateUserProfileHeader: View {
VStack(spacing: 8){
Text(displayName ?? "")
.font(theme.fonts.title3)
.accessibility(identifier: "displayNameText")
.padding(.horizontal)
.lineLimit(1)
TemplateUserProfilePresenceView(presence: presence)
}
}

View File

@@ -31,6 +31,7 @@ struct TemplateUserProfilePresenceView: View {
.frame(width: 8, height: 8)
Text(presence.title)
.font(.subheadline)
.accessibilityIdentifier("presenceText")
}
.foregroundColor(foregroundColor)
.padding(0)

View File

@@ -26,7 +26,7 @@ struct RiotSwiftUIApp: App {
}
var body: some Scene {
WindowGroup {
Text("app")
ScreenList(screens: MockAppScreens.appScreens)
}
}
}

View File

@@ -33,30 +33,25 @@ targets:
dependencies:
- target: RiotSwiftUI
# configFiles:
# Debug: Debug.xcconfig
# Release: Release.xcconfig
settings:
base:
TEST_TARGET_NAME: RiotSwiftUI
# PRODUCT_NAME: RiotSwiftUITests
PRODUCT_BUNDLE_IDENTIFIER: org.matrix.RiotSwiftUITests$(rfc1034identifier)
# BUNDLE_LOADER: $(TEST_HOST)
# FRAMEWORK_SEARCH_PATHS: $(SDKROOT)/Developer/Library/Frameworks $(inherited)
# INFOPLIST_FILE: RiotSwiftUI/Info.plist
# LD_RUNPATH_SEARCH_PATHS: $(inherited) @executable_path/Frameworks @loader_path/Frameworks
# PRODUCT_BUNDLE_IDENTIFIER: org.matrix.$(PRODUCT_NAME:rfc1034identifier)
# PRODUCT_NAME: RiotSwiftUITests
# TEST_TARGET_NAME: $(BUILT_PRODUCTS_DIR)/RiotSwiftUI.app/RiotSwiftUI
# configs:
# Debug:
# Release:
# PROVISIONING_PROFILE: $(RIOT_PROVISIONING_PROFILE)
# PROVISIONING_PROFILE_SPECIFIER: $(RIOT_PROVISIONING_PROFILE_SPECIFIER)
sources:
# Source included/excluded here here are similar to RiotSwiftUI as we
# need access to ScreenStates
- path: ../RiotSwiftUI/Modules
includes:
- "**/Test"
excludes:
- "**/MatrixSDK/**"
- "**/Coordinator/**"
- "**/Test/Unit/**"
- path: ../Riot/Generated/Strings.swift
- path: ../Riot/Generated/Images.swift
- path: ../Riot/Managers/Theme/ThemeIdentifier.swift
- path: ../Riot/Managers/Locale/LocaleProviderType.swift
- path: ../Riot/Assets/en.lproj/Vector.strings
buildPhase: resources
- path: ../Riot/Assets/Images.xcassets
buildPhase: resources
- path: ../Riot/Assets/SharedImages.xcassets
buildPhase: resources