-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Highlight the text in ValueField when shouldResetOnInsertion is enabled
- Loading branch information
Andrii Chernenko
committed
Sep 11, 2020
1 parent
92a5a34
commit 980fe48
Showing
5 changed files
with
184 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
// | ||
// Copyright © 2020 iZettle. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
import Flow | ||
|
||
@testable import Form | ||
|
||
class ValueFieldTests: XCTestCase { | ||
|
||
lazy var window: UIWindow = { | ||
let window = UIWindow() | ||
window.frame.size = .init(width: 100, height: 100) | ||
window.makeKeyAndVisible() | ||
|
||
return window | ||
}() | ||
|
||
func createField() -> (ValueField<String>, Disposable) { | ||
let field = ValueField(value: "", editor: ValueEditor()) | ||
|
||
// add the field to a window so that it can become first responder | ||
window.embedView(field) | ||
|
||
return (field, Disposer { field.removeFromSuperview() }) | ||
} | ||
|
||
func createFieldAndMakeItFirstResponder( | ||
beforeMakingFieldFirstResponder: @escaping (ValueField<String>) -> Void, | ||
afterMakingFieldFirstResponder: @escaping (ValueField<String>) -> Void, | ||
file: StaticString = #file, | ||
line: UInt = #line | ||
) -> XCTestExpectation { | ||
let (field, bag) = createField() | ||
|
||
beforeMakingFieldFirstResponder(field) | ||
|
||
_ = field.becomeFirstResponder() | ||
let becameFirstResponder = expectation(description: "Responder status changed") | ||
|
||
// the change of first responder may not happen immediately | ||
DispatchQueue.main.async { | ||
XCTAssert(field.isFirstResponder, file: file, line: line) | ||
afterMakingFieldFirstResponder(field) | ||
|
||
bag.dispose() | ||
becameFirstResponder.fulfill() | ||
} | ||
|
||
return becameFirstResponder | ||
} | ||
|
||
func testTextHighlight_whenNotAFirstResponder_isDisabled() { | ||
let (field, _) = createField() | ||
|
||
XCTAssertFalse(field.isFirstResponder) | ||
XCTAssertFalse(field.shouldHighlightText) | ||
|
||
field.shouldResetOnInsertion = true | ||
XCTAssertFalse(field.shouldHighlightText) | ||
} | ||
|
||
func testTextHighlight_whenResetOnInsertionIsDisabled_isDisabled() { | ||
let fieldBecameFirstResponder = createFieldAndMakeItFirstResponder( | ||
beforeMakingFieldFirstResponder: { field in | ||
field.shouldResetOnInsertion = false | ||
XCTAssertFalse(field.shouldHighlightText) | ||
}, | ||
afterMakingFieldFirstResponder: { field in | ||
XCTAssertFalse(field.shouldHighlightText) | ||
} | ||
) | ||
|
||
wait(for: [fieldBecameFirstResponder], timeout: 0.01) | ||
} | ||
|
||
func testTextHighlight_whenIsAFirstResponderAndResetOnInsertionIsEnabled_isEnabled() { | ||
let fieldBecameFirstResponder = createFieldAndMakeItFirstResponder( | ||
beforeMakingFieldFirstResponder: { field in | ||
field.shouldResetOnInsertion = true | ||
}, | ||
afterMakingFieldFirstResponder: { field in | ||
XCTAssert(field.shouldHighlightText) | ||
} | ||
) | ||
|
||
wait(for: [fieldBecameFirstResponder], timeout: 0.01) | ||
} | ||
|
||
func testTextHighlight_afterTextIsInserted_isDisabled() { | ||
let fieldBecameFirstResponder = createFieldAndMakeItFirstResponder( | ||
beforeMakingFieldFirstResponder: { field in | ||
field.shouldResetOnInsertion = true | ||
}, | ||
afterMakingFieldFirstResponder: { field in | ||
field.insertText("foo") | ||
XCTAssertFalse(field.shouldHighlightText) | ||
} | ||
) | ||
|
||
wait(for: [fieldBecameFirstResponder], timeout: 0.01) | ||
} | ||
|
||
func testTextHighlight_afterFieldBecomesAndResignsFirstResponder_isDisabled() { | ||
let fieldResignedFirstResponder = expectation(description: "Field resigned first responder") | ||
|
||
let fieldBecameFirstResponder = createFieldAndMakeItFirstResponder( | ||
beforeMakingFieldFirstResponder: { field in | ||
field.shouldResetOnInsertion = true | ||
}, | ||
afterMakingFieldFirstResponder: { field in | ||
XCTAssert(field.shouldHighlightText) | ||
_ = field.resignFirstResponder() | ||
|
||
DispatchQueue.main.async { | ||
XCTAssertFalse(field.isFirstResponder) | ||
XCTAssertFalse(field.shouldHighlightText) | ||
fieldResignedFirstResponder.fulfill() | ||
} | ||
} | ||
) | ||
|
||
wait(for: [fieldBecameFirstResponder, fieldResignedFirstResponder], timeout: 0.01) | ||
} | ||
|
||
func testTextHighlight_afterTextIsPasted_isDisabled() { | ||
let currentPasteboardText = UIPasteboard.general.string | ||
let setPasteboardText = { UIPasteboard.general.string = $0 } | ||
|
||
let fieldBecameFirstResponder = createFieldAndMakeItFirstResponder( | ||
beforeMakingFieldFirstResponder: { field in | ||
setPasteboardText("foo") | ||
field.shouldResetOnInsertion = true | ||
}, | ||
afterMakingFieldFirstResponder: { field in | ||
field.paste(nil) | ||
XCTAssertFalse(field.shouldHighlightText) | ||
setPasteboardText(currentPasteboardText) | ||
} | ||
) | ||
|
||
wait(for: [fieldBecameFirstResponder], timeout: 0.01) | ||
} | ||
|
||
func testTextHighlight_afterTextIsDeleted_isDisabled() { | ||
let fieldBecameFirstResponder = createFieldAndMakeItFirstResponder( | ||
beforeMakingFieldFirstResponder: { field in | ||
field.insertText("foo") | ||
field.shouldResetOnInsertion = true | ||
}, | ||
afterMakingFieldFirstResponder: { field in | ||
field.deleteBackward() | ||
XCTAssertFalse(field.shouldHighlightText) | ||
} | ||
) | ||
|
||
wait(for: [fieldBecameFirstResponder], timeout: 0.01) | ||
} | ||
} |