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

Side by Side Diff: ui/events/platform/platform_event_source_unittest.cc

Issue 2628393003: Remove ScopedVector from ui/events/. (Closed)
Patch Set: chromeos2 Created 3 years, 11 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/events/platform/platform_event_source.h" 5 #include "ui/events/platform/platform_event_source.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <memory> 10 #include <memory>
11 #include <utility> 11 #include <utility>
12 #include <vector>
12 13
13 #include "base/bind.h" 14 #include "base/bind.h"
14 #include "base/macros.h" 15 #include "base/macros.h"
15 #include "base/memory/scoped_vector.h" 16 #include "base/memory/ptr_util.h"
16 #include "base/message_loop/message_loop.h" 17 #include "base/message_loop/message_loop.h"
17 #include "base/run_loop.h" 18 #include "base/run_loop.h"
18 #include "base/single_thread_task_runner.h" 19 #include "base/single_thread_task_runner.h"
19 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
20 #include "ui/events/platform/platform_event_dispatcher.h" 21 #include "ui/events/platform/platform_event_dispatcher.h"
21 #include "ui/events/platform/platform_event_observer.h" 22 #include "ui/events/platform/platform_event_observer.h"
22 #include "ui/events/platform/scoped_event_dispatcher.h" 23 #include "ui/events/platform/scoped_event_dispatcher.h"
23 24
24 namespace ui { 25 namespace ui {
25 26
26 namespace { 27 namespace {
27 28
28 std::unique_ptr<PlatformEvent> CreatePlatformEvent() { 29 std::unique_ptr<PlatformEvent> CreatePlatformEvent() {
29 std::unique_ptr<PlatformEvent> event(new PlatformEvent()); 30 std::unique_ptr<PlatformEvent> event = base::MakeUnique<PlatformEvent>();
sadrul 2017/01/16 20:26:44 Is MakeUnique<> preferred over using new with the
Avi (use Gerrit) 2017/01/16 20:39:38 Yes. https://www.chromium.org/developers/coding-st
30 memset(event.get(), 0, sizeof(PlatformEvent)); 31 memset(event.get(), 0, sizeof(PlatformEvent));
31 return event; 32 return event;
32 } 33 }
33 34
34 template <typename T>
35 void DestroyScopedPtr(std::unique_ptr<T> object) {}
36
37 void RemoveDispatcher(PlatformEventDispatcher* dispatcher) { 35 void RemoveDispatcher(PlatformEventDispatcher* dispatcher) {
38 PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(dispatcher); 36 PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(dispatcher);
39 } 37 }
40 38
41 void RemoveDispatchers(PlatformEventDispatcher* first, 39 void RemoveDispatchers(PlatformEventDispatcher* first,
42 PlatformEventDispatcher* second) { 40 PlatformEventDispatcher* second) {
43 PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(first); 41 PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(first);
44 PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(second); 42 PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(second);
45 } 43 }
46 44
47 void AddDispatcher(PlatformEventDispatcher* dispatcher) { 45 void AddDispatcher(PlatformEventDispatcher* dispatcher) {
48 PlatformEventSource::GetInstance()->AddPlatformEventDispatcher(dispatcher); 46 PlatformEventSource::GetInstance()->AddPlatformEventDispatcher(dispatcher);
49 } 47 }
50 48
51 } // namespace 49 } // namespace
52 50
53 class TestPlatformEventSource : public PlatformEventSource { 51 class TestPlatformEventSource : public PlatformEventSource {
54 public: 52 public:
55 TestPlatformEventSource() 53 TestPlatformEventSource()
56 : stop_stream_(false) { 54 : stop_stream_(false) {
57 } 55 }
58 ~TestPlatformEventSource() override {} 56 ~TestPlatformEventSource() override {}
59 57
60 uint32_t Dispatch(const PlatformEvent& event) { return DispatchEvent(event); } 58 uint32_t Dispatch(const PlatformEvent& event) { return DispatchEvent(event); }
61 59
62 // Dispatches the stream of events, and returns the number of events that are 60 // Dispatches the stream of events, and returns the number of events that are
63 // dispatched before it is requested to stop. 61 // dispatched before it is requested to stop.
64 size_t DispatchEventStream(const ScopedVector<PlatformEvent>& events) { 62 size_t DispatchEventStream(
63 const std::vector<std::unique_ptr<PlatformEvent>>& events) {
65 stop_stream_ = false; 64 stop_stream_ = false;
66 for (size_t count = 0; count < events.size(); ++count) { 65 for (size_t count = 0; count < events.size(); ++count) {
67 DispatchEvent(*events[count]); 66 DispatchEvent(*events[count]);
68 if (stop_stream_) 67 if (stop_stream_)
69 return count + 1; 68 return count + 1;
70 } 69 }
71 return events.size(); 70 return events.size();
72 } 71 }
73 72
74 // PlatformEventSource: 73 // PlatformEventSource:
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 149
151 private: 150 private:
152 std::unique_ptr<TestPlatformEventSource> source_; 151 std::unique_ptr<TestPlatformEventSource> source_;
153 152
154 DISALLOW_COPY_AND_ASSIGN(PlatformEventTest); 153 DISALLOW_COPY_AND_ASSIGN(PlatformEventTest);
155 }; 154 };
156 155
157 // Tests that a dispatcher receives an event. 156 // Tests that a dispatcher receives an event.
158 TEST_F(PlatformEventTest, DispatcherBasic) { 157 TEST_F(PlatformEventTest, DispatcherBasic) {
159 std::vector<int> list_dispatcher; 158 std::vector<int> list_dispatcher;
160 std::unique_ptr<PlatformEvent> event(CreatePlatformEvent()); 159 std::unique_ptr<PlatformEvent> event = CreatePlatformEvent();
161 source()->Dispatch(*event); 160 source()->Dispatch(*event);
162 EXPECT_EQ(0u, list_dispatcher.size()); 161 EXPECT_EQ(0u, list_dispatcher.size());
163 { 162 {
164 TestPlatformEventDispatcher dispatcher(1, &list_dispatcher); 163 TestPlatformEventDispatcher dispatcher(1, &list_dispatcher);
165 164
166 std::unique_ptr<PlatformEvent> event(CreatePlatformEvent()); 165 std::unique_ptr<PlatformEvent> event = CreatePlatformEvent();
167 source()->Dispatch(*event); 166 source()->Dispatch(*event);
168 ASSERT_EQ(1u, list_dispatcher.size()); 167 ASSERT_EQ(1u, list_dispatcher.size());
169 EXPECT_EQ(1, list_dispatcher[0]); 168 EXPECT_EQ(1, list_dispatcher[0]);
170 } 169 }
171 170
172 list_dispatcher.clear(); 171 list_dispatcher.clear();
173 event = CreatePlatformEvent(); 172 event = CreatePlatformEvent();
174 source()->Dispatch(*event); 173 source()->Dispatch(*event);
175 EXPECT_EQ(0u, list_dispatcher.size()); 174 EXPECT_EQ(0u, list_dispatcher.size());
176 } 175 }
177 176
178 // Tests that dispatchers receive events in the correct order. 177 // Tests that dispatchers receive events in the correct order.
179 TEST_F(PlatformEventTest, DispatcherOrder) { 178 TEST_F(PlatformEventTest, DispatcherOrder) {
180 std::vector<int> list_dispatcher; 179 std::vector<int> list_dispatcher;
181 int sequence[] = {21, 3, 6, 45}; 180 int sequence[] = {21, 3, 6, 45};
182 ScopedVector<TestPlatformEventDispatcher> dispatchers; 181 std::vector<std::unique_ptr<TestPlatformEventDispatcher>> dispatchers;
183 for (size_t i = 0; i < arraysize(sequence); ++i) { 182 for (auto id : sequence) {
184 dispatchers.push_back( 183 dispatchers.push_back(
185 new TestPlatformEventDispatcher(sequence[i], &list_dispatcher)); 184 base::MakeUnique<TestPlatformEventDispatcher>(id, &list_dispatcher));
186 } 185 }
187 std::unique_ptr<PlatformEvent> event(CreatePlatformEvent()); 186 std::unique_ptr<PlatformEvent> event = CreatePlatformEvent();
188 source()->Dispatch(*event); 187 source()->Dispatch(*event);
189 ASSERT_EQ(arraysize(sequence), list_dispatcher.size()); 188 ASSERT_EQ(arraysize(sequence), list_dispatcher.size());
190 EXPECT_EQ(std::vector<int>(sequence, sequence + arraysize(sequence)), 189 EXPECT_EQ(std::vector<int>(sequence, sequence + arraysize(sequence)),
191 list_dispatcher); 190 list_dispatcher);
192 } 191 }
193 192
194 // Tests that if a dispatcher consumes the event, the subsequent dispatchers do 193 // Tests that if a dispatcher consumes the event, the subsequent dispatchers do
195 // not receive the event. 194 // not receive the event.
196 TEST_F(PlatformEventTest, DispatcherConsumesEventToStopDispatch) { 195 TEST_F(PlatformEventTest, DispatcherConsumesEventToStopDispatch) {
197 std::vector<int> list_dispatcher; 196 std::vector<int> list_dispatcher;
198 TestPlatformEventDispatcher first(12, &list_dispatcher); 197 TestPlatformEventDispatcher first(12, &list_dispatcher);
199 TestPlatformEventDispatcher second(23, &list_dispatcher); 198 TestPlatformEventDispatcher second(23, &list_dispatcher);
200 199
201 std::unique_ptr<PlatformEvent> event(CreatePlatformEvent()); 200 std::unique_ptr<PlatformEvent> event = CreatePlatformEvent();
202 source()->Dispatch(*event); 201 source()->Dispatch(*event);
203 ASSERT_EQ(2u, list_dispatcher.size()); 202 ASSERT_EQ(2u, list_dispatcher.size());
204 EXPECT_EQ(12, list_dispatcher[0]); 203 EXPECT_EQ(12, list_dispatcher[0]);
205 EXPECT_EQ(23, list_dispatcher[1]); 204 EXPECT_EQ(23, list_dispatcher[1]);
206 list_dispatcher.clear(); 205 list_dispatcher.clear();
207 206
208 first.set_post_dispatch_action(POST_DISPATCH_STOP_PROPAGATION); 207 first.set_post_dispatch_action(POST_DISPATCH_STOP_PROPAGATION);
209 event = CreatePlatformEvent(); 208 event = CreatePlatformEvent();
210 source()->Dispatch(*event); 209 source()->Dispatch(*event);
211 ASSERT_EQ(1u, list_dispatcher.size()); 210 ASSERT_EQ(1u, list_dispatcher.size());
212 EXPECT_EQ(12, list_dispatcher[0]); 211 EXPECT_EQ(12, list_dispatcher[0]);
213 } 212 }
214 213
215 // Tests that observers receive events. 214 // Tests that observers receive events.
216 TEST_F(PlatformEventTest, ObserverBasic) { 215 TEST_F(PlatformEventTest, ObserverBasic) {
217 std::vector<int> list_observer; 216 std::vector<int> list_observer;
218 std::unique_ptr<PlatformEvent> event(CreatePlatformEvent()); 217 std::unique_ptr<PlatformEvent> event = CreatePlatformEvent();
219 source()->Dispatch(*event); 218 source()->Dispatch(*event);
220 EXPECT_EQ(0u, list_observer.size()); 219 EXPECT_EQ(0u, list_observer.size());
221 { 220 {
222 TestPlatformEventObserver observer(31, &list_observer); 221 TestPlatformEventObserver observer(31, &list_observer);
223 222
224 std::unique_ptr<PlatformEvent> event(CreatePlatformEvent()); 223 std::unique_ptr<PlatformEvent> event = CreatePlatformEvent();
225 source()->Dispatch(*event); 224 source()->Dispatch(*event);
226 ASSERT_EQ(1u, list_observer.size()); 225 ASSERT_EQ(1u, list_observer.size());
227 EXPECT_EQ(31, list_observer[0]); 226 EXPECT_EQ(31, list_observer[0]);
228 } 227 }
229 228
230 list_observer.clear(); 229 list_observer.clear();
231 event = CreatePlatformEvent(); 230 event = CreatePlatformEvent();
232 source()->Dispatch(*event); 231 source()->Dispatch(*event);
233 EXPECT_EQ(0u, list_observer.size()); 232 EXPECT_EQ(0u, list_observer.size());
234 } 233 }
235 234
236 // Tests that observers receive events in the correct order. 235 // Tests that observers receive events in the correct order.
237 TEST_F(PlatformEventTest, ObserverOrder) { 236 TEST_F(PlatformEventTest, ObserverOrder) {
238 std::vector<int> list_observer; 237 std::vector<int> list_observer;
239 const int sequence[] = {21, 3, 6, 45}; 238 const int sequence[] = {21, 3, 6, 45};
240 ScopedVector<TestPlatformEventObserver> observers; 239 std::vector<std::unique_ptr<TestPlatformEventObserver>> observers;
241 for (size_t i = 0; i < arraysize(sequence); ++i) { 240 for (auto id : sequence) {
242 observers.push_back( 241 observers.push_back(
243 new TestPlatformEventObserver(sequence[i], &list_observer)); 242 base::MakeUnique<TestPlatformEventObserver>(id, &list_observer));
244 } 243 }
245 std::unique_ptr<PlatformEvent> event(CreatePlatformEvent()); 244 std::unique_ptr<PlatformEvent> event = CreatePlatformEvent();
246 source()->Dispatch(*event); 245 source()->Dispatch(*event);
247 ASSERT_EQ(arraysize(sequence), list_observer.size()); 246 ASSERT_EQ(arraysize(sequence), list_observer.size());
248 EXPECT_EQ(std::vector<int>(sequence, sequence + arraysize(sequence)), 247 EXPECT_EQ(std::vector<int>(sequence, sequence + arraysize(sequence)),
249 list_observer); 248 list_observer);
250 } 249 }
251 250
252 // Tests that observers and dispatchers receive events in the correct order. 251 // Tests that observers and dispatchers receive events in the correct order.
253 TEST_F(PlatformEventTest, DispatcherAndObserverOrder) { 252 TEST_F(PlatformEventTest, DispatcherAndObserverOrder) {
254 std::vector<int> list; 253 std::vector<int> list;
255 TestPlatformEventDispatcher first_d(12, &list); 254 TestPlatformEventDispatcher first_d(12, &list);
256 TestPlatformEventObserver first_o(10, &list); 255 TestPlatformEventObserver first_o(10, &list);
257 TestPlatformEventDispatcher second_d(23, &list); 256 TestPlatformEventDispatcher second_d(23, &list);
258 TestPlatformEventObserver second_o(20, &list); 257 TestPlatformEventObserver second_o(20, &list);
259 std::unique_ptr<PlatformEvent> event(CreatePlatformEvent()); 258 std::unique_ptr<PlatformEvent> event = CreatePlatformEvent();
260 source()->Dispatch(*event); 259 source()->Dispatch(*event);
261 const int expected[] = {10, 20, 12, 23}; 260 const int expected[] = {10, 20, 12, 23};
262 EXPECT_EQ(std::vector<int>(expected, expected + arraysize(expected)), list); 261 EXPECT_EQ(std::vector<int>(expected, expected + arraysize(expected)), list);
263 } 262 }
264 263
265 // Tests that an overridden dispatcher receives events before the default 264 // Tests that an overridden dispatcher receives events before the default
266 // dispatchers. 265 // dispatchers.
267 TEST_F(PlatformEventTest, OverriddenDispatcherBasic) { 266 TEST_F(PlatformEventTest, OverriddenDispatcherBasic) {
268 std::vector<int> list; 267 std::vector<int> list;
269 TestPlatformEventDispatcher dispatcher(10, &list); 268 TestPlatformEventDispatcher dispatcher(10, &list);
270 TestPlatformEventObserver observer(15, &list); 269 TestPlatformEventObserver observer(15, &list);
271 std::unique_ptr<PlatformEvent> event(CreatePlatformEvent()); 270 std::unique_ptr<PlatformEvent> event = CreatePlatformEvent();
272 source()->Dispatch(*event); 271 source()->Dispatch(*event);
273 ASSERT_EQ(2u, list.size()); 272 ASSERT_EQ(2u, list.size());
274 EXPECT_EQ(15, list[0]); 273 EXPECT_EQ(15, list[0]);
275 EXPECT_EQ(10, list[1]); 274 EXPECT_EQ(10, list[1]);
276 list.clear(); 275 list.clear();
277 276
278 TestPlatformEventDispatcher overriding_dispatcher(20, &list); 277 TestPlatformEventDispatcher overriding_dispatcher(20, &list);
279 source()->RemovePlatformEventDispatcher(&overriding_dispatcher); 278 source()->RemovePlatformEventDispatcher(&overriding_dispatcher);
280 std::unique_ptr<ScopedEventDispatcher> handle = 279 std::unique_ptr<ScopedEventDispatcher> handle =
281 source()->OverrideDispatcher(&overriding_dispatcher); 280 source()->OverrideDispatcher(&overriding_dispatcher);
282 source()->Dispatch(*event); 281 source()->Dispatch(*event);
283 ASSERT_EQ(2u, list.size()); 282 ASSERT_EQ(2u, list.size());
284 EXPECT_EQ(15, list[0]); 283 EXPECT_EQ(15, list[0]);
285 EXPECT_EQ(20, list[1]); 284 EXPECT_EQ(20, list[1]);
286 } 285 }
287 286
288 // Tests that an overridden dispatcher can request that the default dispatchers 287 // Tests that an overridden dispatcher can request that the default dispatchers
289 // can dispatch the events. 288 // can dispatch the events.
290 TEST_F(PlatformEventTest, OverriddenDispatcherInvokeDefaultDispatcher) { 289 TEST_F(PlatformEventTest, OverriddenDispatcherInvokeDefaultDispatcher) {
291 std::vector<int> list; 290 std::vector<int> list;
292 TestPlatformEventDispatcher dispatcher(10, &list); 291 TestPlatformEventDispatcher dispatcher(10, &list);
293 TestPlatformEventObserver observer(15, &list); 292 TestPlatformEventObserver observer(15, &list);
294 TestPlatformEventDispatcher overriding_dispatcher(20, &list); 293 TestPlatformEventDispatcher overriding_dispatcher(20, &list);
295 source()->RemovePlatformEventDispatcher(&overriding_dispatcher); 294 source()->RemovePlatformEventDispatcher(&overriding_dispatcher);
296 std::unique_ptr<ScopedEventDispatcher> handle = 295 std::unique_ptr<ScopedEventDispatcher> handle =
297 source()->OverrideDispatcher(&overriding_dispatcher); 296 source()->OverrideDispatcher(&overriding_dispatcher);
298 overriding_dispatcher.set_post_dispatch_action(POST_DISPATCH_PERFORM_DEFAULT); 297 overriding_dispatcher.set_post_dispatch_action(POST_DISPATCH_PERFORM_DEFAULT);
299 298
300 std::unique_ptr<PlatformEvent> event(CreatePlatformEvent()); 299 std::unique_ptr<PlatformEvent> event = CreatePlatformEvent();
301 source()->Dispatch(*event); 300 source()->Dispatch(*event);
302 // First the observer, then the overriding dispatcher, then the default 301 // First the observer, then the overriding dispatcher, then the default
303 // dispatcher. 302 // dispatcher.
304 ASSERT_EQ(3u, list.size()); 303 ASSERT_EQ(3u, list.size());
305 EXPECT_EQ(15, list[0]); 304 EXPECT_EQ(15, list[0]);
306 EXPECT_EQ(20, list[1]); 305 EXPECT_EQ(20, list[1]);
307 EXPECT_EQ(10, list[2]); 306 EXPECT_EQ(10, list[2]);
308 list.clear(); 307 list.clear();
309 308
310 // Install a second overriding dispatcher. 309 // Install a second overriding dispatcher.
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
358 // continues correctly. 357 // continues correctly.
359 TEST_F(PlatformEventTest, DispatcherRemovesNextDispatcherDuringDispatch) { 358 TEST_F(PlatformEventTest, DispatcherRemovesNextDispatcherDuringDispatch) {
360 std::vector<int> list; 359 std::vector<int> list;
361 TestPlatformEventDispatcher first(10, &list); 360 TestPlatformEventDispatcher first(10, &list);
362 RunCallbackDuringDispatch second(15, &list); 361 RunCallbackDuringDispatch second(15, &list);
363 TestPlatformEventDispatcher third(20, &list); 362 TestPlatformEventDispatcher third(20, &list);
364 TestPlatformEventDispatcher fourth(30, &list); 363 TestPlatformEventDispatcher fourth(30, &list);
365 364
366 second.set_callback(base::Bind(&RemoveDispatcher, base::Unretained(&third))); 365 second.set_callback(base::Bind(&RemoveDispatcher, base::Unretained(&third)));
367 366
368 std::unique_ptr<PlatformEvent> event(CreatePlatformEvent()); 367 std::unique_ptr<PlatformEvent> event = CreatePlatformEvent();
369 source()->Dispatch(*event); 368 source()->Dispatch(*event);
370 // |second| removes |third| from the dispatcher list during dispatch. So the 369 // |second| removes |third| from the dispatcher list during dispatch. So the
371 // event should only reach |first|, |second|, and |fourth|. 370 // event should only reach |first|, |second|, and |fourth|.
372 ASSERT_EQ(3u, list.size()); 371 ASSERT_EQ(3u, list.size());
373 EXPECT_EQ(10, list[0]); 372 EXPECT_EQ(10, list[0]);
374 EXPECT_EQ(15, list[1]); 373 EXPECT_EQ(15, list[1]);
375 EXPECT_EQ(30, list[2]); 374 EXPECT_EQ(30, list[2]);
376 } 375 }
377 376
378 // Tests that if a dispatcher removes itself from the dispatcher list during 377 // Tests that if a dispatcher removes itself from the dispatcher list during
379 // dispatching an event, then event dispatching continues correctly. 378 // dispatching an event, then event dispatching continues correctly.
380 TEST_F(PlatformEventTest, DispatcherRemovesSelfDuringDispatch) { 379 TEST_F(PlatformEventTest, DispatcherRemovesSelfDuringDispatch) {
381 std::vector<int> list; 380 std::vector<int> list;
382 TestPlatformEventDispatcher first(10, &list); 381 TestPlatformEventDispatcher first(10, &list);
383 RunCallbackDuringDispatch second(15, &list); 382 RunCallbackDuringDispatch second(15, &list);
384 TestPlatformEventDispatcher third(20, &list); 383 TestPlatformEventDispatcher third(20, &list);
385 384
386 second.set_callback(base::Bind(&RemoveDispatcher, base::Unretained(&second))); 385 second.set_callback(base::Bind(&RemoveDispatcher, base::Unretained(&second)));
387 386
388 std::unique_ptr<PlatformEvent> event(CreatePlatformEvent()); 387 std::unique_ptr<PlatformEvent> event = CreatePlatformEvent();
389 source()->Dispatch(*event); 388 source()->Dispatch(*event);
390 // |second| removes itself from the dispatcher list during dispatch. So the 389 // |second| removes itself from the dispatcher list during dispatch. So the
391 // event should reach all three dispatchers in the list. 390 // event should reach all three dispatchers in the list.
392 ASSERT_EQ(3u, list.size()); 391 ASSERT_EQ(3u, list.size());
393 EXPECT_EQ(10, list[0]); 392 EXPECT_EQ(10, list[0]);
394 EXPECT_EQ(15, list[1]); 393 EXPECT_EQ(15, list[1]);
395 EXPECT_EQ(20, list[2]); 394 EXPECT_EQ(20, list[2]);
396 } 395 }
397 396
398 // Tests that if a dispatcher removes itself from the dispatcher list during 397 // Tests that if a dispatcher removes itself from the dispatcher list during
399 // dispatching an event, and this dispatcher is last in the dispatcher-list, 398 // dispatching an event, and this dispatcher is last in the dispatcher-list,
400 // then event dispatching ends correctly. 399 // then event dispatching ends correctly.
401 TEST_F(PlatformEventTest, DispatcherRemovesSelfDuringDispatchLast) { 400 TEST_F(PlatformEventTest, DispatcherRemovesSelfDuringDispatchLast) {
402 std::vector<int> list; 401 std::vector<int> list;
403 TestPlatformEventDispatcher first(10, &list); 402 TestPlatformEventDispatcher first(10, &list);
404 RunCallbackDuringDispatch second(15, &list); 403 RunCallbackDuringDispatch second(15, &list);
405 404
406 second.set_callback(base::Bind(&RemoveDispatcher, base::Unretained(&second))); 405 second.set_callback(base::Bind(&RemoveDispatcher, base::Unretained(&second)));
407 406
408 std::unique_ptr<PlatformEvent> event(CreatePlatformEvent()); 407 std::unique_ptr<PlatformEvent> event = CreatePlatformEvent();
409 source()->Dispatch(*event); 408 source()->Dispatch(*event);
410 // |second| removes itself during dispatch. So both dispatchers will have 409 // |second| removes itself during dispatch. So both dispatchers will have
411 // received the event. 410 // received the event.
412 ASSERT_EQ(2u, list.size()); 411 ASSERT_EQ(2u, list.size());
413 EXPECT_EQ(10, list[0]); 412 EXPECT_EQ(10, list[0]);
414 EXPECT_EQ(15, list[1]); 413 EXPECT_EQ(15, list[1]);
415 } 414 }
416 415
417 // Tests that if a dispatcher removes a single dispatcher that comes before it 416 // Tests that if a dispatcher removes a single dispatcher that comes before it
418 // in the dispatcher list, then dispatch continues correctly. 417 // in the dispatcher list, then dispatch continues correctly.
419 TEST_F(PlatformEventTest, DispatcherRemovesPrevDispatcherDuringDispatch) { 418 TEST_F(PlatformEventTest, DispatcherRemovesPrevDispatcherDuringDispatch) {
420 std::vector<int> list; 419 std::vector<int> list;
421 TestPlatformEventDispatcher first(10, &list); 420 TestPlatformEventDispatcher first(10, &list);
422 RunCallbackDuringDispatch second(15, &list); 421 RunCallbackDuringDispatch second(15, &list);
423 TestPlatformEventDispatcher third(20, &list); 422 TestPlatformEventDispatcher third(20, &list);
424 423
425 second.set_callback(base::Bind(&RemoveDispatcher, base::Unretained(&first))); 424 second.set_callback(base::Bind(&RemoveDispatcher, base::Unretained(&first)));
426 425
427 std::unique_ptr<PlatformEvent> event(CreatePlatformEvent()); 426 std::unique_ptr<PlatformEvent> event = CreatePlatformEvent();
428 source()->Dispatch(*event); 427 source()->Dispatch(*event);
429 // |second| removes |first| from the dispatcher list during dispatch. The 428 // |second| removes |first| from the dispatcher list during dispatch. The
430 // event should reach all three dispatchers. 429 // event should reach all three dispatchers.
431 ASSERT_EQ(3u, list.size()); 430 ASSERT_EQ(3u, list.size());
432 EXPECT_EQ(10, list[0]); 431 EXPECT_EQ(10, list[0]);
433 EXPECT_EQ(15, list[1]); 432 EXPECT_EQ(15, list[1]);
434 EXPECT_EQ(20, list[2]); 433 EXPECT_EQ(20, list[2]);
435 } 434 }
436 435
437 // Tests that if a dispatcher removes multiple dispatchers that comes before it 436 // Tests that if a dispatcher removes multiple dispatchers that comes before it
438 // in the dispatcher list, then dispatch continues correctly. 437 // in the dispatcher list, then dispatch continues correctly.
439 TEST_F(PlatformEventTest, DispatcherRemovesPrevDispatchersDuringDispatch) { 438 TEST_F(PlatformEventTest, DispatcherRemovesPrevDispatchersDuringDispatch) {
440 std::vector<int> list; 439 std::vector<int> list;
441 TestPlatformEventDispatcher first(10, &list); 440 TestPlatformEventDispatcher first(10, &list);
442 TestPlatformEventDispatcher second(12, &list); 441 TestPlatformEventDispatcher second(12, &list);
443 RunCallbackDuringDispatch third(15, &list); 442 RunCallbackDuringDispatch third(15, &list);
444 TestPlatformEventDispatcher fourth(20, &list); 443 TestPlatformEventDispatcher fourth(20, &list);
445 444
446 third.set_callback(base::Bind(&RemoveDispatchers, 445 third.set_callback(base::Bind(&RemoveDispatchers,
447 base::Unretained(&first), 446 base::Unretained(&first),
448 base::Unretained(&second))); 447 base::Unretained(&second)));
449 448
450 std::unique_ptr<PlatformEvent> event(CreatePlatformEvent()); 449 std::unique_ptr<PlatformEvent> event = CreatePlatformEvent();
451 source()->Dispatch(*event); 450 source()->Dispatch(*event);
452 // |third| removes |first| and |second| from the dispatcher list during 451 // |third| removes |first| and |second| from the dispatcher list during
453 // dispatch. The event should reach all three dispatchers. 452 // dispatch. The event should reach all three dispatchers.
454 ASSERT_EQ(4u, list.size()); 453 ASSERT_EQ(4u, list.size());
455 EXPECT_EQ(10, list[0]); 454 EXPECT_EQ(10, list[0]);
456 EXPECT_EQ(12, list[1]); 455 EXPECT_EQ(12, list[1]);
457 EXPECT_EQ(15, list[2]); 456 EXPECT_EQ(15, list[2]);
458 EXPECT_EQ(20, list[3]); 457 EXPECT_EQ(20, list[3]);
459 } 458 }
460 459
461 // Tests that adding a dispatcher during dispatching an event receives that 460 // Tests that adding a dispatcher during dispatching an event receives that
462 // event. 461 // event.
463 TEST_F(PlatformEventTest, DispatcherAddedDuringDispatchReceivesEvent) { 462 TEST_F(PlatformEventTest, DispatcherAddedDuringDispatchReceivesEvent) {
464 std::vector<int> list; 463 std::vector<int> list;
465 TestPlatformEventDispatcher first(10, &list); 464 TestPlatformEventDispatcher first(10, &list);
466 RunCallbackDuringDispatch second(15, &list); 465 RunCallbackDuringDispatch second(15, &list);
467 TestPlatformEventDispatcher third(20, &list); 466 TestPlatformEventDispatcher third(20, &list);
468 TestPlatformEventDispatcher fourth(30, &list); 467 TestPlatformEventDispatcher fourth(30, &list);
469 RemoveDispatchers(&third, &fourth); 468 RemoveDispatchers(&third, &fourth);
470 469
471 std::unique_ptr<PlatformEvent> event(CreatePlatformEvent()); 470 std::unique_ptr<PlatformEvent> event = CreatePlatformEvent();
472 source()->Dispatch(*event); 471 source()->Dispatch(*event);
473 ASSERT_EQ(2u, list.size()); 472 ASSERT_EQ(2u, list.size());
474 EXPECT_EQ(10, list[0]); 473 EXPECT_EQ(10, list[0]);
475 EXPECT_EQ(15, list[1]); 474 EXPECT_EQ(15, list[1]);
476 475
477 second.set_callback(base::Bind(&AddDispatcher, base::Unretained(&third))); 476 second.set_callback(base::Bind(&AddDispatcher, base::Unretained(&third)));
478 list.clear(); 477 list.clear();
479 source()->Dispatch(*event); 478 source()->Dispatch(*event);
480 ASSERT_EQ(3u, list.size()); 479 ASSERT_EQ(3u, list.size());
481 EXPECT_EQ(10, list[0]); 480 EXPECT_EQ(10, list[0]);
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
532 source()->RemovePlatformEventDispatcher(&first_overriding); 531 source()->RemovePlatformEventDispatcher(&first_overriding);
533 std::unique_ptr<ScopedEventDispatcher> first_override_handle = 532 std::unique_ptr<ScopedEventDispatcher> first_override_handle =
534 source()->OverrideDispatcher(&first_overriding); 533 source()->OverrideDispatcher(&first_overriding);
535 534
536 // Install a second overriding dispatcher. 535 // Install a second overriding dispatcher.
537 TestPlatformEventDispatcher second_overriding(50, &list); 536 TestPlatformEventDispatcher second_overriding(50, &list);
538 source()->RemovePlatformEventDispatcher(&second_overriding); 537 source()->RemovePlatformEventDispatcher(&second_overriding);
539 std::unique_ptr<ScopedEventDispatcher> second_override_handle = 538 std::unique_ptr<ScopedEventDispatcher> second_override_handle =
540 source()->OverrideDispatcher(&second_overriding); 539 source()->OverrideDispatcher(&second_overriding);
541 540
542 std::unique_ptr<PlatformEvent> event(CreatePlatformEvent()); 541 std::unique_ptr<PlatformEvent> event = CreatePlatformEvent();
543 source()->Dispatch(*event); 542 source()->Dispatch(*event);
544 ASSERT_EQ(2u, list.size()); 543 ASSERT_EQ(2u, list.size());
545 EXPECT_EQ(15, list[0]); 544 EXPECT_EQ(15, list[0]);
546 EXPECT_EQ(50, list[1]); 545 EXPECT_EQ(50, list[1]);
547 list.clear(); 546 list.clear();
548 547
549 second_override_handle.reset(); 548 second_override_handle.reset();
550 source()->Dispatch(*event); 549 source()->Dispatch(*event);
551 ASSERT_EQ(2u, list.size()); 550 ASSERT_EQ(2u, list.size());
552 EXPECT_EQ(15, list[0]); 551 EXPECT_EQ(15, list[0]);
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
593 }; 592 };
594 593
595 // Tests that resetting an overridden dispatcher causes the nested message-loop 594 // Tests that resetting an overridden dispatcher causes the nested message-loop
596 // iteration to stop and the rest of the events are dispatched in the next 595 // iteration to stop and the rest of the events are dispatched in the next
597 // iteration. 596 // iteration.
598 class DestroyedNestedOverriddenDispatcherQuitsNestedLoopIteration 597 class DestroyedNestedOverriddenDispatcherQuitsNestedLoopIteration
599 : public PlatformEventTestWithMessageLoop { 598 : public PlatformEventTestWithMessageLoop {
600 public: 599 public:
601 void NestedTask(std::vector<int>* list, 600 void NestedTask(std::vector<int>* list,
602 TestPlatformEventDispatcher* dispatcher) { 601 TestPlatformEventDispatcher* dispatcher) {
603 ScopedVector<PlatformEvent> events; 602 std::vector<std::unique_ptr<PlatformEvent>> events;
604 std::unique_ptr<PlatformEvent> event(CreatePlatformEvent()); 603 events.push_back(CreatePlatformEvent());
605 events.push_back(std::move(event)); 604 events.push_back(CreatePlatformEvent());
606 event = CreatePlatformEvent();
607 events.push_back(std::move(event));
608 605
609 // Attempt to dispatch a couple of events. Dispatching the first event will 606 // Attempt to dispatch a couple of events. Dispatching the first event will
610 // have terminated the ScopedEventDispatcher object, which will terminate 607 // have terminated the ScopedEventDispatcher object, which will terminate
611 // the current iteration of the message-loop. 608 // the current iteration of the message-loop.
612 size_t count = source()->DispatchEventStream(events); 609 size_t count = source()->DispatchEventStream(events);
613 EXPECT_EQ(1u, count); 610 EXPECT_EQ(1u, count);
614 ASSERT_EQ(2u, list->size()); 611 ASSERT_EQ(2u, list->size());
615 EXPECT_EQ(15, (*list)[0]); 612 EXPECT_EQ(15, (*list)[0]);
616 EXPECT_EQ(20, (*list)[1]); 613 EXPECT_EQ(20, (*list)[1]);
617 list->clear(); 614 list->clear();
(...skipping 16 matching lines...) Expand all
634 void RunTestImpl() override { 631 void RunTestImpl() override {
635 std::vector<int> list; 632 std::vector<int> list;
636 TestPlatformEventDispatcher dispatcher(10, &list); 633 TestPlatformEventDispatcher dispatcher(10, &list);
637 TestPlatformEventObserver observer(15, &list); 634 TestPlatformEventObserver observer(15, &list);
638 635
639 DestroyScopedHandleDispatcher overriding(20, &list); 636 DestroyScopedHandleDispatcher overriding(20, &list);
640 source()->RemovePlatformEventDispatcher(&overriding); 637 source()->RemovePlatformEventDispatcher(&overriding);
641 std::unique_ptr<ScopedEventDispatcher> override_handle = 638 std::unique_ptr<ScopedEventDispatcher> override_handle =
642 source()->OverrideDispatcher(&overriding); 639 source()->OverrideDispatcher(&overriding);
643 640
644 std::unique_ptr<PlatformEvent> event(CreatePlatformEvent()); 641 std::unique_ptr<PlatformEvent> event = CreatePlatformEvent();
645 source()->Dispatch(*event); 642 source()->Dispatch(*event);
646 ASSERT_EQ(2u, list.size()); 643 ASSERT_EQ(2u, list.size());
647 EXPECT_EQ(15, list[0]); 644 EXPECT_EQ(15, list[0]);
648 EXPECT_EQ(20, list[1]); 645 EXPECT_EQ(20, list[1]);
649 list.clear(); 646 list.clear();
650 647
651 overriding.SetScopedHandle(std::move(override_handle)); 648 overriding.SetScopedHandle(std::move(override_handle));
652 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); 649 base::MessageLoopForUI* loop = base::MessageLoopForUI::current();
653 base::MessageLoopForUI::ScopedNestableTaskAllower allow_nested(loop); 650 base::MessageLoopForUI::ScopedNestableTaskAllower allow_nested(loop);
654 loop->task_runner()->PostTask( 651 loop->task_runner()->PostTask(
(...skipping 20 matching lines...) Expand all
675 DestroyedNestedOverriddenDispatcherQuitsNestedLoopIteration) 672 DestroyedNestedOverriddenDispatcherQuitsNestedLoopIteration)
676 673
677 // Tests that resetting an overridden dispatcher, and installing another 674 // Tests that resetting an overridden dispatcher, and installing another
678 // overridden dispatcher before the nested message-loop completely unwinds 675 // overridden dispatcher before the nested message-loop completely unwinds
679 // function correctly. 676 // function correctly.
680 class ConsecutiveOverriddenDispatcherInTheSameMessageLoopIteration 677 class ConsecutiveOverriddenDispatcherInTheSameMessageLoopIteration
681 : public PlatformEventTestWithMessageLoop { 678 : public PlatformEventTestWithMessageLoop {
682 public: 679 public:
683 void NestedTask(std::unique_ptr<ScopedEventDispatcher> dispatch_handle, 680 void NestedTask(std::unique_ptr<ScopedEventDispatcher> dispatch_handle,
684 std::vector<int>* list) { 681 std::vector<int>* list) {
685 std::unique_ptr<PlatformEvent> event(CreatePlatformEvent()); 682 std::unique_ptr<PlatformEvent> event = CreatePlatformEvent();
686 source()->Dispatch(*event); 683 source()->Dispatch(*event);
687 ASSERT_EQ(2u, list->size()); 684 ASSERT_EQ(2u, list->size());
688 EXPECT_EQ(15, (*list)[0]); 685 EXPECT_EQ(15, (*list)[0]);
689 EXPECT_EQ(20, (*list)[1]); 686 EXPECT_EQ(20, (*list)[1]);
690 list->clear(); 687 list->clear();
691 688
692 // Reset the override dispatcher. This should restore the default 689 // Reset the override dispatcher. This should restore the default
693 // dispatcher. 690 // dispatcher.
694 dispatch_handle.reset(); 691 dispatch_handle.reset();
695 source()->Dispatch(*event); 692 source()->Dispatch(*event);
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
734 void RunTestImpl() override { 731 void RunTestImpl() override {
735 std::vector<int> list; 732 std::vector<int> list;
736 TestPlatformEventDispatcher dispatcher(10, &list); 733 TestPlatformEventDispatcher dispatcher(10, &list);
737 TestPlatformEventObserver observer(15, &list); 734 TestPlatformEventObserver observer(15, &list);
738 735
739 TestPlatformEventDispatcher overriding(20, &list); 736 TestPlatformEventDispatcher overriding(20, &list);
740 source()->RemovePlatformEventDispatcher(&overriding); 737 source()->RemovePlatformEventDispatcher(&overriding);
741 std::unique_ptr<ScopedEventDispatcher> override_handle = 738 std::unique_ptr<ScopedEventDispatcher> override_handle =
742 source()->OverrideDispatcher(&overriding); 739 source()->OverrideDispatcher(&overriding);
743 740
744 std::unique_ptr<PlatformEvent> event(CreatePlatformEvent()); 741 std::unique_ptr<PlatformEvent> event = CreatePlatformEvent();
745 source()->Dispatch(*event); 742 source()->Dispatch(*event);
746 ASSERT_EQ(2u, list.size()); 743 ASSERT_EQ(2u, list.size());
747 EXPECT_EQ(15, list[0]); 744 EXPECT_EQ(15, list[0]);
748 EXPECT_EQ(20, list[1]); 745 EXPECT_EQ(20, list[1]);
749 list.clear(); 746 list.clear();
750 747
751 // Start a nested message-loop, and destroy |override_handle| in the nested 748 // Start a nested message-loop, and destroy |override_handle| in the nested
752 // loop. That should terminate the nested loop, restore the previous 749 // loop. That should terminate the nested loop, restore the previous
753 // dispatchers, and return control to this function. 750 // dispatchers, and return control to this function.
754 base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); 751 base::MessageLoopForUI* loop = base::MessageLoopForUI::current();
(...skipping 15 matching lines...) Expand all
770 } 767 }
771 768
772 private: 769 private:
773 base::RunLoop run_loop_; 770 base::RunLoop run_loop_;
774 }; 771 };
775 772
776 RUN_TEST_IN_MESSAGE_LOOP( 773 RUN_TEST_IN_MESSAGE_LOOP(
777 ConsecutiveOverriddenDispatcherInTheSameMessageLoopIteration) 774 ConsecutiveOverriddenDispatcherInTheSameMessageLoopIteration)
778 775
779 } // namespace ui 776 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698