[Pkg-mozext-commits] [requestpolicy] 20/65: [imp] links in menu: tab opening behavior

David Prévot taffit at moszumanska.debian.org
Fri Mar 25 22:59:47 UTC 2016


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository requestpolicy.

commit 895224ecb0b3010b1d5fb3f2ff58452564ed0761
Author: Martin Kimmerle <dev at 256k.de>
Date:   Wed Jan 13 20:22:31 2016 +0100

    [imp] links in menu: tab opening behavior
    
    Switch to the corresponding tab if the URI is already loaded in
    a tab in the current window.
    
    Resolves #485
---
 src/content/lib/utils/javascript.jsm               |  18 +++
 src/content/ui/overlay.js                          |  36 ++++-
 tests/marionette/rp_puppeteer/ui/menu.py           |   8 +
 tests/marionette/tests/menu/manifest.ini           |   1 +
 .../marionette/tests/menu/test_settings_buttons.py | 175 +++++++++++++++++++++
 tests/xpcshell/test_jsutils.js                     |  25 +++
 6 files changed, 258 insertions(+), 5 deletions(-)

diff --git a/src/content/lib/utils/javascript.jsm b/src/content/lib/utils/javascript.jsm
index cd547a8..54ebbee 100644
--- a/src/content/lib/utils/javascript.jsm
+++ b/src/content/lib/utils/javascript.jsm
@@ -40,5 +40,23 @@ var JSUtils = (function() {
     return false;
   };
 
+  self.leftRotateArray = function(array, n) {
+    n = n % array.length;
+    let firstPart = array.slice(0, n);
+    let secondPart = array.slice(n);
+    return [].concat(secondPart, firstPart);
+  };
+
+  /**
+   * Create an array containing the elements [0, ..., n-1].
+   */
+  self.range = function(n) {
+    let array = [];
+    for (let i = 0; i < n; i++) {
+      array.push(i);
+    }
+    return array;
+  };
+
   return self;
 }());
