OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "ui/aura/event.h" |
| 6 #include "ui/aura/root_window.h" |
| 7 #include "ui/aura/test/test_window_delegate.h" |
| 8 #include "ui/aura/test/test_windows.h" |
5 #include "ui/aura_shell/shell.h" | 9 #include "ui/aura_shell/shell.h" |
6 #include "ui/aura_shell/shell_accelerator_controller.h" | 10 #include "ui/aura_shell/shell_accelerator_controller.h" |
| 11 #include "ui/aura_shell/shell_window_ids.h" |
7 #include "ui/aura_shell/test/aura_shell_test_base.h" | 12 #include "ui/aura_shell/test/aura_shell_test_base.h" |
8 | 13 |
| 14 #if defined(USE_X11) |
| 15 #include <X11/Xlib.h> |
| 16 #include "ui/base/x/x11_util.h" |
| 17 #endif |
| 18 |
9 namespace aura_shell { | 19 namespace aura_shell { |
10 namespace test { | 20 namespace test { |
11 | 21 |
12 namespace { | 22 namespace { |
13 class TestTarget : public ui::AcceleratorTarget { | 23 class TestTarget : public ui::AcceleratorTarget { |
14 public: | 24 public: |
15 TestTarget() : accelerator_pressed_(false) {}; | 25 TestTarget() : accelerator_pressed_count_(0) {}; |
16 virtual ~TestTarget() {}; | 26 virtual ~TestTarget() {}; |
17 | 27 |
18 bool accelerator_pressed() const { | 28 int accelerator_pressed_count() const { |
19 return accelerator_pressed_; | 29 return accelerator_pressed_count_; |
20 } | 30 } |
21 | 31 |
22 void set_accelerator_pressed(bool accelerator_pressed) { | 32 void set_accelerator_pressed_count(int accelerator_pressed_count) { |
23 accelerator_pressed_ = accelerator_pressed; | 33 accelerator_pressed_count_ = accelerator_pressed_count; |
24 } | 34 } |
25 | 35 |
26 // Overridden from ui::AcceleratorTarget: | 36 // Overridden from ui::AcceleratorTarget: |
27 virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE; | 37 virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE; |
28 | 38 |
29 private: | 39 private: |
30 bool accelerator_pressed_; | 40 int accelerator_pressed_count_; |
31 }; | 41 }; |
32 | 42 |
33 bool TestTarget::AcceleratorPressed(const ui::Accelerator& accelerator) { | 43 bool TestTarget::AcceleratorPressed(const ui::Accelerator& accelerator) { |
34 set_accelerator_pressed(true); | 44 ++accelerator_pressed_count_; |
35 return true; | 45 return true; |
36 } | 46 } |
37 | 47 |
38 } // namespace | 48 } // namespace |
39 | 49 |
40 class ShellAcceleratorControllerTest : public AuraShellTestBase { | 50 class ShellAcceleratorControllerTest : public AuraShellTestBase { |
41 public: | 51 public: |
42 ShellAcceleratorControllerTest() {}; | 52 ShellAcceleratorControllerTest() {}; |
43 virtual ~ShellAcceleratorControllerTest() {}; | 53 virtual ~ShellAcceleratorControllerTest() {}; |
44 | 54 |
45 static ShellAcceleratorController* GetController(); | 55 static ShellAcceleratorController* GetController(); |
46 | |
47 // testing::Test: | |
48 // virtual void SetUp() OVERRIDE; | |
49 // virtual void TearDown() OVERRIDE; | |
50 }; | 56 }; |
51 | 57 |
52 ShellAcceleratorController* ShellAcceleratorControllerTest::GetController() { | 58 ShellAcceleratorController* ShellAcceleratorControllerTest::GetController() { |
53 return Shell::GetInstance()->accelerator_controller(); | 59 return Shell::GetInstance()->accelerator_controller(); |
54 } | 60 } |
55 | 61 |
56 // void ShellAcceleratorControllerTest::SetUp() { | |
57 // AuraShellTestBase::SetUp(); | |
58 // } | |
59 | |
60 // void ShellAcceleratorControllerTest::TearDown() { | |
61 // AuraShellTestBase::TearDown(); | |
62 // } | |
63 | |
64 TEST_F(ShellAcceleratorControllerTest, Register) { | 62 TEST_F(ShellAcceleratorControllerTest, Register) { |
65 const ui::Accelerator accelerator_a(ui::VKEY_A, false, false, false); | 63 const ui::Accelerator accelerator_a(ui::VKEY_A, false, false, false); |
66 TestTarget target; | 64 TestTarget target; |
67 GetController()->Register(accelerator_a, &target); | 65 GetController()->Register(accelerator_a, &target); |
68 | 66 |
69 // The registered accelerator is processed. | 67 // The registered accelerator is processed. |
70 EXPECT_TRUE(GetController()->Process(accelerator_a)); | 68 EXPECT_TRUE(GetController()->Process(accelerator_a)); |
71 EXPECT_TRUE(target.accelerator_pressed()); | 69 EXPECT_EQ(1, target.accelerator_pressed_count()); |
72 } | 70 } |
73 | 71 |
74 TEST_F(ShellAcceleratorControllerTest, RegisterMultipleTarget) { | 72 TEST_F(ShellAcceleratorControllerTest, RegisterMultipleTarget) { |
75 const ui::Accelerator accelerator_a(ui::VKEY_A, false, false, false); | 73 const ui::Accelerator accelerator_a(ui::VKEY_A, false, false, false); |
76 TestTarget target1; | 74 TestTarget target1; |
77 GetController()->Register(accelerator_a, &target1); | 75 GetController()->Register(accelerator_a, &target1); |
78 TestTarget target2; | 76 TestTarget target2; |
79 GetController()->Register(accelerator_a, &target2); | 77 GetController()->Register(accelerator_a, &target2); |
80 | 78 |
81 // If multiple targets are registered with the same accelerator, the target | 79 // If multiple targets are registered with the same accelerator, the target |
82 // registered later processes the accelerator. | 80 // registered later processes the accelerator. |
83 EXPECT_TRUE(GetController()->Process(accelerator_a)); | 81 EXPECT_TRUE(GetController()->Process(accelerator_a)); |
84 EXPECT_FALSE(target1.accelerator_pressed()); | 82 EXPECT_EQ(0, target1.accelerator_pressed_count()); |
85 EXPECT_TRUE(target2.accelerator_pressed()); | 83 EXPECT_EQ(1, target2.accelerator_pressed_count()); |
86 } | 84 } |
87 | 85 |
88 TEST_F(ShellAcceleratorControllerTest, Unregister) { | 86 TEST_F(ShellAcceleratorControllerTest, Unregister) { |
89 const ui::Accelerator accelerator_a(ui::VKEY_A, false, false, false); | 87 const ui::Accelerator accelerator_a(ui::VKEY_A, false, false, false); |
90 TestTarget target; | 88 TestTarget target; |
91 GetController()->Register(accelerator_a, &target); | 89 GetController()->Register(accelerator_a, &target); |
92 const ui::Accelerator accelerator_b(ui::VKEY_B, false, false, false); | 90 const ui::Accelerator accelerator_b(ui::VKEY_B, false, false, false); |
93 GetController()->Register(accelerator_b, &target); | 91 GetController()->Register(accelerator_b, &target); |
94 | 92 |
95 // Unregistering a different accelerator does not affect the other | 93 // Unregistering a different accelerator does not affect the other |
96 // accelerator. | 94 // accelerator. |
97 GetController()->Unregister(accelerator_b, &target); | 95 GetController()->Unregister(accelerator_b, &target); |
98 EXPECT_TRUE(GetController()->Process(accelerator_a)); | 96 EXPECT_TRUE(GetController()->Process(accelerator_a)); |
99 EXPECT_TRUE(target.accelerator_pressed()); | 97 EXPECT_EQ(1, target.accelerator_pressed_count()); |
100 | 98 |
101 // The unregistered accelerator is no longer processed. | 99 // The unregistered accelerator is no longer processed. |
102 target.set_accelerator_pressed(false); | 100 target.set_accelerator_pressed_count(0); |
103 GetController()->Unregister(accelerator_a, &target); | 101 GetController()->Unregister(accelerator_a, &target); |
104 EXPECT_FALSE(GetController()->Process(accelerator_a)); | 102 EXPECT_FALSE(GetController()->Process(accelerator_a)); |
105 EXPECT_FALSE(target.accelerator_pressed()); | 103 EXPECT_EQ(0, target.accelerator_pressed_count()); |
106 } | 104 } |
107 | 105 |
108 TEST_F(ShellAcceleratorControllerTest, UnregisterAll) { | 106 TEST_F(ShellAcceleratorControllerTest, UnregisterAll) { |
109 const ui::Accelerator accelerator_a(ui::VKEY_A, false, false, false); | 107 const ui::Accelerator accelerator_a(ui::VKEY_A, false, false, false); |
110 TestTarget target1; | 108 TestTarget target1; |
111 GetController()->Register(accelerator_a, &target1); | 109 GetController()->Register(accelerator_a, &target1); |
112 const ui::Accelerator accelerator_b(ui::VKEY_B, false, false, false); | 110 const ui::Accelerator accelerator_b(ui::VKEY_B, false, false, false); |
113 GetController()->Register(accelerator_b, &target1); | 111 GetController()->Register(accelerator_b, &target1); |
114 const ui::Accelerator accelerator_c(ui::VKEY_C, false, false, false); | 112 const ui::Accelerator accelerator_c(ui::VKEY_C, false, false, false); |
115 TestTarget target2; | 113 TestTarget target2; |
116 GetController()->Register(accelerator_c, &target2); | 114 GetController()->Register(accelerator_c, &target2); |
117 GetController()->UnregisterAll(&target1); | 115 GetController()->UnregisterAll(&target1); |
118 | 116 |
119 // All the accelerators registered for |target1| are no longer processed. | 117 // All the accelerators registered for |target1| are no longer processed. |
120 EXPECT_FALSE(GetController()->Process(accelerator_a)); | 118 EXPECT_FALSE(GetController()->Process(accelerator_a)); |
121 EXPECT_FALSE(GetController()->Process(accelerator_b)); | 119 EXPECT_FALSE(GetController()->Process(accelerator_b)); |
122 EXPECT_FALSE(target1.accelerator_pressed()); | 120 EXPECT_EQ(0, target1.accelerator_pressed_count()); |
123 | 121 |
124 // UnregisterAll with a different target does not affect the other target. | 122 // UnregisterAll with a different target does not affect the other target. |
125 EXPECT_TRUE(GetController()->Process(accelerator_c)); | 123 EXPECT_TRUE(GetController()->Process(accelerator_c)); |
126 EXPECT_TRUE(target2.accelerator_pressed()); | 124 EXPECT_EQ(1, target2.accelerator_pressed_count()); |
127 } | 125 } |
128 | 126 |
129 TEST_F(ShellAcceleratorControllerTest, Process) { | 127 TEST_F(ShellAcceleratorControllerTest, Process) { |
130 const ui::Accelerator accelerator_a(ui::VKEY_A, false, false, false); | 128 const ui::Accelerator accelerator_a(ui::VKEY_A, false, false, false); |
131 TestTarget target1; | 129 TestTarget target1; |
132 GetController()->Register(accelerator_a, &target1); | 130 GetController()->Register(accelerator_a, &target1); |
133 | 131 |
134 // The registered accelerator is processed. | 132 // The registered accelerator is processed. |
135 EXPECT_TRUE(GetController()->Process(accelerator_a)); | 133 EXPECT_TRUE(GetController()->Process(accelerator_a)); |
136 EXPECT_TRUE(target1.accelerator_pressed()); | 134 EXPECT_EQ(1, target1.accelerator_pressed_count()); |
137 | 135 |
138 // The non-registered accelerator is not processed. | 136 // The non-registered accelerator is not processed. |
139 const ui::Accelerator accelerator_b(ui::VKEY_B, false, false, false); | 137 const ui::Accelerator accelerator_b(ui::VKEY_B, false, false, false); |
140 EXPECT_FALSE(GetController()->Process(accelerator_b)); | 138 EXPECT_FALSE(GetController()->Process(accelerator_b)); |
141 } | 139 } |
142 | 140 |
| 141 #if defined(OS_WIN) || defined(USE_X11) |
| 142 TEST_F(ShellAcceleratorControllerTest, ProcessOnce) { |
| 143 // A focused window must exist for accelerators to be processed. |
| 144 aura::Window* default_container = |
| 145 aura_shell::Shell::GetInstance()->GetContainer( |
| 146 internal::kShellWindowId_DefaultContainer); |
| 147 aura::Window* window = aura::test::CreateTestWindowWithDelegate( |
| 148 new aura::test::TestWindowDelegate, |
| 149 -1, |
| 150 gfx::Rect(), |
| 151 default_container); |
| 152 window->Activate(); |
| 153 |
| 154 const ui::Accelerator accelerator_a(ui::VKEY_A, false, false, false); |
| 155 TestTarget target; |
| 156 GetController()->Register(accelerator_a, &target); |
| 157 |
| 158 // The accelerator is processed only once. |
| 159 #if defined(OS_WIN) |
| 160 MSG msg1 = { NULL, WM_KEYDOWN, ui::VKEY_A, 0 }; |
| 161 aura::KeyEvent key_event1(msg1, false); |
| 162 EXPECT_TRUE(aura::RootWindow::GetInstance()->DispatchKeyEvent(&key_event1)); |
| 163 |
| 164 MSG msg2 = { NULL, WM_CHAR, L'A', 0 }; |
| 165 aura::KeyEvent key_event2(msg2, true); |
| 166 EXPECT_FALSE(aura::RootWindow::GetInstance()->DispatchKeyEvent(&key_event2)); |
| 167 |
| 168 MSG msg3 = { NULL, WM_KEYUP, ui::VKEY_A, 0 }; |
| 169 aura::KeyEvent key_event3(msg3, false); |
| 170 EXPECT_FALSE(aura::RootWindow::GetInstance()->DispatchKeyEvent(&key_event3)); |
| 171 #elif defined(USE_X11) |
| 172 XEvent key_event; |
| 173 ui::InitXKeyEventForTesting(ui::ET_KEY_PRESSED, |
| 174 ui::VKEY_A, |
| 175 0, |
| 176 &key_event); |
| 177 EXPECT_TRUE(aura::RootWindow::GetInstance()->GetDispatcher()->Dispatch( |
| 178 &key_event)); |
| 179 #endif |
| 180 EXPECT_EQ(1, target.accelerator_pressed_count()); |
| 181 } |
| 182 #endif |
| 183 |
143 TEST_F(ShellAcceleratorControllerTest, GlobalAccelerators) { | 184 TEST_F(ShellAcceleratorControllerTest, GlobalAccelerators) { |
144 // TODO(mazda): Uncomment the followings once they are implemented. | 185 // TODO(mazda): Uncomment the followings once they are implemented. |
145 // CycleBackward | 186 // CycleBackward |
146 // EXPECT_TRUE(GetController()->Process( | 187 // EXPECT_TRUE(GetController()->Process( |
147 // ui::Accelerator(ui::VKEY_TAB, true, false, true))); | 188 // ui::Accelerator(ui::VKEY_TAB, true, false, true))); |
148 // CycleForwrard | 189 // CycleForwrard |
149 // EXPECT_TRUE(GetController()->Process( | 190 // EXPECT_TRUE(GetController()->Process( |
150 // ui::Accelerator(ui::VKEY_TAB, false, false, true))); | 191 // ui::Accelerator(ui::VKEY_TAB, false, false, true))); |
151 // TakeScreenshot | 192 // TakeScreenshot |
152 // EXPECT_TRUE(GetController()->Process( | 193 // EXPECT_TRUE(GetController()->Process( |
153 // ui::Accelerator(ui::VKEY_F5, false, true, false))); | 194 // ui::Accelerator(ui::VKEY_F5, false, true, false))); |
154 // EXPECT_TRUE(GetController()->Process( | 195 // EXPECT_TRUE(GetController()->Process( |
155 // ui::Accelerator(ui::VKEY_PRINT, false, false, false))); | 196 // ui::Accelerator(ui::VKEY_PRINT, false, false, false))); |
156 #if !defined(NDEBUG) | 197 #if !defined(NDEBUG) |
157 // RotateScreen | 198 // RotateScreen |
158 EXPECT_TRUE(GetController()->Process( | 199 EXPECT_TRUE(GetController()->Process( |
159 ui::Accelerator(ui::VKEY_HOME, false, true, false))); | 200 ui::Accelerator(ui::VKEY_HOME, false, true, false))); |
160 #if !defined(OS_LINUX) | 201 #if !defined(OS_LINUX) |
161 // ToggleDesktopFullScreen (not implemented yet on Linux) | 202 // ToggleDesktopFullScreen (not implemented yet on Linux) |
162 EXPECT_TRUE(GetController()->Process( | 203 EXPECT_TRUE(GetController()->Process( |
163 ui::Accelerator(ui::VKEY_F11, false, true, false))); | 204 ui::Accelerator(ui::VKEY_F11, false, true, false))); |
164 #endif | 205 #endif |
165 #endif | 206 #endif |
166 } | 207 } |
167 | 208 |
168 } // namespace test | 209 } // namespace test |
169 } // namespace aura_shell | 210 } // namespace aura_shell |
OLD | NEW |