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/x/events_x_utils.h"
15 class TestRootWindowHostDelegate
: public aura::RootWindowHostDelegate
{
17 TestRootWindowHostDelegate() : last_touch_type_(ui::ET_UNKNOWN
),
19 last_touch_location_(0, 0) {
21 virtual ~TestRootWindowHostDelegate() {}
23 virtual bool OnHostKeyEvent(ui::KeyEvent
* event
) OVERRIDE
{
27 virtual bool OnHostMouseEvent(ui::MouseEvent
* event
) OVERRIDE
{
30 virtual bool OnHostScrollEvent(ui::ScrollEvent
* event
) OVERRIDE
{
34 virtual bool OnHostTouchEvent(ui::TouchEvent
* event
) OVERRIDE
{
35 last_touch_id_
= event
->touch_id();
36 last_touch_type_
= event
->type();
37 last_touch_location_
= event
->location();
41 virtual void OnHostCancelMode() OVERRIDE
{}
43 // Called when the windowing system activates the window.
44 virtual void OnHostActivated() OVERRIDE
{}
46 // Called when system focus is changed to another window.
47 virtual void OnHostLostWindowCapture() OVERRIDE
{}
49 // Called when the windowing system has mouse grab because it's performing a
50 // window move on our behalf, but we should still paint as if we're active.
51 virtual void OnHostLostMouseGrab() OVERRIDE
{}
53 virtual void OnHostPaint(const gfx::Rect
& damage_rect
) OVERRIDE
{}
55 virtual void OnHostMoved(const gfx::Point
& origin
) OVERRIDE
{}
56 virtual void OnHostResized(const gfx::Size
& size
) OVERRIDE
{}
58 virtual float GetDeviceScaleFactor() OVERRIDE
{
62 virtual aura::RootWindow
* AsRootWindow() OVERRIDE
{
65 virtual const aura::RootWindow
* AsRootWindow() const OVERRIDE
{
69 ui::EventType
last_touch_type() {
70 return last_touch_type_
;
74 return last_touch_id_
;
77 gfx::Point
last_touch_location() {
78 return last_touch_location_
;
82 ui::EventType last_touch_type_
;
84 gfx::Point last_touch_location_
;
86 DISALLOW_COPY_AND_ASSIGN(TestRootWindowHostDelegate
);
93 typedef test::AuraTestBase RootWindowHostX11Test
;
95 // Send X touch events to one RootWindowHost. The RootWindowHost's
96 // delegate will get corresponding ui::TouchEvent if the touch events
97 // are winthin the bound of the RootWindowHost.
98 TEST_F(RootWindowHostX11Test
, DispatchTouchEventToOneRootWindow
) {
99 #if defined(OS_CHROMEOS)
100 // Fake a ChromeOS running env.
101 const char* kLsbRelease
= "CHROMEOS_RELEASE_NAME=Chromium OS\n";
102 base::SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease
, base::Time());
103 #endif // defined(OS_CHROMEOS)
105 scoped_ptr
<RootWindowHostX11
> root_window_host(
106 new RootWindowHostX11(gfx::Rect(0, 0, 2560, 1700)));
107 scoped_ptr
<TestRootWindowHostDelegate
> delegate(
108 new TestRootWindowHostDelegate());
109 root_window_host
->set_delegate(delegate
.get());
111 std::vector
<unsigned int> devices
;
112 devices
.push_back(0);
113 ui::SetupTouchDevicesForTest(devices
);
114 std::vector
<ui::Valuator
> valuators
;
116 EXPECT_EQ(ui::ET_UNKNOWN
, delegate
->last_touch_type());
117 EXPECT_EQ(-1, delegate
->last_touch_id());
119 #if defined(OS_CHROMEOS)
120 // This touch is out of bounds.
121 ui::ScopedXI2Event
event1(ui::CreateTouchEventForTest(
122 0, XI_TouchBegin
, 5, gfx::Point(1500, 2500), valuators
));
123 root_window_host
->Dispatch(event1
);
124 EXPECT_EQ(ui::ET_UNKNOWN
, delegate
->last_touch_type());
125 EXPECT_EQ(-1, delegate
->last_touch_id());
126 EXPECT_EQ(gfx::Point(0, 0), delegate
->last_touch_location());
127 #endif // defined(OS_CHROMEOS)
129 // Following touchs are within bounds and are passed to delegate.
130 ui::ScopedXI2Event
event2(ui::CreateTouchEventForTest(
131 0, XI_TouchBegin
, 5, gfx::Point(1500, 1500), valuators
));
132 root_window_host
->Dispatch(event2
);
133 EXPECT_EQ(ui::ET_TOUCH_PRESSED
, delegate
->last_touch_type());
134 EXPECT_EQ(0, delegate
->last_touch_id());
135 EXPECT_EQ(gfx::Point(1500, 1500), delegate
->last_touch_location());
137 ui::ScopedXI2Event
event3(ui::CreateTouchEventForTest(
138 0, XI_TouchUpdate
, 5, gfx::Point(1500, 1600), valuators
));
139 root_window_host
->Dispatch(event3
);
140 EXPECT_EQ(ui::ET_TOUCH_MOVED
, delegate
->last_touch_type());
141 EXPECT_EQ(0, delegate
->last_touch_id());
142 EXPECT_EQ(gfx::Point(1500, 1600), delegate
->last_touch_location());
144 ui::ScopedXI2Event
event4(ui::CreateTouchEventForTest(
145 0, XI_TouchEnd
, 5, gfx::Point(1500, 1600), valuators
));
146 root_window_host
->Dispatch(event4
);
147 EXPECT_EQ(ui::ET_TOUCH_RELEASED
, delegate
->last_touch_type());
148 EXPECT_EQ(0, delegate
->last_touch_id());
149 EXPECT_EQ(gfx::Point(1500, 1600), delegate
->last_touch_location());
151 // Revert the CrOS testing env otherwise the following non-CrOS aura
153 #if defined(OS_CHROMEOS)
154 // Fake a ChromeOS running env.
156 base::SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease
, base::Time());
157 #endif // defined(OS_CHROMEOS)
160 // Send X touch events to two RootWindowHost. The RootWindowHost which is
161 // the event target of the X touch events should generate the corresponding
162 // ui::TouchEvent for its delegate.
163 #if defined(OS_CHROMEOS)
164 TEST_F(RootWindowHostX11Test
, DispatchTouchEventToTwoRootWindow
) {
165 // Fake a ChromeOS running env.
166 const char* kLsbRelease
= "CHROMEOS_RELEASE_NAME=Chromium OS\n";
167 base::SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease
, base::Time());
169 scoped_ptr
<RootWindowHostX11
> root_window_host1(
170 new RootWindowHostX11(gfx::Rect(0, 0, 2560, 1700)));
171 scoped_ptr
<TestRootWindowHostDelegate
> delegate1(
172 new TestRootWindowHostDelegate());
173 root_window_host1
->set_delegate(delegate1
.get());
175 int host2_y_offset
= 1700;
176 scoped_ptr
<RootWindowHostX11
> root_window_host2(
177 new RootWindowHostX11(gfx::Rect(0, host2_y_offset
, 1920, 1080)));
178 scoped_ptr
<TestRootWindowHostDelegate
> delegate2(
179 new TestRootWindowHostDelegate());
180 root_window_host2
->set_delegate(delegate2
.get());
182 std::vector
<unsigned int> devices
;
183 devices
.push_back(0);
184 ui::SetupTouchDevicesForTest(devices
);
185 std::vector
<ui::Valuator
> valuators
;
187 EXPECT_EQ(ui::ET_UNKNOWN
, delegate1
->last_touch_type());
188 EXPECT_EQ(-1, delegate1
->last_touch_id());
189 EXPECT_EQ(ui::ET_UNKNOWN
, delegate2
->last_touch_type());
190 EXPECT_EQ(-1, delegate2
->last_touch_id());
192 // 2 Touch events are targeted at the second RootWindowHost.
193 ui::ScopedXI2Event
touch1_begin(ui::CreateTouchEventForTest(
194 0, XI_TouchBegin
, 5, gfx::Point(1500, 2500), valuators
));
195 root_window_host1
->Dispatch(touch1_begin
);
196 root_window_host2
->Dispatch(touch1_begin
);
197 EXPECT_EQ(ui::ET_UNKNOWN
, delegate1
->last_touch_type());
198 EXPECT_EQ(-1, delegate1
->last_touch_id());
199 EXPECT_EQ(gfx::Point(0, 0), delegate1
->last_touch_location());
200 EXPECT_EQ(ui::ET_TOUCH_PRESSED
, delegate2
->last_touch_type());
201 EXPECT_EQ(0, delegate2
->last_touch_id());
202 EXPECT_EQ(gfx::Point(1500, 2500 - host2_y_offset
),
203 delegate2
->last_touch_location());
205 ui::ScopedXI2Event
touch2_begin(ui::CreateTouchEventForTest(
206 0, XI_TouchBegin
, 6, gfx::Point(1600, 2600), valuators
));
207 root_window_host1
->Dispatch(touch2_begin
);
208 root_window_host2
->Dispatch(touch2_begin
);
209 EXPECT_EQ(ui::ET_UNKNOWN
, delegate1
->last_touch_type());
210 EXPECT_EQ(-1, delegate1
->last_touch_id());
211 EXPECT_EQ(gfx::Point(0, 0), delegate1
->last_touch_location());
212 EXPECT_EQ(ui::ET_TOUCH_PRESSED
, delegate2
->last_touch_type());
213 EXPECT_EQ(1, delegate2
->last_touch_id());
214 EXPECT_EQ(gfx::Point(1600, 2600 - host2_y_offset
),
215 delegate2
->last_touch_location());
217 ui::ScopedXI2Event
touch1_move(ui::CreateTouchEventForTest(
218 0, XI_TouchUpdate
, 5, gfx::Point(1500, 2550), valuators
));
219 root_window_host1
->Dispatch(touch1_move
);
220 root_window_host2
->Dispatch(touch1_move
);
221 EXPECT_EQ(ui::ET_UNKNOWN
, delegate1
->last_touch_type());
222 EXPECT_EQ(-1, delegate1
->last_touch_id());
223 EXPECT_EQ(gfx::Point(0, 0), delegate1
->last_touch_location());
224 EXPECT_EQ(ui::ET_TOUCH_MOVED
, delegate2
->last_touch_type());
225 EXPECT_EQ(0, delegate2
->last_touch_id());
226 EXPECT_EQ(gfx::Point(1500, 2550 - host2_y_offset
),
227 delegate2
->last_touch_location());
229 ui::ScopedXI2Event
touch2_move(ui::CreateTouchEventForTest(
230 0, XI_TouchUpdate
, 6, gfx::Point(1600, 2650), valuators
));
231 root_window_host1
->Dispatch(touch2_move
);
232 root_window_host2
->Dispatch(touch2_move
);
233 EXPECT_EQ(ui::ET_UNKNOWN
, delegate1
->last_touch_type());
234 EXPECT_EQ(-1, delegate1
->last_touch_id());
235 EXPECT_EQ(gfx::Point(0, 0), delegate1
->last_touch_location());
236 EXPECT_EQ(ui::ET_TOUCH_MOVED
, delegate2
->last_touch_type());
237 EXPECT_EQ(1, delegate2
->last_touch_id());
238 EXPECT_EQ(gfx::Point(1600, 2650 - host2_y_offset
),
239 delegate2
->last_touch_location());
241 ui::ScopedXI2Event
touch1_end(ui::CreateTouchEventForTest(
242 0, XI_TouchEnd
, 5, gfx::Point(1500, 2550), valuators
));
243 root_window_host1
->Dispatch(touch1_end
);
244 root_window_host2
->Dispatch(touch1_end
);
245 EXPECT_EQ(ui::ET_UNKNOWN
, delegate1
->last_touch_type());
246 EXPECT_EQ(-1, delegate1
->last_touch_id());
247 EXPECT_EQ(gfx::Point(0, 0), delegate1
->last_touch_location());
248 EXPECT_EQ(ui::ET_TOUCH_RELEASED
, delegate2
->last_touch_type());
249 EXPECT_EQ(0, delegate2
->last_touch_id());
250 EXPECT_EQ(gfx::Point(1500, 2550 - host2_y_offset
),
251 delegate2
->last_touch_location());
253 ui::ScopedXI2Event
touch2_end(ui::CreateTouchEventForTest(
254 0, XI_TouchEnd
, 6, gfx::Point(1600, 2650), valuators
));
255 root_window_host1
->Dispatch(touch2_end
);
256 root_window_host2
->Dispatch(touch2_end
);
257 EXPECT_EQ(ui::ET_UNKNOWN
, delegate1
->last_touch_type());
258 EXPECT_EQ(-1, delegate1
->last_touch_id());
259 EXPECT_EQ(gfx::Point(0, 0), delegate1
->last_touch_location());
260 EXPECT_EQ(ui::ET_TOUCH_RELEASED
, delegate2
->last_touch_type());
261 EXPECT_EQ(1, delegate2
->last_touch_id());
262 EXPECT_EQ(gfx::Point(1600, 2650 - host2_y_offset
),
263 delegate2
->last_touch_location());
265 // Revert the CrOS testing env otherwise the following non-CrOS aura
267 // Fake a ChromeOS running env.
269 base::SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease
, base::Time());
271 #endif // defined(OS_CHROMEOS)