Home | History | Annotate | Download | only in views
      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 "base/message_loop/message_loop.h"
      6 #include "base/strings/utf_string_conversions.h"
      7 #include "chrome/test/base/interactive_test_utils.h"
      8 #include "chrome/test/base/ui_test_utils.h"
      9 #include "chrome/test/base/view_event_test_base.h"
     10 #include "ui/base/models/simple_menu_model.h"
     11 #include "ui/base/test/ui_controls.h"
     12 #include "ui/views/controls/button/button_dropdown.h"
     13 
     14 class ButtonDropDownDragTest : public ViewEventTestBase,
     15                                ui::SimpleMenuModel::Delegate {
     16  public:
     17   ButtonDropDownDragTest()
     18       : button_(NULL),
     19         menu_shown_(false),
     20         menu_closed_(false) {
     21   }
     22 
     23   virtual ~ButtonDropDownDragTest() {
     24   }
     25 
     26   // ViewEventTestBase implementation.
     27   virtual void SetUp() OVERRIDE {
     28     button_ = new views::ButtonDropDown(NULL, new ui::SimpleMenuModel(this));
     29 
     30     ViewEventTestBase::SetUp();
     31   }
     32 
     33   virtual void TearDown() OVERRIDE {
     34     ViewEventTestBase::TearDown();
     35   }
     36 
     37   virtual views::View* CreateContentsView() OVERRIDE {
     38     return button_;
     39   }
     40 
     41   virtual gfx::Size GetPreferredSize() OVERRIDE {
     42     return button_->GetPreferredSize();
     43   }
     44 
     45   // ui::SimpleMenuModel::Delegate implementation.
     46   virtual bool IsCommandIdChecked(int id) const OVERRIDE {
     47     return false;
     48   }
     49 
     50   virtual bool IsCommandIdEnabled(int id) const OVERRIDE {
     51     return true;
     52   }
     53 
     54   virtual bool GetAcceleratorForCommandId(
     55       int id,
     56       ui::Accelerator* accelerator) OVERRIDE {
     57     return false;
     58   }
     59 
     60   virtual void ExecuteCommand(int id, int event_flags) OVERRIDE {
     61   }
     62 
     63   virtual void MenuWillShow(ui::SimpleMenuModel* /*source*/) OVERRIDE {
     64     menu_shown_ = true;
     65   }
     66 
     67   virtual void MenuClosed(ui::SimpleMenuModel* /*source*/) OVERRIDE {
     68     menu_closed_ = true;
     69   }
     70 
     71   // ViewEventTestBase implementation.
     72   virtual void DoTestOnMessageLoop() OVERRIDE {
     73     // Click on the ButtonDropDown.
     74     ui_test_utils::MoveMouseToCenterAndPress(
     75         button_,
     76         ui_controls::LEFT,
     77         ui_controls::DOWN,
     78         CreateEventTask(this, &ButtonDropDownDragTest::Step1));
     79   }
     80 
     81   void Step1() {
     82     // Drag to invoke the menu.
     83     gfx::Point view_center;
     84     views::View::ConvertPointToScreen(button_, &view_center);
     85     // The 50 is a bit arbitrary. We just need a value greater than the drag
     86     // threshold.
     87     ui_controls::SendMouseMoveNotifyWhenDone(
     88         view_center.x(), view_center.y() + 50,
     89         CreateEventTask(this, &ButtonDropDownDragTest::Step2));
     90   }
     91 
     92   void Step2() {
     93     ASSERT_TRUE(menu_shown_);
     94 
     95     // Release.
     96     ui_controls::SendMouseEventsNotifyWhenDone(
     97         ui_controls::LEFT,
     98         ui_controls::UP,
     99         CreateEventTask(this, &ButtonDropDownDragTest::Step3));
    100   }
    101 
    102   void Step3() {
    103     // Click mouse to dismiss menu.  The views menu does not dismiss the
    104     // menu on click-drag-release unless an item is selected.
    105     ui_test_utils::MoveMouseToCenterAndPress(
    106         button_,
    107         ui_controls::LEFT,
    108         ui_controls::DOWN | ui_controls::UP,
    109         CreateEventTask(this, &ButtonDropDownDragTest::Step4));
    110   }
    111 
    112   void Step4() {
    113     // One more hop is required because ui::SimpleMenuModel calls
    114     // ui::SimpleMenuModel::Delegate::MenuClosed() via a posted
    115     // task.
    116     base::MessageLoopForUI::current()->PostTask(
    117         FROM_HERE, CreateEventTask(this, &ButtonDropDownDragTest::Step5));
    118   }
    119 
    120   void Step5() {
    121     ASSERT_TRUE(menu_closed_);
    122     Done();
    123   }
    124 
    125  private:
    126   views::ButtonDropDown* button_;
    127   bool menu_shown_;
    128   bool menu_closed_;
    129 };
    130 
    131 #if defined(OS_WIN)
    132 #define MAYBE_DragActivation DISABLED_DragActivation
    133 #else
    134 #define MAYBE_DragActivation DragActivation
    135 #endif
    136 VIEW_TEST(ButtonDropDownDragTest, MAYBE_DragActivation)
    137