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
12namespace aura {
13namespace test {
14namespace {
15
16class DefaultEventGeneratorDelegate : public EventGeneratorDelegateAura {
17 public:
18  static DefaultEventGeneratorDelegate* GetInstance() {
19    return Singleton<DefaultEventGeneratorDelegate>::get();
20  }
21
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;
27  }
28
29  // EventGeneratorDelegateAura:
30  virtual WindowTreeHost* GetHostAt(const gfx::Point& point) const OVERRIDE {
31    return root_window_->GetHost();
32  }
33
34  virtual client::ScreenPositionClient* GetScreenPositionClient(
35      const aura::Window* window) const OVERRIDE {
36    return NULL;
37  }
38
39 private:
40  friend struct DefaultSingletonTraits<DefaultEventGeneratorDelegate>;
41
42  DefaultEventGeneratorDelegate() : root_window_(NULL) {
43    DCHECK(!ui::test::EventGenerator::default_delegate);
44    ui::test::EventGenerator::default_delegate = this;
45  }
46
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
57const Window* WindowFromTarget(const ui::EventTarget* event_target) {
58  return static_cast<const Window*>(event_target);
59}
60
61}  // namespace
62
63void InitializeAuraEventGeneratorDelegate() {
64  DefaultEventGeneratorDelegate::GetInstance();
65}
66
67EventGeneratorDelegateAura::EventGeneratorDelegateAura() {
68}
69
70EventGeneratorDelegateAura::~EventGeneratorDelegateAura() {
71}
72
73ui::EventTarget* EventGeneratorDelegateAura::GetTargetAt(
74    const gfx::Point& location) {
75  return GetHostAt(location)->window();
76}
77
78ui::EventSource* EventGeneratorDelegateAura::GetEventSource(
79    ui::EventTarget* target) {
80  return static_cast<Window*>(target)->GetHost()->GetEventSource();
81}
82
83gfx::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;
89}
90
91gfx::Point EventGeneratorDelegateAura::CenterOfWindow(
92    gfx::NativeWindow window) const {
93  return CenterOfTarget(window);
94}
95
96void 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
108void 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
120void 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