99
1010#import " XCUIElement+FBIsVisible.h"
1111
12+ #import " FBApplication.h"
1213#import " FBConfiguration.h"
14+ #import " FBMathUtils.h"
1315#import " FBXCodeCompatibility.h"
1416#import " XCElementSnapshot+FBHelpers.h"
15- #import " XCElementSnapshot+FBHitPoint.h"
16- #import " XCTestPrivateSymbols.h"
1717#import " XCUIElement+FBUtilities.h"
18+ #import " XCTestPrivateSymbols.h"
19+ #import < XCTest/XCUIDevice.h>
20+ #import " XCElementSnapshot+FBHitPoint.h"
1821
1922@implementation XCUIElement (FBIsVisible)
2023
@@ -27,35 +30,35 @@ - (BOOL)fb_isVisible
2730
2831@implementation XCElementSnapshot (FBIsVisible)
2932
30- - (BOOL )fb_isVisibleInContainerWindow : (XCElementSnapshot *)window hierarchyIntersection : (nullable NSValue *)intersectionRectange appFrame : (CGRect)appFrame
31- {
32- CGRect currentRectangle = nil == intersectionRectange ? self.frame : [intersectionRectange CGRectValue ];
33- XCElementSnapshot *parent = self.parent ;
34- CGRect intersectionWithParent = CGRectIntersection (currentRectangle, parent.frame );
35- if (CGRectIsEmpty (intersectionWithParent)) {
36- return NO ;
37- }
38- if (parent == window) {
39- // Assume the element is visible if its root container is hittable and the frame is onscreen
40- return CGRectContainsPoint (appFrame, self.fb_hitPoint );
41- }
42- return [parent fb_isVisibleInContainerWindow: window hierarchyIntersection: [NSValue valueWithCGRect: intersectionWithParent] appFrame: appFrame];
43- }
44-
4533- (BOOL )fb_isVisible
4634{
47- if (CGRectIsEmpty (self.frame )) {
35+ CGRect frame = self.frame ;
36+ if (CGRectIsEmpty (frame)) {
4837 return NO ;
4938 }
5039 if ([FBConfiguration shouldUseTestManagerForVisibilityDetection ]) {
5140 return [(NSNumber *)[self fb_attributeValue: FB_XCAXAIsVisibleAttribute] boolValue ];
5241 }
5342 CGRect appFrame = [self fb_rootElement ].frame ;
54- XCElementSnapshot *containerWindow = [self fb_parentMatchingType: XCUIElementTypeWindow];
55- if (nil != containerWindow) {
56- return [self fb_isVisibleInContainerWindow: containerWindow hierarchyIntersection: nil appFrame: appFrame];
43+ CGSize screenSize = FBAdjustDimensionsForApplication (appFrame.size , (UIInterfaceOrientation)[XCUIDevice sharedDevice ].orientation );
44+ CGRect screenFrame = CGRectMake (0 , 0 , screenSize.width , screenSize.height );
45+ if (!CGRectIntersectsRect (frame, screenFrame)) {
46+ return NO ;
47+ }
48+ CGPoint midPoint = [self .suggestedHitpoints.lastObject CGPointValue ];
49+ XCElementSnapshot *hitElement = [self hitTest: midPoint];
50+ if (self == hitElement || [self ._allDescendants.copy containsObject: hitElement]) {
51+ return YES ;
52+ }
53+ if (CGRectContainsPoint (appFrame, self.fb_hitPoint )) {
54+ return YES ;
55+ }
56+ for (XCElementSnapshot *elementSnapshot in self.children .copy ) {
57+ if (elementSnapshot.fb_isVisible ) {
58+ return YES ;
59+ }
5760 }
58- return CGRectContainsPoint (appFrame, self. fb_hitPoint ) ;
61+ return NO ;
5962}
6063
6164@end
0 commit comments