Skip to content

Commit

Permalink
[plugin-mobile-app] Fix invalid dpr calculation for android (#2914)
Browse files Browse the repository at this point in the history
The ratio between the device window size and taken screenshot size is always equal to 1 regardless of the device display density on Android platform.See for details: appium/appium-base-driver#306.
  • Loading branch information
uarlouski authored Sep 7, 2022
1 parent 8dec93d commit 8700f64
Show file tree
Hide file tree
Showing 22 changed files with 45 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ public double getDpr()

private double calculateDpr()
{
if (isAndroidNativeApp())
{
// On Android platform the ratio between the device window size and taken screenshot size is equal to 1
// regardless of the device's display density.
// Also see https://github.com/appium/appium-base-driver/pull/306 for details.
return 1D;
}

try
{
byte[] imageBytes = getUnwrappedDriver(TakesScreenshot.class).getScreenshotAs(OutputType.BYTES);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.HasCapabilities;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
Expand Down Expand Up @@ -84,12 +85,8 @@ class MobileAppWebDriverManagerTests

private int mockStatusBarHeightRetrieval()
{
ExecutesMethod executingMethodDriver = mock(ExecutesMethod.class);
when(webDriverProvider.getUnwrapped(ExecutesMethod.class)).thenReturn(executingMethodDriver);
Response response = new Response();
long height = 101L;
response.setValue(Map.of(STAT_BAR_HEIGHT, height));
when(executingMethodDriver.execute(GET_SESSION_COMMAND)).thenReturn(response);
mockGetSession(Map.of(STAT_BAR_HEIGHT, height));
return (int) height;
}

Expand Down Expand Up @@ -138,12 +135,7 @@ void shouldProviderStatusBarHeightForIosWhenSauceLabsThrowAnError()
void shouldPerformJsRequestForStatBarHeightWhenSessionDetailsStatBarHeightIsNullForIos()
{
mockCapabilities(MobilePlatform.IOS);
ExecutesMethod executingMethodDriver = mock(ExecutesMethod.class);
when(webDriverProvider.getUnwrapped(ExecutesMethod.class)).thenReturn(executingMethodDriver);

Response response = new Response();
response.setValue(Collections.EMPTY_MAP);
when(executingMethodDriver.execute(GET_SESSION_COMMAND)).thenReturn(response);
mockGetSession(Collections.EMPTY_MAP);
when(javascriptActions.executeScript(MOBILE_DEVICE_SCREEN_INFO_JS)).thenReturn(STATUS_BAR_SIZE);
assertEquals(44, driverManager.getStatusBarSize());
}
Expand Down Expand Up @@ -177,15 +169,14 @@ private void mockCapabilities(String platform)
{
WebDriver webDriver = mock(WebDriver.class, withSettings().extraInterfaces(HasCapabilities.class));
when(webDriverProvider.get()).thenReturn(webDriver);
Capabilities capabilitiesMock = mock(Capabilities.class);
when(((HasCapabilities) webDriver).getCapabilities()).thenReturn(capabilitiesMock);
Capabilities capabilities = new MutableCapabilities(Map.of(CapabilityType.PLATFORM_NAME, platform));
when(((HasCapabilities) webDriver).getCapabilities()).thenReturn(capabilities);
driverManager.setMobileApp(true);
when(capabilitiesMock.getCapability(CapabilityType.PLATFORM_NAME)).thenReturn(platform);
}

@SuppressWarnings("unchecked")
@Test
void shouldProvideDpr()
void shouldProvideDprForAndroidNativeApp()
{
MobileAppWebDriverManager spyingDriverManager = new MobileAppWebDriverManager(webDriverProvider,
webDriverSessionInfo, javascriptActions)
Expand All @@ -202,6 +193,27 @@ public Dimension getSize()
when(webDriverSessionInfo.get(eq(WebDriverSessionAttributes.DEVICE_PIXEL_RATIO),
any(Supplier.class))).thenAnswer(invocation -> ((Supplier<?>) invocation.getArguments()[1]).get());
assertEquals(1d, spyingDriverManager.getDpr());
mockCapabilities(MobilePlatform.ANDROID);
when(webDriverSessionInfo.get(eq(WebDriverSessionAttributes.DEVICE_PIXEL_RATIO),
any(Supplier.class))).thenAnswer(invocation -> ((Supplier<?>) invocation.getArguments()[1]).get());

assertEquals(1d, driverManager.getDpr());
}

@SuppressWarnings("unchecked")
@Test
void shouldProvideDprForIOS()
{
mockCapabilities(MobilePlatform.IOS);
when(webDriverSessionInfo.get(eq(WebDriverSessionAttributes.DEVICE_PIXEL_RATIO),
any(Supplier.class))).thenAnswer(invocation -> ((Supplier<?>) invocation.getArguments()[1]).get());
when(webDriverSessionInfo.get(eq(WebDriverSessionAttributes.SCREEN_SIZE), any(Supplier.class)))
.thenReturn(new Dimension(1, 1));

TakesScreenshot taker = mock(TakesScreenshot.class);
when(webDriverProvider.getUnwrapped(TakesScreenshot.class)).thenReturn(taker);
when(taker.getScreenshotAs(OutputType.BYTES)).thenReturn(IMAGE);
assertEquals(1d, driverManager.getDpr());
verify(webDriverProvider).getUnwrapped(TakesScreenshot.class);
}

Expand All @@ -221,6 +233,7 @@ void shouldRethrowIOException()
{
try (MockedStatic<ImageIO> imageIo = Mockito.mockStatic(ImageIO.class))
{
mockCapabilities(MobilePlatform.IOS);
imageIo.when(() -> ImageIO.read(any(InputStream.class))).thenThrow(new IOException("io is oi"));
TakesScreenshot taker = mock(TakesScreenshot.class);
when(webDriverProvider.getUnwrapped(TakesScreenshot.class)).thenReturn(taker);
Expand All @@ -230,4 +243,13 @@ void shouldRethrowIOException()
assertThrows(UncheckedIOException.class, driverManager::getDpr);
}
}

private void mockGetSession(Object value)
{
ExecutesMethod executingMethodDriver = mock(ExecutesMethod.class);
when(webDriverProvider.getUnwrapped(ExecutesMethod.class)).thenReturn(executingMethodDriver);
Response response = new Response();
response.setValue(value);
when(executingMethodDriver.execute(GET_SESSION_COMMAND)).thenReturn(response);
}
}
Binary file modified vividus-tests/src/main/resources/baselines/android-AREA-ignore.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified vividus-tests/src/main/resources/baselines/android-context.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8700f64

Please sign in to comment.