Roll src/third_party/WebKit a3b4a2e:7441784 (svn 202551:202552)
[chromium-blink-merge.git] / ui / aura / test / event_generator_delegate_aura.cc
blobf248cb6da9c01d161a56b33f569a06ba24b854a0
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 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 {
36 return NULL;
39 private:
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;
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 // TODO(andresantoso): Add support for EventGenerator::targeting_application()
76 // if needed for Ash.
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 {
87 gfx::Point center =
88 gfx::Rect(WindowFromTarget(target)->bounds().size()).CenterPoint();
89 ConvertPointFromTarget(target, &center);
90 return center;
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 {
101 DCHECK(point);
102 const Window* target = WindowFromTarget(event_target);
103 aura::client::ScreenPositionClient* client = GetScreenPositionClient(target);
104 if (client)
105 client->ConvertPointToScreen(target, point);
106 else
107 aura::Window::ConvertPointToTarget(target, target->GetRootWindow(), point);
110 void EventGeneratorDelegateAura::ConvertPointToTarget(
111 const ui::EventTarget* event_target,
112 gfx::Point* point) const {
113 DCHECK(point);
114 const Window* target = WindowFromTarget(event_target);
115 aura::client::ScreenPositionClient* client = GetScreenPositionClient(target);
116 if (client)
117 client->ConvertPointFromScreen(target, point);
118 else
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);
129 } // namespace test
130 } // namespace aura