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 base::Singleton
<DefaultEventGeneratorDelegate
>::get();
22 // EventGeneratorDelegate:
23 void SetContext(ui::test::EventGenerator
* owner
,
24 gfx::NativeWindow root_window
,
25 gfx::NativeWindow window
) override
{
26 root_window_
= root_window
;
29 // EventGeneratorDelegateAura:
30 WindowTreeHost
* GetHostAt(const gfx::Point
& point
) const override
{
31 return root_window_
->GetHost();
34 client::ScreenPositionClient
* GetScreenPositionClient(
35 const aura::Window
* window
) const override
{
40 friend struct base::DefaultSingletonTraits
<DefaultEventGeneratorDelegate
>;
42 DefaultEventGeneratorDelegate() : root_window_(NULL
) {
43 DCHECK(!ui::test::EventGenerator::default_delegate
);
44 ui::test::EventGenerator::default_delegate
= this;
47 ~DefaultEventGeneratorDelegate() override
{
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 // TODO(andresantoso): Add support for EventGenerator::targeting_application()
77 return GetHostAt(location
)->window();
80 ui::EventSource
* EventGeneratorDelegateAura::GetEventSource(
81 ui::EventTarget
* target
) {
82 return static_cast<Window
*>(target
)->GetHost()->GetEventSource();
85 gfx::Point
EventGeneratorDelegateAura::CenterOfTarget(
86 const ui::EventTarget
* target
) const {
88 gfx::Rect(WindowFromTarget(target
)->bounds().size()).CenterPoint();
89 ConvertPointFromTarget(target
, ¢er
);
93 gfx::Point
EventGeneratorDelegateAura::CenterOfWindow(
94 gfx::NativeWindow window
) const {
95 return CenterOfTarget(window
);
98 void EventGeneratorDelegateAura::ConvertPointFromTarget(
99 const ui::EventTarget
* event_target
,
100 gfx::Point
* point
) const {
102 const Window
* target
= WindowFromTarget(event_target
);
103 aura::client::ScreenPositionClient
* client
= GetScreenPositionClient(target
);
105 client
->ConvertPointToScreen(target
, point
);
107 aura::Window::ConvertPointToTarget(target
, target
->GetRootWindow(), point
);
110 void EventGeneratorDelegateAura::ConvertPointToTarget(
111 const ui::EventTarget
* event_target
,
112 gfx::Point
* point
) const {
114 const Window
* target
= WindowFromTarget(event_target
);
115 aura::client::ScreenPositionClient
* client
= GetScreenPositionClient(target
);
117 client
->ConvertPointFromScreen(target
, point
);
119 aura::Window::ConvertPointToTarget(target
->GetRootWindow(), target
, point
);
122 void EventGeneratorDelegateAura::ConvertPointFromHost(
123 const ui::EventTarget
* hosted_target
,
124 gfx::Point
* point
) const {
125 const Window
* window
= WindowFromTarget(hosted_target
);
126 window
->GetHost()->ConvertPointFromHost(point
);