Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / ui / aura / window_tree_host_x11_unittest.cc
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.
4
5
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"
16
17 namespace {
18 class TestWindowTreeHostDelegate : public aura::WindowTreeHostDelegate,
19                                    public ui::EventProcessor,
20                                    public ui::EventTarget {
21  public:
22   TestWindowTreeHostDelegate() : last_touch_type_(ui::ET_UNKNOWN),
23                                  last_touch_id_(-1),
24                                  last_touch_location_(0, 0) {
25   }
26   virtual ~TestWindowTreeHostDelegate() {}
27
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 {
40     return this;
41   }
42
43   // ui::EventProcessor:
44   virtual ui::EventTarget* GetRootTarget() OVERRIDE { return this; }
45   virtual bool CanDispatchToTarget(ui::EventTarget* target) OVERRIDE {
46     return true;
47   }
48
49   // ui::EventHandler:
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();
54   }
55
56   // ui::EventTarget:
57   virtual bool CanAcceptEvent(const ui::Event& event) OVERRIDE {
58     return true;
59   }
60   virtual ui::EventTarget* GetParentTarget() OVERRIDE { return NULL; }
61   virtual scoped_ptr<ui::EventTargetIterator>
62   GetChildIterator() const OVERRIDE {
63     return scoped_ptr<ui::EventTargetIterator>();
64   }
65   virtual ui::EventTargeter* GetEventTargeter() OVERRIDE { return &targeter_; }
66
67   ui::EventType last_touch_type() {
68     return last_touch_type_;
69   }
70
71   int last_touch_id() {
72     return last_touch_id_;
73   }
74
75   gfx::Point last_touch_location() {
76     return last_touch_location_;
77   }
78
79  private:
80   ui::EventType last_touch_type_;
81   int last_touch_id_;
82   gfx::Point last_touch_location_;
83   ui::EventTargeter targeter_;
84
85   DISALLOW_COPY_AND_ASSIGN(TestWindowTreeHostDelegate);
86 };
87
88 }  // namespace
89
90 namespace aura {
91
92 typedef test::AuraTestBase WindowTreeHostX11Test;
93
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)
103
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());
109
110   std::vector<unsigned int> devices;
111   devices.push_back(0);
112   ui::SetUpTouchDevicesForTest(devices);
113   std::vector<ui::Valuator> valuators;
114
115   EXPECT_EQ(ui::ET_UNKNOWN, delegate->last_touch_type());
116   EXPECT_EQ(-1, delegate->last_touch_id());
117
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)
128
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());
136
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());
143
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());
150
151   // Revert the CrOS testing env otherwise the following non-CrOS aura
152   // tests will fail.
153 #if defined(OS_CHROMEOS)
154   // Fake a ChromeOS running env.
155   kLsbRelease = "";
156   base::SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease, base::Time());
157 #endif  // defined(OS_CHROMEOS)
158 }
159
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());
168
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());
174
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());
181
182   std::vector<unsigned int> devices;
183   devices.push_back(0);
184   ui::SetUpTouchDevicesForTest(devices);
185   std::vector<ui::Valuator> valuators;
186
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());
191
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());
205
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());
217
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());
229
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());
241
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());
253
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());
265
266   // Revert the CrOS testing env otherwise the following non-CrOS aura
267   // tests will fail.
268   // Fake a ChromeOS running env.
269   kLsbRelease = "";
270   base::SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease, base::Time());
271 }
272 #endif  // defined(OS_CHROMEOS)
273
274 }  // namespace aura