Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ui / aura / test / ui_controls_factory_ozone.cc
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 "base/bind.h"
6 #include "base/logging.h"
7 #include "ui/aura/client/screen_position_client.h"
8 #include "ui/aura/env.h"
9 #include "ui/aura/test/aura_test_utils.h"
10 #include "ui/aura/test/ui_controls_factory_aura.h"
11 #include "ui/aura/window_tree_host.h"
12 #include "ui/base/test/ui_controls_aura.h"
13 #include "ui/events/test/events_test_utils.h"
14
15 namespace aura {
16 namespace test {
17 namespace {
18
19 class UIControlsOzone : public ui_controls::UIControlsAura {
20  public:
21   UIControlsOzone(WindowTreeHost* host) : host_(host) {}
22
23   virtual bool SendKeyPress(gfx::NativeWindow window,
24                             ui::KeyboardCode key,
25                             bool control,
26                             bool shift,
27                             bool alt,
28                             bool command) override {
29     return SendKeyPressNotifyWhenDone(
30         window, key, control, shift, alt, command, base::Closure());
31   }
32   virtual bool SendKeyPressNotifyWhenDone(
33       gfx::NativeWindow window,
34       ui::KeyboardCode key,
35       bool control,
36       bool shift,
37       bool alt,
38       bool command,
39       const base::Closure& closure) override {
40     int flags = button_down_mask_;
41
42     if (control) {
43       flags |= ui::EF_CONTROL_DOWN;
44       PostKeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_CONTROL, flags);
45     }
46
47     if (shift) {
48       flags |= ui::EF_SHIFT_DOWN;
49       PostKeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_SHIFT, flags);
50     }
51
52     if (alt) {
53       flags |= ui::EF_ALT_DOWN;
54       PostKeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_MENU, flags);
55     }
56
57     if (command) {
58       flags |= ui::EF_COMMAND_DOWN;
59       PostKeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_LWIN, flags);
60     }
61
62     PostKeyEvent(ui::ET_KEY_PRESSED, key, flags);
63     PostKeyEvent(ui::ET_KEY_RELEASED, key, flags);
64
65     if (alt) {
66       flags &= ~ui::EF_ALT_DOWN;
67       PostKeyEvent(ui::ET_KEY_RELEASED, ui::VKEY_MENU, flags);
68     }
69
70     if (shift) {
71       flags &= ~ui::EF_SHIFT_DOWN;
72       PostKeyEvent(ui::ET_KEY_RELEASED, ui::VKEY_SHIFT, flags);
73     }
74
75     if (control) {
76       flags &= ~ui::EF_CONTROL_DOWN;
77       PostKeyEvent(ui::ET_KEY_RELEASED, ui::VKEY_CONTROL, flags);
78     }
79
80     if (command) {
81       flags &= ~ui::EF_COMMAND_DOWN;
82       PostKeyEvent(ui::ET_KEY_RELEASED, ui::VKEY_LWIN, flags);
83     }
84
85     RunClosureAfterAllPendingUIEvents(closure);
86     return true;
87   }
88
89   virtual bool SendMouseMove(long screen_x, long screen_y) override {
90     return SendMouseMoveNotifyWhenDone(screen_x, screen_y, base::Closure());
91   }
92   virtual bool SendMouseMoveNotifyWhenDone(
93       long screen_x,
94       long screen_y,
95       const base::Closure& closure) override {
96     gfx::Point root_location(screen_x, screen_y);
97     aura::client::ScreenPositionClient* screen_position_client =
98         aura::client::GetScreenPositionClient(host_->window());
99     if (screen_position_client) {
100       screen_position_client->ConvertPointFromScreen(host_->window(),
101                                                      &root_location);
102     }
103     gfx::Point root_current_location =
104         QueryLatestMousePositionRequestInHost(host_);
105     host_->ConvertPointFromHost(&root_current_location);
106
107     if (button_down_mask_)
108       PostMouseEvent(ui::ET_MOUSE_DRAGGED, root_location, 0, 0);
109     else
110       PostMouseEvent(ui::ET_MOUSE_MOVED, root_location, 0, 0);
111
112     RunClosureAfterAllPendingUIEvents(closure);
113     return true;
114   }
115   virtual bool SendMouseEvents(ui_controls::MouseButton type,
116                                int state) override {
117     return SendMouseEventsNotifyWhenDone(type, state, base::Closure());
118   }
119   virtual bool SendMouseEventsNotifyWhenDone(
120       ui_controls::MouseButton type,
121       int state,
122       const base::Closure& closure) override {
123     gfx::Point loc = aura::Env::GetInstance()->last_mouse_location();
124     aura::client::ScreenPositionClient* screen_position_client =
125         aura::client::GetScreenPositionClient(host_->window());
126     if (screen_position_client) {
127       screen_position_client->ConvertPointFromScreen(host_->window(), &loc);
128     }
129     int flag = 0;
130
131     switch (type) {
132       case ui_controls::LEFT:
133         flag = ui::EF_LEFT_MOUSE_BUTTON;
134         break;
135       case ui_controls::MIDDLE:
136         flag = ui::EF_MIDDLE_MOUSE_BUTTON;
137         break;
138       case ui_controls::RIGHT:
139         flag = ui::EF_RIGHT_MOUSE_BUTTON;
140         break;
141       default:
142         NOTREACHED();
143         break;
144     }
145
146     if (state & ui_controls::DOWN) {
147       button_down_mask_ |= flag;
148       PostMouseEvent(ui::ET_MOUSE_PRESSED, loc, button_down_mask_ | flag, flag);
149     }
150     if (state & ui_controls::UP) {
151       button_down_mask_ &= ~flag;
152       PostMouseEvent(
153           ui::ET_MOUSE_RELEASED, loc, button_down_mask_ | flag, flag);
154     }
155
156     RunClosureAfterAllPendingUIEvents(closure);
157     return true;
158   }
159   virtual bool SendMouseClick(ui_controls::MouseButton type) override {
160     return SendMouseEvents(type, ui_controls::UP | ui_controls::DOWN);
161   }
162   virtual void RunClosureAfterAllPendingUIEvents(
163       const base::Closure& closure) override {
164     if (!closure.is_null())
165       base::MessageLoop::current()->PostTask(FROM_HERE, closure);
166   }
167
168  private:
169   void SendEventToProcessor(ui::Event* event) {
170     ui::EventSourceTestApi event_source_test(host_->GetEventSource());
171     ui::EventDispatchDetails details =
172         event_source_test.SendEventToProcessor(event);
173     if (details.dispatcher_destroyed)
174       return;
175   }
176
177   void PostKeyEvent(ui::EventType type, ui::KeyboardCode key_code, int flags) {
178     base::MessageLoop::current()->PostTask(
179         FROM_HERE,
180         base::Bind(&UIControlsOzone::PostKeyEventTask,
181                    base::Unretained(this),
182                    type,
183                    key_code,
184                    flags));
185   }
186
187   void PostKeyEventTask(ui::EventType type,
188                         ui::KeyboardCode key_code,
189                         int flags) {
190     // Do not rewrite injected events. See crbug.com/136465.
191     flags |= ui::EF_FINAL;
192
193     ui::KeyEvent key_event(type, key_code, flags);
194     SendEventToProcessor(&key_event);
195   }
196
197   void PostMouseEvent(ui::EventType type,
198                       const gfx::PointF& location,
199                       int flags,
200                       int changed_button_flags) {
201     base::MessageLoop::current()->PostTask(
202         FROM_HERE,
203         base::Bind(&UIControlsOzone::PostMouseEventTask,
204                    base::Unretained(this),
205                    type,
206                    location,
207                    flags,
208                    changed_button_flags));
209   }
210
211   void PostMouseEventTask(ui::EventType type,
212                           const gfx::PointF& location,
213                           int flags,
214                           int changed_button_flags) {
215     ui::MouseEvent mouse_event(
216         type, location, location, flags, changed_button_flags);
217
218     // This hack is necessary to set the repeat count for clicks.
219     ui::MouseEvent mouse_event2(&mouse_event);
220
221     SendEventToProcessor(&mouse_event2);
222   }
223
224   WindowTreeHost* host_;
225
226   // Mask of the mouse buttons currently down.
227   unsigned button_down_mask_ = 0;
228
229   DISALLOW_COPY_AND_ASSIGN(UIControlsOzone);
230 };
231
232 }  // namespace
233
234 ui_controls::UIControlsAura* CreateUIControlsAura(WindowTreeHost* host) {
235   return new UIControlsOzone(host);
236 }
237
238 }  // namespace test
239 }  // namespace aura