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

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

Issue 2520093003: WindowManagerClient::AddAccelerator() should take an array (Closed)
Patch Set: Add/Update comments. Improve duplication accelerator test. 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
« no previous file with comments | « services/ui/ws/event_dispatcher.cc ('k') | services/ui/ws/window_manager_state.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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))));
422 }
423
424 TEST_F(EventDispatcherTest, AddAccelerators) {
425 ClearSetup();
426 TestEventDispatcherDelegate event_dispatcher_delegate(nullptr);
427 EventDispatcher dispatcher(&event_dispatcher_delegate);
428
429 std::vector<ui::mojom::AcceleratorTransportPtr> accelerators;
430
431 uint32_t accelerator_1 = 1;
432 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher(
433 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown);
434 accelerators.push_back(
435 CreateAcceleratorTransport(accelerator_1, std::move(matcher)));
436
437 uint32_t accelerator_2 = 2;
438 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::N,
439 ui::mojom::kEventFlagNone);
440 accelerators.push_back(
441 CreateAcceleratorTransport(accelerator_2, std::move(matcher)));
442
443 uint32_t accelerator_3 = 3;
444 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::T,
445 ui::mojom::kEventFlagNone);
446 accelerators.push_back(
447 CreateAcceleratorTransport(accelerator_3, std::move(matcher)));
448
449 // Adding unique accelerators should pass.
450 EXPECT_TRUE(dispatcher.AddAccelerators(std::move(accelerators)));
451
452 accelerators.clear();
453
454 uint32_t accelerator_4 = 4;
455 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::W,
456 ui::mojom::kEventFlagControlDown);
457 accelerators.push_back(
458 CreateAcceleratorTransport(accelerator_4, std::move(matcher)));
459
460 uint32_t accelerator_5 = 5;
461 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::W,
462 ui::mojom::kEventFlagControlDown);
463 accelerators.push_back(
464 CreateAcceleratorTransport(accelerator_5, std::move(matcher)));
465
466 uint32_t accelerator_6 = accelerator_5;
467 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::T,
468 ui::mojom::kEventFlagNone);
469 accelerators.push_back(
470 CreateAcceleratorTransport(accelerator_6, std::move(matcher)));
471
472 // Adding accelerator_6 with the same id accelerator_5 should fail.
473 EXPECT_FALSE(dispatcher.AddAccelerators(std::move(accelerators)));
474
475 accelerators.clear();
476
477 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::T,
478 ui::mojom::kEventFlagNone);
479 accelerators.push_back(
480 CreateAcceleratorTransport(accelerator_4, std::move(matcher)));
481
482 // Check if accelerator_4 is already added to the dispatcher.
483 EXPECT_FALSE(dispatcher.AddAccelerators(std::move(accelerators)));
484
485 ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_CONTROL_DOWN);
486 dispatcher.ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
487
488 // Check if accelerator_1 is already added to the dispatcher.
489 EXPECT_EQ(accelerator_1,
490 event_dispatcher_delegate.GetAndClearLastAccelerator());
491
492 dispatcher.RemoveAccelerator(accelerator_1);
mfomitchev 2016/11/30 02:33:04 I don't think we need to test Remove here - it's t
thanhph 2016/11/30 14:24:51 Done.
493 dispatcher.ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
494
495 // Check if accelerator_1 is remove added to the dispatcher.
496 EXPECT_EQ(0u, event_dispatcher_delegate.GetAndClearLastAccelerator());
416 } 497 }
417 498
418 TEST_F(EventDispatcherTest, EventMatching) { 499 TEST_F(EventDispatcherTest, EventMatching) {
419 TestEventDispatcherDelegate* event_dispatcher_delegate = 500 TestEventDispatcherDelegate* event_dispatcher_delegate =
420 test_event_dispatcher_delegate(); 501 test_event_dispatcher_delegate();
421 EventDispatcher* dispatcher = event_dispatcher(); 502 EventDispatcher* dispatcher = event_dispatcher();
422 503
423 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher( 504 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher(
424 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown); 505 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown);
425 uint32_t accelerator_1 = 1; 506 uint32_t accelerator_1 = 1;
426 dispatcher->AddAccelerator(accelerator_1, std::move(matcher)); 507 dispatcher->AddAccelerators(
508 CreateAcceleratorVector(accelerator_1, std::move(matcher)));
427 509
428 ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_CONTROL_DOWN); 510 ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_CONTROL_DOWN);
429 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 511 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
430 EXPECT_EQ(accelerator_1, 512 EXPECT_EQ(accelerator_1,
431 event_dispatcher_delegate->GetAndClearLastAccelerator()); 513 event_dispatcher_delegate->GetAndClearLastAccelerator());
432 514
433 // EF_NUM_LOCK_ON should be ignored since CreateKeyMatcher defaults to 515 // EF_NUM_LOCK_ON should be ignored since CreateKeyMatcher defaults to
434 // ignoring. 516 // ignoring.
435 key = ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_W, 517 key = ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_W,
436 ui::EF_CONTROL_DOWN | ui::EF_NUM_LOCK_ON); 518 ui::EF_CONTROL_DOWN | ui::EF_NUM_LOCK_ON);
437 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 519 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
438 EXPECT_EQ(accelerator_1, 520 EXPECT_EQ(accelerator_1,
439 event_dispatcher_delegate->GetAndClearLastAccelerator()); 521 event_dispatcher_delegate->GetAndClearLastAccelerator());
440 522
441 key = ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_NONE); 523 key = ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_NONE);
442 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 524 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
443 EXPECT_EQ(0u, event_dispatcher_delegate->GetAndClearLastAccelerator()); 525 EXPECT_EQ(0u, event_dispatcher_delegate->GetAndClearLastAccelerator());
444 526
445 uint32_t accelerator_2 = 2; 527 uint32_t accelerator_2 = 2;
446 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::W, 528 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::W,
447 ui::mojom::kEventFlagNone); 529 ui::mojom::kEventFlagNone);
448 dispatcher->AddAccelerator(accelerator_2, std::move(matcher)); 530 dispatcher->AddAccelerators(
531 CreateAcceleratorVector(accelerator_2, std::move(matcher)));
449 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 532 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
450 EXPECT_EQ(accelerator_2, 533 EXPECT_EQ(accelerator_2,
451 event_dispatcher_delegate->GetAndClearLastAccelerator()); 534 event_dispatcher_delegate->GetAndClearLastAccelerator());
452 535
453 dispatcher->RemoveAccelerator(accelerator_2); 536 dispatcher->RemoveAccelerator(accelerator_2);
454 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 537 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
455 EXPECT_EQ(0u, event_dispatcher_delegate->GetAndClearLastAccelerator()); 538 EXPECT_EQ(0u, event_dispatcher_delegate->GetAndClearLastAccelerator());
456 } 539 }
457 540
458 // Tests that a post-target accelerator is not triggered by ProcessEvent. 541 // Tests that a post-target accelerator is not triggered by ProcessEvent.
459 TEST_F(EventDispatcherTest, PostTargetAccelerator) { 542 TEST_F(EventDispatcherTest, PostTargetAccelerator) {
460 TestEventDispatcherDelegate* event_dispatcher_delegate = 543 TestEventDispatcherDelegate* event_dispatcher_delegate =
461 test_event_dispatcher_delegate(); 544 test_event_dispatcher_delegate();
462 EventDispatcher* dispatcher = event_dispatcher(); 545 EventDispatcher* dispatcher = event_dispatcher();
463 546
464 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher( 547 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher(
465 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown); 548 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown);
466 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::POST_TARGET; 549 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::POST_TARGET;
467 uint32_t accelerator_1 = 1; 550 uint32_t accelerator_1 = 1;
468 dispatcher->AddAccelerator(accelerator_1, std::move(matcher)); 551 dispatcher->AddAccelerators(
552 CreateAcceleratorVector(accelerator_1, std::move(matcher)));
469 553
470 ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_CONTROL_DOWN); 554 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. 555 // The post-target accelerator should be fired if there is no focused window.
472 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 556 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
473 EXPECT_EQ(accelerator_1, 557 EXPECT_EQ(accelerator_1,
474 event_dispatcher_delegate->GetAndClearLastAccelerator()); 558 event_dispatcher_delegate->GetAndClearLastAccelerator());
475 std::unique_ptr<DispatchedEventDetails> details = 559 std::unique_ptr<DispatchedEventDetails> details =
476 event_dispatcher_delegate->GetAndAdvanceDispatchedEventDetails(); 560 event_dispatcher_delegate->GetAndAdvanceDispatchedEventDetails();
477 EXPECT_FALSE(details); 561 EXPECT_FALSE(details);
478 562
(...skipping 24 matching lines...) Expand all
503 TEST_F(EventDispatcherTest, ProcessPost) { 587 TEST_F(EventDispatcherTest, ProcessPost) {
504 TestEventDispatcherDelegate* event_dispatcher_delegate = 588 TestEventDispatcherDelegate* event_dispatcher_delegate =
505 test_event_dispatcher_delegate(); 589 test_event_dispatcher_delegate();
506 EventDispatcher* dispatcher = event_dispatcher(); 590 EventDispatcher* dispatcher = event_dispatcher();
507 591
508 uint32_t pre_id = 1; 592 uint32_t pre_id = 1;
509 { 593 {
510 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher( 594 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher(
511 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown); 595 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown);
512 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::PRE_TARGET; 596 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::PRE_TARGET;
513 dispatcher->AddAccelerator(pre_id, std::move(matcher)); 597 dispatcher->AddAccelerators(
598 CreateAcceleratorVector(pre_id, std::move(matcher)));
514 } 599 }
515 600
516 uint32_t post_id = 2; 601 uint32_t post_id = 2;
517 { 602 {
518 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher( 603 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher(
519 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown); 604 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown);
520 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::POST_TARGET; 605 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::POST_TARGET;
521 dispatcher->AddAccelerator(post_id, std::move(matcher)); 606 dispatcher->AddAccelerators(
607 CreateAcceleratorVector(post_id, std::move(matcher)));
522 } 608 }
523 609
524 // Set focused window for EventDispatcher dispatches key events. 610 // Set focused window for EventDispatcher dispatches key events.
525 std::unique_ptr<ServerWindow> child = CreateChildWindow(WindowId(1, 3)); 611 std::unique_ptr<ServerWindow> child = CreateChildWindow(WindowId(1, 3));
526 event_dispatcher_delegate->SetFocusedWindowFromEventDispatcher(child.get()); 612 event_dispatcher_delegate->SetFocusedWindowFromEventDispatcher(child.get());
527 613
528 // Dispatch for ANY, which should trigger PRE and not call 614 // Dispatch for ANY, which should trigger PRE and not call
529 // DispatchInputEventToWindow(). 615 // DispatchInputEventToWindow().
530 ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_CONTROL_DOWN); 616 ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_CONTROL_DOWN);
531 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 617 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. 1791 // The delegate can decide if it really wants to forward the event or not.
1706 EXPECT_EQ(child.get(), 1792 EXPECT_EQ(child.get(),
1707 test_event_dispatcher_delegate()->lost_capture_window()); 1793 test_event_dispatcher_delegate()->lost_capture_window());
1708 EXPECT_EQ(child.get(), event_dispatcher()->capture_window()); 1794 EXPECT_EQ(child.get(), event_dispatcher()->capture_window());
1709 EXPECT_EQ(kClientAreaId, event_dispatcher()->capture_window_client_id()); 1795 EXPECT_EQ(kClientAreaId, event_dispatcher()->capture_window_client_id());
1710 } 1796 }
1711 1797
1712 } // namespace test 1798 } // namespace test
1713 } // namespace ws 1799 } // namespace ws
1714 } // namespace ui 1800 } // namespace ui
OLDNEW
« no previous file with comments | « services/ui/ws/event_dispatcher.cc ('k') | services/ui/ws/window_manager_state.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698