Merge "Merge top menu items contributed by plugins" into stable-2.16
diff --git a/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header.js b/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header.js
index 738d29e..a77c66d 100644
--- a/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header.js
+++ b/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header.js
@@ -179,11 +179,16 @@
     },
 
     _computeLinks(defaultLinks, userLinks, adminLinks, topMenus, docBaseUrl) {
-      const links = defaultLinks.slice();
+      const links = defaultLinks.map(menu => {
+        return {
+          title: menu.title,
+          links: menu.links.slice(),
+        };
+      });
       if (userLinks && userLinks.length > 0) {
         links.push({
           title: 'Your',
-          links: userLinks,
+          links: userLinks.slice(),
         });
       }
       const docLinks = this._getDocLinks(docBaseUrl, DOCUMENTATION_LINKS);
@@ -196,13 +201,20 @@
       }
       links.push({
         title: 'Browse',
-        links: adminLinks,
+        links: adminLinks.slice(),
       });
+      const topMenuLinks = [];
+      links.forEach(link => { topMenuLinks[link.title] = link.links; });
       for (const m of topMenus) {
-        links.push({
-          title: m.name,
-          links: m.items.map(this._fixCustomMenuItem),
-        });
+        const items = m.items.map(this._fixCustomMenuItem);
+        if (m.name in topMenuLinks) {
+          items.forEach(link => { topMenuLinks[m.name].push(link); });
+        } else {
+          links.push({
+            title: m.name,
+            links: topMenuLinks[m.name] = items,
+          });
+        }
       }
       return links;
     },
diff --git a/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header_test.html b/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header_test.html
index b6e64ec..582ca61 100644
--- a/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header_test.html
+++ b/polygerrit-ui/app/elements/core/gr-main-header/gr-main-header_test.html
@@ -191,6 +191,121 @@
       }]);
     });
 
+    test('merge top menus', () => {
+      const adminLinks = [{
+        name: 'Repos',
+        url: '/repos',
+      }];
+      const topMenus = [{
+        name: 'Plugins',
+        items: [{
+          name: 'Manage',
+          target: '_blank',
+          url: 'https://gerrit/plugins/plugin-manager/static/index.html',
+        }],
+      }, {
+        name: 'Plugins',
+        items: [{
+          name: 'Create',
+          target: '_blank',
+          url: 'https://gerrit/plugins/plugin-manager/static/create.html',
+        }],
+      }];
+      assert.deepEqual(element._computeLinks([], [], adminLinks, topMenus), [{
+        title: 'Browse',
+        links: adminLinks,
+      }, {
+        title: 'Plugins',
+        links: [{
+          name: 'Manage',
+          external: true,
+          url: 'https://gerrit/plugins/plugin-manager/static/index.html',
+        }, {
+          name: 'Create',
+          external: true,
+          url: 'https://gerrit/plugins/plugin-manager/static/create.html',
+        }],
+      }]);
+    });
+
+    test('merge top menus in default links', () => {
+      const defaultLinks = [{
+        title: 'Faves',
+        links: [{
+          name: 'Pinterest',
+          url: 'https://pinterest.com',
+        }],
+      }];
+      const topMenus = [{
+        name: 'Faves',
+        items: [{
+          name: 'Manage',
+          target: '_blank',
+          url: 'https://gerrit/plugins/plugin-manager/static/index.html',
+        }],
+      }];
+      assert.deepEqual(element._computeLinks(defaultLinks, [], [], topMenus), [{
+        title: 'Faves',
+        links: defaultLinks[0].links.concat([{
+          name: 'Manage',
+          external: true,
+          url: 'https://gerrit/plugins/plugin-manager/static/index.html',
+        }]),
+      }, {
+        title: 'Browse',
+        links: [],
+      }]);
+    });
+
+    test('merge top menus in user links', () => {
+      const userLinks = [{
+        name: 'Facebook',
+        url: 'https://facebook.com',
+      }];
+      const topMenus = [{
+        name: 'Your',
+        items: [{
+          name: 'Manage',
+          target: '_blank',
+          url: 'https://gerrit/plugins/plugin-manager/static/index.html',
+        }],
+      }];
+      assert.deepEqual(element._computeLinks([], userLinks, [], topMenus), [{
+        title: 'Your',
+        links: userLinks.concat([{
+          name: 'Manage',
+          external: true,
+          url: 'https://gerrit/plugins/plugin-manager/static/index.html',
+        }]),
+      }, {
+        title: 'Browse',
+        links: [],
+      }]);
+    });
+
+    test('merge top menus in admin links', () => {
+      const adminLinks = [{
+        name: 'Repos',
+        url: '/repos',
+      }];
+      const topMenus = [{
+        name: 'Browse',
+        items: [{
+          name: 'Manage',
+          target: '_blank',
+          url: 'https://gerrit/plugins/plugin-manager/static/index.html',
+        }],
+      }];
+      assert.deepEqual(element._computeLinks([], [], adminLinks, topMenus), [{
+        title: 'Browse',
+        links: adminLinks.concat([{
+          name: 'Manage',
+          external: true,
+          url: 'https://gerrit/plugins/plugin-manager/static/index.html',
+        }]),
+      }]);
+    });
+
     test('register URL', () => {
       const config = {
         auth: {