Commit 9c25d8db authored by John Chen's avatar John Chen Committed by Commit Bot

[ChromeDriver] Fix Get Window Size on Android

Fixing several issues with code that gets windows size on Android:
* The size should not be multiplied by devicePixelRatio, according to
  W3C standard.
* The code should not crash when size is non-integer. (This should be
  unlikely to happen, now that the size is no longer multiplied by
  devicePixelRatio. But to be safe, I still added code to handle
  non-integer.)
* In the test, inner size needs to be multiplied by viewport scale to
  be comparable with window size. The existing code was working by
  chance.

Bug: chromedriver:3339
Change-Id: I1cc3105754a041d0476bf9f971a47911331b8062
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2073068Reviewed-by: default avatarCaleb Rouleau <crouleau@chromium.org>
Commit-Queue: John Chen <johnchen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#744695}
parent 77285f4b
......@@ -46,16 +46,15 @@ Status ChromeAndroidImpl::GetWindow(const std::string& target_id,
std::unique_ptr<base::Value> result;
std::string expression =
"[window.screenX, window.screenY, window.outerWidth * "
"window.devicePixelRatio, window.outerHeight * window.devicePixelRatio]";
"[window.screenX, window.screenY, window.outerWidth, window.outerHeight]";
status = web_view->EvaluateScript(target_id, expression, &result);
if (status.IsError())
return status;
window->left = result->GetList()[0].GetInt();
window->top = result->GetList()[1].GetInt();
window->width = result->GetList()[2].GetInt();
window->height = result->GetList()[3].GetInt();
window->left = static_cast<int>(result->GetList()[0].GetDouble());
window->top = static_cast<int>(result->GetList()[1].GetDouble());
window->width = static_cast<int>(result->GetList()[2].GetDouble());
window->height = static_cast<int>(result->GetList()[3].GetDouble());
// Android does not use Window.id or have window states
window->id = 0;
window->state = "";
......
......@@ -3286,19 +3286,20 @@ class ChromeDriverAndroidTest(ChromeDriverBaseTest):
size = self._driver.GetWindowRect()
script_size = self._driver.ExecuteScript(
"return [window.outerWidth * window.devicePixelRatio,"
"window.outerHeight * window.devicePixelRatio, 0, 0]")
'return [window.outerWidth, window.outerHeight, 0, 0]')
self.assertEquals(size, script_size)
script_inner = self._driver.ExecuteScript(
"return [window.innerWidth, window.innerHeight]")
self.assertLessEqual(script_inner[0], size[0])
self.assertLessEqual(script_inner[1], size[1])
# Sanity check: screen dimensions in the range 2-20000px
'return [window.innerWidth * visualViewport.scale, '
'window.innerHeight * visualViewport.scale]')
# Subtract inner size by 1 to compensate for rounding errors.
self.assertLessEqual(script_inner[0] - 1, size[0])
self.assertLessEqual(script_inner[1] - 1, size[1])
# Sanity check: screen dimensions in the range 20-20000px
self.assertLessEqual(size[0], 20000)
self.assertLessEqual(size[1], 20000)
self.assertGreaterEqual(size[0], 2)
self.assertGreaterEqual(size[1], 2)
self.assertGreaterEqual(size[0], 20)
self.assertGreaterEqual(size[1], 20)
class ChromeDownloadDirTest(ChromeDriverBaseTest):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment