1// Copyright (c) 2012 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/test_window_delegate.h"
6
7#include "base/strings/stringprintf.h"
8#include "ui/aura/window.h"
9#include "ui/base/hit_test.h"
10#include "ui/events/event.h"
11#include "ui/gfx/canvas.h"
12#include "ui/gfx/path.h"
13#include "ui/gfx/skia_util.h"
14
15#if defined(USE_AURA)
16#include "ui/base/cursor/cursor.h"
17#endif
18
19namespace aura {
20namespace test {
21
22////////////////////////////////////////////////////////////////////////////////
23// TestWindowDelegate
24
25TestWindowDelegate::TestWindowDelegate()
26    : window_component_(HTCLIENT),
27      delete_on_destroyed_(false),
28      can_focus_(true) {
29}
30
31TestWindowDelegate::~TestWindowDelegate() {
32}
33
34// static
35TestWindowDelegate* TestWindowDelegate::CreateSelfDestroyingDelegate() {
36  TestWindowDelegate* delegate = new TestWindowDelegate;
37  delegate->delete_on_destroyed_ = true;
38  return delegate;
39}
40
41gfx::Size TestWindowDelegate::GetMinimumSize() const {
42  return minimum_size_;
43}
44
45gfx::Size TestWindowDelegate::GetMaximumSize() const {
46  return maximum_size_;
47}
48
49void TestWindowDelegate::OnBoundsChanged(const gfx::Rect& old_bounds,
50                                         const gfx::Rect& new_bounds) {
51}
52
53gfx::NativeCursor TestWindowDelegate::GetCursor(const gfx::Point& point) {
54  return gfx::kNullCursor;
55}
56
57int TestWindowDelegate::GetNonClientComponent(const gfx::Point& point) const {
58  return window_component_;
59}
60
61bool TestWindowDelegate::ShouldDescendIntoChildForEventHandling(
62      Window* child,
63      const gfx::Point& location) {
64  return true;
65}
66
67bool TestWindowDelegate::CanFocus() {
68  return can_focus_;
69}
70
71void TestWindowDelegate::OnCaptureLost() {
72}
73
74void TestWindowDelegate::OnPaint(gfx::Canvas* canvas) {
75}
76
77void TestWindowDelegate::OnDeviceScaleFactorChanged(
78    float device_scale_factor) {
79}
80
81void TestWindowDelegate::OnWindowDestroying() {
82}
83
84void TestWindowDelegate::OnWindowDestroyed() {
85  if (delete_on_destroyed_)
86    delete this;
87}
88
89void TestWindowDelegate::OnWindowTargetVisibilityChanged(bool visible) {
90}
91
92bool TestWindowDelegate::HasHitTestMask() const {
93  return false;
94}
95
96void TestWindowDelegate::GetHitTestMask(gfx::Path* mask) const {
97}
98
99void TestWindowDelegate::DidRecreateLayer(ui::Layer *old_layer,
100                                          ui::Layer *new_layer) {
101}
102
103////////////////////////////////////////////////////////////////////////////////
104// ColorTestWindowDelegate
105
106ColorTestWindowDelegate::ColorTestWindowDelegate(SkColor color)
107    : color_(color),
108      last_key_code_(ui::VKEY_UNKNOWN) {
109}
110
111ColorTestWindowDelegate::~ColorTestWindowDelegate() {
112}
113
114void ColorTestWindowDelegate::OnKeyEvent(ui::KeyEvent* event) {
115  last_key_code_ = event->key_code();
116  event->SetHandled();
117}
118
119void ColorTestWindowDelegate::OnWindowDestroyed() {
120  delete this;
121}
122
123void ColorTestWindowDelegate::OnPaint(gfx::Canvas* canvas) {
124  canvas->DrawColor(color_, SkXfermode::kSrc_Mode);
125}
126
127////////////////////////////////////////////////////////////////////////////////
128// MaskedWindowDelegate
129
130MaskedWindowDelegate::MaskedWindowDelegate(const gfx::Rect mask_rect)
131    : mask_rect_(mask_rect) {
132}
133
134bool MaskedWindowDelegate::HasHitTestMask() const {
135  return true;
136}
137
138void MaskedWindowDelegate::GetHitTestMask(gfx::Path* mask) const {
139  mask->addRect(RectToSkRect(mask_rect_));
140}
141
142////////////////////////////////////////////////////////////////////////////////
143// EventCountDelegate
144
145EventCountDelegate::EventCountDelegate()
146  : mouse_enter_count_(0),
147    mouse_move_count_(0),
148    mouse_leave_count_(0),
149    mouse_press_count_(0),
150    mouse_release_count_(0),
151    key_press_count_(0),
152    key_release_count_(0) {
153}
154
155void EventCountDelegate::OnKeyEvent(ui::KeyEvent* event) {
156  switch (event->type()) {
157    case ui::ET_KEY_PRESSED:
158      key_press_count_++;
159      break;
160    case ui::ET_KEY_RELEASED:
161      key_release_count_++;
162    default:
163      break;
164  }
165}
166
167void EventCountDelegate::OnMouseEvent(ui::MouseEvent* event) {
168  switch (event->type()) {
169    case ui::ET_MOUSE_MOVED:
170      mouse_move_count_++;
171      break;
172    case ui::ET_MOUSE_ENTERED:
173      mouse_enter_count_++;
174      break;
175    case ui::ET_MOUSE_EXITED:
176      mouse_leave_count_++;
177      break;
178    case ui::ET_MOUSE_PRESSED:
179      mouse_press_count_++;
180      break;
181    case ui::ET_MOUSE_RELEASED:
182      mouse_release_count_++;
183      break;
184    default:
185      break;
186  }
187}
188
189std::string EventCountDelegate::GetMouseMotionCountsAndReset() {
190  std::string result = base::StringPrintf("%d %d %d",
191                                          mouse_enter_count_,
192                                          mouse_move_count_,
193                                          mouse_leave_count_);
194  mouse_enter_count_ = 0;
195  mouse_move_count_ = 0;
196  mouse_leave_count_ = 0;
197  return result;
198}
199
200std::string EventCountDelegate::GetMouseButtonCountsAndReset() {
201  std::string result = base::StringPrintf("%d %d",
202                                          mouse_press_count_,
203                                          mouse_release_count_);
204  mouse_press_count_ = 0;
205  mouse_release_count_ = 0;
206  return result;
207}
208
209
210std::string EventCountDelegate::GetKeyCountsAndReset() {
211  std::string result = base::StringPrintf("%d %d",
212                                          key_press_count_,
213                                          key_release_count_);
214  key_press_count_ = 0;
215  key_release_count_ = 0;
216  return result;
217}
218
219}  // namespace test
220}  // namespace aura
221