browser_toolbox_races.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. // Test toggling the toolbox quickly and see if there is any race breaking it.
  6. const URL = "data:text/html;charset=utf-8,Toggling devtools quickly";
  7. add_task(function* () {
  8. // Make sure this test starts with the selectedTool pref cleared. Previous
  9. // tests select various tools, and that sets this pref.
  10. Services.prefs.clearUserPref("devtools.toolbox.selectedTool");
  11. let tab = yield addTab(URL);
  12. let created = 0, ready = 0, destroy = 0, destroyed = 0;
  13. let onCreated = () => {
  14. created++;
  15. };
  16. let onReady = () => {
  17. ready++;
  18. };
  19. let onDestroy = () => {
  20. destroy++;
  21. };
  22. let onDestroyed = () => {
  23. destroyed++;
  24. };
  25. gDevTools.on("toolbox-created", onCreated);
  26. gDevTools.on("toolbox-ready", onReady);
  27. gDevTools.on("toolbox-destroy", onDestroy);
  28. gDevTools.on("toolbox-destroyed", onDestroyed);
  29. // The current implementation won't toggle the toolbox many times,
  30. // instead it will ignore toggles that happens while the toolbox is still
  31. // creating or still destroying.
  32. // Toggle the toolbox at least 3 times.
  33. info("Trying to toggle the toolbox 3 times");
  34. while (created < 3) {
  35. // Sent multiple event to try to race the code during toolbox creation and destruction
  36. toggle();
  37. toggle();
  38. toggle();
  39. // Release the event loop to let a chance to actually create or destroy the toolbox!
  40. yield wait(50);
  41. }
  42. info("Toggled the toolbox 3 times");
  43. // Now wait for the 3rd toolbox to be fully ready before closing it.
  44. // We close the last toolbox manually, out of the first while() loop to
  45. // avoid races and be sure we end up we no toolbox and waited for all the
  46. // requests to be done.
  47. while (ready != 3) {
  48. yield wait(100);
  49. }
  50. toggle();
  51. while (destroyed != 3) {
  52. yield wait(100);
  53. }
  54. is(created, 3, "right number of created events");
  55. is(ready, 3, "right number of ready events");
  56. is(destroy, 3, "right number of destroy events");
  57. is(destroyed, 3, "right number of destroyed events");
  58. gDevTools.off("toolbox-created", onCreated);
  59. gDevTools.off("toolbox-ready", onReady);
  60. gDevTools.off("toolbox-destroy", onDestroy);
  61. gDevTools.off("toolbox-destroyed", onDestroyed);
  62. gBrowser.removeCurrentTab();
  63. });
  64. function toggle() {
  65. EventUtils.synthesizeKey("VK_F12", {});
  66. }