Cotton code
56 lines · 3.2 KB · SwiftRaw
#if os(iOS)import UIKitimport XCTest@testable import CottonUI/// Home's fan's decoded page pictures over many turns (fan3 leak, 15 Oct/// 2026: fanperf2 measured 467 MB of growth over 100 turns on iOS 26.5).@MainActorfinal class PagePictureMemoryTests: XCTestCase {    func testTransparentPagePicturesKeepAlphaThroughCompressionAndDecode() async throws {        let pictures = PagePictures(quality: 0.8, reach: 1, scale: 1)        let image = UIGraphicsImageRenderer(size: CGSize(width: 4, height: 2), format: PagePictures.format(scale: 1)).image { context in            UIColor.red.withAlphaComponent(0.5).setFill()            context.fill(CGRect(x: 0, y: 0, width: 2, height: 2))        }        await pictures.keep(image, for: "synthetic", families: nil)        await pictures.focus(keys: ["synthetic"], settled: 0)        let decoded = try XCTUnwrap(pictures.decoded["synthetic"])        let pixels = PagePictures.pixels(decoded)        XCTAssertEqual(Double(pixels[3]), 128, accuracy: 1, "The object's translucent edge survives")        XCTAssertEqual(pixels[15], 0, "The shell's ground must show through the page")        XCTAssertFalse(PagePictures.format(scale: 1).opaque)    }    /// RULE: However the fan turns — flings that change its heading many times before it rests — no more than the settled tile ± `fan_picture_reach` pictures are ever kept decoded, every one of them within that reach; beyond it only the compressed bytes stay (fan brief; 100 turns once held 139 decoded pictures alive, 3 MB each, and the system killed the app).    func testDecodedPicturesStayWithinReachOverAHundredTurns() async {        let keys = (0 ..< 33).map { "tile-\($0)" }        let reach = Theme.paper.scale.count(.fanPictureReach)        let pictures = PagePictures(quality: 0.8, reach: reach, scale: 2)        let format = PagePictures.format(scale: 2)        for (index, key) in keys.enumerated() {            let image = UIGraphicsImageRenderer(size: CGSize(width: 40, height: 60), format: format).image { context in                UIColor(hue: CGFloat(index) / CGFloat(keys.count), saturation: 0.5, brightness: 0.9, alpha: 1).setFill()                context.fill(CGRect(x: 0, y: 0, width: 40, height: 60))            }            await pictures.keep(image, for: key, families: nil)        }        var settled = 0        var step = 3        for turn in 0 ..< 100 {            if !keys.indices.contains(settled + step) { step = -step }            // A fling: the heading passes every tile on the way.            for heading in stride(from: settled, through: settled + step, by: step > 0 ? 1 : -1) {                pictures.refocus(keys: keys, settled: heading)            }            settled += step            pictures.refocus(keys: keys, settled: settled)            await pictures.decodingDone()            let window = PagePictureLedger.decoded(around: settled, count: keys.count, reach: reach)!            XCTAssertLessThanOrEqual(pictures.decoded.count, 2 * reach + 1, "turn \(turn)")            XCTAssertTrue(pictures.decoded.keys.allSatisfy { window.contains(keys.firstIndex(of: $0)!) }, "turn \(turn)")            XCTAssertNotNil(pictures.decoded[keys[settled]], "the settled tile's picture is ready")        }    }}#endif