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 337 338 339 340
|
// 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 "ash/accessibility/autoclick/autoclick_controller.h"
#include "ash/accessibility/ui/accessibility_focus_ring_controller_impl.h"
#include "ash/accessibility/ui/accessibility_focus_ring_layer.h"
#include "ash/constants/ash_pref_names.h"
#include "ash/public/cpp/accessibility_controller_enums.h"
#include "ash/shell.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/accessibility/service/accessibility_service_router_factory.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/autoclick_test_utils.h"
#include "chrome/browser/ash/accessibility/service/fake_accessibility_service.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/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/prefs/pref_service.h"
#include "content/public/test/accessibility_notification_waiter.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "ui/accessibility/accessibility_features.h"
#include "ui/aura/window_tree_host.h"
#include "ui/events/test/event_generator.h"
#include "url/url_constants.h"
namespace ash {
namespace {
const char* kShowButtonOnClickUrl =
"data:text/html,"
"<input type='button' value='click me'"
"onclick=\"document.getElementById('result').removeAttribute('hidden')\">"
"<input type='button' id='result' hidden value='show me'>";
} // namespace
// Tests that Automatic clicks works with elements in the browser.
class AutoclickBrowserTest
: public AccessibilityFeatureBrowserTest,
public ::testing::WithParamInterface<ManifestVersion> {
public:
AutoclickBrowserTest(const AutoclickBrowserTest&) = delete;
AutoclickBrowserTest& operator=(const AutoclickBrowserTest&) = delete;
protected:
AutoclickBrowserTest() = default;
~AutoclickBrowserTest() override = default;
void SetUpCommandLine(base::CommandLine* command_line) override {
std::vector<base::test::FeatureRef> enabled_features;
std::vector<base::test::FeatureRef> disabled_features;
if (GetParam() == ManifestVersion::kTwo) {
disabled_features.push_back(
::features::kAccessibilityManifestV3AccessibilityCommon);
} else if (GetParam() == ManifestVersion::kThree) {
enabled_features.push_back(
::features::kAccessibilityManifestV3AccessibilityCommon);
}
scoped_feature_list_.InitWithFeatures(enabled_features, disabled_features);
AccessibilityFeatureBrowserTest::SetUpCommandLine(command_line);
}
// InProcessBrowserTest:
void SetUpOnMainThread() override {
aura::Window* root_window = Shell::Get()->GetPrimaryRootWindow();
generator_ = std::make_unique<ui::test::EventGenerator>(root_window);
autoclick_test_utils_ = std::make_unique<AutoclickTestUtils>(GetProfile());
AccessibilityFeatureBrowserTest::SetUpOnMainThread();
NavigateToUrl(GURL(url::kAboutBlankURL));
}
void TearDownOnMainThread() override { autoclick_test_utils_.reset(); }
PrefService* GetPrefs() { return GetProfile()->GetPrefs(); }
// Loads a page with the given URL and then starts up Autoclick.
void LoadURLAndAutoclick(const std::string& url) {
NavigateToUrl(GURL(url));
autoclick_test_utils_->LoadAutoclick();
autoclick_test_utils_->WaitForPageLoad(url);
}
ui::test::EventGenerator* generator() { return generator_.get(); }
AutoclickTestUtils* utils() { return autoclick_test_utils_.get(); }
private:
std::unique_ptr<ui::test::EventGenerator> generator_;
std::unique_ptr<AutoclickTestUtils> autoclick_test_utils_;
base::test::ScopedFeatureList scoped_feature_list_;
};
// TODO(crbug.com/388867838): Add manifest v3 variant when migration is
// complete.
INSTANTIATE_TEST_SUITE_P(ManifestV2,
AutoclickBrowserTest,
::testing::Values(ManifestVersion::kTwo));
IN_PROC_BROWSER_TEST_P(AutoclickBrowserTest, LeftClickButtonOnHover) {
LoadURLAndAutoclick(kShowButtonOnClickUrl);
// No need to change click type: Default should be right-click.
utils()->HoverOverHtmlElement(generator(), "click me", "button");
// Wait for button to be shown.
utils()->GetNodeBoundsInRoot("show me", "button");
}
IN_PROC_BROWSER_TEST_P(AutoclickBrowserTest, DoubleClickHover) {
LoadURLAndAutoclick(
"data:text/html;charset=utf-8,"
"<input type='text' id='text_field'"
"value='peanutbuttersandwichmadewithjam'>");
utils()->SetAutoclickEventTypeWithHover(generator(),
AutoclickEventType::kDoubleClick);
// Double-clicking over the text field should result in the text being
// selected.
utils()->HoverOverHtmlElement(generator(), "peanutbuttersandwichmadewithjam",
"staticText");
utils()->WaitForTextSelectionChangedEvent();
}
IN_PROC_BROWSER_TEST_P(AutoclickBrowserTest, ClickAndDrag) {
LoadURLAndAutoclick(
"data:text/html;charset=utf-8,"
"<input type='text' id='text_field'"
"value='peanutbuttersandwichmadewithjam'>");
utils()->SetAutoclickEventTypeWithHover(generator(),
AutoclickEventType::kDragAndDrop);
gfx::Rect bounds = utils()->GetNodeBoundsInRoot(
"peanutbuttersandwichmadewithjam", "staticText");
// First hover causes a down click even that changes the caret.
generator()->MoveMouseTo(
gfx::Point(bounds.left_center().y(), bounds.x() + 10));
utils()->WaitForTextSelectionChangedEvent();
// Second hover causes a selection.
generator()->MoveMouseTo(bounds.right_center());
utils()->WaitForTextSelectionChangedEvent();
}
IN_PROC_BROWSER_TEST_P(AutoclickBrowserTest,
RightClickOnHoverOpensContextMenu) {
LoadURLAndAutoclick(
"data:text/html;charset=utf-8,"
"<input type='text' id='text_field' value='stop copying me'>");
utils()->SetAutoclickEventTypeWithHover(generator(),
AutoclickEventType::kRightClick);
// Right clicking over the text field should result in a context menu.
utils()->HoverOverHtmlElement(generator(), "stop copying me", "staticText");
// When the context menu is shown, it has options for copy/paste
// because this is a textarea.
utils()->GetNodeBoundsInRoot("Copy Ctrl+C", "menuItem");
utils()->GetNodeBoundsInRoot("Paste Ctrl+V", "menuItem");
}
IN_PROC_BROWSER_TEST_P(AutoclickBrowserTest,
ScrollHoverHighlightsScrollableArea) {
utils()->ObserveFocusRings();
const std::string kQuoteText =
"'Whatever you choose to do, leave tracks. That means don't do it just "
"for yourself. You will want to leave the world a little better for your "
"having lived.'";
LoadURLAndAutoclick(
"data:text/html;charset=utf-8,"
"<textarea id='test_textarea' class='scrollableField' rows='2'' "
"cols='20'>" +
kQuoteText + "</textarea>");
gfx::Rect bounds =
utils()->GetBoundsForNodeInRootByClassName("scrollableField");
gfx::Rect found_bounds;
base::RunLoop waiter;
Shell::Get()->autoclick_controller()->SetScrollableBoundsCallbackForTesting(
base::BindLambdaForTesting([&waiter, &bounds, &found_bounds](
const gfx::Rect& scrollable_bounds) {
found_bounds = scrollable_bounds;
if (scrollable_bounds == bounds && waiter.running()) {
waiter.Quit();
}
}));
AccessibilityFocusRingControllerImpl* controller =
Shell::Get()->accessibility_focus_ring_controller();
std::string focus_ring_id = AccessibilityManager::Get()->GetFocusRingId(
ax::mojom::AssistiveTechnologyType::kAutoClick, "");
const AccessibilityFocusRingGroup* focus_ring_group =
controller->GetFocusRingGroupForTesting(focus_ring_id);
// No focus rings to start.
EXPECT_EQ(nullptr, focus_ring_group);
utils()->SetAutoclickEventTypeWithHover(generator(),
AutoclickEventType::kScroll);
utils()->HoverOverHtmlElement(generator(), kQuoteText, "staticText");
utils()->WaitForFocusRingChanged();
focus_ring_group = controller->GetFocusRingGroupForTesting(focus_ring_id);
ASSERT_NE(nullptr, focus_ring_group);
std::vector<std::unique_ptr<AccessibilityFocusRingLayer>> const& focus_rings =
focus_ring_group->focus_layers_for_testing();
ASSERT_EQ(focus_rings.size(), 1u);
if (found_bounds != bounds) {
// Wait for bounds changed.
waiter.Run();
}
}
IN_PROC_BROWSER_TEST_P(AutoclickBrowserTest, LongDelay) {
utils()->SetAutoclickDelayMs(500);
LoadURLAndAutoclick(kShowButtonOnClickUrl);
base::ElapsedTimer timer;
utils()->HoverOverHtmlElement(generator(), "click me", "button");
utils()->GetNodeBoundsInRoot("show me", "button");
EXPECT_GT(timer.Elapsed().InMilliseconds(), 500);
}
IN_PROC_BROWSER_TEST_P(AutoclickBrowserTest, PauseAutoclick) {
utils()->SetAutoclickDelayMs(5);
LoadURLAndAutoclick(
"data:text/html,"
"<input type='button' value='click me'"
"onclick='window.close()'>");
utils()->SetAutoclickEventTypeWithHover(generator(),
AutoclickEventType::kNoAction);
base::OneShotTimer timer;
base::RunLoop runner;
utils()->HoverOverHtmlElement(generator(), "click me", "button");
timer.Start(FROM_HERE, base::Milliseconds(2000),
base::BindLambdaForTesting([&runner, this]() {
runner.Quit();
// If autoclick was enabled, the webpage would have
// been closed, and this would fail.
utils()->GetNodeBoundsInRoot("click me", "button");
}));
runner.Run();
}
class AutoclickWithAccessibilityServiceTest : public AutoclickBrowserTest {
public:
AutoclickWithAccessibilityServiceTest() = default;
~AutoclickWithAccessibilityServiceTest() override = default;
AutoclickWithAccessibilityServiceTest(
const AutoclickWithAccessibilityServiceTest&) = delete;
AutoclickWithAccessibilityServiceTest& operator=(
const AutoclickWithAccessibilityServiceTest&) = delete;
void SetUpCommandLine(base::CommandLine* command_line) override {
scoped_feature_list_.InitAndEnableFeature(
::features::kAccessibilityService);
}
void SetUpOnMainThread() override {
AutoclickBrowserTest::SetUpOnMainThread();
// Replaces normal AccessibilityService with a fake one.
ax::AccessibilityServiceRouterFactory::GetInstanceForTest()
->SetTestingFactoryAndUse(
ash::AccessibilityManager::Get()->profile(),
base::BindRepeating(&AutoclickWithAccessibilityServiceTest::
CreateTestAccessibilityService,
base::Unretained(this)));
}
protected:
// Unowned.
raw_ptr<FakeAccessibilityService, DanglingUntriaged> fake_service_ = nullptr;
private:
std::unique_ptr<KeyedService> CreateTestAccessibilityService(
content::BrowserContext* context) {
std::unique_ptr<FakeAccessibilityService> fake_service =
std::make_unique<FakeAccessibilityService>();
fake_service_ = fake_service.get();
return std::move(fake_service);
}
base::test::ScopedFeatureList scoped_feature_list_;
};
// TODO(crbug.com/388867838): Add manifest v3 variant when migration is
// complete.
INSTANTIATE_TEST_SUITE_P(ManifestV2,
AutoclickWithAccessibilityServiceTest,
::testing::Values(ManifestVersion::kTwo));
// TODO(b/262637071): When the AccessibilityService is on (instead of a fake),
// check the focus ring bounds too, as autoclick JS should set these.
IN_PROC_BROWSER_TEST_P(AutoclickWithAccessibilityServiceTest,
ScrollableBoundsPlumbing) {
const std::string kQuoteText =
"'Whatever you choose to do, leave tracks. That means don't do it just "
"for yourself. You will want to leave the world a little better for your "
"having lived.'";
LoadURLAndAutoclick(
"data:text/html;charset=utf-8,"
"<textarea id='test_textarea' class='scrollableField' rows='2'' "
"cols='20'>" +
kQuoteText + "</textarea>");
gfx::Rect bounds =
utils()->GetBoundsForNodeInRootByClassName("scrollableField");
fake_service_->BindAnotherAutoclickClient();
utils()->SetAutoclickEventTypeWithHover(generator(),
AutoclickEventType::kScroll);
fake_service_->set_autoclick_scrollable_bounds(bounds);
base::RunLoop waiter;
Shell::Get()->autoclick_controller()->SetScrollableBoundsCallbackForTesting(
base::BindLambdaForTesting(
[&waiter, &bounds](const gfx::Rect& scrollable_bounds) {
if (scrollable_bounds == bounds) {
waiter.Quit();
}
}));
utils()->HoverOverHtmlElement(generator(), kQuoteText, "staticText");
waiter.Run();
}
} // namespace ash
|