summaryrefslogtreecommitdiffstats
path: root/devtools/client/framework/test/helper_disable_cache.js
blob: 31db6d81b400c38c06b80c41bee0076e9796a566 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// This file assumes we have head.js globals for the scope where this is loaded.
/* import-globals-from head.js */

/* exported initTab, checkCacheStateForAllTabs, setDisableCacheCheckboxChecked,
            finishUp */

// Common code shared by browser_toolbox_options_disable_cache-*.js
const TEST_URI = URL_ROOT + "browser_toolbox_options_disable_cache.sjs";
var tabs = [
  {
    title: "Tab 0",
    desc: "Toggles cache on.",
    startToolbox: true,
  },
  {
    title: "Tab 1",
    desc: "Toolbox open before Tab 1 toggles cache.",
    startToolbox: true,
  },
  {
    title: "Tab 2",
    desc: "Opens toolbox after Tab 1 has toggled cache. Also closes and opens.",
    startToolbox: false,
  },
  {
    title: "Tab 3",
    desc: "No toolbox",
    startToolbox: false,
  },
];

async function initTab(tabX, startToolbox) {
  tabX.tab = await addTab(TEST_URI);

  if (startToolbox) {
    tabX.toolbox = await gDevTools.showToolboxForTab(tabX.tab, {
      toolId: "options",
    });
  }
}

async function checkCacheStateForAllTabs(states) {
  for (let i = 0; i < tabs.length; i++) {
    const tab = tabs[i];
    await checkCacheEnabled(tab, states[i]);
  }
}

async function checkCacheEnabled(tabX, expected) {
  gBrowser.selectedTab = tabX.tab;

  await reloadTab(tabX);

  const oldGuid = await SpecialPowers.spawn(
    gBrowser.selectedBrowser,
    [],
    function () {
      const doc = content.document;
      const h1 = doc.querySelector("h1");
      return h1.textContent;
    }
  );

  await reloadTab(tabX);

  const guid = await SpecialPowers.spawn(
    gBrowser.selectedBrowser,
    [],
    function () {
      const doc = content.document;
      const h1 = doc.querySelector("h1");
      return h1.textContent;
    }
  );

  if (expected) {
    is(guid, oldGuid, tabX.title + " cache is enabled");
  } else {
    isnot(guid, oldGuid, tabX.title + " cache is not enabled");
  }
}

async function setDisableCacheCheckboxChecked(tabX, state) {
  gBrowser.selectedTab = tabX.tab;

  const panel = tabX.toolbox.getCurrentPanel();
  const cbx = panel.panelDoc.getElementById("devtools-disable-cache");

  if (cbx.checked !== state) {
    info("Setting disable cache checkbox to " + state + " for " + tabX.title);
    const onReconfigured = tabX.toolbox.once("new-configuration-applied");
    cbx.click();

    // We have to wait for the reconfigure request to be finished before reloading
    // the page.
    await onReconfigured;
  }
}

function reloadTab(tabX) {
  const browser = gBrowser.selectedBrowser;

  const reloadTabPromise = BrowserTestUtils.browserLoaded(browser).then(
    function () {
      info("Reloaded tab " + tabX.title);
    }
  );

  info("Reloading tab " + tabX.title);
  SpecialPowers.spawn(browser, [], () => {
    content.location.reload(false);
  });

  return reloadTabPromise;
}

async function destroyTab(tabX) {
  const toolbox = gDevTools.getToolboxForTab(tabX.tab);

  let onceDestroyed;
  if (toolbox) {
    onceDestroyed = gDevTools.once("toolbox-destroyed");
  }

  info("Removing tab " + tabX.title);
  gBrowser.removeTab(tabX.tab);
  info("Removed tab " + tabX.title);

  info("Waiting for toolbox-destroyed");
  await onceDestroyed;
}

async function finishUp() {
  for (const tab of tabs) {
    await destroyTab(tab);
  }

  tabs = null;
}