Skip to content

Commit

Permalink
Update ImageEncoderTests
Browse files Browse the repository at this point in the history
  • Loading branch information
kean committed Nov 17, 2024
1 parent f9438e3 commit 056d66f
Showing 1 changed file with 42 additions and 42 deletions.
84 changes: 42 additions & 42 deletions Tests/NukeTests/ImageEncoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,95 +2,95 @@
//
// Copyright (c) 2015-2024 Alexander Grebenyuk (github.com/kean).

import XCTest
import Testing
@testable import Nuke

final class ImageEncoderTests: XCTestCase {
func testEncodeImage() throws {
@Suite struct ImageEncoderTests {
@Test func encodeImage() throws {
// Given
let image = Test.image
let encoder = ImageEncoders.Default()

// When
let data = try XCTUnwrap(encoder.encode(image))
let data = try #require(encoder.encode(image))

// Then
XCTAssertEqual(AssetType(data), .jpeg)
#expect(AssetType(data) == .jpeg)
}
func testEncodeImagePNGOpaque() throws {

@Test func encodeImagePNGOpaque() throws {
// Given
let image = Test.image(named: "fixture", extension: "png")
let encoder = ImageEncoders.Default()

// When
let data = try XCTUnwrap(encoder.encode(image))
let data = try #require(encoder.encode(image))

// Then
#if os(macOS)
// It seems that on macOS, NSImage created from png has an alpha
// component regardless of whether the input image has it.
XCTAssertEqual(AssetType(data), .png)
#expect(AssetType(data) == .png)
#else
XCTAssertEqual(AssetType(data), .jpeg)
#expect(AssetType(data) == .jpeg)
#endif
}
func testEncodeImagePNGTransparent() throws {

@Test func encodeImagePNGTransparent() throws {
// Given
let image = Test.image(named: "swift", extension: "png")
let encoder = ImageEncoders.Default()

// When
let data = try XCTUnwrap(encoder.encode(image))
let data = try #require(encoder.encode(image))

// Then
XCTAssertEqual(AssetType(data), .png)
#expect(AssetType(data) == .png)
}
func testPrefersHEIF() throws {

@Test func prefersHEIF() throws {
// Given
let image = Test.image
var encoder = ImageEncoders.Default()
encoder.isHEIFPreferred = true

// When
let data = try XCTUnwrap(encoder.encode(image))
let data = try #require(encoder.encode(image))

// Then
XCTAssertNil(AssetType(data)) // TODO: update when HEIF support is added
#expect(AssetType(data) == nil) // TODO: update when HEIF support is added // TODO: update when HEIF support is added
}

#if os(iOS) || os(tvOS) || os(visionOS)
func testEncodeCoreImageBackedImage() throws {

@Test func encodeCoreImageBackedImage() throws {
// Given
let image = try ImageProcessors.GaussianBlur().processThrowing(Test.image)
let encoder = ImageEncoders.Default()

// When
let data = try XCTUnwrap(encoder.encode(image))
let data = try #require(encoder.encode(image))

// Then encoded as PNG because GaussianBlur produces
// images with alpha channel
XCTAssertEqual(AssetType(data), .png)
#expect(AssetType(data) == .png)
}

#endif

// MARK: - Misc
func testIsOpaqueWithOpaquePNG() {

@Test func isOpaqueWithOpaquePNG() {
let image = Test.image(named: "fixture", extension: "png")
#if os(macOS)
XCTAssertFalse(image.cgImage!.isOpaque)
#expect(!image.cgImage!.isOpaque)
#else
XCTAssertTrue(image.cgImage!.isOpaque)
#expect(image.cgImage!.isOpaque)
#endif
}
func testIsOpaqueWithTransparentPNG() {

@Test func isOpaqueWithTransparentPNG() {
let image = Test.image(named: "swift", extension: "png")
XCTAssertFalse(image.cgImage!.isOpaque)
#expect(!image.cgImage!.isOpaque)
}
}

0 comments on commit 056d66f

Please sign in to comment.