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

Refactor TTS logic in Select to Speak

Refactored the underlying logic of STS. Previously, STS combines node enqueue, navigation control, and TTS playing in one function (i.e., startSpeechQueue_). I separate these functionalities by adding five navigation state variables. Then, enqueue nodes in startSpeechQueue_, play TTS via startCurrentNodeGroup_, and control navigation by manipulating the navigation state variables.

With this CL, STS will enqueue content to TTS one nodeGroup at a time. Previously, STS enqueued all selected nodeGroups to TTS. To reflect this change, I modify the tests.

More details notes:
1. Refactor function updateNodeHighlight_ as we don't need the opt_endIndex in this function.
2. Create function syncCurrentNodeWithCharIndex_, which reuses code logic from the previous function onTtsWordEvent_.
3. Split function startSpeechQueue_ into (1) startSpeechQueue_, (2) startCurrentNodeGroup_, and (3) startNodeGroupAfter_. Function startSpeechQueue_ adds new content to the speaking queue and starts TTS using startCurrentNodeGroup_, which starts reading the current NodeGroup based on the five navigation state variables. Function startNodeGroupAfter_ allows us the change the navigation state variables to the next NodeGroup.

AX-Relnotes: N/A

Bug: 1143817
Change-Id: I1aa719786108e0f98fda36ebade7536967cfd2ce
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2565698Reviewed-by: default avatarDominic Mazzoni <dmazzoni@chromium.org>
Reviewed-by: default avatarAkihiro Ota <akihiroota@chromium.org>
Commit-Queue: Lei Shi <leileilei@google.com>
Cr-Commit-Position: refs/heads/master@{#833620}
parent 2160977f
......@@ -53,6 +53,13 @@ MockTts.prototype = {
this.speechCallbackStack_.pop()(utterance);
}
},
finishPendingUtterance() {
this.pendingUtterances_ = [];
this.currentlySpeaking_ = false;
if (this.options_) {
this.options_.onEvent({type: 'end'});
}
},
stop() {
this.pendingUtterances_ = [];
this.currentlySpeaking_ = false;
......
......@@ -179,9 +179,11 @@ TEST_F(
assertTrue(this.mockTts.currentlySpeaking());
this.assertEqualsCollapseWhitespace(
this.mockTts.pendingUtterances()[0], 'Selected text');
if (this.mockTts.pendingUtterances().length === 2) {
this.mockTts.finishPendingUtterance();
if (this.mockTts.pendingUtterances().length === 1) {
this.assertEqualsCollapseWhitespace(
this.mockTts.pendingUtterances()[1], '');
this.mockTts.pendingUtterances()[0], '');
}
}),
false);
......@@ -321,11 +323,14 @@ TEST_F(
function() {
this.triggerReadSelectedText();
assertTrue(this.mockTts.currentlySpeaking());
assertEquals(this.mockTts.pendingUtterances().length, 2);
assertEquals(this.mockTts.pendingUtterances().length, 1);
this.assertEqualsCollapseWhitespace(
this.mockTts.pendingUtterances()[0], 'paragraph');
this.mockTts.finishPendingUtterance();
assertEquals(this.mockTts.pendingUtterances().length, 1);
this.assertEqualsCollapseWhitespace(
this.mockTts.pendingUtterances()[1], 'text field');
this.mockTts.pendingUtterances()[0], 'text field');
});
});
......@@ -343,11 +348,14 @@ TEST_F(
function() {
this.triggerReadSelectedText();
assertTrue(this.mockTts.currentlySpeaking());
assertEquals(this.mockTts.pendingUtterances().length, 2);
assertEquals(this.mockTts.pendingUtterances().length, 1);
this.assertEqualsCollapseWhitespace(
this.mockTts.pendingUtterances()[0], 'one');
this.mockTts.finishPendingUtterance();
assertEquals(this.mockTts.pendingUtterances().length, 1);
this.assertEqualsCollapseWhitespace(
this.mockTts.pendingUtterances()[1], 'two three');
this.mockTts.pendingUtterances()[0], 'two three');
});
});
......@@ -470,9 +478,11 @@ TEST_F(
assertTrue(this.mockTts.pendingUtterances().length > 0);
this.assertEqualsCollapseWhitespace(
this.mockTts.pendingUtterances()[0], 'Some text');
if (this.mockTts.pendingUtterances().length > 1) {
this.mockTts.finishPendingUtterance();
if (this.mockTts.pendingUtterances().length > 0) {
this.assertEqualsCollapseWhitespace(
this.mockTts.pendingUtterances()[1], '');
this.mockTts.pendingUtterances()[0], '');
}
});
});
......@@ -542,11 +552,14 @@ TEST_F(
this.runWithLoadedTree(html, function() {
this.triggerReadSelectedText();
assertTrue(this.mockTts.currentlySpeaking());
assertEquals(this.mockTts.pendingUtterances().length, 2);
assertEquals(this.mockTts.pendingUtterances().length, 1);
this.assertEqualsCollapseWhitespace(
this.mockTts.pendingUtterances()[0], 'b c');
this.mockTts.finishPendingUtterance();
assertEquals(this.mockTts.pendingUtterances().length, 1);
this.assertEqualsCollapseWhitespace(
this.mockTts.pendingUtterances()[1], 'd e');
this.mockTts.pendingUtterances()[0], 'd e');
});
});
......@@ -567,8 +580,10 @@ TEST_F(
assertTrue(this.mockTts.currentlySpeaking());
this.assertEqualsCollapseWhitespace(
this.mockTts.pendingUtterances()[0], 'a b c');
this.mockTts.finishPendingUtterance();
this.assertEqualsCollapseWhitespace(
this.mockTts.pendingUtterances()[1], 'd e f');
this.mockTts.pendingUtterances()[0], 'd e f');
});
});
......@@ -619,12 +634,18 @@ TEST_F(
assertTrue(this.mockTts.currentlySpeaking());
this.assertEqualsCollapseWhitespace(
this.mockTts.pendingUtterances()[0], 'Column 1, Text 1');
this.mockTts.finishPendingUtterance();
this.assertEqualsCollapseWhitespace(
this.mockTts.pendingUtterances()[1], 'Column 1, Text 2');
this.mockTts.pendingUtterances()[0], 'Column 1, Text 2');
this.mockTts.finishPendingUtterance();
this.assertEqualsCollapseWhitespace(
this.mockTts.pendingUtterances()[2], 'Column 2, Text 1');
this.mockTts.pendingUtterances()[0], 'Column 2, Text 1');
this.mockTts.finishPendingUtterance();
this.assertEqualsCollapseWhitespace(
this.mockTts.pendingUtterances()[3], 'Column 2, Text 2');
this.mockTts.pendingUtterances()[0], 'Column 2, Text 2');
});
});
TEST_F(
......
......@@ -83,6 +83,7 @@ TEST_F(
assertEquals(this.mockTts.pendingUtterances().length, 1);
this.assertEqualsCollapseWhitespace(
utterance, 'This is some text');
this.mockTts.finishPendingUtterance();
}),
this.newCallback(function(utterance) {
this.assertEqualsCollapseWhitespace(
......
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