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/SpellChecker.h" |
| 12 #include "core/editing/VisibleSelection.h" |
| 13 #include "core/editing/VisibleUnits.h" |
8 #include "core/frame/FrameView.h" | 14 #include "core/frame/FrameView.h" |
| 15 #include "core/html/HTMLBRElement.h" |
9 #include "core/html/HTMLDocument.h" | 16 #include "core/html/HTMLDocument.h" |
| 17 #include "core/html/HTMLInputElement.h" |
| 18 #include "core/html/HTMLTextAreaElement.h" |
| 19 #include "core/page/SpellCheckerClient.h" |
| 20 #include "core/rendering/RenderTreeAsText.h" |
10 #include "core/testing/DummyPageHolder.h" | 21 #include "core/testing/DummyPageHolder.h" |
| 22 #include "platform/text/TextCheckerClient.h" |
11 #include "wtf/OwnPtr.h" | 23 #include "wtf/OwnPtr.h" |
12 #include <gtest/gtest.h> | 24 #include <gtest/gtest.h> |
13 | 25 |
| 26 using namespace blink; |
14 using namespace WebCore; | 27 using namespace WebCore; |
15 | 28 |
16 namespace { | 29 namespace { |
17 | 30 |
18 class HTMLTextFormControlElementTest : public ::testing::Test { | 31 class HTMLTextFormControlElementTest : public ::testing::Test { |
19 protected: | 32 protected: |
20 virtual void SetUp() OVERRIDE; | 33 virtual void SetUp() OVERRIDE; |
21 | 34 |
| 35 void SetUpWithPageClients(Page::PageClients*); |
| 36 |
| 37 DummyPageHolder& page() const { return *m_dummyPageHolder; } |
| 38 HTMLDocument& document() const { return *m_document; } |
22 HTMLTextFormControlElement& textControl() const { return *m_textControl; } | 39 HTMLTextFormControlElement& textControl() const { return *m_textControl; } |
23 | 40 |
24 private: | 41 private: |
25 OwnPtr<DummyPageHolder> m_dummyPageHolder; | 42 OwnPtr<DummyPageHolder> m_dummyPageHolder; |
26 | 43 |
| 44 RefPtrWillBePersistent<HTMLDocument> m_document; |
27 RefPtrWillBePersistent<HTMLTextFormControlElement> m_textControl; | 45 RefPtrWillBePersistent<HTMLTextFormControlElement> m_textControl; |
28 }; | 46 }; |
29 | 47 |
30 void HTMLTextFormControlElementTest::SetUp() | 48 void HTMLTextFormControlElementTest::SetUp() |
31 { | 49 { |
32 m_dummyPageHolder = DummyPageHolder::create(IntSize(800, 600)); | 50 Page::PageClients pageClients; |
33 HTMLDocument& document = toHTMLDocument(m_dummyPageHolder->document()); | 51 fillWithEmptyClients(pageClients); |
34 document.documentElement()->setInnerHTML("<body><textarea id=textarea></text
area></body>", ASSERT_NO_EXCEPTION); | 52 SetUpWithPageClients(&pageClients); |
35 document.view()->updateLayoutAndStyleIfNeededRecursive(); | 53 } |
36 m_textControl = toHTMLTextFormControlElement(document.getElementById("textar
ea")); | 54 |
| 55 void HTMLTextFormControlElementTest::SetUpWithPageClients(Page::PageClients* pag
eClients) |
| 56 { |
| 57 m_dummyPageHolder = DummyPageHolder::create(IntSize(800, 600), pageClients); |
| 58 m_document = toHTMLDocument(&m_dummyPageHolder->document()); |
| 59 m_document->documentElement()->setInnerHTML("<body><textarea id=textarea></t
extarea><input id=input /></body>", ASSERT_NO_EXCEPTION); |
| 60 m_document->view()->updateLayoutAndStyleIfNeededRecursive(); |
| 61 m_textControl = toHTMLTextFormControlElement(m_document->getElementById("tex
tarea")); |
37 m_textControl->focus(); | 62 m_textControl->focus(); |
38 } | 63 } |
39 | 64 |
40 TEST_F(HTMLTextFormControlElementTest, SetSelectionRange) | 65 TEST_F(HTMLTextFormControlElementTest, SetSelectionRange) |
41 { | 66 { |
42 EXPECT_EQ(0, textControl().selectionStart()); | 67 EXPECT_EQ(0, textControl().selectionStart()); |
43 EXPECT_EQ(0, textControl().selectionEnd()); | 68 EXPECT_EQ(0, textControl().selectionEnd()); |
44 | 69 |
45 textControl().setInnerEditorValue("Hello, text form."); | 70 textControl().setInnerEditorValue("Hello, text form."); |
46 EXPECT_EQ(0, textControl().selectionStart()); | 71 EXPECT_EQ(0, textControl().selectionStart()); |
47 EXPECT_EQ(0, textControl().selectionEnd()); | 72 EXPECT_EQ(0, textControl().selectionEnd()); |
48 | 73 |
49 textControl().setSelectionRange(1, 3); | 74 textControl().setSelectionRange(1, 3); |
50 EXPECT_EQ(1, textControl().selectionStart()); | 75 EXPECT_EQ(1, textControl().selectionStart()); |
51 EXPECT_EQ(3, textControl().selectionEnd()); | 76 EXPECT_EQ(3, textControl().selectionEnd()); |
52 } | 77 } |
53 | 78 |
| 79 typedef Position (*PositionFunction)(const Position&); |
| 80 typedef VisiblePosition(*VisblePositionFunction)(const VisiblePosition&); |
| 81 |
| 82 void testFunctionEquality(const Position& position, PositionFunction positionFun
ction, VisblePositionFunction visibleFunction) |
| 83 { |
| 84 VisiblePosition visiblePosition(position); |
| 85 VisiblePosition expected = visibleFunction(visiblePosition); |
| 86 VisiblePosition actual = VisiblePosition(positionFunction(position)); |
| 87 EXPECT_EQ(expected, actual); |
54 } | 88 } |
| 89 |
| 90 static VisiblePosition startOfWord(const VisiblePosition& position) |
| 91 { |
| 92 return startOfWord(position, LeftWordIfOnBoundary); |
| 93 } |
| 94 |
| 95 static VisiblePosition endOfWord(const VisiblePosition& position) |
| 96 { |
| 97 return endOfWord(position, RightWordIfOnBoundary); |
| 98 } |
| 99 |
| 100 TEST_F(HTMLTextFormControlElementTest, WordAndSentenceBoundary) |
| 101 { |
| 102 HTMLElement* innerText = textControl().innerEditorElement(); |
| 103 // We have: |
| 104 // innerText(shadow root) |
| 105 // --#Text "Hello, text form." |
| 106 // --BR |
| 107 // --BR |
| 108 // --#TEXT "" |
| 109 // --#TEXT "\n\n" |
| 110 // --#TEXT "\n" |
| 111 // --#Text "Hel\nlo, te" |
| 112 // --#TEXT "xt form." |
| 113 innerText->appendChild(Text::create(document(), "Hello, text form.")); |
| 114 innerText->appendChild(HTMLBRElement::create(document())); |
| 115 innerText->appendChild(HTMLBRElement::create(document())); |
| 116 innerText->appendChild(Text::create(document(), "")); |
| 117 innerText->appendChild(Text::create(document(), "\n\n")); |
| 118 innerText->appendChild(Text::create(document(), "\n")); |
| 119 innerText->appendChild(Text::create(document(), "Hel\nlo, te")); |
| 120 innerText->appendChild(Text::create(document(), "xt form.")); |
| 121 |
| 122 for (unsigned i = 0; i < textControl().innerEditorValue().length(); i++) { |
| 123 textControl().setSelectionRange(i, i); |
| 124 Position pos = document().frame()->selection().start(); |
| 125 SCOPED_TRACE(testing::Message() << "offset " << pos.deprecatedEditingOff
set() << " of " << nodePosition(pos.deprecatedNode()).ascii().data()); |
| 126 { |
| 127 SCOPED_TRACE("HTMLTextFormControlElement::startOfWord"); |
| 128 testFunctionEquality(pos, HTMLTextFormControlElement::startOfWord, s
tartOfWord); |
| 129 } |
| 130 { |
| 131 SCOPED_TRACE("HTMLTextFormControlElement::endOfWord"); |
| 132 testFunctionEquality(pos, HTMLTextFormControlElement::endOfWord, end
OfWord); |
| 133 } |
| 134 { |
| 135 SCOPED_TRACE("HTMLTextFormControlElement::startOfSentence"); |
| 136 testFunctionEquality(pos, HTMLTextFormControlElement::startOfSentenc
e, startOfSentence); |
| 137 } |
| 138 { |
| 139 SCOPED_TRACE("HTMLTextFormControlElement::endOfSentence"); |
| 140 testFunctionEquality(pos, HTMLTextFormControlElement::endOfSentence,
endOfSentence); |
| 141 } |
| 142 } |
| 143 } |
| 144 |
| 145 class DummyTextCheckerClient : public TextCheckerClient { |
| 146 public: |
| 147 virtual bool shouldEraseMarkersAfterChangeSelection(TextCheckingType type) c
onst OVERRIDE { return false; } |
| 148 virtual void checkSpellingOfString(const String&, int* misspellingLocation,
int* misspellingLength) OVERRIDE { } |
| 149 virtual String getAutoCorrectSuggestionForMisspelledWord(const String& missp
elledWord) OVERRIDE { return String(); } |
| 150 virtual void checkGrammarOfString(const String&, Vector<GrammarDetail>&, int
* badGrammarLocation, int* badGrammarLength) OVERRIDE { } |
| 151 virtual void requestCheckingOfString(PassRefPtr<TextCheckingRequest>) OVERRI
DE { } |
| 152 }; |
| 153 |
| 154 class DummySpellCheckerClient : public SpellCheckerClient { |
| 155 public: |
| 156 DummySpellCheckerClient() : m_textChecker(adoptPtr(new DummyTextCheckerClien
t)) { } |
| 157 virtual bool isContinuousSpellCheckingEnabled() OVERRIDE { return true; } |
| 158 virtual void toggleContinuousSpellChecking() OVERRIDE { } |
| 159 virtual bool isGrammarCheckingEnabled() OVERRIDE { return true; } |
| 160 |
| 161 virtual TextCheckerClient& textChecker() OVERRIDE { return *m_textChecker; } |
| 162 |
| 163 virtual void updateSpellingUIWithMisspelledWord(const WTF::String&) OVERRIDE
{ } |
| 164 virtual void showSpellingUI(bool show) OVERRIDE { } |
| 165 virtual bool spellingUIIsShowing() OVERRIDE { return true; } |
| 166 |
| 167 OwnPtr<TextCheckerClient> m_textChecker; |
| 168 }; |
| 169 |
| 170 |
| 171 TEST_F(HTMLTextFormControlElementTest, SpellCheckDoesNotCauseUpdateLayout) |
| 172 { |
| 173 Page::PageClients pageClients; |
| 174 fillWithEmptyClients(pageClients); |
| 175 static SpellCheckerClient* dummySpellCheckerClient = adoptPtr(new DummySpell
CheckerClient).leakPtr(); |
| 176 pageClients.spellCheckerClient = dummySpellCheckerClient; |
| 177 SetUpWithPageClients(&pageClients); |
| 178 |
| 179 HTMLInputElement* input = toHTMLInputElement(document().getElementById("inpu
t")); |
| 180 input->focus(); |
| 181 input->setValue("Hello, input field"); |
| 182 VisibleSelection oldSelection = document().frame()->selection().selection(); |
| 183 |
| 184 Position newPosition(input->innerEditorElement()->firstChild(), 3, Position:
:PositionIsOffsetInAnchor); |
| 185 VisibleSelection newSelection(newPosition, DOWNSTREAM); |
| 186 document().frame()->selection().setSelection(newSelection, FrameSelection::C
loseTyping | FrameSelection::ClearTypingStyle | FrameSelection::DoNotUpdateAppea
rance); |
| 187 ASSERT_EQ(3, input->selectionStart()); |
| 188 |
| 189 OwnPtrWillBeRawPtr<SpellChecker> spellChecker(SpellChecker::create(page().fr
ame())); |
| 190 page().frameView().setFrameRect(WebCore::IntRect(0, 0, 801, 601)); |
| 191 int startCount = page().frameView().layoutCount(); |
| 192 spellChecker->respondToChangedSelection(oldSelection, FrameSelection::CloseT
yping | FrameSelection::ClearTypingStyle); |
| 193 EXPECT_EQ(startCount, page().frameView().layoutCount()); |
| 194 } |
| 195 |
| 196 } |
OLD | NEW |