summaryrefslogtreecommitdiffstats
path: root/accessible/tests/browser/head.js
blob: 8e8d822052c98fcff0b59e031784ea1dde96dd80 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

'use strict';

/* exported initPromise, shutdownPromise,
            setE10sPrefs, unsetE10sPrefs, forceGC */

/**
 * Set e10s related preferences in the test environment.
 * @return {Promise} promise that resolves when preferences are set.
 */
function setE10sPrefs() {
  return new Promise(resolve =>
    SpecialPowers.pushPrefEnv({
      set: [
        ['browser.tabs.remote.autostart', true],
        ['browser.tabs.remote.force-enable', true],
        ['extensions.e10sBlocksEnabling', false]
      ]
    }, resolve));
}

/**
 * Unset e10s related preferences in the test environment.
 * @return {Promise} promise that resolves when preferences are unset.
 */
function unsetE10sPrefs() {
  return new Promise(resolve => {
    SpecialPowers.popPrefEnv(resolve);
  });
}

// Load the shared-head file first.
Services.scriptloader.loadSubScript(
  'chrome://mochitests/content/browser/accessible/tests/browser/shared-head.js',
  this);

/**
 * Returns a promise that resolves when 'a11y-init-or-shutdown' event is fired.
 * @return {Promise} event promise evaluating to event's data
 */
function a11yInitOrShutdownPromise() {
  return new Promise(resolve => {
    let observe = (subject, topic, data) => {
      Services.obs.removeObserver(observe, 'a11y-init-or-shutdown');
      resolve(data);
    };
    Services.obs.addObserver(observe, 'a11y-init-or-shutdown', false);
  });
}

/**
 * Returns a promise that resolves when 'a11y-init-or-shutdown' event is fired
 * in content.
 * @param  {Object}   browser  current "tabbrowser" element
 * @return {Promise}  event    promise evaluating to event's data
 */
function contentA11yInitOrShutdownPromise(browser) {
  return ContentTask.spawn(browser, {}, a11yInitOrShutdownPromise);
}

/**
 * A helper function that maps 'a11y-init-or-shutdown' event to a promise that
 * resovles or rejects depending on whether accessibility service is expected to
 * be initialized or shut down.
 */
function promiseOK(promise, expected) {
  return promise.then(flag =>
    flag === expected ? Promise.resolve() : Promise.reject());
}

/**
 * Checks and returns a promise that resolves when accessibility service is
 * initialized with the correct flag.
 * @param  {?Object} contentBrowser optinal remove browser object that indicates
 *                                  that accessibility service is expected to be
 *                                  initialized in content process.
 * @return {Promise}                promise that resolves when the accessibility
 *                                  service initialized correctly.
 */
function initPromise(contentBrowser) {
  let a11yInitPromise = contentBrowser ?
    contentA11yInitOrShutdownPromise(contentBrowser) :
    a11yInitOrShutdownPromise();
  return promiseOK(a11yInitPromise, '1').then(
    () => ok(true, 'Service initialized correctly'),
    () => ok(false, 'Service shutdown incorrectly'));
}

/**
 * Checks and returns a promise that resolves when accessibility service is
 * shut down with the correct flag.
 * @param  {?Object} contentBrowser optinal remove browser object that indicates
 *                                  that accessibility service is expected to be
 *                                  shut down in content process.
 * @return {Promise}                promise that resolves when the accessibility
 *                                  service shuts down correctly.
 */
function shutdownPromise(contentBrowser) {
  let a11yShutdownPromise = contentBrowser ?
    contentA11yInitOrShutdownPromise(contentBrowser) :
    a11yInitOrShutdownPromise();
  return promiseOK(a11yShutdownPromise, '0').then(
    () => ok(true, 'Service shutdown correctly'),
    () => ok(false, 'Service initialized incorrectly'));
}

/**
 * Force garbage collection.
 */
function forceGC() {
  Cu.forceCC();
  Cu.forceGC();
}