Home | History | Annotate | Download | only in aura
      1 // Copyright (c) 2012 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 #include "ui/aura/root_window.h"
      6 
      7 #include <vector>
      8 
      9 #include "testing/gtest/include/gtest/gtest.h"
     10 #include "ui/aura/client/event_client.h"
     11 #include "ui/aura/env.h"
     12 #include "ui/aura/focus_manager.h"
     13 #include "ui/aura/test/aura_test_base.h"
     14 #include "ui/aura/test/event_generator.h"
     15 #include "ui/aura/test/test_cursor_client.h"
     16 #include "ui/aura/test/test_event_handler.h"
     17 #include "ui/aura/test/test_window_delegate.h"
     18 #include "ui/aura/test/test_windows.h"
     19 #include "ui/aura/window_tracker.h"
     20 #include "ui/base/events/event.h"
     21 #include "ui/base/events/event_handler.h"
     22 #include "ui/base/events/event_utils.h"
     23 #include "ui/base/gestures/gesture_configuration.h"
     24 #include "ui/base/hit_test.h"
     25 #include "ui/base/keycodes/keyboard_codes.h"
     26 #include "ui/gfx/point.h"
     27 #include "ui/gfx/rect.h"
     28 #include "ui/gfx/screen.h"
     29 #include "ui/gfx/transform.h"
     30 
     31 namespace aura {
     32 namespace {
     33 
     34 // A delegate that always returns a non-client component for hit tests.
     35 class NonClientDelegate : public test::TestWindowDelegate {
     36  public:
     37   NonClientDelegate()
     38       : non_client_count_(0),
     39         mouse_event_count_(0),
     40         mouse_event_flags_(0x0) {
     41   }
     42   virtual ~NonClientDelegate() {}
     43 
     44   int non_client_count() const { return non_client_count_; }
     45   gfx::Point non_client_location() const { return non_client_location_; }
     46   int mouse_event_count() const { return mouse_event_count_; }
     47   gfx::Point mouse_event_location() const { return mouse_event_location_; }
     48   int mouse_event_flags() const { return mouse_event_flags_; }
     49 
     50   virtual int GetNonClientComponent(const gfx::Point& location) const OVERRIDE {
     51     NonClientDelegate* self = const_cast<NonClientDelegate*>(this);
     52     self->non_client_count_++;
     53     self->non_client_location_ = location;
     54     return HTTOPLEFT;
     55   }
     56   virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
     57     mouse_event_count_++;
     58     mouse_event_location_ = event->location();
     59     mouse_event_flags_ = event->flags();
     60     event->SetHandled();
     61   }
     62 
     63  private:
     64   int non_client_count_;
     65   gfx::Point non_client_location_;
     66   int mouse_event_count_;
     67   gfx::Point mouse_event_location_;
     68   int mouse_event_flags_;
     69 
     70   DISALLOW_COPY_AND_ASSIGN(NonClientDelegate);
     71 };
     72 
     73 // A simple event handler that consumes key events.
     74 class ConsumeKeyHandler : public test::TestEventHandler {
     75  public:
     76   ConsumeKeyHandler() {}
     77   virtual ~ConsumeKeyHandler() {}
     78 
     79   // Overridden from ui::EventHandler:
     80   virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE {
     81     test::TestEventHandler::OnKeyEvent(event);
     82     event->StopPropagation();
     83   }
     84 
     85  private:
     86   DISALLOW_COPY_AND_ASSIGN(ConsumeKeyHandler);
     87 };
     88 
     89 bool IsFocusedWindow(aura::Window* window) {
     90   return client::GetFocusClient(window)->GetFocusedWindow() == window;
     91 }
     92 
     93 }  // namespace
     94 
     95 typedef test::AuraTestBase RootWindowTest;
     96 
     97 TEST_F(RootWindowTest, OnHostMouseEvent) {
     98   // Create two non-overlapping windows so we don't have to worry about which
     99   // is on top.
    100   scoped_ptr<NonClientDelegate> delegate1(new NonClientDelegate());
    101   scoped_ptr<NonClientDelegate> delegate2(new NonClientDelegate());
    102   const int kWindowWidth = 123;
    103   const int kWindowHeight = 45;
    104   gfx::Rect bounds1(100, 200, kWindowWidth, kWindowHeight);
    105   gfx::Rect bounds2(300, 400, kWindowWidth, kWindowHeight);
    106   scoped_ptr<aura::Window> window1(CreateTestWindowWithDelegate(
    107       delegate1.get(), -1234, bounds1, root_window()));
    108   scoped_ptr<aura::Window> window2(CreateTestWindowWithDelegate(
    109       delegate2.get(), -5678, bounds2, root_window()));
    110 
    111   // Send a mouse event to window1.
    112   gfx::Point point(101, 201);
    113   ui::MouseEvent event1(
    114       ui::ET_MOUSE_PRESSED, point, point, ui::EF_LEFT_MOUSE_BUTTON);
    115   root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(&event1);
    116 
    117   // Event was tested for non-client area for the target window.
    118   EXPECT_EQ(1, delegate1->non_client_count());
    119   EXPECT_EQ(0, delegate2->non_client_count());
    120   // The non-client component test was in local coordinates.
    121   EXPECT_EQ(gfx::Point(1, 1), delegate1->non_client_location());
    122   // Mouse event was received by target window.
    123   EXPECT_EQ(1, delegate1->mouse_event_count());
    124   EXPECT_EQ(0, delegate2->mouse_event_count());
    125   // Event was in local coordinates.
    126   EXPECT_EQ(gfx::Point(1, 1), delegate1->mouse_event_location());
    127   // Non-client flag was set.
    128   EXPECT_TRUE(delegate1->mouse_event_flags() & ui::EF_IS_NON_CLIENT);
    129 }
    130 
    131 TEST_F(RootWindowTest, RepostEvent) {
    132   // Test RepostEvent in RootWindow. It only works for Mouse Press.
    133   EXPECT_FALSE(Env::GetInstance()->is_mouse_button_down());
    134   gfx::Point point(10, 10);
    135   ui::MouseEvent event(
    136       ui::ET_MOUSE_PRESSED, point, point, ui::EF_LEFT_MOUSE_BUTTON);
    137   root_window()->RepostEvent(event);
    138   RunAllPendingInMessageLoop();
    139   EXPECT_TRUE(Env::GetInstance()->is_mouse_button_down());
    140 }
    141 
    142 // Check that we correctly track the state of the mouse buttons in response to
    143 // button press and release events.
    144 TEST_F(RootWindowTest, MouseButtonState) {
    145   EXPECT_FALSE(Env::GetInstance()->is_mouse_button_down());
    146 
    147   gfx::Point location;
    148   scoped_ptr<ui::MouseEvent> event;
    149 
    150   // Press the left button.
    151   event.reset(new ui::MouseEvent(
    152       ui::ET_MOUSE_PRESSED,
    153       location,
    154       location,
    155       ui::EF_LEFT_MOUSE_BUTTON));
    156   root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(event.get());
    157   EXPECT_TRUE(Env::GetInstance()->is_mouse_button_down());
    158 
    159   // Additionally press the right.
    160   event.reset(new ui::MouseEvent(
    161       ui::ET_MOUSE_PRESSED,
    162       location,
    163       location,
    164       ui::EF_LEFT_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON));
    165   root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(event.get());
    166   EXPECT_TRUE(Env::GetInstance()->is_mouse_button_down());
    167 
    168   // Release the left button.
    169   event.reset(new ui::MouseEvent(
    170       ui::ET_MOUSE_RELEASED,
    171       location,
    172       location,
    173       ui::EF_RIGHT_MOUSE_BUTTON));
    174   root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(event.get());
    175   EXPECT_TRUE(Env::GetInstance()->is_mouse_button_down());
    176 
    177   // Release the right button.  We should ignore the Shift-is-down flag.
    178   event.reset(new ui::MouseEvent(
    179       ui::ET_MOUSE_RELEASED,
    180       location,
    181       location,
    182       ui::EF_SHIFT_DOWN));
    183   root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(event.get());
    184   EXPECT_FALSE(Env::GetInstance()->is_mouse_button_down());
    185 
    186   // Press the middle button.
    187   event.reset(new ui::MouseEvent(
    188       ui::ET_MOUSE_PRESSED,
    189       location,
    190       location,
    191       ui::EF_MIDDLE_MOUSE_BUTTON));
    192   root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(event.get());
    193   EXPECT_TRUE(Env::GetInstance()->is_mouse_button_down());
    194 }
    195 
    196 TEST_F(RootWindowTest, TranslatedEvent) {
    197   scoped_ptr<Window> w1(test::CreateTestWindowWithDelegate(NULL, 1,
    198       gfx::Rect(50, 50, 100, 100), root_window()));
    199 
    200   gfx::Point origin(100, 100);
    201   ui::MouseEvent root(ui::ET_MOUSE_PRESSED, origin, origin, 0);
    202 
    203   EXPECT_EQ("100,100", root.location().ToString());
    204   EXPECT_EQ("100,100", root.root_location().ToString());
    205 
    206   ui::MouseEvent translated_event(
    207       root, static_cast<Window*>(root_window()), w1.get(),
    208       ui::ET_MOUSE_ENTERED, root.flags());
    209   EXPECT_EQ("50,50", translated_event.location().ToString());
    210   EXPECT_EQ("100,100", translated_event.root_location().ToString());
    211 }
    212 
    213 namespace {
    214 
    215 class TestEventClient : public client::EventClient {
    216  public:
    217   static const int kNonLockWindowId = 100;
    218   static const int kLockWindowId = 200;
    219 
    220   explicit TestEventClient(RootWindow* root_window)
    221       : root_window_(root_window),
    222         lock_(false) {
    223     client::SetEventClient(root_window_, this);
    224     Window* lock_window =
    225         test::CreateTestWindowWithBounds(root_window_->bounds(), root_window_);
    226     lock_window->set_id(kLockWindowId);
    227     Window* non_lock_window =
    228         test::CreateTestWindowWithBounds(root_window_->bounds(), root_window_);
    229     non_lock_window->set_id(kNonLockWindowId);
    230   }
    231   virtual ~TestEventClient() {
    232     client::SetEventClient(root_window_, NULL);
    233   }
    234 
    235   // Starts/stops locking. Locking prevents windows other than those inside
    236   // the lock container from receiving events, getting focus etc.
    237   void Lock() {
    238     lock_ = true;
    239   }
    240   void Unlock() {
    241     lock_ = false;
    242   }
    243 
    244   Window* GetLockWindow() {
    245     return const_cast<Window*>(
    246         static_cast<const TestEventClient*>(this)->GetLockWindow());
    247   }
    248   const Window* GetLockWindow() const {
    249     return root_window_->GetChildById(kLockWindowId);
    250   }
    251   Window* GetNonLockWindow() {
    252     return root_window_->GetChildById(kNonLockWindowId);
    253   }
    254 
    255  private:
    256   // Overridden from client::EventClient:
    257   virtual bool CanProcessEventsWithinSubtree(
    258       const Window* window) const OVERRIDE {
    259     return lock_ ?
    260         window->Contains(GetLockWindow()) || GetLockWindow()->Contains(window) :
    261         true;
    262   }
    263 
    264   virtual ui::EventTarget* GetToplevelEventTarget() OVERRIDE {
    265     return NULL;
    266   }
    267 
    268   RootWindow* root_window_;
    269   bool lock_;
    270 
    271   DISALLOW_COPY_AND_ASSIGN(TestEventClient);
    272 };
    273 
    274 }  // namespace
    275 
    276 TEST_F(RootWindowTest, CanProcessEventsWithinSubtree) {
    277   TestEventClient client(root_window());
    278   test::TestWindowDelegate d;
    279 
    280   test::TestEventHandler* nonlock_ef = new test::TestEventHandler;
    281   test::TestEventHandler* lock_ef = new test::TestEventHandler;
    282   client.GetNonLockWindow()->SetEventFilter(nonlock_ef);
    283   client.GetLockWindow()->SetEventFilter(lock_ef);
    284 
    285   Window* w1 = test::CreateTestWindowWithBounds(gfx::Rect(10, 10, 20, 20),
    286                                                 client.GetNonLockWindow());
    287   w1->set_id(1);
    288   Window* w2 = test::CreateTestWindowWithBounds(gfx::Rect(30, 30, 20, 20),
    289                                                 client.GetNonLockWindow());
    290   w2->set_id(2);
    291   scoped_ptr<Window> w3(
    292       test::CreateTestWindowWithDelegate(&d, 3, gfx::Rect(30, 30, 20, 20),
    293                                          client.GetLockWindow()));
    294 
    295   w1->Focus();
    296   EXPECT_TRUE(IsFocusedWindow(w1));
    297 
    298   client.Lock();
    299 
    300   // Since we're locked, the attempt to focus w2 will be ignored.
    301   w2->Focus();
    302   EXPECT_TRUE(IsFocusedWindow(w1));
    303   EXPECT_FALSE(IsFocusedWindow(w2));
    304 
    305   {
    306     // Attempting to send a key event to w1 (not in the lock container) should
    307     // cause focus to be reset.
    308     test::EventGenerator generator(root_window());
    309     generator.PressKey(ui::VKEY_SPACE, 0);
    310     EXPECT_EQ(NULL, client::GetFocusClient(w1)->GetFocusedWindow());
    311   }
    312 
    313   {
    314     // Events sent to a window not in the lock container will not be processed.
    315     // i.e. never sent to the non-lock container's event filter.
    316     test::EventGenerator generator(root_window(), w1);
    317     generator.PressLeftButton();
    318     EXPECT_EQ(0, nonlock_ef->num_mouse_events());
    319 
    320     // Events sent to a window in the lock container will be processed.
    321     test::EventGenerator generator3(root_window(), w3.get());
    322     generator3.PressLeftButton();
    323     EXPECT_EQ(1, lock_ef->num_mouse_events());
    324   }
    325 
    326   // Prevent w3 from being deleted by the hierarchy since its delegate is owned
    327   // by this scope.
    328   w3->parent()->RemoveChild(w3.get());
    329 }
    330 
    331 TEST_F(RootWindowTest, IgnoreUnknownKeys) {
    332   test::TestEventHandler* filter = new ConsumeKeyHandler;
    333   root_window()->SetEventFilter(filter);  // passes ownership
    334 
    335   ui::KeyEvent unknown_event(ui::ET_KEY_PRESSED, ui::VKEY_UNKNOWN, 0, false);
    336   EXPECT_FALSE(root_window()->AsRootWindowHostDelegate()->OnHostKeyEvent(
    337       &unknown_event));
    338   EXPECT_EQ(0, filter->num_key_events());
    339 
    340   ui::KeyEvent known_event(ui::ET_KEY_PRESSED, ui::VKEY_A, 0, false);
    341   EXPECT_TRUE(root_window()->AsRootWindowHostDelegate()->OnHostKeyEvent(
    342       &known_event));
    343   EXPECT_EQ(1, filter->num_key_events());
    344 }
    345 
    346 // Tests that touch-events that are beyond the bounds of the root-window do get
    347 // propagated to the event filters correctly with the root as the target.
    348 TEST_F(RootWindowTest, TouchEventsOutsideBounds) {
    349   test::TestEventHandler* filter = new test::TestEventHandler;
    350   root_window()->SetEventFilter(filter);  // passes ownership
    351 
    352   gfx::Point position = root_window()->bounds().origin();
    353   position.Offset(-10, -10);
    354   ui::TouchEvent press(ui::ET_TOUCH_PRESSED, position, 0, base::TimeDelta());
    355   root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&press);
    356   EXPECT_EQ(1, filter->num_touch_events());
    357 
    358   position = root_window()->bounds().origin();
    359   position.Offset(root_window()->bounds().width() + 10,
    360                   root_window()->bounds().height() + 10);
    361   ui::TouchEvent release(ui::ET_TOUCH_RELEASED, position, 0, base::TimeDelta());
    362   root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(&release);
    363   EXPECT_EQ(2, filter->num_touch_events());
    364 }
    365 
    366 // Tests that scroll events are dispatched correctly.
    367 TEST_F(RootWindowTest, ScrollEventDispatch) {
    368   base::TimeDelta now = ui::EventTimeForNow();
    369   test::TestEventHandler* filter = new test::TestEventHandler;
    370   root_window()->SetEventFilter(filter);
    371 
    372   test::TestWindowDelegate delegate;
    373   scoped_ptr<Window> w1(CreateNormalWindow(1, root_window(), &delegate));
    374   w1->SetBounds(gfx::Rect(20, 20, 40, 40));
    375 
    376   // A scroll event on the root-window itself is dispatched.
    377   ui::ScrollEvent scroll1(ui::ET_SCROLL,
    378                           gfx::Point(10, 10),
    379                           now,
    380                           0,
    381                           0, -10,
    382                           0, -10,
    383                           2);
    384   root_window()->AsRootWindowHostDelegate()->OnHostScrollEvent(&scroll1);
    385   EXPECT_EQ(1, filter->num_scroll_events());
    386 
    387   // Scroll event on a window should be dispatched properly.
    388   ui::ScrollEvent scroll2(ui::ET_SCROLL,
    389                           gfx::Point(25, 30),
    390                           now,
    391                           0,
    392                           -10, 0,
    393                           -10, 0,
    394                           2);
    395   root_window()->AsRootWindowHostDelegate()->OnHostScrollEvent(&scroll2);
    396   EXPECT_EQ(2, filter->num_scroll_events());
    397 }
    398 
    399 namespace {
    400 
    401 // FilterFilter that tracks the types of events it's seen.
    402 class EventFilterRecorder : public ui::EventHandler {
    403  public:
    404   typedef std::vector<ui::EventType> Events;
    405 
    406   EventFilterRecorder() {}
    407 
    408   Events& events() { return events_; }
    409 
    410   // ui::EventHandler overrides:
    411   virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE {
    412     events_.push_back(event->type());
    413   }
    414 
    415   virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
    416     events_.push_back(event->type());
    417   }
    418 
    419   virtual void OnScrollEvent(ui::ScrollEvent* event) OVERRIDE {
    420     events_.push_back(event->type());
    421   }
    422 
    423   virtual void OnTouchEvent(ui::TouchEvent* event) OVERRIDE {
    424     events_.push_back(event->type());
    425   }
    426 
    427   virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE {
    428     events_.push_back(event->type());
    429   }
    430 
    431  private:
    432   Events events_;
    433 
    434   DISALLOW_COPY_AND_ASSIGN(EventFilterRecorder);
    435 };
    436 
    437 // Converts an EventType to a string.
    438 std::string EventTypeToString(ui::EventType type) {
    439   switch (type) {
    440     case ui::ET_TOUCH_RELEASED:
    441       return "TOUCH_RELEASED";
    442 
    443     case ui::ET_TOUCH_PRESSED:
    444       return "TOUCH_PRESSED";
    445 
    446     case ui::ET_TOUCH_MOVED:
    447       return "TOUCH_MOVED";
    448 
    449     case ui::ET_MOUSE_PRESSED:
    450       return "MOUSE_PRESSED";
    451 
    452     case ui::ET_MOUSE_DRAGGED:
    453       return "MOUSE_DRAGGED";
    454 
    455     case ui::ET_MOUSE_RELEASED:
    456       return "MOUSE_RELEASED";
    457 
    458     case ui::ET_MOUSE_MOVED:
    459       return "MOUSE_MOVED";
    460 
    461     case ui::ET_MOUSE_ENTERED:
    462       return "MOUSE_ENTERED";
    463 
    464     case ui::ET_MOUSE_EXITED:
    465       return "MOUSE_EXITED";
    466 
    467     case ui::ET_GESTURE_SCROLL_BEGIN:
    468       return "GESTURE_SCROLL_BEGIN";
    469 
    470     case ui::ET_GESTURE_SCROLL_END:
    471       return "GESTURE_SCROLL_END";
    472 
    473     case ui::ET_GESTURE_SCROLL_UPDATE:
    474       return "GESTURE_SCROLL_UPDATE";
    475 
    476     case ui::ET_GESTURE_TAP:
    477       return "GESTURE_TAP";
    478 
    479     case ui::ET_GESTURE_TAP_DOWN:
    480       return "GESTURE_TAP_DOWN";
    481 
    482     case ui::ET_GESTURE_BEGIN:
    483       return "GESTURE_BEGIN";
    484 
    485     case ui::ET_GESTURE_END:
    486       return "GESTURE_END";
    487 
    488     default:
    489       break;
    490   }
    491   return "";
    492 }
    493 
    494 std::string EventTypesToString(const EventFilterRecorder::Events& events) {
    495   std::string result;
    496   for (size_t i = 0; i < events.size(); ++i) {
    497     if (i != 0)
    498       result += " ";
    499     result += EventTypeToString(events[i]);
    500   }
    501   return result;
    502 }
    503 
    504 }  // namespace
    505 
    506 TEST_F(RootWindowTest, MouseMovesHeld) {
    507   EventFilterRecorder* filter = new EventFilterRecorder;
    508   root_window()->SetEventFilter(filter);  // passes ownership
    509 
    510   test::TestWindowDelegate delegate;
    511   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
    512       &delegate, 1, gfx::Rect(0, 0, 100, 100), root_window()));
    513 
    514   ui::MouseEvent mouse_move_event(ui::ET_MOUSE_MOVED, gfx::Point(0, 0),
    515                                   gfx::Point(0, 0), 0);
    516   root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(
    517       &mouse_move_event);
    518   // Discard MOUSE_ENTER.
    519   filter->events().clear();
    520 
    521   root_window()->HoldPointerMoves();
    522 
    523   // Check that we don't immediately dispatch the MOUSE_DRAGGED event.
    524   ui::MouseEvent mouse_dragged_event(ui::ET_MOUSE_DRAGGED, gfx::Point(0, 0),
    525                                      gfx::Point(0, 0), 0);
    526   root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(
    527       &mouse_dragged_event);
    528   EXPECT_TRUE(filter->events().empty());
    529 
    530   // Check that we do dispatch the held MOUSE_DRAGGED event before another type
    531   // of event.
    532   ui::MouseEvent mouse_pressed_event(ui::ET_MOUSE_PRESSED, gfx::Point(0, 0),
    533                                      gfx::Point(0, 0), 0);
    534   root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(
    535       &mouse_pressed_event);
    536   EXPECT_EQ("MOUSE_DRAGGED MOUSE_PRESSED",
    537             EventTypesToString(filter->events()));
    538   filter->events().clear();
    539 
    540   // Check that we coalesce held MOUSE_DRAGGED events.
    541   ui::MouseEvent mouse_dragged_event2(ui::ET_MOUSE_DRAGGED, gfx::Point(1, 1),
    542                                       gfx::Point(1, 1), 0);
    543   root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(
    544       &mouse_dragged_event);
    545   root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(
    546       &mouse_dragged_event2);
    547   EXPECT_TRUE(filter->events().empty());
    548   root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(
    549       &mouse_pressed_event);
    550   EXPECT_EQ("MOUSE_DRAGGED MOUSE_PRESSED",
    551             EventTypesToString(filter->events()));
    552   filter->events().clear();
    553 
    554   // Check that on ReleasePointerMoves, held events are not dispatched
    555   // immediately, but posted instead.
    556   root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(
    557       &mouse_dragged_event);
    558   root_window()->ReleasePointerMoves();
    559   EXPECT_TRUE(filter->events().empty());
    560   RunAllPendingInMessageLoop();
    561   EXPECT_EQ("MOUSE_DRAGGED", EventTypesToString(filter->events()));
    562   filter->events().clear();
    563 
    564   // However if another message comes in before the dispatch,
    565   // the Check that on ReleasePointerMoves, held events are not dispatched
    566   // immediately, but posted instead.
    567   root_window()->HoldPointerMoves();
    568   root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(
    569       &mouse_dragged_event);
    570   root_window()->ReleasePointerMoves();
    571   root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(
    572       &mouse_pressed_event);
    573   EXPECT_EQ("MOUSE_DRAGGED MOUSE_PRESSED",
    574             EventTypesToString(filter->events()));
    575   filter->events().clear();
    576   RunAllPendingInMessageLoop();
    577   EXPECT_TRUE(filter->events().empty());
    578 
    579   // Check that if the other message is another MOUSE_DRAGGED, we still coalesce
    580   // them.
    581   root_window()->HoldPointerMoves();
    582   root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(
    583       &mouse_dragged_event);
    584   root_window()->ReleasePointerMoves();
    585   root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(
    586       &mouse_dragged_event2);
    587   EXPECT_EQ("MOUSE_DRAGGED", EventTypesToString(filter->events()));
    588   filter->events().clear();
    589   RunAllPendingInMessageLoop();
    590   EXPECT_TRUE(filter->events().empty());
    591 }
    592 
    593 TEST_F(RootWindowTest, TouchMovesHeld) {
    594   EventFilterRecorder* filter = new EventFilterRecorder;
    595   root_window()->SetEventFilter(filter);  // passes ownership
    596 
    597   test::TestWindowDelegate delegate;
    598   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
    599       &delegate, 1, gfx::Rect(0, 0, 100, 100), root_window()));
    600 
    601   // Starting the touch and throwing out the first few events, since the system
    602   // is going to generate synthetic mouse events that are not relevant to the
    603   // test.
    604   ui::TouchEvent touch_pressed_event(ui::ET_TOUCH_PRESSED, gfx::Point(0, 0),
    605                                      0, base::TimeDelta());
    606   root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(
    607       &touch_pressed_event);
    608   RunAllPendingInMessageLoop();
    609   filter->events().clear();
    610 
    611   root_window()->HoldPointerMoves();
    612 
    613   // Check that we don't immediately dispatch the TOUCH_MOVED event.
    614   ui::TouchEvent touch_moved_event(ui::ET_TOUCH_MOVED, gfx::Point(0, 0),
    615                                    0, base::TimeDelta());
    616   root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(
    617       &touch_moved_event);
    618   EXPECT_TRUE(filter->events().empty());
    619 
    620   // Check that on ReleasePointerMoves, held events are not dispatched
    621   // immediately, but posted instead.
    622   root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(
    623       &touch_moved_event);
    624   root_window()->ReleasePointerMoves();
    625   EXPECT_TRUE(filter->events().empty());
    626   RunAllPendingInMessageLoop();
    627   EXPECT_EQ("TOUCH_MOVED", EventTypesToString(filter->events()));
    628   filter->events().clear();
    629 
    630   // If another touch event occurs then the held touch should be dispatched
    631   // immediately before it.
    632   ui::TouchEvent touch_released_event(ui::ET_TOUCH_RELEASED, gfx::Point(0, 0),
    633                                       0, base::TimeDelta());
    634   filter->events().clear();
    635   root_window()->HoldPointerMoves();
    636   root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(
    637       &touch_moved_event);
    638   root_window()->AsRootWindowHostDelegate()->OnHostTouchEvent(
    639       &touch_released_event);
    640   EXPECT_EQ("TOUCH_MOVED TOUCH_RELEASED  GESTURE_END",
    641             EventTypesToString(filter->events()));
    642   filter->events().clear();
    643   root_window()->ReleasePointerMoves();
    644   RunAllPendingInMessageLoop();
    645   EXPECT_TRUE(filter->events().empty());
    646 }
    647 
    648 // Tests that synthetic mouse events are ignored when mouse
    649 // events are disabled.
    650 TEST_F(RootWindowTest, DispatchSyntheticMouseEvents) {
    651   EventFilterRecorder* filter = new EventFilterRecorder;
    652   root_window()->SetEventFilter(filter);  // passes ownership
    653 
    654   test::TestWindowDelegate delegate;
    655   scoped_ptr<aura::Window> window(CreateTestWindowWithDelegate(
    656       &delegate, 1234, gfx::Rect(5, 5, 100, 100), root_window()));
    657   window->Show();
    658   window->SetCapture();
    659 
    660   test::TestCursorClient cursor_client(root_window());
    661 
    662   // Dispatch a non-synthetic mouse event when mouse events are enabled.
    663   ui::MouseEvent mouse1(ui::ET_MOUSE_MOVED, gfx::Point(10, 10),
    664                         gfx::Point(10, 10), 0);
    665   root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(&mouse1);
    666   EXPECT_FALSE(filter->events().empty());
    667   filter->events().clear();
    668 
    669   // Dispatch a synthetic mouse event when mouse events are enabled.
    670   ui::MouseEvent mouse2(ui::ET_MOUSE_MOVED, gfx::Point(10, 10),
    671                         gfx::Point(10, 10), ui::EF_IS_SYNTHESIZED);
    672   root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(&mouse2);
    673   EXPECT_FALSE(filter->events().empty());
    674   filter->events().clear();
    675 
    676   // Dispatch a synthetic mouse event when mouse events are disabled.
    677   cursor_client.DisableMouseEvents();
    678   root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(&mouse2);
    679   EXPECT_TRUE(filter->events().empty());
    680 }
    681 
    682 class DeletingEventFilter : public ui::EventHandler {
    683  public:
    684   DeletingEventFilter()
    685       : delete_during_pre_handle_(false) {}
    686   virtual ~DeletingEventFilter() {}
    687 
    688   void Reset(bool delete_during_pre_handle) {
    689     delete_during_pre_handle_ = delete_during_pre_handle;
    690   }
    691 
    692  private:
    693   // Overridden from ui::EventHandler:
    694   virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE {
    695     if (delete_during_pre_handle_)
    696       delete event->target();
    697   }
    698 
    699   virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
    700     if (delete_during_pre_handle_)
    701       delete event->target();
    702   }
    703 
    704   bool delete_during_pre_handle_;
    705 
    706   DISALLOW_COPY_AND_ASSIGN(DeletingEventFilter);
    707 };
    708 
    709 class DeletingWindowDelegate : public test::TestWindowDelegate {
    710  public:
    711   DeletingWindowDelegate()
    712       : window_(NULL),
    713         delete_during_handle_(false),
    714         got_event_(false) {}
    715   virtual ~DeletingWindowDelegate() {}
    716 
    717   void Reset(Window* window, bool delete_during_handle) {
    718     window_ = window;
    719     delete_during_handle_ = delete_during_handle;
    720     got_event_ = false;
    721   }
    722   bool got_event() const { return got_event_; }
    723 
    724  private:
    725   // Overridden from WindowDelegate:
    726   virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE {
    727     if (delete_during_handle_)
    728       delete window_;
    729     got_event_ = true;
    730   }
    731 
    732   virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
    733     if (delete_during_handle_)
    734       delete window_;
    735     got_event_ = true;
    736   }
    737 
    738   Window* window_;
    739   bool delete_during_handle_;
    740   bool got_event_;
    741 
    742   DISALLOW_COPY_AND_ASSIGN(DeletingWindowDelegate);
    743 };
    744 
    745 TEST_F(RootWindowTest, DeleteWindowDuringDispatch) {
    746   // Verifies that we can delete a window during each phase of event handling.
    747   // Deleting the window should not cause a crash, only prevent further
    748   // processing from occurring.
    749   scoped_ptr<Window> w1(CreateNormalWindow(1, root_window(), NULL));
    750   DeletingWindowDelegate d11;
    751   Window* w11 = CreateNormalWindow(11, w1.get(), &d11);
    752   WindowTracker tracker;
    753   DeletingEventFilter* w1_filter = new DeletingEventFilter;
    754   w1->SetEventFilter(w1_filter);
    755   client::GetFocusClient(w1.get())->FocusWindow(w11);
    756 
    757   test::EventGenerator generator(root_window(), w11);
    758 
    759   // First up, no one deletes anything.
    760   tracker.Add(w11);
    761   d11.Reset(w11, false);
    762 
    763   generator.PressLeftButton();
    764   EXPECT_TRUE(tracker.Contains(w11));
    765   EXPECT_TRUE(d11.got_event());
    766   generator.ReleaseLeftButton();
    767 
    768   // Delegate deletes w11. This will prevent the post-handle step from applying.
    769   w1_filter->Reset(false);
    770   d11.Reset(w11, true);
    771   generator.PressKey(ui::VKEY_A, 0);
    772   EXPECT_FALSE(tracker.Contains(w11));
    773   EXPECT_TRUE(d11.got_event());
    774 
    775   // Pre-handle step deletes w11. This will prevent the delegate and the post-
    776   // handle steps from applying.
    777   w11 = CreateNormalWindow(11, w1.get(), &d11);
    778   w1_filter->Reset(true);
    779   d11.Reset(w11, false);
    780   generator.PressLeftButton();
    781   EXPECT_FALSE(tracker.Contains(w11));
    782   EXPECT_FALSE(d11.got_event());
    783 }
    784 
    785 namespace {
    786 
    787 // A window delegate that detaches the parent of the target's parent window when
    788 // it receives a tap event.
    789 class DetachesParentOnTapDelegate : public test::TestWindowDelegate {
    790  public:
    791   DetachesParentOnTapDelegate() {}
    792   virtual ~DetachesParentOnTapDelegate() {}
    793 
    794  private:
    795   virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE {
    796     if (event->type() == ui::ET_GESTURE_TAP_DOWN) {
    797       event->SetHandled();
    798       return;
    799     }
    800 
    801     if (event->type() == ui::ET_GESTURE_TAP) {
    802       Window* parent = static_cast<Window*>(event->target())->parent();
    803       parent->parent()->RemoveChild(parent);
    804       event->SetHandled();
    805     }
    806   }
    807 
    808   DISALLOW_COPY_AND_ASSIGN(DetachesParentOnTapDelegate);
    809 };
    810 
    811 }  // namespace
    812 
    813 // Tests that the gesture recognizer is reset for all child windows when a
    814 // window hides. No expectations, just checks that the test does not crash.
    815 TEST_F(RootWindowTest, GestureRecognizerResetsTargetWhenParentHides) {
    816   scoped_ptr<Window> w1(CreateNormalWindow(1, root_window(), NULL));
    817   DetachesParentOnTapDelegate delegate;
    818   scoped_ptr<Window> parent(CreateNormalWindow(22, w1.get(), NULL));
    819   Window* child = CreateNormalWindow(11, parent.get(), &delegate);
    820   test::EventGenerator generator(root_window(), child);
    821   generator.GestureTapAt(gfx::Point(40, 40));
    822 }
    823 
    824 namespace {
    825 
    826 // A window delegate that processes nested gestures on tap.
    827 class NestedGestureDelegate : public test::TestWindowDelegate {
    828  public:
    829   NestedGestureDelegate(test::EventGenerator* generator,
    830                         const gfx::Point tap_location)
    831       : generator_(generator),
    832         tap_location_(tap_location),
    833         gesture_end_count_(0) {}
    834   virtual ~NestedGestureDelegate() {}
    835 
    836   int gesture_end_count() const { return gesture_end_count_; }
    837 
    838  private:
    839   virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE {
    840     switch (event->type()) {
    841       case ui::ET_GESTURE_TAP_DOWN:
    842         event->SetHandled();
    843         break;
    844       case ui::ET_GESTURE_TAP:
    845         if (generator_)
    846           generator_->GestureTapAt(tap_location_);
    847         event->SetHandled();
    848         break;
    849       case ui::ET_GESTURE_END:
    850         ++gesture_end_count_;
    851         break;
    852       default:
    853         break;
    854     }
    855   }
    856 
    857   test::EventGenerator* generator_;
    858   const gfx::Point tap_location_;
    859   int gesture_end_count_;
    860   DISALLOW_COPY_AND_ASSIGN(NestedGestureDelegate);
    861 };
    862 
    863 }  // namespace
    864 
    865 // Tests that gesture end is delivered after nested gesture processing.
    866 TEST_F(RootWindowTest, GestureEndDeliveredAfterNestedGestures) {
    867   NestedGestureDelegate d1(NULL, gfx::Point());
    868   scoped_ptr<Window> w1(CreateNormalWindow(1, root_window(), &d1));
    869   w1->SetBounds(gfx::Rect(0, 0, 100, 100));
    870 
    871   test::EventGenerator nested_generator(root_window(), w1.get());
    872   NestedGestureDelegate d2(&nested_generator, w1->bounds().CenterPoint());
    873   scoped_ptr<Window> w2(CreateNormalWindow(1, root_window(), &d2));
    874   w2->SetBounds(gfx::Rect(100, 0, 100, 100));
    875 
    876   // Tap on w2 which triggers nested gestures for w1.
    877   test::EventGenerator generator(root_window(), w2.get());
    878   generator.GestureTapAt(w2->bounds().CenterPoint());
    879 
    880   // Both windows should get their gesture end events.
    881   EXPECT_EQ(1, d1.gesture_end_count());
    882   EXPECT_EQ(1, d2.gesture_end_count());
    883 }
    884 
    885 }  // namespace aura
    886