OLD | NEW |
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 TEST_F(HTMLTextFormControlElementTest, WordAndSentenceBoundary) |
| 85 { |
| 86 HTMLElement* innerText = textControl().innerEditorElement(); |
| 87 // We have: |
| 88 // innerText(shadow root) |
| 89 // --#Text "Hello, text form." |
| 90 // --BR |
| 91 // --BR |
| 92 // --#TEXT "" |
| 93 // --#TEXT "\n\n" |
| 94 // --#TEXT "\n" |
| 95 // --#Text "Hel\nlo, te" |
| 96 // --#TEXT "xt form." |
| 97 innerText->appendChild(Text::create(document(), "Hello, text form.")); |
| 98 innerText->appendChild(HTMLBRElement::create(document())); |
| 99 innerText->appendChild(HTMLBRElement::create(document())); |
| 100 innerText->appendChild(Text::create(document(), "")); |
| 101 innerText->appendChild(Text::create(document(), "\n\n")); |
| 102 innerText->appendChild(Text::create(document(), "\n")); |
| 103 innerText->appendChild(Text::create(document(), "Hel\nlo, te")); |
| 104 innerText->appendChild(Text::create(document(), "xt form.")); |
| 105 |
| 106 for (unsigned i = 0; i < textControl().innerEditorValue().length(); i++) { |
| 107 textControl().setSelectionRange(i, i); |
| 108 Position position = document().frame()->selection().start(); |
| 109 SCOPED_TRACE(testing::Message() << "offset " << position.deprecatedEditi
ngOffset() << " of " << nodePositionAsStringForTesting(position.deprecatedNode()
).ascii().data()); |
| 110 { |
| 111 SCOPED_TRACE("HTMLTextFormControlElement::startOfWord"); |
| 112 testFunctionEquivalence(position, HTMLTextFormControlElement::startO
fWord, startOfWord); |
| 113 } |
| 114 { |
| 115 SCOPED_TRACE("HTMLTextFormControlElement::endOfWord"); |
| 116 testFunctionEquivalence(position, HTMLTextFormControlElement::endOfW
ord, endOfWord); |
| 117 } |
| 118 { |
| 119 SCOPED_TRACE("HTMLTextFormControlElement::startOfSentence"); |
| 120 testFunctionEquivalence(position, HTMLTextFormControlElement::startO
fSentence, startOfSentence); |
| 121 } |
| 122 { |
| 123 SCOPED_TRACE("HTMLTextFormControlElement::endOfSentence"); |
| 124 testFunctionEquivalence(position, HTMLTextFormControlElement::endOfS
entence, endOfSentence); |
| 125 } |
| 126 } |
| 127 } |
| 128 |
| 129 } |
OLD | NEW |