| 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/editing/FrameSelection.h" | 5 #include "core/editing/FrameSelection.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ExceptionState.h" | 7 #include "bindings/core/v8/ExceptionState.h" |
| 8 #include "core/dom/Document.h" | 8 #include "core/dom/Document.h" |
| 9 #include "core/dom/Element.h" | 9 #include "core/dom/Element.h" |
| 10 #include "core/dom/Text.h" | 10 #include "core/dom/Text.h" |
| 11 #include "core/editing/EditingTestBase.h" | 11 #include "core/editing/EditingTestBase.h" |
| 12 #include "core/editing/FrameCaret.h" | 12 #include "core/editing/FrameCaret.h" |
| 13 #include "core/editing/SelectionController.h" | 13 #include "core/editing/SelectionController.h" |
| 14 #include "core/frame/FrameView.h" | 14 #include "core/frame/FrameView.h" |
| 15 #include "core/html/HTMLBodyElement.h" | 15 #include "core/html/HTMLBodyElement.h" |
| 16 #include "core/input/EventHandler.h" | 16 #include "core/input/EventHandler.h" |
| 17 #include "core/layout/LayoutBlock.h" |
| 17 #include "core/paint/PaintInfo.h" | 18 #include "core/paint/PaintInfo.h" |
| 18 #include "core/paint/PaintLayer.h" | 19 #include "core/paint/PaintLayer.h" |
| 19 #include "core/testing/DummyPageHolder.h" | 20 #include "core/testing/DummyPageHolder.h" |
| 20 #include "platform/graphics/paint/DrawingRecorder.h" | 21 #include "platform/graphics/paint/DrawingRecorder.h" |
| 21 #include "platform/graphics/paint/PaintController.h" | 22 #include "platform/graphics/paint/PaintController.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 23 #include "wtf/PassRefPtr.h" | 24 #include "wtf/PassRefPtr.h" |
| 24 #include "wtf/RefPtr.h" | 25 #include "wtf/RefPtr.h" |
| 25 #include "wtf/StdLibExtras.h" | 26 #include "wtf/StdLibExtras.h" |
| 26 #include <memory> | 27 #include <memory> |
| 27 | 28 |
| 28 namespace blink { | 29 namespace blink { |
| 29 | 30 |
| 30 class FrameSelectionTest : public EditingTestBase { | 31 class FrameSelectionTest : public EditingTestBase { |
| 31 protected: | 32 protected: |
| 32 const VisibleSelection& visibleSelectionInDOMTree() const { | 33 const VisibleSelection& visibleSelectionInDOMTree() const { |
| 33 return selection().selection(); | 34 return selection().selection(); |
| 34 } | 35 } |
| 35 const VisibleSelectionInFlatTree& visibleSelectionInFlatTree() const { | 36 const VisibleSelectionInFlatTree& visibleSelectionInFlatTree() const { |
| 36 return selection().selectionInFlatTree(); | 37 return selection().selectionInFlatTree(); |
| 37 } | 38 } |
| 38 | 39 |
| 39 Text* appendTextNode(const String& data); | 40 Text* appendTextNode(const String& data); |
| 40 int layoutCount() const { | 41 int layoutCount() const { |
| 41 return dummyPageHolder().frameView().layoutCount(); | 42 return dummyPageHolder().frameView().layoutCount(); |
| 42 } | 43 } |
| 43 | 44 |
| 44 bool isCaretBoundsDirty() const { | |
| 45 return selection().m_frameCaret->m_caretRectDirty; | |
| 46 } | |
| 47 | |
| 48 bool shouldPaintCaretForTesting() const { | |
| 49 return selection().shouldPaintCaretForTesting(); | |
| 50 } | |
| 51 bool isPreviousCaretDirtyForTesting() const { | |
| 52 return selection().isPreviousCaretDirtyForTesting(); | |
| 53 } | |
| 54 | |
| 55 PositionWithAffinity caretPosition() const { | 45 PositionWithAffinity caretPosition() const { |
| 56 return selection().m_frameCaret->caretPosition(); | 46 return selection().m_frameCaret->caretPosition(); |
| 57 } | 47 } |
| 58 | 48 |
| 59 private: | 49 private: |
| 60 Persistent<Text> m_textNode; | 50 Persistent<Text> m_textNode; |
| 61 }; | 51 }; |
| 62 | 52 |
| 63 Text* FrameSelectionTest::appendTextNode(const String& data) { | 53 Text* FrameSelectionTest::appendTextNode(const String& data) { |
| 64 Text* text = document().createTextNode(data); | 54 Text* text = document().createTextNode(data); |
| 65 document().body()->appendChild(text); | 55 document().body()->appendChild(text); |
| 66 return text; | 56 return text; |
| 67 } | 57 } |
| 68 | 58 |
| 69 TEST_F(FrameSelectionTest, SetValidSelection) { | 59 TEST_F(FrameSelectionTest, SetValidSelection) { |
| 70 Text* text = appendTextNode("Hello, World!"); | 60 Text* text = appendTextNode("Hello, World!"); |
| 71 document().view()->updateAllLifecyclePhases(); | 61 document().view()->updateAllLifecyclePhases(); |
| 72 selection().setSelection( | 62 selection().setSelection( |
| 73 SelectionInDOMTree::Builder() | 63 SelectionInDOMTree::Builder() |
| 74 .setBaseAndExtent(Position(text, 0), Position(text, 5)) | 64 .setBaseAndExtent(Position(text, 0), Position(text, 5)) |
| 75 .build()); | 65 .build()); |
| 76 EXPECT_FALSE(selection().isNone()); | 66 EXPECT_FALSE(selection().isNone()); |
| 77 } | 67 } |
| 78 | 68 |
| 79 TEST_F(FrameSelectionTest, InvalidateCaretRect) { | |
| 80 Text* text = appendTextNode("Hello, World!"); | |
| 81 document().view()->updateAllLifecyclePhases(); | |
| 82 | |
| 83 selection().setSelection( | |
| 84 SelectionInDOMTree::Builder().collapse(Position(text, 0)).build()); | |
| 85 selection().setCaretRectNeedsUpdate(); | |
| 86 EXPECT_TRUE(isCaretBoundsDirty()); | |
| 87 selection().invalidateCaretRect(); | |
| 88 EXPECT_FALSE(isCaretBoundsDirty()); | |
| 89 | |
| 90 document().body()->removeChild(text); | |
| 91 document().updateStyleAndLayoutIgnorePendingStylesheets(); | |
| 92 selection().setCaretRectNeedsUpdate(); | |
| 93 EXPECT_TRUE(isCaretBoundsDirty()); | |
| 94 selection().invalidateCaretRect(); | |
| 95 EXPECT_FALSE(isCaretBoundsDirty()); | |
| 96 } | |
| 97 | |
| 98 TEST_F(FrameSelectionTest, PaintCaretShouldNotLayout) { | 69 TEST_F(FrameSelectionTest, PaintCaretShouldNotLayout) { |
| 99 Text* text = appendTextNode("Hello, World!"); | 70 Text* text = appendTextNode("Hello, World!"); |
| 100 document().view()->updateAllLifecyclePhases(); | 71 document().view()->updateAllLifecyclePhases(); |
| 101 | 72 |
| 102 document().body()->setContentEditable("true", ASSERT_NO_EXCEPTION); | 73 document().body()->setContentEditable("true", ASSERT_NO_EXCEPTION); |
| 103 document().body()->focus(); | 74 document().body()->focus(); |
| 104 EXPECT_TRUE(document().body()->isFocused()); | 75 EXPECT_TRUE(document().body()->isFocused()); |
| 105 | 76 |
| 106 selection().setCaretVisible(true); | 77 selection().setCaretVisible(true); |
| 107 selection().setSelection( | 78 selection().setSelection( |
| 108 SelectionInDOMTree::Builder().collapse(Position(text, 0)).build()); | 79 SelectionInDOMTree::Builder().collapse(Position(text, 0)).build()); |
| 80 selection().updateForPaintInvalidation(); |
| 81 EXPECT_TRUE(toLayoutBlock(document().body()->layoutObject()) |
| 82 ->shouldPaintCursorCaret()); |
| 109 EXPECT_TRUE(selection().isCaret()); | 83 EXPECT_TRUE(selection().isCaret()); |
| 110 EXPECT_TRUE(shouldPaintCaretForTesting()); | |
| 111 | 84 |
| 112 int startCount = layoutCount(); | 85 int startCount = layoutCount(); |
| 113 { | 86 { |
| 114 // To force layout in next updateLayout calling, widen view. | 87 // To force layout in next updateLayout calling, widen view. |
| 115 FrameView& frameView = dummyPageHolder().frameView(); | 88 FrameView& frameView = dummyPageHolder().frameView(); |
| 116 IntRect frameRect = frameView.frameRect(); | 89 IntRect frameRect = frameView.frameRect(); |
| 117 frameRect.setWidth(frameRect.width() + 1); | 90 frameRect.setWidth(frameRect.width() + 1); |
| 118 frameRect.setHeight(frameRect.height() + 1); | 91 frameRect.setHeight(frameRect.height() + 1); |
| 119 dummyPageHolder().frameView().setFrameRect(frameRect); | 92 dummyPageHolder().frameView().setFrameRect(frameRect); |
| 120 } | 93 } |
| 121 std::unique_ptr<PaintController> paintController = PaintController::create(); | 94 std::unique_ptr<PaintController> paintController = PaintController::create(); |
| 122 { | 95 { |
| 96 selection().updateForPaintInvalidation(); |
| 123 GraphicsContext context(*paintController); | 97 GraphicsContext context(*paintController); |
| 124 selection().paintCaret(context, LayoutPoint()); | 98 selection().paintCaret(context, LayoutPoint()); |
| 125 } | 99 } |
| 126 paintController->commitNewDisplayItems(); | 100 paintController->commitNewDisplayItems(); |
| 127 EXPECT_EQ(startCount, layoutCount()); | 101 EXPECT_EQ(startCount, layoutCount()); |
| 128 } | 102 } |
| 129 | 103 |
| 130 TEST_F(FrameSelectionTest, InvalidatePreviousCaretAfterRemovingLastCharacter) { | |
| 131 Text* text = appendTextNode("Hello, World!"); | |
| 132 document().view()->updateAllLifecyclePhases(); | |
| 133 | |
| 134 document().body()->setContentEditable("true", ASSERT_NO_EXCEPTION); | |
| 135 document().body()->focus(); | |
| 136 EXPECT_TRUE(document().body()->isFocused()); | |
| 137 | |
| 138 selection().setCaretVisible(true); | |
| 139 EXPECT_TRUE(selection().isCaret()); | |
| 140 EXPECT_TRUE(shouldPaintCaretForTesting()); | |
| 141 | |
| 142 // Simulate to type "Hello, World!". | |
| 143 DisableCompositingQueryAsserts disabler; | |
| 144 document().updateStyleAndLayout(); | |
| 145 selection().setSelection( | |
| 146 SelectionInDOMTree::Builder().collapse(selection().end()).build()); | |
| 147 selection().setCaretRectNeedsUpdate(); | |
| 148 EXPECT_TRUE(isCaretBoundsDirty()); | |
| 149 EXPECT_FALSE(isPreviousCaretDirtyForTesting()); | |
| 150 selection().invalidateCaretRect(); | |
| 151 EXPECT_FALSE(isCaretBoundsDirty()); | |
| 152 EXPECT_TRUE(isPreviousCaretDirtyForTesting()); | |
| 153 | |
| 154 // Simulate to remove all except for "H". | |
| 155 text->replaceWholeText("H"); | |
| 156 document().updateStyleAndLayout(); | |
| 157 selection().setSelection( | |
| 158 SelectionInDOMTree::Builder().collapse(selection().end()).build()); | |
| 159 selection().setCaretRectNeedsUpdate(); | |
| 160 EXPECT_TRUE(isCaretBoundsDirty()); | |
| 161 // "H" remains so early previousCaret invalidation isn't needed. | |
| 162 EXPECT_TRUE(isPreviousCaretDirtyForTesting()); | |
| 163 selection().invalidateCaretRect(); | |
| 164 EXPECT_FALSE(isCaretBoundsDirty()); | |
| 165 EXPECT_TRUE(isPreviousCaretDirtyForTesting()); | |
| 166 | |
| 167 // Simulate to remove the last character. | |
| 168 document().body()->removeChild(text); | |
| 169 // This line is the objective of this test. | |
| 170 // As removing the last character, early previousCaret invalidation is | |
| 171 // executed. | |
| 172 EXPECT_FALSE(isPreviousCaretDirtyForTesting()); | |
| 173 document().updateStyleAndLayoutIgnorePendingStylesheets(); | |
| 174 selection().setCaretRectNeedsUpdate(); | |
| 175 EXPECT_TRUE(isCaretBoundsDirty()); | |
| 176 EXPECT_FALSE(isPreviousCaretDirtyForTesting()); | |
| 177 selection().invalidateCaretRect(); | |
| 178 EXPECT_FALSE(isCaretBoundsDirty()); | |
| 179 EXPECT_TRUE(isPreviousCaretDirtyForTesting()); | |
| 180 } | |
| 181 | |
| 182 #define EXPECT_EQ_SELECTED_TEXT(text) \ | 104 #define EXPECT_EQ_SELECTED_TEXT(text) \ |
| 183 EXPECT_EQ(text, WebString(selection().selectedText()).utf8()) | 105 EXPECT_EQ(text, WebString(selection().selectedText()).utf8()) |
| 184 | 106 |
| 185 TEST_F(FrameSelectionTest, SelectWordAroundPosition) { | 107 TEST_F(FrameSelectionTest, SelectWordAroundPosition) { |
| 186 // "Foo Bar Baz," | 108 // "Foo Bar Baz," |
| 187 Text* text = appendTextNode("Foo Bar Baz,"); | 109 Text* text = appendTextNode("Foo Bar Baz,"); |
| 188 updateAllLifecyclePhases(); | 110 updateAllLifecyclePhases(); |
| 189 | 111 |
| 190 // "Fo|o Bar Baz," | 112 // "Fo|o Bar Baz," |
| 191 EXPECT_TRUE(selection().selectWordAroundPosition( | 113 EXPECT_TRUE(selection().selectWordAroundPosition( |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 selection().updateIfNeeded(); | 248 selection().updateIfNeeded(); |
| 327 | 249 |
| 328 // TODO(yosin): Once lazy canonicalization implemented, selection.start | 250 // TODO(yosin): Once lazy canonicalization implemented, selection.start |
| 329 // should be Position(HTML, 0). | 251 // should be Position(HTML, 0). |
| 330 EXPECT_EQ(Position(), selection().start()) | 252 EXPECT_EQ(Position(), selection().start()) |
| 331 << "updateIfNeeded() makes selection to null."; | 253 << "updateIfNeeded() makes selection to null."; |
| 332 EXPECT_EQ(selection().start(), caretPosition().position()); | 254 EXPECT_EQ(selection().start(), caretPosition().position()); |
| 333 } | 255 } |
| 334 | 256 |
| 335 } // namespace blink | 257 } // namespace blink |
| OLD | NEW |