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

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

Issue 2520093003: WindowManagerClient::AddAccelerator() should take an array (Closed)
Patch Set: Re-upload due to build.gn conflict. 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>
11 11
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/memory/weak_ptr.h" 13 #include "base/memory/weak_ptr.h"
14 #include "services/ui/common/event_matcher_util.h" 14 #include "services/ui/common/accelerator_util.h"
15 #include "services/ui/ws/accelerator.h" 15 #include "services/ui/ws/accelerator.h"
16 #include "services/ui/ws/event_dispatcher_delegate.h" 16 #include "services/ui/ws/event_dispatcher_delegate.h"
17 #include "services/ui/ws/server_window.h" 17 #include "services/ui/ws/server_window.h"
18 #include "services/ui/ws/server_window_compositor_frame_sink_manager_test_api.h" 18 #include "services/ui/ws/server_window_compositor_frame_sink_manager_test_api.h"
19 #include "services/ui/ws/test_server_window_delegate.h" 19 #include "services/ui/ws/test_server_window_delegate.h"
20 #include "services/ui/ws/test_utils.h" 20 #include "services/ui/ws/test_utils.h"
21 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "ui/events/event.h" 22 #include "ui/events/event.h"
23 23
24 namespace ui { 24 namespace ui {
(...skipping 353 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 EXPECT_TRUE(dispatcher.AddAccelerators(
389 CreateAcceleratorVector(accelerator_1, std::move(matcher))));
389 390
390 uint32_t accelerator_2 = 2; 391 uint32_t accelerator_2 = 2;
391 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::N, 392 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::N,
392 ui::mojom::kEventFlagNone); 393 ui::mojom::kEventFlagNone);
393 EXPECT_TRUE(dispatcher.AddAccelerator(accelerator_2, std::move(matcher))); 394 EXPECT_TRUE(dispatcher.AddAccelerators(
395 CreateAcceleratorVector(accelerator_2, std::move(matcher))));
394 396
395 // Attempting to add a new accelerator with the same id should fail. 397 // Attempting to add a new accelerator with the same id should fail.
396 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::T, 398 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::T,
397 ui::mojom::kEventFlagNone); 399 ui::mojom::kEventFlagNone);
398 EXPECT_FALSE(dispatcher.AddAccelerator(accelerator_2, std::move(matcher))); 400 EXPECT_FALSE(dispatcher.AddAccelerators(
401 CreateAcceleratorVector(accelerator_2, std::move(matcher))));
399 402
400 // Adding the accelerator with the same id should succeed once the existing 403 // Adding the accelerator with the same id should succeed once the existing
401 // accelerator is removed. 404 // accelerator is removed.
402 dispatcher.RemoveAccelerator(accelerator_2); 405 dispatcher.RemoveAccelerator(accelerator_2);
403 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::T, 406 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::T,
404 ui::mojom::kEventFlagNone); 407 ui::mojom::kEventFlagNone);
405 EXPECT_TRUE(dispatcher.AddAccelerator(accelerator_2, std::move(matcher))); 408 EXPECT_TRUE(dispatcher.AddAccelerators(
409 CreateAcceleratorVector(accelerator_2, std::move(matcher))));
406 410
407 // Attempting to add an accelerator with the same matcher should fail. 411 // Attempting to add an accelerator with the same matcher should fail.
408 uint32_t accelerator_3 = 3; 412 uint32_t accelerator_3 = 3;
409 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::T, 413 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::T,
410 ui::mojom::kEventFlagNone); 414 ui::mojom::kEventFlagNone);
411 EXPECT_FALSE(dispatcher.AddAccelerator(accelerator_3, std::move(matcher))); 415 EXPECT_FALSE(dispatcher.AddAccelerators(
416 CreateAcceleratorVector(accelerator_3, std::move(matcher))));
412 417
413 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::T, 418 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::T,
414 ui::mojom::kEventFlagControlDown); 419 ui::mojom::kEventFlagControlDown);
415 EXPECT_TRUE(dispatcher.AddAccelerator(accelerator_3, std::move(matcher))); 420 EXPECT_TRUE(dispatcher.AddAccelerators(
421 CreateAcceleratorVector(accelerator_3, std::move(matcher))));
416 } 422 }
417 423
418 TEST_F(EventDispatcherTest, EventMatching) { 424 TEST_F(EventDispatcherTest, EventMatching) {
419 TestEventDispatcherDelegate* event_dispatcher_delegate = 425 TestEventDispatcherDelegate* event_dispatcher_delegate =
420 test_event_dispatcher_delegate(); 426 test_event_dispatcher_delegate();
421 EventDispatcher* dispatcher = event_dispatcher(); 427 EventDispatcher* dispatcher = event_dispatcher();
422 428
423 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher( 429 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher(
424 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown); 430 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown);
425 uint32_t accelerator_1 = 1; 431 uint32_t accelerator_1 = 1;
426 dispatcher->AddAccelerator(accelerator_1, std::move(matcher)); 432 dispatcher->AddAccelerators(
433 CreateAcceleratorVector(accelerator_1, std::move(matcher)));
427 434
428 ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_CONTROL_DOWN); 435 ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_CONTROL_DOWN);
429 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 436 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
430 EXPECT_EQ(accelerator_1, 437 EXPECT_EQ(accelerator_1,
431 event_dispatcher_delegate->GetAndClearLastAccelerator()); 438 event_dispatcher_delegate->GetAndClearLastAccelerator());
432 439
433 // EF_NUM_LOCK_ON should be ignored since CreateKeyMatcher defaults to 440 // EF_NUM_LOCK_ON should be ignored since CreateKeyMatcher defaults to
434 // ignoring. 441 // ignoring.
435 key = ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_W, 442 key = ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_W,
436 ui::EF_CONTROL_DOWN | ui::EF_NUM_LOCK_ON); 443 ui::EF_CONTROL_DOWN | ui::EF_NUM_LOCK_ON);
437 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 444 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
438 EXPECT_EQ(accelerator_1, 445 EXPECT_EQ(accelerator_1,
439 event_dispatcher_delegate->GetAndClearLastAccelerator()); 446 event_dispatcher_delegate->GetAndClearLastAccelerator());
440 447
441 key = ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_NONE); 448 key = ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_NONE);
442 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 449 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
443 EXPECT_EQ(0u, event_dispatcher_delegate->GetAndClearLastAccelerator()); 450 EXPECT_EQ(0u, event_dispatcher_delegate->GetAndClearLastAccelerator());
444 451
445 uint32_t accelerator_2 = 2; 452 uint32_t accelerator_2 = 2;
446 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::W, 453 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::W,
447 ui::mojom::kEventFlagNone); 454 ui::mojom::kEventFlagNone);
448 dispatcher->AddAccelerator(accelerator_2, std::move(matcher)); 455 dispatcher->AddAccelerators(
456 CreateAcceleratorVector(accelerator_2, std::move(matcher)));
449 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 457 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
450 EXPECT_EQ(accelerator_2, 458 EXPECT_EQ(accelerator_2,
451 event_dispatcher_delegate->GetAndClearLastAccelerator()); 459 event_dispatcher_delegate->GetAndClearLastAccelerator());
452 460
453 dispatcher->RemoveAccelerator(accelerator_2); 461 dispatcher->RemoveAccelerator(accelerator_2);
454 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 462 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
455 EXPECT_EQ(0u, event_dispatcher_delegate->GetAndClearLastAccelerator()); 463 EXPECT_EQ(0u, event_dispatcher_delegate->GetAndClearLastAccelerator());
456 } 464 }
457 465
466 TEST_F(EventDispatcherTest, AddMultipleAccelerators) {
467 ClearSetup();
468 TestEventDispatcherDelegate event_dispatcher_delegate(nullptr);
469 EventDispatcher dispatcher(&event_dispatcher_delegate);
470
471 std::vector<ui::mojom::AcceleratorPtr> accelerators;
472
473 uint32_t accelerator_1 = 1;
474 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher(
475 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown);
476 accelerators.push_back(CreateAccelerator(accelerator_1, std::move(matcher)));
477
478 uint32_t accelerator_2 = 2;
479 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::N,
480 ui::mojom::kEventFlagNone);
481 accelerators.push_back(CreateAccelerator(accelerator_2, std::move(matcher)));
482
483 uint32_t accelerator_3 = 3;
484 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::T,
485 ui::mojom::kEventFlagNone);
486 accelerators.push_back(CreateAccelerator(accelerator_3, std::move(matcher)));
487
488 // Adding unique accelerators should pass.
489 EXPECT_TRUE(dispatcher.AddAccelerators(std::move(accelerators)));
490
491 ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_T, ui::EF_NONE);
492 dispatcher.ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
493 // check if accelerator_3 is added
mfomitchev 2016/12/01 20:51:51 Move this to before the line with key declaration.
thanhph 2016/12/01 21:26:37 Done.
494 EXPECT_EQ(accelerator_3,
495 event_dispatcher_delegate.GetAndClearLastAccelerator());
496
497 accelerators.clear();
498
499 uint32_t accelerator_4 = 4;
500 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::W,
501 ui::mojom::kEventFlagControlDown);
502 accelerators.push_back(CreateAccelerator(accelerator_4, std::move(matcher)));
503
504 uint32_t accelerator_6 = 6;
505 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::Q,
506 ui::mojom::kEventFlagControlDown);
507 accelerators.push_back(CreateAccelerator(accelerator_6, std::move(matcher)));
508
509 uint32_t accelerator_7 = 7;
510 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::R,
511 ui::mojom::kEventFlagControlDown);
512 accelerators.push_back(CreateAccelerator(accelerator_7, std::move(matcher)));
513
514 // Adding accelerator_4 with the same matcher as accelerator_1 should fail
515 // while accelerator_6 and accelerator_7 should be added.
516 EXPECT_FALSE(dispatcher.AddAccelerators(std::move(accelerators)));
517
518 // Check if the accelerator_7 was still added regardless of failling
519 // adding attempt of accelerator_4 in the accelerators vector.
520 key = ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_R, ui::EF_CONTROL_DOWN);
521 dispatcher.ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
522 EXPECT_EQ(accelerator_7,
523 event_dispatcher_delegate.GetAndClearLastAccelerator());
524
525 // Check if the accelerator_1 id hasn't changed because of accelerator_4
526 // adding attempt above.
527 key = ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_CONTROL_DOWN);
528 dispatcher.ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
529 EXPECT_EQ(accelerator_1,
530 event_dispatcher_delegate.GetAndClearLastAccelerator());
531 }
532
458 // Tests that a post-target accelerator is not triggered by ProcessEvent. 533 // Tests that a post-target accelerator is not triggered by ProcessEvent.
459 TEST_F(EventDispatcherTest, PostTargetAccelerator) { 534 TEST_F(EventDispatcherTest, PostTargetAccelerator) {
460 TestEventDispatcherDelegate* event_dispatcher_delegate = 535 TestEventDispatcherDelegate* event_dispatcher_delegate =
461 test_event_dispatcher_delegate(); 536 test_event_dispatcher_delegate();
462 EventDispatcher* dispatcher = event_dispatcher(); 537 EventDispatcher* dispatcher = event_dispatcher();
463 538
464 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher( 539 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher(
465 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown); 540 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown);
466 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::POST_TARGET; 541 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::POST_TARGET;
467 uint32_t accelerator_1 = 1; 542 uint32_t accelerator_1 = 1;
468 dispatcher->AddAccelerator(accelerator_1, std::move(matcher)); 543 dispatcher->AddAccelerators(
544 CreateAcceleratorVector(accelerator_1, std::move(matcher)));
469 545
470 ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_CONTROL_DOWN); 546 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. 547 // The post-target accelerator should be fired if there is no focused window.
472 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 548 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
473 EXPECT_EQ(accelerator_1, 549 EXPECT_EQ(accelerator_1,
474 event_dispatcher_delegate->GetAndClearLastAccelerator()); 550 event_dispatcher_delegate->GetAndClearLastAccelerator());
475 std::unique_ptr<DispatchedEventDetails> details = 551 std::unique_ptr<DispatchedEventDetails> details =
476 event_dispatcher_delegate->GetAndAdvanceDispatchedEventDetails(); 552 event_dispatcher_delegate->GetAndAdvanceDispatchedEventDetails();
477 EXPECT_FALSE(details); 553 EXPECT_FALSE(details);
478 554
(...skipping 24 matching lines...) Expand all
503 TEST_F(EventDispatcherTest, ProcessPost) { 579 TEST_F(EventDispatcherTest, ProcessPost) {
504 TestEventDispatcherDelegate* event_dispatcher_delegate = 580 TestEventDispatcherDelegate* event_dispatcher_delegate =
505 test_event_dispatcher_delegate(); 581 test_event_dispatcher_delegate();
506 EventDispatcher* dispatcher = event_dispatcher(); 582 EventDispatcher* dispatcher = event_dispatcher();
507 583
508 uint32_t pre_id = 1; 584 uint32_t pre_id = 1;
509 { 585 {
510 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher( 586 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher(
511 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown); 587 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown);
512 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::PRE_TARGET; 588 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::PRE_TARGET;
513 dispatcher->AddAccelerator(pre_id, std::move(matcher)); 589 dispatcher->AddAccelerators(
590 CreateAcceleratorVector(pre_id, std::move(matcher)));
514 } 591 }
515 592
516 uint32_t post_id = 2; 593 uint32_t post_id = 2;
517 { 594 {
518 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher( 595 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher(
519 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown); 596 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown);
520 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::POST_TARGET; 597 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::POST_TARGET;
521 dispatcher->AddAccelerator(post_id, std::move(matcher)); 598 dispatcher->AddAccelerators(
599 CreateAcceleratorVector(post_id, std::move(matcher)));
522 } 600 }
523 601
524 // Set focused window for EventDispatcher dispatches key events. 602 // Set focused window for EventDispatcher dispatches key events.
525 std::unique_ptr<ServerWindow> child = CreateChildWindow(WindowId(1, 3)); 603 std::unique_ptr<ServerWindow> child = CreateChildWindow(WindowId(1, 3));
526 event_dispatcher_delegate->SetFocusedWindowFromEventDispatcher(child.get()); 604 event_dispatcher_delegate->SetFocusedWindowFromEventDispatcher(child.get());
527 605
528 // Dispatch for ANY, which should trigger PRE and not call 606 // Dispatch for ANY, which should trigger PRE and not call
529 // DispatchInputEventToWindow(). 607 // DispatchInputEventToWindow().
530 ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_CONTROL_DOWN); 608 ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_CONTROL_DOWN);
531 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 609 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. 1783 // The delegate can decide if it really wants to forward the event or not.
1706 EXPECT_EQ(child.get(), 1784 EXPECT_EQ(child.get(),
1707 test_event_dispatcher_delegate()->lost_capture_window()); 1785 test_event_dispatcher_delegate()->lost_capture_window());
1708 EXPECT_EQ(child.get(), event_dispatcher()->capture_window()); 1786 EXPECT_EQ(child.get(), event_dispatcher()->capture_window());
1709 EXPECT_EQ(kClientAreaId, event_dispatcher()->capture_window_client_id()); 1787 EXPECT_EQ(kClientAreaId, event_dispatcher()->capture_window_client_id());
1710 } 1788 }
1711 1789
1712 } // namespace test 1790 } // namespace test
1713 } // namespace ws 1791 } // namespace ws
1714 } // namespace ui 1792 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698