Chromium Code Reviews| Index: chrome/browser/chromeos/accessibility/magnification_controller_browsertest.cc |
| diff --git a/chrome/browser/chromeos/accessibility/magnification_controller_browsertest.cc b/chrome/browser/chromeos/accessibility/magnification_controller_browsertest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0752e0825ed0b165790e837d1d65017683122bdf |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/accessibility/magnification_controller_browsertest.cc |
| @@ -0,0 +1,193 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <string> |
| + |
| +#include "ash/magnifier/magnification_controller.h" |
| +#include "ash/screen_util.h" |
| +#include "ash/shell.h" |
| +#include "base/command_line.h" |
| +#include "chrome/browser/browser_process.h" |
| +#include "chrome/browser/chromeos/accessibility/magnification_manager.h" |
| +#include "chrome/browser/ui/browser.h" |
| +#include "chrome/browser/ui/tabs/tab_strip_model.h" |
| +#include "chrome/common/chrome_switches.h" |
| +#include "chrome/test/base/in_process_browser_test.h" |
| +#include "chrome/test/base/ui_test_utils.h" |
| +#include "content/public/browser/render_widget_host_view.h" |
| +#include "content/public/browser/web_contents.h" |
| +#include "content/public/test/browser_test_utils.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace chromeos { |
| + |
| +namespace { |
| + |
| +const char kDataURIPrefix[] = "data:text/html;charset=utf-8,"; |
| +const char kTestHtmlContent1[] = |
| + "<body style=\"margin-top:0;margin-left:0\">" |
| + "<button type=\"button\" name=\"test_button_1\" id=\"test_button\" " |
| + "style=\"margin-left:200;margin-top:200;width:100;height:50\">" |
| + "Big Button 1</button>" |
| + "</body>"; |
| +const char kTestHtmlContent2[] = |
| + "<body style=\"margin-top:0;margin-left:0\">" |
| + "<button type=\"button\" name=\"test_button_1\" id=\"test_button\" " |
| + "style=\"margin-left:200;margin-top:200;width:100;height:50\">" |
| + "Big Button 1</button>" |
| + "</body>"; |
| + |
| +} // namespace |
| + |
| +class MagnificationControllerTest : public InProcessBrowserTest { |
| + protected: |
| + MagnificationControllerTest() {} |
| + virtual ~MagnificationControllerTest() {} |
|
oshima
2014/12/12 00:48:45
no virutal. just override
jennyz
2014/12/12 23:14:45
Done.
|
| + |
| + void SetUpCommandLine(CommandLine* command_line) override { |
| + InProcessBrowserTest::SetUpCommandLine(command_line); |
| + // Make screens sufficiently wide to host 2 browsers side by side. |
| + command_line->AppendSwitchASCII("ash-host-window-bounds", "1200x800"); |
| + } |
| + |
| + aura::Window* GetRootWindow() const { |
| + return ash::Shell::GetPrimaryRootWindow(); |
| + } |
| + |
| + ash::MagnificationController* GetMagnificationController() const { |
| + return ash::Shell::GetInstance()->magnification_controller(); |
| + } |
| + |
| + bool IsMagnifierEnabled() const { |
| + return MagnificationManager::Get()->IsMagnifierEnabled(); |
| + } |
| + |
| + void SetMagnifierEnabled(bool enabled) { |
| + MagnificationManager::Get()->SetMagnifierEnabled(true); |
| + } |
| + |
| + void MoveMagnifierWindow(int x, int y) { |
| + GetMagnificationController()->MoveWindow(x, y, false); |
| + } |
| + |
| + gfx::Rect GetViewPort() const { |
| + return GetMagnificationController()->GetViewportRect(); |
| + } |
|
oshima
2014/12/12 00:48:45
these methods do not have to be instance methods.
jennyz
2014/12/12 23:14:45
Done.
|
| + |
| + content::WebContents* GetWebContents() { |
| + return browser()->tab_strip_model()->GetActiveWebContents(); |
| + } |
| + |
| + void ExecuteScriptAndExtractInt(const std::string& script, int* result) { |
| + ASSERT_TRUE(content::ExecuteScriptAndExtractInt( |
| + GetWebContents(), |
| + "window.domAutomationController.send(" + script + ");", result)); |
| + } |
| + |
| + void ExecuteScript(const std::string& script) { |
| + ASSERT_TRUE(content::ExecuteScript(GetWebContents(), script)); |
| + } |
| + |
| + gfx::Rect GetControlBoundsInRoot(const std::string& field_id) { |
| + ExecuteScript("var element = document.getElementById('" + field_id + |
| + "');" |
| + "var bounds = element.getBoundingClientRect();"); |
| + int top, left, width, height; |
| + ExecuteScriptAndExtractInt("bounds.top", &top); |
| + ExecuteScriptAndExtractInt("bounds.left", &left); |
| + ExecuteScriptAndExtractInt("bounds.width", &width); |
| + ExecuteScriptAndExtractInt("bounds.height", &height); |
| + gfx::Rect rect(top, left, width, height); |
| + |
| + content::RenderWidgetHostView* view = |
| + GetWebContents()->GetRenderWidgetHostView(); |
| + gfx::Rect view_bounds_in_screen = view->GetViewBounds(); |
| + gfx::Point origin = rect.origin(); |
| + origin.Offset(view_bounds_in_screen.x(), view_bounds_in_screen.y()); |
| + gfx::Rect rect_in_screen(origin.x(), origin.y(), rect.width(), |
| + rect.height()); |
| + |
| + return ash::ScreenUtil::ConvertRectFromScreen(GetRootWindow(), |
| + rect_in_screen); |
| + } |
| + |
| + void SetFocusOnElement(const std::string& element_id) { |
| + ExecuteScript("document.getElementById('" + element_id + "').focus();"); |
| + } |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MagnificationControllerTest); |
| +}; |
| + |
| +IN_PROC_BROWSER_TEST_F(MagnificationControllerTest, |
| + FollowFocusOnWebPageButtonNotIntersected) { |
| + ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| + browser(), GURL(std::string(kDataURIPrefix) + kTestHtmlContent1))); |
| + const gfx::Rect button_bounds = GetControlBoundsInRoot("test_button"); |
| + |
| + // Enable magnifier. |
| + SetMagnifierEnabled(true); |
| + // Confirms that magnifier is enabled. |
| + EXPECT_TRUE(IsMagnifierEnabled()); |
| + EXPECT_EQ(2.0f, GetMagnificationController()->GetScale()); |
| + |
| + // Move the magnifier window to not intersect with the button. |
| + MoveMagnifierWindow(button_bounds.right() + 200, |
| + button_bounds.bottom() + 200); |
| + EXPECT_FALSE(GetViewPort().Intersects(button_bounds)); |
| + |
| + // Set the focus on the button. |
| + SetFocusOnElement("test_button"); |
| + // Verify the magnifier window is moved to contain the focused button. |
| + EXPECT_TRUE(GetViewPort().Contains(button_bounds)); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(MagnificationControllerTest, |
| + FollowFocusOnWebPageButtonIntersected) { |
| + ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| + browser(), GURL(std::string(kDataURIPrefix) + kTestHtmlContent1))); |
| + const gfx::Rect button_bounds = GetControlBoundsInRoot("test_button"); |
| + |
| + // Enable magnifier. |
| + SetMagnifierEnabled(true); |
| + // Confirm that magnifier is enabled. |
| + EXPECT_TRUE(IsMagnifierEnabled()); |
| + EXPECT_EQ(2.0f, GetMagnificationController()->GetScale()); |
| + |
| + // Move the magnifier window to intersect with the button. |
| + MoveMagnifierWindow(button_bounds.CenterPoint().x(), |
| + button_bounds.CenterPoint().y()); |
| + EXPECT_TRUE(GetViewPort().Intersects(button_bounds)); |
| + |
| + // Set the focus on the button. |
| + SetFocusOnElement("test_button"); |
| + // Verify the magnifier window is moved to contain the focused button. |
| + EXPECT_TRUE(GetViewPort().Contains(button_bounds)); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(MagnificationControllerTest, |
| + FollowFocusOnWebButtonContained) { |
| + ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| + browser(), GURL(std::string(kDataURIPrefix) + kTestHtmlContent2))); |
| + const gfx::Rect button_bounds = GetControlBoundsInRoot("test_button"); |
| + |
| + // Enable magnifier. |
| + SetMagnifierEnabled(true); |
| + // Confirm that magnifier is enabled. |
| + EXPECT_TRUE(IsMagnifierEnabled()); |
| + EXPECT_EQ(2.0f, GetMagnificationController()->GetScale()); |
| + |
| + // Move magnifier window to contain the button. |
| + MoveMagnifierWindow(button_bounds.x() - 100, button_bounds.y() - 100); |
| + const gfx::Rect view_port_before_focus = GetViewPort(); |
| + EXPECT_TRUE(view_port_before_focus.Contains(button_bounds)); |
| + |
| + // Set the focus on the button. |
| + SetFocusOnElement("test_button"); |
| + // Verify the magnifier window is not moved and still contains the button. |
| + const gfx::Rect view_port_after_focus = GetViewPort(); |
| + EXPECT_TRUE(view_port_after_focus.Contains(button_bounds)); |
| + EXPECT_EQ(view_port_before_focus, view_port_after_focus); |
| +} |
| + |
| +} // namespace chromeos |