1 // Copyright 2015 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 "ui/strings/grit/ui_strings.h"
6 #include "ui/touch_selection/touch_selection_menu_runner.h"
7 #include "ui/views/test/views_test_base.h"
8 #include "ui/views/touchui/touch_selection_menu_runner_views.h"
9 #include "ui/views/views_delegate.h"
14 // Should match |kMenuButtonWidth| in touch_selection_menu_runner_views.cc.
15 const int kMenuButtonWidth
= 63;
17 // Should match size of |kMenuCommands| array in
18 // touch_selection_menu_runner_views.cc.
19 const int kMenuCommandCount
= 3;
23 class TouchSelectionMenuRunnerViewsTest
: public ViewsTestBase
,
24 public ui::TouchSelectionMenuClient
{
26 TouchSelectionMenuRunnerViewsTest() : no_command_available_(false) {}
27 ~TouchSelectionMenuRunnerViewsTest() override
{}
30 void set_no_commmand_available(bool no_command
) {
31 no_command_available_
= no_command
;
34 gfx::Rect
GetMenuAnchorRect() {
35 TouchSelectionMenuRunnerViews
* menu_runner
=
36 static_cast<TouchSelectionMenuRunnerViews
*>(
37 ui::TouchSelectionMenuRunner::GetInstance());
38 return menu_runner
->GetAnchorRectForTest();
42 // ui::TouchSelectionMenuClient:
43 bool IsCommandIdEnabled(int command_id
) const override
{
44 return !no_command_available_
;
47 void ExecuteCommand(int command_id
, int event_flags
) override
{}
49 void RunContextMenu() override
{}
51 // When set to true, no command would be availble and menu should not be
53 bool no_command_available_
;
55 DISALLOW_COPY_AND_ASSIGN(TouchSelectionMenuRunnerViewsTest
);
58 TEST_F(TouchSelectionMenuRunnerViewsTest
, InstalledAndWorksProperly
) {
59 gfx::Rect
menu_anchor(0, 0, 10, 10);
60 gfx::Size
handle_size(10, 10);
62 // Menu runner instance should be installed, but no menu should be running.
63 EXPECT_TRUE(ui::TouchSelectionMenuRunner::GetInstance());
64 EXPECT_FALSE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
66 // Run menu. Since commands are availble, this should bring up menus.
67 ui::TouchSelectionMenuRunner::GetInstance()->OpenMenu(
68 this, menu_anchor
, handle_size
, GetContext());
69 EXPECT_TRUE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
72 ui::TouchSelectionMenuRunner::GetInstance()->CloseMenu();
74 EXPECT_FALSE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
76 // Try running menu when no commands is available. Menu should not be shown.
77 set_no_commmand_available(true);
78 ui::TouchSelectionMenuRunner::GetInstance()->OpenMenu(
79 this, menu_anchor
, handle_size
, GetContext());
80 EXPECT_FALSE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
83 // Tests if anchor rect for the quick menu is adjusted correctly based on the
84 // distance of handles.
85 TEST_F(TouchSelectionMenuRunnerViewsTest
, QuickMenuAdjustsAnchorRect
) {
86 gfx::Size
handle_size(10, 10);
88 // Calculate the width of quick menu. In addition to |kMenuCommandCount|
89 // commands, there is an item for ellipsis.
90 int quick_menu_width
=
91 (kMenuCommandCount
+ 1) * kMenuButtonWidth
+ kMenuCommandCount
;
93 // Set anchor rect's width a bit smaller than the quick menu width plus handle
94 // image width and check that anchor rect's height is adjusted.
95 gfx::Rect
anchor_rect(0, 0, quick_menu_width
+ handle_size
.width() - 10, 20);
96 ui::TouchSelectionMenuRunner::GetInstance()->OpenMenu(
97 this, anchor_rect
, handle_size
, GetContext());
98 anchor_rect
.Inset(0, 0, 0, -handle_size
.height());
99 EXPECT_EQ(anchor_rect
, GetMenuAnchorRect());
101 // Set anchor rect's width a bit greater than the quick menu width plus handle
102 // image width and check that anchor rect's height is not adjusted.
104 gfx::Rect(0, 0, quick_menu_width
+ handle_size
.width() + 10, 20);
105 ui::TouchSelectionMenuRunner::GetInstance()->OpenMenu(
106 this, anchor_rect
, handle_size
, GetContext());
107 EXPECT_EQ(anchor_rect
, GetMenuAnchorRect());