Skip to content

Commit

Permalink
Add UI tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Supereg committed Jun 27, 2024
1 parent 13218c3 commit ca16a2f
Show file tree
Hide file tree
Showing 7 changed files with 357 additions and 31 deletions.
1 change: 1 addition & 0 deletions Sources/SpeziDevices/HealthMeasurements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public class HealthMeasurements {
@MainActor
public func clearStorage() {
storedMeasurements.removeAll()
pendingMeasurements.removeAll()
}

/// Configure the Module.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public struct MeasurementRecordedSheet: View {
}
}
.presentationDetents([dynamicDetent])
.presentationCornerRadius(25)
}


Expand Down
145 changes: 145 additions & 0 deletions Tests/UITests/TestAppUITests/HealthMeasurementsTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
//
// This source file is part of the Stanford Spezi open-source project
//
// SPDX-FileCopyrightText: 2024 Stanford University and the project authors (see CONTRIBUTORS.md)
//
// SPDX-License-Identifier: MIT
//

import XCTest


class HealthMeasurementsTests: XCTestCase {
override func setUpWithError() throws {
try super.setUpWithError()

continueAfterFailure = false
}


@MainActor
func testNoMeasurements() {
let app = XCUIApplication()
app.launch()

XCTAssert(app.buttons["Measurements"].waitForExistence(timeout: 2.0))
app.buttons["Measurements"].tap()

XCTAssert(app.staticTexts["No Samples"].waitForExistence(timeout: 0.5))
app.staticTexts["No Samples"].tap()

XCTAssert(app.navigationBars.buttons["Add Measurement"].exists)
app.navigationBars.buttons["Add Measurement"].tap()

XCTAssert(app.staticTexts["No Pending Measurements"].waitForExistence(timeout: 2.0))
XCTAssert(app.navigationBars.buttons["Dismiss"].exists)
app.navigationBars.buttons["Dismiss"].tap()
}

@MainActor
func testWeightMeasurement() {
let app = XCUIApplication()
app.launch()

XCTAssert(app.buttons["Measurements"].waitForExistence(timeout: 2.0))
app.buttons["Measurements"].tap()

XCTAssert(app.navigationBars.buttons["More"].exists)
app.navigationBars.buttons["More"].tap()
XCTAssert(app.buttons["Simulate Weight"].waitForExistence(timeout: 0.5))
app.buttons["Simulate Weight"].tap()

XCTAssert(app.staticTexts["Measurement Recorded"].waitForExistence(timeout: 2.0))
XCTAssert(app.staticTexts["42 kg"].exists)
XCTAssert(app.staticTexts["179 cm, 23 BMI"].exists)
XCTAssert(app.buttons["Save"].exists)
XCTAssert(app.buttons["Discard"].exists)

app.buttons["Save"].tap()

XCTAssert(app.staticTexts["42 kg"].waitForExistence(timeout: 0.5))
XCTAssert(app.staticTexts["23 count"].exists)
XCTAssert(app.staticTexts["1.79 m"].exists)
XCTAssert(app.staticTexts["Mock Device"].exists)
}

@MainActor
func testBloodPressureMeasurement() {
let app = XCUIApplication()
app.launch()

XCTAssert(app.buttons["Measurements"].waitForExistence(timeout: 2.0))
app.buttons["Measurements"].tap()

XCTAssert(app.navigationBars.buttons["More"].exists)
app.navigationBars.buttons["More"].tap()
XCTAssert(app.buttons["Simulate Blood Pressure"].waitForExistence(timeout: 0.5))
app.buttons["Simulate Blood Pressure"].tap()

XCTAssert(app.staticTexts["Measurement Recorded"].waitForExistence(timeout: 2.0))
XCTAssert(app.staticTexts["103/64 mmHg"].exists)
XCTAssert(app.staticTexts["62 BPM"].exists)
XCTAssert(app.buttons["Save"].exists)
XCTAssert(app.buttons["Discard"].exists)

app.buttons["Save"].tap()

XCTAssert(app.staticTexts["103 mmHg"].waitForExistence(timeout: 0.5))
XCTAssert(app.staticTexts["64 mmHg"].exists)
XCTAssert(app.staticTexts["62 count/min"].exists)
XCTAssert(app.staticTexts["Mock Device"].exists)
}

@MainActor
func testMultiMeasurementsAndDiscarding() throws {
let app = XCUIApplication()
app.launch()

XCTAssert(app.buttons["Measurements"].waitForExistence(timeout: 2.0))
app.buttons["Measurements"].tap()

XCTAssert(app.navigationBars.buttons["More"].exists)
app.navigationBars.buttons["More"].tap()
XCTAssert(app.buttons["Simulate Weight"].waitForExistence(timeout: 0.5))
app.buttons["Simulate Weight"].tap()

XCTAssert(app.navigationBars.buttons["Dismiss"].waitForExistence(timeout: 0.5))
app.navigationBars.buttons["Dismiss"].tap()

XCTAssert(app.navigationBars.buttons["More"].exists)
app.navigationBars.buttons["More"].tap()
XCTAssert(app.buttons["Simulate Blood Pressure"].waitForExistence(timeout: 0.5))
app.buttons["Simulate Blood Pressure"].tap()

XCTAssert(app.staticTexts["Measurement Recorded"].waitForExistence(timeout: 2.0))
XCTAssert(app.staticTexts["103/64 mmHg"].exists)
XCTAssert(app.staticTexts["62 BPM"].exists)
XCTAssert(app.buttons["Save"].exists)
XCTAssert(app.buttons["Discard"].exists)

XCTAssert(app.steppers["Page"].exists)
let page1Value = try XCTUnwrap(app.steppers["Page"].value as? String, "Unexpected value \(String(describing: app.steppers["Page"].value))")
XCTAssertEqual(page1Value, "Page 1 of 2")
app.steppers["Page"].coordinate(withNormalizedOffset: .init(dx: 0.8, dy: 0.5)).tap()
let page2Value = try XCTUnwrap(app.steppers["Page"].value as? String, "Unexpected value \(String(describing: app.steppers["Page"].value))")
XCTAssertEqual(page2Value, "Page 2 of 2")

XCTAssert(app.staticTexts["Measurement Recorded"].waitForExistence(timeout: 2.0))
XCTAssert(app.staticTexts["42 kg"].exists)
XCTAssert(app.staticTexts["179 cm, 23 BMI"].exists)
XCTAssert(app.buttons["Save"].exists)
XCTAssert(app.buttons["Discard"].exists)

XCTAssert(app.navigationBars.buttons["Dismiss"].exists)
app.navigationBars.buttons["Dismiss"].tap()

XCTAssert(app.navigationBars.buttons["Add Measurement"].waitForExistence(timeout: 0.5))
app.navigationBars.buttons["Add Measurement"].tap()

XCTAssert(app.buttons["Discard"].waitForExistence(timeout: 0.5))
app.buttons["Discard"].tap()

XCTAssert(app.staticTexts["42 kg"].waitForExistence(timeout: 2.0))
XCTAssert(app.staticTexts["179 cm, 23 BMI"].exists)
}
}
166 changes: 166 additions & 0 deletions Tests/UITests/TestAppUITests/PairedDevicesTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
//
// This source file is part of the Stanford Spezi open-source project
//
// SPDX-FileCopyrightText: 2024 Stanford University and the project authors (see CONTRIBUTORS.md)
//
// SPDX-License-Identifier: MIT
//

import XCTest
import XCTestExtensions


class PairedDevicesTests: XCTestCase {
override func setUpWithError() throws {
try super.setUpWithError()

continueAfterFailure = false
}


@MainActor
func testTipsView() throws {
let app = XCUIApplication()
app.launchArguments = ["--testTips"]
app.launch()

XCTAssert(app.buttons["Devices"].waitForExistence(timeout: 2.0))
app.buttons["Devices"].tap()


XCTAssert(app.staticTexts["Fully Unpair Device"].waitForExistence(timeout: 0.5))
XCTAssert(app.buttons["Open Settings"].exists)
app.buttons["Open Settings"].tap()

let settingsApp = XCUIApplication(bundleIdentifier: "com.apple.Preferences")
XCTAssertEqual(settingsApp.state, .runningForeground)
}

@MainActor
func testDiscoveringView() throws {
let app = XCUIApplication()
app.launch()

XCTAssert(app.buttons["Devices"].waitForExistence(timeout: 2.0))
app.buttons["Devices"].tap()


XCTAssert(app.staticTexts["No Devices"].exists)
XCTAssert(app.buttons["Pair New Device"].exists)
app.buttons["Pair New Device"].tap()

XCTAssert(app.staticTexts["Discovering"].waitForExistence(timeout: 0.5))
XCTAssert(app.navigationBars.buttons["Dismiss"].exists)
app.navigationBars.buttons["Dismiss"].tap()

XCTAssert(app.staticTexts["No Devices"].waitForExistence(timeout: 0.5))
// TODO: customize the pairing hint!

Check failure on line 57 in Tests/UITests/TestAppUITests/PairedDevicesTests.swift

View workflow job for this annotation

GitHub Actions / SwiftLint / SwiftLint

Todo Violation: TODOs should be resolved (customize the pairing hint!) (todo)
}

@MainActor
func testPairDevice() throws {

Check failure on line 61 in Tests/UITests/TestAppUITests/PairedDevicesTests.swift

View workflow job for this annotation

GitHub Actions / SwiftLint / SwiftLint

Function Body Length Violation: Function body should span 50 lines or less excluding comments and whitespace: currently spans 55 lines (function_body_length)
let app = XCUIApplication()
app.launch()

XCTAssert(app.buttons["Devices"].waitForExistence(timeout: 2.0))
app.buttons["Devices"].tap()

XCTAssert(app.navigationBars.buttons["More"].exists)
app.navigationBars.buttons["More"].tap()

XCTAssert(app.buttons["Discover Device"].waitForExistence(timeout: 0.5))
app.buttons["Discover Device"].tap()

XCTAssert(app.staticTexts["Pair Accessory"].waitForExistence(timeout: 2.0))
XCTAssert(app.staticTexts["Do you want to pair \"Mock Device\" with the TestApp app?"].exists)
XCTAssert(app.buttons["Pair"].exists)
app.buttons["Pair"].tap()

XCTAssert(app.staticTexts["Accessory Paired"].waitForExistence(timeout: 5.0))
XCTAssert(app.staticTexts["\"Mock Device\" was successfully paired with the TestApp app."].exists)
XCTAssert(app.buttons["Done"].exists)
app.buttons["Done"].tap()

XCTAssert(app.buttons["Mock Device, 85 %"].waitForExistence(timeout: 0.5))
app.buttons["Mock Device, 85 %"].tap()

XCTAssert(app.navigationBars.staticTexts["Device Details"].waitForExistence(timeout: 2.0))
XCTAssert(app.buttons["Name, Mock Device"].exists)
XCTAssert(app.staticTexts["Model, MD1"].exists)
XCTAssert(app.staticTexts["Battery, 85 %"].exists)
XCTAssert(app.buttons["Forget This Device"].exists)
XCTAssert(app.staticTexts["Synchronizing ..."].exists) // assert device currently connected

app.buttons["Name, Mock Device"].tap()

XCTAssert(app.textFields["enter device name"].exists)
app.textFields["enter device name"].tap()
app.typeText("2")

app.dismissKeyboard()

XCTAssert(app.navigationBars.buttons["Done"].waitForExistence(timeout: 0.5))
app.navigationBars.buttons["Done"].tap()

XCTAssert(app.staticTexts["Name, Mock Device2"].waitForExistence(timeout: 0.5))
XCTAssert(app.navigationBars.buttons["Devices"].exists)
app.navigationBars.buttons["Devices"].tap()

XCTAssert(app.buttons["Mock Device2, 85 %"].waitForExistence(timeout: 2.0))

XCTAssert(app.navigationBars.buttons["More"].exists)
app.navigationBars.buttons["More"].tap()
XCTAssert(app.buttons["Disconnect"].waitForExistence(timeout: 0.5))
app.buttons["Disconnect"].tap()
sleep(1)

app.buttons["Mock Device2, 85 %"].tap()
XCTAssert(app.navigationBars.buttons["Devices"].waitForExistence(timeout: 2.0))
app.navigationBars.buttons["Devices"].tap()

XCTAssert(app.navigationBars.buttons["More"].exists)
app.navigationBars.buttons["More"].tap()
XCTAssert(app.buttons["Connect"].waitForExistence(timeout: 0.5))
app.buttons["Connect"].tap()
sleep(3)


XCTAssert(app.buttons["Mock Device2, 85 %"].waitForExistence(timeout: 0.5))
app.buttons["Mock Device2, 85 %"].tap()

XCTAssert(app.buttons["Forget This Device"].waitForExistence(timeout: 2.0))
app.buttons["Forget This Device"].tap()

XCTAssert(app.buttons["Forget Device"].waitForExistence(timeout: 2.0))
app.buttons["Forget Device"].tap()


XCTAssert(app.staticTexts["Fully Unpair Device"].waitForExistence(timeout: 2.0))
}

@MainActor
func testPlusButton() {
let app = XCUIApplication()
app.launch()

XCTAssert(app.buttons["Devices"].waitForExistence(timeout: 2.0))
app.buttons["Devices"].tap()

XCTAssert(app.navigationBars.buttons["More"].exists)
app.navigationBars.buttons["More"].tap()

XCTAssert(app.buttons["Discover Device"].waitForExistence(timeout: 0.5))
app.buttons["Discover Device"].tap()

XCTAssert(app.staticTexts["Pair Accessory"].waitForExistence(timeout: 2.0))
XCTAssert(app.buttons["Dismiss"].exists)
app.buttons["Dismiss"].tap()

XCTAssert(app.navigationBars.buttons["Add Device"].exists)
app.navigationBars.buttons["Add Device"].tap()

XCTAssert(app.staticTexts["Pair Accessory"].waitForExistence(timeout: 2.0))
}

// TODO: forget devices test

Check failure on line 165 in Tests/UITests/TestAppUITests/PairedDevicesTests.swift

View workflow job for this annotation

GitHub Actions / SwiftLint / SwiftLint

Todo Violation: TODOs should be resolved (forget devices test) (todo)
}
25 changes: 0 additions & 25 deletions Tests/UITests/TestAppUITests/TestAppUITests.swift

This file was deleted.

Loading

0 comments on commit ca16a2f

Please sign in to comment.