Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve visibility check #41

Merged
merged 3 commits into from
Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions Sources/Hammer/Utilties/Subviews.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,11 @@ extension EventGenerator {
return currentView == self.window
}

let adjustedBounds = view.convert(view.bounds, to: superview)
guard superview.bounds.isVisible(adjustedBounds, visibility: visibility) else {
return false
if superview.clipsToBounds {
let adjustedBounds = view.convert(view.bounds, to: superview)
guard superview.bounds.isVisible(adjustedBounds, visibility: visibility) else {
return false
}
}

return viewIsVisible(currentView: superview)
Expand Down
76 changes: 76 additions & 0 deletions Tests/HammerTests/HandTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Hammer
import UIKit
import XCTest

// swiftlint:disable:next type_body_length
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll split these up in a future PR

final class HandTests: XCTestCase {
func testButtonTap() throws {
let view = UIButton().size(width: 100, height: 100)
Expand Down Expand Up @@ -71,6 +72,71 @@ final class HandTests: XCTestCase {
}
}

func testButtonTapInsideOfBounds() throws {
let containerView = UIView().size(width: 100, height: 100)
containerView.backgroundColor = .blue
containerView.clipsToBounds = false

let view = UIButton().size(width: 100, height: 100)
view.backgroundColor = .green
containerView.addSubview(view)
view.setOrigin(x: 0, y: 0)

let expectation = XCTestExpectation(description: "Button Tapped")
view.addHandler(forEvent: .primaryActionTriggered, action: expectation.fulfill)

let eventGenerator = try EventGenerator(view: containerView)
try eventGenerator.waitUntilHittable(view, timeout: 1)
try eventGenerator.fingerTap(at: view)

XCTAssertEqual(XCTWaiter.wait(for: [expectation], timeout: 1), .completed)
}

func testButtonTapOutOfBounds() throws {
let containerView = UIView().size(width: 100, height: 100)
containerView.backgroundColor = .blue

let view = UIButton().size(width: 100, height: 100)
view.backgroundColor = .green
containerView.addSubview(view)
view.setOrigin(x: 0, y: 100)

let eventGenerator = try EventGenerator(view: containerView)
try eventGenerator.wait(0.5)

do {
try eventGenerator.fingerTap(at: view)
XCTFail("Button should not be tappable")
} catch HammerError.viewIsNotHittable {
// Success
} catch {
throw error
}
}

func testButtonTapOutOfBoundsClipped() throws {
let containerView = UIView().size(width: 100, height: 100)
containerView.backgroundColor = .blue
containerView.clipsToBounds = true

let view = UIButton().size(width: 100, height: 100)
view.backgroundColor = .green
containerView.addSubview(view)
view.setOrigin(x: 0, y: 100)

let eventGenerator = try EventGenerator(view: containerView)
try eventGenerator.wait(0.5)

do {
try eventGenerator.fingerTap(at: view)
XCTFail("Button should not be tappable")
} catch HammerError.viewIsNotVisible {
// Success
} catch {
throw error
}
}

func testButtonTapOnNonInteractiveSuperview() throws {
let view = UIButton().size(width: 100, height: 100)
view.accessibilityIdentifier = "my_button"
Expand Down Expand Up @@ -268,4 +334,14 @@ extension UIView {
self.heightAnchor.constraint(equalToConstant: height).isActive = true
return self
}

fileprivate func setOrigin(x: CGFloat, y: CGFloat) {
guard let superview = self.superview else {
fatalError("View must be added as a subview before setting origin constraints")
}

self.translatesAutoresizingMaskIntoConstraints = false
self.leadingAnchor.constraint(equalTo: superview.leadingAnchor, constant: x).isActive = true
self.topAnchor.constraint(equalTo: superview.topAnchor, constant: y).isActive = true
}
}