Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ui / aura / test / event_generator_delegate_aura.cc
bloba4041a3c21158863cb462e0aeba61ae09d28d294
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"
12 namespace aura {
13 namespace test {
14 namespace {
16 class DefaultEventGeneratorDelegate : public EventGeneratorDelegateAura {
17 public:
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 {
36 return NULL;
39 private:
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;
52 Window* root_window_;
54 DISALLOW_COPY_AND_ASSIGN(DefaultEventGeneratorDelegate);
57 const Window* WindowFromTarget(const ui::EventTarget* event_target) {
58 return static_cast<const Window*>(event_target);
61 } // namespace
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 {
85 gfx::Point center =
86 gfx::Rect(WindowFromTarget(target)->bounds().size()).CenterPoint();
87 ConvertPointFromTarget(target, &center);
88 return center;
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 {
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);
108 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);
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);
127 } // namespace test
128 } // namespace aura