• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #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/aura/window.h"
10 #include "ui/base/test/ui_controls.h"
11 #include "ui/views/controls/button/menu_button.h"
12 #include "ui/views/controls/menu/menu_item_view.h"
13 #include "ui/views/controls/menu/menu_runner.h"
14 
MenuTestBase()15 MenuTestBase::MenuTestBase()
16     : ViewEventTestBase(),
17       button_(NULL),
18       menu_(NULL),
19       last_command_(0) {
20 }
21 
~MenuTestBase()22 MenuTestBase::~MenuTestBase() {
23 }
24 
Click(views::View * view,const base::Closure & next)25 void MenuTestBase::Click(views::View* view, const base::Closure& next) {
26   ui_test_utils::MoveMouseToCenterAndPress(
27       view,
28       ui_controls::LEFT,
29       ui_controls::DOWN | ui_controls::UP,
30       next);
31 }
32 
KeyPress(ui::KeyboardCode keycode,const base::Closure & next)33 void MenuTestBase::KeyPress(ui::KeyboardCode keycode,
34                             const base::Closure& next) {
35   ui_controls::SendKeyPressNotifyWhenDone(
36       GetWidget()->GetNativeView()->GetRootWindow(), keycode, false, false,
37       false, false, next);
38 }
39 
SetUp()40 void MenuTestBase::SetUp() {
41   button_ = new views::MenuButton(
42       NULL, base::ASCIIToUTF16("Menu Test"), this, true);
43   menu_ = new views::MenuItemView(this);
44   BuildMenu(menu_);
45   menu_runner_.reset(new views::MenuRunner(menu_));
46 
47   ViewEventTestBase::SetUp();
48 }
49 
TearDown()50 void MenuTestBase::TearDown() {
51   menu_runner_.reset();
52   menu_ = NULL;
53   ViewEventTestBase::TearDown();
54 }
55 
CreateContentsView()56 views::View* MenuTestBase::CreateContentsView() {
57   return button_;
58 }
59 
DoTestOnMessageLoop()60 void MenuTestBase::DoTestOnMessageLoop() {
61   Click(button_, CreateEventTask(this, &MenuTestBase::DoTestWithMenuOpen));
62 }
63 
GetPreferredSize() const64 gfx::Size MenuTestBase::GetPreferredSize() const {
65   return button_->GetPreferredSize();
66 }
67 
OnMenuButtonClicked(views::View * source,const gfx::Point & point)68 void MenuTestBase::OnMenuButtonClicked(views::View* source,
69                                        const gfx::Point& point) {
70   gfx::Point screen_location;
71   views::View::ConvertPointToScreen(source, &screen_location);
72   gfx::Rect bounds(screen_location, source->size());
73   ignore_result(menu_runner_->RunMenuAt(source->GetWidget(),
74                                         button_,
75                                         bounds,
76                                         views::MENU_ANCHOR_TOPLEFT,
77                                         ui::MENU_SOURCE_NONE,
78                                         views::MenuRunner::HAS_MNEMONICS));
79 }
80 
ExecuteCommand(int id)81 void MenuTestBase::ExecuteCommand(int id) {
82   last_command_ = id;
83 }
84