Skip to content

Commit

Permalink
Add tests comparing logical SwiftUI and Android text styles
Browse files Browse the repository at this point in the history
  • Loading branch information
marcprux committed Sep 27, 2023
1 parent af4abb8 commit 32ea991
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 67 deletions.
28 changes: 15 additions & 13 deletions Sources/SkipUI/SkipUI/Text/Font.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions Tests/SkipUITests/CanvasTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
68 changes: 34 additions & 34 deletions Tests/SkipUITests/ColorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion Tests/SkipUITests/ImageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions Tests/SkipUITests/LayoutTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
""")
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit 32ea991

Please sign in to comment.