Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(671)

Side by Side Diff: ui/aura_shell/shell_accelerator_controller_unittest.cc

Issue 8833012: Fix the issue of ShellAcceleratorFilter sending accelerators twice for character keys. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add an unittest entry Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | ui/aura_shell/shell_accelerator_filter.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/root_window.h"
6 #include "ui/aura/test/test_window_delegate.h"
7 #include "ui/aura/test/test_windows.h"
5 #include "ui/aura_shell/shell.h" 8 #include "ui/aura_shell/shell.h"
6 #include "ui/aura_shell/shell_accelerator_controller.h" 9 #include "ui/aura_shell/shell_accelerator_controller.h"
10 #include "ui/aura_shell/shell_accelerator_filter.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 "base/memory/scoped_ptr.h"
17 #include "ui/base/keycodes/keyboard_code_conversion_x.h"
18 #endif
19
9 namespace aura_shell { 20 namespace aura_shell {
10 namespace test { 21 namespace test {
11 22
12 namespace { 23 namespace {
13 class TestTarget : public ui::AcceleratorTarget { 24 class TestTarget : public ui::AcceleratorTarget {
14 public: 25 public:
15 TestTarget() : accelerator_pressed_(false) {}; 26 TestTarget() : accelerator_pressed_count_(0) {};
16 virtual ~TestTarget() {}; 27 virtual ~TestTarget() {};
17 28
18 bool accelerator_pressed() const { 29 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
19 return accelerator_pressed_; 30 return accelerator_pressed_count_ > 0;
20 } 31 }
21 32
22 void set_accelerator_pressed(bool accelerator_pressed) { 33 int accelerator_pressed_count() const {
23 accelerator_pressed_ = accelerator_pressed; 34 return accelerator_pressed_count_;
35 }
36
37 void set_accelerator_pressed_count(int accelerator_pressed_count) {
38 accelerator_pressed_count_ = accelerator_pressed_count;
24 } 39 }
25 40
26 // Overridden from ui::AcceleratorTarget: 41 // Overridden from ui::AcceleratorTarget:
27 virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE; 42 virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE;
28 43
29 private: 44 private:
30 bool accelerator_pressed_; 45 int accelerator_pressed_count_;
31 }; 46 };
32 47
33 bool TestTarget::AcceleratorPressed(const ui::Accelerator& accelerator) { 48 bool TestTarget::AcceleratorPressed(const ui::Accelerator& accelerator) {
34 set_accelerator_pressed(true); 49 ++accelerator_pressed_count_;
35 return true; 50 return true;
36 } 51 }
37 52
38 } // namespace 53 } // namespace
39 54
40 class ShellAcceleratorControllerTest : public AuraShellTestBase { 55 class ShellAcceleratorControllerTest : public AuraShellTestBase {
41 public: 56 public:
42 ShellAcceleratorControllerTest() {}; 57 ShellAcceleratorControllerTest() {};
43 virtual ~ShellAcceleratorControllerTest() {}; 58 virtual ~ShellAcceleratorControllerTest() {};
44 59
45 static ShellAcceleratorController* GetController(); 60 static ShellAcceleratorController* GetController();
46 61
47 // testing::Test: 62 private:
48 // virtual void SetUp() OVERRIDE; 63 // Overridden from testing::Test:
49 // virtual void TearDown() OVERRIDE; 64 virtual void SetUp() OVERRIDE;
65 virtual void TearDown() OVERRIDE;
66
67 scoped_ptr<aura_shell::internal::ShellAcceleratorFilter> accelerator_filter_;
50 }; 68 };
51 69
70 void ShellAcceleratorControllerTest::SetUp() {
71 AuraShellTestBase::SetUp();
72 aura_shell::Shell::GetInstance()->AddRootWindowEventFilter(
73 accelerator_filter_.get());
74 // A focused window must exist for accelerators to be processed.
75 aura::Window* default_container =
76 aura_shell::Shell::GetInstance()->GetContainer(
77 internal::kShellWindowId_DefaultContainer);
78 aura::Window* window = aura::test::CreateTestWindowWithDelegate(
79 new aura::test::TestWindowDelegate,
80 -1,
81 gfx::Rect(),
82 default_container);
83 window->Activate();
84 }
85
86 void ShellAcceleratorControllerTest::TearDown() {
87 aura_shell::Shell::GetInstance()->RemoveRootWindowEventFilter(
88 accelerator_filter_.get());
89 AuraShellTestBase::TearDown();
90 }
91
52 ShellAcceleratorController* ShellAcceleratorControllerTest::GetController() { 92 ShellAcceleratorController* ShellAcceleratorControllerTest::GetController() {
53 return Shell::GetInstance()->accelerator_controller(); 93 return Shell::GetInstance()->accelerator_controller();
54 } 94 }
55 95
56 // void ShellAcceleratorControllerTest::SetUp() {
57 // AuraShellTestBase::SetUp();
58 // }
59
60 // void ShellAcceleratorControllerTest::TearDown() {
61 // AuraShellTestBase::TearDown();
62 // }
63
64 TEST_F(ShellAcceleratorControllerTest, Register) { 96 TEST_F(ShellAcceleratorControllerTest, Register) {
65 const ui::Accelerator accelerator_a(ui::VKEY_A, false, false, false); 97 const ui::Accelerator accelerator_a(ui::VKEY_A, false, false, false);
66 TestTarget target; 98 TestTarget target;
67 GetController()->Register(accelerator_a, &target); 99 GetController()->Register(accelerator_a, &target);
68 100
69 // The registered accelerator is processed. 101 // The registered accelerator is processed.
70 EXPECT_TRUE(GetController()->Process(accelerator_a)); 102 EXPECT_TRUE(GetController()->Process(accelerator_a));
71 EXPECT_TRUE(target.accelerator_pressed()); 103 EXPECT_TRUE(target.accelerator_pressed());
72 } 104 }
73 105
(...skipping 18 matching lines...) Expand all
92 const ui::Accelerator accelerator_b(ui::VKEY_B, false, false, false); 124 const ui::Accelerator accelerator_b(ui::VKEY_B, false, false, false);
93 GetController()->Register(accelerator_b, &target); 125 GetController()->Register(accelerator_b, &target);
94 126
95 // Unregistering a different accelerator does not affect the other 127 // Unregistering a different accelerator does not affect the other
96 // accelerator. 128 // accelerator.
97 GetController()->Unregister(accelerator_b, &target); 129 GetController()->Unregister(accelerator_b, &target);
98 EXPECT_TRUE(GetController()->Process(accelerator_a)); 130 EXPECT_TRUE(GetController()->Process(accelerator_a));
99 EXPECT_TRUE(target.accelerator_pressed()); 131 EXPECT_TRUE(target.accelerator_pressed());
100 132
101 // The unregistered accelerator is no longer processed. 133 // The unregistered accelerator is no longer processed.
102 target.set_accelerator_pressed(false); 134 target.set_accelerator_pressed_count(0);
103 GetController()->Unregister(accelerator_a, &target); 135 GetController()->Unregister(accelerator_a, &target);
104 EXPECT_FALSE(GetController()->Process(accelerator_a)); 136 EXPECT_FALSE(GetController()->Process(accelerator_a));
105 EXPECT_FALSE(target.accelerator_pressed()); 137 EXPECT_FALSE(target.accelerator_pressed());
106 } 138 }
107 139
108 TEST_F(ShellAcceleratorControllerTest, UnregisterAll) { 140 TEST_F(ShellAcceleratorControllerTest, UnregisterAll) {
109 const ui::Accelerator accelerator_a(ui::VKEY_A, false, false, false); 141 const ui::Accelerator accelerator_a(ui::VKEY_A, false, false, false);
110 TestTarget target1; 142 TestTarget target1;
111 GetController()->Register(accelerator_a, &target1); 143 GetController()->Register(accelerator_a, &target1);
112 const ui::Accelerator accelerator_b(ui::VKEY_B, false, false, false); 144 const ui::Accelerator accelerator_b(ui::VKEY_B, false, false, false);
(...skipping 20 matching lines...) Expand all
133 165
134 // The registered accelerator is processed. 166 // The registered accelerator is processed.
135 EXPECT_TRUE(GetController()->Process(accelerator_a)); 167 EXPECT_TRUE(GetController()->Process(accelerator_a));
136 EXPECT_TRUE(target1.accelerator_pressed()); 168 EXPECT_TRUE(target1.accelerator_pressed());
137 169
138 // The non-registered accelerator is not processed. 170 // The non-registered accelerator is not processed.
139 const ui::Accelerator accelerator_b(ui::VKEY_B, false, false, false); 171 const ui::Accelerator accelerator_b(ui::VKEY_B, false, false, false);
140 EXPECT_FALSE(GetController()->Process(accelerator_b)); 172 EXPECT_FALSE(GetController()->Process(accelerator_b));
141 } 173 }
142 174
175 #if defined(OS_WIN) || defined(USE_X11)
176 TEST_F(ShellAcceleratorControllerTest, ProcessOnce) {
177 const ui::Accelerator accelerator_a(ui::VKEY_A, false, false, false);
178 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.
179 GetController()->Register(accelerator_a, &target1);
180
181 // The accelerator is processed only once.
182 #if defined(OS_WIN)
183 MSG msg1 = { NULL, WM_KEYDOWN, VKEY_A, 0 };
184 EXPECT_TRUE(aura::RootWindow::GetInstance()->GetDispatcher()->Dispatch(msg1));
185 MSG msg2 = { NULL, WM_CHAR, L'A', 0 };
186 EXPECT_TRUE(aura::RootWindow::GetInstance()->GetDispatcher()->Dispatch(msg2));
187 #elif defined(USE_X11)
188 scoped_ptr<XEvent> key_event(ui::CreateXEventForTesting(ui::ET_KEY_PRESSED,
189 ui::VKEY_A,
190 0));
191 EXPECT_TRUE(aura::RootWindow::GetInstance()->GetDispatcher()->Dispatch(
192 key_event.get()));
193 #endif
194 EXPECT_EQ(1, target1.accelerator_pressed_count());
195 }
196 #endif
197
143 TEST_F(ShellAcceleratorControllerTest, GlobalAccelerators) { 198 TEST_F(ShellAcceleratorControllerTest, GlobalAccelerators) {
144 // TODO(mazda): Uncomment the followings once they are implemented. 199 // TODO(mazda): Uncomment the followings once they are implemented.
145 // CycleBackward 200 // CycleBackward
146 // EXPECT_TRUE(GetController()->Process( 201 // EXPECT_TRUE(GetController()->Process(
147 // ui::Accelerator(ui::VKEY_TAB, true, false, true))); 202 // ui::Accelerator(ui::VKEY_TAB, true, false, true)));
148 // CycleForwrard 203 // CycleForwrard
149 // EXPECT_TRUE(GetController()->Process( 204 // EXPECT_TRUE(GetController()->Process(
150 // ui::Accelerator(ui::VKEY_TAB, false, false, true))); 205 // ui::Accelerator(ui::VKEY_TAB, false, false, true)));
151 // TakeScreenshot 206 // TakeScreenshot
152 // EXPECT_TRUE(GetController()->Process( 207 // EXPECT_TRUE(GetController()->Process(
153 // ui::Accelerator(ui::VKEY_F5, false, true, false))); 208 // ui::Accelerator(ui::VKEY_F5, false, true, false)));
154 // EXPECT_TRUE(GetController()->Process( 209 // EXPECT_TRUE(GetController()->Process(
155 // ui::Accelerator(ui::VKEY_PRINT, false, false, false))); 210 // ui::Accelerator(ui::VKEY_PRINT, false, false, false)));
156 #if !defined(NDEBUG) 211 #if !defined(NDEBUG)
157 // TODO(mazda): Callig RotateScreen in unit test causes a crash because of 212 // TODO(mazda): Callig RotateScreen in unit test causes a crash because of
158 // "pure virtual method called" for some reasons. Need to investigate. 213 // "pure virtual method called" for some reasons. Need to investigate.
159 // RotateScreen 214 // RotateScreen
160 // EXPECT_TRUE(GetController()->Process( 215 // EXPECT_TRUE(GetController()->Process(
161 // ui::Accelerator(ui::VKEY_HOME, false, true, false))); 216 // ui::Accelerator(ui::VKEY_HOME, false, true, false)));
162 #if !defined(OS_LINUX) 217 #if !defined(OS_LINUX)
163 // ToggleDesktopFullScreen (not implemented yet on Linux) 218 // ToggleDesktopFullScreen (not implemented yet on Linux)
164 EXPECT_TRUE(GetController()->Process( 219 EXPECT_TRUE(GetController()->Process(
165 ui::Accelerator(ui::VKEY_F11, false, true, false))); 220 ui::Accelerator(ui::VKEY_F11, false, true, false)));
166 #endif 221 #endif
167 #endif 222 #endif
168 } 223 }
169 224
170 } // namespace test 225 } // namespace test
171 } // namespace aura_shell 226 } // namespace aura_shell
OLDNEW
« no previous file with comments | « no previous file | ui/aura_shell/shell_accelerator_filter.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698