helper_disable_cache.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
  2. /* Any copyright is dedicated to the Public Domain.
  3. * http://creativecommons.org/publicdomain/zero/1.0/ */
  4. "use strict";
  5. // Common code shared by browser_toolbox_options_disable_cache-*.js
  6. const TEST_URI = URL_ROOT + "browser_toolbox_options_disable_cache.sjs";
  7. var tabs = [
  8. {
  9. title: "Tab 0",
  10. desc: "Toggles cache on.",
  11. startToolbox: true
  12. },
  13. {
  14. title: "Tab 1",
  15. desc: "Toolbox open before Tab 1 toggles cache.",
  16. startToolbox: true
  17. },
  18. {
  19. title: "Tab 2",
  20. desc: "Opens toolbox after Tab 1 has toggled cache. Also closes and opens.",
  21. startToolbox: false
  22. },
  23. {
  24. title: "Tab 3",
  25. desc: "No toolbox",
  26. startToolbox: false
  27. }];
  28. function* initTab(tabX, startToolbox) {
  29. tabX.tab = yield addTab(TEST_URI);
  30. tabX.target = TargetFactory.forTab(tabX.tab);
  31. if (startToolbox) {
  32. tabX.toolbox = yield gDevTools.showToolbox(tabX.target, "options");
  33. }
  34. }
  35. function* checkCacheStateForAllTabs(states) {
  36. for (let i = 0; i < tabs.length; i++) {
  37. let tab = tabs[i];
  38. yield checkCacheEnabled(tab, states[i]);
  39. }
  40. }
  41. function* checkCacheEnabled(tabX, expected) {
  42. gBrowser.selectedTab = tabX.tab;
  43. yield reloadTab(tabX);
  44. let oldGuid = yield ContentTask.spawn(gBrowser.selectedBrowser, {}, function () {
  45. let doc = content.document;
  46. let h1 = doc.querySelector("h1");
  47. return h1.textContent;
  48. });
  49. yield reloadTab(tabX);
  50. let guid = yield ContentTask.spawn(gBrowser.selectedBrowser, {}, function () {
  51. let doc = content.document;
  52. let h1 = doc.querySelector("h1");
  53. return h1.textContent;
  54. });
  55. if (expected) {
  56. is(guid, oldGuid, tabX.title + " cache is enabled");
  57. } else {
  58. isnot(guid, oldGuid, tabX.title + " cache is not enabled");
  59. }
  60. }
  61. function* setDisableCacheCheckboxChecked(tabX, state) {
  62. gBrowser.selectedTab = tabX.tab;
  63. let panel = tabX.toolbox.getCurrentPanel();
  64. let cbx = panel.panelDoc.getElementById("devtools-disable-cache");
  65. if (cbx.checked !== state) {
  66. info("Setting disable cache checkbox to " + state + " for " + tabX.title);
  67. cbx.click();
  68. // We need to wait for all checkboxes to be updated and the docshells to
  69. // apply the new cache settings.
  70. yield waitForTick();
  71. }
  72. }
  73. function reloadTab(tabX) {
  74. let def = defer();
  75. let browser = gBrowser.selectedBrowser;
  76. BrowserTestUtils.browserLoaded(browser).then(function () {
  77. info("Reloaded tab " + tabX.title);
  78. def.resolve();
  79. });
  80. info("Reloading tab " + tabX.title);
  81. let mm = getFrameScript();
  82. mm.sendAsyncMessage("devtools:test:reload");
  83. return def.promise;
  84. }
  85. function* destroyTab(tabX) {
  86. let toolbox = gDevTools.getToolbox(tabX.target);
  87. let onceDestroyed = promise.resolve();
  88. if (toolbox) {
  89. onceDestroyed = gDevTools.once("toolbox-destroyed");
  90. }
  91. info("Removing tab " + tabX.title);
  92. gBrowser.removeTab(tabX.tab);
  93. info("Removed tab " + tabX.title);
  94. info("Waiting for toolbox-destroyed");
  95. yield onceDestroyed;
  96. }
  97. function* finishUp() {
  98. for (let tab of tabs) {
  99. yield destroyTab(tab);
  100. }
  101. tabs = null;
  102. }