76 lines · 3.8 KB · SwiftRaw
import CoreGraphicsimport XCTest@testable import CottonUI/// A look is never finished on an undersized picture (coordinator, 27 Nov, NEXT-SMALL-PICTURE.md): on the Mac's real/// photos `.fastFormat` gave every picture under 128 px when 360 was asked, and every look was kept as it came.final class AlbumsPictureTests: XCTestCase { private func picture(_ width: Int, _ height: Int) -> CGImage { let context = CGContext(data: nil, width: width, height: height, bitsPerComponent: 8, bytesPerRow: 0, space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: CGImageAlphaInfo.noneSkipLast.rawValue)! return context.makeImage()! } private func longEdge(_ outcome: LocalPicture) -> Int? { switch outcome { case .picture(let image, _), .small(let image, _): return max(image.width, image.height) default: return nil } } func testA120PixelFetchIsNeverAFinishedLook() async { var asked: [AlbumsPictureMode] = [] let outcome = await AlbumsPicture.ask(originalLongEdge: 4032) { mode in asked.append(mode) return .picture(self.picture(120, 90), degraded: false) } if case .picture = outcome { XCTFail("a 120 px picture of a 4,032 px photo ended as a finished look") } guard case .small = outcome else { return XCTFail("expected a small look, owed a closer one; got \(outcome)") } XCTAssertEqual(asked, [.highQuality, .fast], "short of enough, the other way is asked before giving up") } func testHighQualityIsAskedFirstAndKeptWhenEnough() async { var asked: [AlbumsPictureMode] = [] let outcome = await AlbumsPicture.ask(originalLongEdge: 4032) { mode in asked.append(mode) return .picture(self.picture(360, 270), degraded: false) } guard case .picture = outcome else { return XCTFail("a 360 px picture is enough; got \(outcome)") } XCTAssertEqual(asked, [.highQuality]) } func testTheOtherWayIsTakenWhenTheFirstIsShort() async { let outcome = await AlbumsPicture.ask(originalLongEdge: 4032) { mode in mode == .highQuality ? .picture(self.picture(120, 90), degraded: true) : .picture(self.picture(360, 270), degraded: false) } guard case .picture = outcome else { return XCTFail("the other way's 360 px picture is enough; got \(outcome)") } XCTAssertEqual(longEdge(outcome), 360) } func testTheLargestShortPictureIsKept() async { let outcome = await AlbumsPicture.ask(originalLongEdge: 4032) { mode in mode == .highQuality ? .picture(self.picture(200, 150), degraded: true) : .picture(self.picture(120, 90), degraded: false) } guard case .small = outcome else { return XCTFail("expected small; got \(outcome)") } XCTAssertEqual(longEdge(outcome), 200) } func testASmallOriginalIsFinishedAtItsOwnSize() async { // A 200 px original can never give 256: its own size is enough, and it is not owed anything. let outcome = await AlbumsPicture.ask(originalLongEdge: 200) { _ in .picture(self.picture(200, 150), degraded: false) } guard case .picture = outcome else { return XCTFail("a picture at the original's own size is enough; got \(outcome)") } } func testNothingOnThePhoneStaysOnlyInCloud() async { let outcome = await AlbumsPicture.ask(originalLongEdge: 4032) { _ in .onlyInCloud } guard case .onlyInCloud = outcome else { return XCTFail("got \(outcome)") } } func testAShortPictureBeatsNothing() async { let outcome = await AlbumsPicture.ask(originalLongEdge: 4032) { mode in mode == .highQuality ? .picture(self.picture(120, 90), degraded: true) : .onlyInCloud } guard case .small = outcome else { return XCTFail("the 120 px picture is kept as small; got \(outcome)") } }}