summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_ext_asyncAPICall_isHandlingUserInput.js
blob: 73593b7e81906dcee9461f3a1135608e7baa475e (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const { ExtensionAPI } = ExtensionCommon;

const API_CLASS = class extends ExtensionAPI {
  getAPI(context) {
    return {
      testMockAPI: {
        async anAsyncAPIMethod(...args) {
          const callContextDataBeforeAwait = context.callContextData;
          await Promise.resolve();
          const callContextDataAfterAwait = context.callContextData;
          return {
            args,
            callContextDataBeforeAwait,
            callContextDataAfterAwait,
          };
        },
      },
    };
  }
};

const API_SCRIPT = `
  this.testMockAPI = ${API_CLASS.toString()};
`;

const API_SCHEMA = [
  {
    namespace: "testMockAPI",
    functions: [
      {
        name: "anAsyncAPIMethod",
        type: "function",
        async: true,
        parameters: [
          {
            name: "param1",
            type: "object",
            additionalProperties: {
              type: "string",
            },
          },
          {
            name: "param2",
            type: "string",
          },
        ],
      },
    ],
  },
];

const MODULE_INFO = {
  testMockAPI: {
    schema: `data:,${JSON.stringify(API_SCHEMA)}`,
    scopes: ["addon_parent"],
    paths: [["testMockAPI"]],
    url: URL.createObjectURL(new Blob([API_SCRIPT])),
  },
};

add_setup(async function () {
  // The blob:-URL registered above in MODULE_INFO gets loaded at
  // https://searchfox.org/mozilla-central/rev/0fec57c05d3996cc00c55a66f20dd5793a9bfb5d/toolkit/components/extensions/ExtensionCommon.jsm#1649
  Services.prefs.setBoolPref(
    "security.allow_parent_unrestricted_js_loads",
    true
  );
  registerCleanupFunction(() => {
    Services.prefs.clearUserPref("security.allow_parent_unrestricted_js_loads");
  });

  ExtensionParent.apiManager.registerModules(MODULE_INFO);
});

add_task(
  async function test_propagated_isHandlingUserInput_on_async_api_methods_calls() {
    const extension = ExtensionTestUtils.loadExtension({
      manifest: {
        browser_specific_settings: { gecko: { id: "@test-ext" } },
      },
      background() {
        browser.test.onMessage.addListener(async (msg, args) => {
          if (msg !== "async-method-call") {
            browser.test.fail(`Unexpected test message: ${msg}`);
            return;
          }

          try {
            let result = await browser.testMockAPI.anAsyncAPIMethod(...args);
            browser.test.sendMessage("async-method-call:result", result);
          } catch (err) {
            browser.test.sendMessage("async-method-call:error", err.message);
          }
        });
      },
    });

    await extension.startup();

    const callArgs = [{ param1: "param1" }, "param2"];

    info("Test API method called without handling user input");

    extension.sendMessage("async-method-call", callArgs);
    const result = await extension.awaitMessage("async-method-call:result");
    Assert.deepEqual(
      result?.args,
      callArgs,
      "Got the expected parameters when called without handling user input"
    );
    Assert.deepEqual(
      result?.callContextDataBeforeAwait,
      { isHandlingUserInput: false },
      "Got the expected callContextData before awaiting on a promise"
    );
    Assert.deepEqual(
      result?.callContextDataAfterAwait,
      null,
      "context.callContextData should have been nullified after awaiting on a promise"
    );

    await withHandlingUserInput(extension, async () => {
      extension.sendMessage("async-method-call", callArgs);
      const result = await extension.awaitMessage("async-method-call:result");
      Assert.deepEqual(
        result?.args,
        callArgs,
        "Got the expected parameters when called while handling user input"
      );
      Assert.deepEqual(
        result?.callContextDataBeforeAwait,
        { isHandlingUserInput: true },
        "Got the expected callContextData before awaiting on a promise"
      );
      Assert.deepEqual(
        result?.callContextDataAfterAwait,
        null,
        "context.callContextData should have been nullified after awaiting on a promise"
      );
    });

    await extension.unload();
  }
);