Home | History | Annotate | Download | only in test
      1 // Copyright 2013 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/views/test/widget_test.h"
      6 
      7 #include "ui/gfx/native_widget_types.h"
      8 #include "ui/views/widget/root_view.h"
      9 
     10 namespace views {
     11 namespace test {
     12 
     13 // A widget that assumes mouse capture always works. It won't in testing, so we
     14 // mock it.
     15 NativeWidgetCapture::NativeWidgetCapture(
     16     internal::NativeWidgetDelegate* delegate)
     17     : PlatformNativeWidget(delegate),
     18       mouse_capture_(false) {}
     19 
     20 NativeWidgetCapture::~NativeWidgetCapture() {}
     21 
     22 void NativeWidgetCapture::SetCapture() {
     23   mouse_capture_ = true;
     24 }
     25 
     26 void NativeWidgetCapture::ReleaseCapture() {
     27   if (mouse_capture_)
     28     delegate()->OnMouseCaptureLost();
     29   mouse_capture_ = false;
     30 }
     31 
     32 bool NativeWidgetCapture::HasCapture() const {
     33   return mouse_capture_;
     34 }
     35 
     36 WidgetTest::WidgetTest() {}
     37 WidgetTest::~WidgetTest() {}
     38 
     39 NativeWidget* WidgetTest::CreatePlatformNativeWidget(
     40     internal::NativeWidgetDelegate* delegate) {
     41   return new NativeWidgetCapture(delegate);
     42 }
     43 
     44 Widget* WidgetTest::CreateTopLevelPlatformWidget() {
     45   Widget* toplevel = new Widget;
     46   Widget::InitParams toplevel_params =
     47       CreateParams(Widget::InitParams::TYPE_WINDOW);
     48   toplevel_params.native_widget = CreatePlatformNativeWidget(toplevel);
     49   toplevel->Init(toplevel_params);
     50   return toplevel;
     51 }
     52 
     53 Widget* WidgetTest::CreateTopLevelFramelessPlatformWidget() {
     54   Widget* toplevel = new Widget;
     55   Widget::InitParams toplevel_params =
     56       CreateParams(Widget::InitParams::TYPE_WINDOW_FRAMELESS);
     57   toplevel_params.native_widget = CreatePlatformNativeWidget(toplevel);
     58   toplevel->Init(toplevel_params);
     59   return toplevel;
     60 }
     61 
     62 Widget* WidgetTest::CreateChildPlatformWidget(
     63     gfx::NativeView parent_native_view) {
     64   Widget* child = new Widget;
     65   Widget::InitParams child_params =
     66       CreateParams(Widget::InitParams::TYPE_CONTROL);
     67   child_params.native_widget = CreatePlatformNativeWidget(child);
     68   child_params.parent = parent_native_view;
     69   child->Init(child_params);
     70   child->SetContentsView(new View);
     71   return child;
     72 }
     73 
     74 Widget* WidgetTest::CreateTopLevelNativeWidget() {
     75   Widget* toplevel = new Widget;
     76   Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW);
     77   toplevel->Init(params);
     78   return toplevel;
     79 }
     80 
     81 Widget* WidgetTest::CreateChildNativeWidgetWithParent(Widget* parent) {
     82   Widget* child = new Widget;
     83   Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_CONTROL);
     84   params.parent = parent->GetNativeView();
     85   child->Init(params);
     86   child->SetContentsView(new View);
     87   return child;
     88 }
     89 
     90 Widget* WidgetTest::CreateChildNativeWidget() {
     91   return CreateChildNativeWidgetWithParent(NULL);
     92 }
     93 
     94 View* WidgetTest::GetMousePressedHandler(internal::RootView* root_view) {
     95   return root_view->mouse_pressed_handler_;
     96 }
     97 
     98 View* WidgetTest::GetMouseMoveHandler(internal::RootView* root_view) {
     99   return root_view->mouse_move_handler_;
    100 }
    101 
    102 View* WidgetTest::GetGestureHandler(internal::RootView* root_view) {
    103   return root_view->gesture_handler_;
    104 }
    105 
    106 }  // namespace test
    107 }  // namespace views
    108