From 32ea991f7a39dcfbfe5a0e91272debb90cc8eb4c Mon Sep 17 00:00:00 2001 From: marcprux Date: Wed, 27 Sep 2023 14:06:25 -0400 Subject: [PATCH] Add tests comparing logical SwiftUI and Android text styles --- Sources/SkipUI/SkipUI/Text/Font.swift | 28 ++--- Tests/SkipUITests/CanvasTests.swift | 8 +- Tests/SkipUITests/ColorTests.swift | 68 ++++++------ Tests/SkipUITests/ImageTests.swift | 2 +- Tests/SkipUITests/LayoutTests.swift | 18 ++-- Tests/SkipUITests/TextTests.swift | 120 +++++++++++++++++++++ Tests/SkipUITests/XCSnapshotTestCase.swift | 12 +-- 7 files changed, 189 insertions(+), 67 deletions(-) diff --git a/Sources/SkipUI/SkipUI/Text/Font.swift b/Sources/SkipUI/SkipUI/Text/Font.swift index 07bbe962..92f19589 100644 --- a/Sources/SkipUI/SkipUI/Text/Font.swift +++ b/Sources/SkipUI/SkipUI/Text/Font.swift @@ -42,52 +42,54 @@ extension Font { // labelMedium: Roboto Medium 12/16 // labelSmall: New Roboto Medium 11/16 + // manual offsets are applied to the default font sizes to get them to line up with SwiftUI default sizes; see TextTests.swift + public static let largeTitle = Font(fontImpl: { - adjust(MaterialTheme.typography.titleLarge, by: Float(10.0)) + adjust(MaterialTheme.typography.titleLarge, by: Float(+8.0)) // 41.0px height }) public static let title = Font(fontImpl: { - adjust(MaterialTheme.typography.headlineLarge, by: Float(0.0)) + adjust(MaterialTheme.typography.headlineMedium, by: Float(-3.0)) }) public static let title2 = Font(fontImpl: { - adjust(MaterialTheme.typography.headlineMedium, by: Float(-4.0)) + adjust(MaterialTheme.typography.headlineSmall, by: Float(-5.0)) }) public static let title3 = Font(fontImpl: { - adjust(MaterialTheme.typography.headlineSmall, by: Float(-4.0)) + adjust(MaterialTheme.typography.headlineSmall, by: Float(-6.5)) }) public static let headline = Font(fontImpl: { - adjust(MaterialTheme.typography.titleLarge, by: Float(0.0)) + adjust(MaterialTheme.typography.titleMedium, by: Float(-1.0)) }) public static let subheadline = Font(fontImpl: { - adjust(MaterialTheme.typography.titleMedium, by: Float(0.0)) + adjust(MaterialTheme.typography.titleSmall, by: Float(-1.0)) }) public static let body = Font(fontImpl: { - adjust(MaterialTheme.typography.bodyMedium, by: Float(0.0)) + adjust(MaterialTheme.typography.bodyLarge, by: Float(-1.0)) }) public static let callout = Font(fontImpl: { - adjust(MaterialTheme.typography.bodySmall, by: Float(0.0)) + adjust(MaterialTheme.typography.bodyMedium, by: Float(+0.5)) }) public static let footnote = Font(fontImpl: { - adjust(MaterialTheme.typography.labelMedium, by: Float(0.0)) + adjust(MaterialTheme.typography.bodySmall, by: Float(-0.8)) }) public static let caption = Font(fontImpl: { - adjust(MaterialTheme.typography.labelMedium, by: Float(0.0)) + adjust(MaterialTheme.typography.bodySmall, by: Float(-1.0)) }) public static let caption2 = Font(fontImpl: { - adjust(MaterialTheme.typography.labelSmall, by: Float(0.0)) + adjust(MaterialTheme.typography.bodySmall, by: Float(-2.0)) }) - private static func adjust(_ style: androidx.compose.ui.text.TextStyle, by amount: Float = Float(0.0)) -> androidx.compose.ui.text.TextStyle { - return style.copy(fontSize: (style.fontSize.value + amount).sp) + private static func adjust(_ style: androidx.compose.ui.text.TextStyle, by amount: Float) -> androidx.compose.ui.text.TextStyle { + return amount == Float(0.0) ? style : style.copy(fontSize: (style.fontSize.value + amount).sp) } #endif diff --git a/Tests/SkipUITests/CanvasTests.swift b/Tests/SkipUITests/CanvasTests.swift index 804767c3..605f0e67 100644 --- a/Tests/SkipUITests/CanvasTests.swift +++ b/Tests/SkipUITests/CanvasTests.swift @@ -22,7 +22,7 @@ final class CanvasTests: XCSnapshotTestCase { XCTAssertEqual(try render(compact: 1, view: ZStack { Color.black.frame(width: 12.0, height: 12.0) Color.white.opacity(0.6).frame(width: 6.0, height: 6.0) - }), + }).pixmap, plaf(""" 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 @@ -49,7 +49,7 @@ final class CanvasTests: XCSnapshotTestCase { Color.white.opacity(0.8).frame(width: 8.0, height: 8.0) Color.black.opacity(0.5).frame(width: 4.0, height: 4.0) Color.white.opacity(0.22).frame(width: 2.0, height: 2.0) - }), + }).pixmap, plaf(""" 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 @@ -75,7 +75,7 @@ final class CanvasTests: XCSnapshotTestCase { androidx.compose.foundation.layout.Box(modifier: androidx.compose.ui.Modifier.background(androidx.compose.ui.graphics.Color.White).size(12.dp), contentAlignment: androidx.compose.ui.Alignment.Center) { androidx.compose.foundation.layout.Box(modifier: androidx.compose.ui.Modifier.background(androidx.compose.ui.graphics.Color.Black).size(6.dp, 6.dp)) } - })), + })).pixmap, plaf(""" F F F F F F F F F F F F F F F F F F F F F F F F @@ -101,7 +101,7 @@ final class CanvasTests: XCSnapshotTestCase { androidx.compose.foundation.layout.Box(modifier: androidx.compose.ui.Modifier.size(12.dp).background(androidx.compose.ui.graphics.Color.White), contentAlignment: androidx.compose.ui.Alignment.Center) { androidx.compose.foundation.layout.Box(modifier: androidx.compose.ui.Modifier.size(6.dp, 6.dp).background(androidx.compose.ui.graphics.Color.Black)) } - })), + })).pixmap, plaf(""" F F F F F F F F F F F F F F F F F F F F F F F F diff --git a/Tests/SkipUITests/ColorTests.swift b/Tests/SkipUITests/ColorTests.swift index 1ba71935..f0c24e0c 100644 --- a/Tests/SkipUITests/ColorTests.swift +++ b/Tests/SkipUITests/ColorTests.swift @@ -12,138 +12,138 @@ import Foundation // SKIP INSERT: @org.junit.runner.RunWith(androidx.test.ext.junit.runners.AndroidJUnit4::class) final class ColorTests: XCSnapshotTestCase { func testColorBlackCompact() throws { - XCTAssertEqual("0", try render(compact: 1, view: Color.black.frame(width: 1.0, height: 1.0))) + XCTAssertEqual("0", try render(compact: 1, view: Color.black.frame(width: 1.0, height: 1.0)).pixmap) } func testColorWhiteCompact() throws { - XCTAssertEqual("F", try render(compact: 1, view: Color.white.frame(width: 1.0, height: 1.0))) + XCTAssertEqual("F", try render(compact: 1, view: Color.white.frame(width: 1.0, height: 1.0)).pixmap) } func testColorClearDark() throws { - XCTAssertEqual(plaf("00FF00", macos: "000000", android: "000000"), try render(darkMode: true, view: Color.clear.frame(width: 1.0, height: 1.0))) + XCTAssertEqual(plaf("00FF00", macos: "000000", android: "000000"), try render(darkMode: true, view: Color.clear.frame(width: 1.0, height: 1.0)).pixmap) } func testColorClearLight() throws { - XCTAssertEqual(plaf("FFFF00", macos: "FFFFFF", android: "000000"), try render(view: Color.clear.frame(width: 1.0, height: 1.0))) + XCTAssertEqual(plaf("FFFF00", macos: "FFFFFF", android: "000000"), try render(view: Color.clear.frame(width: 1.0, height: 1.0)).pixmap) } func testColorBlackDark() throws { - XCTAssertEqual(plaf("000000"), try render(darkMode: true, view: Color.black.frame(width: 1.0, height: 1.0))) + XCTAssertEqual(plaf("000000"), try render(darkMode: true, view: Color.black.frame(width: 1.0, height: 1.0)).pixmap) } func testColorBlackLight() throws { - XCTAssertEqual(plaf("000000"), try render(view: Color.black.frame(width: 1.0, height: 1.0))) + XCTAssertEqual(plaf("000000"), try render(view: Color.black.frame(width: 1.0, height: 1.0)).pixmap) } func testColorWhiteDark() throws { - XCTAssertEqual(plaf("FFFFFF"), try render(darkMode: true, view: Color.white.frame(width: 1.0, height: 1.0))) + XCTAssertEqual(plaf("FFFFFF"), try render(darkMode: true, view: Color.white.frame(width: 1.0, height: 1.0)).pixmap) } func testColorWhiteLight() throws { - XCTAssertEqual(plaf("FFFFFF"), try render(view: Color.white.frame(width: 1.0, height: 1.0))) + XCTAssertEqual(plaf("FFFFFF"), try render(view: Color.white.frame(width: 1.0, height: 1.0)).pixmap) } func testColorGrayDark() throws { - XCTAssertEqual(plaf("8E8E93", macos: "98989D"), try render(darkMode: true, view: Color.gray.frame(width: 1.0, height: 1.0))) + XCTAssertEqual(plaf("8E8E93", macos: "98989D"), try render(darkMode: true, view: Color.gray.frame(width: 1.0, height: 1.0)).pixmap) } func testColorGrayLight() throws { - XCTAssertEqual(plaf("8E8E93"), try render(view: Color.gray.frame(width: 1.0, height: 1.0))) + XCTAssertEqual(plaf("8E8E93"), try render(view: Color.gray.frame(width: 1.0, height: 1.0)).pixmap) } func testColorRedDark() throws { - XCTAssertEqual(plaf("FF453A", android: "FF3B30"), try render(darkMode: true, view: Color.red.frame(width: 1.0, height: 1.0))) + XCTAssertEqual(plaf("FF453A", android: "FF3B30"), try render(darkMode: true, view: Color.red.frame(width: 1.0, height: 1.0)).pixmap) } func testColorRedLight() throws { - XCTAssertEqual(plaf("FF3B30"), try render(view: Color.red.frame(width: 1.0, height: 1.0))) + XCTAssertEqual(plaf("FF3B30"), try render(view: Color.red.frame(width: 1.0, height: 1.0)).pixmap) } func testColorOrangeDark() throws { - XCTAssertEqual(plaf("FF9F0A", android: "FF9500"), try render(darkMode: true, view: Color.orange.frame(width: 1.0, height: 1.0))) + XCTAssertEqual(plaf("FF9F0A", android: "FF9500"), try render(darkMode: true, view: Color.orange.frame(width: 1.0, height: 1.0)).pixmap) } func testColorOrangeLight() throws { - XCTAssertEqual(plaf("FF9500"), try render(view: Color.orange.frame(width: 1.0, height: 1.0))) + XCTAssertEqual(plaf("FF9500"), try render(view: Color.orange.frame(width: 1.0, height: 1.0)).pixmap) } func testColorYellowDark() throws { - XCTAssertEqual(plaf("FFD60A", android: "FFCC00"), try render(darkMode: true, view: Color.yellow.frame(width: 1.0, height: 1.0))) + XCTAssertEqual(plaf("FFD60A", android: "FFCC00"), try render(darkMode: true, view: Color.yellow.frame(width: 1.0, height: 1.0)).pixmap) } func testColorYellowLight() throws { - XCTAssertEqual(plaf("FFCC00"), try render(view: Color.yellow.frame(width: 1.0, height: 1.0))) + XCTAssertEqual(plaf("FFCC00"), try render(view: Color.yellow.frame(width: 1.0, height: 1.0)).pixmap) } func testColorGreenDark() throws { - XCTAssertEqual(plaf("30D158", macos: "32D74B", android: "34C759"), try render(darkMode: true, view: Color.green.frame(width: 1.0, height: 1.0))) + XCTAssertEqual(plaf("30D158", macos: "32D74B", android: "34C759"), try render(darkMode: true, view: Color.green.frame(width: 1.0, height: 1.0)).pixmap) } func testColorGreenLight() throws { - XCTAssertEqual(plaf("34C759", macos: "28CD41"), try render(view: Color.green.frame(width: 1.0, height: 1.0))) + XCTAssertEqual(plaf("34C759", macos: "28CD41"), try render(view: Color.green.frame(width: 1.0, height: 1.0)).pixmap) } func testColorMintDark() throws { - XCTAssertEqual(plaf("63E6E2", android: "00C7BE"), try render(darkMode: true, view: Color.mint.frame(width: 1.0, height: 1.0))) + XCTAssertEqual(plaf("63E6E2", android: "00C7BE"), try render(darkMode: true, view: Color.mint.frame(width: 1.0, height: 1.0)).pixmap) } func testColorMintLight() throws { - XCTAssertEqual(plaf("00C7BE"), try render(view: Color.mint.frame(width: 1.0, height: 1.0))) + XCTAssertEqual(plaf("00C7BE"), try render(view: Color.mint.frame(width: 1.0, height: 1.0)).pixmap) } func testColortTealDark() throws { - XCTAssertEqual(plaf("40C8E0", macos: "6AC4DC", android: "30B0C7"), try render(darkMode: true, view: Color.teal.frame(width: 1.0, height: 1.0))) + XCTAssertEqual(plaf("40C8E0", macos: "6AC4DC", android: "30B0C7"), try render(darkMode: true, view: Color.teal.frame(width: 1.0, height: 1.0)).pixmap) } func testColorTealLight() throws { - XCTAssertEqual(plaf("30B0C7", macos: "59ADC4"), try render(view: Color.teal.frame(width: 1.0, height: 1.0))) + XCTAssertEqual(plaf("30B0C7", macos: "59ADC4"), try render(view: Color.teal.frame(width: 1.0, height: 1.0)).pixmap) } func testColorCyanDark() throws { - XCTAssertEqual(plaf("64D2FF", macos: "5AC8F5", android: "32ADE6"), try render(darkMode: true, view: Color.cyan.frame(width: 1.0, height: 1.0))) + XCTAssertEqual(plaf("64D2FF", macos: "5AC8F5", android: "32ADE6"), try render(darkMode: true, view: Color.cyan.frame(width: 1.0, height: 1.0)).pixmap) } func testColorCyanLight() throws { - XCTAssertEqual(plaf("32ADE6", macos: "55BEF0"), try render(view: Color.cyan.frame(width: 1.0, height: 1.0))) + XCTAssertEqual(plaf("32ADE6", macos: "55BEF0"), try render(view: Color.cyan.frame(width: 1.0, height: 1.0)).pixmap) } func testColorBlueDark() throws { - XCTAssertEqual(plaf("0A84FF", android: "007AFF"), try render(darkMode: true, view: Color.blue.frame(width: 1.0, height: 1.0))) + XCTAssertEqual(plaf("0A84FF", android: "007AFF"), try render(darkMode: true, view: Color.blue.frame(width: 1.0, height: 1.0)).pixmap) } func testColorBlueLight() throws { - XCTAssertEqual(plaf("007AFF"), try render(view: Color.blue.frame(width: 1.0, height: 1.0))) + XCTAssertEqual(plaf("007AFF"), try render(view: Color.blue.frame(width: 1.0, height: 1.0)).pixmap) } func testColorIndigoDark() throws { - XCTAssertEqual(plaf("5E5CE6", android: "5856D6"), try render(darkMode: true, view: Color.indigo.frame(width: 1.0, height: 1.0))) + XCTAssertEqual(plaf("5E5CE6", android: "5856D6"), try render(darkMode: true, view: Color.indigo.frame(width: 1.0, height: 1.0)).pixmap) } func testColorIndigoLight() throws { - XCTAssertEqual(plaf("5856D6"), try render(view: Color.indigo.frame(width: 1.0, height: 1.0))) + XCTAssertEqual(plaf("5856D6"), try render(view: Color.indigo.frame(width: 1.0, height: 1.0)).pixmap) } func testColorPurpleDark() throws { - XCTAssertEqual(plaf("BF5AF2", android: "AF52DE"), try render(darkMode: true, view: Color.purple.frame(width: 1.0, height: 1.0))) + XCTAssertEqual(plaf("BF5AF2", android: "AF52DE"), try render(darkMode: true, view: Color.purple.frame(width: 1.0, height: 1.0)).pixmap) } func testColorPurpleLight() throws { - XCTAssertEqual(plaf("AF52DE"), try render(view: Color.purple.frame(width: 1.0, height: 1.0))) + XCTAssertEqual(plaf("AF52DE"), try render(view: Color.purple.frame(width: 1.0, height: 1.0)).pixmap) } func testColorPinkDark() throws { - XCTAssertEqual(plaf("FF375F", android: "FF2D55"), try render(darkMode: true, view: Color.pink.frame(width: 1.0, height: 1.0))) + XCTAssertEqual(plaf("FF375F", android: "FF2D55"), try render(darkMode: true, view: Color.pink.frame(width: 1.0, height: 1.0)).pixmap) } func testColorPinkLight() throws { - XCTAssertEqual(plaf("FF2D55"), try render(view: Color.pink.frame(width: 1.0, height: 1.0))) + XCTAssertEqual(plaf("FF2D55"), try render(view: Color.pink.frame(width: 1.0, height: 1.0)).pixmap) } func testColorBrownDark() throws { - XCTAssertEqual(plaf("AC8E68", android: "A2845E"), try render(darkMode: true, view: Color.brown.frame(width: 1.0, height: 1.0))) + XCTAssertEqual(plaf("AC8E68", android: "A2845E"), try render(darkMode: true, view: Color.brown.frame(width: 1.0, height: 1.0)).pixmap) } func testColorBrownLight() throws { - XCTAssertEqual(plaf("A2845E"), try render(view: Color.brown.frame(width: 1.0, height: 1.0))) + XCTAssertEqual(plaf("A2845E"), try render(view: Color.brown.frame(width: 1.0, height: 1.0)).pixmap) } } diff --git a/Tests/SkipUITests/ImageTests.swift b/Tests/SkipUITests/ImageTests.swift index b735f6a5..10324a22 100644 --- a/Tests/SkipUITests/ImageTests.swift +++ b/Tests/SkipUITests/ImageTests.swift @@ -98,7 +98,7 @@ final class ImageTests: XCSnapshotTestCase { } // a 5x5 red "dot" from https://en.wikipedia.org/wiki/Data_URI_scheme - XCTAssertEqual(try render(view: image("iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==")), + XCTAssertEqual(try render(view: image("iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==")).pixmap, plaf(""" FFFFFF FF0000 FF0000 FF0000 FFFFFF FF0000 FF0000 FF0000 FF0000 FF0000 diff --git a/Tests/SkipUITests/LayoutTests.swift b/Tests/SkipUITests/LayoutTests.swift index 2e2a6110..fdb83217 100644 --- a/Tests/SkipUITests/LayoutTests.swift +++ b/Tests/SkipUITests/LayoutTests.swift @@ -13,11 +13,11 @@ import Foundation final class LayoutTests: XCSnapshotTestCase { func testRenderWhiteDot() throws { - XCTAssertEqual("FFFFFF", try render(view: Color.white.frame(width: 1.0, height: 1.0))) + XCTAssertEqual("FFFFFF", try render(view: Color.white.frame(width: 1.0, height: 1.0)).pixmap) } func testRenderWhiteSquareTiny() throws { - XCTAssertEqual(try render(compact: 1, view: Color.white.frame(width: 2.0, height: 2.0)), """ + XCTAssertEqual(try render(compact: 1, view: Color.white.frame(width: 2.0, height: 2.0)).pixmap, """ F F F F """) @@ -44,7 +44,7 @@ final class LayoutTests: XCSnapshotTestCase { } func testRenderWhiteSquare() throws { - XCTAssertEqual(try render(compact: 1, view: Color.white.frame(width: 4.0, height: 4.0)), """ + XCTAssertEqual(try render(compact: 1, view: Color.white.frame(width: 4.0, height: 4.0)).pixmap, """ F F F F F F F F F F F F @@ -53,7 +53,7 @@ final class LayoutTests: XCSnapshotTestCase { } func testRenderWhiteSquareBig() throws { - XCTAssertEqual(try render(compact: 1, view: Color.white.frame(width: 12.0, height: 12.0)), """ + XCTAssertEqual(try render(compact: 1, view: Color.white.frame(width: 12.0, height: 12.0)).pixmap, """ F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F F @@ -157,7 +157,7 @@ final class LayoutTests: XCSnapshotTestCase { XCTAssertEqual(try render(compact: 2, antiAlias: true, view: ZStack { Color.black.frame(width: 12.0, height: 12.0) Color.white.frame(width: 6.0, height: 6.0).rotationEffect(Angle.degrees(45.0)) - }), + }).pixmap, plaf(""" 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 07 07 00 00 00 00 00 @@ -193,7 +193,7 @@ final class LayoutTests: XCSnapshotTestCase { XCTAssertEqual(try render(compact: 2, antiAlias: false, view: ZStack { Color.black.frame(width: 12.0, height: 12.0) Color.white.frame(width: 6.0, height: 6.0).rotationEffect(Angle.degrees(45.0)) - }), + }).pixmap, plaf(""" 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 FF FF 00 00 00 00 00 @@ -260,7 +260,7 @@ final class LayoutTests: XCSnapshotTestCase { func testDrawCourierBar() throws { XCTAssertEqual(try render(compact: 2, view: ZStack { Text("|").font(Font.custom("courier", size: CGFloat(8.0))).foregroundColor(Color.black) - }.frame(width: 7.0, height: 8.0).background(Color.white)), + }.frame(width: 7.0, height: 8.0).background(Color.white)).pixmap, plaf(""" FF FF F5 9F FF FF FF FF FF F5 9F FF FF FF @@ -294,7 +294,7 @@ final class LayoutTests: XCSnapshotTestCase { func testDrawTextDefaultFont() throws { XCTAssertEqual(try render(compact: 2, view: ZStack { Text("T").foregroundColor(Color.white) - }.background(Color.black)), + }.background(Color.black)).pixmap, plaf(""" 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 @@ -474,7 +474,7 @@ final class LayoutTests: XCSnapshotTestCase { Color.black.frame(height: 10.0) Color.white.opacity(0.8).frame(height: 12.0) Color.black.frame(height: 10.0) - }.background(Color.white).frame(width: 12.0, height: 12.0)), + }.background(Color.white).frame(width: 12.0, height: 12.0)).pixmap, plaf(""" FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF diff --git a/Tests/SkipUITests/TextTests.swift b/Tests/SkipUITests/TextTests.swift index a14a6961..6f9c0cb9 100644 --- a/Tests/SkipUITests/TextTests.swift +++ b/Tests/SkipUITests/TextTests.swift @@ -12,6 +12,126 @@ import Foundation // SKIP INSERT: @org.junit.runner.RunWith(androidx.test.ext.junit.runners.AndroidJUnit4::class) final class TextTests: XCSnapshotTestCase { + func testTextSizeLargeTitle() throws { + let size = try render(view: Text("X").font(.largeTitle)).size + #if SKIP + XCTAssertEqual(size.height, 41.0) + #elseif os(iOS) + XCTAssertEqual(size.height, 41.0) + #elseif os(macOS) + XCTAssertEqual(size.height, 31.0) + #endif + } + + func testTextSizeTitle() throws { + let size = try render(view: Text("X").font(.title)).size + #if SKIP + XCTAssertEqual(size.height, 34.0) + #elseif os(iOS) + XCTAssertEqual(size.height, 34.0) + #elseif os(macOS) + XCTAssertEqual(size.height, 26.0) + #endif + } + + func testTextSizeTitle2() throws { + let size = try render(view: Text("X").font(.title2)).size + #if SKIP + XCTAssertEqual(size.height, 27.0) + #elseif os(iOS) + XCTAssertEqual(size.height, 27.0) + #elseif os(macOS) + XCTAssertEqual(size.height, 21.0) + #endif + } + + func testTextSizeTitle3() throws { + let size = try render(view: Text("X").font(.title3)).size + #if SKIP + XCTAssertEqual(size.height, 24.0) + #elseif os(iOS) + XCTAssertEqual(size.height, 24.0) + #elseif os(macOS) + XCTAssertEqual(size.height, 19.0) + #endif + } + + func testTextSizeHeadline() throws { + let size = try render(view: Text("X").font(.headline)).size + #if SKIP + XCTAssertEqual(size.height, 21.0) + #elseif os(iOS) + XCTAssertEqual(size.height, 21.0) + #elseif os(macOS) + XCTAssertEqual(size.height, 16.0) + #endif + } + + func testTextSizeSubheadline() throws { + let size = try render(view: Text("X").font(.subheadline)).size + #if SKIP + XCTAssertEqual(size.height, 18.0) + #elseif os(iOS) + XCTAssertEqual(size.height, 18.0) + #elseif os(macOS) + XCTAssertEqual(size.height, 14.0) + #endif + } + + func testTextSizeBody() throws { + let size = try render(view: Text("X").font(.body)).size + #if SKIP + XCTAssertEqual(size.height, 21.0) + #elseif os(iOS) + XCTAssertEqual(size.height, 21.0) + #elseif os(macOS) + XCTAssertEqual(size.height, 16.0) + #endif + } + + func testTextSizeCallout() throws { + let size = try render(view: Text("X").font(.callout)).size + #if SKIP + XCTAssertEqual(size.height, 20.0) + #elseif os(iOS) + XCTAssertEqual(size.height, 20.0) + #elseif os(macOS) + XCTAssertEqual(size.height, 15.0) + #endif + } + + func testTextSizeFootnote() throws { + let size = try render(view: Text("X").font(.footnote)).size + #if SKIP + XCTAssertEqual(size.height, 16.0) + #elseif os(iOS) + XCTAssertEqual(size.height, 16.0) + #elseif os(macOS) + XCTAssertEqual(size.height, 13.0) + #endif + } + + func testTextSizeCaption() throws { + let size = try render(view: Text("X").font(.caption)).size + #if SKIP + XCTAssertEqual(size.height, 15.0) + #elseif os(iOS) + XCTAssertEqual(size.height, 15.0) + #elseif os(macOS) + XCTAssertEqual(size.height, 13.0) + #endif + } + + func testTextSizeCaption2() throws { + let size = try render(view: Text("X").font(.caption2)).size + #if SKIP + XCTAssertEqual(size.height, 14.0) // 17.0 + #elseif os(iOS) + XCTAssertEqual(size.height, 14.0) + #elseif os(macOS) + XCTAssertEqual(size.height, 13.0) + #endif + } func testDrawTextMonospacedFont() throws { XCTAssertEqual(try pixmap(brightness: 0.75, content: ZStack { diff --git a/Tests/SkipUITests/XCSnapshotTestCase.swift b/Tests/SkipUITests/XCSnapshotTestCase.swift index 7db8c98b..5e00d769 100644 --- a/Tests/SkipUITests/XCSnapshotTestCase.swift +++ b/Tests/SkipUITests/XCSnapshotTestCase.swift @@ -197,13 +197,13 @@ class XCSnapshotTestCase: XCTestCase { /// Renders the given view to a 2-dimensional ASCII pixel map with non-white pixels showing up as "•". func pixmap(brightness: Double = 0.5, content: V) throws -> String { - try render(clear: "FFFFFF", replace: ".", brightness: brightness, antiAlias: false, view: content) + try render(clear: "FFFFFF", replace: ".", brightness: brightness, antiAlias: false, view: content).pixmap } /// Renders the given SwiftUI view as an ASCII string representing the shapes and colors in the view. /// The optional `outputFile` can be specified to save a PNG form of the view to the given file. /// This function handles the three separate scenarios of iOS (UIKit), macOS (AppKit), and Android (SkipKit), which all have different mechanisms for converting a view into a bitmap image. - func render(outputFile: String? = nil, compact: Int? = nil, clear clearColor: String? = nil, replace: String? = nil, brightness: Double = 0.5, darkMode: Bool = false, antiAlias: Bool? = false, view content: V) throws -> String { + func render(outputFile: String? = nil, compact: Int? = nil, clear clearColor: String? = nil, replace: String? = nil, brightness: Double = 0.5, darkMode: Bool = false, antiAlias: Bool? = false, view content: V) throws -> (pixmap: String, size: (width: Double, height: Double)) { #if SKIP // SKIP INSERT: lateinit var renderView: android.view.View @@ -244,8 +244,8 @@ class XCSnapshotTestCase: XCTestCase { var pixels = IntArray(width * height) bitmap.getPixels(pixels, 0, width, 0, 0, width, height) - return createPixmap(pixels: Array(pixels.toList()), compact: compact, clearColor: clearColor, replace: replace, brightness: brightness, width: Int64(width)) - + let pixmap = createPixmap(pixels: Array(pixels.toList()), compact: compact, clearColor: clearColor, replace: replace, brightness: brightness, width: Int64(width)) + return (pixmap: pixmap, size: (width: Double(width), height: Double(height))) #else let v = content.environment(\.colorScheme, darkMode ? .dark : .light).environment(\.displayScale, 1.0) @@ -340,8 +340,8 @@ class XCSnapshotTestCase: XCTestCase { #endif // canImport(AppKit) - return createPixmapFromColors(pixelData: UnsafePointer(pixelData), compact: compact, clearColor: clearColor, replace: replace, brightness: brightness, width: Int(viewSize.width), height: Int(viewSize.height), bytesPerRow: bytesPerRow, bytesPerPixel: bytesPerPixel) - + let pixmap = createPixmapFromColors(pixelData: UnsafePointer(pixelData), compact: compact, clearColor: clearColor, replace: replace, brightness: brightness, width: Int(viewSize.width), height: Int(viewSize.height), bytesPerRow: bytesPerRow, bytesPerPixel: bytesPerPixel) + return (pixmap: pixmap, size: (width: viewSize.width, height: viewSize.height)) #endif }