Handle tap on user mentions (#1850)

* user mention routing implemented

* more tests

* better naming

* fixed a test
This commit is contained in:
Mauro
2023-10-03 10:11:05 +02:00
committed by GitHub
parent 7124f24d7c
commit 355535cd75
8 changed files with 169 additions and 37 deletions
@@ -108,4 +108,16 @@ class AppRouteURLParserTests: XCTestCase {
// Then the route shouldn't be considered valid and should be ignored.
XCTAssertEqual(route, nil)
}
func testMatrixUserURL() {
let userID = "@test:matrix.org"
guard let url = URL(string: "\(appSettings.permalinkBaseURL)/#/\(userID)") else {
XCTFail("Invalid url")
return
}
let route = appRouteURLParser.route(from: url)
XCTAssertEqual(route, .roomMemberDetails(userID: userID))
}
}
@@ -417,8 +417,40 @@ class AttributedStringBuilderTests: XCTestCase {
let string = "https://matrix.to/#/@test:matrix.org"
let attributedStringFromHTML = attributedStringBuilder.fromHTML(string)
XCTAssertNotNil(attributedStringFromHTML?.attachment)
XCTAssertNotNil(attributedStringFromHTML?.link)
let attributedStringFromPlain = attributedStringBuilder.fromPlain(string)
XCTAssertNotNil(attributedStringFromPlain?.attachment)
XCTAssertNotNil(attributedStringFromHTML?.link)
}
func testUserMentionAtachmentInBlockQuotes() {
let link = "https://matrix.to/#/@test:matrix.org"
let string = "<blockquote>hello \(link) how are you?</blockquote>"
guard let attributedStringFromHTML = attributedStringBuilder.fromHTML(string) else {
XCTFail("Attributed string is nil")
return
}
for run in attributedStringFromHTML.runs {
XCTAssertNotNil(run.blockquote)
}
checkAttachment(attributedString: attributedStringFromHTML, expectedRuns: 3)
checkLinkIn(attributedString: attributedStringFromHTML, expectedLink: link, expectedRuns: 3)
}
func testAllUsersMentionAtachmentInBlockQuotes() {
let string = "<blockquote>hello @room how are you?</blockquote>"
guard let attributedStringFromHTML = attributedStringBuilder.fromHTML(string) else {
XCTFail("Attributed string is nil")
return
}
for run in attributedStringFromHTML.runs {
XCTAssertNotNil(run.blockquote)
}
checkAttachment(attributedString: attributedStringFromHTML, expectedRuns: 3)
}
func testAllUsersMentionAttachment() {
@@ -464,12 +496,65 @@ class AttributedStringBuilderTests: XCTestCase {
XCTAssertNil(attributedStringFromHTML?.link)
}
func testUserMentionIsIgnoredInCode() {
let htmlString = "<pre><code>test https://matrix.org/#/@test:matrix.org test</code></pre>"
let attributedStringFromHTML = attributedStringBuilder.fromHTML(htmlString)
XCTAssert(attributedStringFromHTML?.runs.count == 1)
XCTAssertNil(attributedStringFromHTML?.attachment)
}
func testAllUsersIsIgnoredInCode() {
let htmlString = "<pre><code>test @room test</code></pre>"
let attributedStringFromHTML = attributedStringBuilder.fromHTML(htmlString)
XCTAssert(attributedStringFromHTML?.runs.count == 1)
XCTAssertNil(attributedStringFromHTML?.attachment)
}
func testMultipleMentions() {
guard let url = URL(string: "https://matrix.to/#/@test:matrix.org") else {
XCTFail("Invalid url")
return
}
let string = "Hello @room, but especially hello to you \(url)"
guard let attributedStringFromHTML = attributedStringBuilder.fromHTML(string) else {
XCTFail("Attributed string is nil")
return
}
var foundAttachments = 0
var foundLink: URL?
for run in attributedStringFromHTML.runs {
if run.attachment != nil {
foundAttachments += 1
}
if let link = run.link {
foundLink = link
}
}
XCTAssertEqual(foundLink, url)
XCTAssertEqual(foundAttachments, 2)
guard let attributedStringFromPlain = attributedStringBuilder.fromPlain(string) else {
XCTFail("Attributed string is nil")
return
}
foundAttachments = 0
foundLink = nil
for run in attributedStringFromPlain.runs {
if run.attachment != nil {
foundAttachments += 1
}
if let link = run.link {
foundLink = link
}
}
XCTAssertEqual(foundLink, url)
XCTAssertEqual(foundAttachments, 2)
}
// MARK: - Private
@@ -489,7 +574,7 @@ class AttributedStringBuilderTests: XCTestCase {
XCTFail("Couldn't find expected value.")
}
private func checkAttachment(attributedString: AttributedString?, expectedRuns: Int) {
private func checkAttachment(attributedString: AttributedString?, expectedRuns: Int, expectedAttachments: Int = 1) {
guard let attributedString else {
XCTFail("Could not build the attributed string")
return