File: SpeechRecognition-installOnDevice.https.html

package info (click to toggle)
thunderbird 1%3A140.4.0esr-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,609,432 kB
  • sloc: cpp: 7,672,442; javascript: 5,901,613; ansic: 3,898,954; python: 1,413,343; xml: 653,997; asm: 462,286; java: 180,927; sh: 113,489; makefile: 20,460; perl: 14,288; objc: 13,059; yacc: 4,583; pascal: 3,352; lex: 1,720; ruby: 1,222; exp: 762; sql: 715; awk: 580; php: 436; lisp: 430; sed: 70; csh: 10
file content (290 lines) | stat: -rw-r--r-- 9,945 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<!DOCTYPE html>
<title>SpeechRecognition installOnDevice</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script>
promise_test(async (t) => {
  const validLang = "en-US";
  const validLangAlternateLocale = "en-GB";
  const invalidLang = "invalid language code";
  window.SpeechRecognition =
    window.SpeechRecognition || window.webkitSpeechRecognition;

  // Check the availablility of the on-device language pack.
  const initialAvailabilityPromise =
    SpeechRecognition.availableOnDevice(validLang);
  assert_true(
    initialAvailabilityPromise instanceof Promise,
    "availableOnDevice should return a Promise."
  );

  const initialAvailabilityResult = await initialAvailabilityPromise;
  assert_true(
    typeof initialAvailabilityResult === "string",
    "The resolved value of the availableOnDevice promise should be a string."
  );

  if (initialAvailabilityResult === "downloadable") {
    // Attempt to call installOnDevice directly, without a user gesture with a
    // language that is downloadable but not installed.
    const installWithoutUserGesturePromise =
      SpeechRecognition.installOnDevice(validLang);

    // Assert that the promise rejects with NotAllowedError.
    await promise_rejects_dom(
      t,
      "NotAllowedError",
      window.DOMException,
      installWithoutUserGesturePromise, "SpeechRecognition.installOnDevice() " +
      "must reject with NotAllowedError if called without a user gesture."
    );

    // Test that it returns a promise when called with a valid language.
    const validResultPromise = test_driver.bless(
      "Call SpeechRecognition.installOnDevice with a valid language",
      () => SpeechRecognition.installOnDevice(validLang)
    );
    assert_true(
      validResultPromise instanceof Promise,
      "installOnDevice (with gesture) should return a Promise."
    );

    // Verify the resolved value is a boolean.
    const validResult = await validResultPromise;
    assert_true(
      typeof validResult === "boolean",
      "The resolved value of the installOnDevice promise should be a boolean."
    );

    // Verify that the method returns true when called with a supported language.
    assert_equals(
      validResult,
      true,
      "installOnDevice should resolve with `true` when called with a " +
      "supported language code."
    );

    // Verify that the newly installed language pack is available.
    const availableOnDeviceResultPromise =
      SpeechRecognition.availableOnDevice(validLang);
    assert_true(
      availableOnDeviceResultPromise instanceof Promise,
      "availableOnDevice should return a Promise."
    );

    const availableOnDeviceResult = await availableOnDeviceResultPromise;
    assert_true(
      typeof availableOnDeviceResult === "string",
      "The resolved value of the availableOnDevice promise should be a string."
    );

    assert_true(
      availableOnDeviceResult === "available",
      "The resolved value of the availableOnDevice promise should be " +
      "'available'."
    );

    // Verify that the newly installed language pack is available for an alternate locale.
    const alternateLocaleResultPromise =
      SpeechRecognition.availableOnDevice(validLangAlternateLocale);
    assert_true(
      alternateLocaleResultPromise instanceof Promise,
      "availableOnDevice should return a Promise."
    );

    const alternateLocaleResult = await alternateLocaleResultPromise;
    assert_true(
      typeof alternateLocaleResult === "string",
      "The resolved value of the availableOnDevice promise should be a string."
    );

    assert_true(
      alternateLocaleResult === "available",
      "The resolved value of the availableOnDevice promise should be " +
      "'available'."
    );

    // Verify that installing an already installed language resolves to true.
    const secondResultPromise = test_driver.bless(
      "Call SpeechRecognition.installOnDevice for an already installed language",
      () => SpeechRecognition.installOnDevice(validLang)
    );
    assert_true(
      secondResultPromise instanceof Promise,
      "installOnDevice (with gesture, for already installed language) should " +
      "return a Promise."
    );
    const secondResult = await secondResultPromise;
    assert_true(
      typeof secondResult === "boolean",
      "The resolved value of the second installOnDevice promise should be a " +
        "boolean."
    );
    assert_equals(
      secondResult,
      true,
      "installOnDevice should resolve with `true` if the language is already " +
      "installed."
    );
  }

  // Test that it returns a promise and resolves to false for unsupported lang.
  const invalidResultPromise = test_driver.bless(
    "Call SpeechRecognition.installOnDevice with an unsupported language code",
    () => SpeechRecognition.installOnDevice(invalidLang)
  );
  assert_true(
    invalidResultPromise instanceof Promise,
    "installOnDevice (with gesture, for unsupported language) should return " +
      "a Promise."
  );
  const invalidResult = await invalidResultPromise;
  assert_true(
    typeof invalidResult === "boolean",
    "The resolved value of the installOnDevice promise (unsupported language) " +
      "should be a boolean."
  );
  assert_equals(
    invalidResult,
    false,
    "installOnDevice should resolve with `false` when called with an " +
    "unsupported language code."
  );
}, "SpeechRecognition.installOnDevice resolves with a boolean value " +
   "(with user gesture).");

promise_test(async (t) => {
  const iframe = document.createElement("iframe");
  document.body.appendChild(iframe);
  const frameWindow = iframe.contentWindow;
  const frameDOMException = frameWindow.DOMException;
  const frameSpeechRecognition =
    frameWindow.SpeechRecognition || frameWindow.webkitSpeechRecognition;

  iframe.remove();
  await promise_rejects_dom(
    t,
    "InvalidStateError",
    frameDOMException,
    test_driver.bless(
      "Call SpeechRecognition.installOnDevice in a detached frame context",
      () => {
        return frameSpeechRecognition.installOnDevice("en-US");
      }
    )
  );
}, "SpeechRecognition.installOnDevice rejects in a detached context.");

promise_test(async (t) => {
  const iframe = document.createElement("iframe");
  iframe.setAttribute("allow",
    "on-device-speech-recognition 'none'");
  document.body.appendChild(iframe);
  t.add_cleanup(() => iframe.remove());

  await new Promise(resolve => {
    if (iframe.contentWindow &&
        iframe.contentWindow.document.readyState === 'complete') {
      resolve();
    } else {
      iframe.onload = resolve;
    }
  });

  const frameWindow = iframe.contentWindow;
  const frameSpeechRecognition = frameWindow.SpeechRecognition ||
    frameWindow.webkitSpeechRecognition;
  const frameDOMException = frameWindow.DOMException;

  assert_true(!!frameSpeechRecognition,
    "SpeechRecognition should exist in iframe.");
  assert_true(!!frameSpeechRecognition.installOnDevice,
    "installOnDevice method should exist on SpeechRecognition in iframe.");

  await promise_rejects_dom(
    t,
    "NotAllowedError",
    frameDOMException,
    frameSpeechRecognition.installOnDevice("en-US"),
    "installOnDevice should reject with NotAllowedError if " +
    "'install-on-device-speech-recognition' Permission Policy is " +
    "disabled."
  );
}, "SpeechRecognition.installOnDevice rejects if " +
  "'install-on-device-speech-recognition' Permission Policy is disabled.");

promise_test(async (t) => {
  const html = `
    <!DOCTYPE html>
    <script>
      window.addEventListener('message', async (event) => {
        try {
          const SpeechRecognition = window.SpeechRecognition ||
                                    window.webkitSpeechRecognition;
          if (!SpeechRecognition || !SpeechRecognition.installOnDevice) {
            parent.postMessage({
              type: "rejection",
              name: "NotSupportedError",
              message: "API not available"
            }, "*");
            return;
          }

          await SpeechRecognition.installOnDevice("en-US");
          parent.postMessage({ type: "resolution", result: "success" }, "*");
        } catch (err) {
          parent.postMessage({
            type: "rejection",
            name: err.name,
            message: err.message
          }, "*");
        }
      });
    <\/script>
  `;

  // Create a cross-origin Blob URL by fetching from remote origin
  const blob = new Blob([html], { type: "text/html" });
  const blobUrl = URL.createObjectURL(blob);

  const iframe = document.createElement("iframe");
  iframe.src = blobUrl;
  iframe.setAttribute("sandbox", "allow-scripts");
  document.body.appendChild(iframe);
  t.add_cleanup(() => iframe.remove());

  await new Promise(resolve => iframe.onload = resolve);

  const testResult = await new Promise((resolve, reject) => {
    const timeoutId = t.step_timeout(() => {
      reject(new Error("Timed out waiting for message from iframe"));
    }, 6000);

    window.addEventListener("message", t.step_func((event) => {
      if (event.source !== iframe.contentWindow) return;
      clearTimeout(timeoutId);
      resolve(event.data);
    }));

    iframe.contentWindow.postMessage("runTest", "*");
  });

  assert_equals(
    testResult.type,
    "rejection",
    "Should reject due to cross-origin restriction"
  );
  assert_equals(
    testResult.name,
    "NotAllowedError",
    "Should reject with NotAllowedError"
  );
  assert_true(
    testResult.message.includes("cross-origin iframe") ||
    testResult.message.includes("cross-site subframe"),
    `Error message should reference cross-origin. Got: "${testResult.message}"`
  );
}, "SpeechRecognition.installOnDevice should reject in a cross-origin iframe.");
</script>