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

Side by Side Diff: ui/base/events/event_dispatcher_unittest.cc

Issue 10912168: events: Add some test. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: win-component Created 8 years, 3 months 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 | « ui/base/events/event_dispatcher.cc ('k') | ui/base/events/event_handler.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 "ui/base/events/event_dispatcher.h"
6
7 #include "testing/gtest/include/gtest/gtest.h"
8
9 namespace ui {
10
11 namespace {
12
13 class TestEventDispatcher : public EventDispatcher {
14 public:
15 TestEventDispatcher() {}
16 virtual ~TestEventDispatcher() {}
17
18 private:
19 // Overridden from EventDispatcher:
20 virtual bool CanDispatchToTarget(EventTarget* target) OVERRIDE {
21 return true;
22 }
23
24 virtual void ProcessPreTargetList(EventHandlerList* list) OVERRIDE {
25 }
26
27 virtual void ProcessPostTargetList(EventHandlerList* list) OVERRIDE {
28 }
29
30 DISALLOW_COPY_AND_ASSIGN(TestEventDispatcher);
31 };
32
33 class TestTarget : public EventTarget {
34 public:
35 TestTarget() : parent_(NULL) {}
36 virtual ~TestTarget() {}
37
38 void set_parent(TestTarget* parent) { parent_ = parent; }
39
40 void AddHandlerId(int id) {
41 handler_list_.push_back(id);
42 }
43
44 const std::vector<int>& handler_list() const { return handler_list_; }
45
46 void Reset() {
47 handler_list_.clear();
48 }
49
50 private:
51 // Overridden from EventTarget:
52 virtual bool CanAcceptEvents() OVERRIDE {
53 return true;
54 }
55
56 virtual EventTarget* GetParentTarget() OVERRIDE {
57 return parent_;
58 }
59
60 TestTarget* parent_;
61 std::vector<int> handler_list_;
62
63 DISALLOW_COPY_AND_ASSIGN(TestTarget);
64 };
65
66 class TestEventHandler : public EventHandler {
67 public:
68 TestEventHandler(int id)
69 : id_(id),
70 event_result_(ER_UNHANDLED) {
71 }
72
73 virtual ~TestEventHandler() {}
74
75 void ReceivedEventForTarget(EventTarget* target) {
76 static_cast<TestTarget*>(target)->AddHandlerId(id_);
77 }
78
79 void set_event_result(EventResult result) { event_result_ = result; }
80
81 private:
82 // Overridden from EventHandler:
83 virtual EventResult OnKeyEvent(KeyEvent* event) OVERRIDE {
84 ReceivedEventForTarget(event->target());
85 return event_result_;
86 }
87
88 virtual EventResult OnMouseEvent(MouseEvent* event) OVERRIDE {
89 ReceivedEventForTarget(event->target());
90 return event_result_;
91 }
92
93 virtual EventResult OnScrollEvent(ScrollEvent* event) OVERRIDE {
94 ReceivedEventForTarget(event->target());
95 return event_result_;
96 }
97
98 virtual TouchStatus OnTouchEvent(TouchEvent* event) OVERRIDE {
99 ReceivedEventForTarget(event->target());
100 return ui::TOUCH_STATUS_UNKNOWN;
101 }
102
103 virtual EventResult OnGestureEvent(GestureEvent* event) OVERRIDE {
104 ReceivedEventForTarget(event->target());
105 return event_result_;
106 }
107
108 int id_;
109 EventResult event_result_;
110
111 DISALLOW_COPY_AND_ASSIGN(TestEventHandler);
112 };
113
114 } // namespace
115
116 TEST(EventDispatcherTest, EventDispatchOrder) {
117 TestEventDispatcher dispatcher;
118 TestTarget parent, child;
119 TestEventHandler h1(1), h2(2), h3(3), h4(4);
120 TestEventHandler h5(5), h6(6), h7(7), h8(8);
121
122 child.set_parent(&parent);
123
124 parent.AddPreTargetHandler(&h1);
125 parent.AddPreTargetHandler(&h2);
126
127 child.AddPreTargetHandler(&h3);
128 child.AddPreTargetHandler(&h4);
129
130 child.AddPostTargetHandler(&h5);
131 child.AddPostTargetHandler(&h6);
132
133 parent.AddPostTargetHandler(&h7);
134 parent.AddPostTargetHandler(&h8);
135
136 MouseEvent mouse(ui::ET_MOUSE_MOVED, gfx::Point(3, 4),
137 gfx::Point(3, 4), 0);
138 int result = dispatcher.ProcessEvent(&child, &mouse);
139 EXPECT_FALSE(result & ER_CONSUMED);
140 EXPECT_FALSE(result & ER_HANDLED);
141
142 // Note that the pre-handlers for the target itself (i.e. |child|) will not
143 // receive these events. This is for compatibility reasons for how
144 // event-filters behave in aura. The desired behaviour is that the
145 // pre-handlers for the target will receive the events before the target.
146 // http://crbug.com/147523
147 int expected[] = { 1, 2, 5, 6, 7, 8 };
148 EXPECT_EQ(
149 std::vector<int>(expected, expected + sizeof(expected) / sizeof(int)),
150 child.handler_list());
151
152 child.Reset();
153
154 h1.set_event_result(ER_HANDLED);
155 result = dispatcher.ProcessEvent(&child, &mouse);
156 EXPECT_FALSE(result & ER_CONSUMED);
157 EXPECT_TRUE(result & ER_HANDLED);
158 EXPECT_EQ(
159 std::vector<int>(expected, expected + sizeof(expected) / sizeof(int)),
160 child.handler_list());
161
162 child.Reset();
163
164 int nexpected[] = { 1, 2, 5 };
165 h5.set_event_result(ER_CONSUMED);
166 result = dispatcher.ProcessEvent(&child, &mouse);
167 EXPECT_TRUE(result & ER_CONSUMED);
168 EXPECT_TRUE(result & ER_HANDLED);
169 EXPECT_EQ(
170 std::vector<int>(nexpected, nexpected + sizeof(nexpected) / sizeof(int)),
171 child.handler_list());
172
173 child.Reset();
174
175 int exp[] = { 1 };
176 h1.set_event_result(ER_CONSUMED);
177 result = dispatcher.ProcessEvent(&child, &mouse);
178 EXPECT_TRUE(result & ER_CONSUMED);
179 EXPECT_FALSE(result & ER_HANDLED);
180 EXPECT_EQ(
181 std::vector<int>(exp, exp + sizeof(exp) / sizeof(int)),
182 child.handler_list());
183 }
184
185 } // namespace ui
OLDNEW
« no previous file with comments | « ui/base/events/event_dispatcher.cc ('k') | ui/base/events/event_handler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698