summaryrefslogtreecommitdiffstats
path: root/layout/tools/reftest/api.js
blob: 285bba3e558368ad773746bdecb16627ce9b9304 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* 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/. */

// These globals are available to extensions using privileged APIs.
/* globals XPCOMUtils, ExtensionAPI */

const Cm = Components.manager;

var OnRefTestLoad, OnRefTestUnload;

XPCOMUtils.defineLazyServiceGetter(
  this,
  "resProto",
  "@mozilla.org/network/protocol;1?name=resource",
  "nsISubstitutingProtocolHandler"
);

XPCOMUtils.defineLazyServiceGetter(
  this,
  "aomStartup",
  "@mozilla.org/addons/addon-manager-startup;1",
  "amIAddonManagerStartup"
);

function processTerminated() {
  return new Promise(resolve => {
    Services.obs.addObserver(function observe(subject, topic) {
      if (topic == "ipc:content-shutdown") {
        Services.obs.removeObserver(observe, topic);
        resolve();
      }
    }, "ipc:content-shutdown");
  });
}

function startAndroid(win) {
  // Add setTimeout here because windows.innerWidth/Height are not set yet.
  win.setTimeout(function () {
    OnRefTestLoad(win);
  }, 0);
}

function GetMainWindow() {
  let win = Services.wm.getMostRecentWindow("navigator:browser");
  if (!win) {
    // There is no navigator:browser in the geckoview TestRunnerActivity;
    // try navigator.geckoview instead.
    win = Services.wm.getMostRecentWindow("navigator:geckoview");
  }
  return win;
}

this.reftest = class extends ExtensionAPI {
  onStartup() {
    let uri = Services.io.newURI(
      "chrome/reftest/res/",
      null,
      this.extension.rootURI
    );
    resProto.setSubstitutionWithFlags(
      "reftest",
      uri,
      resProto.ALLOW_CONTENT_ACCESS
    );

    const manifestURI = Services.io.newURI(
      "manifest.json",
      null,
      this.extension.rootURI
    );

    let manifestDirectives = [
      [
        "content",
        "reftest",
        "chrome/reftest/content/",
        "contentaccessible=yes",
      ],
    ];
    if (Services.appinfo.OS == "Android") {
      manifestDirectives.push([
        "override",
        "chrome://global/skin/global.css",
        "chrome://reftest/content/fake-global.css",
      ]);
    }
    this.chromeHandle = aomStartup.registerChrome(
      manifestURI,
      manifestDirectives
    );

    // Starting tests is handled quite differently on android and desktop.
    // On Android, OnRefTestLoad() takes over the main browser window so
    // we just need to call it as soon as the browser window is available.
    // On desktop, a separate window (dummy) is created and explicitly given
    // focus (see bug 859339 for details), then tests are launched in a new
    // top-level window.
    let win = GetMainWindow();
    if (Services.appinfo.OS == "Android") {
      ({ OnRefTestLoad, OnRefTestUnload } = ChromeUtils.importESModule(
        "resource://reftest/reftest.sys.mjs"
      ));
      if (win) {
        startAndroid(win);
      } else {
        // The window type parameter is only available once the window's document
        // element has been created. The main window has already been created
        // however and it is in an in-between state which means that you can't
        // find it by its type nor will domwindowcreated be fired.
        // So we listen to either initial-document-element-inserted which
        // indicates when it's okay to search for the main window by type again.
        Services.obs.addObserver(function observer(aSubject, aTopic) {
          Services.obs.removeObserver(observer, aTopic);
          startAndroid(GetMainWindow());
        }, "initial-document-element-inserted");
      }
      return;
    }

    Services.io.manageOfflineStatus = false;
    Services.io.offline = false;

    let dummy = Services.ww.openWindow(
      null,
      "about:blank",
      "dummy",
      "chrome,dialog=no,left=800,height=200,width=200,all",
      null
    );
    dummy.onload = async function () {
      // Close pre-existing window
      win.close();

      const { PerTestCoverageUtils } = ChromeUtils.importESModule(
        "resource://reftest/PerTestCoverageUtils.sys.mjs"
      );
      if (PerTestCoverageUtils.enabled) {
        // In PerTestCoverage mode, wait for the process belonging to the window we just closed
        // to be terminated, to avoid its shutdown interfering when we reset the counters.
        await processTerminated();
      }

      dummy.focus();
      Services.ww.openWindow(
        null,
        "chrome://reftest/content/reftest.xhtml",
        "_blank",
        "chrome,dialog=no,all",
        {}
      );
    };
  }

  onShutdown() {
    resProto.setSubstitution("reftest", null);

    this.chromeHandle.destruct();
    this.chromeHandle = null;

    if (Services.appinfo.OS == "Android") {
      OnRefTestUnload();
    }
  }
};