Commit a597b417 authored by Nektarios Paisios's avatar Nektarios Paisios Committed by Commit Bot

Adds a unit test for testing upstream affinity in content editables

This currently doesn't work. Any help appreciated.
1. Modifying the selection by moving forward to the end of the current line works.
I verified by using the same HTML in Chrome.
2. The affinity of the DOM selection is not set to upstream as it should have been.
3. As expected, the affinity of the AXSelection is also not set to upstream.
4. The width of the first line is not 15 characters, even though
the width in the style rule is set to "15ch" and the font is a monospace one.
R=dmazzoni@chromium.org, yosin@chromium.org

Bug: 639340
Change-Id: I6b7254107c5f7ca96bf6bd38fe55e6824d05e614
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1536752
Commit-Queue: Nektarios Paisios <nektar@chromium.org>
Reviewed-by: default avatarDominic Mazzoni <dmazzoni@chromium.org>
Cr-Commit-Position: refs/heads/master@{#644817}
parent 8740e96c
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "third_party/blink/renderer/core/editing/position.h" #include "third_party/blink/renderer/core/editing/position.h"
#include "third_party/blink/renderer/core/editing/selection_modifier.h" #include "third_party/blink/renderer/core/editing/selection_modifier.h"
#include "third_party/blink/renderer/core/editing/selection_template.h" #include "third_party/blink/renderer/core/editing/selection_template.h"
#include "third_party/blink/renderer/core/editing/set_selection_options.h"
#include "third_party/blink/renderer/core/editing/text_affinity.h" #include "third_party/blink/renderer/core/editing/text_affinity.h"
#include "third_party/blink/renderer/core/frame/settings.h" #include "third_party/blink/renderer/core/frame/settings.h"
#include "third_party/blink/renderer/core/html/forms/text_control_element.h" #include "third_party/blink/renderer/core/html/forms/text_control_element.h"
...@@ -572,7 +573,7 @@ TEST_F(AccessibilitySelectionTest, FromCurrentSelectionInTextField) { ...@@ -572,7 +573,7 @@ TEST_F(AccessibilitySelectionTest, FromCurrentSelectionInTextField) {
EXPECT_EQ(ax_input, ax_selection.Base().ContainerObject()); EXPECT_EQ(ax_input, ax_selection.Base().ContainerObject());
EXPECT_EQ(0, ax_selection.Base().TextOffset()); EXPECT_EQ(0, ax_selection.Base().TextOffset());
EXPECT_EQ(TextAffinity::kDownstream, ax_selection.Base().Affinity()); EXPECT_EQ(TextAffinity::kDownstream, ax_selection.Base().Affinity());
EXPECT_TRUE(ax_selection.Extent().IsTextPosition()); ASSERT_TRUE(ax_selection.Extent().IsTextPosition());
EXPECT_EQ(ax_input, ax_selection.Extent().ContainerObject()); EXPECT_EQ(ax_input, ax_selection.Extent().ContainerObject());
EXPECT_EQ(18, ax_selection.Extent().TextOffset()); EXPECT_EQ(18, ax_selection.Extent().TextOffset());
EXPECT_EQ(TextAffinity::kDownstream, ax_selection.Extent().Affinity()); EXPECT_EQ(TextAffinity::kDownstream, ax_selection.Extent().Affinity());
...@@ -618,7 +619,7 @@ TEST_F(AccessibilitySelectionTest, FromCurrentSelectionInTextarea) { ...@@ -618,7 +619,7 @@ TEST_F(AccessibilitySelectionTest, FromCurrentSelectionInTextarea) {
EXPECT_EQ(ax_textarea, ax_selection.Base().ContainerObject()); EXPECT_EQ(ax_textarea, ax_selection.Base().ContainerObject());
EXPECT_EQ(0, ax_selection.Base().TextOffset()); EXPECT_EQ(0, ax_selection.Base().TextOffset());
EXPECT_EQ(TextAffinity::kDownstream, ax_selection.Base().Affinity()); EXPECT_EQ(TextAffinity::kDownstream, ax_selection.Base().Affinity());
EXPECT_TRUE(ax_selection.Extent().IsTextPosition()); ASSERT_TRUE(ax_selection.Extent().IsTextPosition());
EXPECT_EQ(ax_textarea, ax_selection.Extent().ContainerObject()); EXPECT_EQ(ax_textarea, ax_selection.Extent().ContainerObject());
EXPECT_EQ(53, ax_selection.Extent().TextOffset()); EXPECT_EQ(53, ax_selection.Extent().TextOffset());
EXPECT_EQ(TextAffinity::kDownstream, ax_selection.Extent().Affinity()); EXPECT_EQ(TextAffinity::kDownstream, ax_selection.Extent().Affinity());
...@@ -632,7 +633,7 @@ TEST_F(AccessibilitySelectionTest, FromCurrentSelectionInTextareaWithAffinity) { ...@@ -632,7 +633,7 @@ TEST_F(AccessibilitySelectionTest, FromCurrentSelectionInTextareaWithAffinity) {
SetBodyInnerHTML(R"HTML( SetBodyInnerHTML(R"HTML(
<textarea id="textarea" <textarea id="textarea"
rows="2" cols="15" rows="2" cols="15"
style="font-family: Ahem; width: 15ch;"> style="font-family: monospace; width: 15ch;">
InsideTextareaField. InsideTextareaField.
</textarea> </textarea>
)HTML"); )HTML");
...@@ -646,10 +647,10 @@ TEST_F(AccessibilitySelectionTest, FromCurrentSelectionInTextareaWithAffinity) { ...@@ -646,10 +647,10 @@ TEST_F(AccessibilitySelectionTest, FromCurrentSelectionInTextareaWithAffinity) {
// This test should only be testing accessibility code. Ordinarily we should // This test should only be testing accessibility code. Ordinarily we should
// be setting up the test using Javascript in order to avoid depending on the // be setting up the test using Javascript in order to avoid depending on the
// internal implementation of DOM selection. However, sending the "end" key is // internal implementation of DOM selection. However, the only way I found to
// the only way I found to achieve this, which might be unreliable on certain // get an upstream affinity is to send the "end" key which might be unreliable
// platforms, so we modify the selection using Blink internal functions // on certain platforms, so we modify the selection using Blink internal
// instead. // functions instead.
textarea->focus(); textarea->focus();
Selection().Modify(SelectionModifyAlteration::kMove, Selection().Modify(SelectionModifyAlteration::kMove,
SelectionModifyDirection::kBackward, SelectionModifyDirection::kBackward,
...@@ -683,7 +684,7 @@ TEST_F( ...@@ -683,7 +684,7 @@ TEST_F(
SetBodyInnerHTML(R"HTML( SetBodyInnerHTML(R"HTML(
<textarea id="textarea" <textarea id="textarea"
rows="2" cols="15" rows="2" cols="15"
style="font-family: Ahem; width: 15ch;"> style="font-family: monospace; width: 15ch;">
InsideTextareaField. InsideTextareaField.
</textarea> </textarea>
)HTML"); )HTML");
...@@ -697,10 +698,10 @@ TEST_F( ...@@ -697,10 +698,10 @@ TEST_F(
// This test should only be testing accessibility code. Ordinarily we should // This test should only be testing accessibility code. Ordinarily we should
// be setting up the test using Javascript in order to avoid depending on the // be setting up the test using Javascript in order to avoid depending on the
// internal implementation of DOM selection. However, sending the "end" key is // internal implementation of DOM selection. However, the only way I found to
// the only way I found to achieve this, which might be unreliable on certain // get an upstream affinity is to send the "end" key which might be unreliable
// platforms, so we modify the selection using Blink internal functions // on certain platforms, so we modify the selection using Blink internal
// instead. // functions instead.
textarea->focus(); textarea->focus();
Selection().Modify(SelectionModifyAlteration::kMove, Selection().Modify(SelectionModifyAlteration::kMove,
SelectionModifyDirection::kBackward, SelectionModifyDirection::kBackward,
...@@ -715,7 +716,7 @@ TEST_F( ...@@ -715,7 +716,7 @@ TEST_F(
ASSERT_NE(nullptr, ax_textarea); ASSERT_NE(nullptr, ax_textarea);
ASSERT_EQ(ax::mojom::Role::kTextField, ax_textarea->RoleValue()); ASSERT_EQ(ax::mojom::Role::kTextField, ax_textarea->RoleValue());
const auto ax_selection = AXSelection::FromCurrentSelection(GetDocument()); const auto ax_selection = AXSelection::FromCurrentSelection(text_control);
ASSERT_TRUE(ax_selection.IsValid()); ASSERT_TRUE(ax_selection.IsValid());
EXPECT_TRUE(ax_selection.Base().IsTextPosition()); EXPECT_TRUE(ax_selection.Base().IsTextPosition());
...@@ -1422,6 +1423,63 @@ TEST_F(AccessibilitySelectionTest, InvalidSelectionInTextarea) { ...@@ -1422,6 +1423,63 @@ TEST_F(AccessibilitySelectionTest, InvalidSelectionInTextarea) {
EXPECT_EQ("backward", ToTextControl(*textarea).selectionDirection()); EXPECT_EQ("backward", ToTextControl(*textarea).selectionDirection());
} }
TEST_F(AccessibilitySelectionTest,
FromCurrentSelectionInContenteditableWithAffinity) {
SetBodyInnerHTML(R"HTML(
<div role="textbox" contenteditable id="contenteditable"
style="font-family: monospace; width: 15ch;">
InsideContenteditableTextboxField.
</div>
)HTML");
ASSERT_FALSE(AXSelection::FromCurrentSelection(GetDocument()).IsValid());
Element* const contenteditable =
GetDocument().QuerySelector("div[role=textbox]");
ASSERT_NE(nullptr, contenteditable);
// This test should only be testing accessibility code. Ordinarily we should
// be setting up the test using Javascript in order to avoid depending on the
// internal implementation of DOM selection. However, the only way I found to
// get an upstream affinity is to send the "end" key which might be unreliable
// on certain platforms, so we modify the selection using Blink internal
// functions instead.
contenteditable->focus();
Selection().Modify(SelectionModifyAlteration::kMove,
SelectionModifyDirection::kBackward,
TextGranularity::kDocumentBoundary, SetSelectionBy::kUser);
Selection().Modify(SelectionModifyAlteration::kMove,
SelectionModifyDirection::kForward,
TextGranularity::kLineBoundary, SetSelectionBy::kUser);
UpdateAllLifecyclePhasesForTest();
ASSERT_EQ(TextAffinity::kUpstream,
Selection().GetSelectionInDOMTree().Affinity());
const AXObject* ax_contenteditable =
GetAXObjectByElementId("contenteditable");
ASSERT_NE(nullptr, ax_contenteditable);
ASSERT_EQ(ax::mojom::Role::kTextField, ax_contenteditable->RoleValue());
const AXObject* ax_text = ax_contenteditable->FirstChild();
ASSERT_NE(nullptr, ax_text);
ASSERT_EQ(ax::mojom::Role::kStaticText, ax_text->RoleValue());
const auto ax_selection = AXSelection::FromCurrentSelection(GetDocument());
ASSERT_TRUE(ax_selection.IsValid());
EXPECT_TRUE(ax_selection.Base().IsTextPosition());
EXPECT_EQ(ax_text, ax_selection.Base().ContainerObject());
EXPECT_LE(15, ax_selection.Base().TextOffset());
EXPECT_GT(int{ax_text->ComputedName().length()},
ax_selection.Base().TextOffset());
EXPECT_EQ(TextAffinity::kUpstream, ax_selection.Base().Affinity());
EXPECT_TRUE(ax_selection.Extent().IsTextPosition());
EXPECT_EQ(ax_text, ax_selection.Extent().ContainerObject());
EXPECT_LE(15, ax_selection.Extent().TextOffset());
EXPECT_GT(int{ax_text->ComputedName().length()},
ax_selection.Extent().TextOffset());
EXPECT_EQ(TextAffinity::kUpstream, ax_selection.Extent().Affinity());
}
TEST_F(AccessibilitySelectionTest, TEST_F(AccessibilitySelectionTest,
SelectEachConsecutiveCharacterInContenteditable) { SelectEachConsecutiveCharacterInContenteditable) {
// The text should wrap after each word. // The text should wrap after each word.
...@@ -1442,7 +1500,6 @@ TEST_F(AccessibilitySelectionTest, ...@@ -1442,7 +1500,6 @@ TEST_F(AccessibilitySelectionTest,
const AXObject* ax_contenteditable = const AXObject* ax_contenteditable =
GetAXObjectByElementId("contenteditable"); GetAXObjectByElementId("contenteditable");
ASSERT_NE(nullptr, ax_contenteditable); ASSERT_NE(nullptr, ax_contenteditable);
ASSERT_EQ(ax::mojom::Role::kTextField, ax_contenteditable->RoleValue());
ASSERT_EQ(1, ax_contenteditable->ChildCount()); ASSERT_EQ(1, ax_contenteditable->ChildCount());
const AXObject* ax_static_text = ax_contenteditable->FirstChild(); const AXObject* ax_static_text = ax_contenteditable->FirstChild();
ASSERT_NE(nullptr, ax_static_text); ASSERT_NE(nullptr, ax_static_text);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment