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"
13 #include "ui/views/widget/widget.h"
15 MenuTestBase::MenuTestBase()
16 : ViewEventTestBase(),
22 MenuTestBase::~MenuTestBase() {
25 void MenuTestBase::Click(views::View
* view
, const base::Closure
& next
) {
26 ui_test_utils::MoveMouseToCenterAndPress(
29 ui_controls::DOWN
| ui_controls::UP
,
33 void MenuTestBase::KeyPress(ui::KeyboardCode keycode
,
34 const base::Closure
& next
) {
35 ui_controls::SendKeyPressNotifyWhenDone(
36 GetWidget()->GetNativeWindow(), keycode
, false, false,
40 int MenuTestBase::GetMenuRunnerFlags() {
41 return views::MenuRunner::HAS_MNEMONICS
;
44 void MenuTestBase::SetUp() {
45 button_
= new views::MenuButton(
46 NULL
, base::ASCIIToUTF16("Menu Test"), this, true);
47 menu_
= new views::MenuItemView(this);
49 menu_runner_
.reset(new views::MenuRunner(menu_
, GetMenuRunnerFlags()));
51 ViewEventTestBase::SetUp();
54 void MenuTestBase::TearDown() {
55 // We cancel the menu first because certain operations (like a menu opened
56 // with views::MenuRunner::FOR_DROP) don't take kindly to simply pulling the
57 // runner out from under them.
58 menu_runner_
->Cancel();
62 ViewEventTestBase::TearDown();
65 views::View
* MenuTestBase::CreateContentsView() {
69 void MenuTestBase::DoTestOnMessageLoop() {
70 Click(button_
, CreateEventTask(this, &MenuTestBase::DoTestWithMenuOpen
));
73 gfx::Size
MenuTestBase::GetPreferredSize() const {
74 return button_
->GetPreferredSize();
77 void MenuTestBase::OnMenuButtonClicked(views::View
* source
,
78 const gfx::Point
& point
) {
79 gfx::Point screen_location
;
80 views::View::ConvertPointToScreen(source
, &screen_location
);
81 gfx::Rect
bounds(screen_location
, source
->size());
82 ignore_result(menu_runner_
->RunMenuAt(source
->GetWidget(),
85 views::MENU_ANCHOR_TOPLEFT
,
86 ui::MENU_SOURCE_NONE
));
89 void MenuTestBase::ExecuteCommand(int id
) {