Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / ash / host / ash_window_tree_host_x11_unittest.cc
blobf9af5c3a0ed71160b4b1b38cf52ccc6265c9ac23
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"
7 #undef None
8 #undef Bool
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/aura/window_tree_host_x11.h"
16 #include "ui/events/event_processor.h"
17 #include "ui/events/event_target.h"
18 #include "ui/events/event_target_iterator.h"
19 #include "ui/events/test/events_test_utils_x11.h"
21 namespace {
23 class RootWindowEventHandler : public ui::EventHandler {
24 public:
25 explicit RootWindowEventHandler(aura::WindowTreeHost* host)
26 : target_(host->window()),
27 last_touch_type_(ui::ET_UNKNOWN),
28 last_touch_id_(-1),
29 last_touch_location_(0, 0) {
30 target_->AddPreTargetHandler(this);
32 ~RootWindowEventHandler() override { target_->RemovePreTargetHandler(this); }
34 // ui::EventHandler:
35 void OnTouchEvent(ui::TouchEvent* event) override {
36 last_touch_id_ = event->touch_id();
37 last_touch_type_ = event->type();
38 last_touch_location_ = event->location();
41 ui::EventType last_touch_type() { return last_touch_type_; }
43 int last_touch_id() { return last_touch_id_; }
45 gfx::Point last_touch_location() { return last_touch_location_; }
47 private:
48 ui::EventTarget* target_;
49 ui::EventType last_touch_type_;
50 int last_touch_id_;
51 gfx::Point last_touch_location_;
53 DISALLOW_COPY_AND_ASSIGN(RootWindowEventHandler);
56 } // namespace
58 namespace ash {
60 class AshWindowTreeHostX11Test : public aura::test::AuraTestBase {
61 public:
62 void SetUp() override {
63 aura::test::AuraTestBase::SetUp();
65 #if defined(OS_CHROMEOS)
66 // Fake a ChromeOS running env.
67 const char* kLsbRelease = "CHROMEOS_RELEASE_NAME=Chromium OS\n";
68 base::SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease, base::Time());
69 #endif
72 void TearDown() override {
73 aura::test::AuraTestBase::TearDown();
75 #if defined(OS_CHROMEOS)
76 // Revert the CrOS testing env otherwise the following non-CrOS aura
77 // tests will fail.
78 // Fake a ChromeOS running env.
79 const char* kLsbRelease = "";
80 base::SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease, base::Time());
81 #endif
85 // Fails on ChromeOS valgrind bot. http://crbug.com/499997
86 #if defined(OS_CHROMEOS)
87 #define MAYBE_DispatchTouchEventToOneRootWindow DISABLED_DispatchTouchEventToOneRootWindow
88 #else
89 #define MAYBE_DispatchTouchEventToOneRootWindow DispatchTouchEventToOneRootWindow
90 #endif
92 // Send X touch events to one WindowTreeHost. The WindowTreeHost's
93 // delegate will get corresponding ui::TouchEvent if the touch events
94 // are targeting this WindowTreeHost.
95 TEST_F(AshWindowTreeHostX11Test, MAYBE_DispatchTouchEventToOneRootWindow) {
96 scoped_ptr<aura::WindowTreeHostX11> window_tree_host(
97 new AshWindowTreeHostX11(gfx::Rect(0, 0, 2560, 1700)));
98 window_tree_host->InitHost();
99 scoped_ptr<RootWindowEventHandler> handler(
100 new RootWindowEventHandler(window_tree_host.get()));
102 std::vector<int> devices;
103 devices.push_back(0);
104 ui::SetUpTouchDevicesForTest(devices);
105 std::vector<ui::Valuator> valuators;
107 EXPECT_EQ(ui::ET_UNKNOWN, handler->last_touch_type());
108 EXPECT_EQ(-1, handler->last_touch_id());
110 ui::ScopedXI2Event scoped_xevent;
111 // This touch is out of bounds.
112 scoped_xevent.InitTouchEvent(
113 0, XI_TouchBegin, 5, gfx::Point(1500, 2500), valuators);
114 if (window_tree_host->CanDispatchEvent(scoped_xevent))
115 window_tree_host->DispatchEvent(scoped_xevent);
116 EXPECT_EQ(ui::ET_UNKNOWN, handler->last_touch_type());
117 EXPECT_EQ(-1, handler->last_touch_id());
118 EXPECT_EQ(gfx::Point(0, 0), handler->last_touch_location());
120 // Following touchs are within bounds and are passed to delegate.
121 scoped_xevent.InitTouchEvent(
122 0, XI_TouchBegin, 5, gfx::Point(1500, 1500), valuators);
123 if (window_tree_host->CanDispatchEvent(scoped_xevent))
124 window_tree_host->DispatchEvent(scoped_xevent);
125 EXPECT_EQ(ui::ET_TOUCH_PRESSED, handler->last_touch_type());
126 EXPECT_EQ(0, handler->last_touch_id());
127 EXPECT_EQ(gfx::Point(1500, 1500), handler->last_touch_location());
129 scoped_xevent.InitTouchEvent(
130 0, XI_TouchUpdate, 5, gfx::Point(1500, 1600), valuators);
131 if (window_tree_host->CanDispatchEvent(scoped_xevent))
132 window_tree_host->DispatchEvent(scoped_xevent);
133 EXPECT_EQ(ui::ET_TOUCH_MOVED, handler->last_touch_type());
134 EXPECT_EQ(0, handler->last_touch_id());
135 EXPECT_EQ(gfx::Point(1500, 1600), handler->last_touch_location());
137 scoped_xevent.InitTouchEvent(
138 0, XI_TouchEnd, 5, gfx::Point(1500, 1600), valuators);
139 if (window_tree_host->CanDispatchEvent(scoped_xevent))
140 window_tree_host->DispatchEvent(scoped_xevent);
141 EXPECT_EQ(ui::ET_TOUCH_RELEASED, handler->last_touch_type());
142 EXPECT_EQ(0, handler->last_touch_id());
143 EXPECT_EQ(gfx::Point(1500, 1600), handler->last_touch_location());
145 handler.reset();
148 // Send X touch events to two WindowTreeHost. The WindowTreeHost which is
149 // the event target of the X touch events should generate the corresponding
150 // ui::TouchEvent for its delegate.
151 TEST_F(AshWindowTreeHostX11Test, DispatchTouchEventToTwoRootWindow) {
152 scoped_ptr<aura::WindowTreeHostX11> window_tree_host1(
153 new AshWindowTreeHostX11(gfx::Rect(0, 0, 2560, 1700)));
154 window_tree_host1->InitHost();
155 scoped_ptr<RootWindowEventHandler> handler1(
156 new RootWindowEventHandler(window_tree_host1.get()));
158 int host2_y_offset = 1700;
159 scoped_ptr<aura::WindowTreeHostX11> window_tree_host2(
160 new AshWindowTreeHostX11(gfx::Rect(0, host2_y_offset, 1920, 1080)));
161 window_tree_host2->InitHost();
162 scoped_ptr<RootWindowEventHandler> handler2(
163 new RootWindowEventHandler(window_tree_host2.get()));
165 std::vector<int> devices;
166 devices.push_back(0);
167 ui::SetUpTouchDevicesForTest(devices);
168 std::vector<ui::Valuator> valuators;
170 EXPECT_EQ(ui::ET_UNKNOWN, handler1->last_touch_type());
171 EXPECT_EQ(-1, handler1->last_touch_id());
172 EXPECT_EQ(ui::ET_UNKNOWN, handler2->last_touch_type());
173 EXPECT_EQ(-1, handler2->last_touch_id());
175 // 2 Touch events are targeted at the second WindowTreeHost.
176 ui::ScopedXI2Event scoped_xevent;
177 scoped_xevent.InitTouchEvent(
178 0, XI_TouchBegin, 5, gfx::Point(1500, 2500), valuators);
179 if (window_tree_host1->CanDispatchEvent(scoped_xevent))
180 window_tree_host1->DispatchEvent(scoped_xevent);
181 if (window_tree_host2->CanDispatchEvent(scoped_xevent))
182 window_tree_host2->DispatchEvent(scoped_xevent);
183 EXPECT_EQ(ui::ET_UNKNOWN, handler1->last_touch_type());
184 EXPECT_EQ(-1, handler1->last_touch_id());
185 EXPECT_EQ(gfx::Point(0, 0), handler1->last_touch_location());
186 EXPECT_EQ(ui::ET_TOUCH_PRESSED, handler2->last_touch_type());
187 EXPECT_EQ(0, handler2->last_touch_id());
188 EXPECT_EQ(gfx::Point(1500, 2500), handler2->last_touch_location());
190 scoped_xevent.InitTouchEvent(
191 0, XI_TouchBegin, 6, gfx::Point(1600, 2600), valuators);
192 if (window_tree_host1->CanDispatchEvent(scoped_xevent))
193 window_tree_host1->DispatchEvent(scoped_xevent);
194 if (window_tree_host2->CanDispatchEvent(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_PRESSED, handler2->last_touch_type());
200 EXPECT_EQ(1, handler2->last_touch_id());
201 EXPECT_EQ(gfx::Point(1600, 2600), handler2->last_touch_location());
203 scoped_xevent.InitTouchEvent(
204 0, XI_TouchUpdate, 5, gfx::Point(1500, 2550), valuators);
205 if (window_tree_host1->CanDispatchEvent(scoped_xevent))
206 window_tree_host1->DispatchEvent(scoped_xevent);
207 if (window_tree_host2->CanDispatchEvent(scoped_xevent))
208 window_tree_host2->DispatchEvent(scoped_xevent);
209 EXPECT_EQ(ui::ET_UNKNOWN, handler1->last_touch_type());
210 EXPECT_EQ(-1, handler1->last_touch_id());
211 EXPECT_EQ(gfx::Point(0, 0), handler1->last_touch_location());
212 EXPECT_EQ(ui::ET_TOUCH_MOVED, handler2->last_touch_type());
213 EXPECT_EQ(0, handler2->last_touch_id());
214 EXPECT_EQ(gfx::Point(1500, 2550), handler2->last_touch_location());
216 scoped_xevent.InitTouchEvent(
217 0, XI_TouchUpdate, 6, gfx::Point(1600, 2650), valuators);
218 if (window_tree_host1->CanDispatchEvent(scoped_xevent))
219 window_tree_host1->DispatchEvent(scoped_xevent);
220 if (window_tree_host2->CanDispatchEvent(scoped_xevent))
221 window_tree_host2->DispatchEvent(scoped_xevent);
222 EXPECT_EQ(ui::ET_UNKNOWN, handler1->last_touch_type());
223 EXPECT_EQ(-1, handler1->last_touch_id());
224 EXPECT_EQ(gfx::Point(0, 0), handler1->last_touch_location());
225 EXPECT_EQ(ui::ET_TOUCH_MOVED, handler2->last_touch_type());
226 EXPECT_EQ(1, handler2->last_touch_id());
227 EXPECT_EQ(gfx::Point(1600, 2650), handler2->last_touch_location());
229 scoped_xevent.InitTouchEvent(
230 0, XI_TouchEnd, 5, gfx::Point(1500, 2550), valuators);
231 if (window_tree_host1->CanDispatchEvent(scoped_xevent))
232 window_tree_host1->DispatchEvent(scoped_xevent);
233 if (window_tree_host2->CanDispatchEvent(scoped_xevent))
234 window_tree_host2->DispatchEvent(scoped_xevent);
235 EXPECT_EQ(ui::ET_UNKNOWN, handler1->last_touch_type());
236 EXPECT_EQ(-1, handler1->last_touch_id());
237 EXPECT_EQ(gfx::Point(0, 0), handler1->last_touch_location());
238 EXPECT_EQ(ui::ET_TOUCH_RELEASED, handler2->last_touch_type());
239 EXPECT_EQ(0, handler2->last_touch_id());
240 EXPECT_EQ(gfx::Point(1500, 2550), handler2->last_touch_location());
242 scoped_xevent.InitTouchEvent(
243 0, XI_TouchEnd, 6, gfx::Point(1600, 2650), valuators);
244 if (window_tree_host1->CanDispatchEvent(scoped_xevent))
245 window_tree_host1->DispatchEvent(scoped_xevent);
246 if (window_tree_host2->CanDispatchEvent(scoped_xevent))
247 window_tree_host2->DispatchEvent(scoped_xevent);
248 EXPECT_EQ(ui::ET_UNKNOWN, handler1->last_touch_type());
249 EXPECT_EQ(-1, handler1->last_touch_id());
250 EXPECT_EQ(gfx::Point(0, 0), handler1->last_touch_location());
251 EXPECT_EQ(ui::ET_TOUCH_RELEASED, handler2->last_touch_type());
252 EXPECT_EQ(1, handler2->last_touch_id());
253 EXPECT_EQ(gfx::Point(1600, 2650), handler2->last_touch_location());
255 handler1.reset();
256 handler2.reset();
259 } // namespace aura