summaryrefslogtreecommitdiffstats
path: root/browser/components/textrecognition/tests/browser/head.js
blob: d3016375e2ea118bcfed27da03358eb7bcf255e9 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * @param {string} text
 */
function setClipboardText(text) {
  const ClipboardHelper = Cc[
    "@mozilla.org/widget/clipboardhelper;1"
  ].getService(Ci.nsIClipboardHelper);
  ClipboardHelper.copyString(text);
}

/**
 * @returns {string}
 */
function getTextFromClipboard() {
  const transferable = Cc["@mozilla.org/widget/transferable;1"].createInstance(
    Ci.nsITransferable
  );
  transferable.init(window.docShell.QueryInterface(Ci.nsILoadContext));
  transferable.addDataFlavor("text/plain");
  Services.clipboard.getData(transferable, Services.clipboard.kGlobalClipboard);

  const results = {};
  transferable.getTransferData("text/plain", results);
  return results.value.QueryInterface(Ci.nsISupportsString)?.data ?? "";
}

/**
 * Returns events specifically for text recognition.
 */
function getTelemetryScalars() {
  const snapshot = Services.telemetry.getSnapshotForKeyedScalars(
    "main",
    true /* clear events */
  );

  if (!snapshot.parent) {
    return {};
  }

  return snapshot.parent;
}

function clearTelemetry() {
  Services.telemetry.clearScalars();
  Services.telemetry
    .getHistogramById("TEXT_RECOGNITION_API_PERFORMANCE")
    .clear();
  Services.telemetry
    .getHistogramById("TEXT_RECOGNITION_INTERACTION_TIMING")
    .clear();
  Services.telemetry.getHistogramById("TEXT_RECOGNITION_TEXT_LENGTH").clear();
}