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

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

Issue 2520093003: WindowManagerClient::AddAccelerator() should take an array (Closed)
Patch Set: Refactor code, reduce code size, change namespace. 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 ui::AddAcceleratorHelper(accelerator_1, std::move(matcher))));
mfomitchev 2016/11/29 19:21:19 I don't think you need ui:: anywhere here?
thanhph 2016/11/29 20:36:45 Done.
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 ui::AddAcceleratorHelper(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 ui::AddAcceleratorHelper(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 ui::AddAcceleratorHelper(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 ui::AddAcceleratorHelper(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 ui::AddAcceleratorHelper(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 std::vector<ui::mojom::AcceleratorTransportPtr> accelerators;
429
430 uint32_t accelerator_1 = 1;
431 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher(
432 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown);
433 accelerators.push_back(std::move(
434 ui::AddAcceleratorHelper(accelerator_1, std::move(matcher)).back()));
435
436 uint32_t accelerator_2 = 2;
437 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::N,
438 ui::mojom::kEventFlagNone);
439 accelerators.push_back(std::move(
440 ui::AddAcceleratorHelper(accelerator_2, std::move(matcher)).back()));
441
442 uint32_t accelerator_3 = 3;
443 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::T,
444 ui::mojom::kEventFlagNone);
445 accelerators.push_back(std::move(
446 ui::AddAcceleratorHelper(accelerator_3, std::move(matcher)).back()));
447
448 // Adding all accelerators with the different ids should pass.
mfomitchev 2016/11/29 19:21:19 Adding unique accelerators should pass.
thanhph 2016/11/29 20:40:48 Done.
449 EXPECT_TRUE(dispatcher.AddAccelerators(std::move(accelerators)));
450
451 accelerators.clear();
452
453 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::W,
454 ui::mojom::kEventFlagControlDown);
455 accelerators.push_back(std::move(
456 ui::AddAcceleratorHelper(accelerator_1, std::move(matcher)).back()));
457
458 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::W,
459 ui::mojom::kEventFlagControlDown);
460 accelerators.push_back(std::move(
461 ui::AddAcceleratorHelper(accelerator_2, std::move(matcher)).back()));
462
463 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::T,
464 ui::mojom::kEventFlagNone);
465 accelerators.push_back(std::move(
466 ui::AddAcceleratorHelper(accelerator_1, std::move(matcher)).back()));
467
468 // Adding accelerators with the duplicated id should fail.
469 EXPECT_FALSE(dispatcher.AddAccelerators(std::move(accelerators)));
416 } 470 }
417 471
418 TEST_F(EventDispatcherTest, EventMatching) { 472 TEST_F(EventDispatcherTest, EventMatching) {
419 TestEventDispatcherDelegate* event_dispatcher_delegate = 473 TestEventDispatcherDelegate* event_dispatcher_delegate =
420 test_event_dispatcher_delegate(); 474 test_event_dispatcher_delegate();
421 EventDispatcher* dispatcher = event_dispatcher(); 475 EventDispatcher* dispatcher = event_dispatcher();
422 476
423 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher( 477 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher(
424 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown); 478 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown);
425 uint32_t accelerator_1 = 1; 479 uint32_t accelerator_1 = 1;
426 dispatcher->AddAccelerator(accelerator_1, std::move(matcher)); 480 dispatcher->AddAccelerators(
481 ui::AddAcceleratorHelper(accelerator_1, std::move(matcher)));
427 482
428 ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_CONTROL_DOWN); 483 ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_CONTROL_DOWN);
429 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 484 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
430 EXPECT_EQ(accelerator_1, 485 EXPECT_EQ(accelerator_1,
431 event_dispatcher_delegate->GetAndClearLastAccelerator()); 486 event_dispatcher_delegate->GetAndClearLastAccelerator());
432 487
433 // EF_NUM_LOCK_ON should be ignored since CreateKeyMatcher defaults to 488 // EF_NUM_LOCK_ON should be ignored since CreateKeyMatcher defaults to
434 // ignoring. 489 // ignoring.
435 key = ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_W, 490 key = ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_W,
436 ui::EF_CONTROL_DOWN | ui::EF_NUM_LOCK_ON); 491 ui::EF_CONTROL_DOWN | ui::EF_NUM_LOCK_ON);
437 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 492 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
438 EXPECT_EQ(accelerator_1, 493 EXPECT_EQ(accelerator_1,
439 event_dispatcher_delegate->GetAndClearLastAccelerator()); 494 event_dispatcher_delegate->GetAndClearLastAccelerator());
440 495
441 key = ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_NONE); 496 key = ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_NONE);
442 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 497 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
443 EXPECT_EQ(0u, event_dispatcher_delegate->GetAndClearLastAccelerator()); 498 EXPECT_EQ(0u, event_dispatcher_delegate->GetAndClearLastAccelerator());
444 499
445 uint32_t accelerator_2 = 2; 500 uint32_t accelerator_2 = 2;
446 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::W, 501 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::W,
447 ui::mojom::kEventFlagNone); 502 ui::mojom::kEventFlagNone);
448 dispatcher->AddAccelerator(accelerator_2, std::move(matcher)); 503 dispatcher->AddAccelerators(
504 ui::AddAcceleratorHelper(accelerator_2, std::move(matcher)));
449 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 505 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
450 EXPECT_EQ(accelerator_2, 506 EXPECT_EQ(accelerator_2,
451 event_dispatcher_delegate->GetAndClearLastAccelerator()); 507 event_dispatcher_delegate->GetAndClearLastAccelerator());
452 508
453 dispatcher->RemoveAccelerator(accelerator_2); 509 dispatcher->RemoveAccelerator(accelerator_2);
454 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 510 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
455 EXPECT_EQ(0u, event_dispatcher_delegate->GetAndClearLastAccelerator()); 511 EXPECT_EQ(0u, event_dispatcher_delegate->GetAndClearLastAccelerator());
456 } 512 }
457 513
458 // Tests that a post-target accelerator is not triggered by ProcessEvent. 514 // Tests that a post-target accelerator is not triggered by ProcessEvent.
459 TEST_F(EventDispatcherTest, PostTargetAccelerator) { 515 TEST_F(EventDispatcherTest, PostTargetAccelerator) {
460 TestEventDispatcherDelegate* event_dispatcher_delegate = 516 TestEventDispatcherDelegate* event_dispatcher_delegate =
461 test_event_dispatcher_delegate(); 517 test_event_dispatcher_delegate();
462 EventDispatcher* dispatcher = event_dispatcher(); 518 EventDispatcher* dispatcher = event_dispatcher();
463 519
464 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher( 520 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher(
465 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown); 521 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown);
466 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::POST_TARGET; 522 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::POST_TARGET;
467 uint32_t accelerator_1 = 1; 523 uint32_t accelerator_1 = 1;
468 dispatcher->AddAccelerator(accelerator_1, std::move(matcher)); 524 dispatcher->AddAccelerators(
525 ui::AddAcceleratorHelper(accelerator_1, std::move(matcher)));
469 526
470 ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_CONTROL_DOWN); 527 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. 528 // The post-target accelerator should be fired if there is no focused window.
472 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 529 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
473 EXPECT_EQ(accelerator_1, 530 EXPECT_EQ(accelerator_1,
474 event_dispatcher_delegate->GetAndClearLastAccelerator()); 531 event_dispatcher_delegate->GetAndClearLastAccelerator());
475 std::unique_ptr<DispatchedEventDetails> details = 532 std::unique_ptr<DispatchedEventDetails> details =
476 event_dispatcher_delegate->GetAndAdvanceDispatchedEventDetails(); 533 event_dispatcher_delegate->GetAndAdvanceDispatchedEventDetails();
477 EXPECT_FALSE(details); 534 EXPECT_FALSE(details);
478 535
(...skipping 24 matching lines...) Expand all
503 TEST_F(EventDispatcherTest, ProcessPost) { 560 TEST_F(EventDispatcherTest, ProcessPost) {
504 TestEventDispatcherDelegate* event_dispatcher_delegate = 561 TestEventDispatcherDelegate* event_dispatcher_delegate =
505 test_event_dispatcher_delegate(); 562 test_event_dispatcher_delegate();
506 EventDispatcher* dispatcher = event_dispatcher(); 563 EventDispatcher* dispatcher = event_dispatcher();
507 564
508 uint32_t pre_id = 1; 565 uint32_t pre_id = 1;
509 { 566 {
510 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher( 567 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher(
511 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown); 568 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown);
512 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::PRE_TARGET; 569 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::PRE_TARGET;
513 dispatcher->AddAccelerator(pre_id, std::move(matcher)); 570 dispatcher->AddAccelerators(
571 ui::AddAcceleratorHelper(pre_id, std::move(matcher)));
514 } 572 }
515 573
516 uint32_t post_id = 2; 574 uint32_t post_id = 2;
517 { 575 {
518 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher( 576 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher(
519 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown); 577 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown);
520 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::POST_TARGET; 578 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::POST_TARGET;
521 dispatcher->AddAccelerator(post_id, std::move(matcher)); 579 dispatcher->AddAccelerators(
580 ui::AddAcceleratorHelper(post_id, std::move(matcher)));
522 } 581 }
523 582
524 // Set focused window for EventDispatcher dispatches key events. 583 // Set focused window for EventDispatcher dispatches key events.
525 std::unique_ptr<ServerWindow> child = CreateChildWindow(WindowId(1, 3)); 584 std::unique_ptr<ServerWindow> child = CreateChildWindow(WindowId(1, 3));
526 event_dispatcher_delegate->SetFocusedWindowFromEventDispatcher(child.get()); 585 event_dispatcher_delegate->SetFocusedWindowFromEventDispatcher(child.get());
527 586
528 // Dispatch for ANY, which should trigger PRE and not call 587 // Dispatch for ANY, which should trigger PRE and not call
529 // DispatchInputEventToWindow(). 588 // DispatchInputEventToWindow().
530 ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_CONTROL_DOWN); 589 ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_CONTROL_DOWN);
531 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 590 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. 1764 // The delegate can decide if it really wants to forward the event or not.
1706 EXPECT_EQ(child.get(), 1765 EXPECT_EQ(child.get(),
1707 test_event_dispatcher_delegate()->lost_capture_window()); 1766 test_event_dispatcher_delegate()->lost_capture_window());
1708 EXPECT_EQ(child.get(), event_dispatcher()->capture_window()); 1767 EXPECT_EQ(child.get(), event_dispatcher()->capture_window());
1709 EXPECT_EQ(kClientAreaId, event_dispatcher()->capture_window_client_id()); 1768 EXPECT_EQ(kClientAreaId, event_dispatcher()->capture_window_client_id());
1710 } 1769 }
1711 1770
1712 } // namespace test 1771 } // namespace test
1713 } // namespace ws 1772 } // namespace ws
1714 } // namespace ui 1773 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698