Skip to content

Commit 4cf1701

Browse files
Internal
PiperOrigin-RevId: 476134510
1 parent 99ba3b6 commit 4cf1701

File tree

2 files changed

+5
-28
lines changed

2 files changed

+5
-28
lines changed

UILib/GREYScreenshotter.m

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,6 @@
3333
// Private class for ModalHostingWindow window that doesn't work with drawViewHierarchyInRect.
3434
static Class gUIModalItemHostingWindowClass;
3535

36-
/**
37-
* @return A current screen if the window exists and @c nil if it does not.
38-
*/
39-
static UIScreen *MainScreen(void) {
40-
UIScreen *mainScreen = [GREYUILibUtils screen];
41-
if (!mainScreen || CGRectEqualToRect(mainScreen.bounds, CGRectNull)) {
42-
return nil;
43-
} else {
44-
return mainScreen;
45-
}
46-
}
47-
4836
@implementation GREYScreenshotter
4937

5038
+ (void)initialize {
@@ -60,8 +48,7 @@ + (void)drawScreenInContext:(CGContextRef)bitmapContextRef
6048
withStatusBar:(BOOL)includeStatusBar {
6149
GREYFatalAssertWithMessage(CGBitmapContextGetBitmapInfo(bitmapContextRef) != 0,
6250
@"The context ref must point to a CGBitmapContext.");
63-
UIScreen *mainScreen = MainScreen();
64-
if (!mainScreen) return;
51+
UIScreen *mainScreen = [GREYUILibUtils screen];
6552
CGRect screenRect = mainScreen.bounds;
6653
[self drawScreenInContext:bitmapContextRef
6754
afterScreenUpdates:afterUpdates
@@ -89,9 +76,7 @@ + (UIImage *)snapshotElement:(id)element {
8976
? (UIView *)element
9077
: [element grey_viewContainingSelf];
9178

92-
UIScreen *mainScreen = MainScreen();
93-
if (!mainScreen) return nil;
94-
UIGraphicsBeginImageContextWithOptions(elementAXFrame.size, NO, mainScreen.scale);
79+
UIGraphicsBeginImageContextWithOptions(elementAXFrame.size, NO, [GREYUILibUtils screen].scale);
9580
[self drawViewInContext:UIGraphicsGetCurrentContext()
9681
view:viewToSnapshot
9782
bounds:elementAXFrame
@@ -111,9 +96,7 @@ + (NSString *)saveImageAsPNG:(UIImage *)image
11196

11297
+ (UIImage *)grey_takeScreenshotAfterScreenUpdates:(BOOL)afterScreenUpdates
11398
withStatusBar:(BOOL)includeStatusBar {
114-
UIScreen *mainScreen = MainScreen();
115-
if (!mainScreen) return nil;
116-
CGRect screenRect = mainScreen.bounds;
99+
CGRect screenRect = [GREYUILibUtils screen].bounds;
117100
return [self grey_takeScreenshotAfterScreenUpdates:afterScreenUpdates
118101
inScreenRect:screenRect
119102
withStatusBar:includeStatusBar];
@@ -169,8 +152,8 @@ + (void)drawViewInContext:(CGContextRef)bitmapContextRef
169152
view:(UIView *)view
170153
bounds:(CGRect)boundsInScreenRect
171154
afterScreenUpdates:(BOOL)afterScreenUpdates {
172-
UIScreen *mainScreen = MainScreen();
173-
if (!mainScreen) return;
155+
UIScreen *mainScreen = [GREYUILibUtils screen];
156+
174157
// The bitmap context width and height are scaled, so we need to undo the scale adjustment.
175158
CGFloat scale = mainScreen.scale;
176159
CGFloat contextWidth = CGBitmapContextGetWidth(bitmapContextRef) / scale;

UILib/Utils/GREYUILibUtils.m

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
//
1616

1717
#import "GREYUILibUtils.h"
18-
#import "GREYLogger.h"
1918

2019
UIWindow *GREYUILibUtilsGetApplicationKeyWindow(UIApplication *application) {
2120
// New API only available on Xcode 13+
@@ -71,11 +70,6 @@ + (UIScreen *)screen {
7170
UIScreen *screen;
7271

7372
if (@available(iOS 13.0, *)) {
74-
UIWindow *window = [self window];
75-
if (!window) {
76-
GREYLog(@"Attempted to take a screenshot when no window exists, i.e. window == nil");
77-
return nil;
78-
}
7973
screen = [self window].windowScene.screen;
8074
// This check is added in case there is an issue with getting the screen i.e. if the screen
8175
// hasn't come up.

0 commit comments

Comments
 (0)