Home | History | Annotate | Download | only in test
      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 #include "ash/shell.h"
      6 #include "ash/shell_factory.h"
      7 #include "ash/wm/coordinate_conversion.h"
      8 #include "ash/wm/window_properties.h"
      9 #include "ui/aura/client/capture_client.h"
     10 #include "ui/aura/client/screen_position_client.h"
     11 #include "ui/aura/env.h"
     12 #include "ui/aura/test/ui_controls_factory_aura.h"
     13 #include "ui/aura/window_property.h"
     14 #include "ui/aura/window_tree_host.h"
     15 #include "ui/base/test/ui_controls.h"
     16 #include "ui/base/test/ui_controls_aura.h"
     17 #include "ui/gfx/screen.h"
     18 
     19 DECLARE_WINDOW_PROPERTY_TYPE(ui_controls::UIControlsAura*)
     20 
     21 namespace ash {
     22 namespace test {
     23 namespace {
     24 
     25 using ui_controls::UIControlsAura;
     26 using ui_controls::MouseButton;
     27 
     28 DEFINE_OWNED_WINDOW_PROPERTY_KEY(UIControlsAura, kUIControlsKey, NULL);
     29 
     30 // Returns the UIControls object for RootWindow.
     31 // kUIControlsKey is owned property and UIControls object
     32 // will be deleted when the root window is deleted.
     33 UIControlsAura* GetUIControlsForRootWindow(aura::Window* root_window) {
     34   UIControlsAura* native_ui_control =
     35       root_window->GetProperty(kUIControlsKey);
     36   if (!native_ui_control) {
     37     native_ui_control =
     38         aura::test::CreateUIControlsAura(root_window->GetHost());
     39     // Pass the ownership to the |root_window|.
     40     root_window->SetProperty(kUIControlsKey, native_ui_control);
     41   }
     42   return native_ui_control;
     43 }
     44 
     45 // Returns the UIControls object for the RootWindow at |point_in_screen|.
     46 UIControlsAura* GetUIControlsAt(const gfx::Point& point_in_screen) {
     47   // TODO(mazda): Support the case passive grab is taken.
     48   return GetUIControlsForRootWindow(ash::wm::GetRootWindowAt(point_in_screen));
     49 }
     50 
     51 }  // namespace
     52 
     53 class UIControlsAsh : public UIControlsAura {
     54  public:
     55   UIControlsAsh() {
     56   }
     57   virtual ~UIControlsAsh() {
     58   }
     59 
     60   // UIControslAura overrides:
     61   virtual bool SendKeyPress(gfx::NativeWindow window,
     62                             ui::KeyboardCode key,
     63                             bool control,
     64                             bool shift,
     65                             bool alt,
     66                             bool command) OVERRIDE {
     67     return SendKeyPressNotifyWhenDone(
     68         window, key, control, shift, alt, command, base::Closure());
     69   }
     70 
     71   virtual bool SendKeyPressNotifyWhenDone(
     72       gfx::NativeWindow window,
     73       ui::KeyboardCode key,
     74       bool control,
     75       bool shift,
     76       bool alt,
     77       bool command,
     78       const base::Closure& closure) OVERRIDE {
     79     aura::Window* root =
     80         window ? window->GetRootWindow() : ash::Shell::GetTargetRootWindow();
     81     UIControlsAura* ui_controls = GetUIControlsForRootWindow(root);
     82     return ui_controls && ui_controls->SendKeyPressNotifyWhenDone(
     83         window, key, control, shift, alt, command, closure);
     84   }
     85 
     86   virtual bool SendMouseMove(long x, long y) OVERRIDE {
     87     gfx::Point p(x, y);
     88     UIControlsAura* ui_controls = GetUIControlsAt(p);
     89     return ui_controls && ui_controls->SendMouseMove(p.x(), p.y());
     90   }
     91 
     92   virtual bool SendMouseMoveNotifyWhenDone(
     93       long x,
     94       long y,
     95       const base::Closure& closure) OVERRIDE {
     96     gfx::Point p(x, y);
     97     UIControlsAura* ui_controls = GetUIControlsAt(p);
     98     return ui_controls &&
     99         ui_controls->SendMouseMoveNotifyWhenDone(p.x(), p.y(), closure);
    100   }
    101 
    102   virtual bool SendMouseEvents(MouseButton type, int state) OVERRIDE {
    103     gfx::Point p(ash::Shell::GetScreen()->GetCursorScreenPoint());
    104     UIControlsAura* ui_controls = GetUIControlsAt(p);
    105     return ui_controls && ui_controls->SendMouseEvents(type, state);
    106   }
    107 
    108   virtual bool SendMouseEventsNotifyWhenDone(
    109       MouseButton type, int state, const base::Closure& closure) OVERRIDE {
    110     gfx::Point p(aura::Env::GetInstance()->last_mouse_location());
    111     UIControlsAura* ui_controls = GetUIControlsAt(p);
    112     return ui_controls && ui_controls->SendMouseEventsNotifyWhenDone(
    113         type, state, closure);
    114   }
    115 
    116   virtual bool SendMouseClick(MouseButton type) OVERRIDE {
    117     gfx::Point p(ash::Shell::GetScreen()->GetCursorScreenPoint());
    118     UIControlsAura* ui_controls = GetUIControlsAt(p);
    119     return ui_controls && ui_controls->SendMouseClick(type);
    120   }
    121 
    122   virtual void RunClosureAfterAllPendingUIEvents(
    123       const base::Closure& closure) OVERRIDE {
    124     UIControlsAura* ui_controls = GetUIControlsForRootWindow(
    125         ash::Shell::GetTargetRootWindow());
    126     if (ui_controls)
    127       ui_controls->RunClosureAfterAllPendingUIEvents(closure);
    128   }
    129 
    130  private:
    131   DISALLOW_COPY_AND_ASSIGN(UIControlsAsh);
    132 };
    133 
    134 ui_controls::UIControlsAura* CreateAshUIControls() {
    135   return new ash::test::UIControlsAsh();
    136 }
    137 
    138 }  // namespace test
    139 }  // namespace ash
    140