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

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

Issue 2520093003: WindowManagerClient::AddAccelerator() should take an array (Closed)
Patch Set: Search/Replace to transport in accelerator_transport. 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::AcceleratorPtr> 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(CreateAccelerator(accelerator_1, std::move(matcher)));
435
436 uint32_t accelerator_2 = 2;
437 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::N,
438 ui::mojom::kEventFlagNone);
439 accelerators.push_back(CreateAccelerator(accelerator_2, std::move(matcher)));
440
441 uint32_t accelerator_3 = 3;
442 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::T,
443 ui::mojom::kEventFlagNone);
444 accelerators.push_back(CreateAccelerator(accelerator_3, std::move(matcher)));
445
446 // Adding unique accelerators should pass.
447 EXPECT_TRUE(dispatcher.AddAccelerators(std::move(accelerators)));
448
449 accelerators.clear();
450
451 uint32_t accelerator_4 = 4;
452 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::W,
453 ui::mojom::kEventFlagControlDown);
454 accelerators.push_back(CreateAccelerator(accelerator_4, std::move(matcher)));
455
456 uint32_t accelerator_5 = 5;
457 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::W,
458 ui::mojom::kEventFlagControlDown);
459 accelerators.push_back(CreateAccelerator(accelerator_5, std::move(matcher)));
460
461 uint32_t accelerator_6 = accelerator_5;
462 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::T,
463 ui::mojom::kEventFlagNone);
464 accelerators.push_back(CreateAccelerator(accelerator_6, std::move(matcher)));
465
466 // Adding accelerator_6 with the same id accelerator_5 should fail.
467 EXPECT_FALSE(dispatcher.AddAccelerators(std::move(accelerators)));
468
469 accelerators.clear();
470
471 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::T,
472 ui::mojom::kEventFlagNone);
473 accelerators.push_back(CreateAccelerator(accelerator_4, std::move(matcher)));
474
475 // Check if accelerator_4 is already added to the dispatcher.
mfomitchev 2016/11/30 17:58:40 It's not added, it's a dupe of accelerator_1 becau
thanhph 2016/11/30 23:35:13 Done.
476 EXPECT_FALSE(dispatcher.AddAccelerators(std::move(accelerators)));
477
478 ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_CONTROL_DOWN);
479 dispatcher.ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
480
481 // Check if accelerator_1 is already added to the dispatcher.
482 EXPECT_EQ(accelerator_1,
483 event_dispatcher_delegate.GetAndClearLastAccelerator());
416 } 484 }
417 485
418 TEST_F(EventDispatcherTest, EventMatching) { 486 TEST_F(EventDispatcherTest, EventMatching) {
419 TestEventDispatcherDelegate* event_dispatcher_delegate = 487 TestEventDispatcherDelegate* event_dispatcher_delegate =
420 test_event_dispatcher_delegate(); 488 test_event_dispatcher_delegate();
421 EventDispatcher* dispatcher = event_dispatcher(); 489 EventDispatcher* dispatcher = event_dispatcher();
422 490
423 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher( 491 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher(
424 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown); 492 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown);
425 uint32_t accelerator_1 = 1; 493 uint32_t accelerator_1 = 1;
426 dispatcher->AddAccelerator(accelerator_1, std::move(matcher)); 494 dispatcher->AddAccelerators(
495 CreateAcceleratorVector(accelerator_1, std::move(matcher)));
427 496
428 ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_CONTROL_DOWN); 497 ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_CONTROL_DOWN);
429 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 498 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
430 EXPECT_EQ(accelerator_1, 499 EXPECT_EQ(accelerator_1,
431 event_dispatcher_delegate->GetAndClearLastAccelerator()); 500 event_dispatcher_delegate->GetAndClearLastAccelerator());
432 501
433 // EF_NUM_LOCK_ON should be ignored since CreateKeyMatcher defaults to 502 // EF_NUM_LOCK_ON should be ignored since CreateKeyMatcher defaults to
434 // ignoring. 503 // ignoring.
435 key = ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_W, 504 key = ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_W,
436 ui::EF_CONTROL_DOWN | ui::EF_NUM_LOCK_ON); 505 ui::EF_CONTROL_DOWN | ui::EF_NUM_LOCK_ON);
437 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 506 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
438 EXPECT_EQ(accelerator_1, 507 EXPECT_EQ(accelerator_1,
439 event_dispatcher_delegate->GetAndClearLastAccelerator()); 508 event_dispatcher_delegate->GetAndClearLastAccelerator());
440 509
441 key = ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_NONE); 510 key = ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_NONE);
442 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 511 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
443 EXPECT_EQ(0u, event_dispatcher_delegate->GetAndClearLastAccelerator()); 512 EXPECT_EQ(0u, event_dispatcher_delegate->GetAndClearLastAccelerator());
444 513
445 uint32_t accelerator_2 = 2; 514 uint32_t accelerator_2 = 2;
446 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::W, 515 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::W,
447 ui::mojom::kEventFlagNone); 516 ui::mojom::kEventFlagNone);
448 dispatcher->AddAccelerator(accelerator_2, std::move(matcher)); 517 dispatcher->AddAccelerators(
518 CreateAcceleratorVector(accelerator_2, std::move(matcher)));
449 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 519 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
450 EXPECT_EQ(accelerator_2, 520 EXPECT_EQ(accelerator_2,
451 event_dispatcher_delegate->GetAndClearLastAccelerator()); 521 event_dispatcher_delegate->GetAndClearLastAccelerator());
452 522
453 dispatcher->RemoveAccelerator(accelerator_2); 523 dispatcher->RemoveAccelerator(accelerator_2);
454 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 524 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
455 EXPECT_EQ(0u, event_dispatcher_delegate->GetAndClearLastAccelerator()); 525 EXPECT_EQ(0u, event_dispatcher_delegate->GetAndClearLastAccelerator());
456 } 526 }
457 527
458 // Tests that a post-target accelerator is not triggered by ProcessEvent. 528 // Tests that a post-target accelerator is not triggered by ProcessEvent.
459 TEST_F(EventDispatcherTest, PostTargetAccelerator) { 529 TEST_F(EventDispatcherTest, PostTargetAccelerator) {
460 TestEventDispatcherDelegate* event_dispatcher_delegate = 530 TestEventDispatcherDelegate* event_dispatcher_delegate =
461 test_event_dispatcher_delegate(); 531 test_event_dispatcher_delegate();
462 EventDispatcher* dispatcher = event_dispatcher(); 532 EventDispatcher* dispatcher = event_dispatcher();
463 533
464 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher( 534 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher(
465 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown); 535 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown);
466 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::POST_TARGET; 536 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::POST_TARGET;
467 uint32_t accelerator_1 = 1; 537 uint32_t accelerator_1 = 1;
468 dispatcher->AddAccelerator(accelerator_1, std::move(matcher)); 538 dispatcher->AddAccelerators(
539 CreateAcceleratorVector(accelerator_1, std::move(matcher)));
469 540
470 ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_CONTROL_DOWN); 541 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. 542 // The post-target accelerator should be fired if there is no focused window.
472 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 543 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
473 EXPECT_EQ(accelerator_1, 544 EXPECT_EQ(accelerator_1,
474 event_dispatcher_delegate->GetAndClearLastAccelerator()); 545 event_dispatcher_delegate->GetAndClearLastAccelerator());
475 std::unique_ptr<DispatchedEventDetails> details = 546 std::unique_ptr<DispatchedEventDetails> details =
476 event_dispatcher_delegate->GetAndAdvanceDispatchedEventDetails(); 547 event_dispatcher_delegate->GetAndAdvanceDispatchedEventDetails();
477 EXPECT_FALSE(details); 548 EXPECT_FALSE(details);
478 549
(...skipping 24 matching lines...) Expand all
503 TEST_F(EventDispatcherTest, ProcessPost) { 574 TEST_F(EventDispatcherTest, ProcessPost) {
504 TestEventDispatcherDelegate* event_dispatcher_delegate = 575 TestEventDispatcherDelegate* event_dispatcher_delegate =
505 test_event_dispatcher_delegate(); 576 test_event_dispatcher_delegate();
506 EventDispatcher* dispatcher = event_dispatcher(); 577 EventDispatcher* dispatcher = event_dispatcher();
507 578
508 uint32_t pre_id = 1; 579 uint32_t pre_id = 1;
509 { 580 {
510 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher( 581 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher(
511 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown); 582 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown);
512 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::PRE_TARGET; 583 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::PRE_TARGET;
513 dispatcher->AddAccelerator(pre_id, std::move(matcher)); 584 dispatcher->AddAccelerators(
585 CreateAcceleratorVector(pre_id, std::move(matcher)));
514 } 586 }
515 587
516 uint32_t post_id = 2; 588 uint32_t post_id = 2;
517 { 589 {
518 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher( 590 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher(
519 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown); 591 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown);
520 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::POST_TARGET; 592 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::POST_TARGET;
521 dispatcher->AddAccelerator(post_id, std::move(matcher)); 593 dispatcher->AddAccelerators(
594 CreateAcceleratorVector(post_id, std::move(matcher)));
522 } 595 }
523 596
524 // Set focused window for EventDispatcher dispatches key events. 597 // Set focused window for EventDispatcher dispatches key events.
525 std::unique_ptr<ServerWindow> child = CreateChildWindow(WindowId(1, 3)); 598 std::unique_ptr<ServerWindow> child = CreateChildWindow(WindowId(1, 3));
526 event_dispatcher_delegate->SetFocusedWindowFromEventDispatcher(child.get()); 599 event_dispatcher_delegate->SetFocusedWindowFromEventDispatcher(child.get());
527 600
528 // Dispatch for ANY, which should trigger PRE and not call 601 // Dispatch for ANY, which should trigger PRE and not call
529 // DispatchInputEventToWindow(). 602 // DispatchInputEventToWindow().
530 ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_CONTROL_DOWN); 603 ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_CONTROL_DOWN);
531 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 604 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. 1778 // The delegate can decide if it really wants to forward the event or not.
1706 EXPECT_EQ(child.get(), 1779 EXPECT_EQ(child.get(),
1707 test_event_dispatcher_delegate()->lost_capture_window()); 1780 test_event_dispatcher_delegate()->lost_capture_window());
1708 EXPECT_EQ(child.get(), event_dispatcher()->capture_window()); 1781 EXPECT_EQ(child.get(), event_dispatcher()->capture_window());
1709 EXPECT_EQ(kClientAreaId, event_dispatcher()->capture_window_client_id()); 1782 EXPECT_EQ(kClientAreaId, event_dispatcher()->capture_window_client_id());
1710 } 1783 }
1711 1784
1712 } // namespace test 1785 } // namespace test
1713 } // namespace ws 1786 } // namespace ws
1714 } // namespace ui 1787 } // 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