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 15namespace views { 16class MenuButton; 17class MenuItemView; 18class 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. 35class 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 // Returns a bitmask of flags to use when creating the |menu_runner_|. By 64 // default, this is only views::MenuRunner::HAS_MNEMONICS. 65 virtual int GetMenuRunnerFlags(); 66 67 // ViewEventTestBase implementation. 68 virtual void SetUp() OVERRIDE; 69 virtual void TearDown() OVERRIDE; 70 virtual views::View* CreateContentsView() OVERRIDE; 71 virtual void DoTestOnMessageLoop() OVERRIDE; 72 virtual gfx::Size GetPreferredSize() const OVERRIDE; 73 74 // views::MenuButtonListener implementation 75 virtual void OnMenuButtonClicked(views::View* source, 76 const gfx::Point& point) OVERRIDE; 77 78 // views::MenuDelegate implementation 79 virtual void ExecuteCommand(int id) OVERRIDE; 80 81 private: 82 views::MenuButton* button_; 83 views::MenuItemView* menu_; 84 scoped_ptr<views::MenuRunner> menu_runner_; 85 86 // The command id of the last pressed menu item since the menu was opened. 87 int last_command_; 88 89 DISALLOW_COPY_AND_ASSIGN(MenuTestBase); 90}; 91 92#endif // CHROME_BROWSER_UI_VIEWS_MENU_TEST_BASE_H_ 93