summaryrefslogtreecommitdiffstats
path: root/services/sync/tps/extensions/tps/api.js
blob: 686abdc6fdf864901d23e9d56df64a64fe4347fa (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
/* 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/. */

const { FileUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/FileUtils.sys.mjs"
);

/* globals ExtensionAPI, Services, XPCOMUtils */

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

async function tpsStartup() {
  try {
    var { TPS } = ChromeUtils.import("resource://tps/tps.jsm");
    let { goQuitApplication } = ChromeUtils.importESModule(
      "resource://tps/quit.sys.mjs"
    );
    TPS.goQuitApplication = goQuitApplication;

    let testFile = Services.prefs.getStringPref("testing.tps.testFile", "");
    let testPhase = Services.prefs.getStringPref("testing.tps.testPhase", "");
    if (!testFile || !testPhase) {
      // Note: this quits.
      TPS.DumpError(
        "TPS no longer takes arguments from the command line. " +
          "instead you need to pass preferences  `testing.tps.{testFile,testPhase}` " +
          "and optionally `testing.tps.{logFile,ignoreUnusedEngines}`.\n"
      );
    }

    let logFile = Services.prefs.getStringPref("testing.tps.logFile", "");
    let ignoreUnusedEngines = Services.prefs.getBoolPref(
      "testing.tps.ignoreUnusedEngines",
      false
    );
    let options = { ignoreUnusedEngines };
    let testFileUri = Services.io.newFileURI(new FileUtils.File(testFile)).spec;

    try {
      await TPS.RunTestPhase(testFileUri, testPhase, logFile, options);
    } catch (err) {
      TPS.DumpError("TestPhase failed", err);
    }
  } catch (e) {
    if (typeof TPS != "undefined") {
      // Note: This calls quit() under the hood
      TPS.DumpError("Test initialization failed", e);
    }
    dump(`TPS test initialization failed: ${e} - ${e.stack}\n`);
    // Try and quit right away, no reason to wait around for python
    // to kill us if initialization failed.
    Services.startup.quit(Ci.nsIAppStartup.eForceQuit);
  }
}

this.tps = class extends ExtensionAPI {
  onStartup() {
    resProto.setSubstitution(
      "tps",
      Services.io.newURI("resource/", null, this.extension.rootURI)
    );
    /* Ignore the platform's online/offline status while running tests. */
    Services.io.manageOfflineStatus = false;
    Services.io.offline = false;
    tpsStartup();
  }

  onShutdown() {
    resProto.setSubstitution("tps", null);
  }
};