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"
13 // Should match |kMenuButtonWidth| in touch_selection_menu_runner_views.cc.
14 const int kMenuButtonWidth
= 63;
16 // Should match size of |kMenuCommands| array in
17 // touch_selection_menu_runner_views.cc.
18 const int kMenuCommandCount
= 3;
22 class TouchSelectionMenuRunnerViewsTest
: public ViewsTestBase
,
23 public ui::TouchSelectionMenuClient
{
25 TouchSelectionMenuRunnerViewsTest() : no_command_available_(false) {}
26 ~TouchSelectionMenuRunnerViewsTest() override
{}
29 void set_no_commmand_available(bool no_command
) {
30 no_command_available_
= no_command
;
33 gfx::Rect
GetMenuAnchorRect() {
34 TouchSelectionMenuRunnerViews
* menu_runner
=
35 static_cast<TouchSelectionMenuRunnerViews
*>(
36 ui::TouchSelectionMenuRunner::GetInstance());
37 return menu_runner
->GetAnchorRectForTest();
41 // ui::TouchSelectionMenuClient:
42 bool IsCommandIdEnabled(int command_id
) const override
{
43 return !no_command_available_
;
46 void ExecuteCommand(int command_id
, int event_flags
) override
{}
48 void RunContextMenu() override
{}
50 // When set to true, no command would be availble and menu should not be
52 bool no_command_available_
;
54 DISALLOW_COPY_AND_ASSIGN(TouchSelectionMenuRunnerViewsTest
);
57 TEST_F(TouchSelectionMenuRunnerViewsTest
, InstalledAndWorksProperly
) {
58 gfx::Rect
menu_anchor(0, 0, 10, 10);
59 gfx::Size
handle_size(10, 10);
61 // Menu runner instance should be installed, but no menu should be running.
62 EXPECT_TRUE(ui::TouchSelectionMenuRunner::GetInstance());
63 EXPECT_FALSE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
65 // Run menu. Since commands are availble, this should bring up menus.
66 ui::TouchSelectionMenuRunner::GetInstance()->OpenMenu(
67 this, menu_anchor
, handle_size
, GetContext());
68 EXPECT_TRUE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
71 ui::TouchSelectionMenuRunner::GetInstance()->CloseMenu();
73 EXPECT_FALSE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
75 // Try running menu when no commands is available. Menu should not be shown.
76 set_no_commmand_available(true);
77 ui::TouchSelectionMenuRunner::GetInstance()->OpenMenu(
78 this, menu_anchor
, handle_size
, GetContext());
79 EXPECT_FALSE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
82 // Tests if anchor rect for the quick menu is adjusted correctly based on the
83 // distance of handles.
84 TEST_F(TouchSelectionMenuRunnerViewsTest
, QuickMenuAdjustsAnchorRect
) {
85 gfx::Size
handle_size(10, 10);
87 // Calculate the width of quick menu. In addition to |kMenuCommandCount|
88 // commands, there is an item for ellipsis.
89 int quick_menu_width
=
90 (kMenuCommandCount
+ 1) * kMenuButtonWidth
+ kMenuCommandCount
;
92 // Set anchor rect's width a bit smaller than the quick menu width plus handle
93 // image width and check that anchor rect's height is adjusted.
94 gfx::Rect
anchor_rect(0, 0, quick_menu_width
+ handle_size
.width() - 10, 20);
95 ui::TouchSelectionMenuRunner::GetInstance()->OpenMenu(
96 this, anchor_rect
, handle_size
, GetContext());
97 anchor_rect
.Inset(0, 0, 0, -handle_size
.height());
98 EXPECT_EQ(anchor_rect
, GetMenuAnchorRect());
100 // Set anchor rect's width a bit greater than the quick menu width plus handle
101 // image width and check that anchor rect's height is not adjusted.
103 gfx::Rect(0, 0, quick_menu_width
+ handle_size
.width() + 10, 20);
104 ui::TouchSelectionMenuRunner::GetInstance()->OpenMenu(
105 this, anchor_rect
, handle_size
, GetContext());
106 EXPECT_EQ(anchor_rect
, GetMenuAnchorRect());
108 ui::TouchSelectionMenuRunner::GetInstance()->CloseMenu();
109 RunPendingMessages();