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.
5 #include "ui/aura/test/event_generator_delegate_aura.h"
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"
16 class DefaultEventGeneratorDelegate
: public EventGeneratorDelegateAura
{
18 static DefaultEventGeneratorDelegate
* GetInstance() {
19 return Singleton
<DefaultEventGeneratorDelegate
>::get();
22 // EventGeneratorDelegate:
23 virtual void SetContext(ui::test::EventGenerator
* owner
,
24 gfx::NativeWindow root_window
,
25 gfx::NativeWindow window
) OVERRIDE
{
26 root_window_
= root_window
;
29 // EventGeneratorDelegateAura:
30 virtual WindowTreeHost
* GetHostAt(const gfx::Point
& point
) const OVERRIDE
{
31 return root_window_
->GetHost();
34 virtual client::ScreenPositionClient
* GetScreenPositionClient(
35 const aura::Window
* window
) const OVERRIDE
{
40 friend struct DefaultSingletonTraits
<DefaultEventGeneratorDelegate
>;
42 DefaultEventGeneratorDelegate() : root_window_(NULL
) {
43 DCHECK(!ui::test::EventGenerator::default_delegate
);
44 ui::test::EventGenerator::default_delegate
= this;
47 virtual ~DefaultEventGeneratorDelegate() {
48 DCHECK_EQ(this, ui::test::EventGenerator::default_delegate
);
49 ui::test::EventGenerator::default_delegate
= NULL
;
54 DISALLOW_COPY_AND_ASSIGN(DefaultEventGeneratorDelegate
);
57 const Window
* WindowFromTarget(const ui::EventTarget
* event_target
) {
58 return static_cast<const Window
*>(event_target
);
63 void InitializeAuraEventGeneratorDelegate() {
64 DefaultEventGeneratorDelegate::GetInstance();
67 EventGeneratorDelegateAura::EventGeneratorDelegateAura() {
70 EventGeneratorDelegateAura::~EventGeneratorDelegateAura() {
73 ui::EventTarget
* EventGeneratorDelegateAura::GetTargetAt(
74 const gfx::Point
& location
) {
75 return GetHostAt(location
)->window();
78 ui::EventSource
* EventGeneratorDelegateAura::GetEventSource(
79 ui::EventTarget
* target
) {
80 return static_cast<Window
*>(target
)->GetHost()->GetEventSource();
83 gfx::Point
EventGeneratorDelegateAura::CenterOfTarget(
84 const ui::EventTarget
* target
) const {
86 gfx::Rect(WindowFromTarget(target
)->bounds().size()).CenterPoint();
87 ConvertPointFromTarget(target
, ¢er
);
91 gfx::Point
EventGeneratorDelegateAura::CenterOfWindow(
92 gfx::NativeWindow window
) const {
93 return CenterOfTarget(window
);
96 void EventGeneratorDelegateAura::ConvertPointFromTarget(
97 const ui::EventTarget
* event_target
,
98 gfx::Point
* point
) const {
100 const Window
* target
= WindowFromTarget(event_target
);
101 aura::client::ScreenPositionClient
* client
= GetScreenPositionClient(target
);
103 client
->ConvertPointToScreen(target
, point
);
105 aura::Window::ConvertPointToTarget(target
, target
->GetRootWindow(), point
);
108 void EventGeneratorDelegateAura::ConvertPointToTarget(
109 const ui::EventTarget
* event_target
,
110 gfx::Point
* point
) const {
112 const Window
* target
= WindowFromTarget(event_target
);
113 aura::client::ScreenPositionClient
* client
= GetScreenPositionClient(target
);
115 client
->ConvertPointFromScreen(target
, point
);
117 aura::Window::ConvertPointToTarget(target
->GetRootWindow(), target
, point
);
120 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
);