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 DISALLOW_COPY_AND_ASSIGN(MagnificationControllerTest); | |
dcheng
2014/12/15 23:43:29
Nit: usually this is in a private: section
jennyz
2014/12/16 00:08:57
Done.
| |
120 }; | |
121 | |
122 IN_PROC_BROWSER_TEST_F(MagnificationControllerTest, | |
123 FollowFocusOnWebPageButtonNotIntersected) { | |
124 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( | |
125 browser(), GURL(std::string(kDataURIPrefix) + kTestHtmlContent1))); | |
126 const gfx::Rect button_bounds = GetControlBoundsInRoot("test_button"); | |
127 | |
128 // Enable magnifier. | |
129 SetMagnifierEnabled(true); | |
130 // Confirms that magnifier is enabled. | |
131 EXPECT_TRUE(IsMagnifierEnabled()); | |
132 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale()); | |
133 | |
134 // Move the magnifier window to not intersect with the button. | |
135 MoveMagnifierWindow(button_bounds.right() + 200, | |
136 button_bounds.bottom() + 200); | |
137 EXPECT_FALSE(GetViewPort().Intersects(button_bounds)); | |
138 | |
139 // Set the focus on the button. | |
140 SetFocusOnElement("test_button"); | |
141 // Verify the magnifier window is moved to contain the focused button. | |
142 EXPECT_TRUE(GetViewPort().Contains(button_bounds)); | |
143 } | |
144 | |
145 IN_PROC_BROWSER_TEST_F(MagnificationControllerTest, | |
146 FollowFocusOnWebPageButtonIntersected) { | |
147 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( | |
148 browser(), GURL(std::string(kDataURIPrefix) + kTestHtmlContent1))); | |
149 const gfx::Rect button_bounds = GetControlBoundsInRoot("test_button"); | |
150 | |
151 // Enable magnifier. | |
152 SetMagnifierEnabled(true); | |
153 // Confirm that magnifier is enabled. | |
154 EXPECT_TRUE(IsMagnifierEnabled()); | |
155 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale()); | |
156 | |
157 // Move the magnifier window to intersect with the button. | |
158 MoveMagnifierWindow(button_bounds.CenterPoint().x(), | |
159 button_bounds.CenterPoint().y()); | |
160 EXPECT_TRUE(GetViewPort().Intersects(button_bounds)); | |
161 | |
162 // Set the focus on the button. | |
163 SetFocusOnElement("test_button"); | |
164 // Verify the magnifier window is moved to contain the focused button. | |
165 EXPECT_TRUE(GetViewPort().Contains(button_bounds)); | |
166 } | |
167 | |
168 IN_PROC_BROWSER_TEST_F(MagnificationControllerTest, | |
169 FollowFocusOnWebButtonContained) { | |
170 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( | |
171 browser(), GURL(std::string(kDataURIPrefix) + kTestHtmlContent2))); | |
172 const gfx::Rect button_bounds = GetControlBoundsInRoot("test_button"); | |
173 | |
174 // Enable magnifier. | |
175 SetMagnifierEnabled(true); | |
176 // Confirm that magnifier is enabled. | |
177 EXPECT_TRUE(IsMagnifierEnabled()); | |
178 EXPECT_EQ(2.0f, GetMagnificationController()->GetScale()); | |
179 | |
180 // Move magnifier window to contain the button. | |
181 MoveMagnifierWindow(button_bounds.x() - 100, button_bounds.y() - 100); | |
182 const gfx::Rect view_port_before_focus = GetViewPort(); | |
183 EXPECT_TRUE(view_port_before_focus.Contains(button_bounds)); | |
184 | |
185 // Set the focus on the button. | |
186 SetFocusOnElement("test_button"); | |
187 // Verify the magnifier window is not moved and still contains the button. | |
188 const gfx::Rect view_port_after_focus = GetViewPort(); | |
189 EXPECT_TRUE(view_port_after_focus.Contains(button_bounds)); | |
190 EXPECT_EQ(view_port_before_focus, view_port_after_focus); | |
191 } | |
192 | |
193 } // namespace chromeos | |
OLD | NEW |