1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import SwiftUI
import UIKit
import XCTest
@testable import OldFella
@MainActor
final class ComposerEditorTests: XCTestCase {
private final class EditingTextView: UITextView {
var isEditing = true
var isComposing = false
override var isFirstResponder: Bool { isEditing }
override var markedTextRange: UITextRange? { isComposing ? selectedTextRange : nil }
}
private final class CategoryTextView: UITextView {
var category = UIContentSizeCategory.large
override var traitCollection: UITraitCollection {
UITraitCollection(preferredContentSizeCategory: category)
}
}
private final class AttributeEdits: NSObject, NSTextStorageDelegate {
var count = 0
func textStorage(
_ textStorage: NSTextStorage, didProcessEditing editedMask: NSTextStorage.EditActions,
range editedRange: NSRange, changeInLength delta: Int
) {
if editedMask.contains(.editedAttributes) { count += 1 }
}
}
private func coordinator(isEnabled: Bool = true) -> ComposerTextEditor.Coordinator {
ComposerTextEditor.Coordinator(
parent: ComposerTextEditor(
text: .constant(""), selection: .constant(NSRange(location: 0, length: 0)),
isFocused: .constant(false), isEnabled: isEnabled, onMentionChange: { _ in }
))
}
func testDisabledEditorRejectsTypingDuringDraftWrites() {
let view = UITextView()
XCTAssertFalse(
coordinator(isEnabled: false).textView(
view, shouldChangeTextIn: NSRange(location: 0, length: 0), replacementText: "More text"))
XCTAssertTrue(
coordinator().textView(
view, shouldChangeTextIn: NSRange(location: 0, length: 0), replacementText: "More text"))
}
func testEmojiSuggestionsFollowCaretAndHideForSelectionsAndBlur() {
var emoji: ComposerEmoji?
var mention: ComposerMention?
let coordinator = ComposerTextEditor.Coordinator(
parent: ComposerTextEditor(
text: .constant(""), selection: .constant(NSRange(location: 0, length: 0)),
isFocused: .constant(true), isEnabled: true,
onMentionChange: { mention = $0 }, onEmojiChange: { emoji = $0 }
))
let view = EditingTextView()
view.text = "Hello :eyes and @ma"
view.selectedRange = NSRange(location: "Hello :ey".utf16.count, length: 0)
coordinator.textViewDidChangeSelection(view)
XCTAssertEqual(emoji?.query, "ey")
XCTAssertNil(mention)
view.selectedRange = NSRange(location: "Hello :ey".utf16.count, length: 1)
coordinator.textViewDidChangeSelection(view)
XCTAssertNil(emoji)
view.selectedRange = NSRange(location: view.text.utf16.count, length: 0)
coordinator.textViewDidChangeSelection(view)
XCTAssertEqual(mention?.query, "ma")
XCTAssertNil(emoji)
coordinator.textViewDidEndEditing(view)
XCTAssertNil(mention)
XCTAssertNil(emoji)
}
func testEmojiSuggestionsWaitForMarkedTextAndHideWhenDisabled() {
var emoji: ComposerEmoji?
let coordinator = ComposerTextEditor.Coordinator(
parent: ComposerTextEditor(
text: .constant(""), selection: .constant(NSRange(location: 0, length: 0)),
isFocused: .constant(true), isEnabled: true,
onMentionChange: { _ in }, onEmojiChange: { emoji = $0 }
))
let view = EditingTextView()
view.text = ":eyes"
view.selectedRange = NSRange(location: view.text.utf16.count, length: 0)
view.isComposing = true
coordinator.textViewDidChange(view)
XCTAssertNil(emoji)
view.isComposing = false
coordinator.textViewDidChange(view)
XCTAssertEqual(emoji?.query, "eyes")
coordinator.parent.isEnabled = false
coordinator.textViewDidChangeSelection(view)
XCTAssertNil(emoji)
}
func testHighlightPreservesTextAndSelectionWithUnicode() {
let text = "🌤️ @mae.example https://example.com #sky"
let view = UITextView()
view.text = text
let selection = NSRange(location: 4, length: 3)
view.selectedRange = selection
coordinator().highlight(view)
XCTAssertEqual(view.text, text)
XCTAssertEqual(view.selectedRange, selection)
for word in ["@mae.example", "https://example.com", "#sky"] {
let range = (text as NSString).range(of: word)
XCTAssertEqual(
view.textStorage.attribute(.underlineStyle, at: range.location, effectiveRange: nil) as? Int,
NSUnderlineStyle.single.rawValue)
}
XCTAssertNil(view.textStorage.attribute(.underlineStyle, at: 0, effectiveRange: nil))
XCTAssertNil(view.typingAttributes[.underlineStyle])
}
func testEditingAwayFacetClearsHighlight() {
let view = UITextView()
view.text = "#sky"
let coordinator = coordinator()
coordinator.highlight(view)
view.textStorage.replaceCharacters(in: NSRange(location: 0, length: 1), with: " ")
coordinator.highlight(view)
XCTAssertEqual(view.text, " sky")
XCTAssertNil(view.textStorage.attribute(.underlineStyle, at: 1, effectiveRange: nil))
}
func testSelectionAndViewUpdatesDoNotRewriteHighlight() {
let view = UITextView()
view.text = "Hello @mae.example"
let edits = AttributeEdits()
view.textStorage.delegate = edits
let coordinator = coordinator()
coordinator.textViewDidChange(view)
let initialEdits = edits.count
XCTAssertGreaterThan(initialEdits, 0)
view.selectedRange = NSRange(location: 8, length: 0)
coordinator.highlight(view)
coordinator.highlight(view)
XCTAssertEqual(edits.count, initialEdits)
XCTAssertNil(view.typingAttributes[.underlineStyle])
}
func testTintChangeUpdatesHighlight() throws {
let view = UITextView()
view.text = "#sky"
view.tintColor = .red
let coordinator = coordinator()
coordinator.highlight(view)
view.tintColor = .blue
coordinator.highlight(view)
let color = try XCTUnwrap(view.textStorage.attribute(.foregroundColor, at: 0, effectiveRange: nil) as? UIColor)
XCTAssertEqual(color, .blue)
}
func testDynamicTypeChangeUpdatesHighlightFont() throws {
let view = CategoryTextView()
view.text = "A draft."
let coordinator = coordinator()
coordinator.highlight(view)
let initialFont = try XCTUnwrap(view.textStorage.attribute(.font, at: 0, effectiveRange: nil) as? UIFont)
view.category = .accessibilityExtraExtraExtraLarge
coordinator.highlight(view)
let updatedFont = try XCTUnwrap(view.textStorage.attribute(.font, at: 0, effectiveRange: nil) as? UIFont)
XCTAssertGreaterThan(updatedFont.pointSize, initialFont.pointSize)
}
func testAnotherEditorWithTheSameTextReceivesHighlight() {
let first = UITextView()
first.text = "#sky"
let second = UITextView()
second.text = first.text
let coordinator = coordinator()
coordinator.highlight(first)
coordinator.highlight(second)
XCTAssertEqual(
second.textStorage.attribute(.underlineStyle, at: 0, effectiveRange: nil) as? Int,
NSUnderlineStyle.single.rawValue)
}
func testEditingTheSameTextRepairsPastedAttributes() {
let view = UITextView()
view.text = "#sky"
let coordinator = coordinator()
coordinator.highlight(view)
view.textStorage.setAttributes([.foregroundColor: UIColor.green], range: NSRange(location: 0, length: 4))
coordinator.textViewDidChange(view)
XCTAssertEqual(
view.textStorage.attribute(.underlineStyle, at: 0, effectiveRange: nil) as? Int,
NSUnderlineStyle.single.rawValue)
}
}