1 // Copyright 2014 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 "ash/host/ash_window_tree_host_x11.h"
10 #include "base/sys_info.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "ui/aura/test/aura_test_base.h"
13 #include "ui/aura/window.h"
14 #include "ui/aura/window_event_dispatcher.h"
15 #include "ui/events/event_processor.h"
16 #include "ui/events/event_target.h"
17 #include "ui/events/event_target_iterator.h"
18 #include "ui/events/test/events_test_utils_x11.h"
22 class RootWindowEventHandler : public ui::EventHandler {
24 explicit RootWindowEventHandler(aura::WindowTreeHost* host)
25 : target_(host->window()),
26 last_touch_type_(ui::ET_UNKNOWN),
28 last_touch_location_(0, 0) {
29 target_->AddPreTargetHandler(this);
31 virtual ~RootWindowEventHandler() { target_->RemovePreTargetHandler(this); }
34 virtual void OnTouchEvent(ui::TouchEvent* event) OVERRIDE {
35 last_touch_id_ = event->touch_id();
36 last_touch_type_ = event->type();
37 last_touch_location_ = event->location();
40 ui::EventType last_touch_type() { return last_touch_type_; }
42 int last_touch_id() { return last_touch_id_; }
44 gfx::Point last_touch_location() { return last_touch_location_; }
47 ui::EventTarget* target_;
48 ui::EventType last_touch_type_;
50 gfx::Point last_touch_location_;
52 DISALLOW_COPY_AND_ASSIGN(RootWindowEventHandler);
59 typedef aura::test::AuraTestBase WindowTreeHostX11Test;
61 // Send X touch events to one WindowTreeHost. The WindowTreeHost's
62 // delegate will get corresponding ui::TouchEvent if the touch events
63 // are winthin the bound of the WindowTreeHost.
64 TEST_F(WindowTreeHostX11Test, DispatchTouchEventToOneRootWindow) {
65 // Fake a ChromeOS running env.
66 const char* kLsbRelease = "CHROMEOS_RELEASE_NAME=Chromium OS\n";
67 base::SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease, base::Time());
69 scoped_ptr<AshWindowTreeHostX11> window_tree_host(
70 new AshWindowTreeHostX11(gfx::Rect(0, 0, 2560, 1700)));
71 window_tree_host->InitHost();
72 scoped_ptr<RootWindowEventHandler> handler(
73 new RootWindowEventHandler(window_tree_host.get()));
75 std::vector<unsigned int> devices;
77 ui::SetUpTouchDevicesForTest(devices);
78 std::vector<ui::Valuator> valuators;
80 EXPECT_EQ(ui::ET_UNKNOWN, handler->last_touch_type());
81 EXPECT_EQ(-1, handler->last_touch_id());
83 ui::ScopedXI2Event scoped_xevent;
84 // This touch is out of bounds.
85 scoped_xevent.InitTouchEvent(
86 0, XI_TouchBegin, 5, gfx::Point(1500, 2500), valuators);
87 window_tree_host->DispatchEvent(scoped_xevent);
88 EXPECT_EQ(ui::ET_UNKNOWN, handler->last_touch_type());
89 EXPECT_EQ(-1, handler->last_touch_id());
90 EXPECT_EQ(gfx::Point(0, 0), handler->last_touch_location());
92 // Following touchs are within bounds and are passed to delegate.
93 scoped_xevent.InitTouchEvent(
94 0, XI_TouchBegin, 5, gfx::Point(1500, 1500), valuators);
95 window_tree_host->DispatchEvent(scoped_xevent);
96 EXPECT_EQ(ui::ET_TOUCH_PRESSED, handler->last_touch_type());
97 EXPECT_EQ(0, handler->last_touch_id());
98 EXPECT_EQ(gfx::Point(1500, 1500), handler->last_touch_location());
100 scoped_xevent.InitTouchEvent(
101 0, XI_TouchUpdate, 5, gfx::Point(1500, 1600), valuators);
102 window_tree_host->DispatchEvent(scoped_xevent);
103 EXPECT_EQ(ui::ET_TOUCH_MOVED, handler->last_touch_type());
104 EXPECT_EQ(0, handler->last_touch_id());
105 EXPECT_EQ(gfx::Point(1500, 1600), handler->last_touch_location());
107 scoped_xevent.InitTouchEvent(
108 0, XI_TouchEnd, 5, gfx::Point(1500, 1600), valuators);
109 window_tree_host->DispatchEvent(scoped_xevent);
110 EXPECT_EQ(ui::ET_TOUCH_RELEASED, handler->last_touch_type());
111 EXPECT_EQ(0, handler->last_touch_id());
112 EXPECT_EQ(gfx::Point(1500, 1600), handler->last_touch_location());
116 // Revert the CrOS testing env otherwise the following non-CrOS aura
118 // Fake a ChromeOS running env.
120 base::SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease, base::Time());
123 // Send X touch events to two WindowTreeHost. The WindowTreeHost which is
124 // the event target of the X touch events should generate the corresponding
125 // ui::TouchEvent for its delegate.
126 TEST_F(WindowTreeHostX11Test, DispatchTouchEventToTwoRootWindow) {
127 // Fake a ChromeOS running env.
128 const char* kLsbRelease = "CHROMEOS_RELEASE_NAME=Chromium OS\n";
129 base::SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease, base::Time());
131 scoped_ptr<AshWindowTreeHostX11> window_tree_host1(
132 new AshWindowTreeHostX11(gfx::Rect(0, 0, 2560, 1700)));
133 window_tree_host1->InitHost();
134 scoped_ptr<RootWindowEventHandler> handler1(
135 new RootWindowEventHandler(window_tree_host1.get()));
137 int host2_y_offset = 1700;
138 scoped_ptr<AshWindowTreeHostX11> window_tree_host2(
139 new AshWindowTreeHostX11(gfx::Rect(0, host2_y_offset, 1920, 1080)));
140 window_tree_host2->InitHost();
141 scoped_ptr<RootWindowEventHandler> handler2(
142 new RootWindowEventHandler(window_tree_host2.get()));
144 std::vector<unsigned int> devices;
145 devices.push_back(0);
146 ui::SetUpTouchDevicesForTest(devices);
147 std::vector<ui::Valuator> valuators;
149 EXPECT_EQ(ui::ET_UNKNOWN, handler1->last_touch_type());
150 EXPECT_EQ(-1, handler1->last_touch_id());
151 EXPECT_EQ(ui::ET_UNKNOWN, handler2->last_touch_type());
152 EXPECT_EQ(-1, handler2->last_touch_id());
154 // 2 Touch events are targeted at the second WindowTreeHost.
155 ui::ScopedXI2Event scoped_xevent;
156 scoped_xevent.InitTouchEvent(
157 0, XI_TouchBegin, 5, gfx::Point(1500, 2500), valuators);
158 window_tree_host1->DispatchEvent(scoped_xevent);
159 window_tree_host2->DispatchEvent(scoped_xevent);
160 EXPECT_EQ(ui::ET_UNKNOWN, handler1->last_touch_type());
161 EXPECT_EQ(-1, handler1->last_touch_id());
162 EXPECT_EQ(gfx::Point(0, 0), handler1->last_touch_location());
163 EXPECT_EQ(ui::ET_TOUCH_PRESSED, handler2->last_touch_type());
164 EXPECT_EQ(0, handler2->last_touch_id());
165 EXPECT_EQ(gfx::Point(1500, 2500 - host2_y_offset),
166 handler2->last_touch_location());
168 scoped_xevent.InitTouchEvent(
169 0, XI_TouchBegin, 6, gfx::Point(1600, 2600), valuators);
170 window_tree_host1->DispatchEvent(scoped_xevent);
171 window_tree_host2->DispatchEvent(scoped_xevent);
172 EXPECT_EQ(ui::ET_UNKNOWN, handler1->last_touch_type());
173 EXPECT_EQ(-1, handler1->last_touch_id());
174 EXPECT_EQ(gfx::Point(0, 0), handler1->last_touch_location());
175 EXPECT_EQ(ui::ET_TOUCH_PRESSED, handler2->last_touch_type());
176 EXPECT_EQ(1, handler2->last_touch_id());
177 EXPECT_EQ(gfx::Point(1600, 2600 - host2_y_offset),
178 handler2->last_touch_location());
180 scoped_xevent.InitTouchEvent(
181 0, XI_TouchUpdate, 5, gfx::Point(1500, 2550), valuators);
182 window_tree_host1->DispatchEvent(scoped_xevent);
183 window_tree_host2->DispatchEvent(scoped_xevent);
184 EXPECT_EQ(ui::ET_UNKNOWN, handler1->last_touch_type());
185 EXPECT_EQ(-1, handler1->last_touch_id());
186 EXPECT_EQ(gfx::Point(0, 0), handler1->last_touch_location());
187 EXPECT_EQ(ui::ET_TOUCH_MOVED, handler2->last_touch_type());
188 EXPECT_EQ(0, handler2->last_touch_id());
189 EXPECT_EQ(gfx::Point(1500, 2550 - host2_y_offset),
190 handler2->last_touch_location());
192 scoped_xevent.InitTouchEvent(
193 0, XI_TouchUpdate, 6, gfx::Point(1600, 2650), valuators);
194 window_tree_host1->DispatchEvent(scoped_xevent);
195 window_tree_host2->DispatchEvent(scoped_xevent);
196 EXPECT_EQ(ui::ET_UNKNOWN, handler1->last_touch_type());
197 EXPECT_EQ(-1, handler1->last_touch_id());
198 EXPECT_EQ(gfx::Point(0, 0), handler1->last_touch_location());
199 EXPECT_EQ(ui::ET_TOUCH_MOVED, handler2->last_touch_type());
200 EXPECT_EQ(1, handler2->last_touch_id());
201 EXPECT_EQ(gfx::Point(1600, 2650 - host2_y_offset),
202 handler2->last_touch_location());
204 scoped_xevent.InitTouchEvent(
205 0, XI_TouchEnd, 5, gfx::Point(1500, 2550), valuators);
206 window_tree_host1->DispatchEvent(scoped_xevent);
207 window_tree_host2->DispatchEvent(scoped_xevent);
208 EXPECT_EQ(ui::ET_UNKNOWN, handler1->last_touch_type());
209 EXPECT_EQ(-1, handler1->last_touch_id());
210 EXPECT_EQ(gfx::Point(0, 0), handler1->last_touch_location());
211 EXPECT_EQ(ui::ET_TOUCH_RELEASED, handler2->last_touch_type());
212 EXPECT_EQ(0, handler2->last_touch_id());
213 EXPECT_EQ(gfx::Point(1500, 2550 - host2_y_offset),
214 handler2->last_touch_location());
215 scoped_xevent.InitTouchEvent(
216 0, XI_TouchEnd, 6, gfx::Point(1600, 2650), valuators);
217 window_tree_host1->DispatchEvent(scoped_xevent);
218 window_tree_host2->DispatchEvent(scoped_xevent);
219 EXPECT_EQ(ui::ET_UNKNOWN, handler1->last_touch_type());
220 EXPECT_EQ(-1, handler1->last_touch_id());
221 EXPECT_EQ(gfx::Point(0, 0), handler1->last_touch_location());
222 EXPECT_EQ(ui::ET_TOUCH_RELEASED, handler2->last_touch_type());
223 EXPECT_EQ(1, handler2->last_touch_id());
224 EXPECT_EQ(gfx::Point(1600, 2650 - host2_y_offset),
225 handler2->last_touch_location());
230 // Revert the CrOS testing env otherwise the following non-CrOS aura
232 // Fake a ChromeOS running env.
234 base::SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease, base::Time());