| 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 { | 45 bool shouldPaintCaretForTesting() const { |
| 49 return selection().shouldPaintCaretForTesting(); | 46 return selection().shouldPaintCaretForTesting(); |
| 50 } | 47 } |
| 51 bool isPreviousCaretDirtyForTesting() const { | |
| 52 return selection().isPreviousCaretDirtyForTesting(); | |
| 53 } | |
| 54 | 48 |
| 55 PositionWithAffinity caretPosition() const { | 49 PositionWithAffinity caretPosition() const { |
| 56 return selection().m_frameCaret->caretPosition(); | 50 return selection().m_frameCaret->caretPosition(); |
| 57 } | 51 } |
| 58 | 52 |
| 59 private: | 53 private: |
| 60 Persistent<Text> m_textNode; | 54 Persistent<Text> m_textNode; |
| 61 }; | 55 }; |
| 62 | 56 |
| 63 Text* FrameSelectionTest::appendTextNode(const String& data) { | 57 Text* FrameSelectionTest::appendTextNode(const String& data) { |
| 64 Text* text = document().createTextNode(data); | 58 Text* text = document().createTextNode(data); |
| 65 document().body()->appendChild(text); | 59 document().body()->appendChild(text); |
| 66 return text; | 60 return text; |
| 67 } | 61 } |
| 68 | 62 |
| 69 TEST_F(FrameSelectionTest, SetValidSelection) { | 63 TEST_F(FrameSelectionTest, SetValidSelection) { |
| 70 Text* text = appendTextNode("Hello, World!"); | 64 Text* text = appendTextNode("Hello, World!"); |
| 71 document().view()->updateAllLifecyclePhases(); | 65 document().view()->updateAllLifecyclePhases(); |
| 72 selection().setSelection( | 66 selection().setSelection( |
| 73 SelectionInDOMTree::Builder() | 67 SelectionInDOMTree::Builder() |
| 74 .setBaseAndExtent(Position(text, 0), Position(text, 5)) | 68 .setBaseAndExtent(Position(text, 0), Position(text, 5)) |
| 75 .build()); | 69 .build()); |
| 76 EXPECT_FALSE(selection().isNone()); | 70 EXPECT_FALSE(selection().isNone()); |
| 77 } | 71 } |
| 78 | 72 |
| 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) { | 73 TEST_F(FrameSelectionTest, PaintCaretShouldNotLayout) { |
| 99 Text* text = appendTextNode("Hello, World!"); | 74 Text* text = appendTextNode("Hello, World!"); |
| 100 document().view()->updateAllLifecyclePhases(); | 75 document().view()->updateAllLifecyclePhases(); |
| 101 | 76 |
| 102 document().body()->setContentEditable("true", ASSERT_NO_EXCEPTION); | 77 document().body()->setContentEditable("true", ASSERT_NO_EXCEPTION); |
| 103 document().body()->focus(); | 78 document().body()->focus(); |
| 104 EXPECT_TRUE(document().body()->isFocused()); | 79 EXPECT_TRUE(document().body()->isFocused()); |
| 105 | 80 |
| 106 selection().setCaretVisible(true); | 81 selection().setCaretVisible(true); |
| 107 selection().setSelection( | 82 selection().setSelection( |
| (...skipping 12 matching lines...) Expand all Loading... |
| 120 } | 95 } |
| 121 std::unique_ptr<PaintController> paintController = PaintController::create(); | 96 std::unique_ptr<PaintController> paintController = PaintController::create(); |
| 122 { | 97 { |
| 123 GraphicsContext context(*paintController); | 98 GraphicsContext context(*paintController); |
| 124 selection().paintCaret(context, LayoutPoint()); | 99 selection().paintCaret(context, LayoutPoint()); |
| 125 } | 100 } |
| 126 paintController->commitNewDisplayItems(); | 101 paintController->commitNewDisplayItems(); |
| 127 EXPECT_EQ(startCount, layoutCount()); | 102 EXPECT_EQ(startCount, layoutCount()); |
| 128 } | 103 } |
| 129 | 104 |
| 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) \ | 105 #define EXPECT_EQ_SELECTED_TEXT(text) \ |
| 183 EXPECT_EQ(text, WebString(selection().selectedText()).utf8()) | 106 EXPECT_EQ(text, WebString(selection().selectedText()).utf8()) |
| 184 | 107 |
| 185 TEST_F(FrameSelectionTest, SelectWordAroundPosition) { | 108 TEST_F(FrameSelectionTest, SelectWordAroundPosition) { |
| 186 // "Foo Bar Baz," | 109 // "Foo Bar Baz," |
| 187 Text* text = appendTextNode("Foo Bar Baz,"); | 110 Text* text = appendTextNode("Foo Bar Baz,"); |
| 188 updateAllLifecyclePhases(); | 111 updateAllLifecyclePhases(); |
| 189 | 112 |
| 190 // "Fo|o Bar Baz," | 113 // "Fo|o Bar Baz," |
| 191 EXPECT_TRUE(selection().selectWordAroundPosition( | 114 EXPECT_TRUE(selection().selectWordAroundPosition( |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 selection().updateIfNeeded(); | 249 selection().updateIfNeeded(); |
| 327 | 250 |
| 328 // TODO(yosin): Once lazy canonicalization implemented, selection.start | 251 // TODO(yosin): Once lazy canonicalization implemented, selection.start |
| 329 // should be Position(HTML, 0). | 252 // should be Position(HTML, 0). |
| 330 EXPECT_EQ(Position(), selection().start()) | 253 EXPECT_EQ(Position(), selection().start()) |
| 331 << "updateIfNeeded() makes selection to null."; | 254 << "updateIfNeeded() makes selection to null."; |
| 332 EXPECT_EQ(selection().start(), caretPosition().position()); | 255 EXPECT_EQ(selection().start(), caretPosition().position()); |
| 333 } | 256 } |
| 334 | 257 |
| 335 } // namespace blink | 258 } // namespace blink |
| OLD | NEW |