summaryrefslogtreecommitdiffstats
path: root/browser/components/search/test/browser_contextmenu.js
blob: c485242b4363564a05a8053135ac70938eb5399b (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
/* Any copyright is dedicated to the Public Domain.
 *  * http://creativecommons.org/publicdomain/zero/1.0/ */
/*
 * Test searching for the selected text using the context menu
 */

add_task(function* () {
  const ss = Services.search;
  const ENGINE_NAME = "Foo";
  var contextMenu;

  // We want select events to be fired.
  yield new Promise(resolve => SpecialPowers.pushPrefEnv({"set": [["dom.select_events.enabled", true]]}, resolve));

  let envService = Cc["@mozilla.org/process/environment;1"].getService(Ci.nsIEnvironment);
  let originalValue = envService.get("XPCSHELL_TEST_PROFILE_DIR");
  envService.set("XPCSHELL_TEST_PROFILE_DIR", "1");

  let url = "chrome://mochitests/content/browser/browser/components/search/test/";
  let resProt = Services.io.getProtocolHandler("resource")
                        .QueryInterface(Ci.nsIResProtocolHandler);
  let originalSubstitution = resProt.getSubstitution("search-plugins");
  resProt.setSubstitution("search-plugins",
                          Services.io.newURI(url, null, null));

  let searchDonePromise;
  yield new Promise(resolve => {
    function observer(aSub, aTopic, aData) {
      switch (aData) {
        case "engine-added":
          var engine = ss.getEngineByName(ENGINE_NAME);
          ok(engine, "Engine was added.");
          ss.currentEngine = engine;
          envService.set("XPCSHELL_TEST_PROFILE_DIR", originalValue);
          resProt.setSubstitution("search-plugins", originalSubstitution);
          break;
        case "engine-current":
          is(ss.currentEngine.name, ENGINE_NAME, "currentEngine set");
          resolve();
          break;
        case "engine-removed":
          Services.obs.removeObserver(observer, "browser-search-engine-modified");
          if (searchDonePromise) {
            searchDonePromise();
          }
          break;
      }
    }

    Services.obs.addObserver(observer, "browser-search-engine-modified", false);
    ss.addEngine("resource://search-plugins/testEngine_mozsearch.xml",
                 null, "data:image/x-icon,%00", false);
  });

  contextMenu = document.getElementById("contentAreaContextMenu");
  ok(contextMenu, "Got context menu XUL");

  let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, "data:text/plain;charset=utf8,test%20search");

  yield ContentTask.spawn(tab.linkedBrowser, "", function*() {
    return new Promise(resolve => {
      content.document.addEventListener("selectionchange", function selectionChanged() {
        content.document.removeEventListener("selectionchange", selectionChanged);
        resolve();
      });
      content.document.getSelection().selectAllChildren(content.document.body);
    });
  });

  var eventDetails = { type: "contextmenu", button: 2 };

  let popupPromise = BrowserTestUtils.waitForEvent(contextMenu, "popupshown");
  BrowserTestUtils.synthesizeMouseAtCenter("body", eventDetails, gBrowser.selectedBrowser);
  yield popupPromise;

  info("checkContextMenu");
  var searchItem = contextMenu.getElementsByAttribute("id", "context-searchselect")[0];
  ok(searchItem, "Got search context menu item");
  is(searchItem.label, 'Search ' + ENGINE_NAME + ' for \u201ctest search\u201d', "Check context menu label");
  is(searchItem.disabled, false, "Check that search context menu item is enabled");

  yield BrowserTestUtils.openNewForegroundTab(gBrowser, () => {
    searchItem.click();
  });

  is(gBrowser.currentURI.spec,
     "http://mochi.test:8888/browser/browser/components/search/test/?test=test+search&ie=utf-8&channel=contextsearch",
     "Checking context menu search URL");

  contextMenu.hidePopup();

  // Remove the tab opened by the search
  gBrowser.removeCurrentTab();

  yield new Promise(resolve => {
    searchDonePromise = resolve;
    ss.removeEngine(ss.currentEngine);
  });

  gBrowser.removeCurrentTab();
});