|
| 1 | +import XCTest |
| 2 | +import SnapshotTestingPlugin |
| 3 | +@testable import SnapshotTesting |
| 4 | +import ImageSerializationPlugin |
| 5 | + |
| 6 | +#if canImport(UIKit) |
| 7 | +import UIKit |
| 8 | +#elseif canImport(AppKit) |
| 9 | +import AppKit |
| 10 | +#endif |
| 11 | + |
| 12 | +class MockImageSerializationPlugin: ImageSerializationPlugin { |
| 13 | + |
| 14 | + static var imageFormat: ImageSerializationFormat = .plugins("mock") |
| 15 | + |
| 16 | + func encodeImage(_ image: SnapImage) -> Data? { |
| 17 | + return "mockImageData".data(using: .utf8) |
| 18 | + } |
| 19 | + |
| 20 | + func decodeImage(_ data: Data) -> SnapImage? { |
| 21 | + let mockImage = SnapImage() |
| 22 | + return mockImage |
| 23 | + } |
| 24 | + |
| 25 | + // MARK: - SnapshotTestingPlugin |
| 26 | + static var identifier: String = "ImageSerializationPlugin.MockImageSerializationPlugin.mock" |
| 27 | + required init() {} |
| 28 | +} |
| 29 | + |
| 30 | +class ImageSerializerTests: XCTestCase { |
| 31 | + |
| 32 | + var imageSerializer: ImageSerializer! |
| 33 | + // #E48900FF |
| 34 | + var _1pxOrangePNGImage = Data(base64Encoded: "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAERlWElmTU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAAAaADAAQAAAABAAAAAQAAAAD5Ip3+AAAADUlEQVQIHWN40snwHwAGLwJteELaggAAAABJRU5ErkJggg==")! |
| 35 | + |
| 36 | + override func setUp() { |
| 37 | + super.setUp() |
| 38 | + PluginRegistry.reset() // Reset state before each test |
| 39 | + |
| 40 | + // Register the mock plugins in the PluginRegistry |
| 41 | + PluginRegistry.registerPlugin(MockImageSerializationPlugin() as SnapshotTestingPlugin) |
| 42 | + |
| 43 | + imageSerializer = ImageSerializer() |
| 44 | + } |
| 45 | + |
| 46 | + override func tearDown() { |
| 47 | + imageSerializer = nil |
| 48 | + PluginRegistry.reset() // Reset state after each test |
| 49 | + super.tearDown() |
| 50 | + } |
| 51 | + |
| 52 | + func testEncodeImageUsingMockPlugin() { |
| 53 | + let mockImage = SnapImage() |
| 54 | + let imageData = imageSerializer.encodeImage( |
| 55 | + mockImage, |
| 56 | + imageFormat: MockImageSerializationPlugin.imageFormat |
| 57 | + ) |
| 58 | + |
| 59 | + XCTAssertNotNil(imageData, "Image data should not be nil for mock plugin.") |
| 60 | + XCTAssertEqual(String(data: imageData!, encoding: .utf8), "mockImageData") |
| 61 | + } |
| 62 | + |
| 63 | + func testDecodeImageUsingMockPlugin() { |
| 64 | + let mockData = "mockImageData".data(using: .utf8)! |
| 65 | + let decodedImage = imageSerializer.decodeImage( |
| 66 | + mockData, |
| 67 | + imageFormat: MockImageSerializationPlugin.imageFormat |
| 68 | + ) |
| 69 | + |
| 70 | + XCTAssertNotNil(decodedImage, "Image should be decoded using the mock plugin.") |
| 71 | + } |
| 72 | + |
| 73 | + // TODO: 1PX png image data |
| 74 | + func testEncodeImageAsPNG() { |
| 75 | + let mockImage = SnapImage() |
| 76 | + let imageData = imageSerializer.encodeImage( |
| 77 | + mockImage, |
| 78 | + imageFormat: .png |
| 79 | + ) |
| 80 | + |
| 81 | + XCTAssertNil(imageData, "The image is empty it should be nil.") |
| 82 | + } |
| 83 | + |
| 84 | + func testDecodeImageAsPNG() { |
| 85 | + let decodedImage = imageSerializer.decodeImage( |
| 86 | + _1pxOrangePNGImage, |
| 87 | + imageFormat: .png |
| 88 | + ) |
| 89 | + |
| 90 | + XCTAssertNotNil(decodedImage, "PNG image should be decoded successfully.") |
| 91 | + XCTAssertEqual( |
| 92 | + decodedImage?.size.width, |
| 93 | + 1, "PNG image should be 1x1." |
| 94 | + ) |
| 95 | + XCTAssertEqual( |
| 96 | + decodedImage?.size.height, |
| 97 | + 1, "PNG image should be 1x1." |
| 98 | + ) |
| 99 | + XCTAssertEqual(getFirstPixelColorHex(from: decodedImage!), "#E48900FF") |
| 100 | + } |
| 101 | + |
| 102 | + func testUnknownImageFormatFallsBackToPNG() { |
| 103 | + let mockImage = SnapImage(data: _1pxOrangePNGImage)! |
| 104 | + let imageData = imageSerializer.encodeImage( |
| 105 | + mockImage, |
| 106 | + imageFormat: .plugins("unknownFormat") |
| 107 | + ) |
| 108 | + |
| 109 | + XCTAssertNotNil(imageData, "Unknown format should fall back to PNG encoding.") |
| 110 | + } |
| 111 | + |
| 112 | + func testPluginRegistryShouldContainRegisteredPlugins() { |
| 113 | + let plugins = PluginRegistry.allPlugins() as [ImageSerialization] |
| 114 | + |
| 115 | + XCTAssertEqual(plugins.count, 1, "There should be two registered plugins.") |
| 116 | + XCTAssertEqual(type(of: plugins[0]).imageFormat.rawValue, "mock", "The first plugin should support the 'mock' format.") |
| 117 | + } |
| 118 | +} |
0 commit comments