browser_options-view-01.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* Any copyright is dedicated to the Public Domain.
  2. http://creativecommons.org/publicdomain/zero/1.0/ */
  3. "use strict";
  4. // Tests that options-view OptionsView responds to events correctly.
  5. const {OptionsView} = require("devtools/client/shared/options-view");
  6. const BRANCH = "devtools.debugger.";
  7. const BLACK_BOX_PREF = "auto-black-box";
  8. const PRETTY_PRINT_PREF = "auto-pretty-print";
  9. const originalBlackBox = Services.prefs.getBoolPref(BRANCH + BLACK_BOX_PREF);
  10. const originalPrettyPrint = Services.prefs.getBoolPref(BRANCH + PRETTY_PRINT_PREF);
  11. add_task(function* () {
  12. info("Setting a couple of preferences");
  13. Services.prefs.setBoolPref(BRANCH + BLACK_BOX_PREF, false);
  14. Services.prefs.setBoolPref(BRANCH + PRETTY_PRINT_PREF, true);
  15. info("Opening a test tab and a toolbox host to create the options view in");
  16. yield addTab("about:blank");
  17. let [host, win] = yield createHost("bottom", OPTIONS_VIEW_URL);
  18. yield testOptionsView(win);
  19. info("Closing the host and current tab");
  20. host.destroy();
  21. gBrowser.removeCurrentTab();
  22. info("Resetting the preferences");
  23. Services.prefs.setBoolPref(BRANCH + BLACK_BOX_PREF, originalBlackBox);
  24. Services.prefs.setBoolPref(BRANCH + PRETTY_PRINT_PREF, originalPrettyPrint);
  25. });
  26. function* testOptionsView(win) {
  27. let events = [];
  28. let options = createOptionsView(win);
  29. yield options.initialize();
  30. let $ = win.document.querySelector.bind(win.document);
  31. options.on("pref-changed", (_, pref) => events.push(pref));
  32. let ppEl = $("menuitem[data-pref='auto-pretty-print']");
  33. let bbEl = $("menuitem[data-pref='auto-black-box']");
  34. // Test default config
  35. is(ppEl.getAttribute("checked"), "true", "`true` prefs are checked on start");
  36. is(bbEl.getAttribute("checked"), "", "`false` prefs are unchecked on start");
  37. // Test buttons update when preferences update outside of the menu
  38. Services.prefs.setBoolPref(BRANCH + PRETTY_PRINT_PREF, false);
  39. Services.prefs.setBoolPref(BRANCH + BLACK_BOX_PREF, true);
  40. is(options.getPref(PRETTY_PRINT_PREF), false, "getPref returns correct value");
  41. is(options.getPref(BLACK_BOX_PREF), true, "getPref returns correct value");
  42. is(ppEl.getAttribute("checked"), "", "menuitems update when preferences change");
  43. is(bbEl.getAttribute("checked"), "true", "menuitems update when preferences change");
  44. // Tests events are fired when preferences update outside of the menu
  45. is(events.length, 2, "two 'pref-changed' events fired");
  46. is(events[0], "auto-pretty-print",
  47. "correct pref passed in 'pref-changed' event (auto-pretty-print)");
  48. is(events[1], "auto-black-box",
  49. "correct pref passed in 'pref-changed' event (auto-black-box)");
  50. // Test buttons update when clicked and preferences are updated
  51. yield click(options, win, ppEl);
  52. is(ppEl.getAttribute("checked"), "true", "menuitems update when clicked");
  53. is(Services.prefs.getBoolPref(BRANCH + PRETTY_PRINT_PREF),
  54. true, "preference updated via click");
  55. yield click(options, win, bbEl);
  56. is(bbEl.getAttribute("checked"), "", "menuitems update when clicked");
  57. is(Services.prefs.getBoolPref(BRANCH + BLACK_BOX_PREF),
  58. false, "preference updated via click");
  59. // Tests events are fired when preferences updated via click
  60. is(events.length, 4, "two 'pref-changed' events fired");
  61. is(events[2], "auto-pretty-print",
  62. "correct pref passed in 'pref-changed' event (auto-pretty-print)");
  63. is(events[3], "auto-black-box",
  64. "correct pref passed in 'pref-changed' event (auto-black-box)");
  65. yield options.destroy();
  66. }
  67. function createOptionsView(win) {
  68. return new OptionsView({
  69. branchName: BRANCH,
  70. menupopup: win.document.querySelector("#options-menupopup")
  71. });
  72. }
  73. function* click(view, win, menuitem) {
  74. let opened = view.once("options-shown");
  75. let closed = view.once("options-hidden");
  76. let button = win.document.querySelector("#options-button");
  77. EventUtils.synthesizeMouseAtCenter(button, {}, win);
  78. yield opened;
  79. is(button.getAttribute("open"), "true", "button has `open` attribute");
  80. EventUtils.synthesizeMouseAtCenter(menuitem, {}, win);
  81. yield closed;
  82. ok(!button.hasAttribute("open"), "button does not have `open` attribute");
  83. }