From a03251ec0208c0dc96b551c983b574d856486318 Mon Sep 17 00:00:00 2001 From: kean Date: Sun, 17 Nov 2024 14:04:47 -0500 Subject: [PATCH] Update ImageDecoderTests --- Sources/Nuke/Caching/Cache.swift | 6 +- Tests/NukeTests/ImageDecoderTests.swift | 281 ++++++++++++------------ 2 files changed, 146 insertions(+), 141 deletions(-) diff --git a/Sources/Nuke/Caching/Cache.swift b/Sources/Nuke/Caching/Cache.swift index f38f9e86e..d8ac82e25 100644 --- a/Sources/Nuke/Caching/Cache.swift +++ b/Sources/Nuke/Caching/Cache.swift @@ -56,8 +56,8 @@ final class Cache: @unchecked Sendable { self.memoryPressure.resume() #if os(iOS) || os(tvOS) || os(visionOS) - Task { - await registerForEnterBackground() + Task { @MainActor in + registerForEnterBackground() } #endif } @@ -70,7 +70,7 @@ final class Cache: @unchecked Sendable { } #if os(iOS) || os(tvOS) || os(visionOS) - @MainActor private func registerForEnterBackground() { + private func registerForEnterBackground() { notificationObserver = NotificationCenter.default.addObserver(forName: UIApplication.didEnterBackgroundNotification, object: nil, queue: nil) { [weak self] _ in self?.clearCacheOnEnterBackground() } diff --git a/Tests/NukeTests/ImageDecoderTests.swift b/Tests/NukeTests/ImageDecoderTests.swift index 74420bb09..2aa649495 100644 --- a/Tests/NukeTests/ImageDecoderTests.swift +++ b/Tests/NukeTests/ImageDecoderTests.swift @@ -2,237 +2,242 @@ // // Copyright (c) 2015-2024 Alexander Grebenyuk (github.com/kean). -import XCTest +import Foundation +import Testing @testable import Nuke -class ImageDecoderTests: XCTestCase { - func testDecodePNG() throws { +@Suite struct ImageDecoderTests { + @Test func decodePNG() throws { // Given let data = Test.data(name: "fixture", extension: "png") let decoder = ImageDecoders.Default() - + // When - let container = try XCTUnwrap(decoder.decode(data)) - + let container = try #require(try decoder.decode(data)) + // Then - XCTAssertEqual(container.type, .png) - XCTAssertFalse(container.isPreview) - XCTAssertNil(container.data) - XCTAssertTrue(container.userInfo.isEmpty) + #expect(container.type == .png) + #expect(!container.isPreview) + #expect(container.data == nil) + #expect(container.userInfo.isEmpty) } - - func testDecodeJPEG() throws { + + @Test func decodeJPEG() throws { // Given let data = Test.data(name: "baseline", extension: "jpeg") let decoder = ImageDecoders.Default() - + // When - let container = try XCTUnwrap(decoder.decode(data)) - + let container = try #require(try decoder.decode(data)) + // Then - XCTAssertEqual(container.type, .jpeg) - XCTAssertFalse(container.isPreview) - XCTAssertNil(container.data) - XCTAssertTrue(container.userInfo.isEmpty) + #expect(container.type == .jpeg) + #expect(!container.isPreview) + #expect(container.data == nil) + #expect(container.userInfo.isEmpty) } - - func testDecodingProgressiveJPEG() { + + @Test func decodingProgressiveJPEG() { let data = Test.data(name: "progressive", extension: "jpeg") let decoder = ImageDecoders.Default() - + // Just before the Start Of Frame - XCTAssertNil(decoder.decodePartiallyDownloadedData(data[0...358])) - XCTAssertEqual(decoder.numberOfScans, 0) - + #expect(decoder.decodePartiallyDownloadedData(data[0...358]) == nil) + #expect(decoder.numberOfScans == 0) + // Right after the Start Of Frame - XCTAssertNil(decoder.decodePartiallyDownloadedData(data[0...359])) - XCTAssertEqual(decoder.numberOfScans, 0) // still haven't finished the first scan - + #expect(decoder.decodePartiallyDownloadedData(data[0...359]) == nil) + #expect(decoder.numberOfScans == 0) // still haven't finished the first scan // still haven't finished the first scan + // Just before the first Start Of Scan - XCTAssertNil(decoder.decodePartiallyDownloadedData(data[0...438])) - XCTAssertEqual(decoder.numberOfScans, 0) // still haven't finished the first scan - + #expect(decoder.decodePartiallyDownloadedData(data[0...438]) == nil) + #expect(decoder.numberOfScans == 0) // still haven't finished the first scan // still haven't finished the first scan + // Found the first Start Of Scan - XCTAssertNil(decoder.decodePartiallyDownloadedData(data[0...439])) - XCTAssertEqual(decoder.numberOfScans, 1) - + #expect(decoder.decodePartiallyDownloadedData(data[0...439]) == nil) + #expect(decoder.numberOfScans == 1) + // Found the second Start of Scan let scan1 = decoder.decodePartiallyDownloadedData(data[0...2952]) - XCTAssertNotNil(scan1) - XCTAssertEqual(scan1?.isPreview, true) + #expect(scan1 != nil) + #expect(scan1?.isPreview == true) if let image = scan1?.image { #if os(macOS) - XCTAssertEqual(image.size.width, 450) - XCTAssertEqual(image.size.height, 300) + #expect(image.size.width == 450) + #expect(image.size.height == 300) #else - XCTAssertEqual(image.size.width * image.scale, 450) - XCTAssertEqual(image.size.height * image.scale, 300) + #expect(image.size.width * image.scale == 450) + #expect(image.size.height * image.scale == 300) #endif } - XCTAssertEqual(decoder.numberOfScans, 2) - XCTAssertEqual(scan1?.userInfo[.scanNumberKey] as? Int, 2) - + #expect(decoder.numberOfScans == 2) + #expect(scan1?.userInfo[.scanNumberKey] as? Int == 2) + // Feed all data and see how many scans are there // In practice the moment we finish receiving data we call // `decode(data: data, isCompleted: true)` so we might not scan all the // of the bytes and encounter all of the scans (e.g. the final chunk // of data that we receive contains multiple scans). - XCTAssertNotNil(decoder.decodePartiallyDownloadedData(data)) - XCTAssertEqual(decoder.numberOfScans, 10) + #expect(decoder.decodePartiallyDownloadedData(data) != nil) + #expect(decoder.numberOfScans == 10) } - - func testDecodeGIF() throws { + + @Test func decodeGIF() throws { // Given let data = Test.data(name: "cat", extension: "gif") let decoder = ImageDecoders.Default() - + // When - let container = try XCTUnwrap(decoder.decode(data)) - + let container = try #require(try decoder.decode(data)) + // Then - XCTAssertEqual(container.type, .gif) - XCTAssertFalse(container.isPreview) - XCTAssertNotNil(container.data) - XCTAssertTrue(container.userInfo.isEmpty) + #expect(container.type == .gif) + #expect(!container.isPreview) + #expect(container.data != nil) + #expect(container.userInfo.isEmpty) } - - func testDecodeHEIC() throws { + + @Test func decodeHEIC() throws { // Given let data = Test.data(name: "img_751", extension: "heic") let decoder = ImageDecoders.Default() - + // When - let container = try XCTUnwrap(decoder.decode(data)) - + let container = try #require(try decoder.decode(data)) + // Then - XCTAssertNil(container.type) // TODO: update when HEIF support is added - XCTAssertFalse(container.isPreview) - XCTAssertNil(container.data) - XCTAssertTrue(container.userInfo.isEmpty) + #expect(container.type == nil) // TODO: update when HEIF support is added // TODO: update when HEIF support is added + #expect(!container.isPreview) + #expect(container.data == nil) + #expect(container.userInfo.isEmpty) } - - func testDecodingGIFDataAttached() throws { + + @Test func decodingGIFDataAttached() throws { let data = Test.data(name: "cat", extension: "gif") - XCTAssertNotNil(try ImageDecoders.Default().decode(data).data) + #expect(try ImageDecoders.Default().decode(data).data != nil) } - - func testDecodingGIFPreview() throws { + + @Test func decodingGIFPreview() throws { let data = Test.data(name: "cat", extension: "gif") - XCTAssertEqual(data.count, 427672) // 427 KB + #expect(data.count == 427672) // 427 KB // 427 KB let chunk = data[...60000] // 6 KB let response = try ImageDecoders.Default().decode(chunk) - XCTAssertEqual(response.image.sizeInPixels, CGSize(width: 500, height: 279)) + #expect(response.image.sizeInPixels == CGSize(width: 500, height: 279)) } - - func testDecodingGIFPreviewGeneratedOnlyOnce() throws { + + @Test func decodingGIFPreviewGeneratedOnlyOnce() throws { let data = Test.data(name: "cat", extension: "gif") - XCTAssertEqual(data.count, 427672) // 427 KB + #expect(data.count == 427672) // 427 KB // 427 KB let chunk = data[...60000] // 6 KB - + let context = ImageDecodingContext.mock(data: chunk) - let decoder = try XCTUnwrap(ImageDecoders.Default(context: context)) - - XCTAssertNotNil(decoder.decodePartiallyDownloadedData(chunk)) - XCTAssertNil(decoder.decodePartiallyDownloadedData(chunk)) + let decoder = try #require(ImageDecoders.Default(context: context)) + + #expect(decoder.decodePartiallyDownloadedData(chunk) != nil) + #expect(decoder.decodePartiallyDownloadedData(chunk) == nil) } - - func testDecodingPNGDataNotAttached() throws { + + @Test func decodingPNGDataNotAttached() throws { let data = Test.data(name: "fixture", extension: "png") let container = try ImageDecoders.Default().decode(data) - XCTAssertNil(container.data) + #expect(container.data == nil) } - + #if os(iOS) || os(tvOS) || os(macOS) || os(visionOS) - func testDecodeBaselineWebP() throws { + @Test func decodeBaselineWebP() throws { if #available(OSX 11.0, iOS 14.0, watchOS 7.0, tvOS 999.0, *) { let data = Test.data(name: "baseline", extension: "webp") let container = try ImageDecoders.Default().decode(data) - XCTAssertEqual(container.image.sizeInPixels, CGSize(width: 550, height: 368)) - XCTAssertNil(container.data) + #expect(container.image.sizeInPixels == CGSize(width: 550, height: 368)) + #expect(container.data == nil) } } #endif } -class ImageTypeTests: XCTestCase { +@Suite + +struct ImageTypeTests { // MARK: PNG - - func testDetectPNG() { + + @Test func detectPNG() { let data = Test.data(name: "fixture", extension: "png") - XCTAssertNil(AssetType(data[0..<1])) - XCTAssertNil(AssetType(data[0..<7])) - XCTAssertEqual(AssetType(data[0..<8]), .png) - XCTAssertEqual(AssetType(data), .png) + #expect(AssetType(data[0..<1]) == nil) + #expect(AssetType(data[0..<7]) == nil) + #expect(AssetType(data[0..<8]) == .png) + #expect(AssetType(data) == .png) } - + // MARK: GIF - - func testDetectGIF() { + + @Test func detectGIF() { let data = Test.data(name: "cat", extension: "gif") - XCTAssertEqual(AssetType(data), .gif) + #expect(AssetType(data) == .gif) } - + // MARK: JPEG - - func testDetectBaselineJPEG() { + + @Test func detectBaselineJPEG() { let data = Test.data(name: "baseline", extension: "jpeg") - XCTAssertNil(AssetType(data[0..<1])) - XCTAssertNil(AssetType(data[0..<2])) - XCTAssertEqual(AssetType(data[0..<3]), .jpeg) - XCTAssertEqual(AssetType(data), .jpeg) + #expect(AssetType(data[0..<1]) == nil) + #expect(AssetType(data[0..<2]) == nil) + #expect(AssetType(data[0..<3]) == .jpeg) + #expect(AssetType(data) == .jpeg) } - - func testDetectProgressiveJPEG() { + + @Test func detectProgressiveJPEG() { let data = Test.data(name: "progressive", extension: "jpeg") // Not enough data - XCTAssertNil(AssetType(Data())) - XCTAssertNil(AssetType(data[0..<2])) - + #expect(AssetType(Data()) == nil) + #expect(AssetType(data[0..<2]) == nil) + // Enough to determine image format - XCTAssertEqual(AssetType(data[0..<3]), .jpeg) - XCTAssertEqual(AssetType(data[0..<33]), .jpeg) - + #expect(AssetType(data[0..<3]) == .jpeg) + #expect(AssetType(data[0..<33]) == .jpeg) + // Full image - XCTAssertEqual(AssetType(data), .jpeg) + #expect(AssetType(data) == .jpeg) } - + // MARK: WebP - - func testDetectBaselineWebP() { + + @Test func detectBaselineWebP() { let data = Test.data(name: "baseline", extension: "webp") - XCTAssertNil(AssetType(data[0..<1])) - XCTAssertNil(AssetType(data[0..<2])) - XCTAssertEqual(AssetType(data[0..<12]), .webp) - XCTAssertEqual(AssetType(data), .webp) + #expect(AssetType(data[0..<1]) == nil) + #expect(AssetType(data[0..<2]) == nil) + #expect(AssetType(data[0..<12]) == .webp) + #expect(AssetType(data) == .webp) } } -class ImagePropertiesTests: XCTestCase { +@Suite + +struct ImagePropertiesTests { // MARK: JPEG - - func testDetectBaselineJPEG() { + + @Test func detectBaselineJPEG() { let data = Test.data(name: "baseline", extension: "jpeg") - XCTAssertNil(ImageProperties.JPEG(data[0..<1])) - XCTAssertNil(ImageProperties.JPEG(data[0..<2])) - XCTAssertNil(ImageProperties.JPEG(data[0..<3])) - XCTAssertEqual(ImageProperties.JPEG(data)?.isProgressive, false) + #expect(ImageProperties.JPEG(data[0..<1]) == nil) + #expect(ImageProperties.JPEG(data[0..<2]) == nil) + #expect(ImageProperties.JPEG(data[0..<3]) == nil) + #expect(ImageProperties.JPEG(data)?.isProgressive == false) } - - func testDetectProgressiveJPEG() { + + @Test func detectProgressiveJPEG() { let data = Test.data(name: "progressive", extension: "jpeg") // Not enough data - XCTAssertNil(ImageProperties.JPEG(Data())) - XCTAssertNil(ImageProperties.JPEG(data[0..<2])) - + #expect(ImageProperties.JPEG(Data()) == nil) + #expect(ImageProperties.JPEG(data[0..<2]) == nil) + // Enough to determine image format - XCTAssertNil(ImageProperties.JPEG(data[0..<3])) - XCTAssertNil(ImageProperties.JPEG(data[0...30])) - + #expect(ImageProperties.JPEG(data[0..<3]) == nil) + #expect(ImageProperties.JPEG(data[0...30]) == nil) + // Just before the first scan - XCTAssertNil(ImageProperties.JPEG(data[0...358])) - XCTAssertEqual(ImageProperties.JPEG(data[0...359])?.isProgressive, true) - + #expect(ImageProperties.JPEG(data[0...358]) == nil) + #expect(ImageProperties.JPEG(data[0...359])?.isProgressive == true) + // Full image - XCTAssertEqual(ImageProperties.JPEG(data[0...359])?.isProgressive, true) + #expect(ImageProperties.JPEG(data[0...359])?.isProgressive == true) } }