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

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

Issue 2520093003: WindowManagerClient::AddAccelerator() should take an array (Closed)
Patch Set: Modify helper to return vector and leverage helper in other files.Rename/format code. 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/accelerator_transport_util.h"
14 #include "services/ui/common/event_matcher_util.h" 15 #include "services/ui/common/event_matcher_util.h"
15 #include "services/ui/ws/accelerator.h" 16 #include "services/ui/ws/accelerator.h"
16 #include "services/ui/ws/event_dispatcher_delegate.h" 17 #include "services/ui/ws/event_dispatcher_delegate.h"
17 #include "services/ui/ws/server_window.h" 18 #include "services/ui/ws/server_window.h"
18 #include "services/ui/ws/server_window_compositor_frame_sink_manager_test_api.h" 19 #include "services/ui/ws/server_window_compositor_frame_sink_manager_test_api.h"
19 #include "services/ui/ws/test_server_window_delegate.h" 20 #include "services/ui/ws/test_server_window_delegate.h"
20 #include "services/ui/ws/test_utils.h" 21 #include "services/ui/ws/test_utils.h"
21 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
22 #include "ui/events/event.h" 23 #include "ui/events/event.h"
23 24
(...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after
370 EXPECT_FALSE(details); 371 EXPECT_FALSE(details);
371 372
372 // Delegate was informed that there wasn't a target. 373 // Delegate was informed that there wasn't a target.
373 ui::Event* event_out = 374 ui::Event* event_out =
374 test_event_dispatcher_delegate()->last_event_target_not_found(); 375 test_event_dispatcher_delegate()->last_event_target_not_found();
375 ASSERT_TRUE(event_out); 376 ASSERT_TRUE(event_out);
376 EXPECT_TRUE(event_out->IsKeyEvent()); 377 EXPECT_TRUE(event_out->IsKeyEvent());
377 EXPECT_EQ(ui::VKEY_A, event_out->AsKeyEvent()->key_code()); 378 EXPECT_EQ(ui::VKEY_A, event_out->AsKeyEvent()->key_code());
378 } 379 }
379 380
380 TEST_F(EventDispatcherTest, AcceleratorBasic) { 381 TEST_F(EventDispatcherTest, AcceleratorBasic) {
mfomitchev 2016/11/29 01:55:46 We need some coverage for adding more than one acc
thanhph 2016/11/29 16:50:17 Done, I created a new test. Thanks!
381 ClearSetup(); 382 ClearSetup();
382 TestEventDispatcherDelegate event_dispatcher_delegate(nullptr); 383 TestEventDispatcherDelegate event_dispatcher_delegate(nullptr);
383 EventDispatcher dispatcher(&event_dispatcher_delegate); 384 EventDispatcher dispatcher(&event_dispatcher_delegate);
384 385
385 uint32_t accelerator_1 = 1; 386 uint32_t accelerator_1 = 1;
386 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher( 387 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher(
387 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown); 388 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown);
388 EXPECT_TRUE(dispatcher.AddAccelerator(accelerator_1, std::move(matcher))); 389
390 std::vector<ui::mojom::AcceleratorTransportPtr> accelerators_1;
391 accelerators_1 = ash::mus::AddAcceleratorHelper(
392 std::move(accelerators_1), accelerator_1, std::move(matcher));
393 EXPECT_TRUE(dispatcher.AddAccelerators(std::move(accelerators_1)));
389 394
390 uint32_t accelerator_2 = 2; 395 uint32_t accelerator_2 = 2;
391 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::N, 396 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::N,
392 ui::mojom::kEventFlagNone); 397 ui::mojom::kEventFlagNone);
393 EXPECT_TRUE(dispatcher.AddAccelerator(accelerator_2, std::move(matcher))); 398
399 std::vector<ui::mojom::AcceleratorTransportPtr> accelerators_2;
400 accelerators_2 = ash::mus::AddAcceleratorHelper(
401 std::move(accelerators_2), accelerator_2, std::move(matcher));
402 EXPECT_TRUE(dispatcher.AddAccelerators(std::move(accelerators_2)));
394 403
395 // Attempting to add a new accelerator with the same id should fail. 404 // Attempting to add a new accelerator with the same id should fail.
396 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::T, 405 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::T,
397 ui::mojom::kEventFlagNone); 406 ui::mojom::kEventFlagNone);
398 EXPECT_FALSE(dispatcher.AddAccelerator(accelerator_2, std::move(matcher))); 407 std::vector<ui::mojom::AcceleratorTransportPtr> accelerators_3;
408 accelerators_3 = ash::mus::AddAcceleratorHelper(
409 std::move(accelerators_3), accelerator_2, std::move(matcher));
410 EXPECT_FALSE(dispatcher.AddAccelerators(std::move(accelerators_3)));
399 411
400 // Adding the accelerator with the same id should succeed once the existing 412 // Adding the accelerator with the same id should succeed once the existing
401 // accelerator is removed. 413 // accelerator is removed.
402 dispatcher.RemoveAccelerator(accelerator_2); 414 dispatcher.RemoveAccelerator(accelerator_2);
403 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::T, 415 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::T,
404 ui::mojom::kEventFlagNone); 416 ui::mojom::kEventFlagNone);
405 EXPECT_TRUE(dispatcher.AddAccelerator(accelerator_2, std::move(matcher))); 417 std::vector<ui::mojom::AcceleratorTransportPtr> accelerators_4;
418 accelerators_4 = ash::mus::AddAcceleratorHelper(
419 std::move(accelerators_4), accelerator_2, std::move(matcher));
420 EXPECT_TRUE(dispatcher.AddAccelerators(std::move(accelerators_4)));
406 421
407 // Attempting to add an accelerator with the same matcher should fail. 422 // Attempting to add an accelerator with the same matcher should fail.
408 uint32_t accelerator_3 = 3; 423 uint32_t accelerator_3 = 3;
409 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::T, 424 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::T,
410 ui::mojom::kEventFlagNone); 425 ui::mojom::kEventFlagNone);
411 EXPECT_FALSE(dispatcher.AddAccelerator(accelerator_3, std::move(matcher))); 426 std::vector<ui::mojom::AcceleratorTransportPtr> accelerators_5;
427 accelerators_5 = ash::mus::AddAcceleratorHelper(
428 std::move(accelerators_5), accelerator_3, std::move(matcher));
429 EXPECT_FALSE(dispatcher.AddAccelerators(std::move(accelerators_5)));
412 430
413 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::T, 431 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::T,
414 ui::mojom::kEventFlagControlDown); 432 ui::mojom::kEventFlagControlDown);
415 EXPECT_TRUE(dispatcher.AddAccelerator(accelerator_3, std::move(matcher))); 433 std::vector<ui::mojom::AcceleratorTransportPtr> accelerators_6;
434 accelerators_6 = ash::mus::AddAcceleratorHelper(
435 std::move(accelerators_6), accelerator_3, std::move(matcher));
436 EXPECT_TRUE(dispatcher.AddAccelerators(std::move(accelerators_6)));
416 } 437 }
417 438
418 TEST_F(EventDispatcherTest, EventMatching) { 439 TEST_F(EventDispatcherTest, EventMatching) {
419 TestEventDispatcherDelegate* event_dispatcher_delegate = 440 TestEventDispatcherDelegate* event_dispatcher_delegate =
420 test_event_dispatcher_delegate(); 441 test_event_dispatcher_delegate();
421 EventDispatcher* dispatcher = event_dispatcher(); 442 EventDispatcher* dispatcher = event_dispatcher();
422 443
423 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher( 444 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher(
424 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown); 445 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown);
425 uint32_t accelerator_1 = 1; 446 uint32_t accelerator_1 = 1;
426 dispatcher->AddAccelerator(accelerator_1, std::move(matcher)); 447 std::vector<ui::mojom::AcceleratorTransportPtr> accelerators_1;
448 accelerators_1 = ash::mus::AddAcceleratorHelper(
449 std::move(accelerators_1), accelerator_1, std::move(matcher));
450 dispatcher->AddAccelerators(std::move(accelerators_1));
427 451
428 ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_CONTROL_DOWN); 452 ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_CONTROL_DOWN);
429 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 453 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
430 EXPECT_EQ(accelerator_1, 454 EXPECT_EQ(accelerator_1,
431 event_dispatcher_delegate->GetAndClearLastAccelerator()); 455 event_dispatcher_delegate->GetAndClearLastAccelerator());
432 456
433 // EF_NUM_LOCK_ON should be ignored since CreateKeyMatcher defaults to 457 // EF_NUM_LOCK_ON should be ignored since CreateKeyMatcher defaults to
434 // ignoring. 458 // ignoring.
435 key = ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_W, 459 key = ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_W,
436 ui::EF_CONTROL_DOWN | ui::EF_NUM_LOCK_ON); 460 ui::EF_CONTROL_DOWN | ui::EF_NUM_LOCK_ON);
437 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 461 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
438 EXPECT_EQ(accelerator_1, 462 EXPECT_EQ(accelerator_1,
439 event_dispatcher_delegate->GetAndClearLastAccelerator()); 463 event_dispatcher_delegate->GetAndClearLastAccelerator());
440 464
441 key = ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_NONE); 465 key = ui::KeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_NONE);
442 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 466 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
443 EXPECT_EQ(0u, event_dispatcher_delegate->GetAndClearLastAccelerator()); 467 EXPECT_EQ(0u, event_dispatcher_delegate->GetAndClearLastAccelerator());
444 468
445 uint32_t accelerator_2 = 2; 469 uint32_t accelerator_2 = 2;
446 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::W, 470 matcher = ui::CreateKeyMatcher(ui::mojom::KeyboardCode::W,
447 ui::mojom::kEventFlagNone); 471 ui::mojom::kEventFlagNone);
448 dispatcher->AddAccelerator(accelerator_2, std::move(matcher)); 472 std::vector<ui::mojom::AcceleratorTransportPtr> accelerators_2;
473 accelerators_2 = ash::mus::AddAcceleratorHelper(
474 std::move(accelerators_2), accelerator_2, std::move(matcher));
475 dispatcher->AddAccelerators(std::move(accelerators_2));
449 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 476 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
450 EXPECT_EQ(accelerator_2, 477 EXPECT_EQ(accelerator_2,
451 event_dispatcher_delegate->GetAndClearLastAccelerator()); 478 event_dispatcher_delegate->GetAndClearLastAccelerator());
452 479
453 dispatcher->RemoveAccelerator(accelerator_2); 480 dispatcher->RemoveAccelerator(accelerator_2);
454 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 481 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
455 EXPECT_EQ(0u, event_dispatcher_delegate->GetAndClearLastAccelerator()); 482 EXPECT_EQ(0u, event_dispatcher_delegate->GetAndClearLastAccelerator());
456 } 483 }
457 484
458 // Tests that a post-target accelerator is not triggered by ProcessEvent. 485 // Tests that a post-target accelerator is not triggered by ProcessEvent.
459 TEST_F(EventDispatcherTest, PostTargetAccelerator) { 486 TEST_F(EventDispatcherTest, PostTargetAccelerator) {
460 TestEventDispatcherDelegate* event_dispatcher_delegate = 487 TestEventDispatcherDelegate* event_dispatcher_delegate =
461 test_event_dispatcher_delegate(); 488 test_event_dispatcher_delegate();
462 EventDispatcher* dispatcher = event_dispatcher(); 489 EventDispatcher* dispatcher = event_dispatcher();
463 490
464 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher( 491 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher(
465 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown); 492 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown);
466 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::POST_TARGET; 493 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::POST_TARGET;
467 uint32_t accelerator_1 = 1; 494 uint32_t accelerator_1 = 1;
468 dispatcher->AddAccelerator(accelerator_1, std::move(matcher)); 495 std::vector<ui::mojom::AcceleratorTransportPtr> accelerators_1;
496 accelerators_1 = ash::mus::AddAcceleratorHelper(
497 std::move(accelerators_1), accelerator_1, std::move(matcher));
498
499 dispatcher->AddAccelerators(std::move(accelerators_1));
469 500
470 ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_CONTROL_DOWN); 501 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. 502 // The post-target accelerator should be fired if there is no focused window.
472 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 503 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY);
473 EXPECT_EQ(accelerator_1, 504 EXPECT_EQ(accelerator_1,
474 event_dispatcher_delegate->GetAndClearLastAccelerator()); 505 event_dispatcher_delegate->GetAndClearLastAccelerator());
475 std::unique_ptr<DispatchedEventDetails> details = 506 std::unique_ptr<DispatchedEventDetails> details =
476 event_dispatcher_delegate->GetAndAdvanceDispatchedEventDetails(); 507 event_dispatcher_delegate->GetAndAdvanceDispatchedEventDetails();
477 EXPECT_FALSE(details); 508 EXPECT_FALSE(details);
478 509
(...skipping 24 matching lines...) Expand all
503 TEST_F(EventDispatcherTest, ProcessPost) { 534 TEST_F(EventDispatcherTest, ProcessPost) {
504 TestEventDispatcherDelegate* event_dispatcher_delegate = 535 TestEventDispatcherDelegate* event_dispatcher_delegate =
505 test_event_dispatcher_delegate(); 536 test_event_dispatcher_delegate();
506 EventDispatcher* dispatcher = event_dispatcher(); 537 EventDispatcher* dispatcher = event_dispatcher();
507 538
508 uint32_t pre_id = 1; 539 uint32_t pre_id = 1;
509 { 540 {
510 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher( 541 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher(
511 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown); 542 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown);
512 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::PRE_TARGET; 543 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::PRE_TARGET;
513 dispatcher->AddAccelerator(pre_id, std::move(matcher)); 544
545 std::vector<ui::mojom::AcceleratorTransportPtr> accelerators_1;
546 accelerators_1 = ash::mus::AddAcceleratorHelper(std::move(accelerators_1),
547 pre_id, std::move(matcher));
548
549 dispatcher->AddAccelerators(std::move(accelerators_1));
514 } 550 }
515 551
516 uint32_t post_id = 2; 552 uint32_t post_id = 2;
517 { 553 {
518 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher( 554 mojom::EventMatcherPtr matcher = ui::CreateKeyMatcher(
519 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown); 555 ui::mojom::KeyboardCode::W, ui::mojom::kEventFlagControlDown);
520 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::POST_TARGET; 556 matcher->accelerator_phase = ui::mojom::AcceleratorPhase::POST_TARGET;
521 dispatcher->AddAccelerator(post_id, std::move(matcher)); 557
558 std::vector<ui::mojom::AcceleratorTransportPtr> accelerators_2;
559 accelerators_2 = ash::mus::AddAcceleratorHelper(
560 std::move(accelerators_2), post_id, std::move(matcher));
561
562 dispatcher->AddAccelerators(std::move(accelerators_2));
522 } 563 }
523 564
524 // Set focused window for EventDispatcher dispatches key events. 565 // Set focused window for EventDispatcher dispatches key events.
525 std::unique_ptr<ServerWindow> child = CreateChildWindow(WindowId(1, 3)); 566 std::unique_ptr<ServerWindow> child = CreateChildWindow(WindowId(1, 3));
526 event_dispatcher_delegate->SetFocusedWindowFromEventDispatcher(child.get()); 567 event_dispatcher_delegate->SetFocusedWindowFromEventDispatcher(child.get());
527 568
528 // Dispatch for ANY, which should trigger PRE and not call 569 // Dispatch for ANY, which should trigger PRE and not call
529 // DispatchInputEventToWindow(). 570 // DispatchInputEventToWindow().
530 ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_CONTROL_DOWN); 571 ui::KeyEvent key(ui::ET_KEY_PRESSED, ui::VKEY_W, ui::EF_CONTROL_DOWN);
531 dispatcher->ProcessEvent(key, EventDispatcher::AcceleratorMatchPhase::ANY); 572 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. 1746 // The delegate can decide if it really wants to forward the event or not.
1706 EXPECT_EQ(child.get(), 1747 EXPECT_EQ(child.get(),
1707 test_event_dispatcher_delegate()->lost_capture_window()); 1748 test_event_dispatcher_delegate()->lost_capture_window());
1708 EXPECT_EQ(child.get(), event_dispatcher()->capture_window()); 1749 EXPECT_EQ(child.get(), event_dispatcher()->capture_window());
1709 EXPECT_EQ(kClientAreaId, event_dispatcher()->capture_window_client_id()); 1750 EXPECT_EQ(kClientAreaId, event_dispatcher()->capture_window_client_id());
1710 } 1751 }
1711 1752
1712 } // namespace test 1753 } // namespace test
1713 } // namespace ws 1754 } // namespace ws
1714 } // namespace ui 1755 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698