Removed 'anonymous' from namespace, added whitespace in thread_restrictions.cc
[chromium-blink-merge.git] / ash / test / ui_controls_factory_ash.cc
blob26f8c245b0b4644670361dc61ba57dcc00318177
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.
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"
19 DECLARE_WINDOW_PROPERTY_TYPE(ui_controls::UIControlsAura*)
21 namespace ash {
22 namespace test {
23 namespace {
25 using ui_controls::UIControlsAura;
26 using ui_controls::MouseButton;
28 DEFINE_OWNED_WINDOW_PROPERTY_KEY(UIControlsAura, kUIControlsKey, NULL);
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);
42 return native_ui_control;
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));
51 } // namespace
53 class UIControlsAsh : public UIControlsAura {
54 public:
55 UIControlsAsh() {
57 ~UIControlsAsh() override {}
59 // UIControslAura overrides:
60 bool SendKeyPress(gfx::NativeWindow window,
61 ui::KeyboardCode key,
62 bool control,
63 bool shift,
64 bool alt,
65 bool command) override {
66 return SendKeyPressNotifyWhenDone(
67 window, key, control, shift, alt, command, base::Closure());
70 bool SendKeyPressNotifyWhenDone(gfx::NativeWindow window,
71 ui::KeyboardCode key,
72 bool control,
73 bool shift,
74 bool alt,
75 bool command,
76 const base::Closure& closure) override {
77 aura::Window* root =
78 window ? window->GetRootWindow() : ash::Shell::GetTargetRootWindow();
79 UIControlsAura* ui_controls = GetUIControlsForRootWindow(root);
80 return ui_controls && ui_controls->SendKeyPressNotifyWhenDone(
81 window, key, control, shift, alt, command, closure);
84 bool SendMouseMove(long x, long y) override {
85 gfx::Point p(x, y);
86 UIControlsAura* ui_controls = GetUIControlsAt(p);
87 return ui_controls && ui_controls->SendMouseMove(p.x(), p.y());
90 bool SendMouseMoveNotifyWhenDone(long x,
91 long y,
92 const base::Closure& closure) override {
93 gfx::Point p(x, y);
94 UIControlsAura* ui_controls = GetUIControlsAt(p);
95 return ui_controls &&
96 ui_controls->SendMouseMoveNotifyWhenDone(p.x(), p.y(), closure);
99 bool SendMouseEvents(MouseButton type, int state) override {
100 gfx::Point p(ash::Shell::GetScreen()->GetCursorScreenPoint());
101 UIControlsAura* ui_controls = GetUIControlsAt(p);
102 return ui_controls && ui_controls->SendMouseEvents(type, state);
105 bool SendMouseEventsNotifyWhenDone(MouseButton type,
106 int state,
107 const base::Closure& closure) override {
108 gfx::Point p(aura::Env::GetInstance()->last_mouse_location());
109 UIControlsAura* ui_controls = GetUIControlsAt(p);
110 return ui_controls && ui_controls->SendMouseEventsNotifyWhenDone(
111 type, state, closure);
114 bool SendMouseClick(MouseButton type) override {
115 gfx::Point p(ash::Shell::GetScreen()->GetCursorScreenPoint());
116 UIControlsAura* ui_controls = GetUIControlsAt(p);
117 return ui_controls && ui_controls->SendMouseClick(type);
120 void RunClosureAfterAllPendingUIEvents(
121 const base::Closure& closure) override {
122 UIControlsAura* ui_controls = GetUIControlsForRootWindow(
123 ash::Shell::GetTargetRootWindow());
124 if (ui_controls)
125 ui_controls->RunClosureAfterAllPendingUIEvents(closure);
128 private:
129 DISALLOW_COPY_AND_ASSIGN(UIControlsAsh);
132 ui_controls::UIControlsAura* CreateAshUIControls() {
133 return new ash::test::UIControlsAsh();
136 } // namespace test
137 } // namespace ash