Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vpeschenkov committed Mar 2, 2019
1 parent b47860a commit f80e2d7
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 81 deletions.
45 changes: 21 additions & 24 deletions LetterAvatarKitTests/Extensions/UIImage+LetterAvatarKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,68 +30,65 @@ import Foundation
class UIImageLetterAvatarKitTests: XCTestCase {
func testAvatarBuild() {
let avatarImage = UIImage.makeLetterAvatar(withUsername: "Letter Avatar")
XCTAssertNotNil(avatarImage)
let testAvatarImage = UIImage(named: "LetterAvatarTestImage")
XCTAssertNotNil(testAvatarImage)
XCTAssert(avatarImage!.isEqualToImage(testAvatarImage))
XCTAssertEqual(avatarImage, testAvatarImage)
}

func testAvatarBuildWithCustomSize() {
let size = CGSize(width: 100, height: 100)
let avatarImage = UIImage.makeLetterAvatar(withUsername: "Letter Avatar", size: size)
XCTAssertTrue(avatarImage?.size.width == size.width)
XCTAssertTrue(avatarImage?.size.height == size.height)
XCTAssertEqual(avatarImage?.size.width, size.width)
XCTAssertEqual(avatarImage?.size.height, size.height)
}

func testAvatarBuildSingleLetterFlag() {
// Make a configuration
let configuration = LetterAvatarBuilderConfiguration()
configuration.username = "Avatar Test"
configuration.singleLetter = true
// Make images
let avatarImage = UIImage.makeLetterAvatar(withConfiguration: configuration)
XCTAssertNotNil(avatarImage)
let testAvatarImage = UIImage(named: "LetterAvatarSingleLetterTestImage")
XCTAssertNotNil(testAvatarImage)
XCTAssertTrue(avatarImage?.size.width == configuration.size.width)
XCTAssertTrue(avatarImage?.size.height == configuration.size.height)
XCTAssert(avatarImage!.isEqualToImage(testAvatarImage))
// Test them
XCTAssertEqual(avatarImage?.size.width, configuration.size.width)
XCTAssertEqual(avatarImage?.size.height, configuration.size.height)
XCTAssertEqual(avatarImage, testAvatarImage)
}

func testAvatarBuildWithConfiguration() {
// Make a configuration
let configuration = LetterAvatarBuilderConfiguration()
configuration.username = "Letter Avatar"
// Make images
let avatarImage = UIImage.makeLetterAvatar(withConfiguration: configuration)
XCTAssertNotNil(avatarImage)
let testAvatarImage = UIImage(named: "LetterAvatarTestImage")
XCTAssertNotNil(testAvatarImage)
XCTAssertTrue(avatarImage?.size.width == configuration.size.width)
XCTAssertTrue(avatarImage?.size.height == configuration.size.height)
XCTAssert(avatarImage!.isEqualToImage(testAvatarImage))
// Test them
XCTAssertEqual(avatarImage?.size.width, configuration.size.width)
XCTAssertEqual(avatarImage?.size.height, configuration.size.height)
XCTAssertEqual(avatarImage!, testAvatarImage)
}

func testAvatarBuildWithOneWordUsername() {
let avatarImage = UIImage.makeLetterAvatar(withUsername: "Avatar")
XCTAssertNotNil(avatarImage)
let testAvatarImage = UIImage(named: "LetterAvatarOneWordTestImage")
XCTAssertNotNil(testAvatarImage)
XCTAssert(avatarImage!.isEqualToImage(testAvatarImage))
XCTAssertEqual(avatarImage, testAvatarImage)
}

func testAvatarBuildWithMoreThanTwoWordsUsername() {
let avatarImage = UIImage.makeLetterAvatar(withUsername: "Letter Test Avatar")
XCTAssertNotNil(avatarImage)
let testAvatarImage = UIImage(named: "LetterAvatarTestImage")
XCTAssertNotNil(testAvatarImage)
XCTAssert(avatarImage!.isEqualToImage(testAvatarImage))
XCTAssertEqual(avatarImage, testAvatarImage)
}

func testAvatarBuildWithOneWordUsernameAndSingleLetterFlag() {
// Make a configuration
let configuration = LetterAvatarBuilderConfiguration()
configuration.username = "Avatar"
configuration.singleLetter = true
// Make images
let avatarImage = UIImage.makeLetterAvatar(withConfiguration: configuration)
XCTAssertNotNil(avatarImage)
let testAvatarImage = UIImage(named: "LetterAvatarSingleLetterTestImage")
XCTAssertNotNil(testAvatarImage)
XCTAssert(avatarImage!.isEqualToImage(testAvatarImage))
// Test them
XCTAssertEqual(avatarImage, testAvatarImage)
}
}
21 changes: 9 additions & 12 deletions LetterAvatarKitTests/LetterAvatarBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class LetterAvatarBuilderTests: XCTestCase {
let configuration = LetterAvatarBuilderConfiguration()
configuration.username = "Letter Avatar"
let avatarImage = LetterAvatarBuilder().makeAvatar(with: configuration)
XCTAssert(avatarImage!.isEqualToImage(UIImage(named: "LetterAvatarTestImage")))
XCTAssertEqual(avatarImage, UIImage(named: "LetterAvatarTestImage"))
}

