File: head.js

package info (click to toggle)
firefox 143.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,617,328 kB
  • sloc: cpp: 7,478,492; javascript: 6,417,157; ansic: 3,720,058; python: 1,396,372; xml: 627,523; asm: 438,677; java: 186,156; sh: 63,477; makefile: 19,171; objc: 13,059; perl: 12,983; yacc: 4,583; cs: 3,846; pascal: 3,405; lex: 1,720; ruby: 1,003; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 53; csh: 10
file content (122 lines) | stat: -rw-r--r-- 3,659 bytes parent folder | download | duplicates (3)
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
Services.scriptloader.loadSubScript(
  "chrome://mochitests/content/browser/toolkit/components/ml/tests/browser/head.js",
  this
);

async function setupRemoteClient() {
  const { removeMocks, remoteClients } = await createAndMockMLRemoteSettings({
    autoDownloadFromRemoteSettings: false,
  });

  return {
    remoteClients,
    async cleanup() {
      await removeMocks();
      await waitForCondition(
        () => EngineProcess.areAllEnginesTerminated(),
        "Waiting for all of the engines to be terminated.",
        100,
        200
      );
    },
  };
}

async function runInferenceProcess(remoteClients) {
  info("Building the egnine process");

  const { createEngine } = ChromeUtils.importESModule(
    "chrome://global/content/ml/EngineProcess.sys.mjs"
  );

  const engine = await createEngine({ taskName: "moz-echo" });
  const inferencePromise = engine.run({ data: "This gets echoed." });
  await remoteClients["ml-onnx-runtime"].resolvePendingDownloads(1);
  Assert.equal(
    (await inferencePromise).output.echo,
    "This gets echoed.",
    "The text get echoed exercising the whole flow."
  );
}

/**
 * The mochitest runs in the parent process. This function opens up a new tab,
 * navigates to about:inference, and passes the test requirements into the content process.
 *
 * @param {object} options - The options object.
 * @param {boolean} options.disabled - Flag to disable the inference functionality.
 * @param {Function} options.runInPage - The function to run in the content process.
 * @param {Array} [options.prefs] - An array of additional preferences to set.
 * @param {boolean} options.runInference - If true, runs an inference task
 *
 * @returns {Promise<void>} A promise that resolves when the test is complete.
 */
async function openAboutInference({
  disabled,
  runInPage,
  prefs,
  runInference = false,
}) {
  await SpecialPowers.pushPrefEnv({
    set: [
      // Enabled by default.
      ["browser.ml.enable", !disabled],
      ["browser.ml.logLevel", "Debug"],
      ["dom.webgpu.enabled", !disabled],
      ["dom.webgpu.service-workers.enabled", !disabled],
      ...(prefs ?? []),
    ],
  });

  let cleanup;
  let remoteClients;
  // run inference
  if (runInference) {
    let set = await setupRemoteClient();
    cleanup = set.cleanup;
    remoteClients = set.remoteClients;

    await runInferenceProcess(remoteClients);
  }
  /**
   * Collect any relevant selectors for the page here.
   */
  const selectors = {
    pageHeader: '[data-l10n-id="about-inference-header"]',
    warning: "moz-message-bar#warning",
    processes: "div#procInfoTableContainer",
  };

  // Start the tab at a blank page.
  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    BLANK_PAGE,
    true // waitForLoad
  );

  // Now load the about:inference page, since the actor could be mocked.
  BrowserTestUtils.startLoadingURIString(tab.linkedBrowser, "about:inference");
  await BrowserTestUtils.browserLoaded(tab.linkedBrowser);

  await ContentTask.spawn(tab.linkedBrowser, { selectors }, runInPage);

  if (runInference) {
    await EngineProcess.destroyMLEngine();
    await cleanup();
  }
  await loadBlankPage();
  BrowserTestUtils.removeTab(tab);
  await SpecialPowers.popPrefEnv();
}

/**
 * Loads the blank-page URL.
 *
 * This is useful for resetting the state during cleanup, and also
 * before starting a test, to further help ensure that there is no
 * unintentional state left over from test case.
 */
async function loadBlankPage() {
  BrowserTestUtils.startLoadingURIString(gBrowser.selectedBrowser, BLANK_PAGE);
  await BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
}