Skip to content

Commit b62cc43

Browse files
committedNov 17, 2024·
Update ImageDecoderTests
1 parent 4e0bca9 commit b62cc43

File tree

2 files changed

+142
-141
lines changed

2 files changed

+142
-141
lines changed
 

‎Sources/Nuke/Caching/Cache.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ final class Cache<Key: Hashable, Value>: @unchecked Sendable {
5656
self.memoryPressure.resume()
5757

5858
#if os(iOS) || os(tvOS) || os(visionOS)
59-
Task {
60-
await registerForEnterBackground()
59+
Task { @MainActor in
60+
registerForEnterBackground()
6161
}
6262
#endif
6363
}
@@ -70,7 +70,7 @@ final class Cache<Key: Hashable, Value>: @unchecked Sendable {
7070
}
7171

7272
#if os(iOS) || os(tvOS) || os(visionOS)
73-
@MainActor private func registerForEnterBackground() {
73+
private func registerForEnterBackground() {
7474
notificationObserver = NotificationCenter.default.addObserver(forName: UIApplication.didEnterBackgroundNotification, object: nil, queue: nil) { [weak self] _ in
7575
self?.clearCacheOnEnterBackground()
7676
}

‎Tests/NukeTests/ImageDecoderTests.swift

+139-138
Original file line numberDiff line numberDiff line change
@@ -2,237 +2,238 @@
22
//
33
// Copyright (c) 2015-2024 Alexander Grebenyuk (github.com/kean).
44

5-
import XCTest
5+
import Foundation
6+
import Testing
67
@testable import Nuke
78

8-
class ImageDecoderTests: XCTestCase {
9-
func testDecodePNG() throws {
9+
@Suite struct ImageDecoderTests {
10+
@Test func decodePNG() throws {
1011
// Given
1112
let data = Test.data(name: "fixture", extension: "png")
1213
let decoder = ImageDecoders.Default()
13-
14+
1415
// When
15-
let container = try XCTUnwrap(decoder.decode(data))
16-
16+
let container = try #require(try decoder.decode(data))
17+
1718
// Then
18-
XCTAssertEqual(container.type, .png)
19-
XCTAssertFalse(container.isPreview)
20-
XCTAssertNil(container.data)
21-
XCTAssertTrue(container.userInfo.isEmpty)
19+
#expect(container.type == .png)
20+
#expect(!container.isPreview)
21+
#expect(container.data == nil)
22+
#expect(container.userInfo.isEmpty)
2223
}
23-
24-
func testDecodeJPEG() throws {
24+
25+
@Test func decodeJPEG() throws {
2526
// Given
2627
let data = Test.data(name: "baseline", extension: "jpeg")
2728
let decoder = ImageDecoders.Default()
28-
29+
2930
// When
30-
let container = try XCTUnwrap(decoder.decode(data))
31-
31+
let container = try #require(try decoder.decode(data))
32+
3233
// Then
33-
XCTAssertEqual(container.type, .jpeg)
34-
XCTAssertFalse(container.isPreview)
35-
XCTAssertNil(container.data)
36-
XCTAssertTrue(container.userInfo.isEmpty)
34+
#expect(container.type == .jpeg)
35+
#expect(!container.isPreview)
36+
#expect(container.data == nil)
37+
#expect(container.userInfo.isEmpty)
3738
}
38-
39-
func testDecodingProgressiveJPEG() {
39+
40+
@Test func decodingProgressiveJPEG() {
4041
let data = Test.data(name: "progressive", extension: "jpeg")
4142
let decoder = ImageDecoders.Default()
42-
43+
4344
// Just before the Start Of Frame
44-
XCTAssertNil(decoder.decodePartiallyDownloadedData(data[0...358]))
45-
XCTAssertEqual(decoder.numberOfScans, 0)
46-
45+
#expect(decoder.decodePartiallyDownloadedData(data[0...358]) == nil)
46+
#expect(decoder.numberOfScans == 0)
47+
4748
// Right after the Start Of Frame
48-
XCTAssertNil(decoder.decodePartiallyDownloadedData(data[0...359]))
49-
XCTAssertEqual(decoder.numberOfScans, 0) // still haven't finished the first scan
50-
49+
#expect(decoder.decodePartiallyDownloadedData(data[0...359]) == nil)
50+
#expect(decoder.numberOfScans == 0) // still haven't finished the first scan // still haven't finished the first scan
51+
5152
// Just before the first Start Of Scan
52-
XCTAssertNil(decoder.decodePartiallyDownloadedData(data[0...438]))
53-
XCTAssertEqual(decoder.numberOfScans, 0) // still haven't finished the first scan
54-
53+
#expect(decoder.decodePartiallyDownloadedData(data[0...438]) == nil)
54+
#expect(decoder.numberOfScans == 0) // still haven't finished the first scan // still haven't finished the first scan
55+
5556
// Found the first Start Of Scan
56-
XCTAssertNil(decoder.decodePartiallyDownloadedData(data[0...439]))
57-
XCTAssertEqual(decoder.numberOfScans, 1)
58-
57+
#expect(decoder.decodePartiallyDownloadedData(data[0...439]) == nil)
58+
#expect(decoder.numberOfScans == 1)
59+
5960
// Found the second Start of Scan
6061
let scan1 = decoder.decodePartiallyDownloadedData(data[0...2952])
61-
XCTAssertNotNil(scan1)
62-
XCTAssertEqual(scan1?.isPreview, true)
62+
#expect(scan1 != nil)
63+
#expect(scan1?.isPreview == true)
6364
if let image = scan1?.image {
6465
#if os(macOS)
65-
XCTAssertEqual(image.size.width, 450)
66-
XCTAssertEqual(image.size.height, 300)
66+
#expect(image.size.width == 450)
67+
#expect(image.size.height == 300)
6768
#else
68-
XCTAssertEqual(image.size.width * image.scale, 450)
69-
XCTAssertEqual(image.size.height * image.scale, 300)
69+
#expect(image.size.width * image.scale == 450)
70+
#expect(image.size.height * image.scale == 300)
7071
#endif
7172
}
72-
XCTAssertEqual(decoder.numberOfScans, 2)
73-
XCTAssertEqual(scan1?.userInfo[.scanNumberKey] as? Int, 2)
74-
73+
#expect(decoder.numberOfScans == 2)
74+
#expect(scan1?.userInfo[.scanNumberKey] as? Int == 2)
75+
7576
// Feed all data and see how many scans are there
7677
// In practice the moment we finish receiving data we call
7778
// `decode(data: data, isCompleted: true)` so we might not scan all the
7879
// of the bytes and encounter all of the scans (e.g. the final chunk
7980
// of data that we receive contains multiple scans).
80-
XCTAssertNotNil(decoder.decodePartiallyDownloadedData(data))
81-
XCTAssertEqual(decoder.numberOfScans, 10)
81+
#expect(decoder.decodePartiallyDownloadedData(data) != nil)
82+
#expect(decoder.numberOfScans == 10)
8283
}
83-
84-
func testDecodeGIF() throws {
84+
85+
@Test func decodeGIF() throws {
8586
// Given
8687
let data = Test.data(name: "cat", extension: "gif")
8788
let decoder = ImageDecoders.Default()
88-
89+
8990
// When
90-
let container = try XCTUnwrap(decoder.decode(data))
91-
91+
let container = try #require(try decoder.decode(data))
92+
9293
// Then
93-
XCTAssertEqual(container.type, .gif)
94-
XCTAssertFalse(container.isPreview)
95-
XCTAssertNotNil(container.data)
96-
XCTAssertTrue(container.userInfo.isEmpty)
94+
#expect(container.type == .gif)
95+
#expect(!container.isPreview)
96+
#expect(container.data != nil)
97+
#expect(container.userInfo.isEmpty)
9798
}
98-
99-
func testDecodeHEIC() throws {
99+
100+
@Test func decodeHEIC() throws {
100101
// Given
101102
let data = Test.data(name: "img_751", extension: "heic")
102103
let decoder = ImageDecoders.Default()
103-
104+
104105
// When
105-
let container = try XCTUnwrap(decoder.decode(data))
106-
106+
let container = try #require(try decoder.decode(data))
107+
107108
// Then
108-
XCTAssertNil(container.type) // TODO: update when HEIF support is added
109-
XCTAssertFalse(container.isPreview)
110-
XCTAssertNil(container.data)
111-
XCTAssertTrue(container.userInfo.isEmpty)
109+
#expect(container.type == nil) // TODO: update when HEIF support is added // TODO: update when HEIF support is added
110+
#expect(!container.isPreview)
111+
#expect(container.data == nil)
112+
#expect(container.userInfo.isEmpty)
112113
}
113-
114-
func testDecodingGIFDataAttached() throws {
114+
115+
@Test func decodingGIFDataAttached() throws {
115116
let data = Test.data(name: "cat", extension: "gif")
116-
XCTAssertNotNil(try ImageDecoders.Default().decode(data).data)
117+
#expect(try ImageDecoders.Default().decode(data).data != nil)
117118
}
118-
119-
func testDecodingGIFPreview() throws {
119+
120+
@Test func decodingGIFPreview() throws {
120121
let data = Test.data(name: "cat", extension: "gif")
121-
XCTAssertEqual(data.count, 427672) // 427 KB
122+
#expect(data.count == 427672) // 427 KB // 427 KB
122123
let chunk = data[...60000] // 6 KB
123124
let response = try ImageDecoders.Default().decode(chunk)
124-
XCTAssertEqual(response.image.sizeInPixels, CGSize(width: 500, height: 279))
125+
#expect(response.image.sizeInPixels == CGSize(width: 500, height: 279))
125126
}
126-
127-
func testDecodingGIFPreviewGeneratedOnlyOnce() throws {
127+
128+
@Test func decodingGIFPreviewGeneratedOnlyOnce() throws {
128129
let data = Test.data(name: "cat", extension: "gif")
129-
XCTAssertEqual(data.count, 427672) // 427 KB
130+
#expect(data.count == 427672) // 427 KB // 427 KB
130131
let chunk = data[...60000] // 6 KB
131-
132+
132133
let context = ImageDecodingContext.mock(data: chunk)
133-
let decoder = try XCTUnwrap(ImageDecoders.Default(context: context))
134-
135-
XCTAssertNotNil(decoder.decodePartiallyDownloadedData(chunk))
136-
XCTAssertNil(decoder.decodePartiallyDownloadedData(chunk))
134+
let decoder = try #require(ImageDecoders.Default(context: context))
135+
136+
#expect(decoder.decodePartiallyDownloadedData(chunk) != nil)
137+
#expect(decoder.decodePartiallyDownloadedData(chunk) == nil)
137138
}
138-
139-
func testDecodingPNGDataNotAttached() throws {
139+
140+
@Test func decodingPNGDataNotAttached() throws {
140141
let data = Test.data(name: "fixture", extension: "png")
141142
let container = try ImageDecoders.Default().decode(data)
142-
XCTAssertNil(container.data)
143+
#expect(container.data == nil)
143144
}
144-
145+
145146
#if os(iOS) || os(tvOS) || os(macOS) || os(visionOS)
146-
func testDecodeBaselineWebP() throws {
147+
@Test func decodeBaselineWebP() throws {
147148
if #available(OSX 11.0, iOS 14.0, watchOS 7.0, tvOS 999.0, *) {
148149
let data = Test.data(name: "baseline", extension: "webp")
149150
let container = try ImageDecoders.Default().decode(data)
150-
XCTAssertEqual(container.image.sizeInPixels, CGSize(width: 550, height: 368))
151-
XCTAssertNil(container.data)
151+
#expect(container.image.sizeInPixels == CGSize(width: 550, height: 368))
152+
#expect(container.data == nil)
152153
}
153154
}
154155
#endif
155156
}
156157

157-
class ImageTypeTests: XCTestCase {
158+
@Suite struct ImageTypeTests {
158159
// MARK: PNG
159-
160-
func testDetectPNG() {
160+
161+
@Test func detectPNG() {
161162
let data = Test.data(name: "fixture", extension: "png")
162-
XCTAssertNil(AssetType(data[0..<1]))
163-
XCTAssertNil(AssetType(data[0..<7]))
164-
XCTAssertEqual(AssetType(data[0..<8]), .png)
165-
XCTAssertEqual(AssetType(data), .png)
163+
#expect(AssetType(data[0..<1]) == nil)
164+
#expect(AssetType(data[0..<7]) == nil)
165+
#expect(AssetType(data[0..<8]) == .png)
166+
#expect(AssetType(data) == .png)
166167
}
167-
168+
168169
// MARK: GIF
169-
170-
func testDetectGIF() {
170+
171+
@Test func detectGIF() {
171172
let data = Test.data(name: "cat", extension: "gif")
172-
XCTAssertEqual(AssetType(data), .gif)
173+
#expect(AssetType(data) == .gif)
173174
}
174-
175+
175176
// MARK: JPEG
176-
177-
func testDetectBaselineJPEG() {
177+
178+
@Test func detectBaselineJPEG() {
178179
let data = Test.data(name: "baseline", extension: "jpeg")
179-
XCTAssertNil(AssetType(data[0..<1]))
180-
XCTAssertNil(AssetType(data[0..<2]))
181-
XCTAssertEqual(AssetType(data[0..<3]), .jpeg)
182-
XCTAssertEqual(AssetType(data), .jpeg)
180+
#expect(AssetType(data[0..<1]) == nil)
181+
#expect(AssetType(data[0..<2]) == nil)
182+
#expect(AssetType(data[0..<3]) == .jpeg)
183+
#expect(AssetType(data) == .jpeg)
183184
}
184-
185-
func testDetectProgressiveJPEG() {
185+
186+
@Test func detectProgressiveJPEG() {
186187
let data = Test.data(name: "progressive", extension: "jpeg")
187188
// Not enough data
188-
XCTAssertNil(AssetType(Data()))
189-
XCTAssertNil(AssetType(data[0..<2]))
190-
189+
#expect(AssetType(Data()) == nil)
190+
#expect(AssetType(data[0..<2]) == nil)
191+
191192
// Enough to determine image format
192-
XCTAssertEqual(AssetType(data[0..<3]), .jpeg)
193-
XCTAssertEqual(AssetType(data[0..<33]), .jpeg)
194-
193+
#expect(AssetType(data[0..<3]) == .jpeg)
194+
#expect(AssetType(data[0..<33]) == .jpeg)
195+
195196
// Full image
196-
XCTAssertEqual(AssetType(data), .jpeg)
197+
#expect(AssetType(data) == .jpeg)
197198
}
198-
199+
199200
// MARK: WebP
200-
201-
func testDetectBaselineWebP() {
201+
202+
@Test func detectBaselineWebP() {
202203
let data = Test.data(name: "baseline", extension: "webp")
203-
XCTAssertNil(AssetType(data[0..<1]))
204-
XCTAssertNil(AssetType(data[0..<2]))
205-
XCTAssertEqual(AssetType(data[0..<12]), .webp)
206-
XCTAssertEqual(AssetType(data), .webp)
204+
#expect(AssetType(data[0..<1]) == nil)
205+
#expect(AssetType(data[0..<2]) == nil)
206+
#expect(AssetType(data[0..<12]) == .webp)
207+
#expect(AssetType(data) == .webp)
207208
}
208209
}
209210

210-
class ImagePropertiesTests: XCTestCase {
211+
@Suite struct ImagePropertiesTests {
211212
// MARK: JPEG
212-
213-
func testDetectBaselineJPEG() {
213+
214+
@Test func detectBaselineJPEG() {
214215
let data = Test.data(name: "baseline", extension: "jpeg")
215-
XCTAssertNil(ImageProperties.JPEG(data[0..<1]))
216-
XCTAssertNil(ImageProperties.JPEG(data[0..<2]))
217-
XCTAssertNil(ImageProperties.JPEG(data[0..<3]))
218-
XCTAssertEqual(ImageProperties.JPEG(data)?.isProgressive, false)
216+
#expect(ImageProperties.JPEG(data[0..<1]) == nil)
217+
#expect(ImageProperties.JPEG(data[0..<2]) == nil)
218+
#expect(ImageProperties.JPEG(data[0..<3]) == nil)
219+
#expect(ImageProperties.JPEG(data)?.isProgressive == false)
219220
}
220-
221-
func testDetectProgressiveJPEG() {
221+
222+
@Test func detectProgressiveJPEG() {
222223
let data = Test.data(name: "progressive", extension: "jpeg")
223224
// Not enough data
224-
XCTAssertNil(ImageProperties.JPEG(Data()))
225-
XCTAssertNil(ImageProperties.JPEG(data[0..<2]))
226-
225+
#expect(ImageProperties.JPEG(Data()) == nil)
226+
#expect(ImageProperties.JPEG(data[0..<2]) == nil)
227+
227228
// Enough to determine image format
228-
XCTAssertNil(ImageProperties.JPEG(data[0..<3]))
229-
XCTAssertNil(ImageProperties.JPEG(data[0...30]))
230-
229+
#expect(ImageProperties.JPEG(data[0..<3]) == nil)
230+
#expect(ImageProperties.JPEG(data[0...30]) == nil)
231+
231232
// Just before the first scan
232-
XCTAssertNil(ImageProperties.JPEG(data[0...358]))
233-
XCTAssertEqual(ImageProperties.JPEG(data[0...359])?.isProgressive, true)
234-
233+
#expect(ImageProperties.JPEG(data[0...358]) == nil)
234+
#expect(ImageProperties.JPEG(data[0...359])?.isProgressive == true)
235+
235236
// Full image
236-
XCTAssertEqual(ImageProperties.JPEG(data[0...359])?.isProgressive, true)
237+
#expect(ImageProperties.JPEG(data[0...359])?.isProgressive == true)
237238
}
238239
}

0 commit comments

Comments
 (0)
Please sign in to comment.