SPB Git

Repository Public

BrowserUITests.swift 11.5 KB 205 lines swift

1import Network2import XCTest34/// End-to-end checks on the simulator. The search test goes through the production SP Search service5/// (real provider results, a few credits per run) — nothing is mocked.6@MainActor7final class BrowserUITests: XCTestCase {8    private var app: XCUIApplication!910    override func setUp() async throws {11        continueAfterFailure = false12        app = XCUIApplication()13        app.launchArguments = ["-SPBDataDir", "temp", "-AppleLanguages", "(en)", "-AppleLocale", "en_US"]14    }1516    /// The provider line is drawn under the results only when the service returned at least one result.17    private func resultsLoaded(timeout: TimeInterval = 30) -> Bool {18        app.staticTexts.matching(NSPredicate(format: "label BEGINSWITH 'SP Search · results retrieved'")).firstMatch.waitForExistence(timeout: timeout)19    }2021    func testSearchThroughTheThreeModesAndOpenAResult() throws {22        app.launch()23        XCTAssertTrue(app.staticTexts["SP Browser"].waitForExistence(timeout: 15), "native start page")24        let startField = app.buttons["start-search-field"]25        XCTAssertTrue(startField.exists)26        XCTAssertEqual(startField.label, "Search the web with SP Search")2728        startField.tap()29        let field = app.textFields["address-field"]30        XCTAssertTrue(field.waitForExistence(timeout: 5))31        field.typeText("Apple Silicon Mac Studio review\n")3233        // Native results page with the three modes.34        XCTAssertTrue(app.buttons["search-tab-web"].waitForExistence(timeout: 10))35        XCTAssertTrue(app.buttons["search-tab-images"].exists && app.buttons["search-tab-videos"].exists)36        XCTAssertTrue(resultsLoaded(), "web results from the real service")37        let results = app.descendants(matching: .any).matching(identifier: "web-result")38        XCTAssertGreaterThanOrEqual(results.count, 3)39        let bar = app.descendants(matching: .any).matching(identifier: "address-bar").firstMatch40        XCTAssertTrue(bar.waitForExistence(timeout: 5))41        XCTAssertEqual(bar.value as? String, "Apple Silicon Mac Studio review", "the address bar shows the query, not an internal address")4243        app.buttons["search-tab-images"].tap()44        XCTAssertTrue(app.buttons["search-tab-images"].isSelected || resultsLoaded())45        XCTAssertTrue(resultsLoaded(), "image results")4647        app.buttons["search-tab-videos"].tap()48        XCTAssertTrue(resultsLoaded(), "video results")4950        // Back to web results, open the first one in the same tab.51        app.buttons["search-tab-web"].tap()52        XCTAssertTrue(resultsLoaded())53        let first = app.descendants(matching: .any).matching(identifier: "web-result").firstMatch54        XCTAssertTrue(first.waitForExistence(timeout: 10))55        // The title sits under the site line, in the upper part of the row.56        first.coordinate(withNormalizedOffset: CGVector(dx: 0.3, dy: 0.36)).tap()57        if !app.buttons["search-tab-web"].waitForNonExistence(timeout: 6) {58            first.coordinate(withNormalizedOffset: CGVector(dx: 0.3, dy: 0.28)).tap()59        }60        XCTAssertTrue(app.webViews.firstMatch.waitForExistence(timeout: 20))61        XCTAssertTrue(app.buttons["search-tab-web"].waitForNonExistence(timeout: 15), "the results page gave way to the web page")6263        // Back returns to the native results, still there.64        let back = app.buttons["Back"]65        XCTAssertTrue(back.isEnabled)66        back.tap()67        XCTAssertTrue(app.buttons["search-tab-web"].waitForExistence(timeout: 10))68        XCTAssertTrue(resultsLoaded(timeout: 15))69    }7071    func testTabOverviewAndPrivateMode() throws {72        app.launch()73        XCTAssertTrue(app.buttons["tabs-button"].waitForExistence(timeout: 15))74        app.buttons["tabs-button"].tap()75        XCTAssertTrue(app.descendants(matching: .any).matching(identifier: "tab-card").firstMatch.waitForExistence(timeout: 5))76        app.buttons["new-tab"].tap()77        XCTAssertTrue(app.buttons["start-search-field"].waitForExistence(timeout: 5))78        app.buttons["tabs-button"].tap()79        XCTAssertEqual(app.descendants(matching: .any).matching(identifier: "tab-card").count, 2)8081        // Private set: empty at first, then a private tab with its explanation.82        app.segmentedControls.buttons["Private"].tap()83        XCTAssertTrue(app.staticTexts["No private tabs"].waitForExistence(timeout: 5))84        app.buttons["new-tab"].tap()85        XCTAssertTrue(app.staticTexts["Private Browsing"].waitForExistence(timeout: 5))86        XCTAssertTrue(app.descendants(matching: .any).matching(identifier: "private-note").firstMatch.exists)8788        // Settings and the account screen render.89        app.buttons["page-menu"].tap()90        app.buttons["Settings…"].tap()91        XCTAssertTrue(app.buttons["settings-account"].waitForExistence(timeout: 5))92        app.buttons["settings-account"].tap()93        XCTAssertTrue(app.buttons["account-sign-in"].waitForExistence(timeout: 5))94        XCTAssertTrue(app.textFields["account-name"].exists)95    }9697    /// Records what the simulator does when asked for a passkey. A full ceremony needs a signed build on a device98    /// whose associated domain is verified, so this only checks that the request reaches the system or reports why not.99    func testPasskeySignInReachesTheSystem() throws {100        app.launchArguments += ["-SPBShow", "account"]101        app.launch()102        let signIn = app.buttons["account-sign-in"]103        XCTAssertTrue(signIn.waitForExistence(timeout: 15))104        signIn.tap()105        let error = app.descendants(matching: .any).matching(identifier: "account-error").firstMatch106        let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")107        let deadline = Date().addingTimeInterval(12)108        var observation = "nothing appeared within 12 s"109        while Date() < deadline {110            if error.exists { observation = "in-app error: " + error.label; break }111            let sheet = springboard.staticTexts.matching(NSPredicate(format: "label CONTAINS[c] 'passkey' OR label CONTAINS[c] 'sign in'")).firstMatch112            if sheet.exists { observation = "system sheet: " + sheet.label; break }113            let inApp = app.staticTexts.matching(NSPredicate(format: "label CONTAINS[c] 'passkey' AND NOT (label CONTAINS[c] 'Sign In with a Passkey') AND NOT (label CONTAINS[c] 'A passkey is created') AND NOT (label CONTAINS[c] 'Create Account') AND NOT (label CONTAINS[c] 'Lost your')")).firstMatch114            if inApp.exists { observation = "sheet text: " + inApp.label; break }115            RunLoop.current.run(until: Date().addingTimeInterval(0.4))116        }117        print("PASSKEY-OBSERVATION: \(observation)")118        XCTAssertTrue(app.state == .runningForeground || app.state == .runningBackground, "the app did not crash")119    }120121    // MARK: AURA (3.0)122123    /// Command Orbit from the bar, Tab Constellation from the tab overview and back, Focus Mode in and out.124    func testOrbitConstellationAndFocusMode() throws {125        app.launch()126        let orbit = app.buttons["orbit-button"]127        XCTAssertTrue(orbit.waitForExistence(timeout: 15))128        orbit.tap()129        XCTAssertTrue(app.buttons["orbit-all"].waitForExistence(timeout: 5), "Orbit opened")130        XCTAssertTrue(app.buttons["orbit-openLocation"].exists && app.buttons["orbit-showFlow"].exists, "the ring comes from the shared catalog")131        // Tapping outside closes it.132        app.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.15)).tap()133        XCTAssertTrue(app.buttons["orbit-all"].waitForNonExistence(timeout: 5))134135        // Constellation is an alternative view of the tab overview; "Done" returns to the grid.136        app.buttons["tabs-button"].tap()137        let card = app.descendants(matching: .any).matching(identifier: "tab-card").firstMatch138        XCTAssertTrue(card.waitForExistence(timeout: 5))139        app.buttons["constellation-button"].tap()140        XCTAssertTrue(app.descendants(matching: .any).matching(identifier: "constellation-card").firstMatch.waitForExistence(timeout: 5))141        app.buttons["constellation-done"].tap()142        XCTAssertTrue(app.buttons["constellation-done"].waitForNonExistence(timeout: 5))143        XCTAssertTrue(card.waitForExistence(timeout: 5), "back on the classic grid")144        app.buttons["tabs-done"].tap()145146        // Focus Mode: the bar gives way to a pill, and comes back at once.147        let bar = app.descendants(matching: .any).matching(identifier: "address-bar").firstMatch148        XCTAssertTrue(bar.waitForExistence(timeout: 5))149        app.buttons["page-menu"].tap()150        app.buttons["Focus Mode"].tap()151        let exit = app.buttons["focus-exit"]152        XCTAssertTrue(exit.waitForExistence(timeout: 5))153        XCTAssertTrue(bar.waitForNonExistence(timeout: 5), "the chrome is hidden in Focus Mode")154        XCTAssertFalse(app.buttons["tabs-button"].exists)155        exit.tap()156        XCTAssertTrue(bar.waitForExistence(timeout: 5))157        XCTAssertTrue(app.buttons["tabs-button"].exists)158    }159160    /// The floating bar contracts while a page is scrolled down and expands on scroll up and on tap.161    /// The page is served from this test process on localhost: nothing leaves the machine.162    func testFloatingBarContractsWhileScrollingAndExpandsBack() throws {163        let listener = try NWListener(using: .tcp, on: .any)164        let ready = expectation(description: "listener")165        listener.stateUpdateHandler = { state in if case .ready = state { ready.fulfill() } }166        listener.newConnectionHandler = { connection in167            connection.start(queue: .global())168            connection.receive(minimumIncompleteLength: 1, maximumLength: 65536) { _, _, _, _ in169                let lines = (1...240).map { "<p>Line \($0) of a long page used to scroll.</p>" }.joined()170                let body = "<!doctype html><html><head><meta name=\"viewport\" content=\"width=device-width\"><title>Long page</title></head><body>\(lines)</body></html>"171                let response = "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\nContent-Length: \(body.utf8.count)\r\nConnection: close\r\n\r\n\(body)"172                connection.send(content: Data(response.utf8), completion: .contentProcessed { _ in connection.cancel() })173            }174        }175        listener.start(queue: .global())176        wait(for: [ready], timeout: 5)177        defer { listener.cancel() }178        let port = try XCTUnwrap(listener.port?.rawValue)179180        app.launchArguments += ["-SPBOpenURLs", "http://127.0.0.1:\(port)/long"]181        app.launch()182        let web = app.webViews.firstMatch183        XCTAssertTrue(web.waitForExistence(timeout: 15))184        XCTAssertTrue(app.staticTexts["Line 1 of a long page used to scroll."].waitForExistence(timeout: 10))185        let bar = app.descendants(matching: .any).matching(identifier: "address-bar").firstMatch186        let pill = app.descendants(matching: .any).matching(identifier: "floating-pill").firstMatch187        XCTAssertTrue(bar.exists && !pill.exists, "the full bar at the top of the page")188189        web.swipeUp()190        XCTAssertTrue(pill.waitForExistence(timeout: 5), "scrolling down contracts the bar to a pill")191        XCTAssertTrue(bar.waitForNonExistence(timeout: 5))192        XCTAssertEqual(pill.value as? String, "127.0.0.1")193194        web.swipeDown(velocity: .slow)195        XCTAssertTrue(bar.waitForExistence(timeout: 5), "scrolling up brings the bar back")196        XCTAssertFalse(pill.exists)197198        web.swipeUp()199        XCTAssertTrue(pill.waitForExistence(timeout: 5))200        pill.tap()201        XCTAssertTrue(bar.waitForExistence(timeout: 5), "a tap expands it")202        XCTAssertTrue(app.buttons["tabs-button"].exists && app.buttons["orbit-button"].exists)203    }204}205
Files main
Menu

Theme