Bug 997406 - Default to Link context menu items in context menus. r=bnicholson, a=lsblakk
authorWes Johnston <wjohnston@mozilla.com>
Tue, 15 Apr 2014 12:49:38 -0700
changeset 191997 08116c1e9a26e8abe26855380d0bce57ea99ced2
parent 191996 981efa650de8aa9dface24a21c4b70409739091d
child 191998 bc3f5515b7c22e6d58ab21f5567cb2fd1bbecf64
push id3503
push userraliiev@mozilla.com
push dateMon, 28 Apr 2014 18:51:11 +0000
treeherdermozilla-beta@c95ac01e332e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbnicholson, lsblakk
bugs997406
milestone30.0a2
Bug 997406 - Default to Link context menu items in context menus. r=bnicholson, a=lsblakk
mobile/android/base/tests/testPictureLinkContextMenu.java
mobile/android/chrome/content/browser.js
--- a/mobile/android/base/tests/testPictureLinkContextMenu.java
+++ b/mobile/android/base/tests/testPictureLinkContextMenu.java
@@ -6,46 +6,44 @@ public class testPictureLinkContextMenu 
 
     // Test website strings
     private static String PICTURE_PAGE_URL;
     private static String BLANK_PAGE_URL;
     private static final String PICTURE_PAGE_TITLE = "Picture Link";
     private static final String tabs [] = { "Image", "Link" };
     private static final String photoMenuItems [] = { "Copy Image Location", "Share Image", "Set Image As", "Save Image" };
     private static final String linkMenuItems [] = { "Open Link in New Tab", "Open Link in Private Tab", "Copy Link", "Share Link", "Bookmark Link"};
-    private static final String linkTitle = "^Link$";
+    private static final String imageTitle = "^Image$";
 
     @Override
     protected int getTestType() {
         return TEST_MOCHITEST;
     }
 
     public void testPictureLinkContextMenu() {
         blockForGeckoReady();
 
         PICTURE_PAGE_URL=getAbsoluteUrl("/robocop/robocop_picture_link.html");
         BLANK_PAGE_URL=getAbsoluteUrl("/robocop/robocop_blank_02.html");
         loadAndPaint(PICTURE_PAGE_URL);
         verifyPageTitle(PICTURE_PAGE_TITLE);
 
+        switchTabs(imageTitle);
         verifyContextMenuItems(photoMenuItems);
         verifyTabs(tabs);
+        switchTabs(imageTitle);
         verifyCopyOption(photoMenuItems[0], "Firefox.jpg"); // Test the "Copy Image Location" option
+        switchTabs(imageTitle);
         verifyShareOption(photoMenuItems[1], PICTURE_PAGE_TITLE); // Test the "Share Image" option
 
-        switchTabs(linkTitle);
         verifyContextMenuItems(linkMenuItems);
         openTabFromContextMenu(linkMenuItems[0],2); // Test the "Open in New Tab" option - expecting 2 tabs: the original and the new one
-        switchTabs(linkTitle);
         openTabFromContextMenu(linkMenuItems[1],2); // Test the "Open in Private Tab" option - expecting only 2 tabs in normal mode
-        switchTabs(linkTitle);
         verifyCopyOption(linkMenuItems[2], BLANK_PAGE_URL); // Test the "Copy Link" option
-        switchTabs(linkTitle);
         verifyShareOption(linkMenuItems[3], PICTURE_PAGE_TITLE); // Test the "Share Link" option
-        switchTabs(linkTitle);
         verifyBookmarkLinkOption(linkMenuItems[4],BLANK_PAGE_URL); // Test the "Bookmark Link" option
     }
 
     @Override
     public void tearDown() throws Exception {
         mDatabaseHelper.deleteBookmark(BLANK_PAGE_URL);
         super.tearDown();
     }
--- a/mobile/android/chrome/content/browser.js
+++ b/mobile/android/chrome/content/browser.js
@@ -2145,16 +2145,21 @@ var NativeWindow = {
     },
 
     set _target(aTarget) {
       if (aTarget)
         this._targetRef = Cu.getWeakReference(aTarget);
       else this._targetRef = null;
     },
 
