1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/views/controls/menu/menu_controller.h"
#include "base/run_loop.h"
#include "ui/aura/scoped_window_targeter.h"
#include "ui/aura/window.h"
#include "ui/events/event_handler.h"
#include "ui/events/null_event_targeter.h"
#include "ui/events/platform/platform_event_source.h"
#include "ui/views/controls/menu/menu_item_view.h"
#include "ui/views/test/views_test_base.h"
#include "ui/wm/public/dispatcher_client.h"
#if defined(OS_WIN)
#include "base/message_loop/message_pump_dispatcher.h"
#elif defined(USE_X11)
#include <X11/Xlib.h>
#undef Bool
#undef None
#include "ui/events/devices/x11/device_data_manager_x11.h"
#include "ui/events/test/events_test_utils_x11.h"
#elif defined(USE_OZONE)
#include "ui/events/event.h"
#endif
namespace views {
namespace {
class TestMenuItemView : public MenuItemView {
public:
TestMenuItemView() : MenuItemView(NULL) {}
~TestMenuItemView() override {}
private:
DISALLOW_COPY_AND_ASSIGN(TestMenuItemView);
};
class TestPlatformEventSource : public ui::PlatformEventSource {
public:
TestPlatformEventSource() {
#if defined(USE_X11)
ui::DeviceDataManagerX11::CreateInstance();
#endif
}
~TestPlatformEventSource() override {}
uint32_t Dispatch(const ui::PlatformEvent& event) {
return DispatchEvent(event);
}
private:
DISALLOW_COPY_AND_ASSIGN(TestPlatformEventSource);
};
class TestDispatcherClient : public aura::client::DispatcherClient {
public:
TestDispatcherClient() : dispatcher_(NULL) {}
~TestDispatcherClient() override {}
base::MessagePumpDispatcher* dispatcher() {
return dispatcher_;
}
// aura::client::DispatcherClient:
void PrepareNestedLoopClosures(base::MessagePumpDispatcher* dispatcher,
base::Closure* run_closure,
base::Closure* quit_closure) override {
scoped_ptr<base::RunLoop> run_loop(new base::RunLoop());
*quit_closure = run_loop->QuitClosure();
*run_closure = base::Bind(&TestDispatcherClient::RunNestedDispatcher,
base::Unretained(this),
base::Passed(&run_loop),
dispatcher);
}
private:
void RunNestedDispatcher(scoped_ptr<base::RunLoop> run_loop,
base::MessagePumpDispatcher* dispatcher) {
base::AutoReset<base::MessagePumpDispatcher*> reset_dispatcher(&dispatcher_,
dispatcher);
base::MessageLoopForUI* loop = base::MessageLoopForUI::current();
base::MessageLoop::ScopedNestableTaskAllower allow(loop);
run_loop->Run();
}
base::MessagePumpDispatcher* dispatcher_;
DISALLOW_COPY_AND_ASSIGN(TestDispatcherClient);
};
} // namespace
class MenuControllerTest : public ViewsTestBase {
public:
MenuControllerTest() : controller_(NULL) {}
~MenuControllerTest() override { ResetMenuController(); }
// Dispatches |count| number of items, each in a separate iteration of the
// message-loop, by posting a task.
void Step3_DispatchEvents(int count) {
base::MessageLoopForUI* loop = base::MessageLoopForUI::current();
base::MessageLoop::ScopedNestableTaskAllower allow(loop);
controller_->exit_type_ = MenuController::EXIT_ALL;
DispatchEvent();
if (count) {
base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&MenuControllerTest::Step3_DispatchEvents,
base::Unretained(this),
count - 1));
} else {
EXPECT_TRUE(run_loop_->running());
run_loop_->Quit();
}
}
// Runs a nested message-loop that does not involve the menu itself.
void Step2_RunNestedLoop() {
base::MessageLoopForUI* loop = base::MessageLoopForUI::current();
base::MessageLoop::ScopedNestableTaskAllower allow(loop);
base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&MenuControllerTest::Step3_DispatchEvents,
base::Unretained(this),
3));
run_loop_.reset(new base::RunLoop());
run_loop_->Run();
}
void Step1_RunMenu() {
base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&MenuControllerTest::Step2_RunNestedLoop,
base::Unretained(this)));
scoped_ptr<Widget> owner(CreateOwnerWidget());
RunMenu(owner.get());
}
scoped_ptr<Widget> CreateOwnerWidget() {
scoped_ptr<Widget> widget(new Widget);
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
widget->Init(params);
widget->Show();
aura::client::SetDispatcherClient(
widget->GetNativeWindow()->GetRootWindow(), &dispatcher_client_);
return widget.Pass();
}
void RunMenu(views::Widget* owner) {
scoped_ptr<TestMenuItemView> menu_item(new TestMenuItemView);
ResetMenuController();
controller_ = new MenuController(NULL, true, NULL);
controller_->owner_ = owner;
controller_->showing_ = true;
controller_->SetSelection(menu_item.get(),
MenuController::SELECTION_UPDATE_IMMEDIATELY);
controller_->RunMessageLoop(false);
}
#if defined(USE_X11)
void DispatchEscapeAndExpect(MenuController::ExitType exit_type) {
ui::ScopedXI2Event key_event;
key_event.InitKeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_ESCAPE, 0);
event_source_.Dispatch(key_event);
EXPECT_EQ(exit_type, controller_->exit_type());
controller_->exit_type_ = MenuController::EXIT_ALL;
DispatchEvent();
}
void DispatchTouch(int evtype, int id) {
ui::ScopedXI2Event touch_event;
std::vector<ui::Valuator> valuators;
touch_event.InitTouchEvent(1, evtype, id, gfx::Point(10, 10), valuators);
event_source_.Dispatch(touch_event);
DispatchEvent();
}
#endif
void DispatchEvent() {
#if defined(USE_X11)
XEvent xevent;
memset(&xevent, 0, sizeof(xevent));
event_source_.Dispatch(&xevent);
#elif defined(OS_WIN)
MSG msg;
memset(&msg, 0, sizeof(MSG));
dispatcher_client_.dispatcher()->Dispatch(msg);
#elif defined(USE_OZONE)
ui::KeyEvent event(' ', ui::VKEY_SPACE, ui::EF_NONE);
event_source_.Dispatch(&event);
#else
#error Unsupported platform
#endif
}
private:
void ResetMenuController() {
if (controller_) {
// These properties are faked by RunMenu for the purposes of testing and
// need to be undone before we call the destructor.
controller_->owner_ = NULL;
controller_->showing_ = false;
delete controller_;
controller_ = NULL;
}
}
// A weak pointer to the MenuController owned by this class.
MenuController* controller_;
scoped_ptr<base::RunLoop> run_loop_;
TestPlatformEventSource event_source_;
TestDispatcherClient dispatcher_client_;
DISALLOW_COPY_AND_ASSIGN(MenuControllerTest);
};
TEST_F(MenuControllerTest, Basic) {
base::MessageLoop::ScopedNestableTaskAllower allow_nested(
base::MessageLoop::current());
message_loop()->PostTask(
FROM_HERE,
base::Bind(&MenuControllerTest::Step1_RunMenu, base::Unretained(this)));
}
#if defined(OS_LINUX) && defined(USE_X11)
// Tests that an event targeter which blocks events will be honored by the menu
// event dispatcher.
TEST_F(MenuControllerTest, EventTargeter) {
{
// Verify that the menu handles the escape key under normal circumstances.
scoped_ptr<Widget> owner(CreateOwnerWidget());
message_loop()->PostTask(
FROM_HERE,
base::Bind(&MenuControllerTest::DispatchEscapeAndExpect,
base::Unretained(this),
MenuController::EXIT_OUTERMOST));
RunMenu(owner.get());
}
{
// With the NULL targeter instantiated and assigned we expect the menu to
// not handle the key event.
scoped_ptr<Widget> owner(CreateOwnerWidget());
aura::ScopedWindowTargeter scoped_targeter(
owner->GetNativeWindow()->GetRootWindow(),
scoped_ptr<ui::EventTargeter>(new ui::NullEventTargeter));
message_loop()->PostTask(
FROM_HERE,
base::Bind(&MenuControllerTest::DispatchEscapeAndExpect,
base::Unretained(this),
MenuController::EXIT_NONE));
RunMenu(owner.get());
}
}
#endif
#if defined(USE_X11)
class TestEventHandler : public ui::EventHandler {
public:
TestEventHandler() : outstanding_touches_(0) {}
void OnTouchEvent(ui::TouchEvent* event) override {
switch(event->type()) {
case ui::ET_TOUCH_PRESSED:
outstanding_touches_++;
break;
case ui::ET_TOUCH_RELEASED:
case ui::ET_TOUCH_CANCELLED:
outstanding_touches_--;
break;
default:
break;
}
}
int outstanding_touches() const { return outstanding_touches_; }
private:
int outstanding_touches_;
};
// Tests that touch event ids are released correctly. See
// crbug.com/439051 for details. When the ids aren't managed
// correctly, we get stuck down touches.
TEST_F(MenuControllerTest, TouchIdsReleasedCorrectly) {
scoped_ptr<Widget> owner(CreateOwnerWidget());
TestEventHandler test_event_handler;
owner->GetNativeWindow()->GetRootWindow()->AddPreTargetHandler(
&test_event_handler);
std::vector<int> devices;
devices.push_back(1);
ui::SetUpTouchDevicesForTest(devices);
DispatchTouch(XI_TouchBegin, 0);
DispatchTouch(XI_TouchBegin, 1);
DispatchTouch(XI_TouchEnd, 0);
message_loop()->PostTask(FROM_HERE,
base::Bind(&MenuControllerTest::DispatchTouch,
base::Unretained(this), XI_TouchEnd, 1));
message_loop()->PostTask(
FROM_HERE,
base::Bind(&MenuControllerTest::DispatchEscapeAndExpect,
base::Unretained(this), MenuController::EXIT_OUTERMOST));
RunMenu(owner.get());
EXPECT_EQ(0, test_event_handler.outstanding_touches());
owner->GetNativeWindow()->GetRootWindow()->RemovePreTargetHandler(
&test_event_handler);
}
#endif // defined(USE_X11)
} // namespace views
|