|
| 1 | +/* |
| 2 | + MIT License |
| 3 | + |
| 4 | + Copyright (c) 2017 MessageKit |
| 5 | + |
| 6 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + of this software and associated documentation files (the "Software"), to deal |
| 8 | + in the Software without restriction, including without limitation the rights |
| 9 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + copies of the Software, and to permit persons to whom the Software is |
| 11 | + furnished to do so, subject to the following conditions: |
| 12 | + |
| 13 | + The above copyright notice and this permission notice shall be included in all |
| 14 | + copies or substantial portions of the Software. |
| 15 | + |
| 16 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +import Quick |
| 26 | +import Nimble |
| 27 | +@testable import MessageKit |
| 28 | + |
| 29 | +final class MessageLabelSpec: QuickSpec { |
| 30 | + |
| 31 | + //swiftlint:disable function_body_length |
| 32 | + override func spec() { |
| 33 | + |
| 34 | + var messageLabel: MessageLabel! |
| 35 | + |
| 36 | + beforeEach { |
| 37 | + messageLabel = MessageLabel() |
| 38 | + } |
| 39 | + |
| 40 | + describe("text recognized by a DetectorType") { |
| 41 | + context("address detection is enabled") { |
| 42 | + it("applies addressAttributes to text") { |
| 43 | + let expectedColor = UIColor.blue |
| 44 | + messageLabel.addressAttributes = [.foregroundColor: expectedColor] |
| 45 | + messageLabel.text = "One Infinite Loop Cupertino, CA 95014" |
| 46 | + messageLabel.enabledDetectors = [.address] |
| 47 | + var attributes = messageLabel.textAttributes |
| 48 | + var textColor = attributes?[.foregroundColor] as? UIColor |
| 49 | + expect(textColor).toNot(beNil()) |
| 50 | + expect(textColor).to(equal(expectedColor)) |
| 51 | + } |
| 52 | + } |
| 53 | + context("phone number detection is enabled") { |
| 54 | + it("applies phoneNumberAttributes to text") { |
| 55 | + let expectedFont = UIFont.systemFont(ofSize: 8) |
| 56 | + messageLabel.phoneNumberAttributes = [.font: expectedFont] |
| 57 | + messageLabel.text = "1-800-555-1234" |
| 58 | + messageLabel.enabledDetectors = [.phoneNumber] |
| 59 | + let attributes = messageLabel.textAttributes |
| 60 | + let textFont = attributes?[.font] as? UIFont |
| 61 | + expect(textFont).toNot(beNil()) |
| 62 | + expect(textFont).to(equal(expectedFont)) |
| 63 | + } |
| 64 | + } |
| 65 | + context("url detection is enabled") { |
| 66 | + it("applies urlAttributes to text") { |
| 67 | + let expectedColor = UIColor.green |
| 68 | + messageLabel.urlAttributes = [.foregroundColor: expectedColor] |
| 69 | + messageLabel.text = "https://github.com/MessageKit" |
| 70 | + messageLabel.enabledDetectors = [.url] |
| 71 | + let attributes = messageLabel.textAttributes |
| 72 | + let textColor = attributes?[.foregroundColor] as? UIColor |
| 73 | + expect(textColor).toNot(beNil()) |
| 74 | + expect(textColor).to(equal(expectedColor)) |
| 75 | + } |
| 76 | + } |
| 77 | + context("date detection is enabled") { |
| 78 | + it("applies dateAttributes to text") { |
| 79 | + let expectedFont = UIFont.italicSystemFont(ofSize: 22) |
| 80 | + messageLabel.dateAttributes = [.font: expectedFont] |
| 81 | + messageLabel.text = "Today" |
| 82 | + messageLabel.enabledDetectors = [.date] |
| 83 | + let attributes = messageLabel.textAttributes |
| 84 | + let textFont = attributes?[.font] as? UIFont |
| 85 | + expect(textFont).toNot(beNil()) |
| 86 | + expect(textFont).to(equal(expectedFont)) |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +// MARK: - Helpers |
| 94 | + |
| 95 | +fileprivate extension MessageLabel { |
| 96 | + |
| 97 | + var textAttributes: [NSAttributedStringKey: Any]? { |
| 98 | + let length = attributedText!.length |
| 99 | + var range = NSRange(location: 0, length: length) |
| 100 | + return attributedText?.attributes(at: 0, effectiveRange: &range) |
| 101 | + } |
| 102 | +} |
0 commit comments