+    get defaultContext() {
+      delete this.defaultContext;
+      return this.defaultContext = Strings.browser.GetStringFromName("browser.menu.context.default");
+    },
+
     /* Gets menuitems for an arbitrary node
      * Parameters:
      *   element - The element to look at. If this element has a contextmenu attribute, the
      *             corresponding contextmenu will be used.
      */
     _getHTMLContextMenuItemsForElement: function(element) {
       let htmlMenu = element.contextMenu;
       if (!htmlMenu) {
@@ -2226,17 +2231,17 @@ var NativeWindow = {
       }
 
       // Otherwise we try the nodeName
       try {
         return Strings.browser.GetStringFromName("browser.menu.context." + element.nodeName.toLowerCase());
       } catch(ex) { }
 
       // Fallback to the default
-      return Strings.browser.GetStringFromName("browser.menu.context.default");
+      return this.defaultContext;
     },
 
     // Adds context menu items added through the add-on api
     _getNativeContextMenuItems: function(element, x, y) {
       let res = [];
       for (let itemId of Object.keys(this.items)) {
         let item = this.items[itemId];
 
@@ -2381,17 +2386,18 @@ var NativeWindow = {
      * menus, will return an array with appropriate tabs/items inside it. i.e. :
      * [
      *    { label: "link", items: [...] },
      *    { label: "image", items: [...] }
      * ]
      */
     _reformatList: function(target) {
       let contexts = Object.keys(this.menus);
-      if (contexts.length == 1) {
+
+      if (contexts.length === 1) {
         // If there's only one context, we'll only show a single flat single select list
         return this._reformatMenuItems(target, this.menus[contexts[0]]);
       }
 
       // If there are multiple contexts, we'll only show a tabbed ui with multiple lists
       return this._reformatListAsTabs(target, this.menus);
     },
 
@@ -2400,22 +2406,34 @@ var NativeWindow = {
      * { link: [...], image: [...] } becomes
      * [ { label: "link", items: [...] } ]
      *
      * Also reformats items and resolves any parmaeters that aren't known until display time
      * (for instance Helper app menu items adjust their title to reflect what Helper App can be used for this link).
      */
     _reformatListAsTabs: function(target, menus) {
       let itemArray = [];
-      for (let context in menus) {
+
+      // Sort the keys so that "link" is always first
+      let contexts = Object.keys(this.menus);
+      contexts.sort((context1, context2) => {
+        if (context1 === this.defaultContext) {
+          return -1;
+        } else if (context2 === this.defaultContext) {
+          return 1;
+        }
+        return 0;
+      });
+
+      contexts.forEach(context => {
         itemArray.push({
           label: context,
           items: this._reformatMenuItems(target, menus[context])
         });
-      }
+      });
 
       return itemArray;
     },
 
     /* Reformats an array of ContextMenuItems into an array that can be handled by Prompt.jsm. Also reformats items
      * and resolves any parmaeters that aren't known until display time
      * (for instance Helper app menu items adjust their title to reflect what Helper App can be used for this link).
      */
@@ -8535,18 +8553,20 @@ HTMLContextMenuItem.prototype = Object.c
 
           let elt = this.menuElementRef.get();
           let target = this.targetElementRef.get();
           if (!elt) {
             return;
           }
 
           var items = NativeWindow.contextmenus._getHTMLContextMenuItemsForMenu(elt, target);
+          // This menu will always only have one context, but we still make sure its the "right" one.
+          var context = NativeWindow.contextmenus._getContextType(target);
           if (items.length > 0) {
-            NativeWindow.contextmenus._addMenuItems(items, "link");
+            NativeWindow.contextmenus._addMenuItems(items, context);
           }
 
         } catch(ex) {
           Cu.reportError(ex);
         }
       } else {
         // otherwise just click the menu item
         elt.click();