Skip to content

Commit

Permalink
Papp 339- fixing status bar, height and width of iphones (#9)
Browse files Browse the repository at this point in the history
* debugging with sending app info in status and nav bar

* debugging with sending app info in status and nav bar

* debugging with sending app info in status and nav bar

* debugging with sending app info in status and nav bar

* debugging with sending app info in status and nav bar

* debugging with sending app info in status and nav bar

* debugging with sending app info in status and nav bar

* debugging with sending app info in status and nav bar

* revert

* sending new data

* fixing filepath

* fixing filepath

* fixing filepath

* fixing filepath

* fixing filepath

* fixing filepath

* using case

* fixing error

* fixing width

* fixing height

* adding tests

* fixing lints
  • Loading branch information
prklm10 authored Aug 7, 2023
1 parent ad65055 commit a2e9a86
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 12 deletions.
3 changes: 2 additions & 1 deletion percy-xcui/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ let package = Package(
targets: [
.target(
name: "PercyXcui",
dependencies: [])
dependencies: []
)
]
)
60 changes: 53 additions & 7 deletions percy-xcui/Sources/PercyXcui/metadata/Metadata.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,24 @@ internal class Metadata {
}

public func deviceScreenWidth() -> CGFloat {
let screenBounds = UIScreen.main.bounds
return screenBounds.width * UIScreen.main.scale
return CGFloat(mapToDeviceWidth(identifier: deviceName().lowercased())) * UIScreen.main.scale
}

public func deviceScreenHeight() -> CGFloat {
let screenBounds = UIScreen.main.bounds
return screenBounds.height * UIScreen.main.scale
return CGFloat(mapToDeviceHeight(identifier: deviceName().lowercased())) * UIScreen.main.scale
}

public func statBarHeight() -> Int {
if options.statusBarHeight != -1 {
return options.statusBarHeight
}

return Int(UIApplication.shared.statusBarFrame.height * UIScreen.main.scale)
return Int(CGFloat(mapToDeviceStatusBar(identifier: deviceName().lowercased())) * UIScreen.main.scale)
}

public func navBarHeight() -> Int {
if options.navigationBarHeight != -1 {
return options.navigationBarHeight
}

return 0
}

Expand All @@ -65,6 +61,16 @@ internal class Metadata {
}
}

func getDefaultStatusBarHeight(forDevice device: String) -> Int {
if device.lowercased().contains("iphone") {
return 44 // Default status bar height for iPhone
} else if device.lowercased().contains("ipad") {
return 20 // Default status bar height for iPad
} else {
return 44 // Default status bar height for unknown devices
}
}

// swiftlint:disable:next cyclomatic_complexity function_body_length
func mapToDevice(identifier: String) -> String {
#if os(iOS)
Expand Down Expand Up @@ -162,4 +168,44 @@ internal class Metadata {
}
#endif
}

func mapToDeviceStatusBar(identifier: String) -> Int {
switch identifier {
case "iphone 14 pro", "iphone 14 pro max": return 54
case "iphone 14", "iphone 14 plus": return 47
case "iPhone 13", "iphone 13 pro", "iphone 13 pro max": return 47
case "iphone 12", "iphone 12 pro", "iphone 12 pro max": return 47
case "iPhone 12 Mini": return 50
case "iphone 11": return 48
case "iphone 11 pro", "iphone 11 pro max": return 44
default: return getDefaultStatusBarHeight(forDevice: identifier)
}
}

func mapToDeviceWidth(identifier: String) -> Int {
switch identifier {
case "iphone 14 pro max": return 430
case "iphone 14 pro": return 393
case "iphone 14 plus", "iphone 12 pro max", "iphone 13 pro max": return 428
case "iphone 14", "iphone 13 pro", "iphone 13": return 390
case "iphone 13 mini", "iphone 12 mini", "iphone 11 pro": return 375
case "iphone 12 pro", "iphone 12": return 390
case "iphone 11 pro max": return 418
case "iphone 11": return 414
default: return Int(UIScreen.main.bounds.width)
}
}

func mapToDeviceHeight(identifier: String) -> Int {
switch identifier {
case "iphone 14 pro max": return 932
case "iphone 14 pro": return 852
case "iphone 14 plus", "iphone 12 pro max", "iphone 13 pro max": return 926
case "iphone 14", "iphone 13 pro", "iphone 13": return 844
case "iphone 13 mini", "iphone 12 mini", "iphone 11 pro": return 812
case "iphone 12 pro", "iphone 12": return 844
case "iphone 11 pro max", "iphone 11": return 896
default: return Int(UIScreen.main.bounds.height)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import XCTest
final class MetadataTests: XCTestCase {
var meta: Metadata = Metadata()

class MockMetadata: Metadata {
var deviceName: String = "iPhone 14 Pro"
override func deviceName() -> String {
return deviceName
}
}

override func setUp() {
meta = Metadata()
}
Expand Down Expand Up @@ -35,13 +42,21 @@ final class MetadataTests: XCTestCase {
}

func testDeviceScreenHeight() throws {
XCTAssertEqual(meta.deviceScreenHeight(), UIScreen.main.bounds.height * UIScreen.main.scale)
let mockMetadata = MockMetadata()
mockMetadata.deviceName = "iPhone 14 Pro"
XCTAssertEqual(mockMetadata.deviceScreenHeight(), 852 * Int(UIScreen.main.scale))
}

func testStatusBarHeight() throws {
XCTAssertEqual(
meta.statBarHeight(), Int(UIApplication.shared.statusBarFrame.height * UIScreen.main.scale))
func testDeviceScreenWidth() throws {
let mockMetadata = MockMetadata()
mockMetadata.deviceName = "iPhone 14 Pro"
XCTAssertEqual(mockMetadata.deviceScreenWidth(), 393 * Int(UIScreen.main.scale))
}

func testStatusBarHeight() throws {
let mockMetadata = MockMetadata()
mockMetadata.deviceName = "iPhone 14 Pro"
XCTAssertEqual(mockMetadata.statBarHeight(), 54 * Int(UIScreen.main.scale))
// with options set
let options = ScreenshotOptions()
options.statusBarHeight = 100
Expand All @@ -62,4 +77,22 @@ final class MetadataTests: XCTestCase {
func testDeviceName() throws {
XCTAssertTrue(meta.deviceName().contains("iPhone"))
}

func testMapToDeviceHeight() throws {
XCTAssertTrue(meta.mapToDeviceHeight("iphone 14 pro max"), 932)
}

func testMapToDeviceWidth() throws {
XCTAssertTrue(meta.mapToDeviceWidth("iphone 14 pro max"), 430)
}

func testMapToDeviceStatusBar() throws {
XCTAssertTrue(meta.mapToDeviceWidth("iphone 14 pro max"), 54)
}

func testGetDefaultStatusBarHeight() throws {
XCTAssertEqual(meta.getDefaultStatusBarHeight("ipad"), 20)
XCTAssertEqual(meta.getDefaultStatusBarHeight("iphone"), 44)
XCTAssertEqual(meta.getDefaultStatusBarHeight("itest"), 44)
}
}

0 comments on commit a2e9a86

Please sign in to comment.