Commit dc7770f2 authored by Jacobo Aragunde Pérez's avatar Jacobo Aragunde Pérez Committed by Commit Bot

Add test for accessibility window activated event.

This is a tricky test due to the asynchronous nature of the event. It
is produced in response to a system window event, so we need the test
to produce an actual window so we can listen to it, and we need to add
a way to wait for it.

Bug: None
Change-Id: If7cd10c5db282f7cc465505286790ab1f4b458a2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2370984
Commit-Queue: Jacobo Aragunde Pérez <jaragunde@igalia.com>
Reviewed-by: default avatarMichael Wasserman <msw@chromium.org>
Reviewed-by: default avatarNektarios Paisios <nektar@chromium.org>
Cr-Commit-Position: refs/heads/master@{#804550}
parent 91f51a38
...@@ -13,8 +13,11 @@ ...@@ -13,8 +13,11 @@
#include "chrome/browser/ui/views/tabs/tab_strip.h" #include "chrome/browser/ui/views/tabs/tab_strip.h"
#include "chrome/common/pref_names.h" #include "chrome/common/pref_names.h"
#include "chrome/test/base/in_process_browser_test.h" #include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h" #include "content/public/test/browser_test.h"
#include "ui/views/buildflags.h"
#include "ui/views/test/ax_event_counter.h"
#if defined(OS_MAC) #if defined(OS_MAC)
#include "chrome/browser/ui/browser_commands_mac.h" #include "chrome/browser/ui/browser_commands_mac.h"
...@@ -27,8 +30,10 @@ namespace { ...@@ -27,8 +30,10 @@ namespace {
class BrowserViewTest : public InProcessBrowserTest { class BrowserViewTest : public InProcessBrowserTest {
public: public:
BrowserViewTest() = default; BrowserViewTest() : ax_observer_(views::AXEventManager::Get()) {}
~BrowserViewTest() override = default; ~BrowserViewTest() override = default;
BrowserViewTest(const BrowserViewTest&) = delete;
BrowserViewTest& operator=(const BrowserViewTest&) = delete;
void SetUpOnMainThread() override { void SetUpOnMainThread() override {
#if defined(OS_MAC) #if defined(OS_MAC)
...@@ -44,8 +49,8 @@ class BrowserViewTest : public InProcessBrowserTest { ...@@ -44,8 +49,8 @@ class BrowserViewTest : public InProcessBrowserTest {
#endif #endif
} }
private: protected:
DISALLOW_COPY_AND_ASSIGN(BrowserViewTest); views::test::AXEventCounter ax_observer_;
}; };
} // namespace } // namespace
...@@ -219,3 +224,26 @@ IN_PROC_BROWSER_TEST_F(BrowserViewTest, FullscreenShowBookmarkBar) { ...@@ -219,3 +224,26 @@ IN_PROC_BROWSER_TEST_F(BrowserViewTest, FullscreenShowBookmarkBar) {
EXPECT_TRUE(browser_view->IsTabStripVisible()); EXPECT_TRUE(browser_view->IsTabStripVisible());
EXPECT_TRUE(browser_view->IsBookmarkBarVisible()); EXPECT_TRUE(browser_view->IsBookmarkBarVisible());
} }
// TODO(crbug.com/897177): Only Aura platforms use the WindowActivated
// accessibility event. We need to harmonize the firing of accessibility events
// between platforms.
#if BUILDFLAG(ENABLE_DESKTOP_AURA)
IN_PROC_BROWSER_TEST_F(BrowserViewTest, WindowActivatedAccessibleEvent) {
// Wait for window activated event from the first browser window.
// This event is asynchronous, it is emitted as a response to a system window
// event. It is possible that we haven't received it yet when we run this test
// and we need to explicitly wait for it.
if (ax_observer_.GetCount(ax::mojom::Event::kWindowActivated) == 0)
ax_observer_.WaitForEvent(ax::mojom::Event::kWindowActivated);
ASSERT_EQ(1, ax_observer_.GetCount(ax::mojom::Event::kWindowActivated));
// Create a new browser window and wait for event again.
ui_test_utils::NavigateToURLWithDisposition(
browser(), GURL(url::kAboutBlankURL), WindowOpenDisposition::NEW_WINDOW,
ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
if (ax_observer_.GetCount(ax::mojom::Event::kWindowActivated) == 1)
ax_observer_.WaitForEvent(ax::mojom::Event::kWindowActivated);
ASSERT_EQ(2, ax_observer_.GetCount(ax::mojom::Event::kWindowActivated));
}
#endif
...@@ -16,11 +16,23 @@ AXEventCounter::~AXEventCounter() = default; ...@@ -16,11 +16,23 @@ AXEventCounter::~AXEventCounter() = default;
void AXEventCounter::OnViewEvent(views::View*, ax::mojom::Event event_type) { void AXEventCounter::OnViewEvent(views::View*, ax::mojom::Event event_type) {
++event_counts_[event_type]; ++event_counts_[event_type];
if (run_loop_ && event_type == wait_for_event_type_) {
wait_for_event_type_ = ax::mojom::Event::kNone;
run_loop_->Quit();
}
} }
int AXEventCounter::GetCount(ax::mojom::Event event_type) { int AXEventCounter::GetCount(ax::mojom::Event event_type) {
return event_counts_[event_type]; return event_counts_[event_type];
} }
void AXEventCounter::WaitForEvent(ax::mojom::Event event_type) {
wait_for_event_type_ = event_type;
base::RunLoop run_loop;
run_loop_ = &run_loop;
run_loop_->Run();
run_loop_ = nullptr;
}
} // namespace test } // namespace test
} // namespace views } // namespace views
...@@ -6,7 +6,9 @@ ...@@ -6,7 +6,9 @@
#define UI_VIEWS_TEST_AX_EVENT_COUNTER_H_ #define UI_VIEWS_TEST_AX_EVENT_COUNTER_H_
#include "base/containers/flat_map.h" #include "base/containers/flat_map.h"
#include "base/run_loop.h"
#include "base/scoped_observer.h" #include "base/scoped_observer.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/views/accessibility/ax_event_manager.h" #include "ui/views/accessibility/ax_event_manager.h"
#include "ui/views/accessibility/ax_event_observer.h" #include "ui/views/accessibility/ax_event_observer.h"
...@@ -14,7 +16,7 @@ namespace views { ...@@ -14,7 +16,7 @@ namespace views {
namespace test { namespace test {
// AXEventCounter provides a convenient way to count events registered by the // AXEventCounter provides a convenient way to count events registered by the
// AXEventManager by their event type. // AXEventManager by their event type, and wait for events of a specific type.
class AXEventCounter : public views::AXEventObserver { class AXEventCounter : public views::AXEventObserver {
public: public:
explicit AXEventCounter(views::AXEventManager* event_manager); explicit AXEventCounter(views::AXEventManager* event_manager);
...@@ -27,11 +29,16 @@ class AXEventCounter : public views::AXEventObserver { ...@@ -27,11 +29,16 @@ class AXEventCounter : public views::AXEventObserver {
// creation of this AXEventManager object. // creation of this AXEventManager object.
int GetCount(ax::mojom::Event event_type); int GetCount(ax::mojom::Event event_type);
// Blocks until an event of the specified type is received.
void WaitForEvent(ax::mojom::Event event_type);
// views::AXEventObserver // views::AXEventObserver
void OnViewEvent(views::View*, ax::mojom::Event event_type) override; void OnViewEvent(views::View*, ax::mojom::Event event_type) override;
private: private:
base::flat_map<ax::mojom::Event, int> event_counts_; base::flat_map<ax::mojom::Event, int> event_counts_;
ax::mojom::Event wait_for_event_type_ = ax::mojom::Event::kNone;
base::RunLoop* run_loop_ = nullptr;
ScopedObserver<views::AXEventManager, views::AXEventObserver> tree_observer_{ ScopedObserver<views::AXEventManager, views::AXEventObserver> tree_observer_{
this}; this};
}; };
......
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