~ knotless Search/
↑↓ move↵ open⇧↵ all results

geesawra.industries/old-fella

Old people's social media app

0
1
main
141 lines · 6.3 KB Raw History
import XCTest

final class ProfileEditUITests: XCTestCase {
    func testEditProfileRetainsChangesAfterSaveFailureAndReloadsSavedValues() throws {
        try XCTSkipIf(UIDevice.current.userInterfaceIdiom == .pad, "Requires the iPhone layout")
        let app = XCUIApplication()
        app.launchArguments = ["-ui-testing", "-ui-testing-signed-in", "-ui-testing-profile-save-failure"]
        app.launch()
        app.openSettings()
        let edit = app.buttons["settingsEditProfile"]
        XCTAssertTrue(edit.waitForExistence(timeout: 5))
        XCTAssertTrue(edit.isHittable)
        XCTAssertTrue(edit.label.contains("Demo Account"))
        XCTAssertLessThan(edit.frame.maxY, app.switches["showAvatars"].frame.minY)
        capture(app, named: "Settings account row")
        edit.tap()

        let editor = app.navigationBars["Edit Profile"]
        let form = app.collectionViews["profileEditForm"]
        let name = app.textFields["profileEditDisplayName"]
        XCTAssertTrue(name.waitForExistence(timeout: 5))
        let avatar = app.buttons["profileEditAvatar"]
        XCTAssertTrue(avatar.exists)
        XCTAssertLessThan(avatar.frame.maxX, name.frame.minX)
        XCTAssertEqual(avatar.frame.midY, name.frame.midY, accuracy: 20)
        capture(app, named: "Profile editor")
        let bio = app.textViews["profileEditBio"]
        XCTAssertTrue(bio.exists)
        let bioFrame = bio.frame
        bio.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.7)).press(
            forDuration: 0.05,
            thenDragTo: bio.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.4)),
            withVelocity: .slow,
            thenHoldForDuration: 0)
        XCTAssertEqual(bio.frame.minY, bioFrame.minY, accuracy: 2)
        capture(app, named: "Scrolled bio")
        name.tap()
        name.typeText(String(repeating: XCUIKeyboardKey.delete.rawValue, count: "Demo Account".count) + "New Name")
        XCTAssertTrue(form.exists)
        form.swipeUp()
        let link = form.staticTexts["GitHub"]
        for _ in 0..<3 where !link.exists { form.swipeUp() }
        XCTAssertTrue(link.exists)
        link.tap()
        let title = app.textFields["profileLinkTitle"]
        XCTAssertTrue(title.waitForExistence(timeout: 5))
        title.tap()
        title.typeText(" Projects")
        app.navigationBars["Edit Link"].buttons["Done"].tap()

        let reordered = form.staticTexts["GitHub Projects"]
        let first = form.staticTexts["My Photos!"]
        let editLinks = app.buttons["profileEditReorderLinks"]
        for _ in 0..<3 where !reordered.exists { form.swipeUp() }
        XCTAssertTrue(first.exists && reordered.exists && editLinks.exists)
        editLinks.tap()
        let row = form.cells.containing(.staticText, identifier: "GitHub Projects").firstMatch
        row.coordinate(withNormalizedOffset: CGVector(dx: 0.93, dy: 0.5)).press(
            forDuration: 0.3, thenDragTo: first.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0)))
        XCTAssertLessThan(reordered.frame.midY, first.frame.midY)
        editLinks.tap()

        let save = editor.buttons["Save"]
        XCTAssertTrue(save.waitForExistence(timeout: 5))
        save.tap()
        let failure = app.alerts["Profile Editor"]
        XCTAssertTrue(failure.waitForExistence(timeout: 5))
        failure.buttons["OK"].tap()
        XCTAssertTrue(reordered.exists)
        save.tap()
        XCTAssertTrue(edit.waitForExistence(timeout: 5))
        XCTAssertTrue(edit.label.contains("New Name"))
        edit.tap()
        XCTAssertTrue(name.waitForExistence(timeout: 5))
        XCTAssertEqual(name.value as? String, "New Name")
        for _ in 0..<3 where !reordered.exists { form.swipeUp() }
        XCTAssertTrue(reordered.exists)
        XCTAssertLessThan(reordered.frame.midY, first.frame.midY)
    }

    func testAvatarMenuRemovesPhotoAndAppliesCropFromPhotoPicker() throws {
        try XCTSkipIf(UIDevice.current.userInterfaceIdiom == .pad, "Requires the iPhone layout")
        let app = XCUIApplication()
        app.launchArguments = ["-ui-testing", "-ui-testing-signed-in", "-ui-testing-profile-avatar"]
        app.launch()
        app.openSettings()
        let edit = app.buttons["settingsEditProfile"]
        edit.tap()

        let avatar = app.buttons["profileEditAvatar"]
        XCTAssertTrue(avatar.waitForExistence(timeout: 5))
        avatar.tap()
        let choose = app.buttons["Choose Photo"].firstMatch
        let remove = app.buttons["Remove Photo"].firstMatch
        XCTAssertTrue(choose.waitForExistence(timeout: 5))
        XCTAssertTrue(remove.exists)
        capture(app, named: "Avatar menu")
        remove.tap()
        app.navigationBars["Edit Profile"].buttons["Save"].tap()

        edit.tap()
        XCTAssertTrue(avatar.waitForExistence(timeout: 5))
        avatar.tap()
        XCTAssertTrue(choose.waitForExistence(timeout: 5))
        XCTAssertFalse(remove.exists)
        choose.tap()
        let picker = app.navigationBars["Photos"]
        XCTAssertTrue(picker.waitForExistence(timeout: 5))
        let photo = app.images.matching(identifier: "PXGGridLayout-Info").firstMatch
        XCTAssertTrue(photo.waitForExistence(timeout: 5))
        photo.tap()

        let crop = app.navigationBars["Crop Avatar"]
        XCTAssertTrue(crop.waitForExistence(timeout: 10))
        let cropArea = app.descendants(matching: .any)["avatarCropArea"].firstMatch
        XCTAssertTrue(cropArea.exists)
        XCTAssertEqual(cropArea.frame.width, cropArea.frame.height, accuracy: 1)
        XCTAssertEqual(cropArea.value as? String, "100 percent")
        app.buttons["Rotate"].tap()
        app.buttons["Flip"].tap()
        cropArea.pinch(withScale: 1.6, velocity: 1)
        XCTAssertNotEqual(cropArea.value as? String, "100 percent")
        capture(app, named: "Avatar crop")

        crop.buttons["Use Photo"].tap()
        XCTAssertTrue(avatar.waitForExistence(timeout: 5))
        app.navigationBars["Edit Profile"].buttons["Save"].tap()
        XCTAssertTrue(edit.waitForExistence(timeout: 5))
        edit.tap()
        XCTAssertTrue(avatar.waitForExistence(timeout: 5))
        avatar.tap()
        XCTAssertTrue(remove.waitForExistence(timeout: 5))
    }

    private func capture(_ app: XCUIApplication, named name: String) {
        let screenshot = XCTAttachment(screenshot: app.screenshot())
        screenshot.name = name
        screenshot.lifetime = .keepAlways
        add(screenshot)
    }
}