From a2e9a86dfc3f5b69ef53cbda28a0ea71098c9f77 Mon Sep 17 00:00:00 2001 From: Pradum Kumar Date: Mon, 7 Aug 2023 18:39:21 +0530 Subject: [PATCH] Papp 339- fixing status bar, height and width of iphones (#9) * 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 --- percy-xcui/Package.swift | 3 +- .../Sources/PercyXcui/metadata/Metadata.swift | 60 ++++++++++++++++--- .../metadata/MetadataTests.swift | 41 +++++++++++-- 3 files changed, 92 insertions(+), 12 deletions(-) diff --git a/percy-xcui/Package.swift b/percy-xcui/Package.swift index 43452aa..0621eb7 100644 --- a/percy-xcui/Package.swift +++ b/percy-xcui/Package.swift @@ -13,6 +13,7 @@ let package = Package( targets: [ .target( name: "PercyXcui", - dependencies: []) + dependencies: [] + ) ] ) diff --git a/percy-xcui/Sources/PercyXcui/metadata/Metadata.swift b/percy-xcui/Sources/PercyXcui/metadata/Metadata.swift index c56c630..67abbac 100644 --- a/percy-xcui/Sources/PercyXcui/metadata/Metadata.swift +++ b/percy-xcui/Sources/PercyXcui/metadata/Metadata.swift @@ -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 } @@ -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) @@ -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) + } + } } diff --git a/xcui-sdk-test-appUITests/PercyXcuiSDKTests/metadata/MetadataTests.swift b/xcui-sdk-test-appUITests/PercyXcuiSDKTests/metadata/MetadataTests.swift index be4dc7a..21b56de 100644 --- a/xcui-sdk-test-appUITests/PercyXcuiSDKTests/metadata/MetadataTests.swift +++ b/xcui-sdk-test-appUITests/PercyXcuiSDKTests/metadata/MetadataTests.swift @@ -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() } @@ -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 @@ -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) + } }