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.
5 #ifndef CHROME_BROWSER_UI_VIEWS_MENU_TEST_BASE_H_
6 #define CHROME_BROWSER_UI_VIEWS_MENU_TEST_BASE_H_
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"
21 // This is a convenience base class for menu related tests to provide some
22 // common functionality.
24 // Subclasses should implement:
25 // BuildMenu() populate the menu
26 // DoTestWithMenuOpen() initiate the test
28 // Subclasses can call:
29 // Click() to post a mouse click on a View
30 // KeyPress() to post a key press
32 // Although it should be possible to post a menu multiple times,
33 // MenuItemView prevents repeated activation of a menu by clicks too
35 class MenuTestBase
: public ViewEventTestBase
,
36 public views::MenuButtonListener
,
37 public views::MenuDelegate
{
40 ~MenuTestBase() override
;
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
);
45 // Generate a keypress and run |next| once the event has been processed.
46 void KeyPress(ui::KeyboardCode keycode
, const base::Closure
& next
);
48 views::MenuItemView
* menu() {
52 int last_command() const {
57 // Called to populate the menu.
58 virtual void BuildMenu(views::MenuItemView
* menu
) = 0;
60 // Called once the menu is open.
61 virtual void DoTestWithMenuOpen() = 0;
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();
67 // ViewEventTestBase implementation.
68 void SetUp() override
;
69 void TearDown() override
;
70 views::View
* CreateContentsView() override
;
71 void DoTestOnMessageLoop() override
;
72 gfx::Size
GetPreferredSize() const override
;
74 // views::MenuButtonListener implementation
75 void OnMenuButtonClicked(views::View
* source
,
76 const gfx::Point
& point
) override
;
78 // views::MenuDelegate implementation
79 void ExecuteCommand(int id
) override
;
82 views::MenuButton
* button_
;
83 views::MenuItemView
* menu_
;
84 scoped_ptr
<views::MenuRunner
> menu_runner_
;
86 // The command id of the last pressed menu item since the menu was opened.
89 DISALLOW_COPY_AND_ASSIGN(MenuTestBase
);
92 #endif // CHROME_BROWSER_UI_VIEWS_MENU_TEST_BASE_H_