Bug 1294937 - The context menu does not appear when right-click on the inspector-searchbox; r=gl draft
authorFred Lin <gasolin@mozilla.com>
Mon, 15 Aug 2016 11:23:16 +0800
changeset 401569 fd011fa238be2c0c2ac178ef911aa237a604e3cb
parent 401536 457289ddb7c94246e73b10200572494efc7e01c3
child 528515 75089733efb9ae25a5d0ac0482415175d207d054
push id26490
push userbmo:gasolin@mozilla.com
push dateWed, 17 Aug 2016 05:15:34 +0000
reviewersgl
bugs1294937
milestone51.0a1
Bug 1294937 - The context menu does not appear when right-click on the inspector-searchbox; r=gl MozReview-Commit-ID: 8Ebs5jeYlkq
devtools/client/inspector/inspector-search.js
devtools/client/inspector/test/browser.ini
devtools/client/inspector/test/browser_inspector_search-filter_context-menu.js
--- a/devtools/client/inspector/inspector-search.js
+++ b/devtools/client/inspector/inspector-search.js
@@ -36,18 +36,22 @@ function InspectorSearch(inspector, inpu
   this.searchClearButton = clearBtn;
   this._lastSearched = null;
 
   this.searchClearButton.hidden = true;
 
   this._onKeyDown = this._onKeyDown.bind(this);
   this._onInput = this._onInput.bind(this);
   this._onClearSearch = this._onClearSearch.bind(this);
+  this._onFilterTextboxContextMenu =
+    this._onFilterTextboxContextMenu.bind(this);
   this.searchBox.addEventListener("keydown", this._onKeyDown, true);
   this.searchBox.addEventListener("input", this._onInput, true);
+  this.searchBox.addEventListener("contextmenu",
+    this._onFilterTextboxContextMenu);
   this.searchClearButton.addEventListener("click", this._onClearSearch);
 
   // For testing, we need to be able to wait for the most recent node request
   // to finish.  Tests can watch this promise for that.
   this._lastQuery = promise.resolve(null);
 
   this.autocompleter = new SelectorAutocompleter(inspector, input);
   EventEmitter.decorate(this);
@@ -58,16 +62,18 @@ exports.InspectorSearch = InspectorSearc
 InspectorSearch.prototype = {
   get walker() {
     return this.inspector.walker;
   },
 
   destroy: function () {
     this.searchBox.removeEventListener("keydown", this._onKeyDown, true);
     this.searchBox.removeEventListener("input", this._onInput, true);
+    this.searchBox.removeEventListener("contextmenu",
+      this._onFilterTextboxContextMenu);
     this.searchClearButton.removeEventListener("click", this._onClearSearch);
     this.searchBox = null;
     this.searchClearButton = null;
     this.autocompleter.destroy();
   },
 
   _onSearch: function (reverse = false) {
     this.doFullTextSearch(this.searchBox.value, reverse)
@@ -126,16 +132,28 @@ InspectorSearch.prototype = {
     const modifierKey = system.constants.platform === "macosx"
                         ? event.metaKey : event.ctrlKey;
     if (event.keyCode === event.DOM_VK_G && modifierKey) {
       this._onSearch(event.shiftKey);
       event.preventDefault();
     }
   },
 
+  /**
+   * Context menu handler for filter search box.
+   */
+  _onFilterTextboxContextMenu: function (event) {
+    try {
+      let contextmenu = this.inspector.toolbox.textboxContextMenuPopup;
+      contextmenu.openPopupAtScreen(event.screenX, event.screenY, true);
+    } catch (e) {
+      console.error(e);
+    }
+  },
+
   _onClearSearch: function () {
     this.searchBox.value = "";
     this.searchClearButton.hidden = true;
   }
 };
 
 /**
  * Converts any input box on a page to a CSS selector search and suggestion box.
--- a/devtools/client/inspector/test/browser.ini
+++ b/devtools/client/inspector/test/browser.ini
@@ -132,16 +132,17 @@ skip-if = os == "mac" # Full keyboard na
 [browser_inspector_search-01.js]
 [browser_inspector_search-02.js]
 [browser_inspector_search-03.js]
 [browser_inspector_search-04.js]
 [browser_inspector_search-05.js]
 [browser_inspector_search-06.js]
 [browser_inspector_search-07.js]
 [browser_inspector_search-08.js]
+[browser_inspector_search-filter_context-menu.js]
 [browser_inspector_search_keyboard_trap.js]
 [browser_inspector_search-reserved.js]
 [browser_inspector_search-selection.js]
 [browser_inspector_search-sidebar.js]
 [browser_inspector_select-docshell.js]
 [browser_inspector_select-last-selected.js]
 [browser_inspector_search-navigation.js]
 [browser_inspector_sidebarstate.js]
new file mode 100644
--- /dev/null
+++ b/devtools/client/inspector/test/browser_inspector_search-filter_context-menu.js
@@ -0,0 +1,68 @@
+/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+"use strict";
+
+// Test inspector's markup view search filter context menu works properly.
+
+const TEST_INPUT = "h1";
+const TEST_URI = "<h1>test filter context menu</h1>";
+
+add_task(function* () {
+  yield addTab("data:text/html;charset=utf-8," + encodeURIComponent(TEST_URI));
+  let {toolbox, inspector} = yield openInspector();
+  let {searchBox} = inspector;
+  yield selectNode("h1", inspector);
+
+  let win = inspector.panelWin;
+  let searchContextMenu = toolbox.textboxContextMenuPopup;
+  ok(searchContextMenu,
+    "The search filter context menu is loaded in the inspector");
+
+  let cmdUndo = searchContextMenu.querySelector("[command=cmd_undo]");
+  let cmdDelete = searchContextMenu.querySelector("[command=cmd_delete]");
+  let cmdSelectAll = searchContextMenu.querySelector("[command=cmd_selectAll]");
+  let cmdCut = searchContextMenu.querySelector("[command=cmd_cut]");
+  let cmdCopy = searchContextMenu.querySelector("[command=cmd_copy]");
+  let cmdPaste = searchContextMenu.querySelector("[command=cmd_paste]");
+
+  info("Opening context menu");
+  let onContextMenuPopup = once(searchContextMenu, "popupshowing");
+  EventUtils.synthesizeMouse(searchBox, 2, 2,
+    {type: "contextmenu", button: 2}, win);
+  yield onContextMenuPopup;
+
+  is(cmdUndo.getAttribute("disabled"), "true", "cmdUndo is disabled");
+  is(cmdDelete.getAttribute("disabled"), "true", "cmdDelete is disabled");
+  is(cmdSelectAll.getAttribute("disabled"), "", "cmdSelectAll is enabled");
+  is(cmdCut.getAttribute("disabled"), "true", "cmdCut is disabled");
+  is(cmdCopy.getAttribute("disabled"), "true", "cmdCopy is disabled");
+  is(cmdPaste.getAttribute("disabled"), "true", "cmdPaste is disabled");
+
+  info("Closing context menu");
+  let onContextMenuHidden = once(searchContextMenu, "popuphidden");
+  searchContextMenu.hidePopup();
+  yield onContextMenuHidden;
+
+  info("Copy text in search field using the context menu");
+  searchBox.value = TEST_INPUT;
+  searchBox.select();
+  EventUtils.synthesizeMouse(searchBox, 2, 2,
+    {type: "contextmenu", button: 2}, win);
+  yield onContextMenuPopup;
+  yield waitForClipboard(() => cmdCopy.click(), TEST_INPUT);
+  searchContextMenu.hidePopup();
+  yield onContextMenuHidden;
+
+  info("Reopen context menu and check command properties");
+  EventUtils.synthesizeMouse(searchBox, 2, 2,
+    {type: "contextmenu", button: 2}, win);
+  yield onContextMenuPopup;
+
+  is(cmdUndo.getAttribute("disabled"), "", "cmdUndo is enabled");
+  is(cmdDelete.getAttribute("disabled"), "", "cmdDelete is enabled");
+  is(cmdSelectAll.getAttribute("disabled"), "", "cmdSelectAll is enabled");
+  is(cmdCut.getAttribute("disabled"), "", "cmdCut is enabled");
+  is(cmdCopy.getAttribute("disabled"), "", "cmdCopy is enabled");
+  is(cmdPaste.getAttribute("disabled"), "", "cmdPaste is enabled");
+});