Skip to content

Commit

Permalink
Merge pull request #43 from Nirma/fix/XSXRSimulatorBug
Browse files Browse the repository at this point in the history
Make simulator detection appear as an actual device
  • Loading branch information
Nirma authored Oct 4, 2019
2 parents 6d344ad + 59a509d commit 0dbbf89
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 99 deletions.
15 changes: 12 additions & 3 deletions Sources/DeviceFamily.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public enum DeviceFamily: String {
case iPhone
case iPod
case iPad
case simulator
case unknown

public init(rawValue: String) {
Expand All @@ -37,10 +36,20 @@ public enum DeviceFamily: String {
self = .iPod
case "iPad":
self = .iPad
case "x86_64", "i386":
self = .simulator
default:
self = .unknown
}
}
}

// MARK: Simulator Detection

extension DeviceFamily {
public var isSimulator: Bool {
#if arch(i386) || arch(x86_64)
return true
#else
return false
#endif
}
}
46 changes: 0 additions & 46 deletions Sources/DeviceModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ extension DeviceModel {
self = DeviceModel.detectIpadModel(with: identifier)
case .iPod:
self = DeviceModel.detectIpodModel(with: identifier)
case .simulator:
self = DeviceModel.detectSimulatorModel()
default:
self = .unknown
}
Expand Down Expand Up @@ -170,50 +168,6 @@ extension DeviceModel {
}


// MARK: Detecting Simulator Models

extension DeviceModel {
fileprivate static func detectSimulatorModel() -> DeviceModel {
guard let versionInfo = ProcessInfo.processInfo.environment["SIMULATOR_VERSION_INFO"],
let startIdx = versionInfo.range(of: "DeviceType: ")?.upperBound else {
return .unknown
}
let deviceType = versionInfo[startIdx..<versionInfo.endIndex]
switch deviceType {
case "iPhone 4s": return .iPhone4S
case "iPhone 5s": return .iPhone5S
case "iPhone 6": return .iPhone6
case "iPhone 6 Plus": return .iPhone6Plus
case "iPhone 6s": return .iPhone6S
case "iPhone 6s Plus": return .iPhone6SPlus
case "iPhone SE": return .iPhoneSE
case "iPhone 7": return .iPhone7
case "iPhone 7 Plus": return .iPhone7Plus
case "iPhone 8": return .iPhone8
case "iPhone 8 Plus": return .iPhone8Plus
case "iPhone X": return .iPhoneX
case "iPhone XS": return .iPhoneXS
case "iPhone XS Max": return .iPhoneXSMax
case "iPhone XR": return .iPhoneXR
case "iPhone 11": return .iPhone11
case "iPhone 11 Pro": return .iPhone11Pro
case "iPhone 11 Pro Max": return .iPhone11ProMax
case "iPad (5th generation)": return .iPadFifthGen
case "iPad (6th generation)": return .iPadSixthGen
case "iPad Air": return .iPadAir
case "iPad Air 2": return .iPadAir2
case "iPad Air 3": return .iPadAir3
case "iPad Pro (9.7-inch)": return .iPadPro9_7Inch
case "iPad Pro (10.5-inch)": return .iPadPro10_5Inch
case "iPad Pro (11-inch)": return .iPadPro11Inch
case "iPad Pro (12.9-inch)": return .iPadPro12_9Inch
case "iPad Pro (12.9-inch) (2nd generation)": return .iPadPro12_9Inch_SecondGen
case "iPad Pro (12.9-inch) (3rd generation)": return .iPadPro12_9Inch_ThirdGen
default: return .unknown
}
}
}

// MARK: Detecting the Notch

