File: textarea_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (369 lines) | stat: -rw-r--r-- 13,035 bytes parent folder | download | duplicates (6)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/views/controls/textarea/textarea.h"

#include <memory>
#include <string>
#include <vector>

#include "base/format_macros.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "ui/base/ui_base_features.h"
#include "ui/events/event.h"
#include "ui/gfx/render_text.h"
#include "ui/gfx/render_text_test_api.h"
#include "ui/strings/grit/ui_strings.h"
#include "ui/views/controls/textfield/textfield_test_api.h"
#include "ui/views/controls/textfield/textfield_unittest.h"
#include "ui/views/style/platform_style.h"
#include "ui/views/widget/widget.h"

namespace {

const char16_t kHebrewLetterSamekh = 0x05E1;

}  // namespace

namespace views {
namespace {

class TextareaTest : public test::TextfieldTest {
 public:
  TextareaTest() = default;

  TextareaTest(const TextareaTest&) = delete;
  TextareaTest& operator=(const TextareaTest&) = delete;

  ~TextareaTest() override = default;

  // TextfieldTest:
  void SetUp() override {
    scoped_feature_list_.InitWithFeatures(
        /*enabled_features=*/{::features::kTouchTextEditingRedesign},
        /*disabled_features=*/{});

    TextfieldTest::SetUp();

    ASSERT_FALSE(textarea_);
    textarea_ = PrepareTextfields(0, std::make_unique<Textarea>(),
                                  gfx::Rect(100, 100, 800, 100));
  }

 protected:
  void RunMoveUpDownTest(int start_index,
                         ui::KeyboardCode key_code,
                         std::vector<int> expected) {
    DCHECK(key_code == ui::VKEY_UP || key_code == ui::VKEY_DOWN);
    textarea_->SetSelectedRange(gfx::Range(start_index));
    for (size_t i = 0; i < expected.size(); ++i) {
      SCOPED_TRACE(testing::Message()
                   << (key_code == ui::VKEY_UP ? "MOVE UP " : "MOVE DOWN ")
                   << i + 1 << " times from Range " << start_index);
      SendKeyEvent(key_code);
      EXPECT_EQ(gfx::Range(expected[i]), textarea_->GetSelectedRange());
    }
  }

  size_t GetCursorLine() {
    return GetTextfieldTestApi().GetRenderText()->GetLineContainingCaret(
        textarea_->GetSelectionModel());
  }

  // TextfieldTest:
  TextfieldTestApi GetTextfieldTestApi() override {
    return TextfieldTestApi(textarea_);
  }

  void SendHomeEvent(bool shift) override {
    SendKeyEvent(ui::VKEY_HOME, shift, TestingNativeMac());
  }

  void SendEndEvent(bool shift) override {
    SendKeyEvent(ui::VKEY_END, shift, TestingNativeMac());
  }

