Skip to content

Commit

Permalink
Restore iOS AvatarTests (microsoft#1964)
Browse files Browse the repository at this point in the history
* Restore AvatarTests and enable for SPM

* Simplify imports for tests

* Remove SPM test target for the moment
  • Loading branch information
mischreiber authored Feb 9, 2024
1 parent 99d6ede commit 65a2c71
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 14 deletions.
14 changes: 4 additions & 10 deletions ios/FluentUI.Tests/AvatarTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,10 @@
//

import XCTest
import FluentUI
@testable import FluentUI

class AvatarTests: XCTestCase {

/// Validates that the number of colors defined for tokens textCalculatedBackgroundColors and textCalculatedForegroundColors are the same.
func testTextCalculatedBackgroundForegroundColorsCount() {
XCTAssertEqual(FluentUIThemeManager.S.MSFAvatarTokens.textCalculatedBackgroundColors.count,
FluentUIThemeManager.S.MSFAvatarTokens.textCalculatedForegroundColors.count,
"Text calculated background and foreground colors should provide the same number of options.")
}

/// Validates that the background and foreground colors for a given index in both arrays (textCalculatedBackgroundColors and textCalculatedForegroundColors tokens) match comparing:
/// 1. A color light mode background color with its counterpart dark mode foreground color. The color should be the same (tint40)
/// 2. A color light mode foreground color with its counterpart dark mode background color. The color should be the same (shade30)
Expand All @@ -27,8 +20,9 @@ class AvatarTests: XCTestCase {
/// - Background: shade30
/// - Foreground: tint40
func testTextCalculatedBackgroundForegroundColorsMatch() {
let bgColors = FluentUIThemeManager.S.MSFAvatarTokens.textCalculatedBackgroundColors
let fgColors = FluentUIThemeManager.S.MSFAvatarTokens.textCalculatedForegroundColors
let colors = Avatar.CalculatedColors.colors
let bgColors = (0..<colors.count).map { Avatar.CalculatedColors.backgroundColor(hashCode: $0) }
let fgColors = (0..<colors.count).map { Avatar.CalculatedColors.foregroundColor(hashCode: $0) }
let lightModeTraitCollection = UITraitCollection(userInterfaceStyle: .light)
let darkModeTraitCollection = UITraitCollection(userInterfaceStyle: .dark)

Expand Down
4 changes: 4 additions & 0 deletions ios/FluentUI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@
926FEEAA2B45A8B4002C61D0 /* Compatibility.swift in Sources */ = {isa = PBXBuildFile; fileRef = 926FEEA92B45A8B4002C61D0 /* Compatibility.swift */; };
9275105626815A7100F12730 /* MSFPersonaButtonCarousel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9275105426815A7100F12730 /* MSFPersonaButtonCarousel.swift */; };
927EB2BD278627440069753D /* PersonaButtonModifiers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 927EB2BC278627440069753D /* PersonaButtonModifiers.swift */; };
929215B92B6C75E500D4EA9F /* AvatarTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 929215B82B6C75E500D4EA9F /* AvatarTests.swift */; };
9298798B2669A875002B1EB4 /* PersonaButtonTokenSet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 927E34C62668350800998031 /* PersonaButtonTokenSet.swift */; };
929DD257266ED3AC00E8175E /* PersonaButtonCarouselTokenSet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 929DD255266ED3AC00E8175E /* PersonaButtonCarouselTokenSet.swift */; };
929DD25A266ED3B600E8175E /* PersonaButtonCarousel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 929DD258266ED3B600E8175E /* PersonaButtonCarousel.swift */; };
Expand Down Expand Up @@ -359,6 +360,7 @@
9275105426815A7100F12730 /* MSFPersonaButtonCarousel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MSFPersonaButtonCarousel.swift; sourceTree = "<group>"; };
927E34C62668350800998031 /* PersonaButtonTokenSet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PersonaButtonTokenSet.swift; sourceTree = "<group>"; };
927EB2BC278627440069753D /* PersonaButtonModifiers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PersonaButtonModifiers.swift; sourceTree = "<group>"; };
929215B82B6C75E500D4EA9F /* AvatarTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AvatarTests.swift; sourceTree = "<group>"; };
929DD255266ED3AC00E8175E /* PersonaButtonCarouselTokenSet.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PersonaButtonCarouselTokenSet.swift; sourceTree = "<group>"; };
929DD258266ED3B600E8175E /* PersonaButtonCarousel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PersonaButtonCarousel.swift; sourceTree = "<group>"; };
92A1E4F326A791590007ED60 /* MSFCardNudge.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MSFCardNudge.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -983,6 +985,7 @@
A5CEC15E20D980B30016922A /* FluentUI.Tests */ = {
isa = PBXGroup;
children = (
929215B82B6C75E500D4EA9F /* AvatarTests.swift */,
8FA3CB5A246B19EA0049E431 /* ColorTests.swift */,
FD053A342224CA33009B6378 /* DatePickerControllerTests.swift */,
A5CEC15F20D980B30016922A /* FluentUITests.swift */,
Expand Down Expand Up @@ -1751,6 +1754,7 @@
files = (
924268A2277AD9F700C5A452 /* FontTests.swift in Sources */,
A5CEC16020D980B30016922A /* FluentUITests.swift in Sources */,
929215B92B6C75E500D4EA9F /* AvatarTests.swift in Sources */,
8FA3CB5B246B19EA0049E431 /* ColorTests.swift in Sources */,
FD053A352224CA33009B6378 /* DatePickerControllerTests.swift in Sources */,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
enableASanStackUseAfterReturn = "YES">
<Testables>
<TestableReference
skipped = "YES">
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "A5CEC15920D980B30016922A"
Expand Down
6 changes: 3 additions & 3 deletions ios/FluentUI/Avatar/Avatar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ public struct Avatar: View, TokenizedControlView {
}

/// Handles calculating colors for Avatar foreground and background.
private struct CalculatedColors {
struct CalculatedColors {
static func backgroundColor(hashCode: Int) -> UIColor {
let colorSet = colors[hashCode % colors.count]
return UIColor(light: GlobalTokens.sharedColor(colorSet, .tint40),
Expand Down Expand Up @@ -492,7 +492,7 @@ public struct Avatar: View, TokenizedControlView {
/// Hash algorithm to determine Avatar color.
/// Referenced from: https://github.com/microsoft/fluentui/blob/master/packages/react-components/react-avatar/src/components/Avatar/useAvatar.tsx#L200
/// - Returns: Hash code
private static func hashCode(_ text: NSString) -> Int32 {
static func hashCode(_ text: NSString) -> Int32 {
var hash: Int32 = 0
for len in (0..<text.length).reversed() {
// Convert from `unichar` to `Int32` to avoid potential arithmetic overflow in the next few lines.
Expand All @@ -505,7 +505,7 @@ public struct Avatar: View, TokenizedControlView {
return hash
}

private static var colors: [GlobalTokens.SharedColorSet] = [
static var colors: [GlobalTokens.SharedColorSet] = [
.darkRed,
.cranberry,
.red,
Expand Down

0 comments on commit 65a2c71

Please sign in to comment.