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