import XCTest final class ShareExtensionTests: XCTestCase { private func openShareComposer(modes: [String] = []) -> XCUIApplication { let app = XCUIApplication() app.launchArguments = ["-ui-testing", "-ui-testing-signed-in", "-ui-testing-share-target"] app.launchArguments.append(contentsOf: modes) app.launch() let launch = app.buttons["shareTestLaunch"] XCTAssertTrue(launch.waitForExistence(timeout: 10)) launch.tap() let target = app.cells.matching(NSPredicate(format: "label == %@", "Old Fella")).firstMatch if !target.waitForExistence(timeout: 5) { let more = app.cells.matching(NSPredicate(format: "label == %@", "More")).firstMatch if !more.isHittable, app.scrollViews.firstMatch.exists { app.scrollViews.firstMatch.swipeLeft() } if more.waitForExistence(timeout: 3), more.isHittable { more.tap() } } XCTAssertTrue(target.waitForExistence(timeout: 10)) target.tap() XCTAssertTrue(app.textViews["composeTextField"].firstMatch.waitForExistence(timeout: 10)) XCTAssertTrue(app.buttons["addPhotosButton"].exists) return app } func testPostsSharedLinkFromShareSheet() { let app = openShareComposer() let editor = app.textViews["composeTextField"].firstMatch XCTAssertEqual(editor.value as? String, "https://example.com/shared-article") editor.coordinate(withNormalizedOffset: CGVector(dx: 0.98, dy: 0.05)).tap() editor.typeText(" An example") let post = app.buttons["publishButton"].firstMatch XCTAssertTrue(post.isEnabled) post.tap() let result = app.staticTexts["shareTestResult"] let posted = NSPredicate(format: "label CONTAINS %@", "https://example.com/shared-article An example") expectation(for: posted, evaluatedWith: result) waitForExpectations(timeout: 10) } func testPostsSharedPhotoWithAltText() { let app = openShareComposer(modes: ["-ui-testing-share-photo"]) let photo = app.buttons["Edit alt text for image 1"].firstMatch XCTAssertTrue(photo.waitForExistence(timeout: 5)) photo.tap() let editor = app.textViews["altTextEditor"].firstMatch XCTAssertTrue(editor.waitForExistence(timeout: 5)) XCTAssertTrue(app.images["Photo"].exists) XCTAssertTrue(editor.isHittable) editor.tap() editor.typeText("A mountain at sunrise") app.buttons["Save"].tap() XCTAssertTrue(photo.waitForExistence(timeout: 5)) photo.tap() XCTAssertTrue(editor.waitForExistence(timeout: 5)) XCTAssertEqual(editor.value as? String, "A mountain at sunrise") app.buttons["Save"].tap() let post = app.buttons["publishButton"].firstMatch XCTAssertTrue(post.waitForExistence(timeout: 5)) XCTAssertTrue(post.isEnabled) post.tap() let result = app.staticTexts["shareTestResult"] let posted = NSPredicate(format: "label == %@", "Photos: 1. Alt text: A mountain at sunrise") expectation(for: posted, evaluatedWith: result) waitForExpectations(timeout: 10) } func testSharedComposerGeneratesAndReviewsAltText() { let app = openShareComposer(modes: ["-ui-testing-share-photo", "-ui-testing-share-alt-success"]) let photo = app.buttons["editAltText-0"] XCTAssertTrue(photo.waitForExistence(timeout: 5)) XCTAssertFalse(app.staticTexts["Add alt text"].exists) photo.tap() let editor = app.textViews["altTextEditor"].firstMatch XCTAssertTrue(editor.waitForExistence(timeout: 5)) let generate = app.buttons["generateAltText-0"] XCTAssertTrue(generate.waitForExistence(timeout: 5)) generate.tap() expectation(for: NSPredicate(format: "value == %@", "A generated description"), evaluatedWith: editor) waitForExpectations(timeout: 5) app.navigationBars["Alt text editor"].buttons["Cancel"].tap() photo.tap() XCTAssertTrue(editor.waitForExistence(timeout: 5)) XCTAssertNotEqual(editor.value as? String, "A generated description") generate.tap() expectation(for: NSPredicate(format: "value == %@", "A generated description"), evaluatedWith: editor) waitForExpectations(timeout: 5) app.buttons["Save"].tap() photo.tap() XCTAssertTrue(editor.waitForExistence(timeout: 5)) XCTAssertEqual(editor.value as? String, "A generated description") app.buttons["Save"].tap() app.buttons["publishButton"].tap() let result = app.staticTexts["shareTestResult"] let posted = NSPredicate(format: "label == %@", "Photos: 1. Alt text: A generated description") expectation(for: posted, evaluatedWith: result) waitForExpectations(timeout: 10) } func testSharedComposerCanStopGeneratingAltText() { let app = openShareComposer(modes: ["-ui-testing-share-photo", "-ui-testing-share-alt-delayed"]) app.buttons["editAltText-0"].tap() let editor = app.textViews["altTextEditor"].firstMatch XCTAssertTrue(editor.waitForExistence(timeout: 5)) let generate = app.buttons["generateAltText-0"] XCTAssertTrue(generate.waitForExistence(timeout: 5)) generate.tap() let stop = app.buttons["stopGeneratingAltTextButton"] XCTAssertTrue(stop.waitForExistence(timeout: 2)) stop.tap() XCTAssertTrue(generate.waitForExistence(timeout: 2)) XCTAssertNotEqual(editor.value as? String, "A delayed description") app.navigationBars["Alt text editor"].buttons["Cancel"].tap() XCTAssertTrue(app.buttons["publishButton"].isEnabled) } func testPostsSharedVideoWithoutAltText() { let app = openShareComposer(modes: ["-ui-testing-share-video"]) XCTAssertTrue(app.buttons["Remove video"].firstMatch.waitForExistence(timeout: 5)) XCTAssertFalse(app.buttons["Edit alt text for image 1"].exists) let post = app.buttons["publishButton"].firstMatch XCTAssertTrue(post.isEnabled) post.tap() XCTAssertFalse(app.alerts["Add alt text?"].exists) let result = app.staticTexts["shareTestResult"] expectation(for: NSPredicate(format: "label == %@", "Video posted"), evaluatedWith: result) waitForExpectations(timeout: 15) } func testKnownFailureKeepsShareComposerReadyToRetry() { let app = openShareComposer(modes: ["-ui-testing-share-failure"]) let post = app.buttons["publishButton"].firstMatch XCTAssertTrue(post.isEnabled) post.tap() let alert = app.alerts["Post not sent"] XCTAssertTrue(alert.waitForExistence(timeout: 5)) XCTAssertTrue(alert.staticTexts["The post was not sent. Try again."].exists) alert.buttons["OK"].tap() XCTAssertTrue(post.isEnabled) XCTAssertTrue(app.textViews["composeTextField"].exists) } func testUnknownOutcomePreventsRetry() { let app = openShareComposer(modes: ["-ui-testing-share-unknown"]) let post = app.buttons["publishButton"].firstMatch XCTAssertTrue(post.isEnabled) post.tap() let alert = app.alerts["Post status unknown"] XCTAssertTrue(alert.waitForExistence(timeout: 5)) XCTAssertTrue( alert.staticTexts.matching(NSPredicate(format: "label CONTAINS %@", "Bluesky may have received this post")) .firstMatch.exists) alert.buttons["OK"].tap() XCTAssertFalse(post.isEnabled) } }