summaryrefslogtreecommitdiff
path: root/chromium/ui/views/selection_controller_unittest.cc
blob: e28d53020299d9f04a18b7d33eb8fb1fdc1e02ff (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright 2018 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/views/selection_controller.h"
#include "base/macros.h"
#include "base/strings/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/events/event.h"
#include "ui/events/event_constants.h"
#include "ui/gfx/render_text.h"
#include "ui/views/metrics.h"
#include "ui/views/selection_controller_delegate.h"
#include "ui/views/style/platform_style.h"

namespace views {
namespace {

const gfx::Point CenterLeft(const gfx::Rect& rect) {
  return gfx::Point(rect.x(), rect.CenterPoint().y());
}

const gfx::Point CenterRight(const gfx::Rect& rect) {
  return gfx::Point(rect.right(), rect.CenterPoint().y());
}

class TestSelectionControllerDelegate : public SelectionControllerDelegate {
 public:
  TestSelectionControllerDelegate(gfx::RenderText* render_text)
      : render_text_(render_text) {}
  ~TestSelectionControllerDelegate() override = default;

  gfx::RenderText* GetRenderTextForSelectionController() override {
    return render_text_;
  }

  bool IsReadOnly() const override { return true; }
  bool SupportsDrag() const override { return true; }
  bool HasTextBeingDragged() const override { return false; }
  void SetTextBeingDragged(bool value) override {}
  int GetViewHeight() const override {
    return render_text_->GetStringSize().height();
  }
  int GetViewWidth() const override {
    return render_text_->GetStringSize().width();
  }
  int GetDragSelectionDelay() const override { return 0; }
  void OnBeforePointerAction() override {}
  void OnAfterPointerAction(bool text_changed,
                            bool selection_changed) override {}
  bool PasteSelectionClipboard() override { return false; }
  void UpdateSelectionClipboard() override {}

 private:
  gfx::RenderText* render_text_;

  DISALLOW_COPY_AND_ASSIGN(TestSelectionControllerDelegate);
};

class SelectionControllerTest : public ::testing::Test {
 public:
  void SetUp() override {
    render_text_ = gfx::RenderText::CreateHarfBuzzInstance();
    delegate_ =
        std::make_unique<TestSelectionControllerDelegate>(render_text_.get());
    controller_ = std::make_unique<SelectionController>(delegate_.get());
  }

  SelectionControllerTest() = default;
  ~SelectionControllerTest() override = default;

  void SetText(const std::string& text) {
    render_text_->SetText(base::ASCIIToUTF16(text));
  }

  std::string GetSelectedText() {
    return base::UTF16ToASCII(
        render_text_->GetTextFromRange(render_text_->selection()));
  }

  void LeftMouseDown(const gfx::Point& location, bool focused = false) {
    PressMouseButton(location, ui::EF_LEFT_MOUSE_BUTTON, focused);
  }

  void LeftMouseUp() { ReleaseMouseButton(ui::EF_LEFT_MOUSE_BUTTON); }

  void DragMouse(const gfx::Point& location) {
    mouse_location_ = location;
    controller_->OnMouseDragged(ui::MouseEvent(ui::ET_MOUSE_DRAGGED, location,
                                               location, last_event_time_,
                                               mouse_flags_, 0));
  }

  void RightMouseDown(const gfx::Point& location, bool focused = false) {
    PressMouseButton(location, ui::EF_RIGHT_MOUSE_BUTTON, focused);
  }

  void RightMouseUp() { ReleaseMouseButton(ui::EF_RIGHT_MOUSE_BUTTON); }

  const gfx::Rect BoundsOfChar(int index) {
    return render_text_->GetSubstringBounds(gfx::Range(index, index + 1))[0];
  }

 private:
  void PressMouseButton(const gfx::Point& location, int button, bool focused) {
    DCHECK(!(mouse_flags_ & button));
    mouse_flags_ |= button;
    mouse_location_ = location;
    // Ensure that mouse presses are spaced apart by at least the double-click
    // interval to avoid triggering a double-click.
    last_event_time_ +=
        base::TimeDelta::FromMilliseconds(views::GetDoubleClickInterval() + 1);
    controller_->OnMousePressed(
        ui::MouseEvent(ui::ET_MOUSE_PRESSED, location, location,
                       last_event_time_, mouse_flags_, button),
        false,
        focused ? SelectionController::FOCUSED
                : SelectionController::UNFOCUSED);
  }

  void ReleaseMouseButton(int button) {
    DCHECK(mouse_flags_ & button);
    mouse_flags_ &= ~button;
    controller_->OnMouseReleased(
        ui::MouseEvent(ui::ET_MOUSE_RELEASED, mouse_location_, mouse_location_,
                       last_event_time_, mouse_flags_, button));
  }

  std::unique_ptr<gfx::RenderText> render_text_;
  std::unique_ptr<TestSelectionControllerDelegate> delegate_;
  std::unique_ptr<SelectionController> controller_;

  int mouse_flags_ = 0;
  gfx::Point mouse_location_;
  base::TimeTicks last_event_time_;

  DISALLOW_COPY_AND_ASSIGN(SelectionControllerTest);
};

TEST_F(SelectionControllerTest, ClickAndDragToSelect) {
  SetText("abc def");
  EXPECT_EQ("", GetSelectedText());

  LeftMouseDown(CenterLeft(BoundsOfChar(0)));
  DragMouse(CenterRight(BoundsOfChar(0)));
  EXPECT_EQ("a", GetSelectedText());

  DragMouse(CenterRight(BoundsOfChar(2)));
  EXPECT_EQ("abc", GetSelectedText());

  LeftMouseUp();
  EXPECT_EQ("abc", GetSelectedText());

  LeftMouseDown(CenterRight(BoundsOfChar(3)));
  EXPECT_EQ("", GetSelectedText());

  DragMouse(CenterRight(BoundsOfChar(4)));
  EXPECT_EQ("d", GetSelectedText());
}

TEST_F(SelectionControllerTest, RightClickWhenUnfocused) {
  SetText("abc def");

  RightMouseDown(CenterRight(BoundsOfChar(0)));
  if (PlatformStyle::kSelectAllOnRightClickWhenUnfocused)
    EXPECT_EQ("abc def", GetSelectedText());
  else
    EXPECT_EQ("", GetSelectedText());
}

TEST_F(SelectionControllerTest, RightClickSelectsWord) {
  SetText("abc def");
  RightMouseDown(CenterRight(BoundsOfChar(5)), true);
  if (PlatformStyle::kSelectWordOnRightClick)
    EXPECT_EQ("def", GetSelectedText());
  else
    EXPECT_EQ("", GetSelectedText());
}

// Regression test for https://crbug.com/856609
TEST_F(SelectionControllerTest, RightClickPastEndDoesntSelectLastWord) {
  SetText("abc def");

  RightMouseDown(CenterRight(BoundsOfChar(6)), true);
  EXPECT_EQ("", GetSelectedText());
}

}  // namespace
}  // namespace views