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
|
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/run_loop.h"
#include "base/win/windows_version.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/view_event_test_base.h"
#include "ui/aura/env.h"
#include "ui/aura/test/env_test_helper.h"
#include "ui/aura/window.h"
#include "ui/aura/window_tree_host.h"
#include "ui/base/models/simple_menu_model.h"
#include "ui/base/test/ui_controls.h"
#include "ui/events/gestures/gesture_recognizer_impl.h"
#include "ui/views/controls/menu/menu_runner.h"
namespace {
class TouchEventHandler : public ui::EventHandler {
public:
TouchEventHandler()
: call_depth_(0),
max_call_depth_(0),
num_touch_presses_(0),
num_pointers_down_(0),
recursion_enabled_(false) {}
~TouchEventHandler() override {}
// OnTouchEvent will simulate a second touch event (at |touch_point|) to force
// recursion in event handling.
void ForceRecursionInEventHandler(const gfx::Point& touch_point) {
recursion_enabled_ = true;
touch_point_ = touch_point;
}
void WaitForIdle() {
base::RunLoop run_loop(base::RunLoop::Type::kNestableTasksAllowed);
run_loop.RunUntilIdle();
}
void WaitForEvents() {
base::RunLoop run_loop(base::RunLoop::Type::kNestableTasksAllowed);
quit_closure_ = run_loop.QuitClosure();
run_loop.Run();
}
int max_call_depth() const { return max_call_depth_; }
int num_touch_presses() const { return num_touch_presses_; }
int num_pointers_down() const { return num_pointers_down_; }
void set_touch_point(const gfx::Point& pt) { touch_point_ = pt; }
private:
// ui::EventHandler:
void OnTouchEvent(ui::TouchEvent* event) override {
max_call_depth_ = std::max(++call_depth_, max_call_depth_);
if (recursion_enabled_ && (event->type() == ui::ET_TOUCH_RELEASED)) {
recursion_enabled_ = false;
ui_controls::SendTouchEvents(ui_controls::PRESS | ui_controls::PRESS, 1,
touch_point_.x(), touch_point_.y());
WaitForIdle();
}
switch (event->type()) {
case ui::ET_TOUCH_PRESSED:
num_touch_presses_++;
num_pointers_down_++;
break;
case ui::ET_TOUCH_RELEASED:
num_pointers_down_--;
if (!quit_closure_.is_null() && num_pointers_down_ == 0) {
quit_closure_.Run();
}
break;
default:
break;
}
--call_depth_;
}
int call_depth_;
int max_call_depth_;
int num_touch_presses_;
int num_pointers_down_;
base::Closure quit_closure_;
bool recursion_enabled_;
gfx::Point touch_point_;
DISALLOW_COPY_AND_ASSIGN(TouchEventHandler);
};
class TestingGestureRecognizer : public ui::GestureRecognizerImpl {
public:
TestingGestureRecognizer() = default;
~TestingGestureRecognizer() override = default;
int num_touch_press_events() const { return num_touch_press_events_; }
int num_touch_release_events() const { return num_touch_release_events_; }
protected:
// Overriden from GestureRecognizerImpl:
bool ProcessTouchEventPreDispatch(ui::TouchEvent* event,
ui::GestureConsumer* consumer) override {
switch (event->type()) {
case ui::ET_TOUCH_PRESSED:
num_touch_press_events_++;
break;
case ui::ET_TOUCH_RELEASED:
num_touch_release_events_++;
break;
default:
break;
}
return ui::GestureRecognizerImpl::ProcessTouchEventPreDispatch(event,
consumer);
}
private:
int num_touch_press_events_ = 0;
int num_touch_release_events_ = 0;
DISALLOW_COPY_AND_ASSIGN(TestingGestureRecognizer);
};
} // namespace
class TouchEventsViewTest : public ViewEventTestBase {
public:
TouchEventsViewTest() : ViewEventTestBase(), touch_view_(nullptr) {}
// ViewEventTestBase:
void SetUp() override {
touch_view_ = new views::View();
ViewEventTestBase::SetUp();
aura::test::EnvTestHelper().SetGestureRecognizer(
std::make_unique<TestingGestureRecognizer>());
gesture_recognizer_ = static_cast<TestingGestureRecognizer*>(
aura::Env::GetInstance()->gesture_recognizer());
}
void TearDown() override {
touch_view_ = nullptr;
gesture_recognizer_ = nullptr;
ViewEventTestBase::TearDown();
}
views::View* CreateContentsView() override { return touch_view_; }
gfx::Size GetPreferredSizeForContents() const override {
return gfx::Size(600, 600);
}
void DoTestOnMessageLoop() override {
// ui_controls::SendTouchEvents which uses InjectTouchInput API only works
// on Windows 8 and up.
if (base::win::GetVersion() <= base::win::VERSION_WIN7) {
Done();
return;
}
const int touch_pointer_count = 3;
TouchEventHandler touch_event_handler;
GetWidget()->GetNativeWindow()->GetHost()->window()->AddPreTargetHandler(
&touch_event_handler);
gfx::Point in_content(touch_view_->width() / 2, touch_view_->height() / 2);
views::View::ConvertPointToScreen(touch_view_, &in_content);
ASSERT_TRUE(ui_controls::SendTouchEvents(ui_controls::PRESS,
touch_pointer_count,
in_content.x(), in_content.y()));
touch_event_handler.WaitForIdle();
touch_event_handler.WaitForEvents();
EXPECT_EQ(touch_pointer_count, touch_event_handler.num_touch_presses());
EXPECT_EQ(0, touch_event_handler.num_pointers_down());
EXPECT_EQ(touch_pointer_count,
gesture_recognizer_->num_touch_press_events());
EXPECT_EQ(touch_pointer_count,
gesture_recognizer_->num_touch_release_events());
GetWidget()->GetNativeWindow()->GetHost()->window()->RemovePreTargetHandler(
&touch_event_handler);
Done();
}
protected:
views::View* touch_view_ = nullptr;
TestingGestureRecognizer* gesture_recognizer_ = nullptr;
ui::GestureRecognizer* initial_gr_ = nullptr;
DISALLOW_COPY_AND_ASSIGN(TouchEventsViewTest);
};
VIEW_TEST(TouchEventsViewTest, CheckWindowsNativeMessageForTouchEvents)
class TouchEventsRecursiveViewTest : public TouchEventsViewTest {
public:
TouchEventsRecursiveViewTest() {}
void DoTestOnMessageLoop() override {
// ui_controls::SendTouchEvents which uses InjectTouchInput API only works
// on Windows 8 and up.
if (base::win::GetVersion() <= base::win::VERSION_WIN7) {
Done();
return;
}
const int touch_pointer_count = 1;
TouchEventHandler touch_event_handler;
GetWidget()->GetNativeWindow()->GetHost()->window()->AddPreTargetHandler(
&touch_event_handler);
gfx::Point in_content(touch_view_->width() / 2, touch_view_->height() / 2);
views::View::ConvertPointToScreen(touch_view_, &in_content);
touch_event_handler.ForceRecursionInEventHandler(in_content);
ASSERT_TRUE(ui_controls::SendTouchEvents(ui_controls::PRESS,
touch_pointer_count,
in_content.x(), in_content.y()));
touch_event_handler.WaitForEvents();
EXPECT_EQ(touch_pointer_count + 1, touch_event_handler.num_touch_presses());
EXPECT_EQ(0, touch_event_handler.num_pointers_down());
EXPECT_EQ(2, touch_event_handler.max_call_depth());
GetWidget()->GetNativeWindow()->GetHost()->window()->RemovePreTargetHandler(
&touch_event_handler);
Done();
}
private:
DISALLOW_COPY_AND_ASSIGN(TouchEventsRecursiveViewTest);
};
VIEW_TEST(TouchEventsRecursiveViewTest, CheckWindowsRecursiveHandler)
|