summaryrefslogtreecommitdiffstats
path: root/ui/aura/root_window_host_x11_unittest.cc
blob: a7dfa1442542a34386cad8552ed3836d21e1132e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.


#include "base/sys_info.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/aura/root_window.h"
#include "ui/aura/root_window_host_x11.h"
#include "ui/aura/test/aura_test_base.h"
#include "ui/aura/window_tree_host_delegate.h"
#include "ui/events/x/events_x_utils.h"

namespace {
class TestWindowTreeHostDelegate : public aura::WindowTreeHostDelegate {
 public:
  TestWindowTreeHostDelegate() : last_touch_type_(ui::ET_UNKNOWN),
                                 last_touch_id_(-1),
                                 last_touch_location_(0, 0) {
  }
  virtual ~TestWindowTreeHostDelegate() {}

  virtual bool OnHostKeyEvent(ui::KeyEvent* event) OVERRIDE {
    return true;
  }

  virtual bool OnHostMouseEvent(ui::MouseEvent* event) OVERRIDE {
    return true;
  }
  virtual bool OnHostScrollEvent(ui::ScrollEvent* event) OVERRIDE {
    return true;
  }

  virtual bool OnHostTouchEvent(ui::TouchEvent* event) OVERRIDE {
    last_touch_id_ = event->touch_id();
    last_touch_type_ = event->type();
    last_touch_location_ = event->location();
    return true;
  }

  virtual void OnHostCancelMode() OVERRIDE {}

  // Called when the windowing system activates the window.
  virtual void OnHostActivated() OVERRIDE {}

  // Called when system focus is changed to another window.
  virtual void OnHostLostWindowCapture() OVERRIDE {}

  // Called when the windowing system has mouse grab because it's performing a
  // window move on our behalf, but we should still paint as if we're active.
  virtual void OnHostLostMouseGrab() OVERRIDE {}

  virtual void OnHostMoved(const gfx::Point& origin) OVERRIDE {}
  virtual void OnHostResized(const gfx::Size& size) OVERRIDE {}

  virtual aura::RootWindow* AsRootWindow() OVERRIDE {
    return NULL;
  }
  virtual const aura::RootWindow* AsRootWindow() const OVERRIDE {
    return NULL;
  }

  ui::EventType last_touch_type() {
    return last_touch_type_;
  }

  int last_touch_id() {
    return last_touch_id_;
  }

  gfx::Point last_touch_location() {
    return last_touch_location_;
  }

 private:
  ui::EventType last_touch_type_;
  int last_touch_id_;
  gfx::Point last_touch_location_;

  DISALLOW_COPY_AND_ASSIGN(TestWindowTreeHostDelegate);
};

}  // namespace

namespace aura {

typedef test::AuraTestBase WindowTreeHostX11Test;

// Send X touch events to one WindowTreeHost. The WindowTreeHost's
// delegate will get corresponding ui::TouchEvent if the touch events
// are winthin the bound of the WindowTreeHost.
TEST_F(WindowTreeHostX11Test, DispatchTouchEventToOneRootWindow) {
#if defined(OS_CHROMEOS)
  // Fake a ChromeOS running env.
  const char* kLsbRelease = "CHROMEOS_RELEASE_NAME=Chromium OS\n";
  base::SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease, base::Time());
#endif  // defined(OS_CHROMEOS)

  scoped_ptr<WindowTreeHostX11> root_window_host(
      new WindowTreeHostX11(gfx::Rect(0, 0, 2560, 1700)));
  scoped_ptr<TestWindowTreeHostDelegate> delegate(
      new TestWindowTreeHostDelegate());
  root_window_host->set_delegate(delegate.get());

  std::vector<unsigned int> devices;
  devices.push_back(0);
  ui::SetupTouchDevicesForTest(devices);
  std::vector<ui::Valuator> valuators;

  EXPECT_EQ(ui::ET_UNKNOWN, delegate->last_touch_type());
  EXPECT_EQ(-1, delegate->last_touch_id());

#if defined(OS_CHROMEOS)
  // This touch is out of bounds.
  ui::XScopedTouchEvent event1(ui::CreateTouchEvent(
      0, XI_TouchBegin, 5, gfx::Point(1500, 2500), valuators));
  root_window_host->Dispatch(event1);
  EXPECT_EQ(ui::ET_UNKNOWN, delegate->last_touch_type());
  EXPECT_EQ(-1, delegate->last_touch_id());
  EXPECT_EQ(gfx::Point(0, 0), delegate->last_touch_location());
#endif  // defined(OS_CHROMEOS)

