Skip to content

Commit cc51a09

Browse files
Nathan Memmottchromium-wpt-export-bot
Nathan Memmott
authored andcommitted
Translator: Don't initiate model download if CreateMonitorCallback throws
If the CreateMonitorCallback throws an error, then model download should not be initiated. Fixed: 409861269 Change-Id: Ib9f09aad91b431ff9b3a5c77df777c4df7f60997 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6598914 Commit-Queue: Nathan Memmott <[email protected]> Reviewed-by: Daseul Lee <[email protected]> Cr-Commit-Position: refs/heads/main@{#1470158}
1 parent c895835 commit cc51a09

File tree

4 files changed

+32
-12
lines changed

4 files changed

+32
-12
lines changed

ai/language_detection/detector.https.window.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,7 @@ promise_test(async t => {
6666
}, 'LanguageDetector.detect() returns valid results');
6767

6868
promise_test(async t => {
69-
const error = new Error('CreateMonitorCallback threw an error');
70-
function monitor(m) {
71-
m.addEventListener('downloadprogress', e => {
72-
assert_unreached(
73-
'This should never be reached since monitor throws an error.');
74-
});
75-
throw error;
76-
}
77-
78-
await promise_rejects_exactly(t, error, createLanguageDetector({monitor}));
69+
await testCreateMonitorCallbackThrowsError(t, createLanguageDetector);
7970
}, 'If monitor throws an error, LanguageDetector.create() rejects with that error');
8071

8172
promise_test(async t => {

ai/resources/util.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,20 @@ async function testMonitor(createFunc, options = {}) {
182182
return result;
183183
}
184184

185+
async function testCreateMonitorCallbackThrowsError(
186+
t, createFunc, options = {}) {
187+
const error = new Error('CreateMonitorCallback threw an error');
188+
function monitor(m) {
189+
m.addEventListener('downloadprogress', e => {
190+
assert_unreached(
191+
'This should never be reached since monitor throws an error.');
192+
});
193+
throw error;
194+
}
195+
196+
await promise_rejects_exactly(t, error, createFunc({...options, monitor}));
197+
}
198+
185199
function run_iframe_test(iframe, test_name) {
186200
const id = getId();
187201
iframe.contentWindow.postMessage({id, type: test_name}, '*');
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// META: title=Translator tests
2+
// META: global=window
3+
// META: timeout=long
4+
// META: script=../resources/util.js
5+
// META: script=/resources/testdriver.js
6+
// META: script=resources/util.js
7+
//
8+
// Setting `timeout=long` as this test may require downloading the translation
9+
// library and the language models.
10+
11+
promise_test(async t => {
12+
// Can pass in valid but unsupported languages since the create monitor error
13+
// should be thrown before language support is checked.
14+
await testCreateMonitorCallbackThrowsError(
15+
t, createTranslator, {sourceLanguage: 'und', targetLanguage: 'und'});
16+
}, 'If monitor throws an error, LanguageDetector.create() rejects with that error');

ai/translator/translator.optional.https.window.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
// META: title=Translator Translate
1+
// META: title=Optional Translator tests
22
// META: global=window
33
// META: timeout=long
44
// META: script=../resources/util.js
5-
// META: script=../resources/language_codes.js
65
// META: script=/resources/testdriver.js
76
// META: script=resources/util.js
87
//

0 commit comments

Comments
 (0)