extension DeviceModel {
Expand Down
7 changes: 0 additions & 7 deletions Sources/Identifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ struct Identifier {
extension Identifier {
static func typeVersionComponents(with identifierString: String) -> (type: String, major: Int?, minor: Int?) {

// Simulator Check
if identifierString == "x86_64" || identifierString == "i386" {
return (identifierString, nil, nil)
}

let numericCharacters: [String] = (0...9).map { "\($0)" }
let type = identifierString.prefix(while: { !numericCharacters.contains(String($0))})

Expand Down Expand Up @@ -74,8 +69,6 @@ extension Identifier: CustomStringConvertible {
return iPadStringRepresentation(major: major, minor: minor)
case .iPod:
return iPodStringRepresentation(major: major, minor: minor)
case .simulator:
return "Simulator"
case .unknown:
return "unknown"
}
Expand Down
11 changes: 10 additions & 1 deletion Sources/System.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,17 @@ class System {
// and the rest of the string being zeroed out.
let encoding: UInt = String.Encoding.ascii.rawValue
if let string = NSString(bytes: &systemInfo.machine, length: Int(_SYS_NAMELEN), encoding: encoding) {
return (string as String).components(separatedBy: "\0").first
let identifier = (string as String).components(separatedBy: "\0").first

// Simulator Check
if identifier == "x86_64" || identifier == "i386" {
return ProcessInfo.processInfo.environment["SIMULATOR_MODEL_IDENTIFIER"]
}

return identifier
}


return nil
}
}
4 changes: 0 additions & 4 deletions Sources/UIDeviceExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ public extension UIDeviceComplete where Base == UIDevice {
return deviceFamily == .iPod
}

/// Simulator
var isSimulator: Bool {
return deviceFamily == .simulator
}
}


Expand Down
18 changes: 14 additions & 4 deletions Tests/DeviceFamilyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,19 @@ class DeviceFamilyTests: XCTestCase {
let deviceFamily = DeviceFamily(rawValue: "iPad")
XCTAssert(deviceFamily == .iPad, "DeviceFamily - .iPad is failing")
}

func testDeviceFamilySimulator() {
let deviceFamily = DeviceFamily(rawValue: "x86_64")
XCTAssert(deviceFamily == .simulator, "DeviceFamily - .simulator is failing")

func testInvalidDeviceFamily() {
let deviceFamily = DeviceFamily(rawValue: "Apple II")
XCTAssert(deviceFamily == .unknown, "DeviceFamily - .unknown is failing")
}

func testDeviceIsSimulator() {
let deviceFamily = DeviceFamily(rawValue: "iPhone")
#if arch(i386) || arch(x86_64)
XCTAssert(deviceFamily.isSimulator, "DeviceFamily - .isSimulator is failing")
#else
XCTAssert(!(deviceFamily.isSimulator), "DeviceFamily - .isSimulator is failing")
#endif
}

}
11 changes: 5 additions & 6 deletions Tests/DeviceModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -325,14 +325,13 @@ class DeviceModelTests: XCTestCase {
XCTAssert(deviceModel == .iPodTouchSeventhGen , "DeviceModel - .iPodSeventhGen is failing")
}

// MARK: Simulator Test
// MARK: Unknown Device Test

func testDeviceModelSimulator() {
let deviceModel = DeviceModel(identifier: Identifier("unknown"))

XCTAssert(deviceModel == .unknown , "DeviceModel - .unknown is failing")
func testInvalidDeviceModel() {
let deviceModel = DeviceModel(identifier: Identifier("iPhone0,1"))
XCTAssert(deviceModel == .unknown, "DeviceModel - .unknown is failing")
}

// MARK: Notch test
func testHasNotch() {
let notchModels: [DeviceModel] = [.iPhoneX, .iPhoneXS, .iPhoneXSMax, .iPhoneXR, .iPhone11, .iPhone11Pro, .iPhone11ProMax]
Expand Down
24 changes: 0 additions & 24 deletions Tests/IdentifierTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,30 +62,6 @@ class IdentifierTests: XCTestCase {
"Identifier initilization is failing"
)
}

func testIdentifierInitSimulatorx86() {
let testString = "x86_64"
let identifier = Identifier(testString)

XCTAssert(
(identifier.type == .simulator) &&
(identifier.version.major == nil) &&
(identifier.version.minor == nil),
"Identifier initilization is failing"
)
}

func testIdentifierInitSimulatori386() {
let testString = "i386"
let identifier = Identifier(testString)

XCTAssert(
(identifier.type == .simulator) &&
(identifier.version.major == nil) &&
(identifier.version.minor == nil),
"Identifier initilization is failing"
)
}


// MARK: - iPhone String Description tests
Expand Down
4 changes: 0 additions & 4 deletions Tests/UIDeviceExtensionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,4 @@ class UIDeviceExtensionsTests: XCTestCase {
XCTAssert(deviceFamily == .iPod, "DeviceExtensions - .isIpod is failing")
}

func testDeviceExtensionsIsSimulator() {
let deviceFamily = DeviceFamily(rawValue: "x86_64")
XCTAssert(deviceFamily == .simulator, "DeviceExtensions - .isSimulator is failing")
}
}

0 comments on commit 0dbbf89

Please sign in to comment.