summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/extensions/test/browser/browser_ext_commands_execute_compose_action.js
blob: a84a2cac3c8a054b3e37384737c900736b57935a (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
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

let gAccount;

async function testExecuteComposeActionWithOptions(options = {}) {
  info(
    `--> Running test commands_execute_compose_action with the following options: ${JSON.stringify(
      options
    )}`
  );

  let extensionOptions = {};
  extensionOptions.manifest = {
    permissions: ["accountsRead"],
    commands: {
      _execute_compose_action: {
        suggested_key: {
          default: "Alt+Shift+J",
          mac: "Ctrl+Shift+J",
        },
      },
    },
    compose_action: {
      browser_style: true,
    },
  };

  if (options.withFormatToolbar) {
    extensionOptions.manifest.compose_action.default_area = "formattoolbar";
  }

  if (options.withPopup) {
    extensionOptions.manifest.compose_action.default_popup = "popup.html";

    extensionOptions.files = {
      "popup.html": `
        <!DOCTYPE html>
        <html>
          <head>
            <meta charset="utf-8">
            <script src="popup.js"></script>
          </head>
          <body>
            Popup
          </body>
      </html>
      `,
      "popup.js": function () {
        browser.test.log("sending from-compose-action-popup");
        browser.runtime.sendMessage("from-compose-action-popup");
      },
    };
  }

  extensionOptions.background = async () => {
    let accounts = await browser.accounts.list();
    browser.test.assertEq(1, accounts.length, "number of accounts");

    browser.test.onMessage.addListener((message, withPopup) => {
      browser.commands.onCommand.addListener(commandName => {
        browser.test.fail(
          "The onCommand listener should never fire for a valid _execute_* command."
        );
      });

      browser.composeAction.onClicked.addListener(() => {
        if (withPopup) {
          browser.test.fail(
            "The onClick listener should never fire if the composeAction has a popup."
          );
          browser.test.notifyFail("execute-compose-action-on-clicked-fired");
        } else {
          browser.test.notifyPass("execute-compose-action-on-clicked-fired");
        }
      });

      browser.runtime.onMessage.addListener(msg => {
        if (msg == "from-compose-action-popup") {
          browser.test.notifyPass("execute-compose-action-popup-opened");
        }
      });

      browser.test.log("Sending send-keys");
      browser.test.sendMessage("send-keys");
    });
  };

  let extension = ExtensionTestUtils.loadExtension(extensionOptions);
  await extension.startup();

  let composeWindow = await openComposeWindow(gAccount);
  await focusWindow(composeWindow);

  // trigger setup of listeners in background and the send-keys msg
  extension.sendMessage("withPopup", options.withPopup);

  await extension.awaitMessage("send-keys");
  info("Simulating ALT+SHIFT+J");
  let modifiers =
    AppConstants.platform == "macosx"
      ? { metaKey: true, shiftKey: true }
      : { altKey: true, shiftKey: true };
  EventUtils.synthesizeKey("j", modifiers, composeWindow);

  if (options.withPopup) {
    await extension.awaitFinish("execute-compose-action-popup-opened");

    if (!getBrowserActionPopup(extension, composeWindow)) {
      await awaitExtensionPanel(extension, composeWindow);
    }
    await closeBrowserAction(extension, composeWindow);
  } else {
    await extension.awaitFinish("execute-compose-action-on-clicked-fired");
  }
  composeWindow.close();
  await extension.unload();
}

add_setup(async () => {
  gAccount = createAccount();
  addIdentity(gAccount);
});

let popupJobs = [true, false];
let formatToolbarJobs = [true, false];

for (let popupJob of popupJobs) {
  for (let formatToolbarJob of formatToolbarJobs) {
    add_task(async () => {
      await testExecuteComposeActionWithOptions({
        withPopup: popupJob,
        withFormatToolbar: formatToolbarJob,
      });
    });
  }
}