Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / chromeos / accessibility / magnification_controller_browsertest.cc
blob242ace50c85378f980a51cf91a272eb2e06ed452
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.
5 #include <string>
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 "base/timer/timer.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/chromeos/accessibility/magnification_manager.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "chrome/test/base/in_process_browser_test.h"
18 #include "chrome/test/base/ui_test_utils.h"
19 #include "content/public/browser/render_widget_host_view.h"
20 #include "content/public/browser/web_contents.h"
21 #include "content/public/test/browser_test_utils.h"
22 #include "testing/gtest/include/gtest/gtest.h"
24 namespace chromeos {
26 namespace {
28 const char kDataURIPrefix[] = "data:text/html;charset=utf-8,";
29 const char kTestHtmlContent1[] =
30 "<body style=\"margin-top:0;margin-left:0\">"
31 "<button type=\"button\" name=\"test_button_1\" id=\"test_button\" "
32 "style=\"margin-left:200;margin-top:200;width:100;height:50\">"
33 "Big Button 1</button>"
34 "</body>";
35 const char kTestHtmlContent2[] =
36 "<body style=\"margin-top:0;margin-left:0\">"
37 "<button type=\"button\" name=\"test_button_1\" id=\"test_button\" "
38 "style=\"margin-left:200;margin-top:200;width:100;height:50\">"
39 "Big Button 1</button>"
40 "</body>";
42 aura::Window* GetRootWindow() {
43 return ash::Shell::GetPrimaryRootWindow();
46 ash::MagnificationController* GetMagnificationController() {
47 return ash::Shell::GetInstance()->magnification_controller();
50 bool IsMagnifierEnabled() {
51 return MagnificationManager::Get()->IsMagnifierEnabled();
54 void SetMagnifierEnabled(bool enabled) {
55 MagnificationManager::Get()->SetMagnifierEnabled(true);
58 void MoveMagnifierWindow(int x, int y) {
59 GetMagnificationController()->MoveWindow(x, y, false);
62 gfx::Rect GetViewPort() {
63 return GetMagnificationController()->GetViewportRect();
66 class MagnifierAnimationWaiter {
67 public:
68 explicit MagnifierAnimationWaiter(ash::MagnificationController* controller)
69 : controller_(controller) {}
71 void Wait() {
72 base::RepeatingTimer<MagnifierAnimationWaiter> check_timer;
73 check_timer.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(10), this,
74 &MagnifierAnimationWaiter::OnTimer);
75 runner_ = new content::MessageLoopRunner;
76 runner_->Run();
79 private:
80 void OnTimer() {
81 DCHECK(runner_.get());
82 if (!controller_->IsOnAnimationForTesting()) {
83 runner_->Quit();
87 ash::MagnificationController* controller_; // not owned
88 scoped_refptr<content::MessageLoopRunner> runner_;
89 DISALLOW_COPY_AND_ASSIGN(MagnifierAnimationWaiter);
92 } // namespace
94 class MagnificationControllerTest : public InProcessBrowserTest {
95 protected:
96 MagnificationControllerTest() {}
97 ~MagnificationControllerTest() override {}
99 void SetUpCommandLine(base::CommandLine* command_line) override {
100 InProcessBrowserTest::SetUpCommandLine(command_line);
101 // Make screens sufficiently wide to host 2 browsers side by side.
102 command_line->AppendSwitchASCII("ash-host-window-bounds", "1200x800");
105 void SetUpOnMainThread() override {
106 SetMagnifierEnabled(true);
108 // Confirms that magnifier is enabled.
109 EXPECT_TRUE(IsMagnifierEnabled());
110 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale());
112 // MagnificationController moves the magnifier window with animation
113 // when the magnifier is set to be enabled. It will move the mouse cursor
114 // when the animation completes. Wait until the animation completes, so that
115 // the mouse movement won't affect the position of magnifier window later.
116 MagnifierAnimationWaiter waiter(GetMagnificationController());
117 waiter.Wait();
118 base::RunLoop().RunUntilIdle();
121 content::WebContents* GetWebContents() {
122 return browser()->tab_strip_model()->GetActiveWebContents();
125 void ExecuteScriptAndExtractInt(const std::string& script, int* result) {
126 ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
127 GetWebContents(),
128 "window.domAutomationController.send(" + script + ");", result));
131 void ExecuteScript(const std::string& script) {
132 ASSERT_TRUE(content::ExecuteScript(GetWebContents(), script));
135 gfx::Rect GetControlBoundsInRoot(const std::string& field_id) {
136 ExecuteScript("var element = document.getElementById('" + field_id +
137 "');"
138 "var bounds = element.getBoundingClientRect();");
139 int top, left, width, height;
140 ExecuteScriptAndExtractInt("bounds.top", &top);
141 ExecuteScriptAndExtractInt("bounds.left", &left);
142 ExecuteScriptAndExtractInt("bounds.width", &width);
143 ExecuteScriptAndExtractInt("bounds.height", &height);
144 gfx::Rect rect(top, left, width, height);
146 content::RenderWidgetHostView* view =
147 GetWebContents()->GetRenderWidgetHostView();
148 gfx::Rect view_bounds_in_screen = view->GetViewBounds();
149 gfx::Point origin = rect.origin();
150 origin.Offset(view_bounds_in_screen.x(), view_bounds_in_screen.y());
151 gfx::Rect rect_in_screen(origin.x(), origin.y(), rect.width(),
152 rect.height());
154 return ash::ScreenUtil::ConvertRectFromScreen(GetRootWindow(),
155 rect_in_screen);
158 void SetFocusOnElement(const std::string& element_id) {
159 ExecuteScript("document.getElementById('" + element_id + "').focus();");
162 private:
163 DISALLOW_COPY_AND_ASSIGN(MagnificationControllerTest);
166 IN_PROC_BROWSER_TEST_F(MagnificationControllerTest,
167 FollowFocusOnWebPageButtonNotIntersected) {
168 DCHECK(IsMagnifierEnabled());
169 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
170 browser(), GURL(std::string(kDataURIPrefix) + kTestHtmlContent1)));
172 // Move the magnifier window to not intersect with the button.
173 const gfx::Rect button_bounds = GetControlBoundsInRoot("test_button");
174 MoveMagnifierWindow(button_bounds.right() + 100,
175 button_bounds.bottom() + 100);
176 EXPECT_FALSE(GetViewPort().Intersects(button_bounds));
178 // Set the focus on the button.
179 SetFocusOnElement("test_button");
181 // Verify the magnifier window is moved to contain the focused button,
182 // and the button is centered at the magnifier window.
183 EXPECT_TRUE(GetViewPort().Contains(button_bounds));
184 EXPECT_EQ(GetViewPort().CenterPoint(), button_bounds.CenterPoint());
187 IN_PROC_BROWSER_TEST_F(MagnificationControllerTest,
188 FollowFocusOnWebPageButtonIntersected) {
189 DCHECK(IsMagnifierEnabled());
190 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
191 browser(), GURL(std::string(kDataURIPrefix) + kTestHtmlContent1)));
193 // Move the magnifier window to intersect with the button.
194 const gfx::Rect button_bounds = GetControlBoundsInRoot("test_button");
195 MoveMagnifierWindow(button_bounds.CenterPoint().x(),
196 button_bounds.CenterPoint().y());
197 EXPECT_TRUE(GetViewPort().Intersects(button_bounds));
199 // Set the focus on the button.
200 SetFocusOnElement("test_button");
202 // Verify the magnifier window is moved to contain the focused button,
203 // and the button is centered at the magnifier window.
204 EXPECT_TRUE(GetViewPort().Contains(button_bounds));
205 EXPECT_EQ(GetViewPort().CenterPoint(), button_bounds.CenterPoint());
208 IN_PROC_BROWSER_TEST_F(MagnificationControllerTest,
209 FollowFocusOnWebButtonContained) {
210 DCHECK(IsMagnifierEnabled());
211 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(
212 browser(), GURL(std::string(kDataURIPrefix) + kTestHtmlContent2)));
214 // Move magnifier window to contain the button.
215 const gfx::Rect button_bounds = GetControlBoundsInRoot("test_button");
216 MoveMagnifierWindow(button_bounds.x() - 100, button_bounds.y() - 100);
217 const gfx::Rect view_port_before_focus = GetViewPort();
218 EXPECT_TRUE(view_port_before_focus.Contains(button_bounds));
220 // Set the focus on the button.
221 SetFocusOnElement("test_button");
223 // Verify the magnifier window is not moved and still contains the button.
224 const gfx::Rect view_port_after_focus = GetViewPort();
225 EXPECT_TRUE(view_port_after_focus.Contains(button_bounds));
226 EXPECT_EQ(view_port_before_focus, view_port_after_focus);
229 } // namespace chromeos