Skip to content

Commit

Permalink
feat: more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillzyusko committed Mar 29, 2024
1 parent 36c9ca4 commit 5f4c76a
Showing 1 changed file with 112 additions and 26 deletions.
138 changes: 112 additions & 26 deletions ios/Tests/TestsTests/TestsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,29 @@
@testable import Tests
import XCTest

extension XCTestCase {
func waitForFocusChange(to textField: TestableTextField, timeout: TimeInterval = 10.0, file: StaticString = #file, line: UInt = #line) {
let expectation = XCTestExpectation(description: "Wait for focus change to \(textField.tag)")

XCTAssertFalse(
textField.becomeFirstResponderCalled,
"Expected focus shouldn't be initially set for tag \(textField.tag)"
)

DispatchQueue.main.async {
XCTAssertTrue(
textField.becomeFirstResponderCalled,
"Expected focus to be set to text field with tag \(textField.tag)",
file: file,
line: line
)
expectation.fulfill()
}

wait(for: [expectation], timeout: timeout)
}
}

class TestableTextField: UITextField {
var becomeFirstResponderCalled = false

Expand All @@ -27,46 +50,109 @@ class TestableTextView: UITextView {
}

final class TestsTests: XCTestCase {
var rootView: UIView!
var textFields: [TestableTextField]!

override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
super.setUp()
rootView = UIView()
textFields = (1 ... 13).map { id in
let textField = TestableTextField()
textField.tag = id
textField.isEnabled = id != 3 && id != 4 // Assuming ids 3 and 4 are not editable, similar to our Android test
return textField
}

let subView = UIView()
for (index, textField) in textFields.enumerated() {
if index == 4 {
rootView.addSubview(subView)
}
if index >= 4, index <= 6 {
subView.addSubview(textField)
} else {
rootView.addSubview(textField)
}
}
}

override func tearDownWithError() throws {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}

func testExample() throws {
// Setup
let rootView = UIView()
let firstTextInput = TestableTextField()
firstTextInput.isEnabled = true
let secondTextInput = TestableTextView()
secondTextInput.isEditable = true
let nonTextInput = UIView()
rootView.addSubview(firstTextInput)
rootView.addSubview(nonTextInput) // Non-text input view
rootView.addSubview(secondTextInput)
func testSetFocusToNextShouldSetFocusToNextField() throws {
let textInput1 = textFields[0]
FocusedInputHolder.shared.set(textInput1)

FocusedInputHolder.shared.set(firstTextInput)
ViewHierarchyNavigator.setFocusTo(direction: "next")

XCTAssertFalse(
secondTextInput.becomeFirstResponderCalled,
"The focus should be set to the second text input"
)
waitForFocusChange(to: textFields[1])
}

func testSetFocusToPrevShouldSetFocusToPreviousField() throws {
let textInput2 = textFields[1]
FocusedInputHolder.shared.set(textInput2)

ViewHierarchyNavigator.setFocusTo(direction: "prev")

waitForFocusChange(to: textFields[0])
}

func testSetFocusToNextShouldSkipNonEditableFields() throws {
let textInput2 = textFields[1]
FocusedInputHolder.shared.set(textInput2)

ViewHierarchyNavigator.setFocusTo(direction: "next")

let expectation = XCTestExpectation(description: "Wait for next focus change")
DispatchQueue.main.async {
XCTAssertTrue(
secondTextInput.becomeFirstResponderCalled,
"The focus should be set to the second text input"
)
expectation.fulfill()
}
wait(for: [expectation], timeout: 10.0)
waitForFocusChange(to: textFields[4])
}

func testSetFocusToPrevShouldSkipNonEditableFields() throws {
let textInput5 = textFields[4]
FocusedInputHolder.shared.set(textInput5)

ViewHierarchyNavigator.setFocusTo(direction: "prev")

waitForFocusChange(to: textFields[1])
}

func testSetFocusToNextWithinGroup() throws {
let textInput5 = textFields[4]
FocusedInputHolder.shared.set(textInput5)

ViewHierarchyNavigator.setFocusTo(direction: "next")

waitForFocusChange(to: textFields[5])
}

func testSetFocusToPrevWithinGroup() throws {
let textInput6 = textFields[5]
FocusedInputHolder.shared.set(textInput6)

ViewHierarchyNavigator.setFocusTo(direction: "prev")

waitForFocusChange(to: textFields[4])
}

func testSetFocusToNextExitsGroup() throws {
let textInput7 = textFields[6]
FocusedInputHolder.shared.set(textInput7)

ViewHierarchyNavigator.setFocusTo(direction: "next")

waitForFocusChange(to: textFields[7])
}

func testSetFocusToPrevEntersGroupAtLastElement() throws {
let textInput8 = textFields[7]
FocusedInputHolder.shared.set(textInput8)

ViewHierarchyNavigator.setFocusTo(direction: "prev")

waitForFocusChange(to: textFields[6])
}

// TODO: get all, testSetFocusToNextDoesNothingIfLastElement, testSetFocusToPrevDoesNothingIfFirstElement
func testPerformanceExample() throws {
// This is an example of a performance test case.
measure {
Expand Down

0 comments on commit 5f4c76a

Please sign in to comment.