browser/components/search/test/browser_contextmenu.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/components/search/test/browser_contextmenu.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,99 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 + *  * http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +/*
     1.7 + * Test searching for the selected text using the context menu
     1.8 + */
     1.9 +
    1.10 +function test() {
    1.11 +  waitForExplicitFinish();
    1.12 +
    1.13 +  const ss = Services.search;
    1.14 +  const ENGINE_NAME = "Foo";
    1.15 +  var contextMenu;
    1.16 +
    1.17 +  function observer(aSub, aTopic, aData) {
    1.18 +    switch (aData) {
    1.19 +      case "engine-added":
    1.20 +        var engine = ss.getEngineByName(ENGINE_NAME);
    1.21 +        ok(engine, "Engine was added.");
    1.22 +        ss.currentEngine = engine;
    1.23 +        break;
    1.24 +      case "engine-current":
    1.25 +        is(ss.currentEngine.name, ENGINE_NAME, "currentEngine set");
    1.26 +        startTest();
    1.27 +        break;
    1.28 +      case "engine-removed":
    1.29 +        Services.obs.removeObserver(observer, "browser-search-engine-modified");
    1.30 +        finish();
    1.31 +        break;
    1.32 +    }
    1.33 +  }
    1.34 +
    1.35 +  Services.obs.addObserver(observer, "browser-search-engine-modified", false);
    1.36 +  ss.addEngine("http://mochi.test:8888/browser/browser/components/search/test/testEngine_mozsearch.xml",
    1.37 +               Ci.nsISearchEngine.DATA_XML, "data:image/x-icon,%00",
    1.38 +               false);
    1.39 +
    1.40 +  function startTest() {
    1.41 +    contextMenu = document.getElementById("contentAreaContextMenu");
    1.42 +    ok(contextMenu, "Got context menu XUL");
    1.43 +
    1.44 +    doOnloadOnce(testContextMenu);
    1.45 +    let tab = gBrowser.selectedTab = gBrowser.addTab("data:text/plain;charset=utf8,test%20search");
    1.46 +    registerCleanupFunction(function () {
    1.47 +      gBrowser.removeTab(tab);
    1.48 +    });
    1.49 +  }
    1.50 +
    1.51 +  function testContextMenu() {
    1.52 +    function rightClickOnDocument() {
    1.53 +      info("rightClickOnDocument: " + content.window.location);
    1.54 +      waitForBrowserContextMenu(checkContextMenu);
    1.55 +      var clickTarget = content.document.body;
    1.56 +      var eventDetails = { type: "contextmenu", button: 2 };
    1.57 +      EventUtils.synthesizeMouseAtCenter(clickTarget, eventDetails, content);
    1.58 +    }
    1.59 +
    1.60 +    // check the search menu item and then perform a search
    1.61 +    function checkContextMenu() {
    1.62 +      info("checkContextMenu");
    1.63 +      var searchItem = contextMenu.getElementsByAttribute("id", "context-searchselect")[0];
    1.64 +      ok(searchItem, "Got search context menu item");
    1.65 +      is(searchItem.label, 'Search ' + ENGINE_NAME + ' for "test search"', "Check context menu label");
    1.66 +      is(searchItem.disabled, false, "Check that search context menu item is enabled");
    1.67 +      doOnloadOnce(checkSearchURL);
    1.68 +      searchItem.click();
    1.69 +      contextMenu.hidePopup();
    1.70 +    }
    1.71 +
    1.72 +    function checkSearchURL(event) {
    1.73 +      is(event.originalTarget.URL,
    1.74 +         "http://mochi.test:8888/browser/browser/components/search/test/?test=test+search&ie=utf-8&client=app&channel=contextsearch",
    1.75 +         "Checking context menu search URL");
    1.76 +      // Remove the tab opened by the search
    1.77 +      gBrowser.removeCurrentTab();
    1.78 +      ss.removeEngine(ss.currentEngine);
    1.79 +    }
    1.80 +
    1.81 +    var selectionListener = {
    1.82 +      notifySelectionChanged: function(doc, sel, reason) {
    1.83 +        if (reason != Ci.nsISelectionListener.SELECTALL_REASON || sel.toString() != "test search")
    1.84 +          return;
    1.85 +        info("notifySelectionChanged: Text selected");
    1.86 +        content.window.getSelection().QueryInterface(Ci.nsISelectionPrivate).
    1.87 +                                      removeSelectionListener(selectionListener);
    1.88 +        SimpleTest.executeSoon(rightClickOnDocument);
    1.89 +      }
    1.90 +    };
    1.91 +
    1.92 +    // Delay the select all to avoid intermittent selection failures.
    1.93 +    setTimeout(function delaySelectAll() {
    1.94 +      info("delaySelectAll: " + content.window.location.toString());
    1.95 +      // add a listener to know when the selection takes effect
    1.96 +      content.window.getSelection().QueryInterface(Ci.nsISelectionPrivate).
    1.97 +                                    addSelectionListener(selectionListener);
    1.98 +      // select the text on the page
    1.99 +      goDoCommand('cmd_selectAll');
   1.100 +    }, 500);
   1.101 +  }
   1.102 +}

mercurial