1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160

ash / utility / rounded_window_targeter_unittest.cc [blame]

// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/utility/rounded_window_targeter.h"

#include <memory>

#include "ui/aura/test/aura_test_base.h"
#include "ui/aura/test/test_window_delegate.h"
#include "ui/aura/window.h"
#include "ui/events/base_event_utils.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/rect.h"

namespace {

constexpr int kRadius = 80;

}  // namespace

class RoundedWindowTargeterTest : public aura::test::AuraTestBase {
 public:
  RoundedWindowTargeterTest() = default;
  RoundedWindowTargeterTest(const RoundedWindowTargeterTest&) = delete;
  RoundedWindowTargeterTest& operator=(const RoundedWindowTargeterTest&) =
      delete;
  ~RoundedWindowTargeterTest() override = default;

 protected:
  void SetUp() override {
    aura::test::AuraTestBase::SetUp();
    window_.reset(CreateNormalWindow(1, root_window(), &delegate_));
  }

  void TearDown() override {
    window_.reset();
    aura::test::AuraTestBase::TearDown();
  }

  std::unique_ptr<aura::Window> window_;

 private:
  aura::test::TestWindowDelegate delegate_;
};

/*
Window shape (global coordinates)
(0,0)_____________
    |.   * | *    | <- mouse move (1,1)
    | *    |    * |
    |*_____|     *|
    |*     (r,r) *|
    | *        *  |
    |____*___*____|
                  (2r, 2r)
This mouse event hits the square but not the circular window targeter.*/
TEST_F(RoundedWindowTargeterTest, HitTestTopLeftCorner) {
  constexpr gfx::Point kTopLeftCorner(1, 1);
  {
    // Without the RoundedWindowTargeter, the event in the top-left corner
    // should target the window.
    ui::MouseEvent move(ui::EventType::kMouseMoved, kTopLeftCorner,
                        kTopLeftCorner, ui::EventTimeForNow(), ui::EF_NONE,
                        ui::EF_NONE);
    ui::EventDispatchDetails details = GetEventSink()->OnEventFromSource(&move);
    ASSERT_FALSE(details.dispatcher_destroyed);
    EXPECT_EQ(window_.get(), move.target());
  }

  window_->SetEventTargeter(
      std::make_unique<ash::RoundedWindowTargeter>(kRadius));

  {
    // With the RoundedWindowTargeter, the event in the top-left corner should
    // fall through to the root window.
    ui::MouseEvent move(ui::EventType::kMouseMoved, kTopLeftCorner,
                        kTopLeftCorner, ui::EventTimeForNow(), ui::EF_NONE,
                        ui::EF_NONE);
    ui::EventDispatchDetails details = GetEventSink()->OnEventFromSource(&move);
    ASSERT_FALSE(details.dispatcher_destroyed);
    EXPECT_EQ(root_window(), move.target());
  }
}

/*
Window shape (global coordinates)
(0,0)_____________
    |    * | *    |
    | *    |    * |
    |*_____|     *| <- mouse move (r,r)
    |*     (r,r) *|
    | *        *  |
    |____*___*____|
                  (2r, 2r)
This mouse event hits both the square and the circular window targeter.*/
TEST_F(RoundedWindowTargeterTest, HitTestCenter) {
  constexpr gfx::Point kCenter(kRadius, kRadius);
  {
    // Without the RoundedWindowTargeter, the event in the center should target
    // the window.
    ui::MouseEvent move(ui::EventType::kMouseMoved, kCenter, kCenter,
                        ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE);
    ui::EventDispatchDetails details = GetEventSink()->OnEventFromSource(&move);
    ASSERT_FALSE(details.dispatcher_destroyed);
    EXPECT_EQ(window_.get(), move.target());
  }

  window_->SetEventTargeter(
      std::make_unique<ash::RoundedWindowTargeter>(kRadius));

  {
    // With the RoundedWindowTargeter, the event in the center should still
    // target the window.
    ui::MouseEvent move(ui::EventType::kMouseMoved, kCenter, kCenter,
                        ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE);
    ui::EventDispatchDetails details = GetEventSink()->OnEventFromSource(&move);
    ASSERT_FALSE(details.dispatcher_destroyed);
    EXPECT_EQ(window_.get(), move.target());
  }
}

/*
Window shape (global coordinates)
(0,0)_____________
    |    * | *    |
    | *    |    * |
    |*_____|     *|
    |*     (r,r) *|
    | *        *  |
    |____*___*____|
                  (2r, 2r) <- <- mouse move (2r, 2r)
This mouse event misses both the square and the circular window targeter.*/
TEST_F(RoundedWindowTargeterTest, HitTestBottomRightCorner) {
  constexpr gfx::Point kBottomRightCorner(2 * kRadius, 2 * kRadius);
  {
    // Without the RoundedWindowTargeter, the event in the bottom-right corner
    // should fall through to the root window.
    ui::MouseEvent move(ui::EventType::kMouseMoved, kBottomRightCorner,
                        kBottomRightCorner, ui::EventTimeForNow(), ui::EF_NONE,
                        ui::EF_NONE);
    ui::EventDispatchDetails details = GetEventSink()->OnEventFromSource(&move);
    ASSERT_FALSE(details.dispatcher_destroyed);
    EXPECT_EQ(root_window(), move.target());
  }

  window_->SetEventTargeter(
      std::make_unique<ash::RoundedWindowTargeter>(kRadius));

  {
    // With the RoundedWindowTargeter, the event in the bottom-right corner
    // should also fall through to the root window.
    ui::MouseEvent move(ui::EventType::kMouseMoved, kBottomRightCorner,
                        kBottomRightCorner, ui::EventTimeForNow(), ui::EF_NONE,
                        ui::EF_NONE);
    ui::EventDispatchDetails details = GetEventSink()->OnEventFromSource(&move);
    ASSERT_FALSE(details.dispatcher_destroyed);
    EXPECT_EQ(root_window(), move.target());
  }
}