Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / ui / aura / test / test_window_delegate.cc
blobbf49079c038da8c90393b90ef05ab1083bc91d6f
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.
5 #include "ui/aura/test/test_window_delegate.h"
7 #include "base/strings/stringprintf.h"
8 #include "ui/aura/window.h"
9 #include "ui/base/hit_test.h"
10 #include "ui/compositor/paint_recorder.h"
11 #include "ui/events/event.h"
12 #include "ui/gfx/canvas.h"
13 #include "ui/gfx/path.h"
14 #include "ui/gfx/skia_util.h"
16 #if defined(USE_AURA)
17 #include "ui/base/cursor/cursor.h"
18 #endif
20 namespace aura {
21 namespace test {
23 ////////////////////////////////////////////////////////////////////////////////
24 // TestWindowDelegate
26 TestWindowDelegate::TestWindowDelegate()
27 : window_component_(HTCLIENT),
28 delete_on_destroyed_(false),
29 can_focus_(true) {
32 TestWindowDelegate::~TestWindowDelegate() {
35 // static
36 TestWindowDelegate* TestWindowDelegate::CreateSelfDestroyingDelegate() {
37 TestWindowDelegate* delegate = new TestWindowDelegate;
38 delegate->delete_on_destroyed_ = true;
39 return delegate;
42 gfx::Size TestWindowDelegate::GetMinimumSize() const {
43 return minimum_size_;
46 gfx::Size TestWindowDelegate::GetMaximumSize() const {
47 return maximum_size_;
50 void TestWindowDelegate::OnBoundsChanged(const gfx::Rect& old_bounds,
51 const gfx::Rect& new_bounds) {
54 gfx::NativeCursor TestWindowDelegate::GetCursor(const gfx::Point& point) {
55 return gfx::kNullCursor;
58 int TestWindowDelegate::GetNonClientComponent(const gfx::Point& point) const {
59 return window_component_;
62 bool TestWindowDelegate::ShouldDescendIntoChildForEventHandling(
63 Window* child,
64 const gfx::Point& location) {
65 return true;
68 bool TestWindowDelegate::CanFocus() {
69 return can_focus_;
72 void TestWindowDelegate::OnCaptureLost() {
75 void TestWindowDelegate::OnPaint(const ui::PaintContext& context) {
78 void TestWindowDelegate::OnDeviceScaleFactorChanged(
79 float device_scale_factor) {
82 void TestWindowDelegate::OnWindowDestroying(Window* window) {
85 void TestWindowDelegate::OnWindowDestroyed(Window* window) {
86 if (delete_on_destroyed_)
87 delete this;
90 void TestWindowDelegate::OnWindowTargetVisibilityChanged(bool visible) {
93 bool TestWindowDelegate::HasHitTestMask() const {
94 return false;
97 void TestWindowDelegate::GetHitTestMask(gfx::Path* mask) const {
100 ////////////////////////////////////////////////////////////////////////////////
101 // ColorTestWindowDelegate
103 ColorTestWindowDelegate::ColorTestWindowDelegate(SkColor color)
104 : color_(color),
105 last_key_code_(ui::VKEY_UNKNOWN) {
108 ColorTestWindowDelegate::~ColorTestWindowDelegate() {
111 void ColorTestWindowDelegate::OnBoundsChanged(const gfx::Rect& old_bounds,
112 const gfx::Rect& new_bounds) {
113 window_size_ = new_bounds.size();
116 void ColorTestWindowDelegate::OnKeyEvent(ui::KeyEvent* event) {
117 last_key_code_ = event->key_code();
118 event->SetHandled();
121 void ColorTestWindowDelegate::OnWindowDestroyed(Window* window) {
122 delete this;
125 void ColorTestWindowDelegate::OnPaint(const ui::PaintContext& context) {
126 ui::PaintRecorder recorder(context, window_size_);
127 recorder.canvas()->DrawColor(color_, SkXfermode::kSrc_Mode);
130 ////////////////////////////////////////////////////////////////////////////////
131 // MaskedWindowDelegate
133 MaskedWindowDelegate::MaskedWindowDelegate(const gfx::Rect mask_rect)
134 : mask_rect_(mask_rect) {
137 bool MaskedWindowDelegate::HasHitTestMask() const {
138 return true;
141 void MaskedWindowDelegate::GetHitTestMask(gfx::Path* mask) const {
142 mask->addRect(RectToSkRect(mask_rect_));
145 ////////////////////////////////////////////////////////////////////////////////
146 // EventCountDelegate
148 EventCountDelegate::EventCountDelegate()
149 : mouse_enter_count_(0),
150 mouse_move_count_(0),
151 mouse_leave_count_(0),
152 mouse_press_count_(0),
153 mouse_release_count_(0),
154 key_press_count_(0),
155 key_release_count_(0),
156 gesture_count_(0) {
159 void EventCountDelegate::OnKeyEvent(ui::KeyEvent* event) {
160 switch (event->type()) {
161 case ui::ET_KEY_PRESSED:
162 key_press_count_++;
163 break;
164 case ui::ET_KEY_RELEASED:
165 key_release_count_++;
166 default:
167 break;
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;
193 void EventCountDelegate::OnGestureEvent(ui::GestureEvent* event) {
194 gesture_count_++;
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;
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;
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;
227 int EventCountDelegate::GetGestureCountAndReset() {
228 int gesture_count = gesture_count_;
229 gesture_count_ = 0;
230 return gesture_count;
233 } // namespace test
234 } // namespace aura