Home | History | Annotate | Download | only in test
      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/events/event.h"
     10 #include "ui/base/hit_test.h"
     11 #include "ui/gfx/canvas.h"
     12 #include "ui/gfx/path.h"
     13 #include "ui/gfx/skia_util.h"
     14 
     15 namespace aura {
     16 namespace test {
     17 
     18 ////////////////////////////////////////////////////////////////////////////////
     19 // TestWindowDelegate
     20 
     21 TestWindowDelegate::TestWindowDelegate()
     22     : window_component_(HTCLIENT),
     23       delete_on_destroyed_(false),
     24       can_focus_(true) {
     25 }
     26 
     27 TestWindowDelegate::~TestWindowDelegate() {
     28 }
     29 
     30 // static
     31 TestWindowDelegate* TestWindowDelegate::CreateSelfDestroyingDelegate() {
     32   TestWindowDelegate* delegate = new TestWindowDelegate;
     33   delegate->delete_on_destroyed_ = true;
     34   return delegate;
     35 }
     36 
     37 gfx::Size TestWindowDelegate::GetMinimumSize() const {
     38   return minimum_size_;
     39 }
     40 
     41 gfx::Size TestWindowDelegate::GetMaximumSize() const {
     42   return maximum_size_;
     43 }
     44 
     45 void TestWindowDelegate::OnBoundsChanged(const gfx::Rect& old_bounds,
     46                                          const gfx::Rect& new_bounds) {
     47 }
     48 
     49 gfx::NativeCursor TestWindowDelegate::GetCursor(const gfx::Point& point) {
     50   return gfx::kNullCursor;
     51 }
     52 
     53 int TestWindowDelegate::GetNonClientComponent(const gfx::Point& point) const {
     54   return window_component_;
     55 }
     56 
     57 bool TestWindowDelegate::ShouldDescendIntoChildForEventHandling(
     58       Window* child,
     59       const gfx::Point& location) {
     60   return true;
     61 }
     62 
     63 bool TestWindowDelegate::CanFocus() {
     64   return can_focus_;
     65 }
     66 
     67 void TestWindowDelegate::OnCaptureLost() {
     68 }
     69 
     70 void TestWindowDelegate::OnPaint(gfx::Canvas* canvas) {
     71 }
     72 
     73 void TestWindowDelegate::OnDeviceScaleFactorChanged(
     74     float device_scale_factor) {
     75 }
     76 
     77 void TestWindowDelegate::OnWindowDestroying() {
     78 }
     79 
     80 void TestWindowDelegate::OnWindowDestroyed() {
     81   if (delete_on_destroyed_)
     82     delete this;
     83 }
     84 
     85 void TestWindowDelegate::OnWindowTargetVisibilityChanged(bool visible) {
     86 }
     87 
     88 bool TestWindowDelegate::HasHitTestMask() const {
     89   return false;
     90 }
     91 
     92 void TestWindowDelegate::GetHitTestMask(gfx::Path* mask) const {
     93 }
     94 
     95 scoped_refptr<ui::Texture> TestWindowDelegate::CopyTexture() {
     96   return scoped_refptr<ui::Texture>();
     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() {
    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 }
    150 
    151 void EventCountDelegate::OnKeyEvent(ui::KeyEvent* event) {
    152   switch (event->type()) {
    153     case ui::ET_KEY_PRESSED:
    154       key_press_count_++;
    155       break;
    156     case ui::ET_KEY_RELEASED:
    157       key_release_count_++;
    158     default:
    159       break;
    160   }
    161 }
    162 
    163 void EventCountDelegate::OnMouseEvent(ui::MouseEvent* event) {
    164   switch (event->type()) {
    165     case ui::ET_MOUSE_MOVED:
    166       mouse_move_count_++;
    167       break;
    168     case ui::ET_MOUSE_ENTERED:
    169       mouse_enter_count_++;
    170       break;
    171     case ui::ET_MOUSE_EXITED:
    172       mouse_leave_count_++;
    173       break;
    174     case ui::ET_MOUSE_PRESSED:
    175       mouse_press_count_++;
    176       break;
    177     case ui::ET_MOUSE_RELEASED:
    178       mouse_release_count_++;
    179       break;
    180     default:
    181       break;
    182   }
    183 }
    184 
    185 std::string EventCountDelegate::GetMouseMotionCountsAndReset() {
    186   std::string result = base::StringPrintf("%d %d %d",
    187                                           mouse_enter_count_,
    188                                           mouse_move_count_,
    189                                           mouse_leave_count_);
    190   mouse_enter_count_ = 0;
    191   mouse_move_count_ = 0;
    192   mouse_leave_count_ = 0;
    193   return result;
    194 }
    195 
    196 std::string EventCountDelegate::GetMouseButtonCountsAndReset() {
    197   std::string result = base::StringPrintf("%d %d",
    198                                           mouse_press_count_,
    199                                           mouse_release_count_);
    200   mouse_press_count_ = 0;
    201   mouse_release_count_ = 0;
    202   return result;
    203 }
    204 
    205 
    206 std::string EventCountDelegate::GetKeyCountsAndReset() {
    207   std::string result = base::StringPrintf("%d %d",
    208                                           key_press_count_,
    209                                           key_release_count_);
    210   key_press_count_ = 0;
    211   key_release_count_ = 0;
    212   return result;
    213 }
    214 
    215 }  // namespace test
    216 }  // namespace aura
    217