summaryrefslogtreecommitdiffstats
path: root/accessible/tests/mochitest/states/test_doc_busy.html
blob: 701c749aca30cbe2c27ade5b88ea55f184fbee9f (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
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
<!DOCTYPE html>
<html>
<head>
  <title>states of document</title>
  <link rel="stylesheet" type="text/css"
        href="chrome://mochikit/content/tests/SimpleTest/test.css" />

  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>

  <script type="application/javascript"
          src="../common.js"></script>
  <script type="application/javascript"
          src="../role.js"></script>
  <script type="application/javascript"
          src="../states.js"></script>
  <script type="application/javascript"
          src="../promisified-events.js"></script>

  <script type="application/javascript">
    const { BrowserTestUtils } = ChromeUtils.importESModule(
      "resource://testing-common/BrowserTestUtils.sys.mjs");
    const { Downloads } = ChromeUtils.importESModule(
      "resource://gre/modules/Downloads.sys.mjs");

    function matchDocBusyChange(isBusy) {
      return function(event) {
        const scEvent = event.QueryInterface(nsIAccessibleStateChangeEvent);
        return (
          event.DOMNode == document &&
          scEvent.state === STATE_BUSY &&
          scEvent.isEnabled === isBusy
        );
      };
    }

    function getDownloadStartedPromise() {
      return Downloads.getList(Downloads.PUBLIC).then(list => {
        return new Promise(resolve => {
          list.addView({
            onDownloadAdded(download) {
              resolve(download);
            }
          })
        });
      });
    }

    async function downloadHandled(downloadResult) {
      if (Window.isInstance(downloadResult)) {
        return BrowserTestUtils.closeWindow(downloadResult);
      }
      // downloadResult is a download object.
      await downloadResult.finalize(true);
      return Downloads.getList(Downloads.PUBLIC).then(list => list.remove(downloadResult));
    }

    async function doTest() {
      // Because of variable timing, there are two acceptable possibilities:
      // 1. We get an event for busy and an event for not busy.
      // 2. The two events get coalesced, so no events are fired.
      // However, we fail this test if we get the first event but not the
      // second.
      let gotBusy = false;
      let gotNotBusy = false;
      const busyEvents = async function() {
        await waitForEvent(EVENT_STATE_CHANGE, matchDocBusyChange(true));
        info("Got busy event");
        gotBusy = true;
        await waitForEvent(EVENT_STATE_CHANGE, matchDocBusyChange(false));
        info("Got not-busy event");
        gotNotBusy = true;
      }();

      const downloadStarted = getDownloadStartedPromise();

      info("Clicking link to trigger download");
      synthesizeMouse(getNode("link"), 1, 1, {});
      info("Waiting for download prompt to open");
      const downloadResult = await downloadStarted;

      // Once we no longer show a dialog for downloads, the not busy event
      // might show up a bit later.
      if (gotBusy && !gotNotBusy) {
        await busyEvents;
      }

      // Any busy events should have been fired by the time the download
      // prompt has opened.
      if (gotBusy && gotNotBusy) {
        ok(true, "Got both busy change and not-busy change");
      } else if (!gotBusy && !gotNotBusy) {
        ok(true, "No busy events, coalesced");
      } else {
        ok(false, "Got busy change but didn't get not-busy change!");
      }
      testStates(document, 0, 0, STATE_BUSY, 0, "Document not busy");

      // Clean up.
      info("Closing download prompt");
      await downloadHandled(downloadResult);
      // We might still be waiting on busy events. Remove any pending observers.
      for (let observer of Services.obs.enumerateObservers(
        "accessible-event")
      ) {
        Services.obs.removeObserver(observer, "accessible-event");
      }

      SimpleTest.finish();
    }

    SimpleTest.waitForExplicitFinish();
    addA11yLoadEvent(doTest);
  </script>
</head>

<body>

  <a target="_blank"
     title="Missing busy state change event when downloading files"
     href="https://bugzilla.mozilla.org/show_bug.cgi?id=446469">Bug 446469</a>

  <p id="display"></p>
  <div id="content" style="display: none"></div>
  <pre id="test">
  </pre>

  <a id="link" href="http://example.com/a11y/accessible/tests/mochitest/dumbfile.zip">a file</a>
</body>
</html>