diff --git a/src/content/ui/overlay.js b/src/content/ui/overlay.js
index a0c593b..c971d33 100644
--- a/src/content/ui/overlay.js
+++ b/src/content/ui/overlay.js
@@ -46,6 +46,7 @@ window.rpcontinued.overlay = (function() {
   let {DomainUtil} = importModule("lib/utils/domains");
   let {StringUtils} = importModule("lib/utils/strings");
   let {WindowUtils} = importModule("lib/utils/windows");
+  let {JSUtils} = importModule("lib/utils/javascript");
   let {Utils} = importModule("lib/utils");
   let {DOMUtils} = importModule("lib/utils/dom");
   let {C} = importModule("lib/utils/constants");
@@ -1116,11 +1117,36 @@ window.rpcontinued.overlay = (function() {
     popupElement.hidePopup();
   }
 
-  self.openPrefs = openLinkInNewTab.bind(this, "about:requestpolicy", true);
-  self.openPolicyManager = openLinkInNewTab.bind(this,
-      "about:requestpolicy?yourpolicy", true);
-  self.openHelp = openLinkInNewTab.bind(this, "https://github.com/" +
-      "RequestPolicyContinued/requestpolicy/wiki/Help-and-Support");
+  function maybeOpenLinkInNewTab(url, equivalentURLs, relatedToCurrent) {
+    let possibleURLs = equivalentURLs.concat(url);
+    let tabbrowser = window.gBrowser;
+
+    let selectedTabIndex = tabbrowser.tabContainer.selectedIndex;
+    let numTabs = tabbrowser.tabs.length;
+
+    // Start iterating at the currently selected tab.
+    let indexes = JSUtils.leftRotateArray(JSUtils.range(numTabs),
+		selectedTabIndex);
+    for (let index of indexes) {
+      let currentBrowser = tabbrowser.getBrowserAtIndex(index);
+      let currentURI = currentBrowser.currentURI.spec;
+      if (JSUtils.arrayIncludes(possibleURLs, currentURI)) {
+        // The URL is already opened. Select this tab.
+        tabbrowser.selectedTab = tabbrowser.tabContainer.childNodes[index];
+        popupElement.hidePopup();
+        return;
+      }
+    }
+
+    openLinkInNewTab(url, relatedToCurrent);
+  }
+
+  self.openPrefs = maybeOpenLinkInNewTab.bind(this, "about:requestpolicy",
+      ["about:requestpolicy?basicprefs"], true);
+  self.openPolicyManager = maybeOpenLinkInNewTab.bind(this,
+      "about:requestpolicy?yourpolicy", [], true);
+  self.openHelp = maybeOpenLinkInNewTab.bind(this, "https://github.com/" +
+      "RequestPolicyContinued/requestpolicy/wiki/Help-and-Support", []);
 
   self.clearRequestLog = function() {
     self.requestLog.clear();
diff --git a/tests/marionette/rp_puppeteer/ui/menu.py b/tests/marionette/rp_puppeteer/ui/menu.py
index 2301608..04921ca 100644
--- a/tests/marionette/rp_puppeteer/ui/menu.py
+++ b/tests/marionette/rp_puppeteer/ui/menu.py
@@ -34,6 +34,14 @@ class Menu(BaseLib):
             return
         self._toggle(trigger=trigger)
 
+    @property
+    def preferences_button(self):
+        return self._popup.find_element("id", "rpc-link-prefs")
+
+    @property
+    def manage_policies_button(self):
+        return self._popup.find_element("id", "rpc-link-policies")
+
     ##################################
     # Private Properties and Methods #
     ##################################
diff --git a/tests/marionette/tests/menu/manifest.ini b/tests/marionette/tests/menu/manifest.ini
index 9df86a6..19a25c8 100644
--- a/tests/marionette/tests/menu/manifest.ini
+++ b/tests/marionette/tests/menu/manifest.ini
@@ -1,2 +1,3 @@
 [test_num_requests.py]
 [test_open.py]
+[test_settings_buttons.py]
diff --git a/tests/marionette/tests/menu/test_settings_buttons.py b/tests/marionette/tests/menu/test_settings_buttons.py
new file mode 100644
index 0000000..eee7353
--- /dev/null
+++ b/tests/marionette/tests/menu/test_settings_buttons.py
@@ -0,0 +1,175 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+from rp_ui_harness import RequestPolicyTestCase
+
+
+URLS = {
+    1: ["http://www.maindomain.test/"],
+    2: ["http://www.otherdomain.test/"],
+    3: ["http://www.thirddomain.test/"],
+    "preferences": ["about:requestpolicy", "about:requestpolicy?basicprefs"],
+    "preferences_1": ["about:requestpolicy"],
+    "preferences_2": ["about:requestpolicy?basicprefs"],
+    "policies": ["about:requestpolicy?yourpolicy"]
+}
+
+
+class TestSettingsButtons(RequestPolicyTestCase):
+
+    TEST_URL = "http://www.maindomain.test/img_1.html";
+
+    def setUp(self):
+        super(TestSettingsButtons, self).setUp()
+        self.tabbar = self.browser.tabbar
+
+    def tearDown(self):
+        try:
+            self._close_all_tabs()
+        finally:
+            super(TestSettingsButtons, self).tearDown()
+
+    ################
+    # Test Methods #
+    ################
+
+    def _test__should_open(self, url_id, settings_id):
+        """Exactly one settings tab is open, which is at the same
+        time the one to be opened."""
+
+        self._open_tabs([1, url_id, 3], 1)
+        self._open_settings(settings_id)
+        self._check_tabs([1, url_id, settings_id, 3], 2)
+
+    def test_no_settings_tab_open(self):
+        """No settings tab is open."""
+        self._test__should_open(2, "preferences")
+        self._test__should_open(2, "policies")
+
+    def test_non_equivalent_settings_tab_open(self):
+        """A non-equivalent settings tab is open."""
+        self._test__should_open("policies", "preferences")
+        self._test__should_open("preferences_1", "policies")
+        self._test__should_open("preferences_2", "policies")
+
+
+    def _test__basic(self, url_id, settings_id):
+        """Exactly one settings tab is open, which is at the same
+        time the one to be opened."""
+
+        # Already on the correct tab
+        self._open_tabs([1, url_id, 3], 1)
+        self._open_settings(settings_id)
+        self._check_tabs([1, url_id, 3], 1)
+
+        # Switch to the correct tab
+        self._open_tabs([url_id, 2, 3], 1)
+        self._open_settings(settings_id)
+        self._check_tabs([url_id, 2, 3], 0)
+
+    def test_preferences__basic(self):
+        self._test__basic("preferences_1", "preferences")
+        self._test__basic("preferences_2", "preferences")
+
+    def test_policies__basic(self):
+        self._test__basic("policies", "policies")
+
+
+    def _test__multiple_equivalent_urls(self, url_id_1, url_id_2, settings_id):
+        """Multiple settings tabs are open, but all are equivalent.
+
+        However, the URLs still could be different; for example,
+        "about:requestpolicy" and "about:requestpolicy?basicprefs"
+        are equivalent.
+        """
+
+        # Already on the correct tab
+        self._open_tabs([url_id_1, url_id_2, 3], 1)
+        self._open_settings(settings_id)
+        self._check_tabs([url_id_1, url_id_2, 3], 1)
+
+        # Switch to the correct tab.
+        # The tab to the right of the current tab should be selected.
+        self._open_tabs([url_id_1, 2, url_id_2], 1)
+        self._open_settings(settings_id)
+        self._check_tabs([url_id_1, 2, url_id_2], 2)
+
+    def test_preferences__multiple(self):
+        self._test__multiple_equivalent_urls("preferences_1", "preferences_1",
+                                             "preferences")
+        self._test__multiple_equivalent_urls("preferences_1", "preferences_2",
+                                             "preferences")
+        self._test__multiple_equivalent_urls("preferences_2", "preferences_1",
+                                             "preferences")
+
+
+    def _test__multiple_non_equivalent_urls(self, url_id, non_equivalent_url_id,
+                                            settings_id):
+        """Multiple settings tabs are open, but they are _not_ equivalent."""
+
+        # Already on the correct tab
+        self._open_tabs([non_equivalent_url_id, url_id, 3], 1)
+        self._open_settings(settings_id)
+        self._check_tabs([non_equivalent_url_id, url_id, 3], 1)
+
+        # Switch to the correct tab (to the left of the current tab).
+        self._open_tabs([url_id, 2, non_equivalent_url_id], 1)
+        self._open_settings(settings_id)
+        self._check_tabs([url_id, 2, non_equivalent_url_id], 0)
+
+        # Switch to the correct tab (to the left of the current tab).
+        # The current tab is the non-equivalent tab.
+        self._open_tabs([url_id, non_equivalent_url_id, 3], 1)
+        self._open_settings(settings_id)
+        self._check_tabs([url_id, non_equivalent_url_id, 3], 0)
+
+    def test_preferences__with_other_settings_tabs(self):
+        self._test__multiple_non_equivalent_urls("preferences_1", "policies",
+                                                 "preferences")
+        self._test__multiple_non_equivalent_urls("preferences_2", "policies",
+                                                 "preferences")
+
+    ##########################
+    # Private Helper Methods #
+    ##########################
+
+    def _open_tabs(self, tabs, select_index=1):
+        self._close_all_tabs()
+
+        first_tab = True
+        for tab_id in tabs:
+            if not first_tab:
+                self.tabbar.open_tab().select()
+            url = URLS[tab_id][0]
+            with self.marionette.using_context("content"):
+                self.marionette.navigate(url)
+            if first_tab:
+                first_tab = False
+        self.tabbar.tabs[select_index].select()
+        self._check_tabs(tabs, select_index)
+
+    def _close_all_tabs(self):
+        self.tabbar.close_all_tabs(exceptions=[self.tabbar.tabs[0]])
+
+    def _check_tabs(self, tabs, expected_selected_index):
+        self.assertEqual(expected_selected_index, self.tabbar.selected_index)
+        expected_tab_urls = [URLS[tab_id] for tab_id in tabs]
+        tab_urls = [tab.location for tab in self.tabbar.tabs]
+        self.assertEqual(len(expected_tab_urls), len(expected_tab_urls))
+        for idx in range(len(expected_tab_urls)):
+            possible_tab_urls = expected_tab_urls[idx]
+            tab_url = tab_urls[idx]
+            self.assertIn(tab_url, possible_tab_urls)
+
+        self.tabbar.tabs[expected_selected_index].select()
+        self.assertEqual(expected_selected_index, self.tabbar.selected_index)
+
+    def _open_settings(self, button):
+        self.menu.open()
+        if button == "preferences":
+            self.menu.preferences_button.click()
+        elif button == "policies":
+            self.menu.manage_policies_button.click()
+        else:
+            self.fail()
diff --git a/tests/xpcshell/test_jsutils.js b/tests/xpcshell/test_jsutils.js
index 25169d9..658ff6f 100644
--- a/tests/xpcshell/test_jsutils.js
+++ b/tests/xpcshell/test_jsutils.js
@@ -4,6 +4,8 @@ function run_test() {
   "use strict";
 
   test_arrayIncludes();
+  test_leftRotateArray();
+  test_range();
 }
 
 function test_arrayIncludes() {
@@ -18,3 +20,26 @@ function test_arrayIncludes() {
   strictEqual(true, arrayIncludes([0], 0));
   strictEqual(false, arrayIncludes([0], "0"));
 }
+
+function test_leftRotateArray() {
+  "use strict";
+
+  let {leftRotateArray} = JSUtils;
+
+  deepEqual([1, 2, 3], leftRotateArray([1, 2, 3], 0));
+  deepEqual([2, 3, 1], leftRotateArray([1, 2, 3], 1));
+  deepEqual([3, 1, 2], leftRotateArray([1, 2, 3], 2));
+  deepEqual([1, 2, 3], leftRotateArray([1, 2, 3], 3));
+  deepEqual([2, 3, 1], leftRotateArray([1, 2, 3], 4));
+}
+
+function test_range() {
+  "use strict";
+
+  let {range} = JSUtils;
+
+  deepEqual([], range(0));
+  deepEqual([0, 1, 2, 3], range(4));
+  deepEqual([0, 1, 2, 3, 4, 5], range(6));
+  deepEqual([], range(-1));
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mozext/requestpolicy.git



More information about the Pkg-mozext-commits mailing list