Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(339)

Unified Diff: third_party/WebKit/Source/core/editing/FrameSelectionTest.cpp

Issue 2841093002: Algorithm for deciding if a frame's selection should be hidden (Closed)
Patch Set: Rebase + cosmetic changes Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/core/editing/FrameSelectionTest.cpp
diff --git a/third_party/WebKit/Source/core/editing/FrameSelectionTest.cpp b/third_party/WebKit/Source/core/editing/FrameSelectionTest.cpp
index 67b5b56b54dbad9f382ac7be1078763930e9ab19..53db8f96e91d1dd201df1b71474f84539c3ecab7 100644
--- a/third_party/WebKit/Source/core/editing/FrameSelectionTest.cpp
+++ b/third_party/WebKit/Source/core/editing/FrameSelectionTest.cpp
@@ -349,4 +349,399 @@ TEST_F(FrameSelectionTest, SelectInvalidPositionInFlatTreeDoesntCrash) {
EXPECT_EQ(PositionInFlatTree(foo, 0), selection.Extent());
}
+TEST_F(FrameSelectionTest, CaretInShadowTree) {
+ SetBodyContent("<input id='field'>"); // <input> hosts a shadow tree.
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsNone());
+ EXPECT_FALSE(Selection().IsActive());
+
+ Element* const field = GetDocument().getElementById("field");
+ field->focus();
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsCaret());
+ EXPECT_TRUE(Selection().IsActive());
+
+ field->blur(); // Move focus to document body.
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsCaret());
+ EXPECT_FALSE(Selection().IsActive()); // Caret is now hidden.
+}
+
+TEST_F(FrameSelectionTest, RangeInShadowTree) {
+ SetBodyContent("<input id='field' value='hola'>");
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsNone());
+ EXPECT_FALSE(Selection().IsActive());
+
+ Element* const field = GetDocument().getElementById("field");
+ field->focus();
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsCaret());
+ EXPECT_TRUE(Selection().IsActive());
+
+ Selection().SelectAll();
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsRange());
+ EXPECT_TRUE(Selection().IsActive());
+
+ field->blur(); // Move focus to document body.
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsRange());
+ EXPECT_FALSE(Selection().IsActive()); // Range is now hidden.
+}
+
+// crbug.com/692898
+TEST_F(FrameSelectionTest, FocusingLinkHidesCaretInTextControl) {
+ SetBodyContent(
+ "<input id='field'>"
+ "<a href='www' id='alink'>link</a>");
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsNone());
+ EXPECT_FALSE(Selection().IsActive());
+
+ Element* const field = GetDocument().getElementById("field");
+ field->focus();
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsCaret());
+ EXPECT_TRUE(Selection().IsActive());
+
+ Element* const alink = GetDocument().getElementById("alink");
+ alink->focus();
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsCaret());
+ EXPECT_FALSE(Selection().IsActive());
+}
+
+// crbug.com/692898
+TEST_F(FrameSelectionTest, FocusingLinkHidesRangeInTextControl) {
+ SetBodyContent(
+ "<input id='field' value='hola'>"
+ "<a href='www' id='alink'>link</a>");
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsNone());
+ EXPECT_FALSE(Selection().IsActive());
+
+ Element* const field = GetDocument().getElementById("field");
+ field->focus();
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsCaret());
+ EXPECT_TRUE(Selection().IsActive());
+
+ Selection().SelectAll();
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsRange());
+ EXPECT_TRUE(Selection().IsActive());
+
+ Element* const alink = GetDocument().getElementById("alink");
+ alink->focus();
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsRange());
+ EXPECT_FALSE(Selection().IsActive());
+}
+
+// crbug.com/713051
+TEST_F(FrameSelectionTest, FocusingNonEditableParentHidesCaretInTextControl) {
+ SetBodyContent(
+ "<div tabindex='-1' id='parent'>"
+ " <input id='field'>"
+ "</div>");
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsNone());
+ EXPECT_FALSE(Selection().IsActive());
+
+ Element* const field = GetDocument().getElementById("field");
+ field->focus();
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsCaret());
+ EXPECT_TRUE(Selection().IsActive());
+
+ // Here the selection belongs to <input>'s shadow tree and that tree has a
+ // non-editable parent that is focused.
+ Element* const parent = GetDocument().getElementById("parent");
+ parent->focus();
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsCaret());
+ EXPECT_FALSE(Selection().IsActive()); // Focus is outside <input>
+ // so caret should not be visible.
+
+ parent->blur(); // Move focus to document body.
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsCaret());
+ EXPECT_FALSE(Selection().IsActive()); // Caret is still hidden.
+}
+
+// crbug.com/713051
+TEST_F(FrameSelectionTest, FocusingNonEditableParentHidesRangeInTextControl) {
+ SetBodyContent(
+ "<div tabindex='-1' id='parent'>"
+ " <input id='field' value='hola'>"
+ "</div>");
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsNone());
+ EXPECT_FALSE(Selection().IsActive());
+
+ Element* const field = GetDocument().getElementById("field");
+ field->focus();
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsCaret());
+ EXPECT_TRUE(Selection().IsActive());
+
+ Selection().SelectAll();
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsRange());
+ EXPECT_TRUE(Selection().IsActive());
+
+ // Here the selection belongs to <input>'s shadow tree and that tree has a
+ // non-editable parent that is focused.
+ Element* const parent = GetDocument().getElementById("parent");
+ parent->focus();
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsRange());
+ EXPECT_FALSE(Selection().IsActive()); // Focus is outside <input>
+ // so range should not be visible.
+
+ parent->blur(); // Move focus to document body.
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsRange());
+ EXPECT_FALSE(Selection().IsActive()); // Range is still hidden.
+}
+
+TEST_F(FrameSelectionTest, CaretInFocusedEditableDiv) {
+ SetBodyContent("<div contenteditable id='ce'>blabla</div>");
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsNone());
+ EXPECT_FALSE(Selection().IsActive());
+
+ Element* const ce = GetDocument().getElementById("ce");
+ ce->focus();
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsCaret());
+ EXPECT_TRUE(Selection().IsActive());
+
+ ce->blur(); // Move focus to document body.
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsCaret());
+ EXPECT_FALSE(Selection().IsActive()); // Caret is now hidden.
+}
+
+TEST_F(FrameSelectionTest, RangeInFocusedEditableDiv) {
+ SetBodyContent("<div contenteditable id='ce'>blabla</div>");
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsNone());
+ EXPECT_FALSE(Selection().IsActive());
+
+ Element* const ce = GetDocument().getElementById("ce");
+ ce->focus();
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsCaret());
+ EXPECT_TRUE(Selection().IsActive());
+
+ Selection().SelectAll();
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsRange());
+ EXPECT_TRUE(Selection().IsActive());
+
+ ce->blur(); // Move focus to document body.
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsRange());
+ EXPECT_TRUE(Selection().IsActive()); // Range is still visible.
+}
+
+TEST_F(FrameSelectionTest, FocusingLinkHidesCaretInContentEditable) {
+ SetBodyContent(
+ "<div contenteditable id='ce'>blabla</div>"
+ "<a href='www' id='alink'>link</a>");
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsNone());
+ EXPECT_FALSE(Selection().IsActive());
+
+ Element* const ce = GetDocument().getElementById("ce");
+ ce->focus();
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsCaret());
+ EXPECT_TRUE(Selection().IsActive());
+
+ Element* const alink = GetDocument().getElementById("alink");
+ alink->focus();
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsCaret());
+ EXPECT_FALSE(Selection().IsActive());
+}
+
+TEST_F(FrameSelectionTest, FocusingLinkKeepsRangeInContentEditable) {
+ SetBodyContent(
+ "<div contenteditable id='ce'>blabla</div>"
+ "<a href='www' id='alink'>link</a>");
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsNone());
+ EXPECT_FALSE(Selection().IsActive());
+
+ Element* const ce = GetDocument().getElementById("ce");
+ ce->focus();
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsCaret());
+ EXPECT_TRUE(Selection().IsActive());
+
+ Selection().SelectAll();
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsRange());
+ EXPECT_TRUE(Selection().IsActive());
+
+ Element* const alink = GetDocument().getElementById("alink");
+ alink->focus();
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsRange());
+ EXPECT_TRUE(Selection().IsActive());
+}
+
+TEST_F(FrameSelectionTest, FocusingEditableParentKeepsEditableCaret) {
+ SetBodyContent(
+ "<div contenteditable tabindex='-1' id='parent'>"
+ "<div contenteditable id='ce'>blabla</div>"
+ "</div>");
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsNone());
+ EXPECT_FALSE(Selection().IsActive());
+
+ // TODO(editing-dev): Blink should be able to focus the inner <div>.
+ // Element* const ce = GetDocument().getElementById("ce");
+ // ce->focus();
+ // EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsCaret());
+ // EXPECT_TRUE(Selection().IsActive());
+
+ Element* const parent = GetDocument().getElementById("parent");
+ parent->focus();
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsCaret());
+ EXPECT_TRUE(Selection().IsActive()); // Focus is within editing boundary,
+ // caret should be visible.
+
+ parent->blur(); // Move focus to document body.
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsCaret());
+ EXPECT_FALSE(Selection().IsActive()); // Focus is outside editing boundary
+ // so caret should be hidden.
+}
+
+TEST_F(FrameSelectionTest, FocusingEditableParentKeepsEditableRange) {
+ SetBodyContent(
+ "<div contenteditable tabindex='-1' id='parent'>"
+ "<div contenteditable id='ce'>blabla</div>"
+ "</div>");
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsNone());
+ EXPECT_FALSE(Selection().IsActive());
+
+ // TODO(editing-dev): Blink should be able to focus the inner <div>.
+ // Element* const ce = GetDocument().getElementById("ce");
+ // ce->focus();
+ // EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsCaret());
+ // EXPECT_TRUE(Selection().IsActive());
+
+ // Selection().SelectAll();
+ // EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsRange());
+ // EXPECT_TRUE(Selection().IsActive());
+
+ Element* const parent = GetDocument().getElementById("parent");
+ parent->focus();
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsCaret());
+ EXPECT_TRUE(Selection().IsActive()); // Focus is within editing boundary,
+ // range should be visible.
+
+ Selection().SelectAll();
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsRange());
+ EXPECT_TRUE(Selection().IsActive());
+
+ parent->blur(); // Move focus to document body.
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsRange());
+ EXPECT_TRUE(Selection().IsActive()); // Focus is outside editing boundary
+ // but range should still be visible.
+}
+
+TEST_F(FrameSelectionTest, FocusingNonEditableParentHidesEditableCaret) {
+ SetBodyContent(
+ "<div tabindex='-1' id='parent'>"
+ "<div contenteditable id='ce'>blabla</div>"
+ "</div>");
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsNone());
+ EXPECT_FALSE(Selection().IsActive());
+
+ Element* const ce = GetDocument().getElementById("ce");
+ ce->focus();
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsCaret());
+ EXPECT_TRUE(Selection().IsActive());
+
+ // Here the selection belongs to <divs>'s shadow tree and that tree has a
+ // non-editable parent that is focused.
+ Element* const parent = GetDocument().getElementById("parent");
+ parent->focus();
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsCaret());
+ EXPECT_FALSE(Selection().IsActive()); // Focus is outside editing boundary
+ // so caret should be hidden.
+
+ parent->blur(); // Move focus to document body.
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsCaret());
+ EXPECT_FALSE(Selection().IsActive()); // Caret is still hidden.
+}
+
+TEST_F(FrameSelectionTest, FocusingNonEditableParentKeepsEditableRange) {
+ SetBodyContent(
+ "<div tabindex='-1' id='parent'>"
+ "<div contenteditable id='ce'>blabla</div>"
+ "</div>");
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsNone());
+ EXPECT_FALSE(Selection().IsActive());
+
+ Element* const ce = GetDocument().getElementById("ce");
+ ce->focus();
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsCaret());
+ EXPECT_TRUE(Selection().IsActive());
+
+ Selection().SelectAll();
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsRange());
+ EXPECT_TRUE(Selection().IsActive());
+
+ // Here the selection belongs to <divs>'s shadow tree and that tree has a
+ // non-editable parent that is focused.
+ Element* const parent = GetDocument().getElementById("parent");
+ parent->focus();
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsRange());
+ EXPECT_TRUE(Selection().IsActive()); // Focus is outside editing boundary
+ // but range should still be visible.
+
+ parent->blur(); // Move focus to document body.
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsRange());
+ EXPECT_TRUE(Selection().IsActive()); // Range is still visible.
+}
+
+// crbug.com/707143
+TEST_F(FrameSelectionTest, RangeContainsFocus) {
+ SetBodyContent(
+ "<div>"
+ " <div>"
+ " <span id='start'>start</span>"
+ " </div>"
+ " <a href='www' id='alink'>link</a>"
+ " <div>line 1</div>"
+ " <div id='middle'>line 2</div>"
+ " <div>line 3</div>"
+ " <div>line 4</div>"
+ " <span id='end'>end</span>"
+ " <div></div>"
+ "</div>");
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsNone());
+ EXPECT_FALSE(Selection().IsActive());
+
+ Element* const start = GetDocument().getElementById("start");
+ Element* const end = GetDocument().getElementById("end");
+ Selection().SetSelection(
+ SelectionInDOMTree::Builder()
+ .SetBaseAndExtent(Position(start, 0), Position(end, 0))
+ .Build());
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsRange());
+ EXPECT_TRUE(Selection().IsActive());
+
+ Element* const alink = GetDocument().getElementById("alink");
+ alink->focus();
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsRange());
+ EXPECT_TRUE(Selection().IsActive()); // Range still visible.
+}
+
+// crbug.com/707143
+TEST_F(FrameSelectionTest, RangeOutsideFocus) {
+ // Here the selection sits on a subtree that hasn't the focused element.
+ // This test case is the reason why didn't gave FrameSelection::IsActive() the
+ // name "HasFocus()". Even when the selection's DOM nodes are completely
+ // disconnected from the focused node, we still want the selection to be
+ // visible (thus "active").
+ SetBodyContent(
+ "<a href='www' id='alink'>link</a>"
+ "<div>"
+ " <div>"
+ " <span id='start'>start</span>"
+ " </div>"
+ " <div>line 1</div>"
+ " <div id='middle'>line 2</div>"
+ " <div>line 3</div>"
+ " <div>line 4</div>"
+ " <span id='end'>end</span>"
+ " <div></div>"
+ "</div>");
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsNone());
+ EXPECT_FALSE(Selection().IsActive());
+
+ Element* const start = GetDocument().getElementById("start");
+ Element* const end = GetDocument().getElementById("end");
+ Selection().SetSelection(
+ SelectionInDOMTree::Builder()
+ .SetBaseAndExtent(Position(start, 0), Position(end, 0))
+ .Build());
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsRange());
+ EXPECT_TRUE(Selection().IsActive());
+
+ Element* const alink = GetDocument().getElementById("alink");
+ alink->focus();
+ EXPECT_TRUE(Selection().GetSelectionInDOMTree().IsRange());
+ EXPECT_TRUE(Selection().IsActive()); // Range still visible.
+}
+
} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698