browser/devtools/scratchpad/test/browser_scratchpad_edit_ui_updates.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/devtools/scratchpad/test/browser_scratchpad_edit_ui_updates.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,184 @@
     1.4 +/* vim: set ts=2 et sw=2 tw=80: */
     1.5 +/* Any copyright is dedicated to the Public Domain.
     1.6 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.7 +/* Bug 699130 */
     1.8 +
     1.9 +"use strict";
    1.10 +
    1.11 +function test()
    1.12 +{
    1.13 +  waitForExplicitFinish();
    1.14 +
    1.15 +  gBrowser.selectedTab = gBrowser.addTab();
    1.16 +  gBrowser.selectedBrowser.addEventListener("load", function onLoad() {
    1.17 +    gBrowser.selectedBrowser.removeEventListener("load", onLoad, true);
    1.18 +    openScratchpad(runTests);
    1.19 +  }, true);
    1.20 +
    1.21 +  content.location = "data:text/html,test Edit menu updates Scratchpad - bug 699130";
    1.22 +}
    1.23 +
    1.24 +function runTests()
    1.25 +{
    1.26 +  let sp = gScratchpadWindow.Scratchpad;
    1.27 +  let doc = gScratchpadWindow.document;
    1.28 +  let winUtils = gScratchpadWindow.QueryInterface(Ci.nsIInterfaceRequestor).
    1.29 +                 getInterface(Ci.nsIDOMWindowUtils);
    1.30 +  let OS = Services.appinfo.OS;
    1.31 +
    1.32 +  info("will test the Edit menu");
    1.33 +
    1.34 +  let pass = 0;
    1.35 +
    1.36 +  sp.setText("bug 699130: hello world! (edit menu)");
    1.37 +
    1.38 +  let editMenu = doc.getElementById("sp-edit-menu");
    1.39 +  ok(editMenu, "the Edit menu");
    1.40 +  let menubar = editMenu.parentNode;
    1.41 +  ok(menubar, "menubar found");
    1.42 +
    1.43 +  let editMenuIndex = -1;
    1.44 +  for (let i = 0; i < menubar.children.length; i++) {
    1.45 +    if (menubar.children[i] === editMenu) {
    1.46 +      editMenuIndex = i;
    1.47 +      break;
    1.48 +    }
    1.49 +  }
    1.50 +  isnot(editMenuIndex, -1, "Edit menu index is correct");
    1.51 +
    1.52 +  let menuPopup = editMenu.menupopup;
    1.53 +  ok(menuPopup, "the Edit menupopup");
    1.54 +  let cutItem = doc.getElementById("menu_cut");
    1.55 +  ok(cutItem, "the Cut menuitem");
    1.56 +  let pasteItem = doc.getElementById("menu_paste");
    1.57 +  ok(pasteItem, "the Paste menuitem");
    1.58 +
    1.59 +  let anchor = doc.documentElement;
    1.60 +  let isContextMenu = false;
    1.61 +
    1.62 +  let openMenu = function(aX, aY, aCallback) {
    1.63 +    if (!editMenu || OS != "Darwin") {
    1.64 +      menuPopup.addEventListener("popupshown", function onPopupShown() {
    1.65 +        menuPopup.removeEventListener("popupshown", onPopupShown, false);
    1.66 +        executeSoon(aCallback);
    1.67 +      }, false);
    1.68 +    }
    1.69 +
    1.70 +    executeSoon(function() {
    1.71 +      if (editMenu) {
    1.72 +        if (OS == "Darwin") {
    1.73 +          winUtils.forceUpdateNativeMenuAt(editMenuIndex);
    1.74 +          executeSoon(aCallback);
    1.75 +        } else {
    1.76 +          editMenu.open = true;
    1.77 +        }
    1.78 +      } else {
    1.79 +        menuPopup.openPopup(anchor, "overlap", aX, aY, isContextMenu, false);
    1.80 +      }
    1.81 +    });
    1.82 +  };
    1.83 +
    1.84 +  let closeMenu = function(aCallback) {
    1.85 +    if (!editMenu || OS != "Darwin") {
    1.86 +      menuPopup.addEventListener("popuphidden", function onPopupHidden() {
    1.87 +        menuPopup.removeEventListener("popuphidden", onPopupHidden, false);
    1.88 +        executeSoon(aCallback);
    1.89 +      }, false);
    1.90 +    }
    1.91 +
    1.92 +    executeSoon(function() {
    1.93 +      if (editMenu) {
    1.94 +        if (OS == "Darwin") {
    1.95 +          winUtils.forceUpdateNativeMenuAt(editMenuIndex);
    1.96 +          executeSoon(aCallback);
    1.97 +        } else {
    1.98 +          editMenu.open = false;
    1.99 +        }
   1.100 +      } else {
   1.101 +        menuPopup.hidePopup();
   1.102 +      }
   1.103 +    });
   1.104 +  };
   1.105 +
   1.106 +  let firstShow = function() {
   1.107 +    ok(cutItem.hasAttribute("disabled"), "cut menuitem is disabled");
   1.108 +    closeMenu(firstHide);
   1.109 +  };
   1.110 +
   1.111 +  let firstHide = function() {
   1.112 +    sp.editor.setSelection({ line: 0, ch: 0 }, { line: 0, ch: 10 });
   1.113 +    openMenu(11, 11, showAfterSelect);
   1.114 +  };
   1.115 +
   1.116 +  let showAfterSelect = function() {
   1.117 +    ok(!cutItem.hasAttribute("disabled"), "cut menuitem is enabled after select");
   1.118 +    closeMenu(hideAfterSelect);
   1.119 +  };
   1.120 +
   1.121 +  let hideAfterSelect = function() {
   1.122 +    sp.editor.on("change", onCut);
   1.123 +    waitForFocus(function () {
   1.124 +      let selectedText = sp.editor.getSelection();
   1.125 +      ok(selectedText.length > 0, "non-empty selected text will be cut");
   1.126 +
   1.127 +      EventUtils.synthesizeKey("x", {accelKey: true}, gScratchpadWindow);
   1.128 +    }, gScratchpadWindow);
   1.129 +  };
   1.130 +
   1.131 +  let onCut = function() {
   1.132 +    sp.editor.off("change", onCut);
   1.133 +    openMenu(12, 12, showAfterCut);
   1.134 +  };
   1.135 +
   1.136 +  let showAfterCut = function() {
   1.137 +    ok(cutItem.hasAttribute("disabled"), "cut menuitem is disabled after cut");
   1.138 +    ok(!pasteItem.hasAttribute("disabled"), "paste menuitem is enabled after cut");
   1.139 +    closeMenu(hideAfterCut);
   1.140 +  };
   1.141 +
   1.142 +  let hideAfterCut = function() {
   1.143 +    sp.editor.on("change", onPaste);
   1.144 +    waitForFocus(function () {
   1.145 +      EventUtils.synthesizeKey("v", {accelKey: true}, gScratchpadWindow);
   1.146 +    }, gScratchpadWindow);
   1.147 +  };
   1.148 +
   1.149 +  let onPaste = function() {
   1.150 +    sp.editor.off("change", onPaste);
   1.151 +    openMenu(13, 13, showAfterPaste);
   1.152 +  };
   1.153 +
   1.154 +  let showAfterPaste = function() {
   1.155 +    ok(cutItem.hasAttribute("disabled"), "cut menuitem is disabled after paste");
   1.156 +    ok(!pasteItem.hasAttribute("disabled"), "paste menuitem is enabled after paste");
   1.157 +    closeMenu(hideAfterPaste);
   1.158 +  };
   1.159 +
   1.160 +  let hideAfterPaste = function() {
   1.161 +    if (pass == 0) {
   1.162 +      pass++;
   1.163 +      testContextMenu();
   1.164 +    } else {
   1.165 +      finish();
   1.166 +    }
   1.167 +  };
   1.168 +
   1.169 +  let testContextMenu = function() {
   1.170 +    info("will test the context menu");
   1.171 +
   1.172 +    editMenu = null;
   1.173 +    isContextMenu = true;
   1.174 +
   1.175 +    menuPopup = doc.getElementById("scratchpad-text-popup");
   1.176 +    ok(menuPopup, "the context menupopup");
   1.177 +    cutItem = doc.getElementById("cMenu_cut");
   1.178 +    ok(cutItem, "the Cut menuitem");
   1.179 +    pasteItem = doc.getElementById("cMenu_paste");
   1.180 +    ok(pasteItem, "the Paste menuitem");
   1.181 +
   1.182 +    sp.setText("bug 699130: hello world! (context menu)");
   1.183 +    openMenu(10, 10, firstShow);
   1.184 +  };
   1.185 +
   1.186 +  openMenu(10, 10, firstShow);
   1.187 +}

mercurial