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

add_task(async function () {
  let extension = ExtensionTestUtils.loadExtension({
    files: {
      "_locales/en/messages.json": {
        with_translation: {
          message: "The description",
          description: "A description",
        },
      },
    },
    manifest: {
      name: "Commands Extension",
      default_locale: "en",
      commands: {
        "with-desciption": {
          suggested_key: {
            default: "Ctrl+Shift+Y",
          },
          description: "should have a description",
        },
        "without-description": {
          suggested_key: {
            default: "Ctrl+Shift+D",
          },
        },
        "with-platform-info": {
          suggested_key: {
            mac: "Ctrl+Shift+M",
            linux: "Ctrl+Shift+L",
            windows: "Ctrl+Shift+W",
            android: "Ctrl+Shift+A",
          },
        },
        "with-translation": {
          description: "__MSG_with_translation__",
        },
        "without-suggested-key": {
          description: "has no suggested_key",
        },
        "without-suggested-key-nor-description": {},
      },
    },

    background: function () {
      browser.test.onMessage.addListener((message, additionalScope) => {
        browser.commands.getAll(commands => {
          let errorMessage = "getAll should return an array of commands";
          browser.test.assertEq(commands.length, 6, errorMessage);

          let command = commands.find(c => c.name == "with-desciption");

          errorMessage =
            "The description should match what is provided in the manifest";
          browser.test.assertEq(
            "should have a description",
            command.description,
            errorMessage
          );

          errorMessage =
            "The shortcut should match the default shortcut provided in the manifest";
          browser.test.assertEq("Ctrl+Shift+Y", command.shortcut, errorMessage);

          command = commands.find(c => c.name == "without-description");

          errorMessage =
            "The description should be empty when it is not provided";
          browser.test.assertEq(null, command.description, errorMessage);

          errorMessage =
            "The shortcut should match the default shortcut provided in the manifest";
          browser.test.assertEq("Ctrl+Shift+D", command.shortcut, errorMessage);

          let platformKeys = {
            macosx: "M",
            linux: "L",
            win: "W",
            android: "A",
          };

          command = commands.find(c => c.name == "with-platform-info");
          let platformKey = platformKeys[additionalScope.platform];
          let shortcut = `Ctrl+Shift+${platformKey}`;
          errorMessage = `The shortcut should match the one provided in the manifest for OS='${additionalScope.platform}'`;
          browser.test.assertEq(shortcut, command.shortcut, errorMessage);

          command = commands.find(c => c.name == "with-translation");
          browser.test.assertEq(
            command.description,
            "The description",
            "The description can be localized"
          );

          command = commands.find(c => c.name == "without-suggested-key");

          browser.test.assertEq(
            "has no suggested_key",
            command.description,
            "The description should match what is provided in the manifest"
          );

          browser.test.assertEq(
            "",
            command.shortcut,
            "The shortcut should be empty if not provided"
          );

          command = commands.find(
            c => c.name == "without-suggested-key-nor-description"
          );

          browser.test.assertEq(
            null,
            command.description,
            "The description should be empty when it is not provided"
          );

          browser.test.assertEq(
            "",
            command.shortcut,
            "The shortcut should be empty if not provided"
          );

          browser.test.notifyPass("commands");
        });
      });
      browser.test.sendMessage("ready");
    },
  });

  await extension.startup();
  await extension.awaitMessage("ready");
  extension.sendMessage("additional-scope", {
    platform: AppConstants.platform,
  });
  await extension.awaitFinish("commands");
  await extension.unload();
});