Skip to content

Commit

Permalink
Demonstrate UIKit bug outlined in #105
Browse files Browse the repository at this point in the history
  • Loading branch information
ZevEisenberg committed Oct 3, 2016
1 parent dceb93b commit 67b72de
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Tests/AssertHelpers.swift
Expand Up @@ -78,6 +78,12 @@ func BONAssert(attributes dictionary: StyleAttributes?, query: (NSParagraphStyle
let data2 = dataFromImage(image: image2)
XCTAssertEqual(data1, data2, file: file, line: line)
}

func BONAssertNotEqualImages(_ image1: BONImage, _ image2: BONImage, file: StaticString = #file, line: UInt = #line) {
let data1 = dataFromImage(image: image1)
let data2 = dataFromImage(image: image2)
XCTAssertNotEqual(data1, data2, file: file, line: line)
}
#else
func BONAssert<T: RawRepresentable where T.RawValue: Equatable>(attributes dictionary: StyleAttributes?, query: (NSParagraphStyle) -> T, value: T, file: StaticString = #file, line: UInt = #line) {
guard let paragraphStyle = dictionary?[NSParagraphStyleAttributeName] as? NSParagraphStyle else {
Expand All @@ -93,4 +99,10 @@ func BONAssert(attributes dictionary: StyleAttributes?, query: (NSParagraphStyle
let data2 = dataFromImage(image: image2)
XCTAssertEqual(data1, data2, file: file, line: line)
}

func BONAssertNotEqualImages(image1: BONImage, _ image2: BONImage, file: StaticString = #file, line: UInt = #line) {
let data1 = dataFromImage(image: image1)
let data2 = dataFromImage(image: image2)
XCTAssertNotEqual(data1, data2, file: file, line: line)
}
#endif
45 changes: 45 additions & 0 deletions Tests/ImageTintingTests.swift
Expand Up @@ -59,4 +59,49 @@ class ImageTintingTests: XCTestCase {
#endif
}

#if os(OSX)
#else
func testDemonstrateUIKitTintingBug() {
let attachment = NSTextAttachment()

// image must be set to Template rendering mode
attachment.image = UIImage(named: "discount", in: testBundle, compatibleWith: nil)!.withRenderingMode(.alwaysTemplate)

let withNBSP = NSMutableAttributedString(string: BonMot.Special.noBreakSpace.description)
let withoutNBSP = NSMutableAttributedString(string: "")

let attachmentString = NSAttributedString(attachment: attachment)

withNBSP.append(attachmentString)
withoutNBSP.append(attachmentString)

withNBSP.addAttribute(NSForegroundColorAttributeName, value: UIColor.orange, range: NSRange(location: 0, length: withNBSP.length))
withoutNBSP.addAttribute(NSForegroundColorAttributeName, value: UIColor.orange, range: NSRange(location: 0, length: withoutNBSP.length))

let images = [withNBSP, withoutNBSP].map { attrString -> BONImage in
let label = UILabel()
label.attributedText = attrString
label.sizeToFit()
label.backgroundColor = .white
return ImageTintingTests.image(of: label)
}

// Demonstrate that UIKit will tint the image, but only if there is
// at least one character in the string before the attachment.
// See details at https://github.com/Raizlabs/BonMot/issues/105
BONAssertNotEqualImages(images[0], images[1])

// Uncomment these lines and enter your username to see the images for yourself:
// let username = "your-username-goes-here"
//
// do {
// try UIImagePNGRepresentation(images[0])!.write(to: URL(fileURLWithPath: "/Users/\(username)/Desktop/withNBSP.png"))
// try UIImagePNGRepresentation(images[1])!.write(to: URL(fileURLWithPath: "/Users/\(username)/Desktop/withoutNBSP.png"))
// }
// catch {
// XCTFail("failed to write images. Did you remember to put the name of your home folder in the path? \(error)")
// }
}
#endif

}

0 comments on commit 67b72de

Please sign in to comment.