func testAvatarBuildWithCustomSize() {
Expand All @@ -45,52 +45,49 @@ class LetterAvatarBuilderTests: XCTestCase {
}

func testAvatarBuildSingleLetterFlag() {
// Make a configuration
let configuration = LetterAvatarBuilderConfiguration()
configuration.username = "Avatar Test"
configuration.singleLetter = true
// Make images
let avatarImage = LetterAvatarBuilder().makeAvatar(with: configuration)
XCTAssertNotNil(avatarImage)
let testAvatarImage = UIImage(named: "LetterAvatarSingleLetterTestImage")
XCTAssertNotNil(testAvatarImage)
// Test them
XCTAssertTrue(avatarImage?.size.width == configuration.size.width)
XCTAssertTrue(avatarImage?.size.height == configuration.size.height)
XCTAssert(avatarImage!.isEqualToImage(testAvatarImage))
XCTAssertEqual(avatarImage, testAvatarImage)
}

func testAvatarBuildWithEmptyUsername() {
let configuration = LetterAvatarBuilderConfiguration()
let avatarImage = LetterAvatarBuilder().makeAvatar(with: configuration)
XCTAssertNotNil(avatarImage)
let testAvatarImage = UIImage(named: "LetterAvatarEmptyTestImage")
XCTAssert(avatarImage!.isEqualToImage(testAvatarImage))
XCTAssertEqual(avatarImage, testAvatarImage)
}

func testAvatarBuildWithOneWordUsername() {
let configuration = LetterAvatarBuilderConfiguration()
configuration.username = "Avatar"
let avatarImage = LetterAvatarBuilder().makeAvatar(with: configuration)
XCTAssertNotNil(avatarImage)
let testAvatarImage = UIImage(named: "LetterAvatarOneWordTestImage")
XCTAssert(avatarImage!.isEqualToImage(testAvatarImage))
XCTAssertEqual(avatarImage, testAvatarImage)
}

func testAvatarBuildWithMoreThanTwoWordsUsername() {
let configuration = LetterAvatarBuilderConfiguration()
configuration.username = "Letter Test Avatar"
let avatarImage = LetterAvatarBuilder().makeAvatar(with: configuration)
XCTAssertNotNil(avatarImage)
let testAvatarImage = UIImage(named: "LetterAvatarTestImage")
XCTAssert(avatarImage!.isEqualToImage(testAvatarImage))
XCTAssertEqual(avatarImage, testAvatarImage)
}

func testAvatarBuildWithOneWordUsernameAndSingleLetterFlag() {
let configuration = LetterAvatarBuilderConfiguration()
configuration.username = "Avatar"
configuration.singleLetter = true
let avatarImage = LetterAvatarBuilder().makeAvatar(with: configuration)
XCTAssertNotNil(avatarImage)
let testAvatarImage = UIImage(named: "LetterAvatarSingleLetterTestImage")
XCTAssert(avatarImage!.isEqualToImage(testAvatarImage))
XCTAssertEqual(avatarImage, testAvatarImage)
}

func testAvatarBuildWithOnlyOneBackgroundColor() {
Expand Down
Binary file modified LetterAvatarKitTests/Resources/LetterAvatarEmptyTestImage@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified LetterAvatarKitTests/Resources/LetterAvatarTestImage@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 3 additions & 45 deletions LetterAvatarKitTests/Utilities/UIImage+TestsUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
//

import UIKit
import Foundation

extension UIImage {

Expand All @@ -43,51 +42,10 @@ extension UIImage {
)
}

func isEqualToImage(_ image: UIImage?, density: CGFloat = 1.0, accuracy: Double = 0.9) -> Bool {
guard let image = image, self.size.equalTo(image.size) else {
open override func isEqual(_ object: Any?) -> Bool {
guard let image = object as? UIImage else {
return false
}
let pixelsWidth: Int = self.cgImage!.width
let pixelsHeight: Int = self.cgImage!.height
let pixelsToCompare: Int = Int(CGFloat(pixelsWidth * pixelsHeight) * density)
var firstImagePixel = UInt()
let firstImageContext = CGContext(
data: &firstImagePixel,
width: 1,
height: 1,
bitsPerComponent: 8,
bytesPerRow: 4,
space: CGColorSpaceCreateDeviceRGB(),
bitmapInfo: CGImageAlphaInfo.noneSkipFirst.rawValue
)
var secondImagePixel = UInt()
let secondImageContext = CGContext(
data: &secondImagePixel,
width: 1,
height: 1,
bitsPerComponent: 8,
bytesPerRow: 4,
space: CGColorSpaceCreateDeviceRGB(),
bitmapInfo: CGImageAlphaInfo.noneSkipFirst.rawValue
)
var misses = 0
for _ in 0..<pixelsToCompare {
let pixelX = Int(arc4random()) % pixelsWidth
let pixelY = Int(arc4random()) % pixelsHeight
let drawRect = CGRect(
x: CGFloat(-pixelX),
y: CGFloat(-pixelY),
width: CGFloat(pixelsWidth),
height: CGFloat(pixelsHeight)
)
firstImageContext?.draw(self.cgImage!, in: drawRect)
secondImageContext?.draw(image.cgImage!, in: drawRect)
if firstImagePixel != secondImagePixel {
misses += 1
}
}
/// (1 - misses) / 1 = percent of miss
return ((Double(pixelsToCompare - misses) / Double(pixelsToCompare)) >= accuracy)
return self.pngData() == image.pngData()
}

}

0 comments on commit f80e2d7

Please sign in to comment.