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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
content / browser / renderer_host / cursor_manager_unittest.cc [blame]
// 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 "components/input/cursor_manager.h"
#include <memory>
#include <utility>
#include "build/build_config.h"
#include "content/browser/renderer_host/mock_render_widget_host.h"
#include "content/browser/renderer_host/render_widget_host_impl.h"
#include "content/browser/site_instance_group.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/mock_render_process_host.h"
#include "content/public/test/test_browser_context.h"
#include "content/test/mock_render_widget_host_delegate.h"
#include "content/test/test_render_view_host.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/cursor/cursor.h"
#include "ui/base/cursor/mojom/cursor_type.mojom-shared.h"
#include "ui/gfx/geometry/point.h"
// CursorManager is only instantiated on Aura and Mac.
#if defined(USE_AURA) || BUILDFLAG(IS_MAC)
namespace content {
namespace {
const ui::Cursor kCursorHand(ui::mojom::CursorType::kHand);
const ui::Cursor kCursorCross(ui::mojom::CursorType::kCross);
const ui::Cursor kCursorPointer(ui::mojom::CursorType::kPointer);
ui::Cursor CreateCustomCursor(int width, int height) {
SkBitmap bitmap;
bitmap.allocN32Pixels(width, height);
bitmap.eraseColor(SK_ColorRED);
return ui::Cursor::NewCustom(std::move(bitmap), /*hotspot=*/gfx::Point(0, 0),
/*image_scale_factor=*/1.0f);
}
const ui::Cursor kCursorCustom = CreateCustomCursor(0, 0);
class MockRenderWidgetHostViewForCursors : public TestRenderWidgetHostView {
public:
MockRenderWidgetHostViewForCursors(RenderWidgetHost* host, bool top_view)
: TestRenderWidgetHostView(host) {
if (top_view) {
cursor_manager_ = std::make_unique<input::CursorManager>(this);
}
}
void DisplayCursor(const ui::Cursor& cursor) override {
current_cursor_ = cursor;
}
input::CursorManager* GetCursorManager() override {
return cursor_manager_.get();
}
const ui::Cursor& cursor() { return current_cursor_; }
private:
ui::Cursor current_cursor_;
std::unique_ptr<input::CursorManager> cursor_manager_;
};
class CursorManagerTest : public testing::Test {
public:
CursorManagerTest() = default;
CursorManagerTest(const CursorManagerTest&) = delete;
CursorManagerTest& operator=(const CursorManagerTest&) = delete;
void SetUp() override {
browser_context_ = std::make_unique<TestBrowserContext>();
process_host_ =
std::make_unique<MockRenderProcessHost>(browser_context_.get());
site_instance_group_ =
base::WrapRefCounted(SiteInstanceGroup::CreateForTesting(
browser_context_.get(), process_host_.get()));
widget_host_ = MakeNewWidgetHost();
top_view_ = std::make_unique<MockRenderWidgetHostViewForCursors>(
widget_host_.get(), true);
}
std::unique_ptr<RenderWidgetHostImpl> MakeNewWidgetHost() {
int32_t routing_id = process_host_->GetNextRoutingID();
return MockRenderWidgetHost::Create(
/*frame_tree=*/nullptr, &delegate_, site_instance_group_->GetSafeRef(),
routing_id);
}
void TearDown() override {
top_view_.reset();
widget_host_ = nullptr;
process_host_->Cleanup();
site_instance_group_.reset();
process_host_ = nullptr;
}
protected:
BrowserTaskEnvironment task_environment_;
std::unique_ptr<BrowserContext> browser_context_;
std::unique_ptr<MockRenderProcessHost> process_host_;
scoped_refptr<SiteInstanceGroup> site_instance_group_;
std::unique_ptr<RenderWidgetHostImpl> widget_host_;
std::unique_ptr<MockRenderWidgetHostViewForCursors> top_view_;
MockRenderWidgetHostDelegate delegate_;
};
} // namespace
// Verify basic CursorManager functionality when no OOPIFs are present.
TEST_F(CursorManagerTest, CursorOnSingleView) {
// Simulate mouse over the top-level frame without an UpdateCursor message.
top_view_->GetCursorManager()->UpdateViewUnderCursor(top_view_.get());
// The view should be using the default cursor.
EXPECT_EQ(top_view_->cursor(), ui::Cursor());
// Update the view with a non-default cursor.
top_view_->GetCursorManager()->UpdateCursor(top_view_.get(), kCursorHand);
// Verify the RenderWidgetHostView now uses the correct cursor.
EXPECT_EQ(top_view_->cursor(), kCursorHand);
}
// Verify cursor interactions between a parent frame and an out-of-process
// child frame.
TEST_F(CursorManagerTest, CursorOverChildView) {
std::unique_ptr<RenderWidgetHostImpl> widget_host(MakeNewWidgetHost());
std::unique_ptr<MockRenderWidgetHostViewForCursors> child_view(
new MockRenderWidgetHostViewForCursors(widget_host.get(), false));
// Set the child frame's cursor to a hand. This should not propagate to the
// top-level view without the mouse moving over the child frame.
top_view_->GetCursorManager()->UpdateCursor(child_view.get(), kCursorHand);
EXPECT_NE(top_view_->cursor(), kCursorHand);
// Now moving the mouse over the child frame should update the overall cursor.
top_view_->GetCursorManager()->UpdateViewUnderCursor(child_view.get());
EXPECT_EQ(top_view_->cursor(), kCursorHand);
// Destruction of the child view should restore the parent frame's cursor.
top_view_->GetCursorManager()->ViewBeingDestroyed(child_view.get());
EXPECT_NE(top_view_->cursor(), kCursorHand);
}
// Verify interactions between two independent OOPIFs, including interleaving
// cursor updates and mouse movements. This simulates potential race
// conditions between cursor updates.
TEST_F(CursorManagerTest, CursorOverMultipleChildViews) {
std::unique_ptr<RenderWidgetHostImpl> widget_host1(MakeNewWidgetHost());
std::unique_ptr<MockRenderWidgetHostViewForCursors> child_view1(
new MockRenderWidgetHostViewForCursors(widget_host1.get(), false));
std::unique_ptr<RenderWidgetHostImpl> widget_host2(MakeNewWidgetHost());
std::unique_ptr<MockRenderWidgetHostViewForCursors> child_view2(
new MockRenderWidgetHostViewForCursors(widget_host2.get(), false));
// Initialize each View to a different cursor.
top_view_->GetCursorManager()->UpdateCursor(top_view_.get(), kCursorHand);
top_view_->GetCursorManager()->UpdateCursor(child_view1.get(), kCursorCross);
top_view_->GetCursorManager()->UpdateCursor(child_view2.get(),
kCursorPointer);
EXPECT_EQ(top_view_->cursor(), kCursorHand);
// Simulate moving the mouse between child views and receiving cursor updates.
top_view_->GetCursorManager()->UpdateViewUnderCursor(child_view1.get());
EXPECT_EQ(top_view_->cursor(), kCursorCross);
top_view_->GetCursorManager()->UpdateViewUnderCursor(child_view2.get());
EXPECT_EQ(top_view_->cursor(), kCursorPointer);
// Simulate cursor updates to both child views and the parent view. An
// update to child_view1 or the parent view should not change the current
// cursor because the mouse is over child_view2.
top_view_->GetCursorManager()->UpdateCursor(child_view1.get(), kCursorHand);
EXPECT_EQ(top_view_->cursor(), kCursorPointer);
top_view_->GetCursorManager()->UpdateCursor(child_view2.get(), kCursorCross);
EXPECT_EQ(top_view_->cursor(), kCursorCross);
top_view_->GetCursorManager()->UpdateCursor(top_view_.get(), kCursorHand);
EXPECT_EQ(top_view_->cursor(), kCursorCross);
// Similarly, destroying child_view1 should have no effect on the cursor,
// but destroying child_view2 should change it.
top_view_->GetCursorManager()->ViewBeingDestroyed(child_view1.get());
EXPECT_EQ(top_view_->cursor(), kCursorCross);
top_view_->GetCursorManager()->ViewBeingDestroyed(child_view2.get());
EXPECT_EQ(top_view_->cursor(), kCursorHand);
}
TEST_F(CursorManagerTest,
CustomCursorDisallowedScope_CustomCursorsAreNotAllowed) {
top_view_->GetCursorManager()->UpdateCursor(top_view_.get(), kCursorCustom);
EXPECT_EQ(top_view_->cursor(), kCursorCustom);
{
auto disallow_scope =
top_view_->GetCursorManager()->CreateDisallowCustomCursorScope(
/*max_dimension_dips=*/0);
EXPECT_EQ(top_view_->cursor(), kCursorPointer);
}
EXPECT_EQ(top_view_->cursor(), kCursorCustom);
}
TEST_F(CursorManagerTest,
CustomCursorDisallowedScope_CustomCursorsAreNotAllowedAboveSizeLimit) {
const ui::Cursor kCursorCustomLarge = CreateCustomCursor(20, 50);
top_view_->GetCursorManager()->UpdateCursor(top_view_.get(),
kCursorCustomLarge);
EXPECT_EQ(top_view_->cursor(), kCursorCustomLarge);
{
auto disallow_scope =
top_view_->GetCursorManager()->CreateDisallowCustomCursorScope(
/*max_dimension_dips=*/40);
EXPECT_EQ(top_view_->cursor(), kCursorPointer);
}
EXPECT_EQ(top_view_->cursor(), kCursorCustomLarge);
}
TEST_F(CursorManagerTest,
CustomCursorDisallowedScope_CustomCursorsAreAllowedBelowSizeLimit) {
const ui::Cursor kCursorCustomLarge = CreateCustomCursor(20, 35);
top_view_->GetCursorManager()->UpdateCursor(top_view_.get(),
kCursorCustomLarge);
EXPECT_EQ(top_view_->cursor(), kCursorCustomLarge);
{
auto disallow_scope =
top_view_->GetCursorManager()->CreateDisallowCustomCursorScope(
/*max_dimension_dips=*/40);
EXPECT_EQ(top_view_->cursor(), kCursorCustomLarge);
}
EXPECT_EQ(top_view_->cursor(), kCursorCustomLarge);
}
TEST_F(CursorManagerTest,
CustomCursorDisallowedScope_CustomCursorSubjectToMultipleSizeLimits) {
const ui::Cursor kCursorCustomLarge = CreateCustomCursor(20, 35);
top_view_->GetCursorManager()->UpdateCursor(top_view_.get(),
kCursorCustomLarge);
EXPECT_EQ(top_view_->cursor(), kCursorCustomLarge);
{
auto disallow_scope1 =
top_view_->GetCursorManager()->CreateDisallowCustomCursorScope(
/*max_dimension_dips=*/40);
EXPECT_EQ(top_view_->cursor(), kCursorCustomLarge);
auto disallow_scope2 =
top_view_->GetCursorManager()->CreateDisallowCustomCursorScope(
/*max_dimension_dips=*/30);
EXPECT_EQ(top_view_->cursor(), kCursorPointer);
// Running the first closure leaves the restriction from the second closure
// in place.
disallow_scope1.RunAndReset();
EXPECT_EQ(top_view_->cursor(), kCursorPointer);
}
EXPECT_EQ(top_view_->cursor(), kCursorCustomLarge);
}
TEST_F(CursorManagerTest,
CustomCursorDisallowedScope_OtherCursorsStillAllowed) {
top_view_->GetCursorManager()->UpdateCursor(top_view_.get(), kCursorHand);
EXPECT_EQ(top_view_->cursor(), kCursorHand);
{
auto disallow_scope =
top_view_->GetCursorManager()->CreateDisallowCustomCursorScope(
/*max_dimension_dips=*/0);
EXPECT_EQ(top_view_->cursor(), kCursorHand);
}
EXPECT_EQ(top_view_->cursor(), kCursorHand);
}
TEST_F(CursorManagerTest,
CustomCursorDisallowedScope_CustomCursorSetDuringScope) {
top_view_->GetCursorManager()->UpdateCursor(top_view_.get(), kCursorHand);
EXPECT_EQ(top_view_->cursor(), kCursorHand);
{
auto disallow_scope =
top_view_->GetCursorManager()->CreateDisallowCustomCursorScope(
/*max_dimension_dips=*/0);
EXPECT_EQ(top_view_->cursor(), kCursorHand);
top_view_->GetCursorManager()->UpdateCursor(top_view_.get(), kCursorCustom);
EXPECT_EQ(top_view_->cursor(), kCursorPointer);
}
EXPECT_EQ(top_view_->cursor(), kCursorCustom);
}
TEST_F(CursorManagerTest,
CustomCursorDisallowedScope_CustomCursorRemovedDuringScope) {
top_view_->GetCursorManager()->UpdateCursor(top_view_.get(), kCursorCustom);
EXPECT_EQ(top_view_->cursor(), kCursorCustom);
{
auto disallow_scope =
top_view_->GetCursorManager()->CreateDisallowCustomCursorScope(
/*max_dimension_dips=*/0);
EXPECT_EQ(top_view_->cursor(), kCursorPointer);
top_view_->GetCursorManager()->UpdateCursor(top_view_.get(), kCursorHand);
EXPECT_EQ(top_view_->cursor(), kCursorHand);
}
EXPECT_EQ(top_view_->cursor(), kCursorHand);
}
TEST_F(CursorManagerTest, CustomCursorDisallowedScope_MultipleScopes) {
top_view_->GetCursorManager()->UpdateCursor(top_view_.get(), kCursorCustom);
EXPECT_EQ(top_view_->cursor(), kCursorCustom);
auto disallow_scope1 =
top_view_->GetCursorManager()->CreateDisallowCustomCursorScope(
/*max_dimension_dips=*/0);
auto disallow_scope2 =
top_view_->GetCursorManager()->CreateDisallowCustomCursorScope(
/*max_dimension_dips=*/0);
EXPECT_EQ(top_view_->cursor(), kCursorPointer);
disallow_scope1.RunAndReset();
EXPECT_EQ(top_view_->cursor(), kCursorPointer);
auto disallow_scope3 =
top_view_->GetCursorManager()->CreateDisallowCustomCursorScope(
/*max_dimension_dips=*/0);
disallow_scope2.RunAndReset();
EXPECT_EQ(top_view_->cursor(), kCursorPointer);
disallow_scope3.RunAndReset();
EXPECT_EQ(top_view_->cursor(), kCursorCustom);
}
TEST_F(CursorManagerTest, CustomCursorDisallowedScope_CustomCursorViewFocused) {
std::unique_ptr<RenderWidgetHostImpl> widget_host(MakeNewWidgetHost());
std::unique_ptr<MockRenderWidgetHostViewForCursors> child_view(
new MockRenderWidgetHostViewForCursors(widget_host.get(), false));
top_view_->GetCursorManager()->UpdateCursor(top_view_.get(), kCursorHand);
top_view_->GetCursorManager()->UpdateCursor(child_view.get(), kCursorCustom);
EXPECT_EQ(top_view_->cursor(), kCursorHand);
{
auto disallow_scope =
top_view_->GetCursorManager()->CreateDisallowCustomCursorScope(
/*max_dimension_dips=*/0);
EXPECT_EQ(top_view_->cursor(), kCursorHand);
top_view_->GetCursorManager()->UpdateViewUnderCursor(child_view.get());
EXPECT_EQ(top_view_->cursor(), kCursorPointer);
}
EXPECT_EQ(top_view_->cursor(), kCursorCustom);
top_view_->GetCursorManager()->ViewBeingDestroyed(child_view.get());
}
TEST_F(CursorManagerTest,
CustomCursorDisallowedScope_CustomCursorViewFocusRemoved) {
std::unique_ptr<RenderWidgetHostImpl> widget_host(MakeNewWidgetHost());
std::unique_ptr<MockRenderWidgetHostViewForCursors> child_view(
new MockRenderWidgetHostViewForCursors(widget_host.get(), false));
top_view_->GetCursorManager()->UpdateCursor(top_view_.get(), kCursorHand);
top_view_->GetCursorManager()->UpdateCursor(child_view.get(), kCursorCustom);
top_view_->GetCursorManager()->UpdateViewUnderCursor(child_view.get());
EXPECT_EQ(top_view_->cursor(), kCursorCustom);
{
auto disallow_scope =
top_view_->GetCursorManager()->CreateDisallowCustomCursorScope(
/*max_dimension_dips=*/0);
EXPECT_EQ(top_view_->cursor(), kCursorPointer);
top_view_->GetCursorManager()->UpdateViewUnderCursor(top_view_.get());
EXPECT_EQ(top_view_->cursor(), kCursorHand);
}
EXPECT_EQ(top_view_->cursor(), kCursorHand);
}
} // namespace content
#endif // defined(USE_AURA) || BUILDFLAG(IS_MAC)