Chromium Code Reviews| Index: ui/aura_shell/shell_accelerator_controller_unittest.cc |
| diff --git a/ui/aura_shell/shell_accelerator_controller_unittest.cc b/ui/aura_shell/shell_accelerator_controller_unittest.cc |
| index 39e1c6486045eaf07d07bb42a8dc6f985242abfb..f6c3a6d42b1c4d7c8795fd5346f116a31c90a64d 100644 |
| --- a/ui/aura_shell/shell_accelerator_controller_unittest.cc |
| +++ b/ui/aura_shell/shell_accelerator_controller_unittest.cc |
| @@ -2,36 +2,51 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| +#include "ui/aura/root_window.h" |
| +#include "ui/aura/test/test_window_delegate.h" |
| +#include "ui/aura/test/test_windows.h" |
| #include "ui/aura_shell/shell.h" |
| #include "ui/aura_shell/shell_accelerator_controller.h" |
| +#include "ui/aura_shell/shell_accelerator_filter.h" |
| +#include "ui/aura_shell/shell_window_ids.h" |
| #include "ui/aura_shell/test/aura_shell_test_base.h" |
| +#if defined(USE_X11) |
| +#include <X11/Xlib.h> |
| +#include "base/memory/scoped_ptr.h" |
| +#include "ui/base/keycodes/keyboard_code_conversion_x.h" |
| +#endif |
| + |
| namespace aura_shell { |
| namespace test { |
| namespace { |
| class TestTarget : public ui::AcceleratorTarget { |
| public: |
| - TestTarget() : accelerator_pressed_(false) {}; |
| + TestTarget() : accelerator_pressed_count_(0) {}; |
| virtual ~TestTarget() {}; |
| bool accelerator_pressed() const { |
|
Daniel Erat
2011/12/07 16:58:07
this method seems unnecessary now -- can't callers
mazda
2011/12/08 05:39:24
Replaced accelerator_pressed with accelerator_pres
|
| - return accelerator_pressed_; |
| + return accelerator_pressed_count_ > 0; |
| } |
| - void set_accelerator_pressed(bool accelerator_pressed) { |
| - accelerator_pressed_ = accelerator_pressed; |
| + int accelerator_pressed_count() const { |
| + return accelerator_pressed_count_; |
| + } |
| + |
| + void set_accelerator_pressed_count(int accelerator_pressed_count) { |
| + accelerator_pressed_count_ = accelerator_pressed_count; |
| } |
| // Overridden from ui::AcceleratorTarget: |
| virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE; |
| private: |
| - bool accelerator_pressed_; |
| + int accelerator_pressed_count_; |
| }; |
| bool TestTarget::AcceleratorPressed(const ui::Accelerator& accelerator) { |
| - set_accelerator_pressed(true); |
| + ++accelerator_pressed_count_; |
| return true; |
| } |
| @@ -44,22 +59,39 @@ class ShellAcceleratorControllerTest : public AuraShellTestBase { |
| static ShellAcceleratorController* GetController(); |
| - // testing::Test: |
| - // virtual void SetUp() OVERRIDE; |
| - // virtual void TearDown() OVERRIDE; |
| + private: |
| + // Overridden from testing::Test: |
| + virtual void SetUp() OVERRIDE; |
| + virtual void TearDown() OVERRIDE; |
| + |
| + scoped_ptr<aura_shell::internal::ShellAcceleratorFilter> accelerator_filter_; |
| }; |
| -ShellAcceleratorController* ShellAcceleratorControllerTest::GetController() { |
| - return Shell::GetInstance()->accelerator_controller(); |
| +void ShellAcceleratorControllerTest::SetUp() { |
| + AuraShellTestBase::SetUp(); |
| + aura_shell::Shell::GetInstance()->AddRootWindowEventFilter( |
| + accelerator_filter_.get()); |
| + // A focused window must exist for accelerators to be processed. |
| + aura::Window* default_container = |
| + aura_shell::Shell::GetInstance()->GetContainer( |
| + internal::kShellWindowId_DefaultContainer); |
| + aura::Window* window = aura::test::CreateTestWindowWithDelegate( |
| + new aura::test::TestWindowDelegate, |
| + -1, |
| + gfx::Rect(), |
| + default_container); |
| + window->Activate(); |
| } |
| -// void ShellAcceleratorControllerTest::SetUp() { |
| -// AuraShellTestBase::SetUp(); |
| -// } |
| +void ShellAcceleratorControllerTest::TearDown() { |
| + aura_shell::Shell::GetInstance()->RemoveRootWindowEventFilter( |
| + accelerator_filter_.get()); |
| + AuraShellTestBase::TearDown(); |
| +} |
| -// void ShellAcceleratorControllerTest::TearDown() { |
| -// AuraShellTestBase::TearDown(); |
| -// } |
| +ShellAcceleratorController* ShellAcceleratorControllerTest::GetController() { |
| + return Shell::GetInstance()->accelerator_controller(); |
| +} |
| TEST_F(ShellAcceleratorControllerTest, Register) { |
| const ui::Accelerator accelerator_a(ui::VKEY_A, false, false, false); |
| @@ -99,7 +131,7 @@ TEST_F(ShellAcceleratorControllerTest, Unregister) { |
| EXPECT_TRUE(target.accelerator_pressed()); |
| // The unregistered accelerator is no longer processed. |
| - target.set_accelerator_pressed(false); |
| + target.set_accelerator_pressed_count(0); |
| GetController()->Unregister(accelerator_a, &target); |
| EXPECT_FALSE(GetController()->Process(accelerator_a)); |
| EXPECT_FALSE(target.accelerator_pressed()); |
| @@ -140,6 +172,29 @@ TEST_F(ShellAcceleratorControllerTest, Process) { |
| EXPECT_FALSE(GetController()->Process(accelerator_b)); |
| } |
| +#if defined(OS_WIN) || defined(USE_X11) |
| +TEST_F(ShellAcceleratorControllerTest, ProcessOnce) { |
| + const ui::Accelerator accelerator_a(ui::VKEY_A, false, false, false); |
| + TestTarget target1; |
|
Daniel Erat
2011/12/07 16:58:07
nit: naming it 'target1' in tests that only have a
mazda
2011/12/08 05:39:24
Done.
|
| + GetController()->Register(accelerator_a, &target1); |
| + |
| + // The accelerator is processed only once. |
| +#if defined(OS_WIN) |
| + MSG msg1 = { NULL, WM_KEYDOWN, VKEY_A, 0 }; |
| + EXPECT_TRUE(aura::RootWindow::GetInstance()->GetDispatcher()->Dispatch(msg1)); |
| + MSG msg2 = { NULL, WM_CHAR, L'A', 0 }; |
| + EXPECT_TRUE(aura::RootWindow::GetInstance()->GetDispatcher()->Dispatch(msg2)); |
| +#elif defined(USE_X11) |
| + scoped_ptr<XEvent> key_event(ui::CreateXEventForTesting(ui::ET_KEY_PRESSED, |
| + ui::VKEY_A, |
| + 0)); |
| + EXPECT_TRUE(aura::RootWindow::GetInstance()->GetDispatcher()->Dispatch( |
| + key_event.get())); |
| +#endif |
| + EXPECT_EQ(1, target1.accelerator_pressed_count()); |
| +} |
| +#endif |
| + |
| TEST_F(ShellAcceleratorControllerTest, GlobalAccelerators) { |
| // TODO(mazda): Uncomment the followings once they are implemented. |
| // CycleBackward |