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/test/aura_test_base.h"
10 #include "ui/aura/window_tree_host_delegate.h"
11 #include "ui/aura/window_tree_host_x11.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 void OnHostCancelMode() OVERRIDE {}
30 virtual void OnHostActivated() OVERRIDE {}
31 virtual void OnHostLostWindowCapture() OVERRIDE {}
32 virtual void OnHostLostMouseGrab() OVERRIDE {}
33 virtual void OnHostMoved(const gfx::Point& origin) OVERRIDE {}
34 virtual void OnHostResized(const gfx::Size& size) OVERRIDE {}
35 virtual void OnCursorMovedToRootLocation(
36 const gfx::Point& root_location) OVERRIDE {}
37 virtual aura::RootWindow* AsRootWindow() OVERRIDE { return NULL; }
38 virtual const aura::RootWindow* AsRootWindow() const OVERRIDE { return NULL; }
39 virtual ui::EventProcessor* GetEventProcessor() OVERRIDE {
43 // ui::EventProcessor:
44 virtual ui::EventTarget* GetRootTarget() OVERRIDE { return this; }
45 virtual bool CanDispatchToTarget(ui::EventTarget* target) OVERRIDE {
50 virtual void OnTouchEvent(ui::TouchEvent* event) OVERRIDE {
51 last_touch_id_ = event->touch_id();
52 last_touch_type_ = event->type();
53 last_touch_location_ = event->location();
57 virtual bool CanAcceptEvent(const ui::Event& event) OVERRIDE {
60 virtual ui::EventTarget* GetParentTarget() OVERRIDE { return NULL; }
61 virtual scoped_ptr<ui::EventTargetIterator>
62 GetChildIterator() const OVERRIDE {
63 return scoped_ptr<ui::EventTargetIterator>();
65 virtual ui::EventTargeter* GetEventTargeter() OVERRIDE { return &targeter_; }
67 ui::EventType last_touch_type() {
68 return last_touch_type_;
72 return last_touch_id_;
75 gfx::Point last_touch_location() {
76 return last_touch_location_;
80 ui::EventType last_touch_type_;
82 gfx::Point last_touch_location_;
83 ui::EventTargeter targeter_;
85 DISALLOW_COPY_AND_ASSIGN(TestWindowTreeHostDelegate);
92 typedef test::AuraTestBase WindowTreeHostX11Test;
94 // Send X touch events to one WindowTreeHost. The WindowTreeHost's
95 // delegate will get corresponding ui::TouchEvent if the touch events
96 // are winthin the bound of the WindowTreeHost.
97 TEST_F(WindowTreeHostX11Test, DispatchTouchEventToOneRootWindow) {
98 #if defined(OS_CHROMEOS)
99 // Fake a ChromeOS running env.
100 const char* kLsbRelease = "CHROMEOS_RELEASE_NAME=Chromium OS\n";
101 base::SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease, base::Time());
102 #endif // defined(OS_CHROMEOS)
104 scoped_ptr<WindowTreeHostX11> window_tree_host(
105 new WindowTreeHostX11(gfx::Rect(0, 0, 2560, 1700)));
106 scoped_ptr<TestWindowTreeHostDelegate> delegate(
107 new TestWindowTreeHostDelegate());
108 window_tree_host->set_delegate(delegate.get());
110 std::vector<unsigned int> devices;
111 devices.push_back(0);
112 ui::SetUpTouchDevicesForTest(devices);
113 std::vector<ui::Valuator> valuators;
115 EXPECT_EQ(ui::ET_UNKNOWN, delegate->last_touch_type());
116 EXPECT_EQ(-1, delegate->last_touch_id());
118 ui::ScopedXI2Event scoped_xevent;
119 #if defined(OS_CHROMEOS)
120 // This touch is out of bounds.
121 scoped_xevent.InitTouchEvent(
122 0, XI_TouchBegin, 5, gfx::Point(1500, 2500), valuators);
123 window_tree_host->Dispatch(scoped_xevent);
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 scoped_xevent.InitTouchEvent(
131 0, XI_TouchBegin, 5, gfx::Point(1500, 1500), valuators);
132 window_tree_host->Dispatch(scoped_xevent);
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 scoped_xevent.InitTouchEvent(
138 0, XI_TouchUpdate, 5, gfx::Point(1500, 1600), valuators);
139 window_tree_host->Dispatch(scoped_xevent);
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 scoped_xevent.InitTouchEvent(
145 0, XI_TouchEnd, 5, gfx::Point(1500, 1600), valuators);
146 window_tree_host->Dispatch(scoped_xevent);
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 WindowTreeHost. The WindowTreeHost 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(WindowTreeHostX11Test, 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<WindowTreeHostX11> window_tree_host1(
170 new WindowTreeHostX11(gfx::Rect(0, 0, 2560, 1700)));
171 scoped_ptr<TestWindowTreeHostDelegate> delegate1(
172 new TestWindowTreeHostDelegate());
173 window_tree_host1->set_delegate(delegate1.get());
175 int host2_y_offset = 1700;
176 scoped_ptr<WindowTreeHostX11> window_tree_host2(
177 new WindowTreeHostX11(gfx::Rect(0, host2_y_offset, 1920, 1080)));
178 scoped_ptr<TestWindowTreeHostDelegate> delegate2(
179 new TestWindowTreeHostDelegate());
180 window_tree_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 WindowTreeHost.
193 ui::ScopedXI2Event scoped_xevent;
194 scoped_xevent.InitTouchEvent(
195 0, XI_TouchBegin, 5, gfx::Point(1500, 2500), valuators);
196 window_tree_host1->Dispatch(scoped_xevent);
197 window_tree_host2->Dispatch(scoped_xevent);
198 EXPECT_EQ(ui::ET_UNKNOWN, delegate1->last_touch_type());
199 EXPECT_EQ(-1, delegate1->last_touch_id());
200 EXPECT_EQ(gfx::Point(0, 0), delegate1->last_touch_location());
201 EXPECT_EQ(ui::ET_TOUCH_PRESSED, delegate2->last_touch_type());
202 EXPECT_EQ(0, delegate2->last_touch_id());
203 EXPECT_EQ(gfx::Point(1500, 2500 - host2_y_offset),
204 delegate2->last_touch_location());
206 scoped_xevent.InitTouchEvent(
207 0, XI_TouchBegin, 6, gfx::Point(1600, 2600), valuators);
208 window_tree_host1->Dispatch(scoped_xevent);
209 window_tree_host2->Dispatch(scoped_xevent);
210 EXPECT_EQ(ui::ET_UNKNOWN, delegate1->last_touch_type());
211 EXPECT_EQ(-1, delegate1->last_touch_id());
212 EXPECT_EQ(gfx::Point(0, 0), delegate1->last_touch_location());
213 EXPECT_EQ(ui::ET_TOUCH_PRESSED, delegate2->last_touch_type());
214 EXPECT_EQ(1, delegate2->last_touch_id());
215 EXPECT_EQ(gfx::Point(1600, 2600 - host2_y_offset),
216 delegate2->last_touch_location());
218 scoped_xevent.InitTouchEvent(
219 0, XI_TouchUpdate, 5, gfx::Point(1500, 2550), valuators);
220 window_tree_host1->Dispatch(scoped_xevent);
221 window_tree_host2->Dispatch(scoped_xevent);
222 EXPECT_EQ(ui::ET_UNKNOWN, delegate1->last_touch_type());
223 EXPECT_EQ(-1, delegate1->last_touch_id());
224 EXPECT_EQ(gfx::Point(0, 0), delegate1->last_touch_location());
225 EXPECT_EQ(ui::ET_TOUCH_MOVED, delegate2->last_touch_type());
226 EXPECT_EQ(0, delegate2->last_touch_id());
227 EXPECT_EQ(gfx::Point(1500, 2550 - host2_y_offset),
228 delegate2->last_touch_location());
230 scoped_xevent.InitTouchEvent(
231 0, XI_TouchUpdate, 6, gfx::Point(1600, 2650), valuators);
232 window_tree_host1->Dispatch(scoped_xevent);
233 window_tree_host2->Dispatch(scoped_xevent);
234 EXPECT_EQ(ui::ET_UNKNOWN, delegate1->last_touch_type());
235 EXPECT_EQ(-1, delegate1->last_touch_id());
236 EXPECT_EQ(gfx::Point(0, 0), delegate1->last_touch_location());
237 EXPECT_EQ(ui::ET_TOUCH_MOVED, delegate2->last_touch_type());
238 EXPECT_EQ(1, delegate2->last_touch_id());
239 EXPECT_EQ(gfx::Point(1600, 2650 - host2_y_offset),
240 delegate2->last_touch_location());
242 scoped_xevent.InitTouchEvent(
243 0, XI_TouchEnd, 5, gfx::Point(1500, 2550), valuators);
244 window_tree_host1->Dispatch(scoped_xevent);
245 window_tree_host2->Dispatch(scoped_xevent);
246 EXPECT_EQ(ui::ET_UNKNOWN, delegate1->last_touch_type());
247 EXPECT_EQ(-1, delegate1->last_touch_id());
248 EXPECT_EQ(gfx::Point(0, 0), delegate1->last_touch_location());
249 EXPECT_EQ(ui::ET_TOUCH_RELEASED, delegate2->last_touch_type());
250 EXPECT_EQ(0, delegate2->last_touch_id());
251 EXPECT_EQ(gfx::Point(1500, 2550 - host2_y_offset),
252 delegate2->last_touch_location());
254 scoped_xevent.InitTouchEvent(
255 0, XI_TouchEnd, 6, gfx::Point(1600, 2650), valuators);
256 window_tree_host1->Dispatch(scoped_xevent);
257 window_tree_host2->Dispatch(scoped_xevent);
258 EXPECT_EQ(ui::ET_UNKNOWN, delegate1->last_touch_type());
259 EXPECT_EQ(-1, delegate1->last_touch_id());
260 EXPECT_EQ(gfx::Point(0, 0), delegate1->last_touch_location());
261 EXPECT_EQ(ui::ET_TOUCH_RELEASED, delegate2->last_touch_type());
262 EXPECT_EQ(1, delegate2->last_touch_id());
263 EXPECT_EQ(gfx::Point(1600, 2650 - host2_y_offset),
264 delegate2->last_touch_location());
266 // Revert the CrOS testing env otherwise the following non-CrOS aura
268 // Fake a ChromeOS running env.
270 base::SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease, base::Time());
272 #endif // defined(OS_CHROMEOS)