~ knotless Search/
↑↓ move↵ open⇧↵ all results

geesawra.industries/old-fella

Old people's social media app

0
1
main
143 lines · 6.4 KB Raw History
import UIKit
import XCTest

@testable import OldFella

final class AvatarRingTests: XCTestCase {
    private func image(background: UIColor, center: UIColor? = nil, size: CGSize = CGSize(width: 256, height: 256))
        -> CGImage {
        let format = UIGraphicsImageRendererFormat()
        format.scale = 1
        return UIGraphicsImageRenderer(size: size, format: format).image { context in
            background.setFill()
            context.fill(CGRect(origin: .zero, size: size))
            if let center {
                center.setFill()
                context.fill(
                    CGRect(
                        x: size.width * 0.25, y: size.height * 0.25,
                        width: size.width * 0.5, height: size.height * 0.5))
            }
        }.cgImage!
    }

    func testLightAndDarkEdgesChooseContrastingRingsInBothModes() {
        let black = AvatarRingColors.calculate(for: image(background: .black))
        let white = AvatarRingColors.calculate(for: image(background: .white))
        for color in [black.light, black.dark] {
            XCTAssertGreaterThan((color.luminance + 0.05) / 0.05, 1.9)
        }
        for color in [white.light, white.dark] {
            XCTAssertGreaterThan(1.05 / (color.luminance + 0.05), 1.9)
        }
        XCTAssertGreaterThan(1.05 / (black.light.luminance + 0.05), 2)
        XCTAssertGreaterThan((white.dark.luminance + 0.05) / 0.05, 2)
    }

    func testLightModeRingsStandOutAgainstWhiteAndGroupedBackgrounds() {
        let avatars: [UIColor] = [
            .white, .black, .gray, .blue, .red, .green, .yellow, .cyan,
            UIColor(red: 0.95, green: 0.82, blue: 0.7, alpha: 1)
        ]
        for background in avatars {
            let ring = AvatarRingColors.calculate(for: image(background: background)).light
            XCTAssertGreaterThanOrEqual(1.05 / (ring.luminance + 0.05), 3.8)
            XCTAssertGreaterThanOrEqual(0.9 / (ring.luminance + 0.05), 3.3)
        }
    }

    func testDarkModeColorsStayTheSame() {
        let blue = AvatarRingColors.calculate(for: image(background: .blue)).dark
        XCTAssertEqual(blue.red, 0.3810918585155354, accuracy: 0.000001)
        XCTAssertEqual(blue.green, 0.3810918585155354, accuracy: 0.000001)
        XCTAssertEqual(blue.blue, 1, accuracy: 0.000001)
        let green = AvatarRingColors.calculate(for: image(background: .green)).dark
        XCTAssertEqual(green.luminance, 0.3326, accuracy: 0.000001)
        XCTAssertEqual(green.red, 0, accuracy: 0.000001)
        XCTAssertEqual(green.blue, 0, accuracy: 0.000001)
    }

    func testRingsKeepTheAvatarColorAndChangeItsBrightness() {
        let blue = AvatarRingColors.calculate(for: image(background: .blue))
        let red = AvatarRingColors.calculate(for: image(background: .red))
        let green = AvatarRingColors.calculate(for: image(background: .green))
        for color in [blue.light, blue.dark] {
            XCTAssertGreaterThan(color.blue, color.red + 0.1)
            XCTAssertGreaterThan(color.blue, color.green + 0.1)
            XCTAssertGreaterThan(color.luminance, 0.0722 * 2)
        }
        for color in [red.light, red.dark] {
            XCTAssertGreaterThan(color.red, color.green + 0.01)
            XCTAssertGreaterThan(color.red, color.blue + 0.01)
        }
        for color in [green.light, green.dark] {
            XCTAssertGreaterThan(color.green, color.red + 0.1)
            XCTAssertGreaterThan(color.green, color.blue + 0.1)
            XCTAssertLessThan(color.luminance, 0.7152 / 2)
        }
    }

    func testAvatarColorsOutsideAnyPresetPaletteRemainDistinct() {
        let teal = AvatarRingColors.calculate(
            for: image(background: UIColor(red: 0.1, green: 0.5, blue: 0.6, alpha: 1)))
        let violet = AvatarRingColors.calculate(
            for: image(background: UIColor(red: 0.5, green: 0.1, blue: 0.6, alpha: 1)))
        for color in [teal.light, teal.dark] {
            XCTAssertGreaterThan(color.blue, color.green)
            XCTAssertGreaterThan(color.green, color.red)
        }
        for color in [violet.light, violet.dark] {
            XCTAssertGreaterThan(color.blue, color.red)
            XCTAssertGreaterThan(color.red, color.green)
        }
        XCTAssertNotEqual(teal, violet)
    }

    func testRingBrightnessTracksEdgeBrightnessWithoutPaletteSteps() {
        let first = AvatarRingColors.calculate(for: image(background: UIColor(white: 0.10, alpha: 1)))
        let second = AvatarRingColors.calculate(for: image(background: UIColor(white: 0.12, alpha: 1)))
        XCTAssertGreaterThan(second.light.luminance, first.light.luminance)
        XCTAssertGreaterThan(second.dark.luminance, first.dark.luminance)
        XCTAssertLessThan(second.light.luminance - first.light.luminance, 0.04)
    }

    func testWhiteBackgroundDoesNotWashOutColoredSubject() {
        let portrait = AvatarRingColors.calculate(for: image(background: .white, center: .blue))
        for color in [portrait.light, portrait.dark] {
            XCTAssertGreaterThan(color.blue, color.red + 0.3)
            XCTAssertGreaterThan(color.blue, color.green + 0.3)
        }
    }

    func testColoredRingsStaySaturatedOnMixedEdges() {
        let portrait = AvatarRingColors.calculate(for: image(background: .gray, center: .red))
        for color in [portrait.light, portrait.dark] {
            XCTAssertGreaterThan(color.red, color.green + 0.3)
            XCTAssertGreaterThan(color.red, color.blue + 0.3)
        }
    }

    func testTransparentImageUsesNeutralFallback() {
        XCTAssertEqual(AvatarRingColors.calculate(for: image(background: .clear)), .fallback)
    }

    func testSamplingMatchesCircularAspectFillCrop() {
        let square = AvatarRingColors.calculate(for: image(background: .red))
        let wide = image(background: .blue, size: CGSize(width: 512, height: 256))
        let format = UIGraphicsImageRendererFormat()
        format.scale = 1
        let cropped = UIGraphicsImageRenderer(size: CGSize(width: 512, height: 256), format: format).image { context in
            context.cgContext.draw(wide, in: CGRect(x: 0, y: 0, width: 512, height: 256))
            UIColor.red.setFill()
            context.fill(CGRect(x: 128, y: 0, width: 256, height: 256))
        }.cgImage!
        XCTAssertEqual(AvatarRingColors.calculate(for: cropped), square)
    }

    func testColorCalculationPerformance() {
        let avatar = image(background: .blue, center: .yellow)
        measure {
            for _ in 0..<100 { _ = AvatarRingColors.calculate(for: avatar) }
        }
    }
}