OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ash/accelerators/accelerator_controller.h" |
| 6 #include "ash/session/session_state_delegate.h" |
| 7 #include "ash/shell.h" |
| 8 #include "ash/shell_window_ids.h" |
| 9 #include "ash/test/ash_test_base.h" |
| 10 #include "base/bind.h" |
| 11 #include "base/event_types.h" |
| 12 #include "base/message_loop/message_loop.h" |
| 13 #include "ui/aura/test/test_windows.h" |
| 14 #include "ui/aura/window.h" |
| 15 #include "ui/aura/window_event_dispatcher.h" |
| 16 #include "ui/base/accelerators/accelerator.h" |
| 17 #include "ui/events/event_constants.h" |
| 18 #include "ui/events/event_utils.h" |
| 19 #include "ui/events/platform/platform_event_dispatcher.h" |
| 20 #include "ui/events/platform/platform_event_source.h" |
| 21 #include "ui/events/platform/scoped_event_dispatcher.h" |
| 22 #include "ui/wm/public/dispatcher_client.h" |
| 23 |
| 24 #if defined(USE_X11) |
| 25 #include <X11/Xlib.h> |
| 26 #include "ui/events/test/events_test_utils_x11.h" |
| 27 #endif // USE_X11 |
| 28 |
| 29 namespace ash { |
| 30 namespace test { |
| 31 |
| 32 namespace { |
| 33 |
| 34 class MockDispatcher : public ui::PlatformEventDispatcher { |
| 35 public: |
| 36 MockDispatcher() : num_key_events_dispatched_(0) { |
| 37 } |
| 38 |
| 39 int num_key_events_dispatched() { return num_key_events_dispatched_; } |
| 40 |
| 41 private: |
| 42 // ui::PlatformEventDispatcher: |
| 43 virtual bool CanDispatchEvent(const ui::PlatformEvent& event) OVERRIDE { |
| 44 return true; |
| 45 } |
| 46 virtual uint32_t DispatchEvent(const ui::PlatformEvent& event) OVERRIDE { |
| 47 if (ui::EventTypeFromNative(event) == ui::ET_KEY_RELEASED) |
| 48 num_key_events_dispatched_++; |
| 49 return ui::POST_DISPATCH_NONE; |
| 50 } |
| 51 |
| 52 int num_key_events_dispatched_; |
| 53 |
| 54 DISALLOW_COPY_AND_ASSIGN(MockDispatcher); |
| 55 }; |
| 56 |
| 57 class TestTarget : public ui::AcceleratorTarget { |
| 58 public: |
| 59 TestTarget() : accelerator_pressed_count_(0) { |
| 60 } |
| 61 virtual ~TestTarget() { |
| 62 } |
| 63 |
| 64 int accelerator_pressed_count() const { |
| 65 return accelerator_pressed_count_; |
| 66 } |
| 67 |
| 68 // Overridden from ui::AcceleratorTarget: |
| 69 virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE { |
| 70 accelerator_pressed_count_++; |
| 71 return true; |
| 72 } |
| 73 virtual bool CanHandleAccelerators() const OVERRIDE { |
| 74 return true; |
| 75 } |
| 76 |
| 77 private: |
| 78 int accelerator_pressed_count_; |
| 79 |
| 80 DISALLOW_COPY_AND_ASSIGN(TestTarget); |
| 81 }; |
| 82 |
| 83 void DispatchKeyReleaseA() { |
| 84 // Sending both keydown and keyup is necessary here because the accelerator |
| 85 // manager only checks a keyup event following a keydown event. See |
| 86 // ShouldHandle() in ui/base/accelerators/accelerator_manager.cc for details. |
| 87 #if defined(OS_WIN) |
| 88 MSG native_event_down = { NULL, WM_KEYDOWN, ui::VKEY_A, 0 }; |
| 89 ash::Shell::GetPrimaryRootWindow()->host()->PostNativeEvent( |
| 90 native_event_down); |
| 91 MSG native_event_up = { NULL, WM_KEYUP, ui::VKEY_A, 0 }; |
| 92 ash::Shell::GetPrimaryRootWindow()->host()->PostNativeEvent(native_event_up); |
| 93 #elif defined(USE_X11) |
| 94 ui::ScopedXI2Event native_event; |
| 95 native_event.InitKeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_A, 0); |
| 96 aura::WindowTreeHost* host = ash::Shell::GetPrimaryRootWindow()->GetHost(); |
| 97 host->PostNativeEvent(native_event); |
| 98 native_event.InitKeyEvent(ui::ET_KEY_RELEASED, ui::VKEY_A, 0); |
| 99 host->PostNativeEvent(native_event); |
| 100 #endif |
| 101 // Make sure the inner message-loop terminates after dispatching the events. |
| 102 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 103 base::MessageLoop::current()->QuitClosure()); |
| 104 } |
| 105 |
| 106 } // namespace |
| 107 |
| 108 typedef AshTestBase NestedDispatcherTest; |
| 109 |
| 110 // Aura window above lock screen in z order. |
| 111 TEST_F(NestedDispatcherTest, AssociatedWindowAboveLockScreen) { |
| 112 MockDispatcher inner_dispatcher; |
| 113 |
| 114 scoped_ptr<aura::Window> mock_lock_container( |
| 115 CreateTestWindowInShellWithId(0)); |
| 116 aura::test::CreateTestWindowWithId(1, mock_lock_container.get()); |
| 117 scoped_ptr<aura::Window> associated_window(CreateTestWindowInShellWithId(2)); |
| 118 EXPECT_TRUE(aura::test::WindowIsAbove(associated_window.get(), |
| 119 mock_lock_container.get())); |
| 120 |
| 121 DispatchKeyReleaseA(); |
| 122 aura::Window* root_window = ash::Shell::GetPrimaryRootWindow(); |
| 123 scoped_ptr<ui::ScopedEventDispatcher> override_dispatcher = |
| 124 ui::PlatformEventSource::GetInstance()->OverrideDispatcher( |
| 125 &inner_dispatcher); |
| 126 aura::client::GetDispatcherClient(root_window)->RunWithDispatcher(NULL); |
| 127 EXPECT_EQ(1, inner_dispatcher.num_key_events_dispatched()); |
| 128 } |
| 129 |
| 130 // Test that the nested dispatcher handles accelerators. |
| 131 TEST_F(NestedDispatcherTest, AcceleratorsHandled) { |
| 132 MockDispatcher inner_dispatcher; |
| 133 aura::Window* root_window = ash::Shell::GetPrimaryRootWindow(); |
| 134 |
| 135 ui::Accelerator accelerator(ui::VKEY_A, ui::EF_NONE); |
| 136 accelerator.set_type(ui::ET_KEY_RELEASED); |
| 137 TestTarget target; |
| 138 Shell::GetInstance()->accelerator_controller()->Register(accelerator, |
| 139 &target); |
| 140 |
| 141 DispatchKeyReleaseA(); |
| 142 scoped_ptr<ui::ScopedEventDispatcher> override_dispatcher = |
| 143 ui::PlatformEventSource::GetInstance()->OverrideDispatcher( |
| 144 &inner_dispatcher); |
| 145 aura::client::GetDispatcherClient(root_window)->RunWithDispatcher(NULL); |
| 146 EXPECT_EQ(0, inner_dispatcher.num_key_events_dispatched()); |
| 147 EXPECT_EQ(1, target.accelerator_pressed_count()); |
| 148 } |
| 149 |
| 150 } // namespace test |
| 151 } // namespace ash |
OLD | NEW |