summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/head_schemas.js
blob: aeba2011fc68d362c37a782548830d6c58e12af7 (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
"use strict";

/* exported Schemas, LocalAPIImplementation, SchemaAPIInterface, getContextWrapper */

const { Schemas } = ChromeUtils.import("resource://gre/modules/Schemas.jsm");

const { ExtensionCommon } = ChromeUtils.import(
  "resource://gre/modules/ExtensionCommon.jsm"
);

let { LocalAPIImplementation, SchemaAPIInterface } = ExtensionCommon;

const contextCloneScope = this;

class TallyingAPIImplementation extends SchemaAPIInterface {
  constructor(context, namespace, name) {
    super();
    this.namespace = namespace;
    this.name = name;
    this.context = context;
  }

  callFunction(args) {
    this.context.tally("call", this.namespace, this.name, args);
    if (this.name === "sub_foo") {
      return 13;
    }
  }

  callFunctionNoReturn(args) {
    this.context.tally("call", this.namespace, this.name, args);
  }

  getProperty() {
    this.context.tally("get", this.namespace, this.name);
  }

  setProperty(value) {
    this.context.tally("set", this.namespace, this.name, value);
  }

  addListener(listener, args) {
    this.context.tally("addListener", this.namespace, this.name, [
      listener,
      args,
    ]);
  }

  removeListener(listener) {
    this.context.tally("removeListener", this.namespace, this.name, [listener]);
  }

  hasListener(listener) {
    this.context.tally("hasListener", this.namespace, this.name, [listener]);
  }
}

function getContextWrapper(manifestVersion = 2) {
  return {
    url: "moz-extension://b66e3509-cdb3-44f6-8eb8-c8b39b3a1d27/",

    cloneScope: contextCloneScope,

    manifestVersion,

    permissions: new Set(),
    tallied: null,
    talliedErrors: [],

    tally(kind, ns, name, args) {
      this.tallied = [kind, ns, name, args];
    },

    verify(...args) {
      Assert.equal(JSON.stringify(this.tallied), JSON.stringify(args));
      this.tallied = null;
    },

    checkErrors(errors) {
      let { talliedErrors } = this;
      Assert.equal(
        talliedErrors.length,
        errors.length,
        "Got expected number of errors"
      );
      for (let [i, error] of errors.entries()) {
        Assert.ok(
          i in talliedErrors && String(talliedErrors[i]).includes(error),
          `${JSON.stringify(error)} is a substring of error ${JSON.stringify(
            talliedErrors[i]
          )}`
        );
      }

      talliedErrors.length = 0;
    },

    checkLoadURL(url) {
      return !url.startsWith("chrome:");
    },

    preprocessors: {
      localize(value, context) {
        return value.replace(
          /__MSG_(.*?)__/g,
          (m0, m1) => `${m1.toUpperCase()}`
        );
      },
    },

    logError(message) {
      this.talliedErrors.push(message);
    },

    hasPermission(permission) {
      return this.permissions.has(permission);
    },

    shouldInject(ns, name, allowedContexts) {
      return name != "do-not-inject";
    },

    getImplementation(namespace, name) {
      return new TallyingAPIImplementation(this, namespace, name);
    },
  };
}