File: shaped_app_window_targeter_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (281 lines) | stat: -rw-r--r-- 11,675 bytes parent folder | download | duplicates (6)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/views/apps/shaped_app_window_targeter.h"

#include <memory>
#include <utility>

#include "apps/ui/views/app_window_frame_view.h"
#include "build/build_config.h"
#include "chrome/browser/ui/views/apps/chrome_native_app_window_views_aura.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/window.h"
#include "ui/aura/window_event_dispatcher.h"
#include "ui/events/event_utils.h"
#include "ui/views/controls/webview/webview.h"
#include "ui/views/test/views_test_base.h"
#include "ui/wm/core/easy_resize_window_targeter.h"

using extensions::AppWindow;

class ShapedAppWindowTargeterTest : public views::ViewsTestBase {
 public:
  ShapedAppWindowTargeterTest() : web_view_(nullptr) {}

  ShapedAppWindowTargeterTest(const ShapedAppWindowTargeterTest&) = delete;
  ShapedAppWindowTargeterTest& operator=(const ShapedAppWindowTargeterTest&) =
      delete;

  ~ShapedAppWindowTargeterTest() override = default;

  views::Widget* widget() { return widget_.get(); }

  extensions::NativeAppWindow* app_window() { return &app_window_; }
  ChromeNativeAppWindowViewsAura* app_window_views() { return &app_window_; }

 protected:
  void SetUp() override {
    views::ViewsTestBase::SetUp();
    widget_ = std::make_unique<views::Widget>();
    views::Widget::InitParams params(
        views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET,
        views::Widget::InitParams::TYPE_WINDOW);
    params.remove_standard_frame = true;
    params.bounds = gfx::Rect(30, 30, 100, 100);
    params.context = root_window();
    widget_->Init(std::move(params));

    app_window_.set_web_view_for_testing(&web_view_);
    app_window_.set_window_for_testing(widget_.get());

    widget_->Show();
  }

  void TearDown() override {
    widget_.reset();
    views::ViewsTestBase::TearDown();
  }

  void SetWindowResizable(bool resizable) {
    widget_->GetNativeWindow()->SetProperty(
        aura::client::kResizeBehaviorKey,
        aura::client::kResizeBehaviorCanMaximize |
            aura::client::kResizeBehaviorCanMinimize |
            (resizable ? aura::client::kResizeBehaviorCanResize : 0));
  }

 private:
  views::WebView web_view_;
  std::unique_ptr<views::Widget> widget_;
  ChromeNativeAppWindowViewsAura app_window_;
};

