summaryrefslogtreecommitdiffstats
path: root/toolkit/components/messaging-system/schemas/SpecialMessageActionSchemas/test/browser/head.js
blob: 53e7b21718f34fbccf6ce448161cee0b1085c785 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const { XPCOMUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/XPCOMUtils.sys.mjs"
);
const { sinon } = ChromeUtils.import("resource://testing-common/Sinon.jsm");
const { JsonSchema } = ChromeUtils.importESModule(
  "resource://gre/modules/JsonSchema.sys.mjs"
);

ChromeUtils.defineModuleGetter(
  this,
  "SpecialMessageActions",
  "resource://messaging-system/lib/SpecialMessageActions.jsm"
);

XPCOMUtils.defineLazyGetter(this, "fetchSMASchema", async () => {
  const response = await fetch(
    "resource://testing-common/SpecialMessageActionSchemas.json"
  );
  const schema = await response.json();
  if (!schema) {
    throw new Error("Failed to load SpecialMessageActionSchemas");
  }
  return schema;
});

const EXAMPLE_URL = "https://example.com/";

const SMATestUtils = {
  /**
   * Checks if an action is valid acording to existing schemas
   * @param {SpecialMessageAction} action
   */
  async validateAction(action) {
    const schema = await fetchSMASchema;
    const result = JsonSchema.validate(action, schema);
    if (result.errors.length) {
      throw new Error(
        `Action with type ${
          action.type
        } was not valid. Errors: ${JSON.stringify(result.errors, undefined, 2)}`
      );
    }
    is(
      result.errors.length,
      0,
      `Should be a valid action of type ${action.type}`
    );
  },

  /**
   * Executes a Special Message Action after validating it
   * @param {SpecialMessageAction} action
   * @param {Browser} browser
   */
  async executeAndValidateAction(action, browser = gBrowser) {
    await SMATestUtils.validateAction(action);
    await SpecialMessageActions.handleAction(action, browser);
  },
};