Skip to content

Commit

Permalink
fix(iOS): clamp Double values before converting to Integer.
Browse files Browse the repository at this point in the history
To avoid Swift runtime failure:
`Double value cannot be converted to Int because the result would be less than Int.min`
  • Loading branch information
asafkorem committed Oct 28, 2024
1 parent dd2ee8e commit f7f7c1c
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions detox/ios/Detox/Utilities/ViewHierarchyGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ struct ViewHierarchyGenerator {
) -> String {
var attributes: [String: String] = [
"class": "\(type(of: view))",
"width": "\(Int(view.frame.size.width))",
"height": "\(Int(view.frame.size.height))",
"width": "\(toClampedInt(view.frame.size.width))",
"height": "\(toClampedInt(view.frame.size.height))",
"visibility": view.isHidden ? "invisible" : "visible",
"alpha": "\(view.alpha)",
"focused": "\(view.isFocused)",
Expand All @@ -144,8 +144,9 @@ struct ViewHierarchyGenerator {

if let superview = view.superview {
let location = view.convert(view.bounds.origin, to: superview)
attributes["x"] = "\(Int(location.x))"
attributes["y"] = "\(Int(location.y))"
// todo: error
attributes["x"] = "\(toClampedInt(location.x))"
attributes["y"] = "\(toClampedInt(location.y))"
}

if shouldInjectIdentifiers {
Expand Down Expand Up @@ -221,3 +222,7 @@ class WebViewHandler: NSObject, WKNavigationDelegate {
}
}
}

func toClampedInt(_ value: Double) -> Int {
return Int(min(max(value, Double(Int.min)), Double(Int.max)))
}

0 comments on commit f7f7c1c

Please sign in to comment.