Skip to content

Commit

Permalink
fix: add validation for controls information to prevent errors while …
Browse files Browse the repository at this point in the history
…adding tags (#306)
  • Loading branch information
serhii-londar authored Dec 12, 2024
1 parent d31764b commit 7f8973f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ extension CWImage {
bitmap.size = size
return bitmap.representation(using: .png, properties: [:])
}

var scale: Double {
guard let cgImage = cgImage(forProposedRect: nil, context: nil, hints: nil) else { return 1 }
return Double(cgImage.width) / Double(size.width)
}
}

extension CWLabel {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ public struct ControlInformation {
}

class ScreenshotInformationCollector {
#if os(OSX)
static let scale = NSScreen.main?.backingScaleFactor ?? 2.0
#elseif os(iOS) || os(tvOS)
static let scale = UIScreen.main.scale
#endif
static let scale = CWScreen.scale()

class func captureControlsInformation() -> [ControlInformation] {
guard let topViewController = ScreenshotFeature.topViewController else { return [] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ class CrowdinScreenshotUploader: ScreenshotUploader {
errorHandler?(NSError(domain: Errors.screenshotIdIsMissing.rawValue, code: defaultCrowdinErrorCode, userInfo: nil))
return
}

let values = self.proceed(controlsInformation: controlsInformation)
let screenRect = CGRect(origin: .zero, size: screenshot.size.applying(CGAffineTransformScale(.identity, screenshot.scale, screenshot.scale)))
let values = self.proceed(controlsInformation: controlsInformation, screenRect: screenRect)
guard values.count > 0 else {
CrowdinLogsCollector.shared.add(log: .warning(with: "Screenshot uploaded without tags"))
success?()
Expand Down Expand Up @@ -205,8 +205,8 @@ class CrowdinScreenshotUploader: ScreenshotUploader {
errorHandler?(NSError(domain: Errors.screenshotIdIsMissing.rawValue, code: defaultCrowdinErrorCode, userInfo: nil))
return
}

let values = self.proceed(controlsInformation: controlsInformation)
let screenRect = CGRect(origin: .zero, size: screenshot.size.applying(CGAffineTransformScale(.identity, screenshot.scale, screenshot.scale)))
let values = self.proceed(controlsInformation: controlsInformation, screenRect: screenRect)
guard values.count > 0 else {
CrowdinLogsCollector.shared.add(log: .warning(with: "Screenshot uploaded without tags"))
success?(.udpated)
Expand All @@ -231,13 +231,26 @@ class CrowdinScreenshotUploader: ScreenshotUploader {
}
}

func proceed(controlsInformation: [ControlInformation]) -> [(id: Int, rect: CGRect)] {
func proceed(controlsInformation: [ControlInformation], screenRect: CGRect) -> [(id: Int, rect: CGRect)] {
var results = [(id: Int, rect: CGRect)]()
var controlsWithId = [(id: Int, rect: CGRect)]()
controlsInformation.forEach { (controlInformation) in
if let id = mappingManager.id(for: controlInformation.key) {
results.append((id: id, rect: controlInformation.rect))
controlsWithId.append((id: id, rect: controlInformation.rect))
}
}

controlsWithId.forEach({
if screenRect.contains($0.rect) {
results.append($0)
} else {
let visibleRect = screenRect.intersection($0.rect)
if visibleRect.isValid {
results.append(($0.id, visibleRect))
}
}
})

return results
}

Expand All @@ -261,4 +274,13 @@ class CrowdinScreenshotUploader: ScreenshotUploader {
}
}

extension CGRect {
func contains(rect: CGRect) -> Bool {
return self.contains(rect.origin) &&
self.contains(CGPoint(x: rect.maxX, y: rect.maxY)) &&
self.contains(CGPoint(x: rect.maxX, y: rect.minY)) &&
self.contains(CGPoint(x: rect.minX, y: rect.maxY))
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ extension CrowdinSDK {
public class func captureScreenshotSync(name: String, image: CWImage, application: XCUIApplication) -> Error? {
var error: Error?
let semaphore = DispatchSemaphore(value: 0)
CrowdinSDK.captureScreenshot(name: name, screenshot: image, controlsInformation: application.getControlsInformation(), success: { }, errorHandler: {
CrowdinSDK.captureScreenshot(name: name, screenshot: image, controlsInformation: application.getControlsInformation(), success: {
semaphore.signal()
}, errorHandler: {
error = $0
semaphore.signal()
})
Expand Down

0 comments on commit 7f8973f

Please sign in to comment.