summaryrefslogtreecommitdiffstats
path: root/ui/views/touchui/touch_selection_menu_runner_views_unittest.cc
blob: eee42581ec36647dcd517dc15755b180a97ebd4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/strings/grit/ui_strings.h"
#include "ui/touch_selection/touch_selection_menu_runner.h"
#include "ui/views/test/views_test_base.h"
#include "ui/views/touchui/touch_selection_menu_runner_views.h"

namespace views {
namespace {

// Should match |kMenuButtonWidth| in touch_selection_menu_runner_views.cc.
const int kMenuButtonWidth = 63;

// Should match size of |kMenuCommands| array in
// touch_selection_menu_runner_views.cc.
const int kMenuCommandCount = 3;

}

class TouchSelectionMenuRunnerViewsTest : public ViewsTestBase,
                                          public ui::TouchSelectionMenuClient {
 public:
  TouchSelectionMenuRunnerViewsTest() : no_command_available_(false) {}
  ~TouchSelectionMenuRunnerViewsTest() override {}

 protected:
  void set_no_commmand_available(bool no_command) {
    no_command_available_ = no_command;
  }

  gfx::Rect GetMenuAnchorRect() {
    TouchSelectionMenuRunnerViews* menu_runner =
        static_cast<TouchSelectionMenuRunnerViews*>(
            ui::TouchSelectionMenuRunner::GetInstance());
    return menu_runner->GetAnchorRectForTest();
  }

 private:
  // ui::TouchSelectionMenuClient:
  bool IsCommandIdEnabled(int command_id) const override {
    return !no_command_available_;
  }

  void ExecuteCommand(int command_id, int event_flags) override {}

  void RunContextMenu() override {}

  // When set to true, no command would be availble and menu should not be
  // shown.
  bool no_command_available_;

  DISALLOW_COPY_AND_ASSIGN(TouchSelectionMenuRunnerViewsTest);
};

TEST_F(TouchSelectionMenuRunnerViewsTest, InstalledAndWorksProperly) {
  gfx::Rect menu_anchor(0, 0, 10, 10);
  gfx::Size handle_size(10, 10);

  // Menu runner instance should be installed, but no menu should be running.
  EXPECT_TRUE(ui::TouchSelectionMenuRunner::GetInstance());
  EXPECT_FALSE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());

  // Run menu. Since commands are availble, this should bring up menus.
  ui::TouchSelectionMenuRunner::GetInstance()->OpenMenu(
      this, menu_anchor, handle_size, GetContext());
  EXPECT_TRUE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());

  // Close menu.
  ui::TouchSelectionMenuRunner::GetInstance()->CloseMenu();
  RunPendingMessages();
  EXPECT_FALSE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());

  // Try running menu when no commands is available. Menu should not be shown.
  set_no_commmand_available(true);
  ui::TouchSelectionMenuRunner::GetInstance()->OpenMenu(
      this, menu_anchor, handle_size, GetContext());
  EXPECT_FALSE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
}

// Tests if anchor rect for the quick menu is adjusted correctly based on the
// distance of handles.
TEST_F(TouchSelectionMenuRunnerViewsTest, QuickMenuAdjustsAnchorRect) {
  gfx::Size handle_size(10, 10);

  // Calculate the width of quick menu. In addition to |kMenuCommandCount|
  // commands, there is an item for ellipsis.
  int quick_menu_width =
      (kMenuCommandCount + 1) * kMenuButtonWidth + kMenuCommandCount;

  // Set anchor rect's width a bit smaller than the quick menu width plus handle
  // image width and check that anchor rect's height is adjusted.
  gfx::Rect anchor_rect(0, 0, quick_menu_width + handle_size.width() - 10, 20);
  ui::TouchSelectionMenuRunner::GetInstance()->OpenMenu(
      this, anchor_rect, handle_size, GetContext());
  anchor_rect.Inset(0, 0, 0, -handle_size.height());
  EXPECT_EQ(anchor_rect, GetMenuAnchorRect());

  // Set anchor rect's width a bit greater than the quick menu width plus handle
  // image width and check that anchor rect's height is not adjusted.
  anchor_rect =
      gfx::Rect(0, 0, quick_menu_width + handle_size.width() + 10, 20);
  ui::TouchSelectionMenuRunner::GetInstance()->OpenMenu(
      this, anchor_rect, handle_size, GetContext());
  EXPECT_EQ(anchor_rect, GetMenuAnchorRect());

  ui::TouchSelectionMenuRunner::GetInstance()->CloseMenu();
  RunPendingMessages();
}

}  // namespace views