Home | History | Annotate | Download | only in host
      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.
      4 
      5 #include "ash/host/ash_window_tree_host_x11.h"
      6 
      7 #undef None
      8 #undef Bool
      9 
     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"
     20 
     21 namespace {
     22 
     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);
     31   }
     32   virtual ~RootWindowEventHandler() { target_->RemovePreTargetHandler(this); }
     33 
     34   // ui::EventHandler:
     35   virtual 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();
     39   }
     40 
     41   ui::EventType last_touch_type() { return last_touch_type_; }
     42 
     43   int last_touch_id() { return last_touch_id_; }
     44 
     45   gfx::Point last_touch_location() { return last_touch_location_; }
     46 
     47  private:
     48   ui::EventTarget* target_;
     49   ui::EventType last_touch_type_;
     50   int last_touch_id_;
     51   gfx::Point last_touch_location_;
     52 
     53   DISALLOW_COPY_AND_ASSIGN(RootWindowEventHandler);
     54 };
     55 
     56 }  // namespace
     57 
     58 namespace ash {
     59 
     60 class AshWindowTreeHostX11Test : public aura::test::AuraTestBase {
     61  public:
     62   virtual void SetUp() OVERRIDE {
     63     aura::test::AuraTestBase::SetUp();
     64 
     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
     70   }
     71 
     72   virtual void TearDown() OVERRIDE {
     73     aura::test::AuraTestBase::TearDown();
     74 
     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
     82   }
     83 };
     84 
     85 // Send X touch events to one WindowTreeHost. The WindowTreeHost's
     86 // delegate will get corresponding ui::TouchEvent if the touch events
     87 // are targeting this WindowTreeHost.
     88 TEST_F(AshWindowTreeHostX11Test, DispatchTouchEventToOneRootWindow) {
     89   scoped_ptr<aura::WindowTreeHostX11> window_tree_host(
     90       new AshWindowTreeHostX11(gfx::Rect(0, 0, 2560, 1700)));
     91   window_tree_host->InitHost();
     92   scoped_ptr<RootWindowEventHandler> handler(
     93       new RootWindowEventHandler(window_tree_host.get()));
     94 
     95   std::vector<unsigned int> devices;
     96   devices.push_back(0);
     97   ui::SetUpTouchDevicesForTest(devices);
     98   std::vector<ui::Valuator> valuators;
     99 
    100   EXPECT_EQ(ui::ET_UNKNOWN, handler->last_touch_type());
    101   EXPECT_EQ(-1, handler->last_touch_id());
    102 
    103   ui::ScopedXI2Event scoped_xevent;
    104   // This touch is out of bounds.
    105   scoped_xevent.InitTouchEvent(
    106       0, XI_TouchBegin, 5, gfx::Point(1500, 2500), valuators);
    107   if (window_tree_host->CanDispatchEvent(scoped_xevent))
    108     window_tree_host->DispatchEvent(scoped_xevent);
    109   EXPECT_EQ(ui::ET_UNKNOWN, handler->last_touch_type());
    110   EXPECT_EQ(-1, handler->last_touch_id());
    111   EXPECT_EQ(gfx::Point(0, 0), handler->last_touch_location());
    112 
    113   // Following touchs are within bounds and are passed to delegate.
    114   scoped_xevent.InitTouchEvent(
    115       0, XI_TouchBegin, 5, gfx::Point(1500, 1500), valuators);
    116   if (window_tree_host->CanDispatchEvent(scoped_xevent))
    117     window_tree_host->DispatchEvent(scoped_xevent);
    118   EXPECT_EQ(ui::ET_TOUCH_PRESSED, handler->last_touch_type());
    119   EXPECT_EQ(0, handler->last_touch_id());
    120   EXPECT_EQ(gfx::Point(1500, 1500), handler->last_touch_location());
    121 
    122   scoped_xevent.InitTouchEvent(
    123       0, XI_TouchUpdate, 5, gfx::Point(1500, 1600), valuators);
    124   if (window_tree_host->CanDispatchEvent(scoped_xevent))
    125     window_tree_host->DispatchEvent(scoped_xevent);
    126   EXPECT_EQ(ui::ET_TOUCH_MOVED, handler->last_touch_type());
    127   EXPECT_EQ(0, handler->last_touch_id());
    128   EXPECT_EQ(gfx::Point(1500, 1600), handler->last_touch_location());
    129 
    130   scoped_xevent.InitTouchEvent(
    131       0, XI_TouchEnd, 5, gfx::Point(1500, 1600), valuators);
    132   if (window_tree_host->CanDispatchEvent(scoped_xevent))
    133     window_tree_host->DispatchEvent(scoped_xevent);
    134   EXPECT_EQ(ui::ET_TOUCH_RELEASED, handler->last_touch_type());
    135   EXPECT_EQ(0, handler->last_touch_id());
    136   EXPECT_EQ(gfx::Point(1500, 1600), handler->last_touch_location());
    137 
    138   handler.reset();
    139 }
    140 
    141 // Send X touch events to two WindowTreeHost. The WindowTreeHost which is
    142 // the event target of the X touch events should generate the corresponding
    143 // ui::TouchEvent for its delegate.
    144 TEST_F(AshWindowTreeHostX11Test, DispatchTouchEventToTwoRootWindow) {
    145   scoped_ptr<aura::WindowTreeHostX11> window_tree_host1(
    146       new AshWindowTreeHostX11(gfx::Rect(0, 0, 2560, 1700)));
    147   window_tree_host1->InitHost();
    148   scoped_ptr<RootWindowEventHandler> handler1(
    149       new RootWindowEventHandler(window_tree_host1.get()));
    150 
    151   int host2_y_offset = 1700;
    152   scoped_ptr<aura::WindowTreeHostX11> window_tree_host2(
    153       new AshWindowTreeHostX11(gfx::Rect(0, host2_y_offset, 1920, 1080)));
    154   window_tree_host2->InitHost();
    155   scoped_ptr<RootWindowEventHandler> handler2(
    156       new RootWindowEventHandler(window_tree_host2.get()));
    157 
    158   std::vector<unsigned int> devices;
    159   devices.push_back(0);
    160   ui::SetUpTouchDevicesForTest(devices);
    161   std::vector<ui::Valuator> valuators;
    162 
    163   EXPECT_EQ(ui::ET_UNKNOWN, handler1->last_touch_type());
    164   EXPECT_EQ(-1, handler1->last_touch_id());
    165   EXPECT_EQ(ui::ET_UNKNOWN, handler2->last_touch_type());
    166   EXPECT_EQ(-1, handler2->last_touch_id());
    167 
    168   // 2 Touch events are targeted at the second WindowTreeHost.
    169   ui::ScopedXI2Event scoped_xevent;
    170   scoped_xevent.InitTouchEvent(
    171       0, XI_TouchBegin, 5, gfx::Point(1500, 2500), valuators);
    172   if (window_tree_host1->CanDispatchEvent(scoped_xevent))
    173     window_tree_host1->DispatchEvent(scoped_xevent);
    174   if (window_tree_host2->CanDispatchEvent(scoped_xevent))
    175     window_tree_host2->DispatchEvent(scoped_xevent);
    176   EXPECT_EQ(ui::ET_UNKNOWN, handler1->last_touch_type());
    177   EXPECT_EQ(-1, handler1->last_touch_id());
    178   EXPECT_EQ(gfx::Point(0, 0), handler1->last_touch_location());
    179   EXPECT_EQ(ui::ET_TOUCH_PRESSED, handler2->last_touch_type());
    180   EXPECT_EQ(0, handler2->last_touch_id());
    181   EXPECT_EQ(gfx::Point(1500, 2500), handler2->last_touch_location());
    182 
    183   scoped_xevent.InitTouchEvent(
    184       0, XI_TouchBegin, 6, gfx::Point(1600, 2600), valuators);
    185   if (window_tree_host1->CanDispatchEvent(scoped_xevent))
    186     window_tree_host1->DispatchEvent(scoped_xevent);
    187   if (window_tree_host2->CanDispatchEvent(scoped_xevent))
    188     window_tree_host2->DispatchEvent(scoped_xevent);
    189   EXPECT_EQ(ui::ET_UNKNOWN, handler1->last_touch_type());
    190   EXPECT_EQ(-1, handler1->last_touch_id());
    191   EXPECT_EQ(gfx::Point(0, 0), handler1->last_touch_location());
    192   EXPECT_EQ(ui::ET_TOUCH_PRESSED, handler2->last_touch_type());
    193   EXPECT_EQ(1, handler2->last_touch_id());
    194   EXPECT_EQ(gfx::Point(1600, 2600), handler2->last_touch_location());
    195 
    196   scoped_xevent.InitTouchEvent(
    197       0, XI_TouchUpdate, 5, gfx::Point(1500, 2550), valuators);
    198   if (window_tree_host1->CanDispatchEvent(scoped_xevent))
    199     window_tree_host1->DispatchEvent(scoped_xevent);
    200   if (window_tree_host2->CanDispatchEvent(scoped_xevent))
    201     window_tree_host2->DispatchEvent(scoped_xevent);
    202   EXPECT_EQ(ui::ET_UNKNOWN, handler1->last_touch_type());
    203   EXPECT_EQ(-1, handler1->last_touch_id());
    204   EXPECT_EQ(gfx::Point(0, 0), handler1->last_touch_location());
    205   EXPECT_EQ(ui::ET_TOUCH_MOVED, handler2->last_touch_type());
    206   EXPECT_EQ(0, handler2->last_touch_id());
    207   EXPECT_EQ(gfx::Point(1500, 2550), handler2->last_touch_location());
    208 
    209   scoped_xevent.InitTouchEvent(
    210       0, XI_TouchUpdate, 6, gfx::Point(1600, 2650), valuators);
    211   if (window_tree_host1->CanDispatchEvent(scoped_xevent))
    212     window_tree_host1->DispatchEvent(scoped_xevent);
    213   if (window_tree_host2->CanDispatchEvent(scoped_xevent))
    214     window_tree_host2->DispatchEvent(scoped_xevent);
    215   EXPECT_EQ(ui::ET_UNKNOWN, handler1->last_touch_type());
    216   EXPECT_EQ(-1, handler1->last_touch_id());
    217   EXPECT_EQ(gfx::Point(0, 0), handler1->last_touch_location());
    218   EXPECT_EQ(ui::ET_TOUCH_MOVED, handler2->last_touch_type());
    219   EXPECT_EQ(1, handler2->last_touch_id());
    220   EXPECT_EQ(gfx::Point(1600, 2650), handler2->last_touch_location());
    221 
    222   scoped_xevent.InitTouchEvent(
    223       0, XI_TouchEnd, 5, gfx::Point(1500, 2550), valuators);
    224   if (window_tree_host1->CanDispatchEvent(scoped_xevent))
    225     window_tree_host1->DispatchEvent(scoped_xevent);
    226   if (window_tree_host2->CanDispatchEvent(scoped_xevent))
    227     window_tree_host2->DispatchEvent(scoped_xevent);
    228   EXPECT_EQ(ui::ET_UNKNOWN, handler1->last_touch_type());
    229   EXPECT_EQ(-1, handler1->last_touch_id());
    230   EXPECT_EQ(gfx::Point(0, 0), handler1->last_touch_location());
    231   EXPECT_EQ(ui::ET_TOUCH_RELEASED, handler2->last_touch_type());
    232   EXPECT_EQ(0, handler2->last_touch_id());
    233   EXPECT_EQ(gfx::Point(1500, 2550), handler2->last_touch_location());
    234 
    235   scoped_xevent.InitTouchEvent(
    236       0, XI_TouchEnd, 6, gfx::Point(1600, 2650), valuators);
    237   if (window_tree_host1->CanDispatchEvent(scoped_xevent))
    238     window_tree_host1->DispatchEvent(scoped_xevent);
    239   if (window_tree_host2->CanDispatchEvent(scoped_xevent))
    240     window_tree_host2->DispatchEvent(scoped_xevent);
    241   EXPECT_EQ(ui::ET_UNKNOWN, handler1->last_touch_type());
    242   EXPECT_EQ(-1, handler1->last_touch_id());
    243   EXPECT_EQ(gfx::Point(0, 0), handler1->last_touch_location());
    244   EXPECT_EQ(ui::ET_TOUCH_RELEASED, handler2->last_touch_type());
    245   EXPECT_EQ(1, handler2->last_touch_id());
    246   EXPECT_EQ(gfx::Point(1600, 2650), handler2->last_touch_location());
    247 
    248   handler1.reset();
    249   handler2.reset();
    250 }
    251 
    252 }  // namespace aura
    253