import XCTest /// Selecting text across blocks with the native gestures: a double tap /// selects a word, then dragging a selection handle extends it into the /// next block. Only touch gestures go through the selection handles, so /// desktop end-to-end tests cannot cover this. final class SelectionTests: XCTestCase { let app = XCUIApplication() /// How long the first tap gets to focus the paragraph before a retry. /// Focus can take over 3 seconds to show when the tap lands, and a /// retry on a paragraph that is about to focus opens the edit menu, so /// retry only once the tap has clearly been dropped. static let focusRetryTimeout: TimeInterval = 10 /// How long the keyboard and the canvas get to settle after focus. static let settleTimeout: TimeInterval = 5 /// The pause between two reads of a frame while it settles. static let settleInterval: useconds_t = 250_000 var web: XCUIElement { app.webViews.firstMatch } var editor: Editor { Editor( app: app ) } override func setUpWithError() throws { continueAfterFailure = false // The first synthesized touch of a run can lose its lift inside the // simulator. Spend it on the status bar of the home screen. let springboard = XCUIApplication( bundleIdentifier: "com.apple.springboard" ) springboard.coordinate( withNormalizedOffset: CGVector( dx: 0.5, dy: 0.01 ) ).tap() } override func tearDownWithError() throws { app.terminate() } func point( _ x: CGFloat, _ y: CGFloat ) -> XCUICoordinate { app.coordinate( withNormalizedOffset: .zero ).withOffset( CGVector( dx: x, dy: y ) ) } /// The element's frame once two reads in a row agree. The keyboard and /// its accessory bar slide in after focus and move the canvas, so an /// early read points at where the element was, e.g. on the accessory bar. func stableFrame( of element: XCUIElement ) -> CGRect { let deadline = Date( timeIntervalSinceNow: Self.settleTimeout ) var previous = element.frame usleep( Self.settleInterval ) var current = element.frame while current != previous && Date() < deadline { previous = current usleep( Self.settleInterval ) current = element.frame } return current } /// Double taps the word at this point and waits for the edit menu of /// its selection. The simulator now and then delivers the double tap as /// two single taps: the second lands on the caret the first placed and /// opens the Paste, Select, Select All menu. Its Select selects the word /// at the caret. func selectWord( at coordinate: XCUICoordinate, file: StaticString = #filePath, line: UInt = #line ) { coordinate.doubleTap() let cut = app.menuItems[ "Cut" ] let select = app.menuItems[ "Select" ] if !cut.waitForExistence( timeout: 5 ) && select.exists { select.tap() } XCTAssertTrue( cut.waitForExistence( timeout: 5 ), "The double tap did not select a word", file: file, line: line ) } func testHandleExtendsSelectionIntoPreviousParagraph() throws { // A new post starting with this content: run.sh passes the content // argument of post-new.php through unescaped. let base = ProcessInfo.processInfo.environment[ "WP_BASE_URL" ] ?? "http://127.0.0.1:9400" var url = URLComponents( string: base + "/wp-admin/post-new.php" )! url.queryItems = [ URLQueryItem( name: "content", value: "
Alpha bravo charlie
Delta echo foxtrot
" ) ] app.launchEnvironment[ "START_URL" ] = url.url!.absoluteString app.launch() let second = editor.block( title: "Paragraph", content: "Delta echo foxtrot" ) XCTAssertTrue( second.waitForExistence( timeout: 240 ), "The post did not load" ) // Select the second paragraph, with the caret at the end of its line: // a tap on the caret opens the edit menu instead of selecting a word. // The simulator now and then drops a tap, like the first one of a // run, so tap again if focus does not show. let lineEnd = second.coordinate( withNormalizedOffset: CGVector( dx: 0.9, dy: 0.5 ) ) lineEnd.tap() let focused = web.textViews.matching( NSPredicate( format: "hasKeyboardFocus == true" ) ).firstMatch if !focused.waitForExistence( timeout: Self.focusRetryTimeout ) { lineEnd.tap() } XCTAssertTrue( focused.waitForExistence( timeout: 10 ), "The tap did not focus the paragraph" ) // Focus that shows long after the tap no longer counts as the // tap's, and WebKit then focuses the paragraph without the // keyboard. The next tap brings the keyboard up without moving the // caret, so spend a tap on it here, not the double tap below. let keyboard = app.keyboards.firstMatch if !keyboard.waitForExistence( timeout: Self.settleTimeout ) { lineEnd.tap() } XCTAssertTrue( keyboard.waitForExistence( timeout: Self.settleTimeout ), "No software keyboard" ) // Wait for the canvas to stop moving under the keyboard, so the // frames below are where the words are now. let target = stableFrame( of: second ) // The emphasis makes "bravo" a text element of its own, with its // own frame. let bravo = web.staticTexts[ "bravo" ].frame // Select the first word, "Delta". selectWord( at: point( target.minX + 15, target.midY ) ) let cut = app.menuItems[ "Cut" ] // The start handle's knob sits above the line, at the start of the // word. The selection moves by as much as the finger does, so drag // the knob by the distance from the start of "Delta" to the start of // "bravo". let knob = target.minY - 6 point( target.minX + 1, knob ).press( forDuration: 0.5, thenDragTo: point( bravo.minX + 1, knob - ( target.minY - bravo.minY ) ), withVelocity: 60, thenHoldForDuration: 1 ) XCTAssertTrue( cut.waitForExistence( timeout: 5 ), "The drag lost the selection" ) cut.tap() // Cutting a selection across the two paragraphs joins what is left // of them into one. editor.expectBlocks( [ Block( title: "Paragraph", content: "Alpha echo foxtrot" ) ] ) } func testTapFromQuoteIntoHeadingPlacesCaret() throws { let base = ProcessInfo.processInfo.environment[ "WP_BASE_URL" ] ?? "http://127.0.0.1:9400" var url = URLComponents( string: base + "/wp-admin/post-new.php" )! url.queryItems = [ URLQueryItem( name: "content", value: "Quote here
Paragraph here
" ) ] app.launchEnvironment[ "START_URL" ] = url.url!.absoluteString app.launch() let quote = editor.block( title: "Paragraph", content: "Quote here" ) let heading = editor.block( title: "Heading 2", content: "Heading here" ) XCTAssertTrue( heading.waitForExistence( timeout: 240 ), "The post did not load" ) // The heading has siblings, so it is edited through the canvas // wrapper. The paragraph in the quote has none and is edited on // its own. heading.coordinate( withNormalizedOffset: CGVector( dx: 0.9, dy: 0.5 ) ).tap() let keyboard = app.keyboards.firstMatch XCTAssertTrue( keyboard.waitForExistence( timeout: 10 ), "The tap did not open the keyboard" ) quote.coordinate( withNormalizedOffset: CGVector( dx: 0.9, dy: 0.5 ) ).tap() app.typeText( "Y" ) // Tapping the heading moves the caret there. heading.coordinate( withNormalizedOffset: CGVector( dx: 0.02, dy: 0.5 ) ).tap() app.typeText( "X" ) // Accessibility lists the blocks in a varying order, so check each. XCTAssertTrue( editor.block( title: "Heading 2", content: "XHeading here" ).waitForExistence( timeout: 5 ), "The heading did not get the X" ) XCTAssertTrue( editor.block( title: "Paragraph", content: "Quote hereY" ).exists, "The quote did not keep only the Y" ) XCTAssertTrue( editor.block( title: "Paragraph", content: "Paragraph here" ).exists ) XCTAssertEqual( editor.getBlocks().count, 4 ) } func testTapAfterHidingKeyboardPlacesCaret() throws { let base = ProcessInfo.processInfo.environment[ "WP_BASE_URL" ] ?? "http://127.0.0.1:9400" var url = URLComponents( string: base + "/wp-admin/post-new.php" )! url.queryItems = [ URLQueryItem( name: "content", value: "First here
Second here
" ) ] app.launchEnvironment[ "START_URL" ] = url.url!.absoluteString app.launch() let second = editor.block( title: "Paragraph", content: "Second here" ) XCTAssertTrue( second.waitForExistence( timeout: 240 ), "The post did not load" ) second.coordinate( withNormalizedOffset: CGVector( dx: 0.9, dy: 0.5 ) ).tap() let keyboard = app.keyboards.firstMatch XCTAssertTrue( keyboard.waitForExistence( timeout: 10 ), "The tap did not open the keyboard" ) app.toolbars[ "Toolbar" ].buttons[ "Done" ].tap() XCTAssertTrue( keyboard.waitForNonExistence( timeout: 5 ), "Done did not hide the keyboard" ) // The block is still selected, but nothing has focus. second.coordinate( withNormalizedOffset: CGVector( dx: 0.02, dy: 0.5 ) ).tap() XCTAssertTrue( keyboard.waitForExistence( timeout: 10 ), "The tap did not open the keyboard again" ) app.typeText( "X" ) XCTAssertTrue( editor.block( title: "Paragraph", content: "XSecond here" ).waitForExistence( timeout: 5 ), "The paragraph did not get the X at its start" ) XCTAssertTrue( editor.block( title: "Paragraph", content: "First here" ).exists ) XCTAssertEqual( editor.getBlocks().count, 2 ) } func testTapAfterSwipingKeyboardAwayPlacesCaret() throws { let base = ProcessInfo.processInfo.environment[ "WP_BASE_URL" ] ?? "http://127.0.0.1:9400" var url = URLComponents( string: base + "/wp-admin/post-new.php" )! url.queryItems = [ URLQueryItem( name: "content", value: "First here
Second here
" ) ] app.launchEnvironment[ "START_URL" ] = url.url!.absoluteString app.launch() let second = editor.block( title: "Paragraph", content: "Second here" ) XCTAssertTrue( second.waitForExistence( timeout: 240 ), "The post did not load" ) second.coordinate( withNormalizedOffset: CGVector( dx: 0.9, dy: 0.5 ) ).tap() let keyboard = app.keyboards.firstMatch XCTAssertTrue( keyboard.waitForExistence( timeout: 10 ), "The tap did not open the keyboard" ) // Unlike Done, this leaves the canvas focused, with the page blurred. let window = app.windows.firstMatch window.coordinate( withNormalizedOffset: CGVector( dx: 0.5, dy: 0.3 ) ).press( forDuration: 0.05, thenDragTo: window.coordinate( withNormalizedOffset: CGVector( dx: 0.5, dy: 0.98 ) ) ) XCTAssertTrue( keyboard.waitForNonExistence( timeout: 5 ), "The drag did not hide the keyboard" ) second.coordinate( withNormalizedOffset: CGVector( dx: 0.02, dy: 0.5 ) ).tap() XCTAssertTrue( keyboard.waitForExistence( timeout: 10 ), "The tap did not open the keyboard again" ) app.typeText( "X" ) XCTAssertTrue( editor.block( title: "Paragraph", content: "XSecond here" ).waitForExistence( timeout: 5 ), "The paragraph did not get the X at its start" ) XCTAssertTrue( editor.block( title: "Paragraph", content: "First here" ).exists ) XCTAssertEqual( editor.getBlocks().count, 2 ) } /// Drags the canvas in small steps until the element's middle is between /// these heights. func scroll( to element: XCUIElement, between low: CGFloat, and high: CGFloat ) { let start = app.windows.firstMatch.coordinate( withNormalizedOffset: CGVector( dx: 0.04, dy: 0.35 ) ) for _ in 0..<40 { let y = element.frame.midY if y > low && y < high { return } start.press( forDuration: 0.05, thenDragTo: start.withOffset( CGVector( dx: 0, dy: y >= high ? -80 : 80 ) ), withVelocity: .slow, thenHoldForDuration: 0.5 ) } } func testTapIntoQuoteWithCaretOffScreenPlacesCaret() throws { let base = ProcessInfo.processInfo.environment[ "WP_BASE_URL" ] ?? "http://127.0.0.1:9400" var url = URLComponents( string: base + "/wp-admin/post-new.php" )! url.queryItems = [ URLQueryItem( name: "content", value: "Quote here
Filler paragraph 1, long enough to wrap onto a few lines on a phone screen so that the page scrolls.
Filler paragraph 2, long enough to wrap onto a few lines on a phone screen so that the page scrolls.
Filler paragraph 3, long enough to wrap onto a few lines on a phone screen so that the page scrolls.
Filler paragraph 4, long enough to wrap onto a few lines on a phone screen so that the page scrolls.
Filler paragraph 5, long enough to wrap onto a few lines on a phone screen so that the page scrolls.
Filler paragraph 6, long enough to wrap onto a few lines on a phone screen so that the page scrolls.
Filler paragraph 7, long enough to wrap onto a few lines on a phone screen so that the page scrolls.
Filler paragraph 8, long enough to wrap onto a few lines on a phone screen so that the page scrolls.
Far below here
" ) ] app.launchEnvironment[ "START_URL" ] = url.url!.absoluteString app.launch() let quote = editor.block( title: "Paragraph", content: "Quote here" ) let below = editor.block( title: "Paragraph", content: "Far below here" ) XCTAssertTrue( quote.waitForExistence( timeout: 240 ), "The post did not load" ) // The last paragraph has siblings, so it is edited through the // canvas wrapper. The paragraph in the quote has none. scroll( to: below, between: 150, and: 350 ) below.coordinate( withNormalizedOffset: CGVector( dx: 0.9, dy: 0.5 ) ).tap() let keyboard = app.keyboards.firstMatch XCTAssertTrue( keyboard.waitForExistence( timeout: 10 ), "The tap did not open the keyboard" ) app.typeText( "A" ) // Back up to the quote: the caret is now far below, out of view. let caretBlock = editor.block( title: "Paragraph", content: "Far below hereA" ) scroll( to: quote, between: 150, and: 300 ) XCTAssertGreaterThan( stableFrame( of: caretBlock ).minY, app.windows.firstMatch.frame.maxY, "The caret's paragraph is still on screen" ) // The paragraph is wider than its text: the caret goes to the end, // away from the start, where a misplaced caret also lands. quote.coordinate( withNormalizedOffset: CGVector( dx: 0.9, dy: 0.5 ) ).tap() app.typeText( "X" ) XCTAssertTrue( editor.block( title: "Paragraph", content: "Quote hereX" ).waitForExistence( timeout: 5 ), "The quote did not get the X at its end" ) XCTAssertTrue( caretBlock.exists, "The paragraph far below changed" ) } /// Opens the block editor that is not in an iframe, like GutenbergKit's, /// with these blocks. func openStandaloneEditor( content: String ) { let base = ProcessInfo.processInfo.environment[ "WP_BASE_URL" ] ?? "http://127.0.0.1:9400" var url = URLComponents( string: base + "/wp-admin/admin.php" )! url.queryItems = [ URLQueryItem( name: "page", value: "standalone-editor" ), URLQueryItem( name: "content", value: content ), ] app.launchEnvironment[ "START_URL" ] = url.url!.absoluteString app.launch() } func testTapFromHeadingIntoQuoteWithoutIframePlacesCaret() throws { openStandaloneEditor( content: "Quote here
Paragraph here
" ) let quote = editor.block( title: "Paragraph", content: "Quote here" ) let heading = editor.block( title: "Heading 2", content: "Heading here" ) XCTAssertTrue( heading.waitForExistence( timeout: 240 ), "The editor did not load" ) // The heading has siblings, so it is edited through the canvas // wrapper. The paragraph in the quote has none and is edited on // its own. heading.coordinate( withNormalizedOffset: CGVector( dx: 0.9, dy: 0.5 ) ).tap() let keyboard = app.keyboards.firstMatch XCTAssertTrue( keyboard.waitForExistence( timeout: 10 ), "The tap did not open the keyboard" ) quote.coordinate( withNormalizedOffset: CGVector( dx: 0.02, dy: 0.5 ) ).tap() app.typeText( "X" ) XCTAssertTrue( editor.block( title: "Paragraph", content: "XQuote here" ).waitForExistence( timeout: 5 ), "The quote did not get the X" ) XCTAssertTrue( editor.block( title: "Heading 2", content: "Heading here" ).exists, "The heading changed" ) XCTAssertTrue( editor.block( title: "Paragraph", content: "Paragraph here" ).exists ) } func testScrollFromQuoteKeepsSelectionAcrossBlocks() throws { // Enough paragraphs after the heading for the canvas to scroll. let filler = ( 1...20 ).map { "Filler \( $0 )
" }.joined() openStandaloneEditor( content: "Quote here
Alpha bravo