summaryrefslogtreecommitdiffstats
path: root/ui/aura/root_window_unittest.cc
blob: 2a53fa607b8e568630ceb2bd98d08c364d3d183b (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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
// Copyright (c) 2012 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 "ui/aura/root_window.h"

#include <vector>

#include "testing/gtest/include/gtest/gtest.h"
#include "ui/aura/client/event_client.h"
#include "ui/aura/env.h"
#include "ui/aura/event.h"
#include "ui/aura/event_filter.h"
#include "ui/aura/test/aura_test_base.h"
#include "ui/aura/test/event_generator.h"
#include "ui/aura/test/test_window_delegate.h"
#include "ui/aura/test/test_windows.h"
#include "ui/base/gestures/gesture_configuration.h"
#include "ui/base/hit_test.h"
#include "ui/base/keycodes/keyboard_codes.h"
#include "ui/gfx/point.h"
#include "ui/gfx/rect.h"
#include "ui/gfx/screen.h"

namespace aura {
namespace {

// A delegate that always returns a non-client component for hit tests.
class NonClientDelegate : public test::TestWindowDelegate {
 public:
  NonClientDelegate()
      : non_client_count_(0),
        mouse_event_count_(0),
        mouse_event_flags_(0x0) {
  }
  virtual ~NonClientDelegate() {}

  int non_client_count() const { return non_client_count_; }
  gfx::Point non_client_location() const { return non_client_location_; }
  int mouse_event_count() const { return mouse_event_count_; }
  gfx::Point mouse_event_location() const { return mouse_event_location_; }
  int mouse_event_flags() const { return mouse_event_flags_; }

  virtual int GetNonClientComponent(const gfx::Point& location) const OVERRIDE {
    NonClientDelegate* self = const_cast<NonClientDelegate*>(this);
    self->non_client_count_++;
    self->non_client_location_ = location;
    return HTTOPLEFT;
  }
  virtual bool OnMouseEvent(MouseEvent* event) OVERRIDE {
    mouse_event_count_++;
    mouse_event_location_ = event->location();
    mouse_event_flags_ = event->flags();
    return true;
  }

 private:
  int non_client_count_;
  gfx::Point non_client_location_;
  int mouse_event_count_;
  gfx::Point mouse_event_location_;
  int mouse_event_flags_;

  DISALLOW_COPY_AND_ASSIGN(NonClientDelegate);
};

// A simple EventFilter that keeps track of the number of key events that it's
// seen.
class EventCountFilter : public EventFilter {
 public:
  EventCountFilter() : num_key_events_(0), num_mouse_events_(0) {}
  virtual ~EventCountFilter() {}

  int num_key_events() const { return num_key_events_; }
  int num_mouse_events() const { return num_mouse_events_; }

  void Reset() {
    num_key_events_ = 0;
    num_mouse_events_ = 0;
  }

  // EventFilter overrides:
  virtual bool PreHandleKeyEvent(Window* target, KeyEvent* event) OVERRIDE {
    num_key_events_++;
    return true;
  }
  virtual bool PreHandleMouseEvent(Window* target, MouseEvent* event) OVERRIDE {
    num_mouse_events_++;
    return true;
  }
  virtual ui::TouchStatus PreHandleTouchEvent(
      Window* target, TouchEvent* event) OVERRIDE {
    return ui::TOUCH_STATUS_UNKNOWN;
  }
  virtual ui::GestureStatus PreHandleGestureEvent(
      Window* target, GestureEvent* event) OVERRIDE {
    return ui::GESTURE_STATUS_UNKNOWN;
  }

 private:
  // How many key events have been received?
  int num_key_events_;

  // How many mouse events have been received?
  int num_mouse_events_;

  DISALLOW_COPY_AND_ASSIGN(EventCountFilter);
};

}  // namespace

typedef test::AuraTestBase RootWindowTest;

TEST_F(RootWindowTest, DispatchMouseEvent) {
  // Create two non-overlapping windows so we don't have to worry about which
  // is on top.
  scoped_ptr<NonClientDelegate> delegate1(new NonClientDelegate());
  scoped_ptr<NonClientDelegate> delegate2(new NonClientDelegate());
  const int kWindowWidth = 123;
  const int kWindowHeight = 45;
  gfx::Rect bounds1(100, 200, kWindowWidth, kWindowHeight);
  gfx::Rect bounds2(300, 400, kWindowWidth, kWindowHeight);
  scoped_ptr<aura::Window> window1(CreateTestWindowWithDelegate(
      delegate1.get(), -1234, bounds1, NULL));
  scoped_ptr<aura::Window> window2(CreateTestWindowWithDelegate(
      delegate2.get(), -5678, bounds2, NULL));

  // Send a mouse event to window1.
  gfx::Point point(101, 201);
  MouseEvent event1(
      ui::ET_MOUSE_PRESSED, point, point, ui::EF_LEFT_MOUSE_BUTTON);
  root_window()->DispatchMouseEvent(&event1);

  // Event was tested for non-client area for the target window.
  EXPECT_EQ(1, delegate1->non_client_count());
  EXPECT_EQ(0, delegate2->non_client_count());
  // The non-client component test was in local coordinates.
  EXPECT_EQ(gfx::Point(1, 1), delegate1->non_client_location());
  // Mouse event was received by target window.
  EXPECT_EQ(1, delegate1->mouse_event_count());
  EXPECT_EQ(0, delegate2->mouse_event_count());
  // Event was in local coordinates.
  EXPECT_EQ(gfx::Point(1, 1), delegate1->mouse_event_location());
  // Non-client flag was set.
  EXPECT_TRUE(delegate1->mouse_event_flags() & ui::EF_IS_NON_CLIENT);
}

// Check that we correctly track the state of the mouse buttons in response to
// button press and release events.
TEST_F(RootWindowTest, MouseButtonState) {
  EXPECT_FALSE(Env::GetInstance()->is_mouse_button_down());

  gfx::Point location;
  scoped_ptr<MouseEvent> event;

  // Press the left button.
  event.reset(new MouseEvent(
      ui::ET_MOUSE_PRESSED,
      location,
      location,
      ui::EF_LEFT_MOUSE_BUTTON));
  root_window()->DispatchMouseEvent(event.get());
  EXPECT_TRUE(Env::GetInstance()->is_mouse_button_down());

  // Additionally press the right.
  event.reset(new MouseEvent(
      ui::ET_MOUSE_PRESSED,
      location,
      location,
      ui::EF_LEFT_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON));
  root_window()->DispatchMouseEvent(event.get());
  EXPECT_TRUE(Env::GetInstance()->is_mouse_button_down());

  // Release the left button.
  event.reset(new MouseEvent(
      ui::ET_MOUSE_RELEASED,
      location,
      location,
      ui::EF_RIGHT_MOUSE_BUTTON));
  root_window()->DispatchMouseEvent(event.get());
  EXPECT_TRUE(Env::GetInstance()->is_mouse_button_down());

  // Release the right button.  We should ignore the Shift-is-down flag.
  event.reset(new MouseEvent(
      ui::ET_MOUSE_RELEASED,
      location,
      location,
      ui::EF_SHIFT_DOWN));
  root_window()->DispatchMouseEvent(event.get());
  EXPECT_FALSE(Env::GetInstance()->is_mouse_button_down());

  // Press the middle button.
  event.reset(new MouseEvent(
      ui::ET_MOUSE_PRESSED,
      location,
      location,
      ui::EF_MIDDLE_MOUSE_BUTTON));
  root_window()->DispatchMouseEvent(event.get());
  EXPECT_TRUE(Env::GetInstance()->is_mouse_button_down());
}

TEST_F(RootWindowTest, TranslatedEvent) {
  scoped_ptr<Window> w1(test::CreateTestWindowWithDelegate(NULL, 1,
      gfx::Rect(50, 50, 100, 100), NULL));

  gfx::Point origin(100, 100);
  MouseEvent root(ui::ET_MOUSE_PRESSED, origin, origin, 0);

  EXPECT_EQ("100,100", root.location().ToString());
  EXPECT_EQ("100,100", root.root_location().ToString());

  MouseEvent translated_event(
      root, root_window(), w1.get(),
      ui::ET_MOUSE_ENTERED, root.flags());
  EXPECT_EQ("50,50", translated_event.location().ToString());
  EXPECT_EQ("100,100", translated_event.root_location().ToString());
}

namespace {

class TestEventClient : public client::EventClient {
 public:
  static const int kNonLockWindowId = 100;
  static const int kLockWindowId = 200;

  explicit TestEventClient(RootWindow* root_window)
      : root_window_(root_window),
        lock_(false) {
    client::SetEventClient(root_window_, this);
    Window* lock_window =
        test::CreateTestWindowWithBounds(root_window_->bounds(), root_window_);
    lock_window->set_id(kLockWindowId);
    Window* non_lock_window =
        test::CreateTestWindowWithBounds(root_window_->bounds(), root_window_);
    non_lock_window->set_id(kNonLockWindowId);
  }
  virtual ~TestEventClient() {
    client::SetEventClient(root_window_, NULL);
  }

  // Starts/stops locking. Locking prevents windows other than those inside
  // the lock container from receiving events, getting focus etc.
  void Lock() {
    lock_ = true;
  }
  void Unlock() {
    lock_ = false;
  }

  Window* GetLockWindow() {
    return const_cast<Window*>(
        static_cast<const TestEventClient*>(this)->GetLockWindow());
  }
  const Window* GetLockWindow() const {
    return root_window_->GetChildById(kLockWindowId);
  }
  Window* GetNonLockWindow() {
    return root_window_->GetChildById(kNonLockWindowId);
  }

 private:
  // Overridden from client::EventClient:
  virtual bool CanProcessEventsWithinSubtree(
      const Window* window) const OVERRIDE {
    return lock_ ? GetLockWindow()->Contains(window) : true;
  }

  RootWindow* root_window_;
  bool lock_;

  DISALLOW_COPY_AND_ASSIGN(TestEventClient);
};

}  // namespace

TEST_F(RootWindowTest, CanProcessEventsWithinSubtree) {
  TestEventClient client(root_window());
  test::TestWindowDelegate d;

  EventCountFilter* nonlock_ef = new EventCountFilter;
  EventCountFilter* lock_ef = new EventCountFilter;
  client.GetNonLockWindow()->SetEventFilter(nonlock_ef);
  client.GetLockWindow()->SetEventFilter(lock_ef);

  Window* w1 = test::CreateTestWindowWithBounds(gfx::Rect(10, 10, 20, 20),
                                                client.GetNonLockWindow());
  w1->set_id(1);
  Window* w2 = test::CreateTestWindowWithBounds(gfx::Rect(30, 30, 20, 20),
                                                client.GetNonLockWindow());
  w2->set_id(2);
  scoped_ptr<Window> w3(
      test::CreateTestWindowWithDelegate(&d, 3, gfx::Rect(20, 20, 20, 20),
                                         client.GetLockWindow()));

  w1->Focus();
  EXPECT_TRUE(w1->GetFocusManager()->IsFocusedWindow(w1));

  client.Lock();

  // Since we're locked, the attempt to focus w2 will be ignored.
  w2->Focus();
  EXPECT_TRUE(w1->GetFocusManager()->IsFocusedWindow(w1));
  EXPECT_FALSE(w1->GetFocusManager()->IsFocusedWindow(w2));

  {
    // Attempting to send a key event to w1 (not in the lock container) should
    // cause focus to be reset.
    test::EventGenerator generator(root_window());
    generator.PressKey(ui::VKEY_SPACE, 0);
    EXPECT_EQ(NULL, w1->GetFocusManager()->GetFocusedWindow());
  }

  {
    // Events sent to a window not in the lock container will not be processed.
    // i.e. never sent to the non-lock container's event filter.
    test::EventGenerator generator(root_window(), w1);
    generator.PressLeftButton();
    EXPECT_EQ(0, nonlock_ef->num_mouse_events());

    // Events sent to a window in the lock container will be processed.
    test::EventGenerator generator3(root_window(), w3.get());
    generator3.PressLeftButton();
    EXPECT_EQ(1, lock_ef->num_mouse_events());
  }

  // Prevent w3 from being deleted by the hierarchy since its delegate is owned
  // by this scope.
  w3->parent()->RemoveChild(w3.get());
}

TEST_F(RootWindowTest, IgnoreUnknownKeys) {
  EventCountFilter* filter = new EventCountFilter;
  root_window()->SetEventFilter(filter);  // passes ownership

  KeyEvent unknown_event(ui::ET_KEY_PRESSED, ui::VKEY_UNKNOWN, 0);
  EXPECT_FALSE(root_window()->DispatchKeyEvent(&unknown_event));
  EXPECT_EQ(0, filter->num_key_events());

  KeyEvent known_event(ui::ET_KEY_PRESSED, ui::VKEY_A, 0);
  EXPECT_TRUE(root_window()->DispatchKeyEvent(&known_event));
  EXPECT_EQ(1, filter->num_key_events());
}

namespace {

// FilterFilter that tracks the types of events it's seen.
class EventFilterRecorder : public EventFilter {
 public:
  typedef std::vector<ui::EventType> Events;

  EventFilterRecorder() {}

  Events& events() { return events_; }

  // EventFilter overrides:
  virtual bool PreHandleKeyEvent(Window* target, KeyEvent* event) OVERRIDE {
    events_.push_back(event->type());
    return true;
  }
  virtual bool PreHandleMouseEvent(Window* target, MouseEvent* event) OVERRIDE {
    events_.push_back(event->type());
    return true;
  }
  virtual ui::TouchStatus PreHandleTouchEvent(Window* target,
                                              TouchEvent* event) OVERRIDE {
    events_.push_back(event->type());
    return ui::TOUCH_STATUS_UNKNOWN;
  }
  virtual ui::GestureStatus PreHandleGestureEvent(
      Window* target,
      GestureEvent* event) OVERRIDE {
    events_.push_back(event->type());
    return ui::GESTURE_STATUS_UNKNOWN;
  }

 private:
  Events events_;

  DISALLOW_COPY_AND_ASSIGN(EventFilterRecorder);
};

// Converts an EventType to a string.
std::string EventTypeToString(ui::EventType type) {
  switch (type) {
    case ui::ET_TOUCH_RELEASED:
      return "TOUCH_RELEASED";

    case ui::ET_TOUCH_PRESSED:
      return "TOUCH_PRESSED";

    case ui::ET_TOUCH_MOVED:
      return "TOUCH_MOVED";

    case ui::ET_MOUSE_PRESSED:
      return "MOUSE_PRESSED";

    case ui::ET_MOUSE_DRAGGED:
      return "MOUSE_DRAGGED";

    case ui::ET_MOUSE_RELEASED:
      return "MOUSE_RELEASED";

    case ui::ET_MOUSE_MOVED:
      return "MOUSE_MOVED";

    case ui::ET_MOUSE_ENTERED:
      return "MOUSE_ENTERED";

    case ui::ET_MOUSE_EXITED:
      return "MOUSE_EXITED";

    case ui::ET_GESTURE_SCROLL_BEGIN:
      return "GESTURE_SCROLL_BEGIN";

    case ui::ET_GESTURE_SCROLL_END:
      return "GESTURE_SCROLL_END";

    case ui::ET_GESTURE_SCROLL_UPDATE:
      return "GESTURE_SCROLL_UPDATE";

    case ui::ET_GESTURE_TAP:
      return "GESTURE_TAP";

    case ui::ET_GESTURE_TAP_DOWN:
      return "GESTURE_TAP_DOWN";

    case ui::ET_GESTURE_DOUBLE_TAP:
      return "GESTURE_DOUBLE_TAP";

    default:
      break;
  }
  return "";
}

std::string EventTypesToString(const EventFilterRecorder::Events& events) {
  std::string result;
  for (size_t i = 0; i < events.size(); ++i) {
    if (i != 0)
      result += " ";
    result += EventTypeToString(events[i]);
  }
  return result;
}

}  // namespace

// Makes sure touch events are mapped to mouse events.
TEST_F(RootWindowTest, GestureToMouseEventTest) {
  EventFilterRecorder* filter = new EventFilterRecorder;
  root_window()->SetEventFilter(filter);  // passes ownership

  test::TestWindowDelegate delegate;
  scoped_ptr<aura::Window> window1(CreateTestWindowWithDelegate(
      &delegate, 1, gfx::Rect(0, 0, 250, 250), NULL));

  // ET_TOUCH_PRESSED/RELEASED should generate mouse pressed/released.
  {
    TouchEvent touch_pressed_event(ui::ET_TOUCH_PRESSED, gfx::Point(100, 101),
                                   1, base::TimeDelta());
    int time_ms =
        static_cast<int>(ui::GestureConfiguration::
                         min_touch_down_duration_in_seconds_for_click() * 1000);
    TouchEvent touch_released_event(
        ui::ET_TOUCH_RELEASED, gfx::Point(100, 101), 1,
        base::TimeDelta::FromMilliseconds(time_ms));
    root_window()->DispatchTouchEvent(&touch_pressed_event);
    root_window()->DispatchTouchEvent(&touch_released_event);
    EXPECT_EQ("TOUCH_PRESSED GESTURE_TAP_DOWN TOUCH_RELEASED GESTURE_TAP "
              "MOUSE_ENTERED MOUSE_MOVED MOUSE_PRESSED MOUSE_RELEASED",
              EventTypesToString(filter->events()));
    filter->events().clear();
  }

  // ET_TOUCH_PRESSED should generate a GESTURE_TAP_DOWN.
  {
    TouchEvent touch_event(ui::ET_TOUCH_PRESSED, gfx::Point(100, 101), 1,
                           base::TimeDelta());
    root_window()->DispatchTouchEvent(&touch_event);
    EXPECT_EQ("TOUCH_PRESSED GESTURE_TAP_DOWN",
              EventTypesToString(filter->events()));
    filter->events().clear();
  }

  // ET_TOUCH_MOVED should start a scroll and generate mouse drags (among other
  // things).
  {
    TouchEvent touch_event(ui::ET_TOUCH_MOVED, gfx::Point(200, 201), 1,
                           base::TimeDelta());
    root_window()->DispatchTouchEvent(&touch_event);
    EXPECT_EQ("TOUCH_MOVED GESTURE_SCROLL_BEGIN MOUSE_PRESSED MOUSE_DRAGGED "
              "GESTURE_SCROLL_UPDATE MOUSE_DRAGGED",
              EventTypesToString(filter->events()));
    filter->events().clear();
  }

  // The location of the cursor should have been updated.
  EXPECT_EQ("200,201", gfx::Screen::GetCursorScreenPoint().ToString());

  // ET_TOUCH_MOVED should generate a scroll and drag.
  {
    TouchEvent touch_event(ui::ET_TOUCH_MOVED, gfx::Point(300, 201), 1,
                           base::TimeDelta());
    root_window()->DispatchTouchEvent(&touch_event);
    EXPECT_EQ("TOUCH_MOVED GESTURE_SCROLL_UPDATE MOUSE_DRAGGED",
              EventTypesToString(filter->events()));
    filter->events().clear();
  }

  // ET_TOUCH_RELEASED should end the scroll and release the mouse.
  {
    TouchEvent touch_event(ui::ET_TOUCH_RELEASED, gfx::Point(300, 201), 1,
                           base::TimeDelta());
    root_window()->DispatchTouchEvent(&touch_event);
    EXPECT_EQ("TOUCH_RELEASED GESTURE_SCROLL_END MOUSE_DRAGGED MOUSE_RELEASED",
              EventTypesToString(filter->events()));
    filter->events().clear();
  }
}

TEST_F(RootWindowTest, MouseMoveThenTouch) {
  EventFilterRecorder* filter = new EventFilterRecorder;
  root_window()->SetEventFilter(filter);  // passes ownership

  test::TestWindowDelegate delegate;
  scoped_ptr<aura::Window> window1(CreateTestWindowWithDelegate(
      &delegate, 1, gfx::Rect(0, 0, 100, 100), NULL));
  scoped_ptr<aura::Window> window2(CreateTestWindowWithDelegate(
      &delegate, 1, gfx::Rect(150, 150, 50, 50), NULL));

  // Move the mouse over window1.
  {
    MouseEvent move_mouse_event(ui::ET_MOUSE_MOVED, gfx::Point(50, 50),
                                gfx::Point(50, 50), 0);
    root_window()->DispatchMouseEvent(&move_mouse_event);
    EXPECT_EQ("MOUSE_ENTERED MOUSE_MOVED",
              EventTypesToString(filter->events()));
    filter->events().clear();
  }

  // Touch window2.
  {
    TouchEvent touch_pressed_event(ui::ET_TOUCH_PRESSED, gfx::Point(151, 151),
                                   1, base::TimeDelta());
    int time_ms =
        static_cast<int>(ui::GestureConfiguration::
                         min_touch_down_duration_in_seconds_for_click() * 1000);
    TouchEvent touch_released_event(
        ui::ET_TOUCH_RELEASED, gfx::Point(151, 151), 1,
        base::TimeDelta::FromMilliseconds(time_ms));
    root_window()->DispatchTouchEvent(&touch_pressed_event);
    root_window()->DispatchTouchEvent(&touch_released_event);
    EXPECT_EQ("TOUCH_PRESSED GESTURE_TAP_DOWN TOUCH_RELEASED GESTURE_TAP "
              "MOUSE_EXITED MOUSE_ENTERED MOUSE_MOVED MOUSE_PRESSED "
              "MOUSE_RELEASED",
              EventTypesToString(filter->events()));
    filter->events().clear();
  }
}

}  // namespace aura