TEST_F(ShapedAppWindowTargeterTest, HitTestBasic) {
  aura::Window* window = widget()->GetNativeWindow();
  {
    // Without any custom shapes, the event should be targeted correctly to the
    // window.
    ui::MouseEvent move(ui::EventType::kMouseMoved, gfx::Point(40, 40),
                        gfx::Point(40, 40), ui::EventTimeForNow(), ui::EF_NONE,
                        ui::EF_NONE);
    ui::EventDispatchDetails details = GetEventSink()->OnEventFromSource(&move);
    ASSERT_FALSE(details.dispatcher_destroyed);
    EXPECT_EQ(window, move.target());
  }

  auto rects = std::make_unique<AppWindow::ShapeRects>();
  rects->emplace_back();
  app_window()->UpdateShape(std::move(rects));
  {
    // With an empty custom shape, all events within the window should fall
    // through to the root window.
    ui::MouseEvent move(ui::EventType::kMouseMoved, gfx::Point(40, 40),
                        gfx::Point(40, 40), 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)
  //    30       70    90        130
  //  30 +        +-----+
  //       .      |     |             <- mouse move (40,40)
  //  70 +--------+     +---------+
  //     |           .            |   <- mouse move (80,80)
  //  90 +--------+     +---------+
  //              |     |
  // 130          +-----+
  rects = std::make_unique<AppWindow::ShapeRects>();
  rects->emplace_back(40, 0, 20, 100);
  rects->emplace_back(0, 40, 100, 20);
  app_window()->UpdateShape(std::move(rects));
  {
    // With the custom shape, the events that don't fall within the custom shape
    // will go through to the root window.
    ui::MouseEvent move(ui::EventType::kMouseMoved, gfx::Point(40, 40),
                        gfx::Point(40, 40), 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());

    // But events within the shape will still reach the window.
    ui::MouseEvent move2(ui::EventType::kMouseMoved, gfx::Point(80, 80),
                         gfx::Point(80, 80), ui::EventTimeForNow(), ui::EF_NONE,
                         ui::EF_NONE);
    details = GetEventSink()->OnEventFromSource(&move2);
    ASSERT_FALSE(details.dispatcher_destroyed);
    EXPECT_EQ(window, move2.target());
  }
}

TEST_F(ShapedAppWindowTargeterTest, HitTestOnlyForShapedWindow) {
  // Install a window-targeter on the root window that allows a window to
  // receive events outside of its bounds. Verify that this window-targeter is
  // active unless the window has a custom shape.
  gfx::Insets inset(-30);
  root_window()->SetEventTargeter(
      std::make_unique<wm::EasyResizeWindowTargeter>(inset, inset));

  aura::Window* window = widget()->GetNativeWindow();
  {
    // Without any custom shapes, an event within the window bounds should be
    // targeted correctly to the window.
    ui::MouseEvent move_inside(ui::EventType::kMouseMoved, gfx::Point(40, 40),
                               gfx::Point(40, 40), ui::EventTimeForNow(),
                               ui::EF_NONE, ui::EF_NONE);
    ui::EventDispatchDetails details =
        GetEventSink()->OnEventFromSource(&move_inside);
    ASSERT_FALSE(details.dispatcher_destroyed);
    EXPECT_EQ(window, move_inside.target());
  }

  ui::MouseEvent move_outside(ui::EventType::kMouseMoved, gfx::Point(10, 10),
                              gfx::Point(10, 10), ui::EventTimeForNow(),
                              ui::EF_NONE, ui::EF_NONE);
  SetWindowResizable(false);
  {
    // Without any custom shapes, an event that falls just outside the window
    // bounds should also be targeted correctly to the root window (for
    // non-resizable windows).
    ui::MouseEvent move(move_outside);
    ui::EventDispatchDetails details = GetEventSink()->OnEventFromSource(&move);
    ASSERT_FALSE(details.dispatcher_destroyed);
    EXPECT_EQ(root_window(), move.target());
  }

  SetWindowResizable(true);
  {
    // Without any custom shapes, an event that falls just outside the window
    // bounds should also be targeted correctly to the window, because of the
    // targeter installed on the root-window (for resizable windows).
    ui::MouseEvent move(move_outside);
    ui::EventDispatchDetails details = GetEventSink()->OnEventFromSource(&move);
    ASSERT_FALSE(details.dispatcher_destroyed);
    EXPECT_EQ(window, move.target());
  }

  auto rects = std::make_unique<AppWindow::ShapeRects>();
  rects->emplace_back(40, 0, 20, 100);
  rects->emplace_back(0, 40, 100, 20);
  app_window()->UpdateShape(std::move(rects));
  {
    // With the custom shape, the events that don't fall within the custom shape
    // will go through to the root window.
    ui::MouseEvent move(move_outside);
    ui::EventDispatchDetails details = GetEventSink()->OnEventFromSource(&move);
    ASSERT_FALSE(details.dispatcher_destroyed);
    EXPECT_EQ(root_window(), move.target());
  }

  // Remove the custom shape. This should restore the behaviour of targeting the
  // app window for events just outside its bounds (for a resizable window).
  app_window()->UpdateShape(std::unique_ptr<AppWindow::ShapeRects>());
  SetWindowResizable(true);
  {
    ui::MouseEvent move(move_outside);
    ui::EventDispatchDetails details = GetEventSink()->OnEventFromSource(&move);
    ASSERT_FALSE(details.dispatcher_destroyed);
    EXPECT_EQ(window, move.target());
  }

  // Make the window non-resizable. Events near (but outside) of the window
  // should reach the root window.
  SetWindowResizable(false);
  {
    ui::MouseEvent move(move_outside);
    ui::EventDispatchDetails details = GetEventSink()->OnEventFromSource(&move);
    ASSERT_FALSE(details.dispatcher_destroyed);
    EXPECT_EQ(root_window(), move.target());
  }
}

// Tests targeting of events on a window with an EasyResizeWindowTargeter
// installed on its container.
TEST_F(ShapedAppWindowTargeterTest, ResizeInsetsWithinBounds) {
  aura::Window* window = widget()->GetNativeWindow();
  {
    // An event in the center of the window should always have
    // |window| as its target.
    ui::MouseEvent move(ui::EventType::kMouseMoved, gfx::Point(80, 80),
                        gfx::Point(80, 80), ui::EventTimeForNow(), ui::EF_NONE,
                        ui::EF_NONE);
    ui::EventDispatchDetails details = GetEventSink()->OnEventFromSource(&move);
    ASSERT_FALSE(details.dispatcher_destroyed);
    EXPECT_EQ(window, move.target());
  }
  {
    // Without an EasyResizeTargeter on the container, an event
    // inside the window and within 5px of an edge should have
    // |window| as its target.
    ui::MouseEvent move(ui::EventType::kMouseMoved, gfx::Point(32, 37),
                        gfx::Point(32, 37), ui::EventTimeForNow(), ui::EF_NONE,
                        ui::EF_NONE);
    ui::EventDispatchDetails details = GetEventSink()->OnEventFromSource(&move);
    ASSERT_FALSE(details.dispatcher_destroyed);
    EXPECT_EQ(window, move.target());
  }

#if !BUILDFLAG(IS_CHROMEOS)
  // The non standard app frame has a easy resize targetter installed.
  std::unique_ptr<views::NonClientFrameView> frame(
      app_window_views()->CreateNonStandardAppFrame());
  {
    // Ensure that the window has an event targeter (there should be an
    // EasyResizeWindowTargeter installed).
    EXPECT_TRUE(static_cast<ui::EventTarget*>(window)->GetEventTargeter());
  }
  {
    // An event in the center of the window should always have
    // |window| as its target.
    // TODO(mgiuca): This isn't really testing anything (note that it has the
    // same expectation as the border case below). In the real environment, the
    // target will actually be the RenderWidgetHostViewAura's window that is the
    // child of the child of |window|, whereas in the border case it *will* be
    // |window|. However, since this test environment does not have a
    // RenderWidgetHostViewAura, we cannot differentiate the two cases. Fix
    // the test environment so that the test can assert that non-border events
    // bubble down to a child of |window|.
    ui::MouseEvent move(ui::EventType::kMouseMoved, gfx::Point(80, 80),
                        gfx::Point(80, 80), ui::EventTimeForNow(), ui::EF_NONE,
                        ui::EF_NONE);
    ui::EventDispatchDetails details = GetEventSink()->OnEventFromSource(&move);
    ASSERT_FALSE(details.dispatcher_destroyed);
    EXPECT_EQ(window, move.target());
  }
  {
    // With an EasyResizeTargeter on the container, an event
    // inside the window and within 5px of an edge should have
    // |window| as its target.
    ui::MouseEvent move(ui::EventType::kMouseMoved, gfx::Point(32, 37),
                        gfx::Point(32, 37), ui::EventTimeForNow(), ui::EF_NONE,
                        ui::EF_NONE);
    ui::EventDispatchDetails details = GetEventSink()->OnEventFromSource(&move);
    ASSERT_FALSE(details.dispatcher_destroyed);
    EXPECT_EQ(window, move.target());
  }
#endif  // defined (OS_CHROMEOS)
}