OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <string> |
| 6 |
| 7 #include "ash/magnifier/magnification_controller.h" |
| 8 #include "ash/screen_util.h" |
| 9 #include "ash/shell.h" |
| 10 #include "base/command_line.h" |
| 11 #include "chrome/browser/browser_process.h" |
| 12 #include "chrome/browser/chromeos/accessibility/magnification_manager.h" |
| 13 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 15 #include "chrome/common/chrome_switches.h" |
| 16 #include "chrome/test/base/in_process_browser_test.h" |
| 17 #include "chrome/test/base/ui_test_utils.h" |
| 18 #include "content/public/browser/render_widget_host_view.h" |
| 19 #include "content/public/browser/web_contents.h" |
| 20 #include "content/public/test/browser_test_utils.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 |
| 23 namespace chromeos { |
| 24 |
| 25 namespace { |
| 26 |
| 27 const char kDataURIPrefix[] = "data:text/html;charset=utf-8,"; |
| 28 const char kTestHtmlContent1[] = |
| 29 "<body style=\"margin-top:0;margin-left:0\">" |
| 30 "<button type=\"button\" name=\"test_button_1\" id=\"test_button\" " |
| 31 "style=\"margin-left:200;margin-top:200;width:100;height:50\">" |
| 32 "Big Button 1</button>" |
| 33 "</body>"; |
| 34 const char kTestHtmlContent2[] = |
| 35 "<body style=\"margin-top:0;margin-left:0\">" |
| 36 "<button type=\"button\" name=\"test_button_1\" id=\"test_button\" " |
| 37 "style=\"margin-left:200;margin-top:200;width:100;height:50\">" |
| 38 "Big Button 1</button>" |
| 39 "</body>"; |
| 40 |
| 41 aura::Window* GetRootWindow() { |
| 42 return ash::Shell::GetPrimaryRootWindow(); |
| 43 } |
| 44 |
| 45 ash::MagnificationController* GetMagnificationController() { |
| 46 return ash::Shell::GetInstance()->magnification_controller(); |
| 47 } |
| 48 |
| 49 bool IsMagnifierEnabled() { |
| 50 return MagnificationManager::Get()->IsMagnifierEnabled(); |
| 51 } |
| 52 |
| 53 void SetMagnifierEnabled(bool enabled) { |
| 54 MagnificationManager::Get()->SetMagnifierEnabled(true); |
| 55 } |
| 56 |
| 57 void MoveMagnifierWindow(int x, int y) { |
| 58 GetMagnificationController()->MoveWindow(x, y, false); |
| 59 } |
| 60 |
| 61 gfx::Rect GetViewPort() { |
| 62 return GetMagnificationController()->GetViewportRect(); |
| 63 } |
| 64 |
| 65 } // namespace |
| 66 |
| 67 class MagnificationControllerTest : public InProcessBrowserTest { |
| 68 protected: |
| 69 MagnificationControllerTest() {} |
| 70 ~MagnificationControllerTest() override {} |
| 71 |
| 72 void SetUpCommandLine(CommandLine* command_line) override { |
| 73 InProcessBrowserTest::SetUpCommandLine(command_line); |
| 74 // Make screens sufficiently wide to host 2 browsers side by side. |
| 75 command_line->AppendSwitchASCII("ash-host-window-bounds", "1200x800"); |
| 76 } |
| 77 |
| 78 content::WebContents* GetWebContents() { |
| 79 return browser()->tab_strip_model()->GetActiveWebContents(); |
| 80 } |
| 81 |
| 82 void ExecuteScriptAndExtractInt(const std::string& script, int* result) { |
| 83 ASSERT_TRUE(content::ExecuteScriptAndExtractInt( |
| 84 GetWebContents(), |
| 85 "window.domAutomationController.send(" + script + ");", result)); |
| 86 } |
| 87 |
| 88 void ExecuteScript(const std::string& script) { |
| 89 ASSERT_TRUE(content::ExecuteScript(GetWebContents(), script)); |
| 90 } |
| 91 |
| 92 gfx::Rect GetControlBoundsInRoot(const std::string& field_id) { |
| 93 ExecuteScript("var element = document.getElementById('" + field_id + |
| 94 "');" |
| 95 "var bounds = element.getBoundingClientRect();"); |
| 96 int top, left, width, height; |
| 97 ExecuteScriptAndExtractInt("bounds.top", &top); |
| 98 ExecuteScriptAndExtractInt("bounds.left", &left); |
| 99 ExecuteScriptAndExtractInt("bounds.width", &width); |
| 100 ExecuteScriptAndExtractInt("bounds.height", &height); |
| 101 gfx::Rect rect(top, left, width, height); |
| 102 |
| 103 content::RenderWidgetHostView* view = |
| 104 GetWebContents()->GetRenderWidgetHostView(); |
| 105 gfx::Rect view_bounds_in_screen = view->GetViewBounds(); |
| 106 gfx::Point origin = rect.origin(); |
| 107 origin.Offset(view_bounds_in_screen.x(), view_bounds_in_screen.y()); |
| 108 gfx::Rect rect_in_screen(origin.x(), origin.y(), rect.width(), |
| 109 rect.height()); |
| 110 |
| 111 return ash::ScreenUtil::ConvertRectFromScreen(GetRootWindow(), |
| 112 rect_in_screen); |
| 113 } |
| 114 |
| 115 void SetFocusOnElement(const std::string& element_id) { |
| 116 ExecuteScript("document.getElementById('" + element_id + "').focus();"); |
| 117 } |
| 118 |
| 119 private: |
| 120 DISALLOW_COPY_AND_ASSIGN(MagnificationControllerTest); |
| 121 }; |
| 122 |
| 123 IN_PROC_BROWSER_TEST_F(MagnificationControllerTest, |
| 124 FollowFocusOnWebPageButtonNotIntersected) { |
| 125 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 126 browser(), GURL(std::string(kDataURIPrefix) + kTestHtmlContent1))); |
| 127 const gfx::Rect button_bounds = GetControlBoundsInRoot("test_button"); |
| 128 |
| 129 // Enable magnifier. |
| 130 SetMagnifierEnabled(true); |
| 131 // Confirms that magnifier is enabled. |
| 132 EXPECT_TRUE(IsMagnifierEnabled()); |
| 133 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale()); |
| 134 |
| 135 // Move the magnifier window to not intersect with the button. |
| 136 MoveMagnifierWindow(button_bounds.right() + 200, |
| 137 button_bounds.bottom() + 200); |
| 138 EXPECT_FALSE(GetViewPort().Intersects(button_bounds)); |
| 139 |
| 140 // Set the focus on the button. |
| 141 SetFocusOnElement("test_button"); |
| 142 // Verify the magnifier window is moved to contain the focused button. |
| 143 EXPECT_TRUE(GetViewPort().Contains(button_bounds)); |
| 144 } |
| 145 |
| 146 IN_PROC_BROWSER_TEST_F(MagnificationControllerTest, |
| 147 FollowFocusOnWebPageButtonIntersected) { |
| 148 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 149 browser(), GURL(std::string(kDataURIPrefix) + kTestHtmlContent1))); |
| 150 const gfx::Rect button_bounds = GetControlBoundsInRoot("test_button"); |
| 151 |
| 152 // Enable magnifier. |
| 153 SetMagnifierEnabled(true); |
| 154 // Confirm that magnifier is enabled. |
| 155 EXPECT_TRUE(IsMagnifierEnabled()); |
| 156 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale()); |
| 157 |
| 158 // Move the magnifier window to intersect with the button. |
| 159 MoveMagnifierWindow(button_bounds.CenterPoint().x(), |
| 160 button_bounds.CenterPoint().y()); |
| 161 EXPECT_TRUE(GetViewPort().Intersects(button_bounds)); |
| 162 |
| 163 // Set the focus on the button. |
| 164 SetFocusOnElement("test_button"); |
| 165 // Verify the magnifier window is moved to contain the focused button. |
| 166 EXPECT_TRUE(GetViewPort().Contains(button_bounds)); |
| 167 } |
| 168 |
| 169 IN_PROC_BROWSER_TEST_F(MagnificationControllerTest, |
| 170 FollowFocusOnWebButtonContained) { |
| 171 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 172 browser(), GURL(std::string(kDataURIPrefix) + kTestHtmlContent2))); |
| 173 const gfx::Rect button_bounds = GetControlBoundsInRoot("test_button"); |
| 174 |
| 175 // Enable magnifier. |
| 176 SetMagnifierEnabled(true); |
| 177 // Confirm that magnifier is enabled. |
| 178 EXPECT_TRUE(IsMagnifierEnabled()); |
| 179 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale()); |
| 180 |
| 181 // Move magnifier window to contain the button. |
| 182 MoveMagnifierWindow(button_bounds.x() - 100, button_bounds.y() - 100); |
| 183 const gfx::Rect view_port_before_focus = GetViewPort(); |
| 184 EXPECT_TRUE(view_port_before_focus.Contains(button_bounds)); |
| 185 |
| 186 // Set the focus on the button. |
| 187 SetFocusOnElement("test_button"); |
| 188 // Verify the magnifier window is not moved and still contains the button. |
| 189 const gfx::Rect view_port_after_focus = GetViewPort(); |
| 190 EXPECT_TRUE(view_port_after_focus.Contains(button_bounds)); |
| 191 EXPECT_EQ(view_port_before_focus, view_port_after_focus); |
| 192 } |
| 193 |
| 194 } // namespace chromeos |
OLD | NEW |