| 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 "core/html/TextControlElement.h" | 5 #include "core/html/TextControlElement.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include "core/dom/Document.h" | 8 #include "core/dom/Document.h" |
| 9 #include "core/editing/FrameSelection.h" | 9 #include "core/editing/FrameSelection.h" |
| 10 #include "core/frame/FrameView.h" | 10 #include "core/frame/FrameView.h" |
| 11 #include "core/html/HTMLInputElement.h" | 11 #include "core/html/HTMLInputElement.h" |
| 12 #include "core/html/HTMLTextAreaElement.h" | 12 #include "core/html/HTMLTextAreaElement.h" |
| 13 #include "core/loader/EmptyClients.h" | 13 #include "core/loader/EmptyClients.h" |
| 14 #include "core/testing/DummyPageHolder.h" | 14 #include "core/testing/DummyPageHolder.h" |
| 15 #include "platform/testing/UnitTestHelpers.h" | 15 #include "platform/testing/UnitTestHelpers.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "wtf/PtrUtil.h" | 17 #include "wtf/PtrUtil.h" |
| 18 | 18 |
| 19 namespace blink { | 19 namespace blink { |
| 20 | 20 |
| 21 class TextControlElementTest : public ::testing::Test { | 21 class TextControlElementTest : public ::testing::Test { |
| 22 protected: | 22 protected: |
| 23 void SetUp() override; | 23 void SetUp() override; |
| 24 | 24 |
| 25 DummyPageHolder& page() const { return *m_dummyPageHolder; } | 25 DummyPageHolder& page() const { return *m_dummyPageHolder; } |
| 26 Document& document() const { return *m_document; } | 26 Document& document() const { return *m_document; } |
| 27 TextControlElement& textControl() const { return *m_textControl; } | 27 TextControlElement& textControl() const { return *m_textControl; } |
| 28 HTMLInputElement& input() const { return *m_input; } | 28 HTMLInputElement& input() const { return *m_input; } |
| 29 | 29 |
| 30 int layoutCount() const { return page().frameView().layoutCount(); } | |
| 31 void forceLayoutFlag(); | |
| 32 | |
| 33 private: | 30 private: |
| 34 std::unique_ptr<DummyPageHolder> m_dummyPageHolder; | 31 std::unique_ptr<DummyPageHolder> m_dummyPageHolder; |
| 35 | 32 |
| 36 Persistent<Document> m_document; | 33 Persistent<Document> m_document; |
| 37 Persistent<TextControlElement> m_textControl; | 34 Persistent<TextControlElement> m_textControl; |
| 38 Persistent<HTMLInputElement> m_input; | 35 Persistent<HTMLInputElement> m_input; |
| 39 }; | 36 }; |
| 40 | 37 |
| 41 void TextControlElementTest::SetUp() { | 38 void TextControlElementTest::SetUp() { |
| 42 Page::PageClients pageClients; | 39 Page::PageClients pageClients; |
| 43 fillWithEmptyClients(pageClients); | 40 fillWithEmptyClients(pageClients); |
| 44 m_dummyPageHolder = DummyPageHolder::create(IntSize(800, 600), &pageClients); | 41 m_dummyPageHolder = DummyPageHolder::create(IntSize(800, 600), &pageClients); |
| 45 | 42 |
| 46 m_document = &m_dummyPageHolder->document(); | 43 m_document = &m_dummyPageHolder->document(); |
| 47 m_document->documentElement()->setInnerHTML( | 44 m_document->documentElement()->setInnerHTML( |
| 48 "<body><textarea id=textarea></textarea><input id=input /></body>"); | 45 "<body><textarea id=textarea></textarea><input id=input /></body>"); |
| 49 m_document->view()->updateAllLifecyclePhases(); | 46 m_document->view()->updateAllLifecyclePhases(); |
| 50 m_textControl = toTextControlElement(m_document->getElementById("textarea")); | 47 m_textControl = toTextControlElement(m_document->getElementById("textarea")); |
| 51 m_textControl->focus(); | 48 m_textControl->focus(); |
| 52 m_input = toHTMLInputElement(m_document->getElementById("input")); | 49 m_input = toHTMLInputElement(m_document->getElementById("input")); |
| 53 } | 50 } |
| 54 | 51 |
| 55 void TextControlElementTest::forceLayoutFlag() { | |
| 56 FrameView& frameView = page().frameView(); | |
| 57 IntRect frameRect = frameView.frameRect(); | |
| 58 frameRect.setWidth(frameRect.width() + 1); | |
| 59 frameRect.setHeight(frameRect.height() + 1); | |
| 60 page().frameView().setFrameRect(frameRect); | |
| 61 document().updateStyleAndLayoutIgnorePendingStylesheets(); | |
| 62 } | |
| 63 | |
| 64 TEST_F(TextControlElementTest, SetSelectionRange) { | 52 TEST_F(TextControlElementTest, SetSelectionRange) { |
| 65 EXPECT_EQ(0u, textControl().selectionStart()); | 53 EXPECT_EQ(0u, textControl().selectionStart()); |
| 66 EXPECT_EQ(0u, textControl().selectionEnd()); | 54 EXPECT_EQ(0u, textControl().selectionEnd()); |
| 67 | 55 |
| 68 textControl().setInnerEditorValue("Hello, text form."); | 56 textControl().setInnerEditorValue("Hello, text form."); |
| 69 EXPECT_EQ(0u, textControl().selectionStart()); | 57 EXPECT_EQ(0u, textControl().selectionStart()); |
| 70 EXPECT_EQ(0u, textControl().selectionEnd()); | 58 EXPECT_EQ(0u, textControl().selectionEnd()); |
| 71 | 59 |
| 72 textControl().setSelectionRange(1, 3); | 60 textControl().setSelectionRange(1, 3); |
| 73 EXPECT_EQ(1u, textControl().selectionStart()); | 61 EXPECT_EQ(1u, textControl().selectionStart()); |
| 74 EXPECT_EQ(3u, textControl().selectionEnd()); | 62 EXPECT_EQ(3u, textControl().selectionEnd()); |
| 75 } | 63 } |
| 76 | 64 |
| 77 TEST_F(TextControlElementTest, SetSelectionRangeDoesNotCauseLayout) { | 65 TEST_F(TextControlElementTest, SetSelectionRangeDoesNotCauseLayout) { |
| 78 input().focus(); | 66 input().focus(); |
| 79 input().setValue("Hello, input form."); | 67 input().setValue("Hello, input form."); |
| 80 input().setSelectionRange(1, 1); | 68 input().setSelectionRange(1, 1); |
| 81 FrameSelection& frameSelection = document().frame()->selection(); | |
| 82 forceLayoutFlag(); | |
| 83 LayoutRect oldCaretRect(frameSelection.absoluteCaretBounds()); | |
| 84 EXPECT_FALSE(oldCaretRect.isEmpty()); | |
| 85 int startLayoutCount = layoutCount(); | |
| 86 input().setSelectionRange(1, 1); | |
| 87 EXPECT_EQ(startLayoutCount, layoutCount()); | |
| 88 LayoutRect newCaretRect(frameSelection.absoluteCaretBounds()); | |
| 89 EXPECT_EQ(oldCaretRect, newCaretRect); | |
| 90 | 69 |
| 91 forceLayoutFlag(); | 70 // Force layout if document().updateStyleAndLayoutIgnorePendingStylesheets() |
| 92 oldCaretRect = LayoutRect(frameSelection.absoluteCaretBounds()); | 71 // is called. |
| 93 EXPECT_FALSE(oldCaretRect.isEmpty()); | 72 document().body()->appendChild(document().createTextNode("foo")); |
| 94 startLayoutCount = layoutCount(); | 73 const int startLayoutCount = page().frameView().layoutCount(); |
| 74 EXPECT_TRUE(document().needsLayoutTreeUpdate()); |
| 95 input().setSelectionRange(2, 2); | 75 input().setSelectionRange(2, 2); |
| 96 EXPECT_EQ(startLayoutCount, layoutCount()); | 76 EXPECT_EQ(startLayoutCount, page().frameView().layoutCount()); |
| 97 newCaretRect = LayoutRect(frameSelection.absoluteCaretBounds()); | |
| 98 EXPECT_NE(oldCaretRect, newCaretRect); | |
| 99 } | 77 } |
| 100 | 78 |
| 101 TEST_F(TextControlElementTest, IndexForPosition) { | 79 TEST_F(TextControlElementTest, IndexForPosition) { |
| 102 HTMLInputElement* input = | 80 HTMLInputElement* input = |
| 103 toHTMLInputElement(document().getElementById("input")); | 81 toHTMLInputElement(document().getElementById("input")); |
| 104 input->setValue("Hello"); | 82 input->setValue("Hello"); |
| 105 HTMLElement* innerEditor = input->innerEditorElement(); | 83 HTMLElement* innerEditor = input->innerEditorElement(); |
| 106 EXPECT_EQ(5u, TextControlElement::indexForPosition( | 84 EXPECT_EQ(5u, TextControlElement::indexForPosition( |
| 107 innerEditor, | 85 innerEditor, |
| 108 Position(innerEditor, PositionAnchorType::AfterAnchor))); | 86 Position(innerEditor, PositionAnchorType::AfterAnchor))); |
| 109 } | 87 } |
| 110 | 88 |
| 111 } // namespace blink | 89 } // namespace blink |
| OLD | NEW |