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 "ui/aura/test/event_generator_delegate_aura.h"
6
7 #include "base/bind.h"
8 #include "base/macros.h"
9 #include "base/run_loop.h"
10 #include "ui/aura/client/screen_position_client.h"
11 #include "ui/aura/test/default_event_generator_delegate.h"
12 #include "ui/aura/window_tree_host.h"
13 #include "ui/base/ime/input_method.h"
14 #include "ui/events/event_source.h"
15
16 namespace aura {
17 namespace test {
18 namespace {
19
WindowFromTarget(const ui::EventTarget * event_target)20 const Window* WindowFromTarget(const ui::EventTarget* event_target) {
21 return static_cast<const Window*>(event_target);
22 }
WindowFromTarget(ui::EventTarget * event_target)23 Window* WindowFromTarget(ui::EventTarget* event_target) {
24 return static_cast<Window*>(event_target);
25 }
26
27 } // namespace
28
29 // static
30 std::unique_ptr<ui::test::EventGeneratorDelegate>
Create(ui::test::EventGenerator * owner,gfx::NativeWindow root_window,gfx::NativeWindow target_window)31 EventGeneratorDelegateAura::Create(ui::test::EventGenerator* owner,
32 gfx::NativeWindow root_window,
33 gfx::NativeWindow target_window) {
34 // Tests should not create event generators for a "root window" that's not
35 // actually the root window.
36 if (root_window)
37 DCHECK_EQ(root_window, root_window->GetRootWindow());
38
39 return std::make_unique<DefaultEventGeneratorDelegate>(root_window);
40 }
41
42 EventGeneratorDelegateAura::EventGeneratorDelegateAura() = default;
43
44 EventGeneratorDelegateAura::~EventGeneratorDelegateAura() = default;
45
46 client::ScreenPositionClient*
GetScreenPositionClient(const Window * window) const47 EventGeneratorDelegateAura::GetScreenPositionClient(
48 const Window* window) const {
49 return client::GetScreenPositionClient(window->GetRootWindow());
50 }
51
SetTargetWindow(gfx::NativeWindow target_window)52 void EventGeneratorDelegateAura::SetTargetWindow(
53 gfx::NativeWindow target_window) {
54 NOTIMPLEMENTED();
55 }
56
GetEventSource(ui::EventTarget * target)57 ui::EventSource* EventGeneratorDelegateAura::GetEventSource(
58 ui::EventTarget* target) {
59 return WindowFromTarget(target)->GetHost()->GetEventSource();
60 }
61
CenterOfTarget(const ui::EventTarget * target) const62 gfx::Point EventGeneratorDelegateAura::CenterOfTarget(
63 const ui::EventTarget* target) const {
64 return CenterOfWindow(WindowFromTarget(target));
65 }
66
CenterOfWindow(gfx::NativeWindow window) const67 gfx::Point EventGeneratorDelegateAura::CenterOfWindow(
68 gfx::NativeWindow window) const {
69 return CenterOfWindow(static_cast<const Window*>(window));
70 }
71
ConvertPointFromTarget(const ui::EventTarget * event_target,gfx::Point * point) const72 void EventGeneratorDelegateAura::ConvertPointFromTarget(
73 const ui::EventTarget* event_target,
74 gfx::Point* point) const {
75 ConvertPointFromWindow(WindowFromTarget(event_target), point);
76 }
77
ConvertPointToTarget(const ui::EventTarget * event_target,gfx::Point * point) const78 void EventGeneratorDelegateAura::ConvertPointToTarget(
79 const ui::EventTarget* event_target,
80 gfx::Point* point) const {
81 DCHECK(point);
82 const Window* target = WindowFromTarget(event_target);
83 aura::client::ScreenPositionClient* client = GetScreenPositionClient(target);
84 if (client)
85 client->ConvertPointFromScreen(target, point);
86 else
87 aura::Window::ConvertPointToTarget(target->GetRootWindow(), target, point);
88 }
89
ConvertPointFromWindow(gfx::NativeWindow window,gfx::Point * point) const90 void EventGeneratorDelegateAura::ConvertPointFromWindow(
91 gfx::NativeWindow window,
92 gfx::Point* point) const {
93 return ConvertPointFromWindow(static_cast<const Window*>(window), point);
94 }
95
ConvertPointFromHost(const ui::EventTarget * hosted_target,gfx::Point * point) const96 void EventGeneratorDelegateAura::ConvertPointFromHost(
97 const ui::EventTarget* hosted_target,
98 gfx::Point* point) const {
99 const Window* window = WindowFromTarget(hosted_target);
100 window->GetHost()->ConvertPixelsToDIP(point);
101 }
102
DispatchKeyEventToIME(ui::EventTarget * target,ui::KeyEvent * event)103 ui::EventDispatchDetails EventGeneratorDelegateAura::DispatchKeyEventToIME(
104 ui::EventTarget* target,
105 ui::KeyEvent* event) {
106 Window* const window = WindowFromTarget(target);
107 return window->GetHost()->GetInputMethod()->DispatchKeyEvent(event);
108 }
109
CenterOfWindow(const Window * window) const110 gfx::Point EventGeneratorDelegateAura::CenterOfWindow(
111 const Window* window) const {
112 gfx::Point center = gfx::Rect(window->bounds().size()).CenterPoint();
113 ConvertPointFromWindow(window, ¢er);
114 return center;
115 }
116
ConvertPointFromWindow(const Window * window,gfx::Point * point) const117 void EventGeneratorDelegateAura::ConvertPointFromWindow(
118 const Window* window,
119 gfx::Point* point) const {
120 DCHECK(point);
121 aura::client::ScreenPositionClient* client = GetScreenPositionClient(window);
122 if (client)
123 client->ConvertPointToScreen(window, point);
124 else
125 aura::Window::ConvertPointToTarget(window, window->GetRootWindow(), point);
126 }
127
128 } // namespace test
129 } // namespace aura
130