diff options
| author | azurewei <azurewei@chromium.org> | 2015-12-08 18:38:50 -0800 |
|---|---|---|
| committer | Commit bot <commit-bot@chromium.org> | 2015-12-09 02:39:41 +0000 |
| commit | 845f44879d36391a7bff22822e080f78a6021a59 (patch) | |
| tree | 8e4d81cb791d267be7e98b6f62260b919f26d1a1 | |
| parent | e3d4152491d20a918ddf7ed0f7d25676d33ae395 (diff) | |
| download | chromium_src-845f44879d36391a7bff22822e080f78a6021a59.zip chromium_src-845f44879d36391a7bff22822e080f78a6021a59.tar.gz chromium_src-845f44879d36391a7bff22822e080f78a6021a59.tar.bz2 | |
Make EventGenerator to support input method.
1) EventGenerator makes the decision when to send the key-event through IME or should be handled locally.
2) Use EventGeneratorDelegate to send the key-event to IME.
BUG=512315
Test=Verified bug not repro on local build.
Review URL: https://codereview.chromium.org/1328093002
Cr-Commit-Position: refs/heads/master@{#363936}
| -rw-r--r-- | ash/test/ash_test_base.cc | 6 | ||||
| -rw-r--r-- | chrome/chrome_tests_unit.gypi | 11 | ||||
| -rw-r--r-- | chrome/test/BUILD.gn | 12 | ||||
| -rw-r--r-- | chrome/test/base/default_ash_event_generator_delegate.cc | 53 | ||||
| -rw-r--r-- | chrome/test/base/default_ash_event_generator_delegate.h | 44 | ||||
| -rw-r--r-- | chrome/test/base/in_process_browser_test.cc | 8 | ||||
| -rw-r--r-- | content/public/test/browser_test_base.cc | 7 | ||||
| -rw-r--r-- | ui/aura/test/event_generator_delegate_aura.cc | 11 | ||||
| -rw-r--r-- | ui/aura/test/event_generator_delegate_aura.h | 2 | ||||
| -rw-r--r-- | ui/events/test/event_generator.cc | 17 | ||||
| -rw-r--r-- | ui/events/test/event_generator.h | 6 | ||||
| -rw-r--r-- | ui/views/controls/textfield/textfield_unittest.cc | 28 | ||||
| -rw-r--r-- | ui/views/test/event_generator_delegate_mac.mm | 6 |
13 files changed, 177 insertions, 34 deletions
diff --git a/ash/test/ash_test_base.cc b/ash/test/ash_test_base.cc index 7b0d5a0..fa706e0 100644 --- a/ash/test/ash_test_base.cc +++ b/ash/test/ash_test_base.cc @@ -75,6 +75,12 @@ class AshEventGeneratorDelegate return aura::client::GetScreenPositionClient(window->GetRootWindow()); } + void DispatchKeyEventToIME(ui::EventTarget* target, + ui::KeyEvent* event) override { + // In Ash environment, the key event will be processed by event rewriters + // first. + } + private: DISALLOW_COPY_AND_ASSIGN(AshEventGeneratorDelegate); }; diff --git a/chrome/chrome_tests_unit.gypi b/chrome/chrome_tests_unit.gypi index 47ab796..2d440f4 100644 --- a/chrome/chrome_tests_unit.gypi +++ b/chrome/chrome_tests_unit.gypi @@ -2006,6 +2006,17 @@ 'test/base/test_browser_window_aura.h', ], }], + ['use_ash==1', { + 'sources': [ + 'test/base/default_ash_event_generator_delegate.cc', + 'test/base/default_ash_event_generator_delegate.h', + ], + 'dependencies': [ + '../ash/ash.gyp:ash', + '../ui/aura/aura.gyp:aura', + '../ui/aura/aura.gyp:aura_test_support', + ], + }], ['configuration_policy==1', { 'dependencies': [ '../components/components.gyp:policy_component_test_support', diff --git a/chrome/test/BUILD.gn b/chrome/test/BUILD.gn index 3670d55..d947f58 100644 --- a/chrome/test/BUILD.gn +++ b/chrome/test/BUILD.gn @@ -225,7 +225,17 @@ source_set("test_support") { } if (use_ash) { - public_deps += [ "//ash:test_support" ] + sources += [ + "base/default_ash_event_generator_delegate.cc", + "base/default_ash_event_generator_delegate.h", + ] + + public_deps += [ + "//ash", + "//ash:test_support", + "//ui/aura", + "//ui/aura:test_support", + ] } if (toolkit_views) { public_deps += [ "//ui/views:test_support" ] diff --git a/chrome/test/base/default_ash_event_generator_delegate.cc b/chrome/test/base/default_ash_event_generator_delegate.cc new file mode 100644 index 0000000..26a909c --- /dev/null +++ b/chrome/test/base/default_ash_event_generator_delegate.cc @@ -0,0 +1,53 @@ +// Copyright 2015 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/test/base/default_ash_event_generator_delegate.h" + +#include "chrome/browser/ui/host_desktop.h" + +DefaultAshEventGeneratorDelegate* +DefaultAshEventGeneratorDelegate::GetInstance() { + return base::Singleton<DefaultAshEventGeneratorDelegate>::get(); +} + +void DefaultAshEventGeneratorDelegate::SetContext( + ui::test::EventGenerator* owner, + gfx::NativeWindow root_window, + gfx::NativeWindow window) { + root_window_ = root_window; +} + +aura::WindowTreeHost* DefaultAshEventGeneratorDelegate::GetHostAt( + const gfx::Point& point) const { + return root_window_->GetHost(); +} + +aura::client::ScreenPositionClient* +DefaultAshEventGeneratorDelegate::GetScreenPositionClient( + const aura::Window* window) const { + return nullptr; +} + +void DefaultAshEventGeneratorDelegate::DispatchKeyEventToIME( + ui::EventTarget* target, + ui::KeyEvent* event) { + // In Ash environment, the key event will be processed by event rewriters + // first and event will not be hanlded. + // Otherwise, use EventGeneratorDelegateAura::DispatchKeyEventToIME() to + // dispatch |event| to the input method. + if (chrome::GetActiveDesktop() != chrome::HOST_DESKTOP_TYPE_ASH) { + EventGeneratorDelegateAura::DispatchKeyEventToIME(target, event); + } +} + +DefaultAshEventGeneratorDelegate::DefaultAshEventGeneratorDelegate() + : root_window_(nullptr) { + DCHECK(!ui::test::EventGenerator::default_delegate); + ui::test::EventGenerator::default_delegate = this; +} + +DefaultAshEventGeneratorDelegate::~DefaultAshEventGeneratorDelegate() { + DCHECK_EQ(this, ui::test::EventGenerator::default_delegate); + ui::test::EventGenerator::default_delegate = nullptr; +} diff --git a/chrome/test/base/default_ash_event_generator_delegate.h b/chrome/test/base/default_ash_event_generator_delegate.h new file mode 100644 index 0000000..54c543c --- /dev/null +++ b/chrome/test/base/default_ash_event_generator_delegate.h @@ -0,0 +1,44 @@ +// Copyright 2015 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_TEST_BASE_DEFAULT_ASH_EVENT_GENERATOR_DELEGATE_H_ +#define CHROME_TEST_BASE_DEFAULT_ASH_EVENT_GENERATOR_DELEGATE_H_ + +#include "ash/shell.h" +#include "base/macros.h" +#include "base/memory/singleton.h" +#include "ui/aura/test/event_generator_delegate_aura.h" +#include "ui/aura/window.h" + +class DefaultAshEventGeneratorDelegate + : public aura::test::EventGeneratorDelegateAura { + public: + static DefaultAshEventGeneratorDelegate* GetInstance(); + + // EventGeneratorDelegate: + void SetContext(ui::test::EventGenerator* owner, + gfx::NativeWindow root_window, + gfx::NativeWindow window) override; + + aura::WindowTreeHost* GetHostAt(const gfx::Point& point) const override; + + aura::client::ScreenPositionClient* GetScreenPositionClient( + const aura::Window* window) const override; + + void DispatchKeyEventToIME(ui::EventTarget* target, + ui::KeyEvent* event) override; + + private: + friend struct base::DefaultSingletonTraits<DefaultAshEventGeneratorDelegate>; + + DefaultAshEventGeneratorDelegate(); + + ~DefaultAshEventGeneratorDelegate() override; + + aura::Window* root_window_; + + DISALLOW_COPY_AND_ASSIGN(DefaultAshEventGeneratorDelegate); +}; + +#endif // CHROME_TEST_BASE_DEFAULT_ASH_EVENT_GENERATOR_DELEGATE_H_ diff --git a/chrome/test/base/in_process_browser_test.cc b/chrome/test/base/in_process_browser_test.cc index 14c145a..be1898c 100644 --- a/chrome/test/base/in_process_browser_test.cc +++ b/chrome/test/base/in_process_browser_test.cc @@ -83,6 +83,10 @@ #include "chrome/browser/chromeos/input_method/input_method_configuration.h" #endif +#if defined(USE_ASH) +#include "chrome/test/base/default_ash_event_generator_delegate.h" +#endif + namespace { // Passed as value of kTestType. @@ -196,6 +200,10 @@ InProcessBrowserTest::InProcessBrowserTest() #if defined(OS_MACOSX) bundle_swizzler_.reset(new ScopedBundleSwizzlerMac); #endif + +#if defined(USE_ASH) + DefaultAshEventGeneratorDelegate::GetInstance(); +#endif } InProcessBrowserTest::~InProcessBrowserTest() { diff --git a/content/public/test/browser_test_base.cc b/content/public/test/browser_test_base.cc index 669a85d..2868983 100644 --- a/content/public/test/browser_test_base.cc +++ b/content/public/test/browser_test_base.cc @@ -144,12 +144,9 @@ BrowserTestBase::BrowserTestBase() base::mac::SetOverrideAmIBundled(true); #endif -#if defined(USE_AURA) -#if defined(USE_X11) +#if defined(USE_AURA) && defined(USE_X11) aura::test::SetUseOverrideRedirectWindowByDefault(true); #endif - aura::test::InitializeAuraEventGeneratorDelegate(); -#endif #if defined(OS_POSIX) handle_sigterm_ = true; @@ -215,6 +212,8 @@ void BrowserTestBase::SetUp() { // us to, or it's requested on the command line. if (!enable_pixel_output_ && !use_software_compositing_) command_line->AppendSwitch(switches::kDisableGLDrawingForTests); + + aura::test::InitializeAuraEventGeneratorDelegate(); #endif bool use_osmesa = true; diff --git a/ui/aura/test/event_generator_delegate_aura.cc b/ui/aura/test/event_generator_delegate_aura.cc index f248cb6..9ee54cf 100644 --- a/ui/aura/test/event_generator_delegate_aura.cc +++ b/ui/aura/test/event_generator_delegate_aura.cc @@ -8,6 +8,7 @@ #include "ui/aura/client/screen_position_client.h" #include "ui/aura/window_event_dispatcher.h" #include "ui/aura/window_tree_host.h" +#include "ui/base/ime/input_method.h" namespace aura { namespace test { @@ -61,7 +62,9 @@ const Window* WindowFromTarget(const ui::EventTarget* event_target) { } // namespace void InitializeAuraEventGeneratorDelegate() { - DefaultEventGeneratorDelegate::GetInstance(); + if (!ui::test::EventGenerator::default_delegate) { + DefaultEventGeneratorDelegate::GetInstance(); + } } EventGeneratorDelegateAura::EventGeneratorDelegateAura() { @@ -126,5 +129,11 @@ void EventGeneratorDelegateAura::ConvertPointFromHost( window->GetHost()->ConvertPointFromHost(point); } +void EventGeneratorDelegateAura::DispatchKeyEventToIME(ui::EventTarget* target, + ui::KeyEvent* event) { + Window* window = static_cast<Window*>(target); + window->GetHost()->GetInputMethod()->DispatchKeyEvent(event); +} + } // namespace test } // namespace aura diff --git a/ui/aura/test/event_generator_delegate_aura.h b/ui/aura/test/event_generator_delegate_aura.h index 7cc7f2f..e235622 100644 --- a/ui/aura/test/event_generator_delegate_aura.h +++ b/ui/aura/test/event_generator_delegate_aura.h @@ -45,6 +45,8 @@ class EventGeneratorDelegateAura : public ui::test::EventGeneratorDelegate { gfx::Point* point) const override; void ConvertPointFromHost(const ui::EventTarget* hosted_target, gfx::Point* point) const override; + void DispatchKeyEventToIME(ui::EventTarget* target, + ui::KeyEvent* event) override; private: DISALLOW_COPY_AND_ASSIGN(EventGeneratorDelegateAura); diff --git a/ui/events/test/event_generator.cc b/ui/events/test/event_generator.cc index 08c8d18..021268b 100644 --- a/ui/events/test/event_generator.cc +++ b/ui/events/test/event_generator.cc @@ -665,11 +665,18 @@ void EventGenerator::DoDispatchEvent(ui::Event* event, bool async) { } pending_events_.push_back(pending_event); } else { - ui::EventSource* event_source = delegate()->GetEventSource(current_target_); - ui::EventSourceTestApi event_source_test(event_source); - ui::EventDispatchDetails details = - event_source_test.SendEventToProcessor(event); - CHECK(!details.dispatcher_destroyed); + if (event->IsKeyEvent()) { + delegate()->DispatchKeyEventToIME(current_target_, + static_cast<ui::KeyEvent*>(event)); + } + if (!event->handled()) { + ui::EventSource* event_source = + delegate()->GetEventSource(current_target_); + ui::EventSourceTestApi event_source_test(event_source); + ui::EventDispatchDetails details = + event_source_test.SendEventToProcessor(event); + CHECK(!details.dispatcher_destroyed); + } } } diff --git a/ui/events/test/event_generator.h b/ui/events/test/event_generator.h index 9a72695..9261cc8 100644 --- a/ui/events/test/event_generator.h +++ b/ui/events/test/event_generator.h @@ -74,6 +74,12 @@ class EventGeneratorDelegate { // |hosted_target| into the root window's coordinate system. virtual void ConvertPointFromHost(const EventTarget* hosted_target, gfx::Point* point) const = 0; + + // Detemines whether the input method should be the first to handle key events + // before dispathcing to Views. If it does, the given |event| will be + // dispatched and processed by the input method from the host of |target|. + virtual void DispatchKeyEventToIME(EventTarget* target, + ui::KeyEvent* event) = 0; }; // ui::test::EventGenerator is a tool that generates and dispatches events. diff --git a/ui/views/controls/textfield/textfield_unittest.cc b/ui/views/controls/textfield/textfield_unittest.cc index fecc190..0d0fd29 100644 --- a/ui/views/controls/textfield/textfield_unittest.cc +++ b/ui/views/controls/textfield/textfield_unittest.cc @@ -27,6 +27,7 @@ #include "ui/events/event_processor.h" #include "ui/events/event_utils.h" #include "ui/events/keycodes/keyboard_codes.h" +#include "ui/events/test/event_generator.h" #include "ui/gfx/render_text.h" #include "ui/strings/grit/ui_strings.h" #include "ui/views/controls/textfield/textfield_controller.h" @@ -51,10 +52,6 @@ #include "ui/events/event_utils.h" #endif -#if defined(OS_MACOSX) && !defined(USE_AURA) -#include "ui/events/test/event_generator.h" -#endif - using base::ASCIIToUTF16; using base::UTF8ToUTF16; using base::WideToUTF16; @@ -145,8 +142,10 @@ void MockInputMethod::DispatchKeyEvent(ui::KeyEvent* key) { // Checks whether the key event is from EventGenerator on Windows which will // generate key event for WM_CHAR. // The MockInputMethod will insert char on WM_KEYDOWN so ignore WM_CHAR here. - if (key->is_char() && key->HasNativeEvent()) + if (key->is_char() && key->HasNativeEvent()) { + key->SetHandled(); return; + } bool handled = !IsTextInputTypeNone() && HasComposition(); ClearStates(); @@ -436,13 +435,10 @@ class TextfieldTest : public ViewsTestBase, public TextfieldController { // don't want parallel tests to steal active status either, so fake it. #if defined(OS_MACOSX) && !defined(USE_AURA) fake_activation_ = test::WidgetTest::FakeWidgetIsActiveAlways(); - - // This is only used on Mac. See comment regarding |event_generator_| below. +#endif event_generator_.reset( new ui::test::EventGenerator(GetContext(), widget_->GetNativeWindow())); -#endif } - ui::MenuModel* GetContextMenuModel() { test_api_->UpdateContextMenu(); return test_api_->context_menu_contents(); @@ -467,17 +463,7 @@ class TextfieldTest : public ViewsTestBase, public TextfieldController { protected: void SendKeyPress(ui::KeyboardCode key_code, int flags) { -#if defined(OS_MACOSX) && !defined(USE_AURA) - // The Mac EventGenerator hooks in before IME. It sends events first to an - // NSResponder, which is necessary to interpret keyboard events into - // appropriate editing commands. event_generator_->PressKey(key_code, flags); -#else - // TODO(shuchen): making EventGenerator support input method and using - // EventGenerator here. crbug.com/512315. - ui::KeyEvent event(ui::ET_KEY_PRESSED, key_code, flags); - input_method_->DispatchKeyEvent(&event); -#endif } void SendKeyEvent(ui::KeyboardCode key_code, @@ -669,11 +655,7 @@ class TextfieldTest : public ViewsTestBase, public TextfieldController { private: ui::ClipboardType copied_to_clipboard_; scoped_ptr<test::WidgetTest::FakeActivation> fake_activation_; - -#if defined(OS_MACOSX) && !defined(USE_AURA) scoped_ptr<ui::test::EventGenerator> event_generator_; -#endif - DISALLOW_COPY_AND_ASSIGN(TextfieldTest); }; diff --git a/ui/views/test/event_generator_delegate_mac.mm b/ui/views/test/event_generator_delegate_mac.mm index 64b93d7..74c3edc 100644 --- a/ui/views/test/event_generator_delegate_mac.mm +++ b/ui/views/test/event_generator_delegate_mac.mm @@ -284,6 +284,12 @@ class EventGeneratorDelegateMac : public ui::EventTarget, gfx::Point* point) const override {} void ConvertPointFromHost(const ui::EventTarget* hosted_target, gfx::Point* point) const override {} + void DispatchKeyEventToIME(EventTarget* target, + ui::KeyEvent* event) override { + // InputMethodMac does not send native events nor do the necessary + // translation. Key events must be handled natively by an NSResponder which + // translates keyboard events into editing commands. + } private: friend struct base::DefaultSingletonTraits<EventGeneratorDelegateMac>; |
