| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ash/accelerators/accelerator_controller.h" |
| 5 #include "ash/shell.h" | 6 #include "ash/shell.h" |
| 6 #include "ash/shell_window_ids.h" | 7 #include "ash/shell_window_ids.h" |
| 7 #include "ash/test/ash_test_base.h" | 8 #include "ash/test/ash_test_base.h" |
| 8 #include "base/bind.h" | 9 #include "base/bind.h" |
| 9 #include "base/event_types.h" | 10 #include "base/event_types.h" |
| 10 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 11 #include "ui/aura/client/dispatcher_client.h" | 12 #include "ui/aura/client/dispatcher_client.h" |
| 12 #include "ui/aura/root_window.h" | 13 #include "ui/aura/root_window.h" |
| 13 #include "ui/aura/test/test_windows.h" | 14 #include "ui/aura/test/test_windows.h" |
| 14 #include "ui/aura/window.h" | 15 #include "ui/aura/window.h" |
| 16 #include "ui/base/accelerators/accelerator.h" |
| 15 | 17 |
| 16 #if defined(USE_X11) | 18 #if defined(USE_X11) |
| 17 #include <X11/Xlib.h> | 19 #include <X11/Xlib.h> |
| 18 #include "ui/base/x/x11_util.h" | 20 #include "ui/base/x/x11_util.h" |
| 19 #endif // USE_X11 | 21 #endif // USE_X11 |
| 20 | 22 |
| 21 namespace ash { | 23 namespace ash { |
| 22 namespace test { | 24 namespace test { |
| 23 | 25 |
| 24 namespace { | 26 namespace { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 43 num_key_events_dispatched_++; | 45 num_key_events_dispatched_++; |
| 44 return ui::IsNoopEvent(xev) ? MessagePumpDispatcher::EVENT_QUIT : | 46 return ui::IsNoopEvent(xev) ? MessagePumpDispatcher::EVENT_QUIT : |
| 45 MessagePumpDispatcher::EVENT_IGNORED; | 47 MessagePumpDispatcher::EVENT_IGNORED; |
| 46 } | 48 } |
| 47 #endif | 49 #endif |
| 48 | 50 |
| 49 private: | 51 private: |
| 50 int num_key_events_dispatched_; | 52 int num_key_events_dispatched_; |
| 51 }; | 53 }; |
| 52 | 54 |
| 53 void DispatchKeyEvent() { | 55 class TestTarget : public ui::AcceleratorTarget { |
| 56 public: |
| 57 TestTarget() : accelerator_pressed_count_(0) { |
| 58 } |
| 59 virtual ~TestTarget() { |
| 60 } |
| 61 |
| 62 int accelerator_pressed_count() const { |
| 63 return accelerator_pressed_count_; |
| 64 } |
| 65 |
| 66 // Overridden from ui::AcceleratorTarget: |
| 67 virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE { |
| 68 accelerator_pressed_count_++; |
| 69 return true; |
| 70 } |
| 71 virtual bool CanHandleAccelerators() const OVERRIDE { |
| 72 return true; |
| 73 } |
| 74 |
| 75 private: |
| 76 int accelerator_pressed_count_; |
| 77 |
| 78 DISALLOW_COPY_AND_ASSIGN(TestTarget); |
| 79 }; |
| 80 |
| 81 void DispatchKeyReleaseA() { |
| 54 #if defined(OS_WIN) | 82 #if defined(OS_WIN) |
| 55 MSG native_event = { NULL, WM_KEYUP, ui::VKEY_A, 0 }; | 83 MSG native_event = { NULL, WM_KEYUP, ui::VKEY_A, 0 }; |
| 56 ash::Shell::GetRootWindow()->PostNativeEvent(native_event); | 84 ash::Shell::GetRootWindow()->PostNativeEvent(native_event); |
| 57 #elif defined(USE_X11) | 85 #elif defined(USE_X11) |
| 58 XEvent native_event; | 86 XEvent native_event; |
| 59 ui::InitXKeyEventForTesting(ui::ET_KEY_RELEASED, | 87 ui::InitXKeyEventForTesting(ui::ET_KEY_RELEASED, |
| 60 ui::VKEY_A, | 88 ui::VKEY_A, |
| 61 0, | 89 0, |
| 62 &native_event); | 90 &native_event); |
| 63 ash::Shell::GetRootWindow()->PostNativeEvent(&native_event); | 91 ash::Shell::GetRootWindow()->PostNativeEvent(&native_event); |
| 64 #endif | 92 #endif |
| 65 | 93 |
| 66 // Send noop event to signal dispatcher to exit. | 94 // Send noop event to signal dispatcher to exit. |
| 67 ash::Shell::GetRootWindow()->PostNativeEvent(ui::CreateNoopEvent()); | 95 ash::Shell::GetRootWindow()->PostNativeEvent(ui::CreateNoopEvent()); |
| 68 } | 96 } |
| 69 | 97 |
| 70 } // namespace | 98 } // namespace |
| 71 | 99 |
| 72 typedef AshTestBase NestedDispatcherTest; | 100 typedef AshTestBase NestedDispatcherTest; |
| 73 | 101 |
| 74 // Aura window below lock screen in z order. | 102 // Aura window below lock screen in z order. |
| 75 TEST_F(NestedDispatcherTest, AssociatedWindowBelowLockScreen) { | 103 TEST_F(NestedDispatcherTest, AssociatedWindowBelowLockScreen) { |
| 76 MockDispatcher inner_dispatcher; | 104 MockDispatcher inner_dispatcher; |
| 77 aura::Window* default_container = Shell::GetInstance()->GetContainer( | 105 aura::Window* default_container = Shell::GetInstance()->GetContainer( |
| 78 ash::internal::kShellWindowId_DefaultContainer); | 106 internal::kShellWindowId_DefaultContainer); |
| 79 scoped_ptr<aura::Window>associated_window(aura::test::CreateTestWindowWithId( | 107 scoped_ptr<aura::Window>associated_window(aura::test::CreateTestWindowWithId( |
| 80 0, default_container)); | 108 0, default_container)); |
| 81 scoped_ptr<aura::Window>mock_lock_container( | 109 scoped_ptr<aura::Window>mock_lock_container( |
| 82 aura::test::CreateTestWindowWithId(0, default_container)); | 110 aura::test::CreateTestWindowWithId(0, default_container)); |
| 83 mock_lock_container->set_stops_event_propagation(true); | 111 mock_lock_container->set_stops_event_propagation(true); |
| 84 aura::test::CreateTestWindowWithId(0, mock_lock_container.get()); | 112 aura::test::CreateTestWindowWithId(0, mock_lock_container.get()); |
| 85 EXPECT_TRUE(aura::test::WindowIsAbove(mock_lock_container.get(), | 113 EXPECT_TRUE(aura::test::WindowIsAbove(mock_lock_container.get(), |
| 86 associated_window.get())); | 114 associated_window.get())); |
| 87 MessageLoop::current()->PostDelayedTask( | 115 DispatchKeyReleaseA(); |
| 88 FROM_HERE, | |
| 89 base::Bind(&DispatchKeyEvent), | |
| 90 base::TimeDelta::FromMilliseconds(100)); | |
| 91 aura::RootWindow* root_window = ash::Shell::GetInstance()->GetRootWindow(); | 116 aura::RootWindow* root_window = ash::Shell::GetInstance()->GetRootWindow(); |
| 92 aura::client::GetDispatcherClient(root_window)->RunWithDispatcher( | 117 aura::client::GetDispatcherClient(root_window)->RunWithDispatcher( |
| 93 &inner_dispatcher, | 118 &inner_dispatcher, |
| 94 associated_window.get(), | 119 associated_window.get(), |
| 95 true /* nestable_tasks_allowed */); | 120 true /* nestable_tasks_allowed */); |
| 96 EXPECT_EQ(0, inner_dispatcher.num_key_events_dispatched()); | 121 EXPECT_EQ(0, inner_dispatcher.num_key_events_dispatched()); |
| 97 } | 122 } |
| 98 | 123 |
| 99 // Aura window above lock screen in z order. | 124 // Aura window above lock screen in z order. |
| 100 TEST_F(NestedDispatcherTest, AssociatedWindowAboveLockScreen) { | 125 TEST_F(NestedDispatcherTest, AssociatedWindowAboveLockScreen) { |
| 101 MockDispatcher inner_dispatcher; | 126 MockDispatcher inner_dispatcher; |
| 102 | 127 |
| 103 aura::Window* default_container = Shell::GetInstance()->GetContainer( | 128 aura::Window* default_container = Shell::GetInstance()->GetContainer( |
| 104 ash::internal::kShellWindowId_DefaultContainer); | 129 internal::kShellWindowId_DefaultContainer); |
| 105 scoped_ptr<aura::Window>mock_lock_container( | 130 scoped_ptr<aura::Window>mock_lock_container( |
| 106 aura::test::CreateTestWindowWithId(0, default_container)); | 131 aura::test::CreateTestWindowWithId(0, default_container)); |
| 107 mock_lock_container->set_stops_event_propagation(true); | 132 mock_lock_container->set_stops_event_propagation(true); |
| 108 aura::test::CreateTestWindowWithId(0, mock_lock_container.get()); | 133 aura::test::CreateTestWindowWithId(0, mock_lock_container.get()); |
| 109 scoped_ptr<aura::Window>associated_window(aura::test::CreateTestWindowWithId( | 134 scoped_ptr<aura::Window>associated_window(aura::test::CreateTestWindowWithId( |
| 110 0, default_container)); | 135 0, default_container)); |
| 111 EXPECT_TRUE(aura::test::WindowIsAbove(associated_window.get(), | 136 EXPECT_TRUE(aura::test::WindowIsAbove(associated_window.get(), |
| 112 mock_lock_container.get())); | 137 mock_lock_container.get())); |
| 113 | 138 |
| 114 MessageLoop::current()->PostDelayedTask( | 139 DispatchKeyReleaseA(); |
| 115 FROM_HERE, | |
| 116 base::Bind(&DispatchKeyEvent), | |
| 117 base::TimeDelta::FromMilliseconds(100)); | |
| 118 aura::RootWindow* root_window = ash::Shell::GetInstance()->GetRootWindow(); | 140 aura::RootWindow* root_window = ash::Shell::GetInstance()->GetRootWindow(); |
| 119 aura::client::GetDispatcherClient(root_window)->RunWithDispatcher( | 141 aura::client::GetDispatcherClient(root_window)->RunWithDispatcher( |
| 120 &inner_dispatcher, | 142 &inner_dispatcher, |
| 121 associated_window.get(), | 143 associated_window.get(), |
| 122 true /* nestable_tasks_allowed */); | 144 true /* nestable_tasks_allowed */); |
| 123 EXPECT_EQ(1, inner_dispatcher.num_key_events_dispatched()); | 145 EXPECT_EQ(1, inner_dispatcher.num_key_events_dispatched()); |
| 124 } | 146 } |
| 125 | 147 |
| 148 // Test that the nested dispatcher handles accelerators. |
| 149 TEST_F(NestedDispatcherTest, AcceleratorsHandled) { |
| 150 MockDispatcher inner_dispatcher; |
| 151 aura::RootWindow* root_window = ash::Shell::GetInstance()->GetRootWindow(); |
| 152 |
| 153 ui::Accelerator accelerator(ui::VKEY_A, false, false, false); |
| 154 accelerator.set_type(ui::ET_TRANSLATED_KEY_RELEASE); |
| 155 TestTarget target; |
| 156 Shell::GetInstance()->accelerator_controller()->Register(accelerator, |
| 157 &target); |
| 158 |
| 159 DispatchKeyReleaseA(); |
| 160 aura::client::GetDispatcherClient(root_window)->RunWithDispatcher( |
| 161 &inner_dispatcher, |
| 162 root_window, |
| 163 true /* nestable_tasks_allowed */); |
| 164 EXPECT_EQ(0, inner_dispatcher.num_key_events_dispatched()); |
| 165 EXPECT_EQ(1, target.accelerator_pressed_count()); |
| 166 } |
| 167 |
| 126 } // namespace test | 168 } // namespace test |
| 127 } // namespace ash | 169 } // namespace ash |
| OLD | NEW |