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/root_window.h" 13 #include "ui/aura/test/ui_controls_factory_aura.h" 14 #include "ui/aura/window_property.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->GetDispatcher()); 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 the |point_in_screen| 46 // in virtual screen coordinates, and updates the |point| relative to the 47 // UIControlsAura's root window. NULL if there is no RootWindow under 48 // the |point_in_screen|. 49 UIControlsAura* GetUIControlsAt(gfx::Point* point_in_screen) { 50 // TODO(mazda): Support the case passive grab is taken. 51 aura::Window* root = ash::wm::GetRootWindowAt(*point_in_screen); 52 53 aura::client::ScreenPositionClient* screen_position_client = 54 aura::client::GetScreenPositionClient(root); 55 if (screen_position_client) 56 screen_position_client->ConvertPointFromScreen(root, point_in_screen); 57 58 return GetUIControlsForRootWindow(root); 59 } 60 61 } // namespace 62 63 class UIControlsAsh : public UIControlsAura { 64 public: 65 UIControlsAsh() { 66 } 67 virtual ~UIControlsAsh() { 68 } 69 70 // UIControslAura overrides: 71 virtual bool SendKeyPress(gfx::NativeWindow window, 72 ui::KeyboardCode key, 73 bool control, 74 bool shift, 75 bool alt, 76 bool command) OVERRIDE { 77 return SendKeyPressNotifyWhenDone( 78 window, key, control, shift, alt, command, base::Closure()); 79 } 80 81 virtual bool SendKeyPressNotifyWhenDone( 82 gfx::NativeWindow window, 83 ui::KeyboardCode key, 84 bool control, 85 bool shift, 86 bool alt, 87 bool command, 88 const base::Closure& closure) OVERRIDE { 89 aura::Window* root = 90 window ? window->GetRootWindow() : ash::Shell::GetTargetRootWindow(); 91 UIControlsAura* ui_controls = GetUIControlsForRootWindow(root); 92 return ui_controls && ui_controls->SendKeyPressNotifyWhenDone( 93 window, key, control, shift, alt, command, closure); 94 } 95 96 virtual bool SendMouseMove(long x, long y) OVERRIDE { 97 gfx::Point p(x, y); 98 UIControlsAura* ui_controls = GetUIControlsAt(&p); 99 return ui_controls && ui_controls->SendMouseMove(p.x(), p.y()); 100 } 101 102 virtual bool SendMouseMoveNotifyWhenDone( 103 long x, 104 long y, 105 const base::Closure& closure) OVERRIDE { 106 gfx::Point p(x, y); 107 UIControlsAura* ui_controls = GetUIControlsAt(&p); 108 return ui_controls && 109 ui_controls->SendMouseMoveNotifyWhenDone(p.x(), p.y(), closure); 110 } 111 112 virtual bool SendMouseEvents(MouseButton type, int state) OVERRIDE { 113 gfx::Point p(ash::Shell::GetScreen()->GetCursorScreenPoint()); 114 UIControlsAura* ui_controls = GetUIControlsAt(&p); 115 return ui_controls && ui_controls->SendMouseEvents(type, state); 116 } 117 118 virtual bool SendMouseEventsNotifyWhenDone( 119 MouseButton type, int state, const base::Closure& closure) OVERRIDE { 120 gfx::Point p(aura::Env::GetInstance()->last_mouse_location()); 121 UIControlsAura* ui_controls = GetUIControlsAt(&p); 122 return ui_controls && ui_controls->SendMouseEventsNotifyWhenDone( 123 type, state, closure); 124 } 125 126 virtual bool SendMouseClick(MouseButton type) OVERRIDE { 127 gfx::Point p(ash::Shell::GetScreen()->GetCursorScreenPoint()); 128 UIControlsAura* ui_controls = GetUIControlsAt(&p); 129 return ui_controls && ui_controls->SendMouseClick(type); 130 } 131 132 virtual void RunClosureAfterAllPendingUIEvents( 133 const base::Closure& closure) OVERRIDE { 134 UIControlsAura* ui_controls = GetUIControlsForRootWindow( 135 ash::Shell::GetTargetRootWindow()); 136 if (ui_controls) 137 ui_controls->RunClosureAfterAllPendingUIEvents(closure); 138 } 139 140 private: 141 DISALLOW_COPY_AND_ASSIGN(UIControlsAsh); 142 }; 143 144 ui_controls::UIControlsAura* CreateAshUIControls() { 145 return new ash::test::UIControlsAsh(); 146 } 147 148 } // namespace test 149 } // namespace ash 150