  // Following touchs are within bounds and are passed to delegate.
  ui::XScopedTouchEvent event2(ui::CreateTouchEvent(
      0, XI_TouchBegin, 5, gfx::Point(1500, 1500), valuators));
  root_window_host->Dispatch(event2);
  EXPECT_EQ(ui::ET_TOUCH_PRESSED, delegate->last_touch_type());
  EXPECT_EQ(0, delegate->last_touch_id());
  EXPECT_EQ(gfx::Point(1500, 1500), delegate->last_touch_location());

  ui::XScopedTouchEvent event3(ui::CreateTouchEvent(
      0, XI_TouchUpdate, 5, gfx::Point(1500, 1600), valuators));
  root_window_host->Dispatch(event3);
  EXPECT_EQ(ui::ET_TOUCH_MOVED, delegate->last_touch_type());
  EXPECT_EQ(0, delegate->last_touch_id());
  EXPECT_EQ(gfx::Point(1500, 1600), delegate->last_touch_location());

  ui::XScopedTouchEvent event4(ui::CreateTouchEvent(
      0, XI_TouchEnd, 5, gfx::Point(1500, 1600), valuators));
  root_window_host->Dispatch(event4);
  EXPECT_EQ(ui::ET_TOUCH_RELEASED, delegate->last_touch_type());
  EXPECT_EQ(0, delegate->last_touch_id());
  EXPECT_EQ(gfx::Point(1500, 1600), delegate->last_touch_location());

  // Revert the CrOS testing env otherwise the following non-CrOS aura
  // tests will fail.
#if defined(OS_CHROMEOS)
  // Fake a ChromeOS running env.
  kLsbRelease = "";
  base::SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease, base::Time());
#endif  // defined(OS_CHROMEOS)
}

