Commit 8f402af9 authored by David Tseng's avatar David Tseng Committed by Chromium LUCI CQ

Catch js errors from native browser tests for all accessibility component extensions

AX-Relnotes: n/a
Bug: none

R=katie@chromium.org

Change-Id: Id29f9a97d0342e8333f8bebff9781460fb05fa56
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2643758Reviewed-by: default avatarKatie Dektar <katie@chromium.org>
Commit-Queue: David Tseng <dtseng@chromium.org>
Cr-Commit-Position: refs/heads/master@{#845892}
parent 2051bc2e
......@@ -4,8 +4,10 @@
#include "ash/public/cpp/ash_pref_names.h"
#include "chrome/browser/ash/accessibility/accessibility_manager.h"
#include "chrome/browser/ash/accessibility/accessibility_test_utils.h"
#include "chrome/browser/extensions/component_loader.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/prefs/pref_service.h"
......@@ -24,8 +26,22 @@ class AccessibilityCommonTest : public InProcessBrowserTest {
->Exists(id);
}
void SetUpOnMainThread() override {
console_observer_ = std::make_unique<ExtensionConsoleErrorObserver>(
browser()->profile(), extension_misc::kAccessibilityCommonExtensionId);
}
void TearDownOnMainThread() override {
EXPECT_FALSE(console_observer_->HasErrorsOrWarnings())
<< "Found console.log or console.warn with message: "
<< console_observer_->GetErrorOrWarningAt(0);
}
protected:
AccessibilityCommonTest() = default;
private:
std::unique_ptr<ExtensionConsoleErrorObserver> console_observer_;
};
IN_PROC_BROWSER_TEST_F(AccessibilityCommonTest, ToggleFeatures) {
......
// Copyright 2021 The Chromium Authors. All rights reserved.
// 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/accessibility_test_utils.h"
#include "extensions/browser/extension_host.h"
#include "extensions/browser/process_manager.h"
ExtensionConsoleErrorObserver::ExtensionConsoleErrorObserver(
content::BrowserContext* context,
const char* extension_id)
: context_(context), extension_id_(extension_id) {
auto* pm = extensions::ProcessManager::Get(context_);
CHECK(pm);
pm->AddObserver(this);
// In case the extension already loaded.
extensions::ExtensionHost* host =
pm->GetBackgroundHostForExtension(extension_id_);
if (host)
OnBackgroundHostCreated(host);
}
ExtensionConsoleErrorObserver ::~ExtensionConsoleErrorObserver() {
// Intentionally skip removal of this instance from ProcessManager; it leads
// to errors in DependencyManager.
}
bool ExtensionConsoleErrorObserver::HasErrorsOrWarnings() {
return console_observer_ && !console_observer_->messages().empty();
}
std::string ExtensionConsoleErrorObserver::GetErrorOrWarningAt(
size_t index) const {
return console_observer_ ? console_observer_->GetMessageAt(index)
: std::string();
}
size_t ExtensionConsoleErrorObserver::GetErrorsAndWarningsCount() const {
return console_observer_ ? console_observer_->messages().size() : 0U;
}
void ExtensionConsoleErrorObserver::OnBackgroundHostCreated(
extensions::ExtensionHost* host) {
if (host->extension_id() != extension_id_)
return;
console_observer_ = std::make_unique<content::WebContentsConsoleObserver>(
host->host_contents());
auto filter =
[](const content::WebContentsConsoleObserver::Message& message) {
return message.log_level ==
blink::mojom::ConsoleMessageLevel::kWarning ||
message.log_level == blink::mojom::ConsoleMessageLevel::kError;
};
console_observer_->SetFilter(base::BindRepeating(filter));
}
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_ASH_ACCESSIBILITY_ACCESSIBILITY_TEST_UTILS_H_
#define CHROME_BROWSER_ASH_ACCESSIBILITY_ACCESSIBILITY_TEST_UTILS_H_
#include "content/public/test/browser_test_utils.h"
#include "extensions/browser/process_manager_observer.h"
// Instantiate this class to get errors and warnings for an extension.
//
// Sample usage:
//
// ExtensionConsoleErrorObserver console_observer(browser_context,
// some_extension_id); LoadSomeExtension();
// ...
// EXPECT_FALSE(console_observer.HasErrorsOrWarnings());
class ExtensionConsoleErrorObserver
: public extensions::ProcessManagerObserver {
public:
ExtensionConsoleErrorObserver(content::BrowserContext* context,
const char* extension_id);
~ExtensionConsoleErrorObserver() override;
// Returns whether errors or warnings were received.
bool HasErrorsOrWarnings();
// A helper method to return the string content (in UTF8) of the error or
// warning at the given |index|. This will cause a test failure if there is no
// such message.
std::string GetErrorOrWarningAt(size_t index) const;
// Get the number of errors and warnings received.
size_t GetErrorsAndWarningsCount() const;
// extensions::ProcessManagerObserver:
void OnBackgroundHostCreated(extensions::ExtensionHost* host) override;
private:
content::BrowserContext* context_;
const char* extension_id_;
std::unique_ptr<content::WebContentsConsoleObserver> console_observer_;
};
#endif // CHROME_BROWSER_ASH_ACCESSIBILITY_ACCESSIBILITY_TEST_UTILS_H_
......@@ -19,6 +19,7 @@
#include "base/memory/weak_ptr.h"
#include "build/branding_buildflags.h"
#include "chrome/browser/ash/accessibility/accessibility_manager.h"
#include "chrome/browser/ash/accessibility/accessibility_test_utils.h"
#include "chrome/browser/ash/accessibility/speech_monitor.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
......@@ -59,6 +60,8 @@ class SelectToSpeakTest : public InProcessBrowserTest {
void SetUpOnMainThread() override {
ASSERT_FALSE(AccessibilityManager::Get()->IsSelectToSpeakEnabled());
console_observer_ = std::make_unique<ExtensionConsoleErrorObserver>(
browser()->profile(), extension_misc::kSelectToSpeakExtensionId);
tray_test_api_ = ash::SystemTrayTestApi::Create();
content::WindowedNotificationObserver extension_load_waiter(
......@@ -67,21 +70,6 @@ class SelectToSpeakTest : public InProcessBrowserTest {
AccessibilityManager::Get()->SetSelectToSpeakEnabled(true);
extension_load_waiter.Wait();
extensions::ExtensionHost* host =
extensions::ProcessManager::Get(browser()->profile())
->GetBackgroundHostForExtension(
extension_misc::kSelectToSpeakExtensionId);
console_observer_ = std::make_unique<content::WebContentsConsoleObserver>(
host->host_contents());
// STS should not log warnings or errors: these should cause test failures.
auto filter =
[](const content::WebContentsConsoleObserver::Message& message) {
return message.log_level ==
blink::mojom::ConsoleMessageLevel::kWarning ||
message.log_level == blink::mojom::ConsoleMessageLevel::kError;
};
console_observer_->SetFilter(base::BindRepeating(filter));
aura::Window* root_window = ash::Shell::Get()->GetPrimaryRootWindow();
generator_.reset(new ui::test::EventGenerator(root_window));
......@@ -90,9 +78,9 @@ class SelectToSpeakTest : public InProcessBrowserTest {
void TearDownOnMainThread() override {
// Check STS has not generated any errors.
EXPECT_EQ(0u, console_observer_->messages().size())
EXPECT_FALSE(console_observer_->HasErrorsOrWarnings())
<< "Found console.log or console.warn with message: "
<< console_observer_->GetMessageAt(0);
<< console_observer_->GetErrorOrWarningAt(0);
}
test::SpeechMonitor sm_;
......@@ -169,7 +157,7 @@ class SelectToSpeakTest : public InProcessBrowserTest {
private:
scoped_refptr<content::MessageLoopRunner> loop_runner_;
scoped_refptr<content::MessageLoopRunner> tray_loop_runner_;
std::unique_ptr<content::WebContentsConsoleObserver> console_observer_;
std::unique_ptr<ExtensionConsoleErrorObserver> console_observer_;
base::WeakPtrFactory<SelectToSpeakTest> weak_ptr_factory_{this};
DISALLOW_COPY_AND_ASSIGN(SelectToSpeakTest);
};
......
......@@ -67,6 +67,12 @@ void LoggedInSpokenFeedbackTest::SetUpInProcessBrowserTestFixture() {
}
void LoggedInSpokenFeedbackTest::TearDownOnMainThread() {
if (console_observer_) {
EXPECT_FALSE(console_observer_->HasErrorsOrWarnings())
<< "Found console.log or console.warn with message: "
<< console_observer_->GetErrorOrWarningAt(0);
}
AccessibilityManager::SetBrailleControllerForTest(nullptr);
// Unload the ChromeVox extension so the browser doesn't try to respond to
// in-flight requests during test shutdown. https://crbug.com/923090
......@@ -152,6 +158,8 @@ void LoggedInSpokenFeedbackTest::EnableChromeVox() {
// Test setup.
// Enable ChromeVox, wait for something to be spoken, and disable earcons.
ASSERT_FALSE(AccessibilityManager::Get()->IsSpokenFeedbackEnabled());
// TODO(accessibility): fix console error/warnings and insantiate
// |console_observer_| here.
AccessibilityManager::Get()->EnableSpokenFeedback(true);
sm_.ExpectSpeechPattern("*");
......
......@@ -6,6 +6,7 @@
#define CHROME_BROWSER_ASH_ACCESSIBILITY_SPOKEN_FEEDBACK_BROWSERTEST_H_
#include "ash/public/cpp/accelerators.h"
#include "chrome/browser/ash/accessibility/accessibility_test_utils.h"
#include "chrome/browser/ash/accessibility/speech_monitor.h"
#include "chrome/browser/extensions/api/braille_display_private/stub_braille_controller.h"
#include "chrome/test/base/in_process_browser_test.h"
......@@ -53,6 +54,7 @@ class LoggedInSpokenFeedbackTest : public InProcessBrowserTest {
private:
StubBrailleController braille_controller_;
ui::ScopedAnimationDurationScaleMode animation_mode_;
std::unique_ptr<ExtensionConsoleErrorObserver> console_observer_;
DISALLOW_COPY_AND_ASSIGN(LoggedInSpokenFeedbackTest);
};
......
......@@ -8,6 +8,7 @@
#include "base/files/file_util.h"
#include "base/path_service.h"
#include "chrome/browser/ash/accessibility/accessibility_manager.h"
#include "chrome/browser/ash/accessibility/accessibility_test_utils.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/test/base/extension_load_waiter_one_shot.h"
......@@ -62,6 +63,17 @@ class SwitchAccessTest : public InProcessBrowserTest {
return output;
}
void SetUpOnMainThread() override {
console_observer_ = std::make_unique<ExtensionConsoleErrorObserver>(
browser()->profile(), extension_misc::kSwitchAccessExtensionId);
}
void TearDownOnMainThread() override {
EXPECT_FALSE(console_observer_->HasErrorsOrWarnings())
<< "Found console.log or console.warn with message: "
<< console_observer_->GetErrorOrWarningAt(0);
}
protected:
SwitchAccessTest() = default;
~SwitchAccessTest() override = default;
......@@ -110,6 +122,9 @@ class SwitchAccessTest : public InProcessBrowserTest {
extensions::browsertest_util::ScriptUserActivation::kDontActivate);
ASSERT_EQ(result, "ok");
}
private:
std::unique_ptr<ExtensionConsoleErrorObserver> console_observer_;
};
// TODO(anastasi): Add a test for typing with the virtual keyboard.
......@@ -237,4 +252,3 @@ IN_PROC_BROWSER_TEST_F(SwitchAccessTest, NavigateButtonsInTextFieldMenu) {
// Wrap back around to the "keyboard" button.
WaitForFocusRing("primary", "button", "Keyboard");
}
......@@ -2395,6 +2395,8 @@ if (!is_android) {
"../browser/apps/platform_apps/app_window_interactive_uitest_base.h",
"../browser/ash/accessibility/accessibility_common_browsertest.cc",
"../browser/ash/accessibility/accessibility_manager_browsertest.cc",
"../browser/ash/accessibility/accessibility_test_utils.cc",
"../browser/ash/accessibility/accessibility_test_utils.h",
"../browser/ash/accessibility/dictation_browsertest.cc",
"../browser/ash/accessibility/magnification_controller_browsertest.cc",
"../browser/ash/accessibility/magnification_manager_browsertest.cc",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment