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 |
| 21 using namespace blink; |
14 using namespace WebCore; | 22 using namespace WebCore; |
15 | 23 |
16 namespace { | 24 namespace { |
17 | 25 |
18 class HTMLTextFormControlElementTest : public ::testing::Test { | 26 class HTMLTextFormControlElementTest : public ::testing::Test { |
19 protected: | 27 protected: |
20 virtual void SetUp() OVERRIDE; | 28 virtual void SetUp() OVERRIDE; |
21 | 29 |
| 30 HTMLDocument& document() const { return *m_document; } |
22 HTMLTextFormControlElement& textControl() const { return *m_textControl; } | 31 HTMLTextFormControlElement& textControl() const { return *m_textControl; } |
23 | 32 |
24 private: | 33 private: |
25 OwnPtr<DummyPageHolder> m_dummyPageHolder; | 34 OwnPtr<DummyPageHolder> m_dummyPageHolder; |
26 | 35 |
| 36 RefPtrWillBePersistent<HTMLDocument> m_document; |
27 RefPtrWillBePersistent<HTMLTextFormControlElement> m_textControl; | 37 RefPtrWillBePersistent<HTMLTextFormControlElement> m_textControl; |
28 }; | 38 }; |
29 | 39 |
30 void HTMLTextFormControlElementTest::SetUp() | 40 void HTMLTextFormControlElementTest::SetUp() |
31 { | 41 { |
32 m_dummyPageHolder = DummyPageHolder::create(IntSize(800, 600)); | 42 m_dummyPageHolder = DummyPageHolder::create(IntSize(800, 600)); |
33 HTMLDocument& document = toHTMLDocument(m_dummyPageHolder->document()); | 43 m_document = toHTMLDocument(&m_dummyPageHolder->document()); |
34 document.documentElement()->setInnerHTML("<body><textarea id=textarea></text
area></body>", ASSERT_NO_EXCEPTION); | 44 m_document->documentElement()->setInnerHTML("<body><textarea id=textarea></t
extarea></body>", ASSERT_NO_EXCEPTION); |
35 document.view()->updateLayoutAndStyleIfNeededRecursive(); | 45 m_document->view()->updateLayoutAndStyleIfNeededRecursive(); |
36 m_textControl = toHTMLTextFormControlElement(document.getElementById("textar
ea")); | 46 m_textControl = toHTMLTextFormControlElement(m_document->getElementById("tex
tarea")); |
37 m_textControl->focus(); | 47 m_textControl->focus(); |
38 } | 48 } |
39 | 49 |
40 TEST_F(HTMLTextFormControlElementTest, SetSelectionRange) | 50 TEST_F(HTMLTextFormControlElementTest, SetSelectionRange) |
41 { | 51 { |
42 EXPECT_EQ(0, textControl().selectionStart()); | 52 EXPECT_EQ(0, textControl().selectionStart()); |
43 EXPECT_EQ(0, textControl().selectionEnd()); | 53 EXPECT_EQ(0, textControl().selectionEnd()); |
44 | 54 |
45 textControl().setInnerEditorValue("Hello, text form."); | 55 textControl().setInnerEditorValue("Hello, text form."); |
46 EXPECT_EQ(0, textControl().selectionStart()); | 56 EXPECT_EQ(0, textControl().selectionStart()); |
47 EXPECT_EQ(0, textControl().selectionEnd()); | 57 EXPECT_EQ(0, textControl().selectionEnd()); |
48 | 58 |
49 textControl().setSelectionRange(1, 3); | 59 textControl().setSelectionRange(1, 3); |
50 EXPECT_EQ(1, textControl().selectionStart()); | 60 EXPECT_EQ(1, textControl().selectionStart()); |
51 EXPECT_EQ(3, textControl().selectionEnd()); | 61 EXPECT_EQ(3, textControl().selectionEnd()); |
52 } | 62 } |
53 | 63 |
| 64 typedef Position (*PositionFunction)(const Position&); |
| 65 typedef VisiblePosition(*VisblePositionFunction)(const VisiblePosition&); |
| 66 |
| 67 void testFunctionEquality(const Position& position, PositionFunction positionFun
ction, VisblePositionFunction visibleFunction) |
| 68 { |
| 69 VisiblePosition visiblePosition(position); |
| 70 VisiblePosition expected = visibleFunction(visiblePosition); |
| 71 VisiblePosition actual = VisiblePosition(positionFunction(position)); |
| 72 EXPECT_EQ(expected, actual); |
54 } | 73 } |
| 74 |
| 75 static VisiblePosition startOfWord(const VisiblePosition& position) |
| 76 { |
| 77 return startOfWord(position, LeftWordIfOnBoundary); |
| 78 } |
| 79 |
| 80 static VisiblePosition endOfWord(const VisiblePosition& position) |
| 81 { |
| 82 return endOfWord(position, RightWordIfOnBoundary); |
| 83 } |
| 84 |
| 85 TEST_F(HTMLTextFormControlElementTest, WordAndSentenceBoundary) |
| 86 { |
| 87 HTMLElement* innerText = textControl().innerEditorElement(); |
| 88 // We have: |
| 89 // innerText(shadow root) |
| 90 // --#Text "Hello, text form." |
| 91 // --BR |
| 92 // --BR |
| 93 // --#TEXT "" |
| 94 // --#TEXT "\n\n" |
| 95 // --#TEXT "\n" |
| 96 // --#Text "Hel\nlo, te" |
| 97 // --#TEXT "xt form." |
| 98 innerText->appendChild(Text::create(document(), "Hello, text form.")); |
| 99 innerText->appendChild(HTMLBRElement::create(document())); |
| 100 innerText->appendChild(HTMLBRElement::create(document())); |
| 101 innerText->appendChild(Text::create(document(), "")); |
| 102 innerText->appendChild(Text::create(document(), "\n\n")); |
| 103 innerText->appendChild(Text::create(document(), "\n")); |
| 104 innerText->appendChild(Text::create(document(), "Hel\nlo, te")); |
| 105 innerText->appendChild(Text::create(document(), "xt form.")); |
| 106 |
| 107 for (unsigned i = 0; i < textControl().innerEditorValue().length(); i++) { |
| 108 textControl().setSelectionRange(i, i); |
| 109 Position pos = document().frame()->selection().start(); |
| 110 SCOPED_TRACE(testing::Message() << "offset " << pos.deprecatedEditingOff
set() << " of " << nodePosition(pos.deprecatedNode()).ascii().data()); |
| 111 { |
| 112 SCOPED_TRACE("HTMLTextFormControlElement::startOfWord"); |
| 113 testFunctionEquality(pos, HTMLTextFormControlElement::startOfWord, s
tartOfWord); |
| 114 } |
| 115 { |
| 116 SCOPED_TRACE("HTMLTextFormControlElement::endOfWord"); |
| 117 testFunctionEquality(pos, HTMLTextFormControlElement::endOfWord, end
OfWord); |
| 118 } |
| 119 { |
| 120 SCOPED_TRACE("HTMLTextFormControlElement::startOfSentence"); |
| 121 testFunctionEquality(pos, HTMLTextFormControlElement::startOfSentenc
e, startOfSentence); |
| 122 } |
| 123 { |
| 124 SCOPED_TRACE("HTMLTextFormControlElement::endOfSentence"); |
| 125 testFunctionEquality(pos, HTMLTextFormControlElement::endOfSentence,
endOfSentence); |
| 126 } |
| 127 } |
| 128 } |
| 129 |
| 130 } |
OLD | NEW |