Commit 9fa3f8ac authored by Peter Kasting's avatar Peter Kasting Committed by Commit Bot

Add test-only hook to get a callback when dragging enters a target view.

This will be used by drag and drop tests to take action on dragging over various
views (typically, sending a mouse release event to end the drag).

Bug: 923188
Change-Id: I40a3885cf8076f9e0a5f0490ca1eda3f32907086
Reviewed-on: https://chromium-review.googlesource.com/c/1493435
Auto-Submit: Peter Kasting <pkasting@chromium.org>
Reviewed-by: default avatarScott Violet <sky@chromium.org>
Commit-Queue: Peter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/master@{#636343}
parent 5c3aa450
......@@ -4,6 +4,8 @@
#include "ui/views/widget/drop_helper.h"
#include "base/callback.h"
#include "base/no_destructor.h"
#include "build/build_config.h"
#include "ui/base/dragdrop/drag_drop_types.h"
#include "ui/views/view.h"
......@@ -11,6 +13,17 @@
namespace views {
namespace {
const View* g_drag_entered_callback_view = nullptr;
base::RepeatingClosure* GetDragEnteredCallback() {
static base::NoDestructor<base::RepeatingClosure> callback;
return callback.get();
}
} // namespace
DropHelper::DropHelper(View* root_view)
: root_view_(root_view),
target_view_(NULL),
......@@ -20,6 +33,14 @@ DropHelper::DropHelper(View* root_view)
DropHelper::~DropHelper() {
}
// static
void DropHelper::SetDragEnteredCallbackForTesting(
const View* view,
base::RepeatingClosure callback) {
g_drag_entered_callback_view = view;
*GetDragEnteredCallback() = std::move(callback);
}
void DropHelper::ResetTargetViewIfEquals(View* view) {
if (target_view_ == view)
target_view_ = NULL;
......@@ -30,16 +51,26 @@ void DropHelper::ResetTargetViewIfEquals(View* view) {
int DropHelper::OnDragOver(const OSExchangeData& data,
const gfx::Point& root_view_location,
int drag_operation) {
const View* old_deepest_view = deepest_view_;
View* view = CalculateTargetViewImpl(root_view_location, data, true,
&deepest_view_);
if (view != target_view_) {
// Target changed notify old drag exited, then new drag entered.
// Target changed. Notify old drag exited, then new drag entered.
NotifyDragExit();
target_view_ = view;
NotifyDragEntered(data, root_view_location, drag_operation);
}
// Notify testing callback if the drag newly moved over the target view.
if (g_drag_entered_callback_view &&
g_drag_entered_callback_view->Contains(deepest_view_) &&
!g_drag_entered_callback_view->Contains(old_deepest_view)) {
auto* callback = GetDragEnteredCallback();
if (!callback->is_null())
callback->Run();
}
return NotifyDragOver(data, root_view_location, drag_operation);
}
......
......@@ -5,8 +5,10 @@
#ifndef UI_VIEWS_WIDGET_DROP_HELPER_H_
#define UI_VIEWS_WIDGET_DROP_HELPER_H_
#include "base/macros.h"
#include <utility>
#include "base/callback_forward.h"
#include "base/macros.h"
#include "ui/views/views_export.h"
namespace gfx {
......@@ -33,6 +35,11 @@ class VIEWS_EXPORT DropHelper {
explicit DropHelper(View* root_view);
~DropHelper();
// Sets a callback that is run any time a drag enters |view|. Only exposed
// for testing.
static void SetDragEnteredCallbackForTesting(const View* view,
base::RepeatingClosure callback);
// Current view drop events are targeted at, may be NULL.
View* target_view() const { return target_view_; }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment