summaryrefslogtreecommitdiffstats
path: root/browser/components/resistfingerprinting/test/mochitest/test_speech_synthesis.html
blob: a1778a0a0d435d63550c1dc72d6eaa11760601e9 (plain)
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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1333641
-->
<head>
  <meta charset="utf-8">
  <title>Test for Bug 1333641</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  <script type="application/javascript">

  /** Test for Bug 1333641 */
  SimpleTest.waitForExplicitFinish();
  window.onload = setupSpeechSynthesis;

  // This function setup the speechSynthesis and flip 'privacy.resistFingerprinting'
  // after it has been setup correctly.
  function setupSpeechSynthesis() {
    window.speechSynthesis.addEventListener("voiceschanged", () => {
      isnot(window.speechSynthesis.getVoices().length, 0, "Voices added");
      SimpleTest.waitForFocus(() => {
        SpecialPowers.pushPrefEnv({"set":
          [["privacy.resistFingerprinting", true]],
        }, doGetVoicesTest);
      }, window);
    }, {once: true});

    is(window.speechSynthesis.getVoices().length, 0, "No voices added initially");
  }

  function doGetVoicesTest() {
    is(window.speechSynthesis.getVoices().length, 0,
       "There should be no voices after fingerprinting resistance is enabled.");
    doVoiceschangedEventTest();
  }

  function doVoiceschangedEventTest() {
    window.speechSynthesis.addEventListener("voiceschanged", () => {
      ok(false, "The voiceschanged event should not be fired.");
      doSpeakTestAsync();
    }, {once: true});

    window.addEventListener("TestEvent", () => {
      // If we receive this event without receiving a 'voiceschanged' event, this means
      // the voiceschanged event has been blocked correctly.
      ok(true, "Got the 'TestEvent' event.");
      doSpeakTestAsync();
    }, {once: true});

    // Notify 'synth-voices-changed' for triggering the voiceschanged event.
    SpecialPowers.Services.obs.notifyObservers(null, "synth-voices-changed");
    window.dispatchEvent(new CustomEvent("TestEvent"));
  }

  // This tests Speak() and its asynchronousness.
  function doSpeakTestAsync() {
    // For non-e10s, this test will always fail since the event will be triggered immediately
    // after speak() is called. So, new added events after speak() won't be called. We skip
    // this test if it is non-e10s.
    if (SpecialPowers.Services.appinfo.browserTabsRemoteAutostart) {
      let utterance = new window.SpeechSynthesisUtterance("Hello, world!");
      window.speechSynthesis.speak(utterance);

      utterance.addEventListener("start", () => {
        ok(false, "speechSynthesis should not start speaking if fingerprinting resistance is enabled.");
        doSpeakTestSync();
      }, {once: true});

      utterance.addEventListener("error", () => {
        ok(true, "speechSynthesis.speak should fail if fingerprinting resistance is enabled.");
        doSpeakTestSync();
      }, {once: true});
    } else {
      doSpeakTestSync();
    }
  }

  // This tests Speak() and its synchronousness.
  function doSpeakTestSync() {
    let utterance = new window.SpeechSynthesisUtterance("Hello, world!");
    utterance.addEventListener("start", () => {
      ok(false, "speechSynthesis should not start speaking if fingerprinting resistance is enabled.");
      SimpleTest.finish();
    }, {once: true});

    utterance.addEventListener("error", () => {
      ok(true, "speechSynthesis.speak should fail if fingerprinting resistance is enabled.");
      SimpleTest.finish();
    }, {once: true});

    window.speechSynthesis.speak(utterance);
  }

  </script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=">Mozilla Bug </a>
<p id="display"></p>
<div id="content" style="display: none">

</div>
<pre id="test"></pre>
</body>
</html>