Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / render_widget_unittest.cc
blob431b2fa5fd59cffa10aec71790e6a4ea2ff0d189
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/renderer/render_widget.h"
7 #include <vector>
9 #include "content/common/input/synthetic_web_input_event_builders.h"
10 #include "content/common/input_messages.h"
11 #include "content/public/test/mock_render_thread.h"
12 #include "content/test/mock_render_process.h"
13 #include "ipc/ipc_test_sink.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "third_party/WebKit/public/web/WebInputEvent.h"
16 #include "ui/gfx/geometry/rect.h"
18 namespace content {
20 class RenderWidgetUnittest : public testing::Test {
21 public:
22 RenderWidgetUnittest() {}
23 ~RenderWidgetUnittest() override {}
25 private:
26 MockRenderProcess render_process_;
27 MockRenderThread render_thread_;
29 DISALLOW_COPY_AND_ASSIGN(RenderWidgetUnittest);
32 class TouchableRenderWidget : public RenderWidget {
33 public:
34 TouchableRenderWidget()
35 : RenderWidget(blink::WebPopupTypeNone,
36 blink::WebScreenInfo(),
37 false,
38 false,
39 false) {}
41 void SetTouchRegion(const std::vector<gfx::Rect>& rects) {
42 rects_ = rects;
45 void SendInputEvent(const blink::WebInputEvent& event) {
46 OnHandleInputEvent(&event, ui::LatencyInfo(), false);
49 IPC::TestSink* sink() { return &sink_; }
51 protected:
52 ~TouchableRenderWidget() override {}
54 // Overridden from RenderWidget:
55 bool HasTouchEventHandlersAt(const gfx::Point& point) const override {
56 for (std::vector<gfx::Rect>::const_iterator iter = rects_.begin();
57 iter != rects_.end(); ++iter) {
58 if ((*iter).Contains(point))
59 return true;
61 return false;
64 bool Send(IPC::Message* msg) override {
65 sink_.OnMessageReceived(*msg);
66 delete msg;
67 return true;
70 private:
71 std::vector<gfx::Rect> rects_;
72 IPC::TestSink sink_;
74 DISALLOW_COPY_AND_ASSIGN(TouchableRenderWidget);
77 TEST_F(RenderWidgetUnittest, TouchHitTestSinglePoint) {
78 scoped_refptr<TouchableRenderWidget> widget = new TouchableRenderWidget();
80 SyntheticWebTouchEvent touch;
81 touch.PressPoint(10, 10);
83 widget->SendInputEvent(touch);
84 ASSERT_EQ(1u, widget->sink()->message_count());
86 // Since there's currently no touch-event handling region, the response should
87 // be 'no consumer exists'.
88 const IPC::Message* message = widget->sink()->GetMessageAt(0);
89 EXPECT_EQ(InputHostMsg_HandleInputEvent_ACK::ID, message->type());
90 InputHostMsg_HandleInputEvent_ACK::Param params;
91 InputHostMsg_HandleInputEvent_ACK::Read(message, &params);
92 InputEventAckState ack_state = get<0>(params).state;
93 EXPECT_EQ(INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS, ack_state);
94 widget->sink()->ClearMessages();
96 std::vector<gfx::Rect> rects;
97 rects.push_back(gfx::Rect(0, 0, 20, 20));
98 rects.push_back(gfx::Rect(25, 0, 10, 10));
99 widget->SetTouchRegion(rects);
101 widget->SendInputEvent(touch);
102 ASSERT_EQ(1u, widget->sink()->message_count());
103 message = widget->sink()->GetMessageAt(0);
104 EXPECT_EQ(InputHostMsg_HandleInputEvent_ACK::ID, message->type());
105 InputHostMsg_HandleInputEvent_ACK::Read(message, &params);
106 ack_state = get<0>(params).state;
107 EXPECT_EQ(INPUT_EVENT_ACK_STATE_NOT_CONSUMED, ack_state);
108 widget->sink()->ClearMessages();
111 TEST_F(RenderWidgetUnittest, TouchHitTestMultiplePoints) {
112 scoped_refptr<TouchableRenderWidget> widget = new TouchableRenderWidget();
113 std::vector<gfx::Rect> rects;
114 rects.push_back(gfx::Rect(0, 0, 20, 20));
115 rects.push_back(gfx::Rect(25, 0, 10, 10));
116 widget->SetTouchRegion(rects);
118 SyntheticWebTouchEvent touch;
119 touch.PressPoint(25, 25);
121 widget->SendInputEvent(touch);
122 ASSERT_EQ(1u, widget->sink()->message_count());
124 // Since there's currently no touch-event handling region, the response should
125 // be 'no consumer exists'.
126 const IPC::Message* message = widget->sink()->GetMessageAt(0);
127 EXPECT_EQ(InputHostMsg_HandleInputEvent_ACK::ID, message->type());
128 InputHostMsg_HandleInputEvent_ACK::Param params;
129 InputHostMsg_HandleInputEvent_ACK::Read(message, &params);
130 InputEventAckState ack_state = get<0>(params).state;
131 EXPECT_EQ(INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS, ack_state);
132 widget->sink()->ClearMessages();
134 // Press a second touch point. This time, on a touch-handling region.
135 touch.PressPoint(10, 10);
136 widget->SendInputEvent(touch);
137 ASSERT_EQ(1u, widget->sink()->message_count());
138 message = widget->sink()->GetMessageAt(0);
139 EXPECT_EQ(InputHostMsg_HandleInputEvent_ACK::ID, message->type());
140 InputHostMsg_HandleInputEvent_ACK::Read(message, &params);
141 ack_state = get<0>(params).state;
142 EXPECT_EQ(INPUT_EVENT_ACK_STATE_NOT_CONSUMED, ack_state);
143 widget->sink()->ClearMessages();
146 } // namespace content