Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(108)

Unified Diff: chrome/browser/chromeos/accessibility/select_to_speak_event_handler_unittest.cc

Issue 2493923002: Select-to-speak event handler (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/chromeos/accessibility/select_to_speak_event_handler_unittest.cc
diff --git a/chrome/browser/chromeos/accessibility/select_to_speak_event_handler_unittest.cc b/chrome/browser/chromeos/accessibility/select_to_speak_event_handler_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..76e4d0d2593ef894a23668cef765ddfa7c5f8925
--- /dev/null
+++ b/chrome/browser/chromeos/accessibility/select_to_speak_event_handler_unittest.cc
@@ -0,0 +1,211 @@
+// Copyright 2016 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/chromeos/accessibility/select_to_speak_event_handler.h"
+
+#include <set>
+
+#include "ash/shell.h"
+#include "ash/test/ash_test_base.h"
+#include "base/macros.h"
+#include "chrome/browser/ui/aura/accessibility/automation_manager_aura.h"
+#include "chrome/test/base/testing_profile.h"
+#include "ui/aura/window.h"
+#include "ui/events/event.h"
+#include "ui/events/event_constants.h"
+#include "ui/events/test/event_generator.h"
+#include "ui/views/test/test_views_delegate.h"
+#include "ui/views/views_delegate.h"
+
+using chromeos::SelectToSpeakEventHandler;
+
+namespace {
+
+// Records all key events.
+class EventCapturer : public ui::EventHandler {
+ public:
+ EventCapturer() {}
+ ~EventCapturer() override {}
+
+ void Reset() {
+ last_key_event_.reset(nullptr);
+ last_mouse_event_.reset(nullptr);
+ }
+
+ ui::KeyEvent* last_key_event() { return last_key_event_.get(); }
+ ui::MouseEvent* last_mouse_event() { return last_mouse_event_.get(); }
+
+ private:
+ void OnMouseEvent(ui::MouseEvent* event) override {
+ last_mouse_event_.reset(new ui::MouseEvent(*event));
+ }
+ void OnKeyEvent(ui::KeyEvent* event) override {
+ last_key_event_.reset(new ui::KeyEvent(*event));
+ }
+
+ std::unique_ptr<ui::KeyEvent> last_key_event_;
+ std::unique_ptr<ui::MouseEvent> last_mouse_event_;
+
+ DISALLOW_COPY_AND_ASSIGN(EventCapturer);
+};
+
+class SelectToSpeakTestViewsDelegate : public views::TestViewsDelegate {
+ public:
+ SelectToSpeakTestViewsDelegate() {}
+ ~SelectToSpeakTestViewsDelegate() override {}
+
+ void Reset() { events_captured_.clear(); }
+
+ bool CapturedAXEvent(ui::AXEvent event_type) {
+ return events_captured_.find(event_type) != events_captured_.end();
+ }
+
+ // Overriden from TestViewsDelegate.
+ void NotifyAccessibilityEvent(views::View* view,
+ ui::AXEvent event_type) override {
+ events_captured_.insert(event_type);
+ }
+
+ private:
+ std::set<ui::AXEvent> events_captured_;
+
+ DISALLOW_COPY_AND_ASSIGN(SelectToSpeakTestViewsDelegate);
+};
+
+class SelectToSpeakEventHandlerTest : public ash::test::AshTestBase {
+ public:
+ SelectToSpeakEventHandlerTest()
+ : generator_(nullptr),
+ select_to_speak_event_handler_(new SelectToSpeakEventHandler()) {}
+
+ void SetUp() override {
+ ash::test::AshTestBase::SetUp();
+ views_delegate_.reset(new SelectToSpeakTestViewsDelegate);
+ generator_ = &AshTestBase::GetEventGenerator();
+ CurrentContext()->AddPreTargetHandler(select_to_speak_event_handler_.get());
+ CurrentContext()->AddPreTargetHandler(&event_capturer_);
+ AutomationManagerAura::GetInstance()->Enable(&profile_);
+ }
+
+ void TearDown() override {
+ CurrentContext()->RemovePreTargetHandler(
+ select_to_speak_event_handler_.get());
+ CurrentContext()->RemovePreTargetHandler(&event_capturer_);
+ generator_ = nullptr;
+ ash::test::AshTestBase::TearDown();
+ }
+
+ protected:
+ ui::test::EventGenerator* generator_;
+ EventCapturer event_capturer_;
+ TestingProfile profile_;
+ std::unique_ptr<SelectToSpeakTestViewsDelegate> views_delegate_;
+
+ private:
+ std::unique_ptr<SelectToSpeakEventHandler> select_to_speak_event_handler_;
+
+ DISALLOW_COPY_AND_ASSIGN(SelectToSpeakEventHandlerTest);
+};
+
+} // namespace
+
+TEST_F(SelectToSpeakEventHandlerTest, PressAndReleaseSearchNotHandled) {
+ // If the user presses and releases the Search key, with no mouse
+ // presses, the key events won't be handled by the SelectToSpeakEventHandler
+ // and the normal behavior will occur.
+
+ EXPECT_FALSE(event_capturer_.last_key_event());
+
+ generator_->PressKey(ui::VKEY_LWIN, ui::EF_COMMAND_DOWN);
+ ASSERT_TRUE(event_capturer_.last_key_event());
+ EXPECT_FALSE(event_capturer_.last_key_event()->handled());
+
+ event_capturer_.Reset();
+ generator_->ReleaseKey(ui::VKEY_LWIN, ui::EF_COMMAND_DOWN);
+ ASSERT_TRUE(event_capturer_.last_key_event());
+ EXPECT_FALSE(event_capturer_.last_key_event()->handled());
+}
+
+TEST_F(SelectToSpeakEventHandlerTest, SearchPlusClick) {
+ // If the user holds the Search key and then clicks the mouse button,
+ // the mouse events and the key release event get hancled by the
+ // SelectToSpeakEventHandler, and accessibility events are generated.
+
+ generator_->PressKey(ui::VKEY_LWIN, ui::EF_COMMAND_DOWN);
+ ASSERT_TRUE(event_capturer_.last_key_event());
+ EXPECT_FALSE(event_capturer_.last_key_event()->handled());
+
+ generator_->set_current_location(gfx::Point(100, 12));
+ generator_->PressLeftButton();
+ EXPECT_FALSE(event_capturer_.last_mouse_event());
+
+ EXPECT_TRUE(views_delegate_->CapturedAXEvent(ui::AX_EVENT_MOUSE_PRESSED));
+
+ generator_->ReleaseLeftButton();
+ EXPECT_FALSE(event_capturer_.last_mouse_event());
+
+ EXPECT_TRUE(views_delegate_->CapturedAXEvent(ui::AX_EVENT_MOUSE_RELEASED));
+
+ event_capturer_.Reset();
+ generator_->ReleaseKey(ui::VKEY_LWIN, ui::EF_COMMAND_DOWN);
+ EXPECT_FALSE(event_capturer_.last_key_event());
+}
+
+TEST_F(SelectToSpeakEventHandlerTest, SearchPlusClickTwice) {
+ // Same as SearchPlusClick, above, but test that the user can keep
+ // holding down Search and click again.
+
+ generator_->PressKey(ui::VKEY_LWIN, ui::EF_COMMAND_DOWN);
+ ASSERT_TRUE(event_capturer_.last_key_event());
+ EXPECT_FALSE(event_capturer_.last_key_event()->handled());
+
+ generator_->set_current_location(gfx::Point(100, 12));
+ generator_->PressLeftButton();
+ EXPECT_FALSE(event_capturer_.last_mouse_event());
+ EXPECT_TRUE(views_delegate_->CapturedAXEvent(ui::AX_EVENT_MOUSE_PRESSED));
+
+ generator_->ReleaseLeftButton();
+ EXPECT_FALSE(event_capturer_.last_mouse_event());
+ EXPECT_TRUE(views_delegate_->CapturedAXEvent(ui::AX_EVENT_MOUSE_RELEASED));
+
+ views_delegate_->Reset();
+ EXPECT_FALSE(views_delegate_->CapturedAXEvent(ui::AX_EVENT_MOUSE_PRESSED));
+ EXPECT_FALSE(views_delegate_->CapturedAXEvent(ui::AX_EVENT_MOUSE_RELEASED));
+
+ generator_->PressLeftButton();
+ EXPECT_FALSE(event_capturer_.last_mouse_event());
+ EXPECT_TRUE(views_delegate_->CapturedAXEvent(ui::AX_EVENT_MOUSE_PRESSED));
+
+ generator_->ReleaseLeftButton();
+ EXPECT_FALSE(event_capturer_.last_mouse_event());
+ EXPECT_TRUE(views_delegate_->CapturedAXEvent(ui::AX_EVENT_MOUSE_RELEASED));
+
+ event_capturer_.Reset();
+ generator_->ReleaseKey(ui::VKEY_LWIN, ui::EF_COMMAND_DOWN);
+ EXPECT_FALSE(event_capturer_.last_key_event());
+}
+
+TEST_F(SelectToSpeakEventHandlerTest, SearchPlusMouseThenCancel) {
+ // If the user holds the Search key and then presses the mouse button,
+ // but then releases the Search key first while the mouse is still down,
+ // a cancel AX event is sent and the subsequent mouse up is canceled too.
+
+ generator_->PressKey(ui::VKEY_LWIN, ui::EF_COMMAND_DOWN);
+ ASSERT_TRUE(event_capturer_.last_key_event());
+ EXPECT_FALSE(event_capturer_.last_key_event()->handled());
+
+ generator_->set_current_location(gfx::Point(100, 12));
+ generator_->PressLeftButton();
+ EXPECT_FALSE(event_capturer_.last_mouse_event());
+ EXPECT_TRUE(views_delegate_->CapturedAXEvent(ui::AX_EVENT_MOUSE_PRESSED));
+
+ event_capturer_.Reset();
+ generator_->ReleaseKey(ui::VKEY_LWIN, ui::EF_COMMAND_DOWN);
+ EXPECT_FALSE(event_capturer_.last_key_event());
+
+ EXPECT_TRUE(views_delegate_->CapturedAXEvent(ui::AX_EVENT_MOUSE_CANCELED));
+
+ generator_->ReleaseLeftButton();
+ EXPECT_FALSE(event_capturer_.last_mouse_event());
+}

Powered by Google App Engine
This is Rietveld 408576698