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.
6 #include "base/sys_info.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "ui/aura/root_window.h"
9 #include "ui/aura/root_window_host_x11.h"
10 #include "ui/aura/test/aura_test_base.h"
11 #include "ui/aura/window_tree_host_delegate.h"
12 #include "ui/events/event_processor.h"
13 #include "ui/events/event_target.h"
14 #include "ui/events/event_target_iterator.h"
15 #include "ui/events/test/events_test_utils_x11.h"
18 class TestWindowTreeHostDelegate
: public aura::WindowTreeHostDelegate
,
19 public ui::EventProcessor
,
20 public ui::EventTarget
{
22 TestWindowTreeHostDelegate() : last_touch_type_(ui::ET_UNKNOWN
),
24 last_touch_location_(0, 0) {
26 virtual ~TestWindowTreeHostDelegate() {}
28 // aura::WindowTreeHostDelegate:
29 virtual bool OnHostKeyEvent(ui::KeyEvent
* event
) OVERRIDE
{
32 virtual bool OnHostMouseEvent(ui::MouseEvent
* event
) OVERRIDE
{
35 virtual bool OnHostScrollEvent(ui::ScrollEvent
* event
) OVERRIDE
{
38 virtual bool OnHostTouchEvent(ui::TouchEvent
* event
) OVERRIDE
{
39 last_touch_id_
= event
->touch_id();
40 last_touch_type_
= event
->type();
41 last_touch_location_
= event
->location();
45 virtual void OnHostCancelMode() OVERRIDE
{}
46 virtual void OnHostActivated() OVERRIDE
{}
47 virtual void OnHostLostWindowCapture() OVERRIDE
{}
48 virtual void OnHostLostMouseGrab() OVERRIDE
{}
49 virtual void OnHostMoved(const gfx::Point
& origin
) OVERRIDE
{}
50 virtual void OnHostResized(const gfx::Size
& size
) OVERRIDE
{}
51 virtual aura::RootWindow
* AsRootWindow() OVERRIDE
{ return NULL
; }
52 virtual const aura::RootWindow
* AsRootWindow() const OVERRIDE
{ return NULL
; }
53 virtual ui::EventProcessor
* GetEventProcessor() OVERRIDE
{
57 // ui::EventProcessor:
58 virtual ui::EventTarget
* GetRootTarget() OVERRIDE
{ return this; }
59 virtual bool CanDispatchToTarget(ui::EventTarget
* target
) OVERRIDE
{
64 virtual void OnTouchEvent(ui::TouchEvent
* event
) OVERRIDE
{
65 last_touch_id_
= event
->touch_id();
66 last_touch_type_
= event
->type();
67 last_touch_location_
= event
->location();
71 virtual bool CanAcceptEvent(const ui::Event
& event
) OVERRIDE
{
74 virtual ui::EventTarget
* GetParentTarget() OVERRIDE
{ return NULL
; }
75 virtual scoped_ptr
<ui::EventTargetIterator
>
76 GetChildIterator() const OVERRIDE
{
77 return scoped_ptr
<ui::EventTargetIterator
>();
79 virtual ui::EventTargeter
* GetEventTargeter() OVERRIDE
{ return &targeter_
; }
81 ui::EventType
last_touch_type() {
82 return last_touch_type_
;
86 return last_touch_id_
;
89 gfx::Point
last_touch_location() {
90 return last_touch_location_
;
94 ui::EventType last_touch_type_
;
96 gfx::Point last_touch_location_
;
97 ui::EventTargeter targeter_
;
99 DISALLOW_COPY_AND_ASSIGN(TestWindowTreeHostDelegate
);
106 typedef test::AuraTestBase WindowTreeHostX11Test
;
108 // Send X touch events to one WindowTreeHost. The WindowTreeHost's
109 // delegate will get corresponding ui::TouchEvent if the touch events
110 // are winthin the bound of the WindowTreeHost.
111 TEST_F(WindowTreeHostX11Test
, DispatchTouchEventToOneRootWindow
) {
112 #if defined(OS_CHROMEOS)
113 // Fake a ChromeOS running env.
114 const char* kLsbRelease
= "CHROMEOS_RELEASE_NAME=Chromium OS\n";
115 base::SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease
, base::Time());
116 #endif // defined(OS_CHROMEOS)
118 scoped_ptr
<WindowTreeHostX11
> root_window_host(
119 new WindowTreeHostX11(gfx::Rect(0, 0, 2560, 1700)));
120 scoped_ptr
<TestWindowTreeHostDelegate
> delegate(
121 new TestWindowTreeHostDelegate());
122 root_window_host
->set_delegate(delegate
.get());
124 std::vector
<unsigned int> devices
;
125 devices
.push_back(0);
126 ui::SetUpTouchDevicesForTest(devices
);
127 std::vector
<ui::Valuator
> valuators
;
129 EXPECT_EQ(ui::ET_UNKNOWN
, delegate
->last_touch_type());
130 EXPECT_EQ(-1, delegate
->last_touch_id());
132 ui::ScopedXI2Event scoped_xevent
;
133 #if defined(OS_CHROMEOS)
134 // This touch is out of bounds.
135 scoped_xevent
.InitTouchEvent(
136 0, XI_TouchBegin
, 5, gfx::Point(1500, 2500), valuators
);
137 root_window_host
->Dispatch(scoped_xevent
);
138 EXPECT_EQ(ui::ET_UNKNOWN
, delegate
->last_touch_type());
139 EXPECT_EQ(-1, delegate
->last_touch_id());
140 EXPECT_EQ(gfx::Point(0, 0), delegate
->last_touch_location());
141 #endif // defined(OS_CHROMEOS)
143 // Following touchs are within bounds and are passed to delegate.
144 scoped_xevent
.InitTouchEvent(
145 0, XI_TouchBegin
, 5, gfx::Point(1500, 1500), valuators
);
146 root_window_host
->Dispatch(scoped_xevent
);
147 EXPECT_EQ(ui::ET_TOUCH_PRESSED
, delegate
->last_touch_type());
148 EXPECT_EQ(0, delegate
->last_touch_id());
149 EXPECT_EQ(gfx::Point(1500, 1500), delegate
->last_touch_location());
151 scoped_xevent
.InitTouchEvent(
152 0, XI_TouchUpdate
, 5, gfx::Point(1500, 1600), valuators
);
153 root_window_host
->Dispatch(scoped_xevent
);
154 EXPECT_EQ(ui::ET_TOUCH_MOVED
, delegate
->last_touch_type());
155 EXPECT_EQ(0, delegate
->last_touch_id());
156 EXPECT_EQ(gfx::Point(1500, 1600), delegate
->last_touch_location());
158 scoped_xevent
.InitTouchEvent(
159 0, XI_TouchEnd
, 5, gfx::Point(1500, 1600), valuators
);
160 root_window_host
->Dispatch(scoped_xevent
);
161 EXPECT_EQ(ui::ET_TOUCH_RELEASED
, delegate
->last_touch_type());
162 EXPECT_EQ(0, delegate
->last_touch_id());
163 EXPECT_EQ(gfx::Point(1500, 1600), delegate
->last_touch_location());
165 // Revert the CrOS testing env otherwise the following non-CrOS aura
167 #if defined(OS_CHROMEOS)
168 // Fake a ChromeOS running env.
170 base::SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease
, base::Time());
171 #endif // defined(OS_CHROMEOS)
174 // Send X touch events to two WindowTreeHost. The WindowTreeHost which is
175 // the event target of the X touch events should generate the corresponding
176 // ui::TouchEvent for its delegate.
177 #if defined(OS_CHROMEOS)
178 TEST_F(WindowTreeHostX11Test
, DispatchTouchEventToTwoRootWindow
) {
179 // Fake a ChromeOS running env.
180 const char* kLsbRelease
= "CHROMEOS_RELEASE_NAME=Chromium OS\n";
181 base::SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease
, base::Time());
183 scoped_ptr
<WindowTreeHostX11
> root_window_host1(
184 new WindowTreeHostX11(gfx::Rect(0, 0, 2560, 1700)));
185 scoped_ptr
<TestWindowTreeHostDelegate
> delegate1(
186 new TestWindowTreeHostDelegate());
187 root_window_host1
->set_delegate(delegate1
.get());
189 int host2_y_offset
= 1700;
190 scoped_ptr
<WindowTreeHostX11
> root_window_host2(
191 new WindowTreeHostX11(gfx::Rect(0, host2_y_offset
, 1920, 1080)));
192 scoped_ptr
<TestWindowTreeHostDelegate
> delegate2(
193 new TestWindowTreeHostDelegate());
194 root_window_host2
->set_delegate(delegate2
.get());
196 std::vector
<unsigned int> devices
;
197 devices
.push_back(0);
198 ui::SetUpTouchDevicesForTest(devices
);
199 std::vector
<ui::Valuator
> valuators
;
201 EXPECT_EQ(ui::ET_UNKNOWN
, delegate1
->last_touch_type());
202 EXPECT_EQ(-1, delegate1
->last_touch_id());
203 EXPECT_EQ(ui::ET_UNKNOWN
, delegate2
->last_touch_type());
204 EXPECT_EQ(-1, delegate2
->last_touch_id());
206 // 2 Touch events are targeted at the second WindowTreeHost.
207 ui::ScopedXI2Event scoped_xevent
;
208 scoped_xevent
.InitTouchEvent(
209 0, XI_TouchBegin
, 5, gfx::Point(1500, 2500), valuators
);
210 root_window_host1
->Dispatch(scoped_xevent
);
211 root_window_host2
->Dispatch(scoped_xevent
);
212 EXPECT_EQ(ui::ET_UNKNOWN
, delegate1
->last_touch_type());
213 EXPECT_EQ(-1, delegate1
->last_touch_id());
214 EXPECT_EQ(gfx::Point(0, 0), delegate1
->last_touch_location());
215 EXPECT_EQ(ui::ET_TOUCH_PRESSED
, delegate2
->last_touch_type());
216 EXPECT_EQ(0, delegate2
->last_touch_id());
217 EXPECT_EQ(gfx::Point(1500, 2500 - host2_y_offset
),
218 delegate2
->last_touch_location());
220 scoped_xevent
.InitTouchEvent(
221 0, XI_TouchBegin
, 6, gfx::Point(1600, 2600), valuators
);
222 root_window_host1
->Dispatch(scoped_xevent
);
223 root_window_host2
->Dispatch(scoped_xevent
);
224 EXPECT_EQ(ui::ET_UNKNOWN
, delegate1
->last_touch_type());
225 EXPECT_EQ(-1, delegate1
->last_touch_id());
226 EXPECT_EQ(gfx::Point(0, 0), delegate1
->last_touch_location());
227 EXPECT_EQ(ui::ET_TOUCH_PRESSED
, delegate2
->last_touch_type());
228 EXPECT_EQ(1, delegate2
->last_touch_id());
229 EXPECT_EQ(gfx::Point(1600, 2600 - host2_y_offset
),
230 delegate2
->last_touch_location());
232 scoped_xevent
.InitTouchEvent(
233 0, XI_TouchUpdate
, 5, gfx::Point(1500, 2550), valuators
);
234 root_window_host1
->Dispatch(scoped_xevent
);
235 root_window_host2
->Dispatch(scoped_xevent
);
236 EXPECT_EQ(ui::ET_UNKNOWN
, delegate1
->last_touch_type());
237 EXPECT_EQ(-1, delegate1
->last_touch_id());
238 EXPECT_EQ(gfx::Point(0, 0), delegate1
->last_touch_location());
239 EXPECT_EQ(ui::ET_TOUCH_MOVED
, delegate2
->last_touch_type());
240 EXPECT_EQ(0, delegate2
->last_touch_id());
241 EXPECT_EQ(gfx::Point(1500, 2550 - host2_y_offset
),
242 delegate2
->last_touch_location());
244 scoped_xevent
.InitTouchEvent(
245 0, XI_TouchUpdate
, 6, gfx::Point(1600, 2650), valuators
);
246 root_window_host1
->Dispatch(scoped_xevent
);
247 root_window_host2
->Dispatch(scoped_xevent
);
248 EXPECT_EQ(ui::ET_UNKNOWN
, delegate1
->last_touch_type());
249 EXPECT_EQ(-1, delegate1
->last_touch_id());
250 EXPECT_EQ(gfx::Point(0, 0), delegate1
->last_touch_location());
251 EXPECT_EQ(ui::ET_TOUCH_MOVED
, delegate2
->last_touch_type());
252 EXPECT_EQ(1, delegate2
->last_touch_id());
253 EXPECT_EQ(gfx::Point(1600, 2650 - host2_y_offset
),
254 delegate2
->last_touch_location());
256 scoped_xevent
.InitTouchEvent(
257 0, XI_TouchEnd
, 5, gfx::Point(1500, 2550), valuators
);
258 root_window_host1
->Dispatch(scoped_xevent
);
259 root_window_host2
->Dispatch(scoped_xevent
);
260 EXPECT_EQ(ui::ET_UNKNOWN
, delegate1
->last_touch_type());
261 EXPECT_EQ(-1, delegate1
->last_touch_id());
262 EXPECT_EQ(gfx::Point(0, 0), delegate1
->last_touch_location());
263 EXPECT_EQ(ui::ET_TOUCH_RELEASED
, delegate2
->last_touch_type());
264 EXPECT_EQ(0, delegate2
->last_touch_id());
265 EXPECT_EQ(gfx::Point(1500, 2550 - host2_y_offset
),
266 delegate2
->last_touch_location());
268 scoped_xevent
.InitTouchEvent(
269 0, XI_TouchEnd
, 6, gfx::Point(1600, 2650), valuators
);
270 root_window_host1
->Dispatch(scoped_xevent
);
271 root_window_host2
->Dispatch(scoped_xevent
);
272 EXPECT_EQ(ui::ET_UNKNOWN
, delegate1
->last_touch_type());
273 EXPECT_EQ(-1, delegate1
->last_touch_id());
274 EXPECT_EQ(gfx::Point(0, 0), delegate1
->last_touch_location());
275 EXPECT_EQ(ui::ET_TOUCH_RELEASED
, delegate2
->last_touch_type());
276 EXPECT_EQ(1, delegate2
->last_touch_id());
277 EXPECT_EQ(gfx::Point(1600, 2650 - host2_y_offset
),
278 delegate2
->last_touch_location());
280 // Revert the CrOS testing env otherwise the following non-CrOS aura
282 // Fake a ChromeOS running env.
284 base::SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease
, base::Time());
286 #endif // defined(OS_CHROMEOS)