rounded_window_targeter_unittest.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/utility/rounded_window_targeter.h"
  5. #include <memory>
  6. #include "ui/aura/test/aura_test_base.h"
  7. #include "ui/aura/test/test_window_delegate.h"
  8. #include "ui/aura/window.h"
  9. #include "ui/events/base_event_utils.h"
  10. #include "ui/gfx/geometry/point.h"
  11. #include "ui/gfx/geometry/rect.h"
  12. namespace {
  13. constexpr int kRadius = 80;
  14. } // namespace
  15. class RoundedWindowTargeterTest : public aura::test::AuraTestBase {
  16. public:
  17. RoundedWindowTargeterTest() = default;
  18. RoundedWindowTargeterTest(const RoundedWindowTargeterTest&) = delete;
  19. RoundedWindowTargeterTest& operator=(const RoundedWindowTargeterTest&) =
  20. delete;
  21. ~RoundedWindowTargeterTest() override = default;
  22. protected:
  23. void SetUp() override {
  24. aura::test::AuraTestBase::SetUp();
  25. window_.reset(CreateNormalWindow(1, root_window(), &delegate_));
  26. }
  27. void TearDown() override {
  28. window_.reset();
  29. aura::test::AuraTestBase::TearDown();
  30. }
  31. std::unique_ptr<aura::Window> window_;
  32. private:
  33. aura::test::TestWindowDelegate delegate_;
  34. };
  35. /*
  36. Window shape (global coordinates)
  37. (0,0)_____________
  38. |. * | * | <- mouse move (1,1)
  39. | * | * |
  40. |*_____| *|
  41. |* (r,r) *|
  42. | * * |
  43. |____*___*____|
  44. (2r, 2r)
  45. This mouse event hits the square but not the circular window targeter.*/
  46. TEST_F(RoundedWindowTargeterTest, HitTestTopLeftCorner) {
  47. constexpr gfx::Point kTopLeftCorner(1, 1);
  48. {
  49. // Without the RoundedWindowTargeter, the event in the top-left corner
  50. // should target the window.
  51. ui::MouseEvent move(ui::ET_MOUSE_MOVED, kTopLeftCorner, kTopLeftCorner,
  52. ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE);
  53. ui::EventDispatchDetails details = GetEventSink()->OnEventFromSource(&move);
  54. ASSERT_FALSE(details.dispatcher_destroyed);
  55. EXPECT_EQ(window_.get(), move.target());
  56. }
  57. window_->SetEventTargeter(
  58. std::make_unique<ash::RoundedWindowTargeter>(kRadius));
  59. {
  60. // With the RoundedWindowTargeter, the event in the top-left corner should
  61. // fall through to the root window.
  62. ui::MouseEvent move(ui::ET_MOUSE_MOVED, kTopLeftCorner, kTopLeftCorner,
  63. ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE);
  64. ui::EventDispatchDetails details = GetEventSink()->OnEventFromSource(&move);
  65. ASSERT_FALSE(details.dispatcher_destroyed);
  66. EXPECT_EQ(root_window(), move.target());
  67. }
  68. }
  69. /*
  70. Window shape (global coordinates)
  71. (0,0)_____________
  72. | * | * |
  73. | * | * |
  74. |*_____| *| <- mouse move (r,r)
  75. |* (r,r) *|
  76. | * * |
  77. |____*___*____|
  78. (2r, 2r)
  79. This mouse event hits both the square and the circular window targeter.*/
  80. TEST_F(RoundedWindowTargeterTest, HitTestCenter) {
  81. constexpr gfx::Point kCenter(kRadius, kRadius);
  82. {
  83. // Without the RoundedWindowTargeter, the event in the center should target
  84. // the window.
  85. ui::MouseEvent move(ui::ET_MOUSE_MOVED, kCenter, kCenter,
  86. ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE);
  87. ui::EventDispatchDetails details = GetEventSink()->OnEventFromSource(&move);
  88. ASSERT_FALSE(details.dispatcher_destroyed);
  89. EXPECT_EQ(window_.get(), move.target());
  90. }
  91. window_->SetEventTargeter(
  92. std::make_unique<ash::RoundedWindowTargeter>(kRadius));
  93. {
  94. // With the RoundedWindowTargeter, the event in the center should still
  95. // target the window.
  96. ui::MouseEvent move(ui::ET_MOUSE_MOVED, kCenter, kCenter,
  97. ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE);
  98. ui::EventDispatchDetails details = GetEventSink()->OnEventFromSource(&move);
  99. ASSERT_FALSE(details.dispatcher_destroyed);
  100. EXPECT_EQ(window_.get(), move.target());
  101. }
  102. }
  103. /*
  104. Window shape (global coordinates)
  105. (0,0)_____________
  106. | * | * |
  107. | * | * |
  108. |*_____| *|
  109. |* (r,r) *|
  110. | * * |
  111. |____*___*____|
  112. (2r, 2r) <- <- mouse move (2r, 2r)
  113. This mouse event misses both the square and the circular window targeter.*/
  114. TEST_F(RoundedWindowTargeterTest, HitTestBottomRightCorner) {
  115. constexpr gfx::Point kBottomRightCorner(2 * kRadius, 2 * kRadius);
  116. {
  117. // Without the RoundedWindowTargeter, the event in the bottom-right corner
  118. // should fall through to the root window.
  119. ui::MouseEvent move(ui::ET_MOUSE_MOVED, kBottomRightCorner,
  120. kBottomRightCorner, ui::EventTimeForNow(), ui::EF_NONE,
  121. ui::EF_NONE);
  122. ui::EventDispatchDetails details = GetEventSink()->OnEventFromSource(&move);
  123. ASSERT_FALSE(details.dispatcher_destroyed);
  124. EXPECT_EQ(root_window(), move.target());
  125. }
  126. window_->SetEventTargeter(
  127. std::make_unique<ash::RoundedWindowTargeter>(kRadius));
  128. {
  129. // With the RoundedWindowTargeter, the event in the bottom-right corner
  130. // should also fall through to the root window.
  131. ui::MouseEvent move(ui::ET_MOUSE_MOVED, kBottomRightCorner,
  132. kBottomRightCorner, ui::EventTimeForNow(), ui::EF_NONE,
  133. ui::EF_NONE);
  134. ui::EventDispatchDetails details = GetEventSink()->OnEventFromSource(&move);
  135. ASSERT_FALSE(details.dispatcher_destroyed);
  136. EXPECT_EQ(root_window(), move.target());
  137. }
  138. }