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

fix: make adjusting screenshot coordinate configurable - screenshotOrientation settings api #277

Merged
4 changes: 4 additions & 0 deletions WebDriverAgentLib/Commands/FBSessionCommands.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
static NSString* const INCLUDE_NON_MODAL_ELEMENTS = @"includeNonModalElements";
static NSString* const ACCEPT_ALERT_BUTTON_SELECTOR = @"acceptAlertButtonSelector";
static NSString* const DISMISS_ALERT_BUTTON_SELECTOR = @"dismissAlertButtonSelector";
static NSString* const AUTO_ADJUST_SCREENSHOT_ORIENTATION = @"autoAdjustScreenshotOrientation";

Choose a reason for hiding this comment

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

I think we could make it more flexible for clients by explicitly providing the appropriate orientation value, like UIImageOrientationLeft or UIImageOrientationUp and leaving it to auto by default

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea



@implementation FBSessionCommands
Expand Down Expand Up @@ -327,6 +328,9 @@ + (NSArray *)routes
if (nil != [settings objectForKey:DISMISS_ALERT_BUTTON_SELECTOR]) {
[FBConfiguration setDismissAlertButtonSelector:(NSString *)[settings objectForKey:DISMISS_ALERT_BUTTON_SELECTOR]];
}
if (nil != [settings objectForKey:AUTO_ADJUST_SCREENSHOT_ORIENTATION]) {
[FBConfiguration setAutoAdjustScreenshotOrientation:[[settings objectForKey:AUTO_ADJUST_SCREENSHOT_ORIENTATION] boolValue]];
}

return [self handleGetSettings:request];
}
Expand Down
12 changes: 12 additions & 0 deletions WebDriverAgentLib/Utilities/FBConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,18 @@ NS_ASSUME_NONNULL_BEGIN
+ (void)setDismissAlertButtonSelector:(NSString *)classChainSelector;
+ (NSString *)dismissAlertButtonSelector;

/**
Auto adjust the screenshot orientation.
The adjustment helps to fix the screenshot coordinate when a user change the device orientation.
But the logic sometimes cannot set the screenshot orientation properly. Skiping the logic can fix it.
Xcode versions, OS versions or device models and simulator or real device could influence this logic.

@param isEnabled Set to NO in order to skip adjusting screenshot coordinate. Defaults to YES.
*/
+ (void)setAutoAdjustScreenshotOrientation:(BOOL)isEnabled;
+ (BOOL)autoAdjustScreenshotOrientation;


@end

NS_ASSUME_NONNULL_END
11 changes: 11 additions & 0 deletions WebDriverAgentLib/Utilities/FBConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
static NSString *FBDismissAlertButtonSelector = @"";
static NSString *FBSnapshotMaxDepthKey = @"maxDepth";
static NSMutableDictionary *FBSnapshotRequestParameters;
static BOOL FBAutoAdjustScreenshotOrientation = YES;


@implementation FBConfiguration
Expand Down Expand Up @@ -343,6 +344,16 @@ + (NSString *)dismissAlertButtonSelector
return FBDismissAlertButtonSelector;
}

+ (void)setAutoAdjustScreenshotOrientation:(BOOL)isEnabled
{
FBAutoAdjustScreenshotOrientation = isEnabled;
}

+ (BOOL)autoAdjustScreenshotOrientation
{
return FBAutoAdjustScreenshotOrientation;
}

#pragma mark Private

+ (BOOL)keyboardsPreference:(nonnull NSString *)key
Expand Down
31 changes: 17 additions & 14 deletions WebDriverAgentLib/Utilities/FBImageUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import "FBImageUtils.h"

#import "FBMacros.h"
#import "FBConfiguration.h"

static uint8_t JPEG_MAGIC[] = { 0xff, 0xd8 };
static const NSUInteger JPEG_MAGIC_LEN = 2;
Expand Down Expand Up @@ -67,21 +68,23 @@ BOOL FBIsPngImage(NSData *imageData)
NSData *FBAdjustScreenshotOrientationForApplication(NSData *screenshotData, UIInterfaceOrientation orientation)
{
UIImageOrientation imageOrientation;
if (SYSTEM_VERSION_LESS_THAN(@"11.0")) {
// In iOS < 11.0 screenshots are already adjusted properly
imageOrientation = UIImageOrientationUp;
} else if (orientation == UIInterfaceOrientationLandscapeRight) {
imageOrientation = UIImageOrientationLeft;
} else if (orientation == UIInterfaceOrientationLandscapeLeft) {
imageOrientation = UIImageOrientationRight;
} else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
imageOrientation = UIImageOrientationDown;
} else {
if (FBIsPngImage(screenshotData)) {
return screenshotData;
// Apply auto rotation by default for iOS >= 11.0. In iOS < 11.0 screenshots are already adjusted properly.
if (FBConfiguration.autoAdjustScreenshotOrientation && SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"11.0")) {
if (orientation == UIInterfaceOrientationLandscapeRight) {

Choose a reason for hiding this comment

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

if (SYSTEM_VERSION_LESS_THAN(@"11.0")) { is missing here. The previous implementation should be kept unchanged

imageOrientation = UIImageOrientationLeft;
} else if (orientation == UIInterfaceOrientationLandscapeLeft) {
imageOrientation = UIImageOrientationRight;
} else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
imageOrientation = UIImageOrientationDown;
} else {
if (FBIsPngImage(screenshotData)) {
return screenshotData;
}
UIImage *image = [UIImage imageWithData:screenshotData];
return (NSData *)UIImagePNGRepresentation(image);
}
UIImage *image = [UIImage imageWithData:screenshotData];
return (NSData *)UIImagePNGRepresentation(image);
} else {
imageOrientation = UIImageOrientationUp;
}

UIImage *image = [UIImage imageWithData:screenshotData];
Expand Down