summaryrefslogtreecommitdiffstats
path: root/devtools/shared/network-observer/test/browser/head.js
blob: deb7becff67c6e88c389b4ff2327567fd19e3a2c (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

ChromeUtils.defineESModuleGetters(this, {
  NetworkObserver:
    "resource://devtools/shared/network-observer/NetworkObserver.sys.mjs",
});

const TEST_DIR = gTestPath.substr(0, gTestPath.lastIndexOf("/"));
const CHROME_URL_ROOT = TEST_DIR + "/";
const URL_ROOT = CHROME_URL_ROOT.replace(
  "chrome://mochitests/content/",
  "https://example.com/"
);

/**
 * Load the provided url in an existing browser.
 * Returns a promise which will resolve when the page is loaded.
 *
 * @param {Browser} browser
 *     The browser element where the URL should be loaded.
 * @param {String} url
 *     The URL to load in the new tab
 */
async function loadURL(browser, url) {
  const loaded = BrowserTestUtils.browserLoaded(browser);
  BrowserTestUtils.startLoadingURIString(browser, url);
  return loaded;
}

/**
 * Create a new foreground tab loading the provided url.
 * Returns a promise which will resolve when the page is loaded.
 *
 * @param {String} url
 *     The URL to load in the new tab
 */
async function addTab(url) {
  const tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, url);
  registerCleanupFunction(() => {
    gBrowser.removeTab(tab);
  });
  return tab;
}

/**
 * Base network event owner class implementing all mandatory callbacks and
 * keeping track of which callbacks have been called.
 */
class NetworkEventOwner {
  hasEventTimings = false;
  hasResponseCache = false;
  hasResponseContent = false;
  hasResponseStart = false;
  hasSecurityInfo = false;
  hasServerTimings = false;

  addEventTimings() {
    this.hasEventTimings = true;
  }
  addResponseCache() {
    this.hasResponseCache = true;
  }
  addResponseContent() {
    this.hasResponseContent = true;
  }
  addResponseStart() {
    this.hasResponseStart = true;
  }
  addSecurityInfo() {
    this.hasSecurityInfo = true;
  }
  addServerTimings() {
    this.hasServerTimings = true;
  }
  addServiceWorkerTimings() {
    this.hasServiceWorkerTimings = true;
  }
}

/**
 * Create a simple network event owner, with mock implementations of all
 * the expected APIs for a NetworkEventOwner.
 */
function createNetworkEventOwner(event) {
  return new NetworkEventOwner();
}

/**
 * Wait for network events matching the provided URL, until the count reaches
 * the provided expected count.
 *
 * @param {string|null} expectedUrl
 *     The URL which should be monitored by the NetworkObserver.If set to null watch for
 *      all requests
 * @param {number} expectedRequestsCount
 *     How many different events (requests) are expected.
 * @returns {Promise}
 *     A promise which will resolve with an array of network event owners, when
 *     the expected event count is reached.
 */
async function waitForNetworkEvents(expectedUrl = null, expectedRequestsCount) {
  const events = [];
  const networkObserver = new NetworkObserver({
    ignoreChannelFunction: channel =>
      expectedUrl ? channel.URI.spec !== expectedUrl : false,
    onNetworkEvent: () => {
      info("waitForNetworkEvents received a new event");
      const owner = createNetworkEventOwner();
      events.push(owner);
      return owner;
    },
  });
  registerCleanupFunction(() => networkObserver.destroy());

  info("Wait until the events count reaches " + expectedRequestsCount);
  await BrowserTestUtils.waitForCondition(
    () => events.length >= expectedRequestsCount
  );
  return events;
}