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

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

Issue 2520093003: WindowManagerClient::AddAccelerator() should take an array (Closed)
Patch Set: Adding 1 test that has two unique and one duped accelerators. 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))));
422 }
423
424 TEST_F(EventDispatcherTest, AddAccelerators) {
mfomitchev 2016/12/01 15:32:36 nit: consider putting this after EventMatching, si
mfomitchev 2016/12/01 15:32:36 Can we call this test AddMultipleAccelerators or s
thanhph 2016/12/01 19:03:19 Done, thanks!
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)));
mfomitchev 2016/12/01 15:32:36 Add a test for accelerator_3 below confirming that
thanhph 2016/12/01 19:03:19 Done.
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_6 = 6;
457 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::Q,
458 ui::mojom::kEventFlagControlDown);
459 accelerators.push_back(CreateAccelerator(accelerator_6, std::move(matcher)));
460
461 uint32_t accelerator_7 = 7;
462 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::R,
463 ui::mojom::kEventFlagControlDown);
464 accelerators.push_back(CreateAccelerator(accelerator_7, std::move(matcher)));
465
466 // Adding accelerator_4 with the same matcher as accelerator_1 should fail
467 // while accelerator_6 and accelerator_7 should be added.
468 EXPECT_FALSE(dispatcher.AddAccelerators(std::move(accelerators)));
469
470 ui::KeyEvent key_1(ui::ET_KEY_PRESSED, ui::VKEY_R, ui::EF_CONTROL_DOWN);
mfomitchev 2016/12/01 15:32:37 nit: Just call this key, otherwise it makes one th
thanhph 2016/12/01 19:03:19 Done.
471 dispatcher.ProcessEvent(key_1, EventDispatcher::AcceleratorMatchPhase::ANY);
472
473 // Check if the accelerator_7 was still added regardless of failling
mfomitchev 2016/12/01 15:32:37 nit: move this comment about key_1 declaration, si
thanhph 2016/12/01 19:03:19 Done.
474 // adding attempt of accelerator_4 in the accelerators vector.
475 EXPECT_EQ(accelerator_7,
476 event_dispatcher_delegate.GetAndClearLastAccelerator());
477
478 ui::KeyEvent key_2(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_CONTROL_DOWN);
mfomitchev 2016/12/01 15:32:36 ditto: key
thanhph 2016/12/01 19:03:19 Done.
479 dispatcher.ProcessEvent(key_2, EventDispatcher::AcceleratorMatchPhase::ANY);
480
481 // Check if the accelerator_1 id hasn't changed because of accelerator_4
mfomitchev 2016/12/01 15:32:37 ditto: move
thanhph 2016/12/01 19:03:19 Done.
482 // adding attempt above.
483 EXPECT_EQ(accelerator_1,
484 event_dispatcher_delegate.GetAndClearLastAccelerator());
485
486 accelerators.clear();
mfomitchev 2016/12/01 15:32:36 I don't think we need the test below - you've alre
487
488 uint32_t accelerator_5 = accelerator_1;
489 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::M,
490 ui::mojom::kEventFlagNone);
491 accelerators.push_back(CreateAccelerator(accelerator_5, std::move(matcher)));
492
493 // Adding accelerator_5 with the same id as accelerator_1 should fail.
494 EXPECT_FALSE(dispatcher.AddAccelerators(std::move(accelerators)));
416 } 495 }
417 496
418 TEST_F(EventDispatcherTest, EventMatching) { 497 TEST_F(EventDispatcherTest, EventMatching) {
419 TestEventDispatcherDelegate* event_dispatcher_delegate = 498 TestEventDispatcherDelegate* event_dispatcher_delegate =
420 test_event_dispatcher_delegate(); 499 test_event_dispatcher_delegate();
421 EventDispatcher* dispatcher = event_dispatcher(); 500 EventDispatcher* dispatcher = event_dispatcher();
422 501
423 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher( 502 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher(
424 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown); 503 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown);
425 uint32_t accelerator_1 = 1; 504 uint32_t accelerator_1 = 1;
426 dispatcher->AddAccelerator(accelerator_1, std::move(matcher)); 505 dispatcher->AddAccelerators(
506 CreateAcceleratorVector(accelerator_1, std::move(matcher)));
427 507
428 ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_CONTROL_DOWN); 508 ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_CONTROL_DOWN);
429 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 509 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
430 EXPECT_EQ(accelerator_1, 510 EXPECT_EQ(accelerator_1,
431 event_dispatcher_delegate->GetAndClearLastAccelerator()); 511 event_dispatcher_delegate->GetAndClearLastAccelerator());
432 512
433 // EF_NUM_LOCK_ON should be ignored since CreateKeyMatcher defaults to 513 // EF_NUM_LOCK_ON should be ignored since CreateKeyMatcher defaults to
434 // ignoring. 514 // ignoring.
435 key = ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_W, 515 key = ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_W,
436 ui::EF_CONTROL_DOWN | ui::EF_NUM_LOCK_ON); 516 ui::EF_CONTROL_DOWN | ui::EF_NUM_LOCK_ON);
437 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 517 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
438 EXPECT_EQ(accelerator_1, 518 EXPECT_EQ(accelerator_1,
439 event_dispatcher_delegate->GetAndClearLastAccelerator()); 519 event_dispatcher_delegate->GetAndClearLastAccelerator());
440 520
441 key = ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_NONE); 521 key = ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_NONE);
442 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 522 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
443 EXPECT_EQ(0u, event_dispatcher_delegate->GetAndClearLastAccelerator()); 523 EXPECT_EQ(0u, event_dispatcher_delegate->GetAndClearLastAccelerator());
444 524
445 uint32_t accelerator_2 = 2; 525 uint32_t accelerator_2 = 2;
446 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::W, 526 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::W,
447 ui::mojom::kEventFlagNone); 527 ui::mojom::kEventFlagNone);
448 dispatcher->AddAccelerator(accelerator_2, std::move(matcher)); 528 dispatcher->AddAccelerators(
529 CreateAcceleratorVector(accelerator_2, std::move(matcher)));
449 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 530 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
450 EXPECT_EQ(accelerator_2, 531 EXPECT_EQ(accelerator_2,
451 event_dispatcher_delegate->GetAndClearLastAccelerator()); 532 event_dispatcher_delegate->GetAndClearLastAccelerator());
452 533
453 dispatcher->RemoveAccelerator(accelerator_2); 534 dispatcher->RemoveAccelerator(accelerator_2);
454 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 535 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
455 EXPECT_EQ(0u, event_dispatcher_delegate->GetAndClearLastAccelerator()); 536 EXPECT_EQ(0u, event_dispatcher_delegate->GetAndClearLastAccelerator());
456 } 537 }
457 538
458 // Tests that a post-target accelerator is not triggered by ProcessEvent. 539 // Tests that a post-target accelerator is not triggered by ProcessEvent.
459 TEST_F(EventDispatcherTest, PostTargetAccelerator) { 540 TEST_F(EventDispatcherTest, PostTargetAccelerator) {
460 TestEventDispatcherDelegate* event_dispatcher_delegate = 541 TestEventDispatcherDelegate* event_dispatcher_delegate =
461 test_event_dispatcher_delegate(); 542 test_event_dispatcher_delegate();
462 EventDispatcher* dispatcher = event_dispatcher(); 543 EventDispatcher* dispatcher = event_dispatcher();
463 544
464 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher( 545 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher(
465 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown); 546 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown);
466 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::POST_TARGET; 547 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::POST_TARGET;
467 uint32_t accelerator_1 = 1; 548 uint32_t accelerator_1 = 1;
468 dispatcher->AddAccelerator(accelerator_1, std::move(matcher)); 549 dispatcher->AddAccelerators(
550 CreateAcceleratorVector(accelerator_1, std::move(matcher)));
469 551
470 ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_CONTROL_DOWN); 552 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. 553 // The post-target accelerator should be fired if there is no focused window.
472 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 554 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
473 EXPECT_EQ(accelerator_1, 555 EXPECT_EQ(accelerator_1,
474 event_dispatcher_delegate->GetAndClearLastAccelerator()); 556 event_dispatcher_delegate->GetAndClearLastAccelerator());
475 std::unique_ptr<DispatchedEventDetails> details = 557 std::unique_ptr<DispatchedEventDetails> details =
476 event_dispatcher_delegate->GetAndAdvanceDispatchedEventDetails(); 558 event_dispatcher_delegate->GetAndAdvanceDispatchedEventDetails();
477 EXPECT_FALSE(details); 559 EXPECT_FALSE(details);
478 560
(...skipping 24 matching lines...) Expand all
503 TEST_F(EventDispatcherTest, ProcessPost) { 585 TEST_F(EventDispatcherTest, ProcessPost) {
504 TestEventDispatcherDelegate* event_dispatcher_delegate = 586 TestEventDispatcherDelegate* event_dispatcher_delegate =
505 test_event_dispatcher_delegate(); 587 test_event_dispatcher_delegate();
506 EventDispatcher* dispatcher = event_dispatcher(); 588 EventDispatcher* dispatcher = event_dispatcher();
507 589
508 uint32_t pre_id = 1; 590 uint32_t pre_id = 1;
509 { 591 {
510 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher( 592 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher(
511 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown); 593 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown);
512 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::PRE_TARGET; 594 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::PRE_TARGET;
513 dispatcher->AddAccelerator(pre_id, std::move(matcher)); 595 dispatcher->AddAccelerators(
596 CreateAcceleratorVector(pre_id, std::move(matcher)));
514 } 597 }
515 598
516 uint32_t post_id = 2; 599 uint32_t post_id = 2;
517 { 600 {
518 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher( 601 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher(
519 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown); 602 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown);
520 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::POST_TARGET; 603 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::POST_TARGET;
521 dispatcher->AddAccelerator(post_id, std::move(matcher)); 604 dispatcher->AddAccelerators(
605 CreateAcceleratorVector(post_id, std::move(matcher)));
522 } 606 }
523 607
524 // Set focused window for EventDispatcher dispatches key events. 608 // Set focused window for EventDispatcher dispatches key events.
525 std::unique_ptr<ServerWindow> child = CreateChildWindow(WindowId(1, 3)); 609 std::unique_ptr<ServerWindow> child = CreateChildWindow(WindowId(1, 3));
526 event_dispatcher_delegate->SetFocusedWindowFromEventDispatcher(child.get()); 610 event_dispatcher_delegate->SetFocusedWindowFromEventDispatcher(child.get());
527 611
528 // Dispatch for ANY, which should trigger PRE and not call 612 // Dispatch for ANY, which should trigger PRE and not call
529 // DispatchInputEventToWindow(). 613 // DispatchInputEventToWindow().
530 ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_CONTROL_DOWN); 614 ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_CONTROL_DOWN);
531 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 615 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. 1789 // The delegate can decide if it really wants to forward the event or not.
1706 EXPECT_EQ(child.get(), 1790 EXPECT_EQ(child.get(),
1707 test_event_dispatcher_delegate()->lost_capture_window()); 1791 test_event_dispatcher_delegate()->lost_capture_window());
1708 EXPECT_EQ(child.get(), event_dispatcher()->capture_window()); 1792 EXPECT_EQ(child.get(), event_dispatcher()->capture_window());
1709 EXPECT_EQ(kClientAreaId, event_dispatcher()->capture_window_client_id()); 1793 EXPECT_EQ(kClientAreaId, event_dispatcher()->capture_window_client_id());
1710 } 1794 }
1711 1795
1712 } // namespace test 1796 } // namespace test
1713 } // namespace ws 1797 } // namespace ws
1714 } // namespace ui 1798 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698