Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions example/ios/CodepadExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
761780ED2CA45674006654EE /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 761780EC2CA45674006654EE /* AppDelegate.swift */; };
77E4DC2FD1B23B76EB060A44 /* ToolbarApiTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 90EF449DDEB258D79CEA3F22 /* ToolbarApiTests.swift */; };
789D6BA1A8D384C14452E7C6 /* LanguageSwitchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B813E2B1BCDEE1AE37F47D2 /* LanguageSwitchTests.swift */; };
7A3C91E2B04D5F6618C2A9E1 /* PageKeyboardTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E1B8D47C93A2F0A6D4E7B13 /* PageKeyboardTests.swift */; };
81AB9BB82411601600AC10FF /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 81AB9BB72411601600AC10FF /* LaunchScreen.storyboard */; };
828B78BB2272434BF5005EAC /* GapLayoutTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AC3DED79C5D26EBDEBBDB0A6 /* GapLayoutTests.swift */; };
83260D5CF89F1C93CB5B7A0A /* SearchPaintTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B2F036F77C4601D08E5875C5 /* SearchPaintTests.swift */; };
Expand Down Expand Up @@ -70,6 +71,7 @@
5BD68E46112559997B9B140B /* Pods-CodepadTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-CodepadTests.debug.xcconfig"; path = "Target Support Files/Pods-CodepadTests/Pods-CodepadTests.debug.xcconfig"; sourceTree = "<group>"; };
5DBB89C709979FAAE1972B1E /* ExpoModulesProvider.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = ExpoModulesProvider.swift; path = "Pods/Target Support Files/Pods-CodepadExample/ExpoModulesProvider.swift"; sourceTree = "<group>"; };
5DCACB8F33CDC322A6C60F78 /* libPods-CodepadExample.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-CodepadExample.a"; sourceTree = BUILT_PRODUCTS_DIR; };
5E1B8D47C93A2F0A6D4E7B13 /* PageKeyboardTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = PageKeyboardTests.swift; sourceTree = "<group>"; };
6191685A83EE035E525D2D44 /* TextInputTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = TextInputTests.swift; sourceTree = "<group>"; };
6F5E9991D5A1AA0A5BA57DAB /* Pods-CodepadTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-CodepadTests.release.xcconfig"; path = "Target Support Files/Pods-CodepadTests/Pods-CodepadTests.release.xcconfig"; sourceTree = "<group>"; };
7133C4873626B3B59917468F /* RecordingTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = RecordingTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -218,6 +220,7 @@
90EF449DDEB258D79CEA3F22 /* ToolbarApiTests.swift */,
D8C50157F7EEE5B90548A381 /* UndoBridgeTests.swift */,
2FC5A2DC7199A9C215380CB2 /* KeyboardInsetTests.swift */,
5E1B8D47C93A2F0A6D4E7B13 /* PageKeyboardTests.swift */,
7133C4873626B3B59917468F /* RecordingTests.swift */,
4A958039B4E3547886C83EB7 /* InitialTextTests.swift */,
0A9F1DCEAA80D2FE89C5BB26 /* KeyFrameHighlightTests.swift */,
Expand Down Expand Up @@ -521,6 +524,7 @@
77E4DC2FD1B23B76EB060A44 /* ToolbarApiTests.swift in Sources */,
1DECFFDCE384662B9BBE2BF7 /* UndoBridgeTests.swift in Sources */,
140C2EFB0059C4E49233BA8C /* KeyboardInsetTests.swift in Sources */,
7A3C91E2B04D5F6618C2A9E1 /* PageKeyboardTests.swift in Sources */,
52B28AF3AAEE86E988E04DDF /* RecordingTests.swift in Sources */,
8CEE448DF1D2EBEE2DB4BB33 /* InitialTextTests.swift in Sources */,
886FE20130DB50D29808051C /* KeyFrameHighlightTests.swift in Sources */,
Expand Down
11 changes: 6 additions & 5 deletions example/ios/CodepadTests/KeyboardInsetTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ final class KeyboardInsetTests: XCTestCase {
let before = editor.contentOffset.y
XCTAssertEqual(before, editor.contentSize.height - 400, accuracy: 0.5, "scrolled to the end")
// The inset extends the range by its height and the reveal moves the caret row above the keyboard.
editor.applyKeyboardInset(300)
editor.applyKeyboardTop(editor.convert(editor.bounds, to: nil).maxY - 300)
draw(editor)
XCTAssertEqual(editor.contentOffset.y - before, 300, accuracy: 1)
XCTAssertEqual(sizes.count, 1, "content size excludes the keyboard inset")
editor.applyKeyboardInset(0)
editor.applyKeyboardTop(.greatestFiniteMagnitude)
draw(editor)
XCTAssertEqual(editor.contentOffset.y, before, accuracy: 1, "the scroll clamps back to the end")
XCTAssertEqual(sizes.count, 1)
Expand All @@ -55,9 +55,10 @@ final class KeyboardInsetTests: XCTestCase {
func testOverlapIsMeasuredInTheEditorsCoordinates() {
let editor = launch(height: 300)
editor.frame = CGRect(x: 0, y: 0, width: 300, height: 200)
let tracker = KeyboardInsetTracker(view: editor) { _ in }
// A 300 pt keyboard over a 300 pt window: the editor ends 100 pt above the window bottom, so 200 pt overlap.
XCTAssertEqual(tracker.overlap(forKeyboardFrame: CGRect(x: 0, y: 0, width: 300, height: 300)), 200)
XCTAssertEqual(tracker.overlap(forKeyboardFrame: CGRect(x: 0, y: 300, width: 300, height: 300)), 0)
editor.applyKeyboardTop(0)
XCTAssertEqual(editor.keyboardInset, 200)
editor.applyKeyboardTop(300)
XCTAssertEqual(editor.keyboardInset, 0)
}
}
252 changes: 252 additions & 0 deletions example/ios/CodepadTests/PageKeyboardTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
import UIKit
import XCTest

@testable import Codepad

/// A page-mode editor under avoidKeyboard in a UIScrollView, with keyboard tops injected through the tracker's path.
@MainActor
final class PageKeyboardTests: XCTestCase {
private let text = (0..<60).map { "line \($0)" }.joined(separator: "\n")

// The editor 300 pt wide and 1600 pt tall between spacers of above and below pt, in a 300 by 600 page.
private func page(above: CGFloat = 0, below: CGFloat = 1000, sideways: Bool = false) -> (UIScrollView, CodeEditorView) {
let editor = launch(width: 300, height: 600)
let window = editor.window!
let page = UIScrollView(frame: CGRect(x: 0, y: 0, width: 300, height: 600))
// The window can run past the screen, whose safe area would inset the page.
page.contentInsetAdjustmentBehavior = .never
let controller = UIViewController()
controller.view = page
window.rootViewController = controller
var holder: UIView = page
if sideways {
let row = UIScrollView(frame: CGRect(x: 0, y: above, width: 300, height: 1600))
row.contentSize = CGSize(width: 600, height: 1600)
page.addSubview(row)
holder = row
}
editor.frame = CGRect(x: 0, y: sideways ? 0 : above, width: 300, height: 1600)
holder.addSubview(editor)
page.contentSize = CGSize(width: 300, height: above + 1600 + below)
window.layoutIfNeeded()
editor.setScrollEnabled(false)
editor.setAvoidKeyboard(true)
editor.setInitialText(text)
draw(editor)
return (page, editor)
}

// The caret row's window top and bottom in points, from the last published frame.
private func caretRow(_ editor: CodeEditorView) -> (top: CGFloat, bottom: CGFloat) {
guard let caret = editor.latestFrame.caret else {
XCTFail("no caret in the frame")
return (0, 0)
}
let top = editor.convert(CGPoint(x: 0, y: caret.minY + editor.contentOffset.y), to: nil).y
let bottom = editor.convert(CGPoint(x: 0, y: caret.maxY + editor.contentOffset.y), to: nil).y
return (top, bottom)
}

// Puts the caret on the line whose row ends about 150 pt above the page's bottom, and draws.
private func caretLow(_ page: UIScrollView, _ editor: CodeEditorView) {
editor.core.setSelection(anchor: 0, head: 0)
draw(editor)
let row = caretRow(editor)
let pageBottom = page.convert(page.bounds, to: nil).maxY
let line = Int((pageBottom - 150 - row.top) / (row.bottom - row.top))
let offset = (text as NSString).range(of: "line \(line)\n").location
editor.core.setSelection(anchor: offset, head: offset)
draw(editor)
}

func testAKeyboardOverTheCaretScrollsThePageUntilTheRowClearsIt() {
let (page, editor) = page()
editor.focus()
caretLow(page, editor)
let row = caretRow(editor)
let keyboard = (row.bottom - 100).rounded()
editor.applyKeyboardTop(keyboard)
let after = caretRow(editor).bottom
XCTAssertLessThanOrEqual(after, keyboard + 0.5)
XCTAssertGreaterThan(after, keyboard - (row.bottom - row.top))
XCTAssertGreaterThan(page.contentOffset.y, 0, "the page scrolled")
}

func testPageModeLeavesTheKeyboardOutOfTheEditorsInset() {
let (page, editor) = page()
editor.focus()
caretLow(page, editor)
editor.applyKeyboardTop((caretRow(editor).bottom - 100).rounded())
draw(editor)
XCTAssertEqual(editor.keyboardInset, 0)
XCTAssertEqual(editor.contentOffset.y, 0, accuracy: 0.5)
}

func testKeyboardOffsetKeepsTheRowThatFarAboveTheKeyboard() {
let (page, editor) = page()
editor.focus()
editor.setKeyboardOffset(52)
caretLow(page, editor)
let keyboard = (caretRow(editor).bottom - 100).rounded()
editor.applyKeyboardTop(keyboard)
XCTAssertLessThanOrEqual(caretRow(editor).bottom, keyboard - 52 + 0.5)
}

func testAnUnfocusedEditorLeavesThePageAlone() {
let (page, editor) = page()
caretLow(page, editor)
let before = page.contentOffset.y
editor.applyKeyboardTop((caretRow(editor).bottom - 100).rounded())
XCTAssertEqual(page.contentOffset.y, before)
}

func testAHorizontalScrollerBetweenIsSkipped() {
let (page, editor) = page(sideways: true)
editor.focus()
caretLow(page, editor)
let keyboard = (caretRow(editor).bottom - 100).rounded()
editor.applyKeyboardTop(keyboard)
XCTAssertGreaterThan(page.contentOffset.y, 0, "the page scrolled")
XCTAssertLessThanOrEqual(caretRow(editor).bottom, keyboard + 0.5)
}

func testKeyboardOffsetCoversAScrollingEditorThatEndsJustAboveTheKeyboard() {
let editor = launch(width: 300, height: 400)
editor.frame = CGRect(x: 0, y: 0, width: 300, height: 300)
editor.setAvoidKeyboard(true)
editor.setInitialText(text)
editor.setKeyboardOffset(52)
editor.focus()
draw(editor)
let before = editor.keyboardInset
// The editor ends at 300 in a 400 pt window, 20 pt above the keyboard's top.
editor.applyKeyboardTop(320)
XCTAssertEqual(editor.keyboardInset - before, 32, accuracy: 0.5)
}

func testKeyboardOffsetIsLeftOutWhileTheKeyboardRests() {
let editor = launch(width: 300, height: 400)
editor.window?.rootViewController?.additionalSafeAreaInsets.bottom = 34
editor.window?.layoutIfNeeded()
editor.setAvoidKeyboard(true)
// With the keyboard down the guide rests at the bottom, and the inset stays what it was before the offset.
let resting = editor.keyboardInset
editor.setKeyboardOffset(52)
XCTAssertEqual(editor.keyboardInset, resting, accuracy: 0.5)
}

func testTheScrollerShrinkingWhileTheKeyboardIsUpRevealsAgain() {
// The editor ends the page and the page sits at its end, so only room from a smaller scroller lifts the row.
let (page, editor) = page(below: 0)
editor.focus()
page.contentOffset.y = page.contentSize.height - page.bounds.height
// The last line, which the page at its end shows, so the caret move itself scrolls nothing.
editor.core.setSelection(anchor: text.utf16.count, head: text.utf16.count)
draw(editor)
let keyboard = (caretRow(editor).bottom - 100).rounded()
editor.applyKeyboardTop(keyboard)
XCTAssertGreaterThan(caretRow(editor).bottom, keyboard, "the row starts covered")
page.frame.size.height -= 200
XCTAssertLessThanOrEqual(caretRow(editor).bottom, keyboard + 0.5)
}

func testAPageScrolledByHandStaysWhereItIs() {
let (page, editor) = page()
editor.focus()
caretLow(page, editor)
editor.applyKeyboardTop((caretRow(editor).bottom - 100).rounded())
page.contentOffset.y -= 50
let scrolled = page.contentOffset.y
page.layoutIfNeeded()
draw(editor)
XCTAssertEqual(page.contentOffset.y, scrolled)
}

func testAPageThatGainsRoomAfterFocusStillFollowsTheCaret() {
let (page, editor) = page()
// No vertical range at focus: the page fits, as a short exercise does before the keyboard opens.
let full = page.contentSize
page.contentSize = CGSize(width: 300, height: 600)
editor.focus()
draw(editor)
page.contentSize = full
caretLow(page, editor)
let keyboard = (caretRow(editor).bottom - 100).rounded()
editor.applyKeyboardTop(keyboard)
// An arrow key moves the caret, as the keyboard's input does.
let line = (text as NSString).range(of: "line 40\n").location
editor.core.setSelection(anchor: line, head: line)
editor.revealAfterInput()
draw(editor)
XCTAssertLessThanOrEqual(caretRow(editor).bottom, keyboard + 0.5)
}

func testAPageThatFitsAtFocusRevealsOnceItShrinks() {
// No vertical range at focus, until the page gets shorter as a KeyboardAvoidingView makes it.
let (page, editor) = page()
page.contentSize = CGSize(width: 300, height: 600)
editor.focus()
caretLow(page, editor)
let keyboard = (caretRow(editor).bottom - 100).rounded()
editor.applyKeyboardTop(keyboard)
XCTAssertGreaterThan(caretRow(editor).bottom, keyboard, "the row starts covered")
page.frame.size.height -= 200
XCTAssertLessThanOrEqual(caretRow(editor).bottom, keyboard + 0.5)
}

func testTheEditorGrowingAfterAShortRevealShowsTheCaret() {
// The editor ends the page at its end, so the reveal comes up short until the app grows the editor.
let (page, editor) = page(below: 0)
editor.focus()
page.contentOffset.y = page.contentSize.height - page.bounds.height
editor.core.setSelection(anchor: text.utf16.count, head: text.utf16.count)
draw(editor)
let keyboard = (caretRow(editor).bottom - 100).rounded()
editor.applyKeyboardTop(keyboard)
XCTAssertGreaterThan(caretRow(editor).bottom, keyboard, "the row starts covered")
editor.frame.size.height += 150
page.contentSize.height += 150
draw(editor)
XCTAssertLessThanOrEqual(caretRow(editor).bottom, keyboard + 0.5)
}

func testEnterOnTheLastLineShowsTheNewRowOnceTheEditorGrows() {
// Until the app grows the editor, its own reveal scrolls it a row, which the growth then gives back.
let (page, editor) = page(below: 100)
// Sizes the editor to its text, as an app does from onContentSizeChange.
func fit() {
let grow = editor.contentSize.height - editor.frame.height
editor.frame.size.height += grow
page.contentSize.height += grow
draw(editor)
}
fit()
editor.focus()
page.contentOffset.y = page.contentSize.height - page.bounds.height
editor.core.setSelection(anchor: text.utf16.count, head: text.utf16.count)
draw(editor)
let row = caretRow(editor)
let keyboard = (row.bottom + (row.bottom - row.top) / 2).rounded()
editor.applyKeyboardTop(keyboard)
editor.typeText("\n")
draw(editor)
fit()
XCTAssertLessThanOrEqual(caretRow(editor).bottom, keyboard + 0.5)
}

func testThePageGainingRoomAfterTheEditorGrowsShowsTheCaret() {
// The editor takes its new height in one layout and the page's content size follows in a later one.
let (page, editor) = page(below: 0)
editor.focus()
page.contentOffset.y = page.contentSize.height - page.bounds.height
editor.core.setSelection(anchor: text.utf16.count, head: text.utf16.count)
draw(editor)
let keyboard = (caretRow(editor).bottom - 100).rounded()
editor.applyKeyboardTop(keyboard)
editor.frame.size.height += 150
draw(editor)
XCTAssertGreaterThan(caretRow(editor).bottom, keyboard, "still covered with no room")
page.contentSize.height += 150
XCTAssertLessThanOrEqual(caretRow(editor).bottom, keyboard + 0.5)
}
}
5 changes: 5 additions & 0 deletions ios/Codepad/layout/KeyboardInset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ enum KeyboardInset {
static func overlap(viewBottomInWindow: CGFloat, windowHeight: CGFloat, keyboardTop: CGFloat) -> CGFloat {
max(0, viewBottomInWindow - min(keyboardTop, windowHeight))
}

/// Whether a keyboard layout guide's top sits above where it rests with the keyboard down, both in window points.
static func isUp(keyboardTop: CGFloat, restingTop: CGFloat) -> Bool {
keyboardTop < restingTop - 0.5
}
}
Loading