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
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/accessibility/switch_access_test_utils.h"
#include "ash/constants/ash_pref_names.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/path_service.h"
#include "base/strings/stringprintf.h"
#include "chrome/browser/ash/accessibility/accessibility_manager.h"
#include "chrome/common/extensions/extension_constants.h"
#include "extensions/browser/browsertest_util.h"
#include "extensions/browser/extension_host_test_helper.h"
#include "extensions/browser/extension_registry_test_helper.h"
namespace {
constexpr char kTestSupportPathMV2[] =
"chrome/browser/resources/chromeos/accessibility/switch_access/mv2/"
"test_support.js";
constexpr char kTestSupportPathMV3[] =
"chrome/browser/resources/chromeos/accessibility/switch_access/mv3/"
"test_support.js";
} // namespace
namespace ash {
SwitchAccessTestUtils::SwitchAccessTestUtils(Profile* profile) {
profile_ = profile;
console_observer_ = std::make_unique<ExtensionConsoleErrorObserver>(
profile_, extension_misc::kSwitchAccessExtensionId);
}
SwitchAccessTestUtils::~SwitchAccessTestUtils() = default;
void SwitchAccessTestUtils::EnableSwitchAccess(
const std::set<int>& select_key_codes,
const std::set<int>& next_key_codes,
const std::set<int>& previous_key_codes) {
AccessibilityManager* manager = AccessibilityManager::Get();
// Watch events from an MV2 extension which runs in a background page.
extensions::ExtensionHostTestHelper host_helper(
profile_, extension_misc::kSwitchAccessExtensionId);
// Watch events from an MV3 extension which runs in a service worker.
extensions::ExtensionRegistryTestHelper observer(
extension_misc::kSwitchAccessExtensionId, profile_);
manager->SetSwitchAccessEnabled(true);
if (observer.WaitForManifestVersion() == 3) {
version_ = ManifestVersion::kThree;
observer.WaitForServiceWorkerStart();
} else {
version_ = ManifestVersion::kTwo;
host_helper.WaitForHostCompletedFirstLoad();
}
manager->SetSwitchAccessKeysForTest(
select_key_codes, prefs::kAccessibilitySwitchAccessSelectDeviceKeyCodes);
manager->SetSwitchAccessKeysForTest(
next_key_codes, prefs::kAccessibilitySwitchAccessNextDeviceKeyCodes);
manager->SetSwitchAccessKeysForTest(
previous_key_codes,
prefs::kAccessibilitySwitchAccessPreviousDeviceKeyCodes);
EXPECT_TRUE(manager->IsSwitchAccessEnabled());
InjectFocusRingWatcher();
}
void SwitchAccessTestUtils::WaitForFocusRing(const std::string& type,
const std::string& role,
const std::string& name) {
ASSERT_TRUE(type == "primary" || type == "preview");
std::string script = base::StringPrintf(
R"JS(
waitForFocusRing("%s", "%s", "%s", () => {
chrome.test.sendScriptResult('ok');
});
)JS",
type.c_str(), role.c_str(), name.c_str());
WaitForJS(script);
}
void SwitchAccessTestUtils::DoDefault(const std::string& name) {
std::string script = base::StringPrintf(
R"JS(
doDefault("%s", () => {
chrome.test.sendScriptResult('ok');
});
)JS",
name.c_str());
WaitForJS(script);
}
void SwitchAccessTestUtils::PointScanClick(const int x, const int y) {
std::string script = base::StringPrintf(
R"JS(
pointScanClick("%d", "%d", () => {
chrome.test.sendScriptResult('ok');
});
)JS",
x, y);
WaitForJS(script);
}
void SwitchAccessTestUtils::WaitForEventOnAutomationNode(
const std::string& eventType,
const std::string& name) {
std::string script = base::StringPrintf(
R"JS(
waitForEventOnAutomationNode("%s", "%s", () => {
chrome.test.sendScriptResult('ok');
});
)JS",
eventType.c_str(), name.c_str());
WaitForJS(script);
}
void SwitchAccessTestUtils::WaitForBackButtonInitialized() {
std::string script = base::StringPrintf(
R"JS(
waitForBackButtonInitialized();
)JS");
WaitForJS(script);
}
void SwitchAccessTestUtils::ResetConsoleObserver() {
console_observer_.reset();
}
void SwitchAccessTestUtils::WaitForJS(const std::string& js_to_eval) {
base::Value value =
extensions::browsertest_util::ExecuteScriptInBackgroundPage(
profile_, extension_misc::kSwitchAccessExtensionId, js_to_eval,
extensions::browsertest_util::ScriptUserActivation::kDontActivate);
ASSERT_EQ(value, "ok");
}
void SwitchAccessTestUtils::InjectFocusRingWatcher() {
base::ScopedAllowBlockingForTesting allow_blocking;
base::FilePath source_dir;
CHECK(base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT, &source_dir));
auto test_support_path = version_ == ManifestVersion::kThree
? source_dir.AppendASCII(kTestSupportPathMV3)
: source_dir.AppendASCII(kTestSupportPathMV2);
std::string script;
ASSERT_TRUE(base::ReadFileToString(test_support_path, &script))
<< test_support_path;
base::Value value =
extensions::browsertest_util::ExecuteScriptInBackgroundPage(
profile_, extension_misc::kSwitchAccessExtensionId, script);
ASSERT_EQ("ready", value);
}
} // namespace ash
|