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 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/views/interaction/interaction_test_util_mouse.h"
#include <memory>
#include <utility>
#include <variant>
#include "base/auto_reset.h"
#include "base/check.h"
#include "base/containers/contains.h"
#include "base/functional/callback_forward.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/run_loop.h"
#include "base/scoped_observation.h"
#include "base/task/single_thread_task_runner.h"
#include "base/threading/thread.h"
#include "build/build_config.h"
#include "ui/base/test/ui_controls.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/views/widget/widget.h"
#if defined(USE_AURA)
#include "ui/aura/client/drag_drop_client.h"
#include "ui/aura/client/drag_drop_client_observer.h"
#include "ui/aura/window.h"
#include "ui/aura/window_observer.h"
#endif // defined(USE_AURA)
// Currently, touch is only supported on ChromeOS Ash.
#if BUILDFLAG(IS_CHROMEOS)
#define TOUCH_INPUT_SUPPORTED 1
#else
#define TOUCH_INPUT_SUPPORTED 0
#endif
namespace views::test {
namespace {
raw_ptr<InteractionTestUtilMouse> g_current_mouse_util = nullptr;
void PostTask(base::OnceClosure task) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(FROM_HERE,
std::move(task));
}
#if TOUCH_INPUT_SUPPORTED
ui_controls::TouchType GetTouchAction(ui_controls::MouseButtonState state) {
switch (state) {
case ui_controls::DOWN:
return ui_controls::kTouchPress;
case ui_controls::UP:
return ui_controls::kTouchRelease;
}
}
int GetTouchCount(ui_controls::MouseButton button) {
switch (button) {
case ui_controls::LEFT:
return 1;
case ui_controls::MIDDLE:
return 3;
case ui_controls::RIGHT:
return 2;
}
}
#endif // TOUCH_INPUT_SUPPORTED
} // namespace
InteractionTestUtilMouse::GestureParams::GestureParams() = default;
InteractionTestUtilMouse::GestureParams::GestureParams(
gfx::NativeWindow window_hint_,
bool force_async_)
: window_hint(window_hint_), force_async(force_async_) {}
InteractionTestUtilMouse::GestureParams::GestureParams(const GestureParams&) =
default;
InteractionTestUtilMouse::GestureParams&
InteractionTestUtilMouse::GestureParams::operator=(const GestureParams&) =
default;
InteractionTestUtilMouse::GestureParams::GestureParams(
GestureParams&&) noexcept = default;
InteractionTestUtilMouse::GestureParams&
InteractionTestUtilMouse::GestureParams::operator=(GestureParams&&) noexcept =
default;
InteractionTestUtilMouse::GestureParams::~GestureParams() = default;
#if defined(USE_AURA)
// Ends any drag currently in progress or that starts during this object's
// lifetime. This is needed because the drag controller can get out of sync with
// mouse event handling - especially when running ChromeOS-on-Linux. This can
// result in weird test hangs/timeouts during mouse-up after a drag, or (more
// insidiously) during test shutdown.
//
// Once started, the DragEnder will kill any drags that start until:
// - Stop() is called.
// - The Aura window it is watching goes away.
// - The DragEnder is destroyed (which should happen no earlier than the end of
// ShutDownOnMainThread()).
class InteractionTestUtilMouse::DragEnder
: public aura::client::DragDropClientObserver,
public aura::WindowObserver {
public:
explicit DragEnder(aura::Window* window) : window_(window) {
window_observation_.Observe(window);
}
~DragEnder() override = default;
// Either cancels a current drag, or starts observing for a future drag start
// event (at which point the drag will be canceled).
void Start() {
if (CancelDragNow() || drag_client_observation_.IsObserving()) {
return;
}
// Only Ash actually supports observing the drag-drop client. Therefore, on
// other platforms, only direct cancel is possible.
#if BUILDFLAG(IS_CHROMEOS)
if (auto* const client = GetClient()) {
drag_client_observation_.Observe(client);
}
#endif
}
// Stops any ongoing observation of drag start events.
void Stop() { drag_client_observation_.Reset(); }
// Cancels any drag that is currently happening, but does not watch for future
// drag start events.
bool CancelDragNow() {
if (auto* const client = GetClient()) {
if (client->IsDragDropInProgress()) {
LOG(WARNING)
<< "InteractionTestUtilMouse: Force-canceling spurious drag "
"operation.\n"
<< "This can happen when the drag controller gets out of sync with "
"mouse events being sent by the test, and is especially likely "
"on ChromeOS-on-Linux.\n"
<< "This is not necessarily a serious error if the test functions "
"normally; however, if you see this too often or your test "
"flakes as a result of the cancel you may need to restructure "
"the test so that you can be sure the drag has started before "
"attempting to invoke ReleaseMouse().";
client->DragCancel();
return true;
}
}
return false;
}
base::WeakPtr<DragEnder> GetWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
private:
// aura::client::DragDropClientObserver:
void OnDragStarted() override {
drag_client_observation_.Reset();
PostTask(base::BindOnce(base::IgnoreResult(&DragEnder::CancelDragNow),
weak_ptr_factory_.GetWeakPtr()));
}
// aura::WindowObserver:
void OnWindowDestroying(aura::Window* window) override {
DCHECK_EQ(window_, window);
drag_client_observation_.Reset();
window_observation_.Reset();
window_ = nullptr;
}
aura::client::DragDropClient* GetClient() {
return window_ ? aura::client::GetDragDropClient(window_->GetRootWindow())
: nullptr;
}
// Since there is no "DragDropClientDestroying" event, use the aura::Window as
// a proxy for the existence of the DragDropClient, and unregister listeners
// when the window goes away. If this is not done, UAFs may happen when the
// scoped observation of the drag client goes away.
raw_ptr<aura::Window> window_;
base::ScopedObservation<aura::Window, aura::WindowObserver>
window_observation_{this};
base::ScopedObservation<aura::client::DragDropClient,
aura::client::DragDropClientObserver>
drag_client_observation_{this};
base::WeakPtrFactory<DragEnder> weak_ptr_factory_{this};
};
#endif // defined(USE_AURA)
InteractionTestUtilMouse::InteractionTestUtilMouse(views::Widget* widget)
: InteractionTestUtilMouse(widget->GetNativeWindow()) {}
InteractionTestUtilMouse::~InteractionTestUtilMouse() {
CHECK(!performing_gestures_)
<< "InteractionTestUtilMouse destroyed with pending actions.";
LOG_IF(ERROR, g_current_mouse_util != this)
<< "Expected |this| to be current InteractionTestUtilMouse.";
g_current_mouse_util = nullptr;
}
// static
InteractionTestUtilMouse::MouseGesture InteractionTestUtilMouse::MoveTo(
gfx::Point point) {
return MouseGesture(point);
}
// static
InteractionTestUtilMouse::MouseGesture InteractionTestUtilMouse::MouseDown(
ui_controls::MouseButton button,
int modifier_keys) {
return MouseButtonGesture(button, ui_controls::DOWN, modifier_keys);
}
// static
InteractionTestUtilMouse::MouseGesture InteractionTestUtilMouse::MouseUp(
ui_controls::MouseButton button,
int modifier_keys) {
return MouseButtonGesture(button, ui_controls::UP, modifier_keys);
}
// static
InteractionTestUtilMouse::MouseGestures InteractionTestUtilMouse::Click(
ui_controls::MouseButton button,
int modifier_keys) {
return MouseGestures{MouseDown(button, modifier_keys),
MouseUp(button, modifier_keys)};
}
// static
InteractionTestUtilMouse::MouseGestures InteractionTestUtilMouse::DragAndHold(
gfx::Point destination) {
return MouseGestures{MouseDown(ui_controls::LEFT), MoveTo(destination)};
}
// static
InteractionTestUtilMouse::MouseGestures
InteractionTestUtilMouse::DragAndRelease(gfx::Point destination) {
return MouseGestures{MouseDown(ui_controls::LEFT), MoveTo(destination),
MouseUp(ui_controls::LEFT)};
}
bool InteractionTestUtilMouse::ShouldCancelDrag() const {
#if defined(USE_AURA)
return dragging_;
#else
return false;
#endif
}
void InteractionTestUtilMouse::CancelFutureDrag() {
#if defined(USE_AURA)
// Allow the system to finish processing any mouse input before force-
// canceling any ongoing drag. It's possible that a drag that was queued to
// complete simply hasn't yet.
PostTask(base::BindOnce(&DragEnder::Start, drag_ender_->GetWeakPtr()));
#endif
}
void InteractionTestUtilMouse::CancelDragNow() {
#if defined(USE_AURA)
CHECK(!dragging_);
drag_ender_->Stop();
drag_ender_->CancelDragNow();
#endif
}
bool InteractionTestUtilMouse::SendButtonPress(
const MouseButtonGesture& gesture,
const GestureParams& params,
base::OnceClosure on_complete) {
if (!params.force_async) {
#if TOUCH_INPUT_SUPPORTED
if (touch_mode_) {
return ui_controls::SendTouchEventsNotifyWhenDone(
GetTouchAction(gesture.button_state), GetTouchCount(gesture.button),
touch_hover_point_.x(), touch_hover_point_.y(),
std::move(on_complete));
}
#endif // TOUCH_INPUT_SUPPORTED
return ui_controls::SendMouseEventsNotifyWhenDone(
gesture.button, gesture.button_state, std::move(on_complete),
gesture.modifier_keys, params.window_hint);
}
#if TOUCH_INPUT_SUPPORTED
if (touch_mode_) {
PostTask(base::BindOnce(
[](base::WeakPtr<InteractionTestUtilMouse> util,
base::OnceClosure on_complete, MouseButtonGesture gesture,
gfx::Point target) {
if (!util) {
return;
}
CHECK(ui_controls::SendTouchEventsNotifyWhenDone(
GetTouchAction(gesture.button_state),
GetTouchCount(gesture.button), target.x(), target.y(),
std::move(on_complete)));
},
weak_ptr_factory_.GetWeakPtr(), std::move(on_complete), gesture,
touch_hover_point_));
return true;
}
#endif // TOUCH_INPUT_SUPPORTED
PostTask(base::BindOnce(
[](base::WeakPtr<InteractionTestUtilMouse> util,
base::OnceClosure on_complete, MouseButtonGesture gesture,
gfx::NativeWindow window_hint) {
if (!util) {
return;
}
CHECK(ui_controls::SendMouseEventsNotifyWhenDone(
gesture.button, gesture.button_state, std::move(on_complete),
gesture.modifier_keys, window_hint));
},
weak_ptr_factory_.GetWeakPtr(), std::move(on_complete), gesture,
params.window_hint));
return true;
}
bool InteractionTestUtilMouse::SendMove(const MouseMoveGesture& gesture,
const GestureParams& params,
base::OnceClosure on_complete) {
#if TOUCH_INPUT_SUPPORTED
if (touch_mode_) {
// Need to remember where our finger is.
touch_hover_point_ = gesture;
// If no fingers are down, there's nothing to do.
if (buttons_down_.empty()) {
std::move(on_complete).Run();
return true;
}
// Should never have two different sets of fingers down at once.
CHECK_EQ(1U, buttons_down_.size());
}
#endif // TOUCH_INPUT_SUPPORTED
if (!params.force_async) {
#if TOUCH_INPUT_SUPPORTED
if (touch_mode_) {
return ui_controls::SendTouchEventsNotifyWhenDone(
ui_controls::kTouchMove, GetTouchCount(*buttons_down_.begin()),
gesture.x(), gesture.y(), std::move(on_complete));
}
#endif // TOUCH_INPUT_SUPPORTED
return ui_controls::SendMouseMoveNotifyWhenDone(
gesture.x(), gesture.y(), std::move(on_complete), params.window_hint);
}
#if TOUCH_INPUT_SUPPORTED
if (touch_mode_) {
PostTask(base::BindOnce(
[](base::WeakPtr<InteractionTestUtilMouse> util,
base::OnceClosure on_complete, MouseMoveGesture gesture) {
if (!util) {
return;
}
const int touch_count = GetTouchCount(*util->buttons_down_.begin());
CHECK(ui_controls::SendTouchEventsNotifyWhenDone(
ui_controls::kTouchMove, touch_count, gesture.x(), gesture.y(),
std::move(on_complete)));
},
weak_ptr_factory_.GetWeakPtr(), std::move(on_complete), gesture));
return true;
}
#endif // TOUCH_INPUT_SUPPORTED
PostTask(base::BindOnce(
[](base::WeakPtr<InteractionTestUtilMouse> util,
base::OnceClosure on_complete, MouseMoveGesture gesture,
gfx::NativeWindow window_hint) {
if (!util) {
return;
}
CHECK(ui_controls::SendMouseMoveNotifyWhenDone(
gesture.x(), gesture.y(), std::move(on_complete), window_hint));
},
weak_ptr_factory_.GetWeakPtr(), std::move(on_complete), gesture,
params.window_hint));
return true;
}
bool InteractionTestUtilMouse::SetTouchMode(bool touch_mode) {
if (touch_mode == touch_mode_) {
return true;
}
CHECK(buttons_down_.empty())
<< "Cannot toggle touch mode when buttons or fingers are down.";
#if TOUCH_INPUT_SUPPORTED
touch_mode_ = touch_mode;
return true;
#else
LOG(WARNING) << "Touch mode not supported on this platform.";
return false;
#endif
}
bool InteractionTestUtilMouse::GetTouchMode() const {
return touch_mode_;
}
bool InteractionTestUtilMouse::PerformGesturesImpl(const GestureParams& params,
MouseGestures gestures) {
CHECK(!gestures.empty());
CHECK(!performing_gestures_);
base::AutoReset<bool> performing_gestures(&performing_gestures_, true);
canceled_ = false;
for (auto& gesture : gestures) {
if (canceled_) {
break;
}
base::RunLoop run_loop{base::RunLoop::Type::kNestableTasksAllowed};
if (MouseButtonGesture* const button =
std::get_if<MouseButtonGesture>(&gesture)) {
switch (button->button_state) {
case ui_controls::UP: {
CHECK(buttons_down_.erase(button->button));
base::OnceClosure on_complete =
params.force_async ? base::DoNothing() : run_loop.QuitClosure();
if (ShouldCancelDrag()) {
// This will bail out of any nested drag-drop run loop, allowing
// the code to proceed even if the drag somehow starts while the
// mouse-up is being processed.
on_complete = std::move(on_complete)
.Then(base::BindOnce(
&InteractionTestUtilMouse::CancelFutureDrag,
weak_ptr_factory_.GetWeakPtr()));
}
#if defined(USE_AURA)
dragging_ = false;
#endif
if (!SendButtonPress(*button, params, std::move(on_complete))) {
LOG(ERROR) << "Mouse button " << button->button << " up failed.";
return false;
}
if (!params.force_async) {
run_loop.Run();
}
break;
}
case ui_controls::DOWN:
#if TOUCH_INPUT_SUPPORTED
CHECK(!touch_mode_ || buttons_down_.empty())
<< "In touch mode, only one set of fingers may be down at any "
"given time.";
#endif
CHECK(buttons_down_.insert(button->button).second);
#if BUILDFLAG(IS_MAC)
if (!params.force_async && button->button == ui_controls::RIGHT) {
LOG(WARNING)
<< "InteractionTestUtilMouse::PerformGestures() - "
"Important note:\n"
<< "Because right-clicking on Mac typically results in a "
"context menu, and because some (but not all) context menus "
"on Mac are native and take over the main message loop, "
"right-clicking could cause the test to hang.\n"
<< "If you notice your test hangs on Mac, use "
"MayInvolveNativeContextMenu() and minimize the number of "
"test "
"steps performed while the context menu is open.";
}
#endif
CancelDragNow();
if (!SendButtonPress(*button, params,
params.force_async ? base::DoNothing()
: run_loop.QuitClosure())) {
LOG(ERROR) << "Mouse button " << button->button << " down failed.";
return false;
}
if (!params.force_async) {
run_loop.Run();
}
break;
}
} else {
const auto& move = std::get<MouseMoveGesture>(gesture);
#if defined(USE_AURA)
if (!buttons_down_.empty()) {
CHECK(base::Contains(buttons_down_, ui_controls::LEFT));
dragging_ = true;
}
#endif
if (!SendMove(move, params,
params.force_async ? base::DoNothing()
: run_loop.QuitClosure())) {
LOG(ERROR) << "Mouse move to " << move.ToString() << " failed.";
return false;
}
if (!params.force_async) {
run_loop.Run();
}
}
}
return !canceled_;
}
void InteractionTestUtilMouse::CancelAllGestures() {
weak_ptr_factory_.InvalidateWeakPtrs();
canceled_ = true;
#if TOUCH_INPUT_SUPPORTED
if (touch_mode_ && !buttons_down_.empty()) {
// Should never get in a state where multiple finger combinations are down
// at the same time.
CHECK_EQ(1U, buttons_down_.size());
const auto& button = *buttons_down_.begin();
if (!ui_controls::SendTouchEvents(
ui_controls::kTouchRelease, GetTouchCount(button),
touch_hover_point_.x(), touch_hover_point_.y())) {
LOG(WARNING) << "Unable to send touch up.";
}
buttons_down_.clear();
}
#endif // TOUCH_INPUT_SUPPORTED
// Now that no additional actions will happen, release all mouse buttons.
for (ui_controls::MouseButton button : buttons_down_) {
if (!ui_controls::SendMouseEvents(button, ui_controls::UP)) {
LOG(WARNING) << "Unable to release mouse button " << button;
}
}
buttons_down_.clear();
// Maybe handle dragging stopped.
if (ShouldCancelDrag()) {
CancelFutureDrag();
}
}
InteractionTestUtilMouse::InteractionTestUtilMouse(gfx::NativeWindow window)
#if defined(USE_AURA)
: drag_ender_(std::make_unique<DragEnder>(window))
#endif
{
CHECK(window);
CHECK(!g_current_mouse_util)
<< "Cannot have multiple overlapping InteractionTestUtilMouse instances";
g_current_mouse_util = this;
}
// static
void InteractionTestUtilMouse::AddGestures(MouseGestures& gestures,
MouseGesture to_add) {
gestures.emplace_back(std::move(to_add));
}
// static
void InteractionTestUtilMouse::AddGestures(MouseGestures& gestures,
MouseGestures to_add) {
for (auto& gesture : to_add) {
gestures.emplace_back(std::move(gesture));
}
}
} // namespace views::test
|