File: TextControlElementTest.cpp

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (127 lines) | stat: -rw-r--r-- 4,549 bytes parent folder | download
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
// Copyright 2014 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 "core/html/TextControlElement.h"

#include "core/dom/Document.h"
#include "core/editing/spellcheck/SpellChecker.h"
#include "core/frame/FrameView.h"
#include "core/html/HTMLInputElement.h"
#include "core/html/HTMLTextAreaElement.h"
#include "core/loader/EmptyClients.h"
#include "core/page/SpellCheckerClient.h"
#include "core/testing/DummyPageHolder.h"
#include "platform/testing/UnitTestHelpers.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "wtf/PtrUtil.h"
#include <memory>

namespace blink {

class TextControlElementTest : public ::testing::Test {
 protected:
  void SetUp() override;

  DummyPageHolder& page() const { return *m_dummyPageHolder; }
  Document& document() const { return *m_document; }
  TextControlElement& textControl() const { return *m_textControl; }
  HTMLInputElement& input() const { return *m_input; }

  int layoutCount() const { return page().frameView().layoutCount(); }
  void forceLayoutFlag();

 private:
  std::unique_ptr<SpellCheckerClient> m_spellCheckerClient;
  std::unique_ptr<DummyPageHolder> m_dummyPageHolder;

  Persistent<Document> m_document;
  Persistent<TextControlElement> m_textControl;
  Persistent<HTMLInputElement> m_input;
};

class DummySpellCheckerClient : public EmptySpellCheckerClient {
 public:
  virtual ~DummySpellCheckerClient() {}

  bool isSpellCheckingEnabled() override { return true; }

  TextCheckerClient& textChecker() override { return m_emptyTextCheckerClient; }

 private:
  EmptyTextCheckerClient m_emptyTextCheckerClient;
};

void TextControlElementTest::SetUp() {
  Page::PageClients pageClients;
  fillWithEmptyClients(pageClients);
  m_spellCheckerClient = WTF::wrapUnique(new DummySpellCheckerClient);
  pageClients.spellCheckerClient = m_spellCheckerClient.get();
  m_dummyPageHolder = DummyPageHolder::create(IntSize(800, 600), &pageClients);

  m_document = &m_dummyPageHolder->document();
  m_document->documentElement()->setInnerHTML(
      "<body><textarea id=textarea></textarea><input id=input /></body>");
  m_document->view()->updateAllLifecyclePhases();
  m_textControl = toTextControlElement(m_document->getElementById("textarea"));
  m_textControl->focus();
  m_input = toHTMLInputElement(m_document->getElementById("input"));
}

void TextControlElementTest::forceLayoutFlag() {
  FrameView& frameView = page().frameView();
  IntRect frameRect = frameView.frameRect();
  frameRect.setWidth(frameRect.width() + 1);
  frameRect.setHeight(frameRect.height() + 1);
  page().frameView().setFrameRect(frameRect);
  document().updateStyleAndLayoutIgnorePendingStylesheets();
}

TEST_F(TextControlElementTest, SetSelectionRange) {
  EXPECT_EQ(0, textControl().selectionStart());
  EXPECT_EQ(0, textControl().selectionEnd());

  textControl().setInnerEditorValue("Hello, text form.");
  EXPECT_EQ(0, textControl().selectionStart());
  EXPECT_EQ(0, textControl().selectionEnd());

  textControl().setSelectionRange(1, 3);
  EXPECT_EQ(1, textControl().selectionStart());
  EXPECT_EQ(3, textControl().selectionEnd());
}

TEST_F(TextControlElementTest, SetSelectionRangeDoesNotCauseLayout) {
  input().focus();
  input().setValue("Hello, input form.");
  input().setSelectionRange(1, 1);
  FrameSelection& frameSelection = document().frame()->selection();
  forceLayoutFlag();
  LayoutRect oldCaretRect(frameSelection.absoluteCaretBounds());
  EXPECT_FALSE(oldCaretRect.isEmpty());
  int startLayoutCount = layoutCount();
  input().setSelectionRange(1, 1);
  EXPECT_EQ(startLayoutCount, layoutCount());
  LayoutRect newCaretRect(frameSelection.absoluteCaretBounds());
  EXPECT_EQ(oldCaretRect, newCaretRect);

  forceLayoutFlag();
  oldCaretRect = LayoutRect(frameSelection.absoluteCaretBounds());
  EXPECT_FALSE(oldCaretRect.isEmpty());
  startLayoutCount = layoutCount();
  input().setSelectionRange(2, 2);
  EXPECT_EQ(startLayoutCount, layoutCount());
  newCaretRect = LayoutRect(frameSelection.absoluteCaretBounds());
  EXPECT_NE(oldCaretRect, newCaretRect);
}

TEST_F(TextControlElementTest, IndexForPosition) {
  HTMLInputElement* input =
      toHTMLInputElement(document().getElementById("input"));
  input->setValue("Hello");
  HTMLElement* innerEditor = input->innerEditorElement();
  EXPECT_EQ(5, TextControlElement::indexForPosition(
                   innerEditor,
                   Position(innerEditor, PositionAnchorType::AfterAnchor)));
}

}  // namespace blink