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 virtual ~MenuTestBase();
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 // 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
;
70 // views::MenuButtonListener implementation
71 virtual void OnMenuButtonClicked(views::View
* source
,
72 const gfx::Point
& point
) OVERRIDE
;
74 // views::MenuDelegate implementation
75 virtual void ExecuteCommand(int id
) OVERRIDE
;
78 views::MenuButton
* button_
;
79 views::MenuItemView
* menu_
;
80 scoped_ptr
<views::MenuRunner
> menu_runner_
;
82 // The command id of the last pressed menu item since the menu was opened.
85 DISALLOW_COPY_AND_ASSIGN(MenuTestBase
);
88 #endif // CHROME_BROWSER_UI_VIEWS_MENU_TEST_BASE_H_