Commit e6f38d3f authored by Lei Shi's avatar Lei Shi Committed by Chromium LUCI CQ

Fix negative offset in Select-to-Speak that causes issues when resuming

When resuming from the end of a nodeGroupItem,
findNodeFromNodeGroupByCharIndex returns a node with a -1 offset. This
will throw exceptions. This CL fixes this bug.

Bug: 1167260
Change-Id: I2fe466a07f3daa2103dec53e6ebcedd5be27d285
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2638863
Commit-Queue: Lei Shi <leileilei@google.com>
Reviewed-by: default avatarKatie Dektar <katie@chromium.org>
Cr-Commit-Position: refs/heads/master@{#845406}
parent 99796755
......@@ -505,9 +505,18 @@ export class ParagraphUtils {
// We iterate over each nodeGroupItem until the |charIndex| is pointing
// before the current node's name.
if (currentNodeGroupItem.startChar + currentNodeNameLength > charIndex) {
// The currentNodeOffset is the position of the |charIndex| within the
// current nodeGroupItem.
const currentNodeOffset = charIndex - currentNodeGroupItem.startChar;
// The |currentNodeOffset| is the position of the |charIndex| within the
// current nodeGroupItem. ParagraphUtils.getNodeName returns a string
// without an ending space character, though |charIndex| is based on a
// string that was generated with an extra space character between each
// nodeGroupItems. Thus, there might be a gap between the previous
// nodeGroupItem and the current nodeGroupItem. If
// |currentNodeGroupItem.startChar| is greater than |charIndex|, that
// means the |charIndex| is pointing to the gap. In such case, we set
// |currentNodeOffset| to 0 and return the beginning of the current
// nodeGroupItem.
const currentNodeOffset =
Math.max(0, charIndex - currentNodeGroupItem.startChar);
if (!currentNodeGroupItem.hasInlineText) {
// If the nodeGroupItem does not have inline text, we return the
// corresponding node and the current offset.
......
......@@ -567,6 +567,17 @@ SYNC_TEST_F(
assertEquals(result.node.name, fourthInline);
assertEquals(result.offset, 3);
// Pointing to the gap between the fourthInline and thirdStatic.
result = ParagraphUtils.findNodeFromNodeGroupByCharIndex(
nodeGroup, 50 /* charIndex */);
assertEquals(result.node.name, thirdStatic);
assertEquals(result.offset, 0);
result = ParagraphUtils.findNodeFromNodeGroupByCharIndex(
nodeGroup, 51 /* charIndex */);
assertEquals(result.node.name, thirdStatic);
assertEquals(result.offset, 0);
result = ParagraphUtils.findNodeFromNodeGroupByCharIndex(
nodeGroup, 52 /* charIndex */);
assertEquals(result.node.name, thirdStatic);
......
......@@ -433,6 +433,41 @@ TEST_F(
});
});
TEST_F(
'SelectToSpeakNavigationControlTest', 'PauseResumeAtTheEndOfNodeGroupItem',
function() {
const bodyHtml = `
<p id="p1">Sentence <span>one</span>. Sentence two.</p>
`;
this.runWithLoadedTree(
this.generateHtmlWithSelectedElement('p1', bodyHtml), () => {
this.triggerReadSelectedText();
// Finishes the second word.
this.mockTts.speakUntilCharIndex(13);
assertTrue(this.mockTts.currentlySpeaking());
assertEquals(this.mockTts.pendingUtterances().length, 1);
this.assertEqualsCollapseWhitespace(
this.mockTts.pendingUtterances()[0],
'Sentence one . Sentence two.');
// Hitting pause will stop the current TTS.
selectToSpeak.onSelectToSpeakPanelAction_(
chrome.accessibilityPrivate.SelectToSpeakPanelAction.PAUSE);
assertFalse(this.mockTts.currentlySpeaking());
assertEquals(this.mockTts.pendingUtterances().length, 0);
// Hitting resume will start from the remaining content of the
// paragraph.
selectToSpeak.onSelectToSpeakPanelAction_(
chrome.accessibilityPrivate.SelectToSpeakPanelAction.RESUME);
assertTrue(this.mockTts.currentlySpeaking());
assertEquals(this.mockTts.pendingUtterances().length, 1);
this.assertEqualsCollapseWhitespace(
this.mockTts.pendingUtterances()[0], '. Sentence two.');
});
});
TEST_F(
'SelectToSpeakNavigationControlTest', 'PauseResumeFromKeystrokeSelection',
function() {
......
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