summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/test/browser/browser_ext_webRequest_error_after_stopped_or_closed.js
blob: acf8b4446a3b3c3c7d15f7c86b702cfa8359f1ba (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
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

const SLOW_PAGE =
  getRootDirectory(gTestPath).replace(
    "chrome://mochitests/content",
    "http://www.example.com"
  ) + "file_slowed_document.sjs";

async function runTest(stopLoadFunc) {
  async function background() {
    let urls = ["http://www.example.com/*"];
    browser.webRequest.onCompleted.addListener(
      details => {
        browser.test.sendMessage("done", {
          msg: "onCompleted",
          requestId: details.requestId,
        });
      },
      { urls }
    );

    browser.webRequest.onBeforeRequest.addListener(
      details => {
        browser.test.sendMessage("onBeforeRequest", {
          requestId: details.requestId,
        });
      },
      { urls },
      ["blocking"]
    );

    browser.webRequest.onErrorOccurred.addListener(
      details => {
        browser.test.sendMessage("done", {
          msg: "onErrorOccurred",
          requestId: details.requestId,
        });
      },
      { urls }
    );
  }

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: [
        "webRequest",
        "webRequestBlocking",
        "http://www.example.com/*",
      ],
    },
    background,
  });
  await extension.startup();

  // Open a SLOW_PAGE and don't wait for it to load
  let slowTab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    SLOW_PAGE,
    false
  );

  stopLoadFunc(slowTab);

  // Retrieve the requestId from onBeforeRequest
  let requestIdOnBeforeRequest = await extension.awaitMessage(
    "onBeforeRequest"
  );

  // Now verify that we got the correct event and request id
  let doneMessage = await extension.awaitMessage("done");

  // We shouldn't get the onCompleted message here
  is(doneMessage.msg, "onErrorOccurred", "received onErrorOccurred message");
  is(
    requestIdOnBeforeRequest.requestId,
    doneMessage.requestId,
    "request Ids match"
  );

  BrowserTestUtils.removeTab(slowTab);
  await extension.unload();
}

/**
 * Check that after we cancel a slow page load, we get an error associated with
 * our request.
 */
add_task(async function test_click_stop_button() {
  await runTest(async slowTab => {
    // Stop the load
    let stopButton = document.getElementById("stop-button");
    await TestUtils.waitForCondition(() => {
      return !stopButton.disabled;
    });
    stopButton.click();
  });
});

/**
 * Check that after we close the tab corresponding to a slow page load,
 * that we get an error associated with our request.
 */
add_task(async function test_remove_tab() {
  await runTest(slowTab => {
    // Remove the tab
    BrowserTestUtils.removeTab(slowTab);
  });
});