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 #include "base/strings/utf_string_conversions.h"
6 #include "chrome/browser/ui/views/menu_test_base.h"
7 #include "chrome/test/base/interactive_test_utils.h"
8 #include "chrome/test/base/ui_test_utils.h"
9 #include "ui/base/test/ui_controls.h"
10 #include "ui/views/controls/button/menu_button.h"
11 #include "ui/views/controls/menu/menu_item_view.h"
12 #include "ui/views/controls/menu/menu_runner.h"
14 MenuTestBase::MenuTestBase()
15 : ViewEventTestBase(),
21 MenuTestBase::~MenuTestBase() {
24 void MenuTestBase::Click(views::View
* view
, const base::Closure
& next
) {
25 ui_test_utils::MoveMouseToCenterAndPress(
28 ui_controls::DOWN
| ui_controls::UP
,
32 void MenuTestBase::KeyPress(ui::KeyboardCode keycode
,
33 const base::Closure
& next
) {
34 ui_controls::SendKeyPressNotifyWhenDone(
35 GetWidget()->GetNativeWindow(), keycode
, false, false,
39 int MenuTestBase::GetMenuRunnerFlags() {
40 return views::MenuRunner::HAS_MNEMONICS
;
43 void MenuTestBase::SetUp() {
44 button_
= new views::MenuButton(
45 NULL
, base::ASCIIToUTF16("Menu Test"), this, true);
46 menu_
= new views::MenuItemView(this);
48 menu_runner_
.reset(new views::MenuRunner(menu_
, GetMenuRunnerFlags()));
50 ViewEventTestBase::SetUp();
53 void MenuTestBase::TearDown() {
54 // We cancel the menu first because certain operations (like a menu opened
55 // with views::MenuRunner::FOR_DROP) don't take kindly to simply pulling the
56 // runner out from under them.
57 menu_runner_
->Cancel();
61 ViewEventTestBase::TearDown();
64 views::View
* MenuTestBase::CreateContentsView() {
68 void MenuTestBase::DoTestOnMessageLoop() {
69 Click(button_
, CreateEventTask(this, &MenuTestBase::DoTestWithMenuOpen
));
72 gfx::Size
MenuTestBase::GetPreferredSize() const {
73 return button_
->GetPreferredSize();
76 void MenuTestBase::OnMenuButtonClicked(views::View
* source
,
77 const gfx::Point
& point
) {
78 gfx::Point screen_location
;
79 views::View::ConvertPointToScreen(source
, &screen_location
);
80 gfx::Rect
bounds(screen_location
, source
->size());
81 ignore_result(menu_runner_
->RunMenuAt(source
->GetWidget(),
84 views::MENU_ANCHOR_TOPLEFT
,
85 ui::MENU_SOURCE_NONE
));
88 void MenuTestBase::ExecuteCommand(int id
) {