Bug 713192. (Av1c-FF) Improve code. r=gavin.sharp.
authorSerge Gautherie <sgautherie.bz@free.fr>
Sun, 08 Jan 2012 04:09:00 +0100
changeset 85239 cf8c9f9aeefc43d6801c2253c9c08eda985d7568
parent 85238 0e46065c6d9f52ffba5edef215795826d5fb053a
child 85242 9a230265bad5fb7d2c4ead3c9e4173081d8e90a7
push id805
push userakeybl@mozilla.com
push dateWed, 01 Feb 2012 18:17:35 +0000
treeherdermozilla-aurora@6fb3bf232436 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgavin
bugs713192
milestone12.0a1
Bug 713192. (Av1c-FF) Improve code. r=gavin.sharp.
browser/base/content/test/test_contextmenu.html
--- a/browser/base/content/test/test_contextmenu.html
+++ b/browser/base/content/test/test_contextmenu.html
@@ -1,13 +1,13 @@
 <!DOCTYPE HTML>
 <html>
 <head>
   <title>Tests for browser context menu</title>
-  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>  
+  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
   <script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
 </head>
 <body>
 Browser context menu tests.
 <p id="display"></p>
 
 <div id="content">
@@ -23,39 +23,31 @@ Components.utils.import("resource://gre/
 
 const Cc = Components.classes;
 const Ci = Components.interfaces;
 
 function openContextMenuFor(element, shiftkey, shouldWaitForFocus) {
     // Context menu should be closed before we open it again.
     is(contextMenu.state, "closed", "checking if popup is closed");
 
-    //Some elements need time to focus and spellcheck before any tests are
-    //run on them.
-    if(shouldWaitForFocus)
-    {
-      if (lastElement)
-        lastElement.blur();
-      element.focus();
-      
-      SimpleTest.executeSoon(function() {
-        lastElement = element;
-        var eventDetails = { type : "contextmenu", button : 2, shiftKey : shiftkey };
-        synthesizeMouse(element, 2, 2, eventDetails, element.ownerDocument.defaultView);
-      });
-    }
-    else
-    {
-      if (lastElement)
-          lastElement.blur();
-      element.focus();
+    if (lastElement)
+      lastElement.blur();
+    element.focus();
+
+    // Some elements need time to focus and spellcheck before any tests are
+    // run on them.
+    function actuallyOpenContextMenuFor() {
       lastElement = element;
       var eventDetails = { type : "contextmenu", button : 2, shiftKey : shiftkey };
       synthesizeMouse(element, 2, 2, eventDetails, element.ownerDocument.defaultView);
     }
+    if (shouldWaitForFocus)
+      SimpleTest.executeSoon(actuallyOpenContextMenuFor);
+    else
+      actuallyOpenContextMenuFor();
 }
 
 function closeContextMenu() {
     contextMenu.hidePopup();
 }
 
 function executeCopyCommand(command, expectedValue)
 {
@@ -72,17 +64,17 @@ function executeCopyCommand(command, exp
 }
 
 function invokeItemAction(generatedItemId)
 {
   var item = contextMenu.getElementsByAttribute("generateditemid",
                                                 generatedItemId)[0];
   ok(item, "Got generated XUL menu item");
   item.doCommand();
-  is(pagemenu.hasAttribute("hopeless"), false, "attribute got removed");
+  ok(!pagemenu.hasAttribute("hopeless"), "attribute got removed");
 }
 
 function getVisibleMenuItems(aMenu, aData) {
     var items = [];
     var accessKeys = {};
     for (var i = 0; i < aMenu.childNodes.length; i++) {
         var item = aMenu.childNodes[i];
         if (item.hidden)
@@ -173,32 +165,36 @@ function checkContextMenu(expectedItems)
  * expectedItems is a array of (1) item IDs and (2) a boolean specifying if
  * the item is enabled or not (or null to ignore it). Submenus can be checked
  * by providing a nested array entry after the expected <menu> ID.
  * For example: ["blah", true,              // item enabled
  *               "submenu", null,           // submenu
  *                   ["sub1", true,         // submenu contents
  *                    "sub2", false], null, // submenu contents
  *               "lol", false]              // item disabled
- * 
+ *
  */
 function checkMenu(menu, expectedItems, data) {
     var actualItems = getVisibleMenuItems(menu, data);
     //ok(false, "Items are: " + actualItems);
     for (var i = 0; i < expectedItems.length; i+=2) {
         var actualItem   = actualItems[i];
         var actualEnabled = actualItems[i + 1];
         var expectedItem = expectedItems[i];
         var expectedEnabled = expectedItems[i + 1];
         if (expectedItem instanceof Array) {
             ok(true, "Checking submenu...");
             var menuID = expectedItems[i - 2]; // The last item was the menu ID.
             var submenu = menu.getElementsByAttribute("id", menuID)[0];
-            ok(submenu && submenu.nodeName == "menu", "got expected submenu element");
-            checkMenu(submenu.menupopup, expectedItem, data);
+            ok(submenu, "got a submenu element of id='" + menuID + "'");
+            if (submenu) {
+              is(submenu.nodeName, "menu", "submenu element of id='" + menuID +
+                                           "' has expected nodeName");
+              checkMenu(submenu.menupopup, expectedItem, data);
+            }
         } else {
             is(actualItem, expectedItem,
                "checking item #" + i/2 + " (" + expectedItem + ") name");
 
             if (typeof expectedEnabled == "object" && expectedEnabled != null ||
                 typeof actualEnabled == "object" && actualEnabled != null) {
 
                 ok(!(actualEnabled == null), "actualEnabled is not null");
@@ -508,17 +504,17 @@ function runTest(testNum) {
                           "context-delete",      false,
                           "---",                 null,
                           "context-selectall",   true,
                           "---",                 null,
                           "spell-check-enabled", true,
                           "spell-dictionaries",  true,
                               ["spell-check-dictionary-en-US", true,
                                "---",                          null,
-                               "spell-add-dictionaries",       true], null,
+                               "spell-add-dictionaries",       true], null
                          ].concat(inspectItems));
         contextMenu.ownerDocument.getElementById("spell-add-to-dictionary").doCommand(); // Add to dictionary
         closeContextMenu();
         openContextMenuFor(textarea, false, true); // Invoke context menu for next test.
         break;
     
     case 15:    
         // Context menu for textarea after a word has been added
@@ -532,17 +528,17 @@ function runTest(testNum) {
                           "context-delete",      false,
                           "---",                 null,
                           "context-selectall",   true,
                           "---",                 null,
                           "spell-check-enabled", true,
                           "spell-dictionaries",  true,
                               ["spell-check-dictionary-en-US", true,
                                "---",                          null,
-                               "spell-add-dictionaries",       true], null,
+                               "spell-add-dictionaries",       true], null
                          ].concat(inspectItems));
         contextMenu.ownerDocument.getElementById("spell-undo-add-to-dictionary").doCommand(); // Undo add to dictionary
         closeContextMenu();
         openContextMenuFor(contenteditable);
         break;
 
     case 16:
         // Context menu for contenteditable
@@ -656,16 +652,17 @@ function runTest(testNum) {
                           "context-sendpage",     true,
                           "---",                  null,
                           "context-viewbgimage",  false,
                           "context-selectall",    true,
                           "---",                  null,
                           "context-viewsource",   true,
                           "context-viewinfo",     true
                          ].concat(inspectItems));
+        closeContextMenu();
 
         subwindow.close();
         SimpleTest.finish();
         return;
 
     /*
      * Other things that would be nice to test:
      *  - selected text
@@ -698,16 +695,17 @@ function startTest() {
                     .rootTreeItem
                     .QueryInterface(Ci.nsIInterfaceRequestor)
                     .getInterface(Ci.nsIDOMWindow)
                     .QueryInterface(Ci.nsIDOMChromeWindow);
     contextMenu = chromeWin.document.getElementById("contentAreaContextMenu");
     ok(contextMenu, "Got context menu XUL");
 
     if (chromeWin.document.getElementById("Browser:Stop").getAttribute("disabled") != "true") {
+      todo(false, "Wait for subwindow to load... (This should usually happen once.)");
       SimpleTest.executeSoon(startTest);
       return;
     }
 
     lastElement = null;
 
     text   = subwindow.document.getElementById("test-text");
     link   = subwindow.document.getElementById("test-link");