Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Side by Side Diff: Source/core/html/HTMLTextFormControlElementTest.cpp

Issue 357603003: Add functions searching a word boundary without VisualPosition to HTMLTextFormControlElement. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Wrap comment Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "config.h" 5 #include "config.h"
6 #include "core/html/HTMLTextFormControlElement.h" 6 #include "core/html/HTMLTextFormControlElement.h"
7 7
8 #include "core/dom/Position.h"
9 #include "core/dom/Text.h"
10 #include "core/editing/FrameSelection.h"
11 #include "core/editing/VisibleSelection.h"
12 #include "core/editing/VisibleUnits.h"
8 #include "core/frame/FrameView.h" 13 #include "core/frame/FrameView.h"
14 #include "core/html/HTMLBRElement.h"
9 #include "core/html/HTMLDocument.h" 15 #include "core/html/HTMLDocument.h"
16 #include "core/rendering/RenderTreeAsText.h"
10 #include "core/testing/DummyPageHolder.h" 17 #include "core/testing/DummyPageHolder.h"
11 #include "wtf/OwnPtr.h" 18 #include "wtf/OwnPtr.h"
12 #include <gtest/gtest.h> 19 #include <gtest/gtest.h>
13 20
14 using namespace WebCore; 21 using namespace WebCore;
15 22
16 namespace { 23 namespace {
17 24
18 class HTMLTextFormControlElementTest : public ::testing::Test { 25 class HTMLTextFormControlElementTest : public ::testing::Test {
19 protected: 26 protected:
20 virtual void SetUp() OVERRIDE; 27 virtual void SetUp() OVERRIDE;
21 28
29 HTMLDocument& document() const { return *m_document; }
22 HTMLTextFormControlElement& textControl() const { return *m_textControl; } 30 HTMLTextFormControlElement& textControl() const { return *m_textControl; }
23 31
24 private: 32 private:
25 OwnPtr<DummyPageHolder> m_dummyPageHolder; 33 OwnPtr<DummyPageHolder> m_dummyPageHolder;
26 34
35 RefPtrWillBePersistent<HTMLDocument> m_document;
27 RefPtrWillBePersistent<HTMLTextFormControlElement> m_textControl; 36 RefPtrWillBePersistent<HTMLTextFormControlElement> m_textControl;
28 }; 37 };
29 38
30 void HTMLTextFormControlElementTest::SetUp() 39 void HTMLTextFormControlElementTest::SetUp()
31 { 40 {
32 m_dummyPageHolder = DummyPageHolder::create(IntSize(800, 600)); 41 m_dummyPageHolder = DummyPageHolder::create(IntSize(800, 600));
33 HTMLDocument& document = toHTMLDocument(m_dummyPageHolder->document()); 42 m_document = toHTMLDocument(&m_dummyPageHolder->document());
34 document.documentElement()->setInnerHTML("<body><textarea id=textarea></text area></body>", ASSERT_NO_EXCEPTION); 43 m_document->documentElement()->setInnerHTML("<body><textarea id=textarea></t extarea></body>", ASSERT_NO_EXCEPTION);
35 document.view()->updateLayoutAndStyleIfNeededRecursive(); 44 m_document->view()->updateLayoutAndStyleIfNeededRecursive();
36 m_textControl = toHTMLTextFormControlElement(document.getElementById("textar ea")); 45 m_textControl = toHTMLTextFormControlElement(m_document->getElementById("tex tarea"));
37 m_textControl->focus(); 46 m_textControl->focus();
38 } 47 }
39 48
40 TEST_F(HTMLTextFormControlElementTest, SetSelectionRange) 49 TEST_F(HTMLTextFormControlElementTest, SetSelectionRange)
41 { 50 {
42 EXPECT_EQ(0, textControl().selectionStart()); 51 EXPECT_EQ(0, textControl().selectionStart());
43 EXPECT_EQ(0, textControl().selectionEnd()); 52 EXPECT_EQ(0, textControl().selectionEnd());
44 53
45 textControl().setInnerEditorValue("Hello, text form."); 54 textControl().setInnerEditorValue("Hello, text form.");
46 EXPECT_EQ(0, textControl().selectionStart()); 55 EXPECT_EQ(0, textControl().selectionStart());
47 EXPECT_EQ(0, textControl().selectionEnd()); 56 EXPECT_EQ(0, textControl().selectionEnd());
48 57
49 textControl().setSelectionRange(1, 3); 58 textControl().setSelectionRange(1, 3);
50 EXPECT_EQ(1, textControl().selectionStart()); 59 EXPECT_EQ(1, textControl().selectionStart());
51 EXPECT_EQ(3, textControl().selectionEnd()); 60 EXPECT_EQ(3, textControl().selectionEnd());
52 } 61 }
53 62
63 typedef Position (*PositionFunction)(const Position&);
64 typedef VisiblePosition(*VisblePositionFunction)(const VisiblePosition&);
65
66 void testFunctionEquivalence(const Position& position, PositionFunction position Function, VisblePositionFunction visibleFunction)
67 {
68 VisiblePosition visiblePosition(position);
69 VisiblePosition expected = visibleFunction(visiblePosition);
70 VisiblePosition actual = VisiblePosition(positionFunction(position));
71 EXPECT_EQ(expected, actual);
54 } 72 }
73
74 static VisiblePosition startOfWord(const VisiblePosition& position)
75 {
76 return startOfWord(position, LeftWordIfOnBoundary);
77 }
78
79 static VisiblePosition endOfWord(const VisiblePosition& position)
80 {
81 return endOfWord(position, RightWordIfOnBoundary);
82 }
83
84 void testBoundary(HTMLDocument& document, HTMLTextFormControlElement& textContro l)
85 {
86 for (unsigned i = 0; i < textControl.innerEditorValue().length(); i++) {
87 textControl.setSelectionRange(i, i);
88 Position position = document.frame()->selection().start();
89 SCOPED_TRACE(testing::Message() << "offset " << position.deprecatedEditi ngOffset() << " of " << nodePositionAsStringForTesting(position.deprecatedNode() ).ascii().data());
90 {
91 SCOPED_TRACE("HTMLTextFormControlElement::startOfWord");
92 testFunctionEquivalence(position, HTMLTextFormControlElement::startO fWord, startOfWord);
93 }
94 {
95 SCOPED_TRACE("HTMLTextFormControlElement::endOfWord");
96 testFunctionEquivalence(position, HTMLTextFormControlElement::endOfW ord, endOfWord);
97 }
98 {
99 SCOPED_TRACE("HTMLTextFormControlElement::startOfSentence");
100 testFunctionEquivalence(position, HTMLTextFormControlElement::startO fSentence, startOfSentence);
101 }
102 {
103 SCOPED_TRACE("HTMLTextFormControlElement::endOfSentence");
104 testFunctionEquivalence(position, HTMLTextFormControlElement::endOfS entence, endOfSentence);
105 }
106 }
107 }
108
109 TEST_F(HTMLTextFormControlElementTest, WordAndSentenceBoundary)
110 {
111 HTMLElement* innerText = textControl().innerEditorElement();
112 {
113 SCOPED_TRACE("String is value.");
114 innerText->removeChildren();
115 innerText->setNodeValue("Hel\nlo, text form.\n");
116 testBoundary(document(), textControl());
117 }
118 {
119 SCOPED_TRACE("A Text node and a BR element");
120 innerText->removeChildren();
121 innerText->setNodeValue("");
122 innerText->appendChild(Text::create(document(), "Hello, text form."));
123 innerText->appendChild(HTMLBRElement::create(document()));
124 testBoundary(document(), textControl());
125 }
126 {
127 SCOPED_TRACE("Text nodes.");
128 innerText->removeChildren();
129 innerText->setNodeValue("");
130 innerText->appendChild(Text::create(document(), "Hel\nlo, te"));
131 innerText->appendChild(Text::create(document(), "xt form."));
132 testBoundary(document(), textControl());
133 }
134 }
135
136 }
OLDNEW
« no previous file with comments | « Source/core/html/HTMLTextFormControlElement.cpp ('k') | Source/core/rendering/RenderTreeAsText.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698