  raw_ptr<Textarea> textarea_ = nullptr;
  base::test::ScopedFeatureList scoped_feature_list_;
};

}  // namespace

// Disabled when using XKB for crbug.com/1171828.
#if BUILDFLAG(USE_XKBCOMMON)
#define MAYBE_InsertNewlineTest DISABLED_InsertNewlineTest
#else
#define MAYBE_InsertNewlineTest InsertNewlineTest
#endif  // BUILDFLAG(USE_XKBCOMMON)
TEST_F(TextareaTest, MAYBE_InsertNewlineTest) {
  for (size_t i = 0; i < 5; i++) {
    SendKeyEvent(static_cast<ui::KeyboardCode>(ui::VKEY_A + i));
    SendKeyEvent(ui::VKEY_RETURN);
  }
  EXPECT_EQ(u"a\nb\nc\nd\ne\n", textarea_->GetText());
}

TEST_F(TextareaTest, PasteNewlineTest) {
  const std::u16string kText = u"abc\n   \n";
  textarea_->SetText(kText);
  textarea_->SelectAll(false);
  textarea_->ExecuteCommand(Textfield::kCopy, 0);
  textarea_->SetText(std::u16string());
  textarea_->ExecuteCommand(Textfield::kPaste, 0);
  EXPECT_EQ(kText, textarea_->GetText());
}

// Re-enable when crbug.com/1163587 is fixed.
TEST_F(TextareaTest, DISABLED_CursorMovement) {
  textarea_->SetText(u"one\n\ntwo three");

  // Move Up/Down at the front of the line.
  RunMoveUpDownTest(0, ui::VKEY_DOWN, {4, 5, 14});
  RunMoveUpDownTest(5, ui::VKEY_UP, {4, 0, 0});

  // Move Up/Down at the end of the line.
  RunMoveUpDownTest(3, ui::VKEY_DOWN, {4, 8, 14});
  RunMoveUpDownTest(14, ui::VKEY_UP, {4, 3, 0});

  // Move Up/Down at the middle position.
  RunMoveUpDownTest(2, ui::VKEY_DOWN, {4, 7, 14});
  RunMoveUpDownTest(7, ui::VKEY_UP, {4, 2, 0});

  // Test Home/End key on each lines.
  textarea_->SetSelectedRange(gfx::Range(2));  // First line.
  SendHomeEvent(false);
  EXPECT_EQ(gfx::Range(0), textarea_->GetSelectedRange());
  SendEndEvent(false);
  EXPECT_EQ(gfx::Range(3), textarea_->GetSelectedRange());
  textarea_->SetSelectedRange(gfx::Range(4));  // 2nd line.
  SendHomeEvent(false);
  EXPECT_EQ(gfx::Range(4), textarea_->GetSelectedRange());
  SendEndEvent(false);
  EXPECT_EQ(gfx::Range(4), textarea_->GetSelectedRange());
  textarea_->SetSelectedRange(gfx::Range(7));  // 3rd line.
  SendHomeEvent(false);
  EXPECT_EQ(gfx::Range(5), textarea_->GetSelectedRange());
  SendEndEvent(false);
  EXPECT_EQ(gfx::Range(14), textarea_->GetSelectedRange());
}

// Ensure cursor view is always inside display rect.
TEST_F(TextareaTest, CursorViewBounds) {
  textarea_->SetBounds(0, 0, 100, 31);
  for (size_t i = 0; i < 10; ++i) {
    SCOPED_TRACE(base::StringPrintf("VKEY_RETURN %" PRIuS " times", i + 1));
    SendKeyEvent(ui::VKEY_RETURN);
    ASSERT_TRUE(textarea_->GetVisibleBounds().Contains(GetCursorViewRect()));
    ASSERT_FALSE(GetCursorViewRect().size().IsEmpty());
  }

  for (size_t i = 0; i < 10; ++i) {
    SCOPED_TRACE(base::StringPrintf("VKEY_UP %" PRIuS " times", i + 1));
    SendKeyEvent(ui::VKEY_UP);
    ASSERT_TRUE(textarea_->GetVisibleBounds().Contains(GetCursorViewRect()));
    ASSERT_FALSE(GetCursorViewRect().size().IsEmpty());
  }
}

TEST_F(TextareaTest, LineSelection) {
  textarea_->SetText(u"12\n34567 89");

  // Place the cursor after "5".
  textarea_->SetEditableSelectionRange(gfx::Range(6));

  // Select line towards right.
  SendEndEvent(true);
  EXPECT_EQ(u"67 89", textarea_->GetSelectedText());

  // Select line towards left. On Mac, the existing selection should be extended
  // to cover the whole line.
  SendHomeEvent(true);

  if (Textarea::kLineSelectionBehavior == gfx::SELECTION_EXTEND) {
    EXPECT_EQ(u"34567 89", textarea_->GetSelectedText());
  } else {
    EXPECT_EQ(u"345", textarea_->GetSelectedText());
  }

  EXPECT_TRUE(textarea_->GetSelectedRange().is_reversed());

  // Select line towards right.
  SendEndEvent(true);

  if (Textarea::kLineSelectionBehavior == gfx::SELECTION_EXTEND) {
    EXPECT_EQ(u"34567 89", textarea_->GetSelectedText());
  } else {
    EXPECT_EQ(u"67 89", textarea_->GetSelectedText());
  }

  EXPECT_FALSE(textarea_->GetSelectedRange().is_reversed());
}

// Disabled on Mac for crbug.com/1171826.
#if BUILDFLAG(IS_MAC)
#define MAYBE_MoveUpDownAndModifySelection DISABLED_MoveUpDownAndModifySelection
#else
#define MAYBE_MoveUpDownAndModifySelection MoveUpDownAndModifySelection
#endif  // BUILDFLAG(IS_MAC)
TEST_F(TextareaTest, MAYBE_MoveUpDownAndModifySelection) {
  textarea_->SetText(u"12\n34567 89");
  textarea_->SetEditableSelectionRange(gfx::Range(6));
  EXPECT_EQ(1U, GetCursorLine());

  // Up key should place the cursor after "2" not after newline to place the
  // cursor on the first line.
  SendKeyEvent(ui::VKEY_UP);
  EXPECT_EQ(0U, GetCursorLine());
  EXPECT_EQ(gfx::Range(2), textarea_->GetSelectedRange());

  // Down key after Up key should select the same range as the previous one.
  SendKeyEvent(ui::VKEY_DOWN);
  EXPECT_EQ(1U, GetCursorLine());
  EXPECT_EQ(gfx::Range(6), textarea_->GetSelectedRange());

  // Shift+Up should select the text to the upper line position including
  // the newline character.
  SendKeyEvent(ui::VKEY_UP, true /* shift */, false /* command */);
  EXPECT_EQ(gfx::Range(6, 2), textarea_->GetSelectedRange());

  // Shift+Down should collapse the selection.
  SendKeyEvent(ui::VKEY_DOWN, true /* shift */, false /* command */);
  EXPECT_EQ(gfx::Range(6), textarea_->GetSelectedRange());

  // Shift+Down again should select the text to the end of the last line.
  SendKeyEvent(ui::VKEY_DOWN, true /* shift */, false /* command */);
  EXPECT_EQ(gfx::Range(6, 11), textarea_->GetSelectedRange());
}

TEST_F(TextareaTest, MovePageUpDownAndModifySelection) {
  textarea_->SetText(u"12\n34567 89");
  textarea_->SetEditableSelectionRange(gfx::Range(6));

  EXPECT_TRUE(
      textarea_->IsTextEditCommandEnabled(ui::TextEditCommand::MOVE_PAGE_UP));
  EXPECT_TRUE(
      textarea_->IsTextEditCommandEnabled(ui::TextEditCommand::MOVE_PAGE_DOWN));
  EXPECT_TRUE(textarea_->IsTextEditCommandEnabled(
      ui::TextEditCommand::MOVE_PAGE_UP_AND_MODIFY_SELECTION));
  EXPECT_TRUE(textarea_->IsTextEditCommandEnabled(
      ui::TextEditCommand::MOVE_PAGE_DOWN_AND_MODIFY_SELECTION));

  GetTextfieldTestApi().ExecuteTextEditCommand(
      ui::TextEditCommand::MOVE_PAGE_UP);
  EXPECT_EQ(gfx::Range(0), textarea_->GetSelectedRange());

  GetTextfieldTestApi().ExecuteTextEditCommand(
      ui::TextEditCommand::MOVE_PAGE_DOWN);
  EXPECT_EQ(gfx::Range(11), textarea_->GetSelectedRange());

  textarea_->SetEditableSelectionRange(gfx::Range(6));
  GetTextfieldTestApi().ExecuteTextEditCommand(
      ui::TextEditCommand::MOVE_PAGE_UP_AND_MODIFY_SELECTION);
  EXPECT_EQ(gfx::Range(6, 0), textarea_->GetSelectedRange());

  GetTextfieldTestApi().ExecuteTextEditCommand(
      ui::TextEditCommand::MOVE_PAGE_DOWN_AND_MODIFY_SELECTION);

  if (Textarea::kLineSelectionBehavior == gfx::SELECTION_EXTEND) {
    EXPECT_EQ(gfx::Range(0, 11), textarea_->GetSelectedRange());
  } else {
    EXPECT_EQ(gfx::Range(6, 11), textarea_->GetSelectedRange());
  }
}

// Ensure the textarea breaks the long word and scrolls on overflow.
TEST_F(TextareaTest, OverflowTest) {
  const size_t count = 50U;
  textarea_->SetBounds(0, 0, 60, 40);

  textarea_->SetText(std::u16string(count, 'a'));
  EXPECT_TRUE(GetDisplayRect().Contains(GetCursorBounds()));

  textarea_->SetText(std::u16string(count, kHebrewLetterSamekh));
  EXPECT_TRUE(GetDisplayRect().Contains(GetCursorBounds()));
}

TEST_F(TextareaTest, OverflowInRTLTest) {
  const size_t count = 50U;
  textarea_->SetBounds(0, 0, 60, 40);
  std::string locale = base::i18n::GetConfiguredLocale();
  base::i18n::SetICUDefaultLocale("he");

  textarea_->SetText(std::u16string(count, 'a'));
  EXPECT_TRUE(GetDisplayRect().Contains(GetCursorBounds()));

  textarea_->SetText(std::u16string(count, kHebrewLetterSamekh));
  EXPECT_TRUE(GetDisplayRect().Contains(GetCursorBounds()));

  // Reset locale.
  base::i18n::SetICUDefaultLocale(locale);
}

TEST_F(TextareaTest, OnBlurTest) {
  const std::string& kText = "abcdef";
  textarea_->SetText(base::ASCIIToUTF16(kText));

  SendEndEvent(false);
  EXPECT_EQ(kText.size(), textarea_->GetCursorPosition());

  // A focus loss should not change the cursor position.
  textarea_->OnBlur();
  EXPECT_EQ(kText.size(), textarea_->GetCursorPosition());
}

TEST_F(TextareaTest, MoveRangeSelectionExtentExpandByWord) {
  constexpr int kGlyphHeight = 10;
  gfx::test::RenderTextTestApi(GetTextfieldTestApi().GetRenderText())
      .SetGlyphHeight(kGlyphHeight);
  textarea_->SetText(u"a textarea\nwith multiline text");
  const int kFirstLineMiddleY = GetCursorYForTesting() + kGlyphHeight / 2;
  const int kSecondLineMiddleY = kFirstLineMiddleY + kGlyphHeight;
  textarea_->SelectBetweenCoordinates(
      gfx::Point(GetCursorPositionX(2), kFirstLineMiddleY),
      gfx::Point(GetCursorPositionX(3), kFirstLineMiddleY));

  // Expand the selection. The end of the selection should move to the nearest
  // word boundary.
  textarea_->MoveRangeSelectionExtent(
      gfx::Point(GetCursorPositionX(22), kSecondLineMiddleY));
  gfx::Range range;
  textarea_->GetEditableSelectionRange(&range);
  EXPECT_EQ(range, gfx::Range(2, 25));
  EXPECT_EQ(textarea_->GetSelectedText(), u"textarea\nwith multiline");

  // Shrink then expand the selection again.
  textarea_->MoveRangeSelectionExtent(
      gfx::Point(GetCursorPositionX(7), kFirstLineMiddleY));
  textarea_->MoveRangeSelectionExtent(
      gfx::Point(GetCursorPositionX(29), kSecondLineMiddleY));
  textarea_->GetEditableSelectionRange(&range);
  EXPECT_EQ(range, gfx::Range(2, 30));
  EXPECT_EQ(textarea_->GetSelectedText(), u"textarea\nwith multiline text");
}

TEST_F(TextareaTest, MoveRangeSelectionExtentShrinkByCharacter) {
  constexpr int kGlyphHeight = 10;
  gfx::test::RenderTextTestApi(GetTextfieldTestApi().GetRenderText())
      .SetGlyphHeight(kGlyphHeight);
  textarea_->SetText(u"a textarea\nwith multiline text");
  const int kFirstLineMiddleY = GetCursorYForTesting() + kGlyphHeight / 2;
  const int kSecondLineMiddleY = kFirstLineMiddleY + kGlyphHeight;
  textarea_->SelectBetweenCoordinates(
      gfx::Point(GetCursorPositionX(2), kFirstLineMiddleY),
      gfx::Point(GetCursorPositionX(25), kSecondLineMiddleY));

  // Shrink the selection.
  textarea_->MoveRangeSelectionExtent(
      gfx::Point(GetCursorPositionX(6), kFirstLineMiddleY));
  gfx::Range range;
  textarea_->GetEditableSelectionRange(&range);
  EXPECT_EQ(range, gfx::Range(2, 6));
  EXPECT_EQ(textarea_->GetSelectedText(), u"text");
}

}  // namespace views