head.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. 'use strict';
  5. /* exported initPromise, shutdownPromise,
  6. setE10sPrefs, unsetE10sPrefs, forceGC */
  7. /**
  8. * Set e10s related preferences in the test environment.
  9. * @return {Promise} promise that resolves when preferences are set.
  10. */
  11. function setE10sPrefs() {
  12. return new Promise(resolve =>
  13. SpecialPowers.pushPrefEnv({
  14. set: [
  15. ['browser.tabs.remote.autostart', true],
  16. ['browser.tabs.remote.force-enable', true],
  17. ['extensions.e10sBlocksEnabling', false]
  18. ]
  19. }, resolve));
  20. }
  21. /**
  22. * Unset e10s related preferences in the test environment.
  23. * @return {Promise} promise that resolves when preferences are unset.
  24. */
  25. function unsetE10sPrefs() {
  26. return new Promise(resolve => {
  27. SpecialPowers.popPrefEnv(resolve);
  28. });
  29. }
  30. // Load the shared-head file first.
  31. Services.scriptloader.loadSubScript(
  32. 'chrome://mochitests/content/browser/accessible/tests/browser/shared-head.js',
  33. this);
  34. /**
  35. * Returns a promise that resolves when 'a11y-init-or-shutdown' event is fired.
  36. * @return {Promise} event promise evaluating to event's data
  37. */
  38. function a11yInitOrShutdownPromise() {
  39. return new Promise(resolve => {
  40. let observe = (subject, topic, data) => {
  41. Services.obs.removeObserver(observe, 'a11y-init-or-shutdown');
  42. resolve(data);
  43. };
  44. Services.obs.addObserver(observe, 'a11y-init-or-shutdown', false);
  45. });
  46. }
  47. /**
  48. * Returns a promise that resolves when 'a11y-init-or-shutdown' event is fired
  49. * in content.
  50. * @param {Object} browser current "tabbrowser" element
  51. * @return {Promise} event promise evaluating to event's data
  52. */
  53. function contentA11yInitOrShutdownPromise(browser) {
  54. return ContentTask.spawn(browser, {}, a11yInitOrShutdownPromise);
  55. }
  56. /**
  57. * A helper function that maps 'a11y-init-or-shutdown' event to a promise that
  58. * resovles or rejects depending on whether accessibility service is expected to
  59. * be initialized or shut down.
  60. */
  61. function promiseOK(promise, expected) {
  62. return promise.then(flag =>
  63. flag === expected ? Promise.resolve() : Promise.reject());
  64. }
  65. /**
  66. * Checks and returns a promise that resolves when accessibility service is
  67. * initialized with the correct flag.
  68. * @param {?Object} contentBrowser optinal remove browser object that indicates
  69. * that accessibility service is expected to be
  70. * initialized in content process.
  71. * @return {Promise} promise that resolves when the accessibility
  72. * service initialized correctly.
  73. */
  74. function initPromise(contentBrowser) {
  75. let a11yInitPromise = contentBrowser ?
  76. contentA11yInitOrShutdownPromise(contentBrowser) :
  77. a11yInitOrShutdownPromise();
  78. return promiseOK(a11yInitPromise, '1').then(
  79. () => ok(true, 'Service initialized correctly'),
  80. () => ok(false, 'Service shutdown incorrectly'));
  81. }
  82. /**
  83. * Checks and returns a promise that resolves when accessibility service is
  84. * shut down with the correct flag.
  85. * @param {?Object} contentBrowser optinal remove browser object that indicates
  86. * that accessibility service is expected to be
  87. * shut down in content process.
  88. * @return {Promise} promise that resolves when the accessibility
  89. * service shuts down correctly.
  90. */
  91. function shutdownPromise(contentBrowser) {
  92. let a11yShutdownPromise = contentBrowser ?
  93. contentA11yInitOrShutdownPromise(contentBrowser) :
  94. a11yInitOrShutdownPromise();
  95. return promiseOK(a11yShutdownPromise, '0').then(
  96. () => ok(true, 'Service shutdown correctly'),
  97. () => ok(false, 'Service initialized incorrectly'));
  98. }
  99. /**
  100. * Force garbage collection.
  101. */
  102. function forceGC() {
  103. Cu.forceCC();
  104. Cu.forceGC();
  105. }