1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include "ash/accessibility/ui/accessibility_cursor_ring_layer.h"
#include "ash/accessibility/ui/accessibility_focus_ring_controller_impl.h"
#include "ash/accessibility/ui/accessibility_focus_ring_layer.h"
#include "ash/accessibility/ui/accessibility_highlight_layer.h"
#include "ash/constants/ash_pref_names.h"
#include "ash/public/cpp/accelerators.h"
#include "ash/shell.h"
#include "build/build_config.h"
#include "chrome/browser/ash/accessibility/accessibility_feature_browsertest.h"
#include "chrome/browser/ash/accessibility/accessibility_manager.h"
#include "chrome/browser/ash/accessibility/accessibility_test_utils.h"
#include "chrome/browser/ash/accessibility/automation_test_utils.h"
#include "chrome/browser/ash/accessibility/select_to_speak_test_utils.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/test/accessibility_notification_waiter.h"
#include "content/public/test/browser_test.h"
#include "ui/compositor/layer.h"
#include "ui/events/keycodes/keyboard_codes_posix.h"
#include "ui/events/test/event_generator.h"
#include "ui/wm/core/coordinate_conversion.h"
namespace ash {
class AccessibilityHighlightsBrowserTest
: public AccessibilityFeatureBrowserTest {
public:
AccessibilityHighlightsBrowserTest(
const AccessibilityHighlightsBrowserTest&) = delete;
AccessibilityHighlightsBrowserTest& operator=(
const AccessibilityHighlightsBrowserTest&) = delete;
protected:
AccessibilityHighlightsBrowserTest() = default;
~AccessibilityHighlightsBrowserTest() override = default;
// InProcessBrowserTest:
void SetUpOnMainThread() override {
aura::Window* root_window = Shell::Get()->GetPrimaryRootWindow();
generator_ = std::make_unique<ui::test::EventGenerator>(root_window);
AccessibilityManager::Get()->SetFocusRingObserverForTest(
base::BindRepeating(
&AccessibilityHighlightsBrowserTest::OnFocusRingsChanged,
base::Unretained(this)));
Shell::Get()->accessibility_focus_ring_controller()->SetNoFadeForTesting();
AccessibilityFeatureBrowserTest::SetUpOnMainThread();
// Load Select to Speak so we have a Javascript context with access to the
// Automation API to inject AutomationTestUtils. Select to Speak doesn't do
// any work unless it is triggered, so this does not impact the test.
sts_test_utils::TurnOnSelectToSpeakForTest(GetProfile());
utils_ = std::make_unique<AutomationTestUtils>(
extension_misc::kSelectToSpeakExtensionId);
utils_->SetUpTestSupport();
NavigateToUrl(GURL(url::kAboutBlankURL));
}
void OnFocusRingsChanged() {
if (focus_ring_waiter_) {
std::move(focus_ring_waiter_).Run();
}
}
void WaitForFocusRingsChanged() {
base::RunLoop run_loop;
focus_ring_waiter_ = run_loop.QuitClosure();
run_loop.Run();
}
std::unique_ptr<ui::test::EventGenerator> generator_;
std::unique_ptr<AutomationTestUtils> utils_;
private:
base::OnceClosure focus_ring_waiter_;
};
IN_PROC_BROWSER_TEST_F(AccessibilityHighlightsBrowserTest,
CursorHighlightAddsFocusRing) {
AccessibilityFocusRingControllerImpl* controller =
Shell::Get()->accessibility_focus_ring_controller();
EXPECT_FALSE(controller->cursor_layer_for_testing());
PrefService* prefs = GetProfile()->GetPrefs();
prefs->SetBoolean(prefs::kAccessibilityCursorHighlightEnabled, true);
gfx::Point mouse_location(100, 100);
generator_->MoveMouseTo(mouse_location);
AccessibilityCursorRingLayer* cursor_layer =
controller->cursor_layer_for_testing();
ASSERT_TRUE(cursor_layer);
gfx::Rect bounds = cursor_layer->layer()->GetTargetBounds();
EXPECT_EQ(bounds.CenterPoint(), mouse_location);
mouse_location = gfx::Point(200, 100);
generator_->MoveMouseTo(mouse_location);
bounds = cursor_layer->layer()->GetTargetBounds();
EXPECT_EQ(bounds.CenterPoint(), mouse_location);
// Turns off again.
prefs->SetBoolean(prefs::kAccessibilityCursorHighlightEnabled, false);
EXPECT_FALSE(controller->cursor_layer_for_testing());
}
IN_PROC_BROWSER_TEST_F(AccessibilityHighlightsBrowserTest,
CaretHighlightWebContents) {
AccessibilityFocusRingControllerImpl* controller =
Shell::Get()->accessibility_focus_ring_controller();
EXPECT_FALSE(controller->caret_layer_for_testing());
PrefService* prefs = GetProfile()->GetPrefs();
prefs->SetBoolean(prefs::kAccessibilityCaretHighlightEnabled, true);
// Still doesn't exist because no input text area is focused.
EXPECT_FALSE(controller->caret_layer_for_testing());
const struct {
std::string url;
std::string name;
std::string role;
} kTestCases[] = {{"data:text/html;charset=utf-8,"
"<textarea>Hello there</textarea>",
"Hello there", "staticText"},
{"data:text/html;charset=utf-8,"
"<input type=\"text\" value=\"Hows it going?\">",
"Hows it going?", "staticText"},
{"data:text/html;charset=utf-8,"
"<div contenteditable=\"true\">"
"<p>Not bad, and <b>you</b>?</p>"
"</div>",
"Not bad, and ", "staticText"}};
for (const auto& testCase : kTestCases) {
NavigateToUrl(GURL(testCase.url));
gfx::Rect element_bounds =
utils_->GetNodeBoundsInRoot(testCase.name, testCase.role);
generator_->PressAndReleaseKey(ui::KeyboardCode::VKEY_TAB);
WaitForFocusRingsChanged();
AccessibilityCursorRingLayer* caret_layer =
controller->caret_layer_for_testing();
while (caret_layer == nullptr) {
WaitForFocusRingsChanged();
caret_layer = controller->caret_layer_for_testing();
}
ASSERT_TRUE(caret_layer);
gfx::Rect initial_bounds = caret_layer->layer()->GetTargetBounds();
EXPECT_TRUE(element_bounds.Contains(initial_bounds.CenterPoint()));
// Right arrow shifts the bounds to the right slightly.
generator_->PressAndReleaseKey(ui::KeyboardCode::VKEY_RIGHT);
WaitForFocusRingsChanged();
gfx::Rect new_bounds = caret_layer->layer()->GetTargetBounds();
EXPECT_EQ(initial_bounds.y(), new_bounds.y());
EXPECT_LT(initial_bounds.x(), new_bounds.x());
// Typing something shifts the bounds to the right also.
generator_->PressAndReleaseKey(ui::KeyboardCode::VKEY_A);
WaitForFocusRingsChanged();
initial_bounds = new_bounds;
new_bounds = caret_layer->layer()->GetTargetBounds();
EXPECT_EQ(initial_bounds.y(), new_bounds.y());
EXPECT_LT(initial_bounds.x(), new_bounds.x());
}
// Turns off again.
prefs->SetBoolean(prefs::kAccessibilityCaretHighlightEnabled, false);
EXPECT_FALSE(controller->caret_layer_for_testing());
}
IN_PROC_BROWSER_TEST_F(AccessibilityHighlightsBrowserTest,
CaretHighlightOmnibox) {
AccessibilityFocusRingControllerImpl* controller =
Shell::Get()->accessibility_focus_ring_controller();
PrefService* prefs = GetProfile()->GetPrefs();
prefs->SetBoolean(prefs::kAccessibilityCaretHighlightEnabled, true);
// Will wait for the omnibox to be shown.
const gfx::Rect omnibox_bounds =
utils_->GetBoundsForNodeInRootByClassName("OmniboxViewViews");
// Jump to the omnibox.
generator_->PressAndReleaseKeyAndModifierKeys(ui::KeyboardCode::VKEY_L,
ui::EF_CONTROL_DOWN);
WaitForFocusRingsChanged();
AccessibilityCursorRingLayer* caret_layer =
controller->caret_layer_for_testing();
ASSERT_TRUE(caret_layer);
gfx::Rect bounds = caret_layer->layer()->GetTargetBounds();
EXPECT_EQ(bounds.CenterPoint().y(), omnibox_bounds.CenterPoint().y());
// On the left edge of the omnibox.
EXPECT_LT(bounds.x(), omnibox_bounds.x());
EXPECT_GT(bounds.right(), omnibox_bounds.x());
// Typing something shifts the bounds to the right.
generator_->PressAndReleaseKey(ui::KeyboardCode::VKEY_K);
gfx::Rect new_bounds = caret_layer->layer()->GetTargetBounds();
if (new_bounds == bounds) {
WaitForFocusRingsChanged();
new_bounds = caret_layer->layer()->GetTargetBounds();
}
EXPECT_EQ(bounds.y(), new_bounds.y());
EXPECT_LT(bounds.x(), new_bounds.x());
prefs->SetBoolean(prefs::kAccessibilityCaretHighlightEnabled, false);
}
IN_PROC_BROWSER_TEST_F(AccessibilityHighlightsBrowserTest, FocusHighlight) {
AccessibilityFocusRingControllerImpl* controller =
Shell::Get()->accessibility_focus_ring_controller();
PrefService* prefs = GetProfile()->GetPrefs();
prefs->SetBoolean(prefs::kAccessibilityFocusHighlightEnabled, true);
const std::string url =
"data:text/html;charset=utf-8,"
"<input type=\"text\" value=\"long enough text to fill a whole textbox\">"
"<input type=\"checkbox\" id=\"focus2\"><label for=\"focus2\">pick "
"me</label>"
"<input type=\"radio\" id=\"focus3\"><label for=\"focus3\">radio "
"me</label>"
"<input type=\"submit\">"
"<a href=\"\">link</a>";
NavigateToUrl(GURL(url));
const struct {
std::string name;
std::string role;
} kTestCases[] = {{"long enough text to fill a whole textbox", "staticText"},
{"pick me", "checkBox"},
{"radio me", "radioButton"},
{"Submit", "button"},
{"link", "link"}};
for (const auto& testCase : kTestCases) {
// Waits for the page to be loaded.
gfx::Rect element_bounds =
utils_->GetNodeBoundsInRoot(testCase.name, testCase.role);
generator_->PressAndReleaseKey(ui::KeyboardCode::VKEY_TAB);
WaitForFocusRingsChanged();
const AccessibilityFocusRingGroup* highlights =
controller->GetFocusRingGroupForTesting("HighlightController");
ASSERT_TRUE(highlights);
auto& focus_rings = highlights->focus_layers_for_testing();
EXPECT_EQ(focus_rings.size(), 1u);
gfx::Rect focus_bounds = focus_rings.at(0)->layer()->GetTargetBounds();
if (testCase.role == "staticText") {
// In the case of the text input field, the static text node we've
// found bounds for within the input field is slightly shorter than its
// parent to visually show that it will scroll. Check the center points
// are within one pixel of each other.
EXPECT_LT(
(element_bounds.CenterPoint() - focus_bounds.CenterPoint()).Length(),
2);
} else {
EXPECT_EQ(element_bounds.CenterPoint(), focus_bounds.CenterPoint());
}
EXPECT_TRUE(focus_bounds.Contains(element_bounds));
}
prefs->SetBoolean(prefs::kAccessibilityFocusHighlightEnabled, false);
}
IN_PROC_BROWSER_TEST_F(AccessibilityHighlightsBrowserTest,
FocusHighlightNative) {
AccessibilityFocusRingControllerImpl* controller =
Shell::Get()->accessibility_focus_ring_controller();
PrefService* prefs = GetProfile()->GetPrefs();
prefs->SetBoolean(prefs::kAccessibilityFocusHighlightEnabled, true);
// Wait for omnibox, at this point the browser window has loaded.
utils_->GetBoundsForNodeInRootByClassName("OmniboxViewViews");
// Jump to the omnibox. Focus ring will also jump there.
generator_->PressAndReleaseKeyAndModifierKeys(ui::KeyboardCode::VKEY_L,
ui::EF_CONTROL_DOWN);
WaitForFocusRingsChanged();
const AccessibilityFocusRingGroup* highlights =
controller->GetFocusRingGroupForTesting("HighlightController");
ASSERT_TRUE(highlights);
auto& focus_rings = highlights->focus_layers_for_testing();
EXPECT_EQ(focus_rings.size(), 1u);
gfx::Rect omnibox_focus_bounds =
focus_rings.at(0)->layer()->GetTargetBounds();
// Open quick settings.
AcceleratorController::Get()->PerformActionIfEnabled(
AcceleratorAction::kToggleSystemTrayBubble, {});
// Wait for quick settings to be open, then focus the first button.
utils_->SetFocusOnNode("Show Bluetooth settings. Bluetooth is on.", "button");
generator_->PressAndReleaseKey(ui::KeyboardCode::VKEY_TAB);
// The accessibility highlight should draw there (note: it draws immediately
// because it's in the same process).
gfx::Rect bounds = utils_->GetNodeBoundsInRoot(
"Show Bluetooth settings. Bluetooth is on.", "button");
// Focus has moved from the omnibox.
gfx::Rect focus_bounds = focus_rings.at(0)->layer()->GetTargetBounds();
EXPECT_NE(omnibox_focus_bounds, focus_bounds);
EXPECT_NE(omnibox_focus_bounds.CenterPoint().x(),
focus_bounds.CenterPoint().x());
EXPECT_FALSE(std::abs(omnibox_focus_bounds.CenterPoint().y() -
focus_bounds.CenterPoint().y()) < 100);
// Focus has landed on the button (with some buffer).
EXPECT_EQ(bounds.CenterPoint().x(), focus_bounds.CenterPoint().x());
EXPECT_TRUE(std::abs(bounds.CenterPoint().y() -
focus_bounds.CenterPoint().y()) < 100);
// Close quick settings, focus should be gone from that area.
AcceleratorController::Get()->PerformActionIfEnabled(
AcceleratorAction::kToggleSystemTrayBubble, {});
// Check the focus rings are now different again.
gfx::Rect final_bounds = focus_rings.at(0)->layer()->GetTargetBounds();
EXPECT_NE(final_bounds, focus_bounds);
EXPECT_NE(final_bounds.CenterPoint().x(), focus_bounds.CenterPoint().x());
EXPECT_FALSE(std::abs(final_bounds.CenterPoint().y() -
focus_bounds.CenterPoint().y()) < 100);
}
} // namespace ash
|