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/memory/singleton.h"
8 #include "ui/aura/client/screen_position_client.h"
9 #include "ui/aura/window_event_dispatcher.h"
10 #include "ui/aura/window_tree_host.h"
11
12 namespace aura {
13 namespace test {
14 namespace {
15
16 class DefaultEventGeneratorDelegate : public EventGeneratorDelegateAura {
17 public:
GetInstance()18 static DefaultEventGeneratorDelegate* GetInstance() {
19 return Singleton<DefaultEventGeneratorDelegate>::get();
20 }
21
22 // EventGeneratorDelegate:
SetContext(ui::test::EventGenerator * owner,gfx::NativeWindow root_window,gfx::NativeWindow window)23 virtual void SetContext(ui::test::EventGenerator* owner,
24 gfx::NativeWindow root_window,
25 gfx::NativeWindow window) OVERRIDE {
26 root_window_ = root_window;
27 }
28
29 // EventGeneratorDelegateAura:
GetHostAt(const gfx::Point & point) const30 virtual WindowTreeHost* GetHostAt(const gfx::Point& point) const OVERRIDE {
31 return root_window_->GetHost();
32 }
33
GetScreenPositionClient(const aura::Window * window) const34 virtual client::ScreenPositionClient* GetScreenPositionClient(
35 const aura::Window* window) const OVERRIDE {
36 return NULL;
37 }
38
39 private:
40 friend struct DefaultSingletonTraits<DefaultEventGeneratorDelegate>;
41
DefaultEventGeneratorDelegate()42 DefaultEventGeneratorDelegate() : root_window_(NULL) {
43 DCHECK(!ui::test::EventGenerator::default_delegate);
44 ui::test::EventGenerator::default_delegate = this;
45 }
46
~DefaultEventGeneratorDelegate()47 virtual ~DefaultEventGeneratorDelegate() {
48 DCHECK_EQ(this, ui::test::EventGenerator::default_delegate);
49 ui::test::EventGenerator::default_delegate = NULL;
50 }
51
52 Window* root_window_;
53
54 DISALLOW_COPY_AND_ASSIGN(DefaultEventGeneratorDelegate);
55 };
56
WindowFromTarget(const ui::EventTarget * event_target)57 const Window* WindowFromTarget(const ui::EventTarget* event_target) {
58 return static_cast<const Window*>(event_target);
59 }
60
61 } // namespace
62
InitializeAuraEventGeneratorDelegate()63 void InitializeAuraEventGeneratorDelegate() {
64 DefaultEventGeneratorDelegate::GetInstance();
65 }
66
EventGeneratorDelegateAura()67 EventGeneratorDelegateAura::EventGeneratorDelegateAura() {
68 }
69
~EventGeneratorDelegateAura()70 EventGeneratorDelegateAura::~EventGeneratorDelegateAura() {
71 }
72
GetTargetAt(const gfx::Point & location)73 ui::EventTarget* EventGeneratorDelegateAura::GetTargetAt(
74 const gfx::Point& location) {
75 return GetHostAt(location)->window();
76 }
77
GetEventSource(ui::EventTarget * target)78 ui::EventSource* EventGeneratorDelegateAura::GetEventSource(
79 ui::EventTarget* target) {
80 return static_cast<Window*>(target)->GetHost()->GetEventSource();
81 }
82
CenterOfTarget(const ui::EventTarget * target) const83 gfx::Point EventGeneratorDelegateAura::CenterOfTarget(
84 const ui::EventTarget* target) const {
85 gfx::Point center =
86 gfx::Rect(WindowFromTarget(target)->bounds().size()).CenterPoint();
87 ConvertPointFromTarget(target, ¢er);
88 return center;
89 }
90
CenterOfWindow(gfx::NativeWindow window) const91 gfx::Point EventGeneratorDelegateAura::CenterOfWindow(
92 gfx::NativeWindow window) const {
93 return CenterOfTarget(window);
94 }
95
ConvertPointFromTarget(const ui::EventTarget * event_target,gfx::Point * point) const96 void EventGeneratorDelegateAura::ConvertPointFromTarget(
97 const ui::EventTarget* event_target,
98 gfx::Point* point) const {
99 DCHECK(point);
100 const Window* target = WindowFromTarget(event_target);
101 aura::client::ScreenPositionClient* client = GetScreenPositionClient(target);
102 if (client)
103 client->ConvertPointToScreen(target, point);
104 else
105 aura::Window::ConvertPointToTarget(target, target->GetRootWindow(), point);
106 }
107
ConvertPointToTarget(const ui::EventTarget * event_target,gfx::Point * point) const108 void EventGeneratorDelegateAura::ConvertPointToTarget(
109 const ui::EventTarget* event_target,
110 gfx::Point* point) const {
111 DCHECK(point);
112 const Window* target = WindowFromTarget(event_target);
113 aura::client::ScreenPositionClient* client = GetScreenPositionClient(target);
114 if (client)
115 client->ConvertPointFromScreen(target, point);
116 else
117 aura::Window::ConvertPointToTarget(target->GetRootWindow(), target, point);
118 }
119
ConvertPointFromHost(const ui::EventTarget * hosted_target,gfx::Point * point) const120 void EventGeneratorDelegateAura::ConvertPointFromHost(
121 const ui::EventTarget* hosted_target,
122 gfx::Point* point) const {
123 const Window* window = WindowFromTarget(hosted_target);
124 window->GetHost()->ConvertPointFromHost(point);
125 }
126
127 } // namespace test
128 } // namespace aura
129