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

Side by Side Diff: services/ui/ws/event_dispatcher_unittest.cc

Issue 2520093003: WindowManagerClient::AddAccelerator() should take an array (Closed)
Patch Set: Create anonymous namespace helper for AcceleratorTransport mojom struct. Cleanup/format codes. Created 4 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
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "services/ui/ws/event_dispatcher.h" 5 #include "services/ui/ws/event_dispatcher.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <queue> 10 #include <queue>
(...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after
378 } 378 }
379 379
380 TEST_F(EventDispatcherTest, AcceleratorBasic) { 380 TEST_F(EventDispatcherTest, AcceleratorBasic) {
381 ClearSetup(); 381 ClearSetup();
382 TestEventDispatcherDelegate event_dispatcher_delegate(nullptr); 382 TestEventDispatcherDelegate event_dispatcher_delegate(nullptr);
383 EventDispatcher dispatcher(&event_dispatcher_delegate); 383 EventDispatcher dispatcher(&event_dispatcher_delegate);
384 384
385 uint32_t accelerator_1 = 1; 385 uint32_t accelerator_1 = 1;
386 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher( 386 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher(
387 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown); 387 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown);
388 EXPECT_TRUE(dispatcher.AddAccelerator(accelerator_1, std::move(matcher))); 388
389 ui::mojom::AcceleratorTransportPtr accelerator_transport(
mfomitchev 2016/11/24 21:21:04 Try to get rid of the duplicated code here, e.g. b
thanhph 2016/11/29 00:08:29 Done, thanks! I use the helper defined in accelera
390 ui::mojom::AcceleratorTransport::New());
391 accelerator_transport->id = accelerator_1;
392 accelerator_transport->event_matcher = std::move(matcher);
393 std::vector<ui::mojom::AcceleratorTransportPtr> multi_accelerators_1;
394 multi_accelerators_1.push_back(std::move(accelerator_transport));
395
396 EXPECT_TRUE(dispatcher.AddAccelerators(std::move(multi_accelerators_1)));
389 397
390 uint32_t accelerator_2 = 2; 398 uint32_t accelerator_2 = 2;
391 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::N, 399 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::N,
392 ui::mojom::kEventFlagNone); 400 ui::mojom::kEventFlagNone);
393 EXPECT_TRUE(dispatcher.AddAccelerator(accelerator_2, std::move(matcher))); 401
402 accelerator_transport = ui::mojom::AcceleratorTransport::New();
403 accelerator_transport->id = accelerator_2;
404 accelerator_transport->event_matcher = std::move(matcher);
405 std::vector<ui::mojom::AcceleratorTransportPtr> multi_accelerators_2;
406 multi_accelerators_2.push_back(std::move(accelerator_transport));
407
408 EXPECT_TRUE(dispatcher.AddAccelerators(std::move(multi_accelerators_2)));
394 409
395 // Attempting to add a new accelerator with the same id should fail. 410 // Attempting to add a new accelerator with the same id should fail.
396 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::T, 411 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::T,
397 ui::mojom::kEventFlagNone); 412 ui::mojom::kEventFlagNone);
398 EXPECT_FALSE(dispatcher.AddAccelerator(accelerator_2, std::move(matcher))); 413 accelerator_transport = ui::mojom::AcceleratorTransport::New();
414 accelerator_transport->id = accelerator_2;
415 accelerator_transport->event_matcher = std::move(matcher);
416 std::vector<ui::mojom::AcceleratorTransportPtr> multi_accelerators_3;
417 multi_accelerators_3.push_back(std::move(accelerator_transport));
418 EXPECT_FALSE(dispatcher.AddAccelerators(std::move(multi_accelerators_3)));
399 419
400 // Adding the accelerator with the same id should succeed once the existing 420 // Adding the accelerator with the same id should succeed once the existing
401 // accelerator is removed. 421 // accelerator is removed.
402 dispatcher.RemoveAccelerator(accelerator_2); 422 dispatcher.RemoveAccelerator(accelerator_2);
403 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::T, 423 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::T,
404 ui::mojom::kEventFlagNone); 424 ui::mojom::kEventFlagNone);
405 EXPECT_TRUE(dispatcher.AddAccelerator(accelerator_2, std::move(matcher))); 425
426 accelerator_transport = ui::mojom::AcceleratorTransport::New();
427 accelerator_transport->id = accelerator_2;
428 accelerator_transport->event_matcher = std::move(matcher);
429 std::vector<ui::mojom::AcceleratorTransportPtr> multi_accelerators_4;
430 multi_accelerators_4.push_back(std::move(accelerator_transport));
431 EXPECT_TRUE(dispatcher.AddAccelerators(std::move(multi_accelerators_4)));
406 432
407 // Attempting to add an accelerator with the same matcher should fail. 433 // Attempting to add an accelerator with the same matcher should fail.
408 uint32_t accelerator_3 = 3; 434 uint32_t accelerator_3 = 3;
409 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::T, 435 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::T,
410 ui::mojom::kEventFlagNone); 436 ui::mojom::kEventFlagNone);
411 EXPECT_FALSE(dispatcher.AddAccelerator(accelerator_3, std::move(matcher))); 437 accelerator_transport = ui::mojom::AcceleratorTransport::New();
438 accelerator_transport->id = accelerator_3;
439 accelerator_transport->event_matcher = std::move(matcher);
440 std::vector<ui::mojom::AcceleratorTransportPtr> multi_accelerators_5;
441 multi_accelerators_5.push_back(std::move(accelerator_transport));
442 EXPECT_FALSE(dispatcher.AddAccelerators(std::move(multi_accelerators_5)));
412 443
413 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::T, 444 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::T,
414 ui::mojom::kEventFlagControlDown); 445 ui::mojom::kEventFlagControlDown);
415 EXPECT_TRUE(dispatcher.AddAccelerator(accelerator_3, std::move(matcher))); 446 accelerator_transport = ui::mojom::AcceleratorTransport::New();
447 accelerator_transport->id = accelerator_3;
448 accelerator_transport->event_matcher = std::move(matcher);
449 std::vector<ui::mojom::AcceleratorTransportPtr> multi_accelerators_6;
450 multi_accelerators_6.push_back(std::move(accelerator_transport));
451 EXPECT_TRUE(dispatcher.AddAccelerators(std::move(multi_accelerators_6)));
416 } 452 }
417 453
418 TEST_F(EventDispatcherTest, EventMatching) { 454 TEST_F(EventDispatcherTest, EventMatching) {
419 TestEventDispatcherDelegate* event_dispatcher_delegate = 455 TestEventDispatcherDelegate* event_dispatcher_delegate =
420 test_event_dispatcher_delegate(); 456 test_event_dispatcher_delegate();
421 EventDispatcher* dispatcher = event_dispatcher(); 457 EventDispatcher* dispatcher = event_dispatcher();
422 458
423 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher( 459 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher(
424 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown); 460 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown);
425 uint32_t accelerator_1 = 1; 461 uint32_t accelerator_1 = 1;
426 dispatcher->AddAccelerator(accelerator_1, std::move(matcher)); 462
463 ui::mojom::AcceleratorTransportPtr accelerator_transport(
464 ui::mojom::AcceleratorTransport::New());
465 accelerator_transport->id = accelerator_1;
466 accelerator_transport->event_matcher = std::move(matcher);
467 std::vector<ui::mojom::AcceleratorTransportPtr> multi_accelerators_1;
468 multi_accelerators_1.push_back(std::move(accelerator_transport));
469 dispatcher->AddAccelerators(std::move(multi_accelerators_1));
427 470
428 ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_CONTROL_DOWN); 471 ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_CONTROL_DOWN);
429 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 472 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
430 EXPECT_EQ(accelerator_1, 473 EXPECT_EQ(accelerator_1,
431 event_dispatcher_delegate->GetAndClearLastAccelerator()); 474 event_dispatcher_delegate->GetAndClearLastAccelerator());
432 475
433 // EF_NUM_LOCK_ON should be ignored since CreateKeyMatcher defaults to 476 // EF_NUM_LOCK_ON should be ignored since CreateKeyMatcher defaults to
434 // ignoring. 477 // ignoring.
435 key = ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_W, 478 key = ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_W,
436 ui::EF_CONTROL_DOWN | ui::EF_NUM_LOCK_ON); 479 ui::EF_CONTROL_DOWN | ui::EF_NUM_LOCK_ON);
437 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 480 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
438 EXPECT_EQ(accelerator_1, 481 EXPECT_EQ(accelerator_1,
439 event_dispatcher_delegate->GetAndClearLastAccelerator()); 482 event_dispatcher_delegate->GetAndClearLastAccelerator());
440 483
441 key = ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_NONE); 484 key = ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_NONE);
442 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 485 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
443 EXPECT_EQ(0u, event_dispatcher_delegate->GetAndClearLastAccelerator()); 486 EXPECT_EQ(0u, event_dispatcher_delegate->GetAndClearLastAccelerator());
444 487
445 uint32_t accelerator_2 = 2; 488 uint32_t accelerator_2 = 2;
446 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::W, 489 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::W,
447 ui::mojom::kEventFlagNone); 490 ui::mojom::kEventFlagNone);
448 dispatcher->AddAccelerator(accelerator_2, std::move(matcher)); 491 accelerator_transport = ui::mojom::AcceleratorTransport::New();
492 accelerator_transport->id = accelerator_2;
493 accelerator_transport->event_matcher = std::move(matcher);
494 std::vector<ui::mojom::AcceleratorTransportPtr> multi_accelerators_2;
495 multi_accelerators_2.push_back(std::move(accelerator_transport));
496 dispatcher->AddAccelerators(std::move(multi_accelerators_2));
449 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 497 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
450 EXPECT_EQ(accelerator_2, 498 EXPECT_EQ(accelerator_2,
451 event_dispatcher_delegate->GetAndClearLastAccelerator()); 499 event_dispatcher_delegate->GetAndClearLastAccelerator());
452 500
453 dispatcher->RemoveAccelerator(accelerator_2); 501 dispatcher->RemoveAccelerator(accelerator_2);
454 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 502 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
455 EXPECT_EQ(0u, event_dispatcher_delegate->GetAndClearLastAccelerator()); 503 EXPECT_EQ(0u, event_dispatcher_delegate->GetAndClearLastAccelerator());
456 } 504 }
457 505
458 // Tests that a post-target accelerator is not triggered by ProcessEvent. 506 // Tests that a post-target accelerator is not triggered by ProcessEvent.
459 TEST_F(EventDispatcherTest, PostTargetAccelerator) { 507 TEST_F(EventDispatcherTest, PostTargetAccelerator) {
460 TestEventDispatcherDelegate* event_dispatcher_delegate = 508 TestEventDispatcherDelegate* event_dispatcher_delegate =
461 test_event_dispatcher_delegate(); 509 test_event_dispatcher_delegate();
462 EventDispatcher* dispatcher = event_dispatcher(); 510 EventDispatcher* dispatcher = event_dispatcher();
463 511
464 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher( 512 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher(
465 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown); 513 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown);
466 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::POST_TARGET; 514 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::POST_TARGET;
467 uint32_t accelerator_1 = 1; 515 uint32_t accelerator_1 = 1;
468 dispatcher->AddAccelerator(accelerator_1, std::move(matcher)); 516 ui::mojom::AcceleratorTransportPtr accelerator_transport(
517 ui::mojom::AcceleratorTransport::New());
518 accelerator_transport->id = accelerator_1;
519 accelerator_transport->event_matcher = std::move(matcher);
520 std::vector<ui::mojom::AcceleratorTransportPtr> multi_accelerators_1;
521 multi_accelerators_1.push_back(std::move(accelerator_transport));
522 dispatcher->AddAccelerators(std::move(multi_accelerators_1));
469 523
470 ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_CONTROL_DOWN); 524 ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_CONTROL_DOWN);
471 // The post-target accelerator should be fired if there is no focused window. 525 // The post-target accelerator should be fired if there is no focused window.
472 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 526 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
473 EXPECT_EQ(accelerator_1, 527 EXPECT_EQ(accelerator_1,
474 event_dispatcher_delegate->GetAndClearLastAccelerator()); 528 event_dispatcher_delegate->GetAndClearLastAccelerator());
475 std::unique_ptr<DispatchedEventDetails> details = 529 std::unique_ptr<DispatchedEventDetails> details =
476 event_dispatcher_delegate->GetAndAdvanceDispatchedEventDetails(); 530 event_dispatcher_delegate->GetAndAdvanceDispatchedEventDetails();
477 EXPECT_FALSE(details); 531 EXPECT_FALSE(details);
478 532
(...skipping 24 matching lines...) Expand all
503 TEST_F(EventDispatcherTest, ProcessPost) { 557 TEST_F(EventDispatcherTest, ProcessPost) {
504 TestEventDispatcherDelegate* event_dispatcher_delegate = 558 TestEventDispatcherDelegate* event_dispatcher_delegate =
505 test_event_dispatcher_delegate(); 559 test_event_dispatcher_delegate();
506 EventDispatcher* dispatcher = event_dispatcher(); 560 EventDispatcher* dispatcher = event_dispatcher();
507 561
508 uint32_t pre_id = 1; 562 uint32_t pre_id = 1;
509 { 563 {
510 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher( 564 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher(
511 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown); 565 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown);
512 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::PRE_TARGET; 566 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::PRE_TARGET;
513 dispatcher->AddAccelerator(pre_id, std::move(matcher)); 567
568 ui::mojom::AcceleratorTransportPtr accelerator_transport(
569 ui::mojom::AcceleratorTransport::New());
570 accelerator_transport->id = pre_id;
571 accelerator_transport->event_matcher = std::move(matcher);
572 std::vector<ui::mojom::AcceleratorTransportPtr> multi_accelerators_1;
573 multi_accelerators_1.push_back(std::move(accelerator_transport));
574 dispatcher->AddAccelerators(std::move(multi_accelerators_1));
514 } 575 }
515 576
516 uint32_t post_id = 2; 577 uint32_t post_id = 2;
517 { 578 {
518 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher( 579 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher(
519 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown); 580 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown);
520 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::POST_TARGET; 581 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::POST_TARGET;
521 dispatcher->AddAccelerator(post_id, std::move(matcher)); 582 ui::mojom::AcceleratorTransportPtr accelerator_transport(
583 ui::mojom::AcceleratorTransport::New());
584 accelerator_transport->id = post_id;
585 accelerator_transport->event_matcher = std::move(matcher);
586 std::vector<ui::mojom::AcceleratorTransportPtr> multi_accelerators_2;
587 multi_accelerators_2.push_back(std::move(accelerator_transport));
588 dispatcher->AddAccelerators(std::move(multi_accelerators_2));
522 } 589 }
523 590
524 // Set focused window for EventDispatcher dispatches key events. 591 // Set focused window for EventDispatcher dispatches key events.
525 std::unique_ptr<ServerWindow> child = CreateChildWindow(WindowId(1, 3)); 592 std::unique_ptr<ServerWindow> child = CreateChildWindow(WindowId(1, 3));
526 event_dispatcher_delegate->SetFocusedWindowFromEventDispatcher(child.get()); 593 event_dispatcher_delegate->SetFocusedWindowFromEventDispatcher(child.get());
527 594
528 // Dispatch for ANY, which should trigger PRE and not call 595 // Dispatch for ANY, which should trigger PRE and not call
529 // DispatchInputEventToWindow(). 596 // DispatchInputEventToWindow().
530 ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_CONTROL_DOWN); 597 ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_CONTROL_DOWN);
531 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 598 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
(...skipping 1173 matching lines...) Expand 10 before | Expand all | Expand 10 after
1705 // The delegate can decide if it really wants to forward the event or not. 1772 // The delegate can decide if it really wants to forward the event or not.
1706 EXPECT_EQ(child.get(), 1773 EXPECT_EQ(child.get(),
1707 test_event_dispatcher_delegate()->lost_capture_window()); 1774 test_event_dispatcher_delegate()->lost_capture_window());
1708 EXPECT_EQ(child.get(), event_dispatcher()->capture_window()); 1775 EXPECT_EQ(child.get(), event_dispatcher()->capture_window());
1709 EXPECT_EQ(kClientAreaId, event_dispatcher()->capture_window_client_id()); 1776 EXPECT_EQ(kClientAreaId, event_dispatcher()->capture_window_client_id());
1710 } 1777 }
1711 1778
1712 } // namespace test 1779 } // namespace test
1713 } // namespace ws 1780 } // namespace ws
1714 } // namespace ui 1781 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698