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