summaryrefslogtreecommitdiffstats
path: root/browser/components/search/test/browser/browser_contentContextMenu.js
blob: 684428821e5edce95ba8ea9499ac3907799938d8 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* Make sure context menu includes option to search hyperlink text on search
 * engine.
 */

add_task(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["browser.search.separatePrivateDefault", true],
      ["browser.search.separatePrivateDefault.ui.enabled", true],
    ],
  });

  const url =
    "http://mochi.test:8888/browser/browser/components/search/test/browser/browser_contentContextMenu.xhtml";
  await BrowserTestUtils.openNewForegroundTab(gBrowser, url);

  const ellipsis = "\u2026";

  let contentAreaContextMenu = document.getElementById(
    "contentAreaContextMenu"
  );

  const originalPrivateDefault = await Services.search.getDefaultPrivate();
  let otherPrivateDefault;
  for (let engine of await Services.search.getVisibleEngines()) {
    if (engine.name != originalPrivateDefault.name) {
      otherPrivateDefault = engine;
      break;
    }
  }

  // Tests if the "Search <engine> for '<some terms>'" context menu item is
  // shown for the given query string of an element. Tests to make sure label
  // includes the proper search terms.
  //
  // Each test:
  //
  //   id: The id of the element to test.
  //   isSelected: Flag to enable selecting (text highlight) the contents of the
  //               element.
  //   shouldBeShown: The display state of the menu item.
  //   expectedLabelContents: The menu item label should contain a portion of
  //                          this string. Will only be tested if shouldBeShown
  //                          is true.
  //   shouldPrivateBeShown: The display state of the Private Window menu item.
  //   expectedPrivateLabelContents: The menu item label for the Private Window
  //                                 should contain a portion of this string.
  //                                 Will only be tested if shouldPrivateBeShown
  //                                 is true.
  let tests = [
    {
      id: "link",
      isSelected: true,
      shouldBeShown: true,
      expectedLabelContents: "I'm a link!",
      shouldPrivateBeShown: true,
      expectedPrivateLabelContents: "Search in",
    },
    {
      id: "link",
      isSelected: false,
      shouldBeShown: true,
      expectedLabelContents: "I'm a link!",
      shouldPrivateBeShown: true,
      expectedPrivateLabelContents: "Search in",
    },
    {
      id: "longLink",
      isSelected: true,
      shouldBeShown: true,
      expectedLabelContents: "I'm a really lo" + ellipsis,
      shouldPrivateBeShown: true,
      expectedPrivateLabelContents: "Search in",
    },
    {
      id: "longLink",
      isSelected: false,
      shouldBeShown: true,
      expectedLabelContents: "I'm a really lo" + ellipsis,
      shouldPrivateBeShown: true,
      expectedPrivateLabelContents: "Search in",
    },
    {
      id: "plainText",
      isSelected: true,
      shouldBeShown: true,
      expectedLabelContents: "Right clicking " + ellipsis,
      shouldPrivateBeShown: true,
      expectedPrivateLabelContents: "Search in",
    },
    {
      id: "plainText",
      isSelected: false,
      shouldBeShown: false,
      shouldPrivateBeShown: false,
    },
    {
      id: "mixedContent",
      isSelected: true,
      shouldBeShown: true,
      expectedLabelContents: "I'm some text, " + ellipsis,
      shouldPrivateBeShown: true,
      expectedPrivateLabelContents: "Search in",
    },
    {
      id: "mixedContent",
      isSelected: false,
      shouldBeShown: false,
      shouldPrivateBeShown: false,
    },
    {
      id: "partialLink",
      isSelected: true,
      shouldBeShown: true,
      expectedLabelContents: "link selection",
      shouldPrivateBeShown: true,
      expectedPrivateLabelContents: "Search in",
    },
    {
      id: "partialLink",
      isSelected: false,
      shouldBeShown: true,
      expectedLabelContents: "A partial link " + ellipsis,
      shouldPrivateBeShown: true,
      expectedPrivateLabelContents: "Search with " + otherPrivateDefault.name,
      changePrivateDefaultEngine: true,
    },
    {
      id: "surrogatePair",
      isSelected: true,
      shouldBeShown: true,
      expectedLabelContents: "This character\uD83D\uDD25" + ellipsis,
      shouldPrivateBeShown: true,
      expectedPrivateLabelContents: "Search with " + otherPrivateDefault.name,
      changePrivateDefaultEngine: true,
    },
  ];

  for (let test of tests) {
    if (test.changePrivateDefaultEngine) {
      await Services.search.setDefaultPrivate(
        otherPrivateDefault,
        Ci.nsISearchService.CHANGE_REASON_UNKNOWN
      );
    }

    await SpecialPowers.spawn(
      gBrowser.selectedBrowser,
      [{ selectElement: test.isSelected ? test.id : null }],
      async function (arg) {
        let selection = content.getSelection();
        selection.removeAllRanges();

        if (arg.selectElement) {
          selection.selectAllChildren(
            content.document.getElementById(arg.selectElement)
          );
        }
      }
    );

    let popupShownPromise = BrowserTestUtils.waitForEvent(
      contentAreaContextMenu,
      "popupshown"
    );
    await BrowserTestUtils.synthesizeMouseAtCenter(
      "#" + test.id,
      { type: "contextmenu", button: 2 },
      gBrowser.selectedBrowser
    );
    await popupShownPromise;

    let menuItem = document.getElementById("context-searchselect");
    is(
      menuItem.hidden,
      !test.shouldBeShown,
      "search context menu item is shown for  '#" +
        test.id +
        "' and selected is '" +
        test.isSelected +
        "'"
    );

    if (test.shouldBeShown) {
      ok(
        menuItem.label.includes(test.expectedLabelContents),
        "Menu item text '" +
          menuItem.label +
          "' contains the correct search terms '" +
          test.expectedLabelContents +
          "'"
      );
    }

    menuItem = document.getElementById("context-searchselect-private");
    is(
      menuItem.hidden,
      !test.shouldPrivateBeShown,
      "private search context menu item is shown for  '#" + test.id + "' "
    );

    if (test.shouldPrivateBeShown) {
      ok(
        menuItem.label.includes(test.expectedPrivateLabelContents),
        "Menu item text '" +
          menuItem.label +
          "' contains the correct search terms '" +
          test.expectedPrivateLabelContents +
          "'"
      );
    }

    let popupHiddenPromise = BrowserTestUtils.waitForEvent(
      contentAreaContextMenu,
      "popuphidden"
    );
    contentAreaContextMenu.hidePopup();
    await popupHiddenPromise;

    if (test.changePrivateDefaultEngine) {
      await Services.search.setDefaultPrivate(
        originalPrivateDefault,
        Ci.nsISearchService.CHANGE_REASON_UNKNOWN
      );
    }
  }

  // Cleanup.
  gBrowser.removeCurrentTab();
});