1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import SwiftUI
import XCTest
@testable import OldFella
@MainActor
final class MediaControlAppearanceTests: XCTestCase {
func testMediaControlsHaveVisibleSurfaces() throws {
for scheme in [ColorScheme.light, .dark] {
for contrast in [UIAccessibilityContrast.normal, .high] {
let image = try render(scheme: scheme, contrast: contrast)
let name = "Media controls \(scheme) contrast \(contrast.rawValue)"
let attachment = XCTAttachment(image: image)
attachment.name = name
attachment.lifetime = .keepAlways
add(attachment)
for row in 0..<3 {
let outside = try pixel(image, column: 4, row: row * 80 + 40)
let inside = try pixel(image, column: 24, row: row * 80 + 40)
XCTAssertGreaterThan(abs(inside - outside), 0.04, "\(name), row \(row)")
}
}
}
}
private func render(scheme: ColorScheme, contrast: UIAccessibilityContrast) throws -> UIImage {
let suite = "MediaControlAppearanceTests.\(UUID().uuidString)"
let defaults = try XCTUnwrap(UserDefaults(suiteName: suite))
defer { defaults.removePersistentDomain(forName: suite) }
defaults.set(true, forKey: "hidePictures")
let url = URL(fileURLWithPath: "/media-control-test.gif")
let gallery = VStack(spacing: 0) {
GIFMedia(imageURLs: [url], videoURLs: [], descriptions: [:], isReplyContext: true)
.padding(16)
InlineImages(urls: [url])
.padding(16)
VideoLinks(urls: [url])
.padding(16)
}
.frame(width: 320)
.background(Color(uiColor: .systemBackground))
.defaultAppStorage(defaults)
.environment(\.colorScheme, scheme)
.environment(AppModel(service: MockBlueskyService(), feedDefaults: defaults))
let host = UIHostingController(rootView: gallery)
host.safeAreaRegions = []
host.traitOverrides.accessibilityContrast = contrast
let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 320, height: 240))
window.overrideUserInterfaceStyle = scheme == .dark ? .dark : .light
window.rootViewController = host
window.isHidden = false
defer { window.isHidden = true }
host.view.frame = window.bounds
host.view.setNeedsLayout()
host.view.layoutIfNeeded()
let format = UIGraphicsImageRendererFormat()
format.scale = 1
return UIGraphicsImageRenderer(size: window.bounds.size, format: format).image { _ in
host.view.drawHierarchy(in: host.view.bounds, afterScreenUpdates: true)
}
}
private func pixel(_ image: UIImage, column: Int, row: Int) throws -> Double {
let source = try XCTUnwrap(image.cgImage)
var data = [UInt8](repeating: 0, count: source.width * source.height * 4)
let context = try XCTUnwrap(
CGContext(
data: &data, width: source.width, height: source.height,
bitsPerComponent: 8, bytesPerRow: source.width * 4,
space: CGColorSpaceCreateDeviceRGB(),
bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue))
context.draw(source, in: CGRect(x: 0, y: 0, width: source.width, height: source.height))
let offset = (row * source.width + column) * 4
return (Double(data[offset]) + Double(data[offset + 1]) + Double(data[offset + 2])) / (3 * 255)
}
}