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 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
|
// 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/dictation_test_utils.h"
#include <string_view>
#include "ash/constants/ash_pref_names.h"
#include "ash/shell.h"
#include "base/base_paths.h"
#include "base/containers/fixed_flat_set.h"
#include "base/files/file_util.h"
#include "base/path_service.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_restrictions.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/profiles/profile.h"
#include "chrome/browser/speech/speech_recognition_constants.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/test/base/interactive_test_utils.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/prefs/pref_service.h"
#include "content/public/test/fake_speech_recognition_manager.h"
#include "extensions/browser/browsertest_util.h"
#include "extensions/browser/extension_host_test_helper.h"
#include "extensions/browser/extension_registry_test_helper.h"
#include "media/mojo/mojom/speech_recognition_service.mojom.h"
#include "ui/accessibility/accessibility_features.h"
#include "ui/base/clipboard/clipboard_monitor.h"
#include "ui/base/clipboard/clipboard_observer.h"
#include "ui/base/ime/ash/ime_bridge.h"
#include "ui/base/ime/ash/mock_ime_input_context_handler.h"
#include "ui/base/ime/input_method_base.h"
#include "ui/events/test/event_generator.h"
#include "url/gurl.h"
namespace ash {
namespace {
constexpr char kContentEditableUrl[] = R"(
data:text/html;charset=utf-8,<div id='input' class='editableForDictation'
contenteditable autofocus></div>
)";
constexpr char kFormattedContentEditableUrl[] = R"(
data:text/html;charset=utf-8,<div id='input' class='editableForDictation'
contenteditable autofocus>
<p><strong>This</strong> <b>is</b> a <em>test</em></p></div>
)";
constexpr char kInputUrl[] = R"(
data:text/html;charset=utf-8,<input id='input' class='editableForDictation'
type='text' autofocus></input>
)";
constexpr char kTextAreaUrl[] = R"(
data:text/html;charset=utf-8,<textarea id='input'
class='editableForDictation' autofocus></textarea>
)";
constexpr char kPumpkinMV2TestFilePath[] =
"resources/chromeos/accessibility/accessibility_common/mv2/third_party/"
"pumpkin";
constexpr char kPumpkinMV3TestFilePath[] =
"resources/chromeos/accessibility/accessibility_common/mv3/third_party/"
"pumpkin";
constexpr char kTestSupportMV2Path[] =
"chrome/browser/resources/chromeos/accessibility/accessibility_common/mv2/"
"dictation/dictation_test_support.js";
constexpr char kTestSupportMV3Path[] =
"chrome/browser/resources/chromeos/accessibility/accessibility_common/mv3/"
"dictation/dictation_test_support.js";
// Listens for changes to the clipboard. This class only allows `Wait()` to be
// called once. If you need to call `Wait()` multiple times, create multiple
// instances of this class.
class ClipboardChangedWaiter : public ui::ClipboardObserver {
public:
ClipboardChangedWaiter() {
ui::ClipboardMonitor::GetInstance()->AddObserver(this);
}
ClipboardChangedWaiter(const ClipboardChangedWaiter&) = delete;
ClipboardChangedWaiter& operator=(const ClipboardChangedWaiter&) = delete;
~ClipboardChangedWaiter() override {
ui::ClipboardMonitor::GetInstance()->RemoveObserver(this);
}
void Wait() { run_loop_.Run(); }
private:
// ui::ClipboardObserver:
void OnClipboardDataChanged() override { run_loop_.Quit(); }
base::RunLoop run_loop_;
};
// Listens to when the IME commits text. This class only allows `Wait()` to be
// called once. If you need to call `Wait()` multiple times, create multiple
// instances of this class.
class CommitTextWaiter : public MockIMEInputContextHandler::Observer {
public:
CommitTextWaiter() = default;
CommitTextWaiter(const CommitTextWaiter&) = delete;
CommitTextWaiter& operator=(const CommitTextWaiter&) = delete;
~CommitTextWaiter() override = default;
void Wait(const std::u16string& expected_commit_text) {
expected_commit_text_ = expected_commit_text;
run_loop_.Run();
}
private:
// MockIMEInputContextHandler::Observer
void OnCommitText(const std::u16string& text) override {
if (text == expected_commit_text_) {
run_loop_.Quit();
}
}
std::u16string expected_commit_text_;
base::RunLoop run_loop_;
};
} // namespace
DictationTestUtils::DictationTestUtils(
speech::SpeechRecognitionType speech_recognition_type,
EditableType editable_type)
: wait_for_accessibility_common_extension_load_(true),
speech_recognition_type_(speech_recognition_type),
editable_type_(editable_type) {
automation_test_utils_ = std::make_unique<AutomationTestUtils>(
extension_misc::kAccessibilityCommonExtensionId);
test_helper_ = std::make_unique<SpeechRecognitionTestHelper>(
speech_recognition_type, media::mojom::RecognizerClientType::kDictation);
}
DictationTestUtils::~DictationTestUtils() {
if (speech_recognition_type_ == speech::SpeechRecognitionType::kNetwork) {
content::SpeechRecognitionManager::SetManagerForTesting(nullptr);
}
}
void DictationTestUtils::EnableDictation(
Profile* profile,
base::OnceCallback<void(const GURL&)> navigate_to_url) {
profile_ = profile;
console_observer_ = std::make_unique<ExtensionConsoleErrorObserver>(
profile_, extension_misc::kAccessibilityCommonExtensionId);
generator_ = std::make_unique<ui::test::EventGenerator>(
Shell::Get()->GetPrimaryRootWindow());
const bool v3_manifest =
::features::IsAccessibilityManifestV3EnabledForAccessibilityCommon();
// Set up the Pumpkin dir before turning on Dictation because the
// extension will immediately request a Pumpkin installation once activated.
DictationTestUtils::SetUpPumpkinDir(v3_manifest ? kPumpkinMV3TestFilePath
: kPumpkinMV2TestFilePath);
test_helper_->SetUp(profile_);
ASSERT_FALSE(AccessibilityManager::Get()->IsDictationEnabled());
profile_->GetPrefs()->SetBoolean(
prefs::kDictationAcceleratorDialogHasBeenAccepted, true);
if (wait_for_accessibility_common_extension_load_) {
// Use ExtensionHostTestHelper to detect when the accessibility common
// extension loads.
extensions::ExtensionHostTestHelper host_helper(
profile_, extension_misc::kAccessibilityCommonExtensionId);
// Watch events from an MV3 extension which runs in a service worker.
extensions::ExtensionRegistryTestHelper observer(
extension_misc::kAccessibilityCommonExtensionId, profile);
AccessibilityManager::Get()->SetDictationEnabled(true);
if (observer.WaitForManifestVersion() == 3) {
observer.WaitForServiceWorkerStart();
} else {
host_helper.WaitForHostCompletedFirstLoad();
}
} else {
// In some cases (e.g. DictationWithAutoclickTest) the accessibility
// common extension is already setup and loaded. For these cases, simply
// toggle Dictation.
AccessibilityManager::Get()->SetDictationEnabled(true);
}
std::string url = GetUrlForEditableType();
std::move(navigate_to_url).Run(GURL(url));
// Dictation test support references the main Dictation object, so wait for
// the main object to be created before installing test support.
WaitForDictationJSReady();
// Setup automation test support.
automation_test_utils_->SetUpTestSupport();
// Create an instance of the DictationTestSupport JS class, which can be
// used from these tests to interact with Dictation JS. For more
// information, see kTestSupportMV3Path.
SetUpTestSupport(v3_manifest ? kTestSupportMV3Path : kTestSupportMV2Path);
// Wait for focus to propagate.
WaitForEditableFocus();
// Increase Dictation's NO_FOCUSED_IME timeout to reduce flakiness on slower
// builds.
std::string script =
"dictationTestSupport.setNoFocusedImeTimeout(1000 * 1000);";
ExecuteAccessibilityCommonScript(script);
// Dictation will request a Pumpkin install when it starts up. Wait for
// the install to succeed.
WaitForPumpkinTaggerReady();
}
void DictationTestUtils::ToggleDictationWithKeystroke() {
ASSERT_NO_FATAL_FAILURE(ASSERT_TRUE(ui_test_utils::SendKeyPressToWindowSync(
nullptr, ui::KeyboardCode::VKEY_D, false, false, false, true)));
}
void DictationTestUtils::SendFinalResultAndWaitForEditableValue(
const std::string& result,
const std::string& value) {
SendFinalResultAndWait(result);
if (speech_recognition_type_ == speech::SpeechRecognitionType::kNetwork) {
automation_test_utils_->WaitForValueChangedEvent();
}
WaitForEditableValue(value);
}
void DictationTestUtils::SendFinalResultAndWaitForSelection(
const std::string& result,
int start,
int end) {
SendFinalResultAndWait(result);
automation_test_utils_->WaitForTextSelectionChangedEvent();
WaitForSelection(start, end);
}
void DictationTestUtils::SendFinalResultAndWaitForClipboardChanged(
const std::string& result) {
ClipboardChangedWaiter waiter;
SendFinalResultAndWait(result);
waiter.Wait();
}
void DictationTestUtils::WaitForRecognitionStarted() {
test_helper_->WaitForRecognitionStarted();
// Dictation initializes FocusHandler when speech recognition starts.
// Several tests require FocusHandler logic, so wait for it to initialize
// before proceeding.
WaitForFocusHandler();
}
void DictationTestUtils::WaitForRecognitionStopped() {
test_helper_->WaitForRecognitionStopped();
}
void DictationTestUtils::SendInterimResultAndWait(
const std::string& transcript) {
test_helper_->SendInterimResultAndWait(transcript);
}
void DictationTestUtils::SendFinalResultAndWait(const std::string& transcript) {
test_helper_->SendFinalResultAndWait(transcript);
}
void DictationTestUtils::SendErrorAndWait() {
test_helper_->SendErrorAndWait();
}
std::vector<base::test::FeatureRef> DictationTestUtils::GetEnabledFeatures() {
return test_helper_->GetEnabledFeatures();
}
std::vector<base::test::FeatureRef> DictationTestUtils::GetDisabledFeatures() {
return test_helper_->GetDisabledFeatures();
}
void DictationTestUtils::ExecuteAccessibilityCommonScript(
const std::string& script) {
extensions::browsertest_util::ExecuteScriptInBackgroundPage(
/*context=*/profile_,
/*extension_id=*/extension_misc::kAccessibilityCommonExtensionId,
/*script=*/script);
}
void DictationTestUtils::DisablePumpkin() {
std::string script = "dictationTestSupport.disablePumpkin();";
ExecuteAccessibilityCommonScript(script);
}
std::string DictationTestUtils::GetUrlForEditableType() {
switch (editable_type_) {
case EditableType::kTextArea:
return kTextAreaUrl;
case EditableType::kFormattedContentEditable:
return kFormattedContentEditableUrl;
case EditableType::kInput:
return kInputUrl;
case EditableType::kContentEditable:
return kContentEditableUrl;
}
}
std::string DictationTestUtils::GetEditableValue() {
return automation_test_utils_->GetValueForNodeWithClassName(
"editableForDictation");
}
void DictationTestUtils::WaitForEditableValue(const std::string& value) {
std::string script = base::StringPrintf(
"dictationTestSupport.waitForEditableValue(`%s`);", value.c_str());
ExecuteAccessibilityCommonScript(script);
}
void DictationTestUtils::WaitForSelection(int start, int end) {
std::string script = base::StringPrintf(
"dictationTestSupport.waitForSelection(%d, %d);", start, end);
ExecuteAccessibilityCommonScript(script);
}
void DictationTestUtils::InstallMockInputContextHandler() {
input_context_handler_ = std::make_unique<MockIMEInputContextHandler>();
IMEBridge::Get()->SetInputContextHandler(input_context_handler_.get());
}
// Retrieves the number of times commit text is updated.
int DictationTestUtils::GetCommitTextCallCount() {
EXPECT_TRUE(input_context_handler_);
return input_context_handler_->commit_text_call_count();
}
void DictationTestUtils::WaitForCommitText(const std::u16string& value) {
if (value == input_context_handler_->last_commit_text()) {
return;
}
CommitTextWaiter waiter;
input_context_handler_->AddObserver(&waiter);
waiter.Wait(value);
input_context_handler_->RemoveObserver(&waiter);
}
void DictationTestUtils::SetUpPumpkinDir(const char* pumpkin_dir) {
// Set the path to the Pumpkin test files. For more details, see the
// `pumpkin_test_files` rule in the accessibility_common BUILD file.
base::ScopedAllowBlockingForTesting allow_blocking;
base::FilePath gen_root_dir;
ASSERT_TRUE(
base::PathService::Get(base::DIR_OUT_TEST_DATA_ROOT, &gen_root_dir));
base::FilePath pumpkin_test_file_path = gen_root_dir.AppendASCII(pumpkin_dir);
ASSERT_TRUE(base::PathExists(pumpkin_test_file_path));
AccessibilityManager::Get()->SetDlcPathForTest(pumpkin_test_file_path);
}
void DictationTestUtils::SetUpTestSupport(const char* test_support_dir) {
base::ScopedAllowBlockingForTesting allow_blocking;
base::FilePath source_dir;
CHECK(base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT, &source_dir));
auto test_support_path = source_dir.AppendASCII(test_support_dir);
std::string script;
ASSERT_TRUE(base::ReadFileToString(test_support_path, &script))
<< test_support_path;
ExecuteAccessibilityCommonScript(script);
}
void DictationTestUtils::WaitForDictationJSReady() {
std::string script = base::StringPrintf(R"JS(
(async function() {
globalThis.accessibilityCommon.setFeatureLoadCallbackForTest('dictation',
() => {
chrome.test.sendScriptResult('ready');
});
})();
)JS");
ExecuteAccessibilityCommonScript(script);
}
void DictationTestUtils::WaitForEditableFocus() {
std::string script = "dictationTestSupport.waitForEditableFocus();";
ExecuteAccessibilityCommonScript(script);
}
void DictationTestUtils::WaitForPumpkinTaggerReady() {
std::string locale =
profile_->GetPrefs()->GetString(prefs::kAccessibilityDictationLocale);
static constexpr auto kPumpkinLocales =
base::MakeFixedFlatSet<std::string_view>(
{"en-US", "fr-FR", "it-IT", "de-DE", "es-ES"});
if (!base::Contains(kPumpkinLocales, locale)) {
// If Pumpkin doesn't support the dictation locale, then it will never
// initialize.
return;
}
std::string script = "dictationTestSupport.waitForPumpkinTaggerReady();";
ExecuteAccessibilityCommonScript(script);
}
void DictationTestUtils::WaitForFocusHandler() {
std::string script = R"(
dictationTestSupport.waitForFocusHandler('editableForDictation');
)";
ExecuteAccessibilityCommonScript(script);
}
} // namespace ash
|