1 // Copyright 2014 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 #ifndef CHROME_BROWSER_UI_VIEWS_MENU_TEST_BASE_H_ 6 #define CHROME_BROWSER_UI_VIEWS_MENU_TEST_BASE_H_ 7 8 #include "base/callback.h" 9 #include "base/memory/scoped_ptr.h" 10 #include "chrome/test/base/view_event_test_base.h" 11 #include "ui/events/keycodes/keyboard_codes.h" 12 #include "ui/views/controls/button/menu_button_listener.h" 13 #include "ui/views/controls/menu/menu_delegate.h" 14 15 namespace views { 16 class MenuButton; 17 class MenuItemView; 18 class MenuRunner; 19 } 20 21 // This is a convenience base class for menu related tests to provide some 22 // common functionality. 23 // 24 // Subclasses should implement: 25 // BuildMenu() populate the menu 26 // DoTestWithMenuOpen() initiate the test 27 // 28 // Subclasses can call: 29 // Click() to post a mouse click on a View 30 // KeyPress() to post a key press 31 // 32 // Although it should be possible to post a menu multiple times, 33 // MenuItemView prevents repeated activation of a menu by clicks too 34 // close in time. 35 class MenuTestBase : public ViewEventTestBase, 36 public views::MenuButtonListener, 37 public views::MenuDelegate { 38 public: 39 MenuTestBase(); 40 virtual ~MenuTestBase(); 41 42 // Generate a mouse click and run |next| once the event has been processed. 43 virtual void Click(views::View* view, const base::Closure& next); 44 45 // Generate a keypress and run |next| once the event has been processed. 46 void KeyPress(ui::KeyboardCode keycode, const base::Closure& next); 47 48 views::MenuItemView* menu() { 49 return menu_; 50 } 51 52 int last_command() const { 53 return last_command_; 54 } 55 56 protected: 57 // Called to populate the menu. 58 virtual void BuildMenu(views::MenuItemView* menu) = 0; 59 60 // Called once the menu is open. 61 virtual void DoTestWithMenuOpen() = 0; 62 63 // ViewEventTestBase implementation. 64 virtual void SetUp() OVERRIDE; 65 virtual void TearDown() OVERRIDE; 66 virtual views::View* CreateContentsView() OVERRIDE; 67 virtual void DoTestOnMessageLoop() OVERRIDE; 68 virtual gfx::Size GetPreferredSize() const OVERRIDE; 69 70 // views::MenuButtonListener implementation 71 virtual void OnMenuButtonClicked(views::View* source, 72 const gfx::Point& point) OVERRIDE; 73 74 // views::MenuDelegate implementation 75 virtual void ExecuteCommand(int id) OVERRIDE; 76 77 private: 78 views::MenuButton* button_; 79 views::MenuItemView* menu_; 80 scoped_ptr<views::MenuRunner> menu_runner_; 81 82 // The command id of the last pressed menu item since the menu was opened. 83 int last_command_; 84 85 DISALLOW_COPY_AND_ASSIGN(MenuTestBase); 86 }; 87 88 #endif // CHROME_BROWSER_UI_VIEWS_MENU_TEST_BASE_H_ 89