// Send X touch events to two WindowTreeHost. The WindowTreeHost which is
// the event target of the X touch events should generate the corresponding
// ui::TouchEvent for its delegate.
#if defined(OS_CHROMEOS)
TEST_F(WindowTreeHostX11Test, DispatchTouchEventToTwoRootWindow) {
  // Fake a ChromeOS running env.
  const char* kLsbRelease = "CHROMEOS_RELEASE_NAME=Chromium OS\n";
  base::SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease, base::Time());

  scoped_ptr<WindowTreeHostX11> root_window_host1(
      new WindowTreeHostX11(gfx::Rect(0, 0, 2560, 1700)));
  scoped_ptr<TestWindowTreeHostDelegate> delegate1(
      new TestWindowTreeHostDelegate());
  root_window_host1->set_delegate(delegate1.get());

  int host2_y_offset = 1700;
  scoped_ptr<WindowTreeHostX11> root_window_host2(
      new WindowTreeHostX11(gfx::Rect(0, host2_y_offset, 1920, 1080)));
  scoped_ptr<TestWindowTreeHostDelegate> delegate2(
      new TestWindowTreeHostDelegate());
  root_window_host2->set_delegate(delegate2.get());

  std::vector<unsigned int> devices;
  devices.push_back(0);
  ui::SetupTouchDevicesForTest(devices);
  std::vector<ui::Valuator> valuators;

  EXPECT_EQ(ui::ET_UNKNOWN, delegate1->last_touch_type());
  EXPECT_EQ(-1, delegate1->last_touch_id());
  EXPECT_EQ(ui::ET_UNKNOWN, delegate2->last_touch_type());
  EXPECT_EQ(-1, delegate2->last_touch_id());

  // 2 Touch events are targeted at the second WindowTreeHost.
  ui::XScopedTouchEvent touch1_begin(ui::CreateTouchEvent(
      0, XI_TouchBegin, 5, gfx::Point(1500, 2500), valuators));
  root_window_host1->Dispatch(touch1_begin);
  root_window_host2->Dispatch(touch1_begin);
  EXPECT_EQ(ui::ET_UNKNOWN, delegate1->last_touch_type());
  EXPECT_EQ(-1, delegate1->last_touch_id());
  EXPECT_EQ(gfx::Point(0, 0), delegate1->last_touch_location());
  EXPECT_EQ(ui::ET_TOUCH_PRESSED, delegate2->last_touch_type());
  EXPECT_EQ(0, delegate2->last_touch_id());
  EXPECT_EQ(gfx::Point(1500, 2500 - host2_y_offset),
            delegate2->last_touch_location());

  ui::XScopedTouchEvent touch2_begin(ui::CreateTouchEvent(
      0, XI_TouchBegin, 6, gfx::Point(1600, 2600), valuators));
  root_window_host1->Dispatch(touch2_begin);
  root_window_host2->Dispatch(touch2_begin);
  EXPECT_EQ(ui::ET_UNKNOWN, delegate1->last_touch_type());
  EXPECT_EQ(-1, delegate1->last_touch_id());
  EXPECT_EQ(gfx::Point(0, 0), delegate1->last_touch_location());
  EXPECT_EQ(ui::ET_TOUCH_PRESSED, delegate2->last_touch_type());
  EXPECT_EQ(1, delegate2->last_touch_id());
  EXPECT_EQ(gfx::Point(1600, 2600 - host2_y_offset),
            delegate2->last_touch_location());

  ui::XScopedTouchEvent touch1_move(ui::CreateTouchEvent(
      0, XI_TouchUpdate, 5, gfx::Point(1500, 2550), valuators));
  root_window_host1->Dispatch(touch1_move);
  root_window_host2->Dispatch(touch1_move);
  EXPECT_EQ(ui::ET_UNKNOWN, delegate1->last_touch_type());
  EXPECT_EQ(-1, delegate1->last_touch_id());
  EXPECT_EQ(gfx::Point(0, 0), delegate1->last_touch_location());
  EXPECT_EQ(ui::ET_TOUCH_MOVED, delegate2->last_touch_type());
  EXPECT_EQ(0, delegate2->last_touch_id());
  EXPECT_EQ(gfx::Point(1500, 2550 - host2_y_offset),
            delegate2->last_touch_location());

  ui::XScopedTouchEvent touch2_move(ui::CreateTouchEvent(
      0, XI_TouchUpdate, 6, gfx::Point(1600, 2650), valuators));
  root_window_host1->Dispatch(touch2_move);
  root_window_host2->Dispatch(touch2_move);
  EXPECT_EQ(ui::ET_UNKNOWN, delegate1->last_touch_type());
  EXPECT_EQ(-1, delegate1->last_touch_id());
  EXPECT_EQ(gfx::Point(0, 0), delegate1->last_touch_location());
  EXPECT_EQ(ui::ET_TOUCH_MOVED, delegate2->last_touch_type());
  EXPECT_EQ(1, delegate2->last_touch_id());
  EXPECT_EQ(gfx::Point(1600, 2650 - host2_y_offset),
            delegate2->last_touch_location());

  ui::XScopedTouchEvent touch1_end(ui::CreateTouchEvent(
      0, XI_TouchEnd, 5, gfx::Point(1500, 2550), valuators));
  root_window_host1->Dispatch(touch1_end);
  root_window_host2->Dispatch(touch1_end);
  EXPECT_EQ(ui::ET_UNKNOWN, delegate1->last_touch_type());
  EXPECT_EQ(-1, delegate1->last_touch_id());
  EXPECT_EQ(gfx::Point(0, 0), delegate1->last_touch_location());
  EXPECT_EQ(ui::ET_TOUCH_RELEASED, delegate2->last_touch_type());
  EXPECT_EQ(0, delegate2->last_touch_id());
  EXPECT_EQ(gfx::Point(1500, 2550 - host2_y_offset),
            delegate2->last_touch_location());

  ui::XScopedTouchEvent touch2_end(ui::CreateTouchEvent(
      0, XI_TouchEnd, 6, gfx::Point(1600, 2650), valuators));
  root_window_host1->Dispatch(touch2_end);
  root_window_host2->Dispatch(touch2_end);
  EXPECT_EQ(ui::ET_UNKNOWN, delegate1->last_touch_type());
  EXPECT_EQ(-1, delegate1->last_touch_id());
  EXPECT_EQ(gfx::Point(0, 0), delegate1->last_touch_location());
  EXPECT_EQ(ui::ET_TOUCH_RELEASED, delegate2->last_touch_type());
  EXPECT_EQ(1, delegate2->last_touch_id());
  EXPECT_EQ(gfx::Point(1600, 2650 - host2_y_offset),
            delegate2->last_touch_location());

  // Revert the CrOS testing env otherwise the following non-CrOS aura
  // tests will fail.
  // Fake a ChromeOS running env.
  kLsbRelease = "";
  base::SysInfo::SetChromeOSVersionInfoForTest(kLsbRelease, base::Time());
}
#endif  // defined(OS_CHROMEOS)

}  // namespace aura