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

Side by Side Diff: ui/events/event_processor_unittest.cc

Issue 519113002: Do not mutate ui::Event properties during nested event processing (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: comments changed Created 6 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
« no previous file with comments | « ui/events/event_processor.cc ('k') | no next file » | 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) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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 <vector> 5 #include <vector>
6 6
7 #include "testing/gtest/include/gtest/gtest.h" 7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "ui/events/event.h" 8 #include "ui/events/event.h"
9 #include "ui/events/event_targeter.h" 9 #include "ui/events/event_targeter.h"
10 #include "ui/events/test/events_test_utils.h" 10 #include "ui/events/test/events_test_utils.h"
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 161
162 MouseEvent second(ET_MOUSE_MOVED, gfx::Point(12, 12), gfx::Point(12, 12), 162 MouseEvent second(ET_MOUSE_MOVED, gfx::Point(12, 12), gfx::Point(12, 12),
163 EF_NONE, EF_NONE); 163 EF_NONE, EF_NONE);
164 DispatchEvent(&second); 164 DispatchEvent(&second);
165 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED)); 165 EXPECT_FALSE(root()->DidReceiveEvent(ET_MOUSE_MOVED));
166 EXPECT_FALSE(parent_r->DidReceiveEvent(ET_MOUSE_MOVED)); 166 EXPECT_FALSE(parent_r->DidReceiveEvent(ET_MOUSE_MOVED));
167 EXPECT_FALSE(child_r->DidReceiveEvent(ET_MOUSE_MOVED)); 167 EXPECT_FALSE(child_r->DidReceiveEvent(ET_MOUSE_MOVED));
168 EXPECT_TRUE(grandchild_r->DidReceiveEvent(ET_MOUSE_MOVED)); 168 EXPECT_TRUE(grandchild_r->DidReceiveEvent(ET_MOUSE_MOVED));
169 } 169 }
170 170
171 // ReDispatchEventHandler is used to receive mouse events and forward them
172 // to a specified EventProcessor. Verifies that the event has the correct
173 // target and phase both before and after the nested event processing.
174 class ReDispatchEventHandler : public TestEventHandler {
175 public:
176 ReDispatchEventHandler(EventProcessor* processor, EventTarget* target)
177 : processor_(processor), expected_target_(target) {}
178 virtual ~ReDispatchEventHandler() {}
179
180 // TestEventHandler:
181 virtual void OnMouseEvent(MouseEvent* event) OVERRIDE {
182 TestEventHandler::OnMouseEvent(event);
183
184 EXPECT_EQ(expected_target_, event->target());
185 EXPECT_EQ(EP_TARGET, event->phase());
186
187 EventDispatchDetails details = processor_->OnEventFromSource(event);
188 EXPECT_FALSE(details.dispatcher_destroyed);
189 EXPECT_FALSE(details.target_destroyed);
190
191 // The nested event-processing should not have mutated the target or
192 // phase of |event|.
193 EXPECT_EQ(expected_target_, event->target());
194 EXPECT_EQ(EP_TARGET, event->phase());
195 }
196
197 private:
198 EventProcessor* processor_;
199 EventTarget* expected_target_;
200
201 DISALLOW_COPY_AND_ASSIGN(ReDispatchEventHandler);
202 };
203
204 // Verifies that the phase and target information of an event is not mutated
205 // as a result of sending the event to an event processor while it is still
206 // being processed by another event processor.
207 TEST_F(EventProcessorTest, NestedEventProcessing) {
208 // Add one child to the default event processor used in this test suite.
209 scoped_ptr<TestEventTarget> child(new TestEventTarget());
210 root()->AddChild(child.Pass());
211
212 // Define a second root target and child.
213 scoped_ptr<EventTarget> second_root_scoped(new TestEventTarget());
214 TestEventTarget* second_root =
215 static_cast<TestEventTarget*>(second_root_scoped.get());
216 second_root->SetEventTargeter(make_scoped_ptr(new EventTargeter()));
217 scoped_ptr<TestEventTarget> second_child(new TestEventTarget());
218 second_root->AddChild(second_child.Pass());
219
220 // Define a second event processor which owns the second root.
221 scoped_ptr<TestEventProcessor> second_processor(new TestEventProcessor());
222 second_processor->SetRoot(second_root_scoped.Pass());
223
224 // Indicate that an event which is dispatched to the child target owned by the
225 // first event processor should be handled by |target_handler| instead.
226 scoped_ptr<TestEventHandler> target_handler(
227 new ReDispatchEventHandler(second_processor.get(), root()->child_at(0)));
228 root()->child_at(0)->set_target_handler(target_handler.get());
229
230 // Dispatch a mouse event to the tree of event targets owned by the first
231 // event processor, checking in ReDispatchEventHandler that the phase and
232 // target information of the event is correct.
233 MouseEvent mouse(
234 ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), EF_NONE, EF_NONE);
235 DispatchEvent(&mouse);
236
237 // Verify also that |mouse| was seen by the child nodes contained in both
238 // event processors and that the event was not handled.
239 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED));
240 EXPECT_TRUE(second_root->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED));
241 EXPECT_FALSE(mouse.handled());
242 second_root->child_at(0)->ResetReceivedEvents();
243 root()->child_at(0)->ResetReceivedEvents();
244
245 // Indicate that the child of the second root should handle events, and
246 // dispatch another mouse event to verify that it is marked as handled.
247 second_root->child_at(0)->set_mark_events_as_handled(true);
248 MouseEvent mouse2(
249 ET_MOUSE_MOVED, gfx::Point(10, 10), gfx::Point(10, 10), EF_NONE, EF_NONE);
250 DispatchEvent(&mouse2);
251 EXPECT_TRUE(root()->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED));
252 EXPECT_TRUE(second_root->child_at(0)->DidReceiveEvent(ET_MOUSE_MOVED));
253 EXPECT_TRUE(mouse2.handled());
254 }
255
171 class IgnoreEventTargeter : public EventTargeter { 256 class IgnoreEventTargeter : public EventTargeter {
172 public: 257 public:
173 IgnoreEventTargeter() {} 258 IgnoreEventTargeter() {}
174 virtual ~IgnoreEventTargeter() {} 259 virtual ~IgnoreEventTargeter() {}
175 260
176 private: 261 private:
177 // EventTargeter: 262 // EventTargeter:
178 virtual bool SubtreeShouldBeExploredForEvent( 263 virtual bool SubtreeShouldBeExploredForEvent(
179 EventTarget* target, const LocatedEvent& event) OVERRIDE { 264 EventTarget* target, const LocatedEvent& event) OVERRIDE {
180 return false; 265 return false;
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
365 DispatchEvent(&mouse); 450 DispatchEvent(&mouse);
366 451
367 std::string expected[] = { "PreR", "PreC", "PreG", "G", "PostG", "PostC", 452 std::string expected[] = { "PreR", "PreC", "PreG", "G", "PostG", "PostC",
368 "PostR", "PreR", "PreC", "C", "PostC", "PostR", "PreR", "R", "PostR" }; 453 "PostR", "PreR", "PreC", "C", "PostC", "PostR", "PreR", "R", "PostR" };
369 EXPECT_EQ(std::vector<std::string>( 454 EXPECT_EQ(std::vector<std::string>(
370 expected, expected + arraysize(expected)), recorder); 455 expected, expected + arraysize(expected)), recorder);
371 } 456 }
372 457
373 } // namespace test 458 } // namespace test
374 } // namespace ui 459 } // namespace ui
OLDNEW
« no previous file with comments | « ui/events/event_processor.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698