[Pkg-mozext-commits] [requestpolicy] 246/257: [tst][ref] Marionette: Create "Redirection Utils"

David Prévot taffit at moszumanska.debian.org
Thu Jan 28 03:20:18 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 0bdfa8e7db11563a8df2ccfdd3fe9b42d8255837
Author: Martin Kimmerle <dev at 256k.de>
Date:   Mon Dec 28 22:16:43 2015 +0100

    [tst][ref] Marionette: Create "Redirection Utils"
    
    Create "Redirection Utilities" so that modifying
    and extending redirection unit tests gets easier.
    The core of the Redirection Utils is
    `for_each_possible_redirection_scenario()`.
    
    Other changes:
    
    * Some PHP pages have been added so that redirection
    pages with specific destination URLs can be
    created dynamically.
    
    * Test coverage has been increase because _all_
    origin --> destination  combinations can be run
    for _all_ redirection methods.
---
 tests/content/redirect-http-location-header.php    |   3 +-
 tests/content/redirect-http-refresh-header.php     |   4 +-
 .../content/redirect-js-document-location-auto.php |  16 ++
 .../content/redirect-js-document-location-link.php |  25 +++
 .../redirect-meta-tag-different-formatting.php     |  22 ++
 tests/content/redirect-meta-tag.php                |  20 ++
 .../tests/test_redirect_notification.py            |  13 +-
 tests/marionette/rp_ui_harness/utils/__init__.py   |   0
 .../marionette/rp_ui_harness/utils/redirections.py | 227 +++++++++++++++++++++
 .../tests/redirections/test_auto_redirect.py       | 123 +++++------
 .../tests/redirections/test_inline_redirect.py     |   6 +-
 .../tests/redirections/test_link_click_redirect.py |  83 +++-----
 .../test_link_click_redirect_in_new_tab.py         |  99 +++++----
 13 files changed, 459 insertions(+), 182 deletions(-)

diff --git a/tests/content/redirect-http-location-header.php b/tests/content/redirect-http-location-header.php
index b97e915..2b35f1e 100644
--- a/tests/content/redirect-http-location-header.php
+++ b/tests/content/redirect-http-location-header.php
@@ -1,3 +1,4 @@
 <?php
+$pre_path = isset($_GET['pre_path']) ? $_GET['pre_path'] : 'http://www.otherdomain.test/';
 $path = isset($_GET['path']) ? $_GET['path'] : '';
-header('Location: http://www.otherdomain.test/' . $path);
+header('Location: ' . $pre_path . $path);
diff --git a/tests/content/redirect-http-refresh-header.php b/tests/content/redirect-http-refresh-header.php
index e9e31f2..366453d 100644
--- a/tests/content/redirect-http-refresh-header.php
+++ b/tests/content/redirect-http-refresh-header.php
@@ -1,2 +1,4 @@
 <?php
-  header('Refresh: 0; url=http://www.otherdomain.test');
+$pre_path = isset($_GET['pre_path']) ? $_GET['pre_path'] : 'http://www.otherdomain.test/';
+$path = isset($_GET['path']) ? $_GET['path'] : '';
+header('Refresh: 0; url=' . $pre_path . $path);
diff --git a/tests/content/redirect-js-document-location-auto.php b/tests/content/redirect-js-document-location-auto.php
new file mode 100644
index 0000000..1485047
--- /dev/null
+++ b/tests/content/redirect-js-document-location-auto.php
@@ -0,0 +1,16 @@
+<?php
+$pre_path = isset($_GET['pre_path']) ? $_GET['pre_path'] : 'http://www.otherdomain.test/';
+$path = isset($_GET['path']) ? $_GET['path'] : '';
+?>
+<!doctype html>
+<html>
+<head>
+  <meta charset="utf-8" />
+</head>
+<body onload="document.location = '<?=$pre_path?><?=$path?>'">
+  <p>
+    This page wants to redirect to
+    <a href="<?=$pre_path?><?=$path?>">this URL</a>.
+  <p>
+</body>
+</html>
diff --git a/tests/content/redirect-js-document-location-link.php b/tests/content/redirect-js-document-location-link.php
new file mode 100644
index 0000000..340a6c4
--- /dev/null
+++ b/tests/content/redirect-js-document-location-link.php
@@ -0,0 +1,25 @@
+<?php
+$pre_path = isset($_GET['pre_path']) ? $_GET['pre_path'] : 'http://www.otherdomain.test/';
+$path = isset($_GET['path']) ? $_GET['path'] : '';
+$link_href = "javascript:document.location = '" . $pre_path . $path . "'";
+$link_href_encoded = htmlspecialchars($link_href);
+?>
+<!doctype html>
+<html>
+<head>
+  <meta charset="utf-8" />
+</head>
+<body>
+
+<p>
+  The following link will redirect to another URL.
+</p>
+
+<p>
+  <a href="<?=$link_href?>">
+    link with href: <tt><?=$link_href_encoded?></tt>
+  </a>
+<p>
+
+</body>
+</html>
diff --git a/tests/content/redirect-meta-tag-different-formatting.php b/tests/content/redirect-meta-tag-different-formatting.php
new file mode 100644
index 0000000..2327319
--- /dev/null
+++ b/tests/content/redirect-meta-tag-different-formatting.php
@@ -0,0 +1,22 @@
+<?php
+$pre_path = isset($_GET['pre_path']) ? $_GET['pre_path'] : 'http://www.otherdomain.test/';
+$path = isset($_GET['path']) ? $_GET['path'] : '';
+$delay = isset($_GET['delay']) ? $_GET['delay'] : '0';
+?>
+<!doctype html>
+<html>
+  <head>
+    <meta charset="utf-8" />
+    <meta http-equiv="refresh"
+          content=" <?=$delay?>   ;  url = <?=$pre_path?><?=$path?>     " />
+  </head>
+  <body>
+
+    <p>
+      This page redirects to a different page using the
+      <tt><meta></tt> tag with different formatting.
+      The delay is <?=$delay?> seconds.
+    </p>
+
+  </body>
+</html>
diff --git a/tests/content/redirect-meta-tag.php b/tests/content/redirect-meta-tag.php
new file mode 100644
index 0000000..8abd9f2
--- /dev/null
+++ b/tests/content/redirect-meta-tag.php
@@ -0,0 +1,20 @@
+<?php
+$pre_path = isset($_GET['pre_path']) ? $_GET['pre_path'] : 'http://www.otherdomain.test/';
+$path = isset($_GET['path']) ? $_GET['path'] : '';
+$delay = isset($_GET['delay']) ? $_GET['delay'] : '0';
+?>
+<!doctype html>
+<html>
+  <head>
+    <meta charset="utf-8" />
+    <meta http-equiv="refresh" content="<?=$delay?>;url=<?=$pre_path?><?=$path?>" />
+  </head>
+  <body>
+
+    <p>
+      This page redirects to a different page using the
+      <tt><meta></tt> tag. The delay is <?=$delay?> seconds.
+    </p>
+
+  </body>
+</html>
diff --git a/tests/marionette/rp_puppeteer/tests/test_redirect_notification.py b/tests/marionette/rp_puppeteer/tests/test_redirect_notification.py
index a3d1709..f36acc3 100644
--- a/tests/marionette/rp_puppeteer/tests/test_redirect_notification.py
+++ b/tests/marionette/rp_puppeteer/tests/test_redirect_notification.py
@@ -3,12 +3,11 @@
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 from rp_ui_harness import RequestPolicyTestCase
+from rp_ui_harness.utils import redirections
 
 
-PRE_PATH = "http://www.maindomain.test/"
-PAGE_WITH_REDIRECT = PRE_PATH + "redirect-meta-tag-01-immediate.html"
-PAGE_WITH_REDIRECT__DEST = ("http://www.otherdomain.test/destination.html?"
-                            "redirect-meta-tag-01%20redirected%20here.")
+(PAGE_WITH_REDIRECT,
+ PAGE_WITH_REDIRECT__DEST) = redirections.get_auto_redirection_uri("<meta>")
 PREF_DEFAULT_ALLOW = "extensions.requestpolicy.defaultPolicy.allow"
 
 
@@ -28,8 +27,7 @@ class TestRedirectNotification(RequestPolicyTestCase):
 
     def test_allow(self):
         with self.marionette.using_context("content"):
-            # FIXME: Remove the "?..." part when #726 is fixed.
-            self.marionette.navigate(PAGE_WITH_REDIRECT + "?test_allow")
+            self.marionette.navigate(PAGE_WITH_REDIRECT)
 
         self.assertTrue(self.redir.is_shown())
         self.redir.allow()
@@ -41,8 +39,7 @@ class TestRedirectNotification(RequestPolicyTestCase):
 
     def test_close(self):
         with self.marionette.using_context("content"):
-            # FIXME: Remove the "?..." part when #726 is fixed.
-            self.marionette.navigate(PAGE_WITH_REDIRECT + "?test_close")
+            self.marionette.navigate(PAGE_WITH_REDIRECT)
 
         self.assertTrue(self.redir.is_shown())
         self.redir.close()
diff --git a/tests/marionette/rp_ui_harness/utils/__init__.py b/tests/marionette/rp_ui_harness/utils/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/tests/marionette/rp_ui_harness/utils/redirections.py b/tests/marionette/rp_ui_harness/utils/redirections.py
new file mode 100644
index 0000000..9d2ff04
--- /dev/null
+++ b/tests/marionette/rp_ui_harness/utils/redirections.py
@@ -0,0 +1,227 @@
+# 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 marionette_driver.wait import Wait
+from marionette_driver.errors import TimeoutException
+import urllib
+import time
+
+
+# delay on <meta> refresh pages
+DELAY = 2
+
+
+def _get_random_string():
+    # not a random string, but something unique
+    return str(str(time.clock()))
+
+def get_auto_redirection_uri(redirection_method,
+        append_random_querystring=True,
+        origin_pre_path="http://www.maindomain.test/",
+        dest_pre_path="http://www.otherdomain.test/",
+        dest_path="",
+        expected_dest_pre_path=None):
+    additional_parameters = ""
+    if redirection_method == "http:location":
+        filename = "redirect-http-location-header.php"
+    elif redirection_method == "http:refresh":
+        filename = "redirect-http-refresh-header.php"
+    elif redirection_method == "<meta>":
+        filename = "redirect-meta-tag.php"
+    elif redirection_method == "<meta> delayed":
+        filename = "redirect-meta-tag.php"
+        additional_parameters = "&delay=" + str(DELAY)
+    elif redirection_method == "<meta> 2":
+        filename = "redirect-meta-tag-different-formatting.php"
+    elif redirection_method == "<meta> 2 delayed":
+        filename = "redirect-meta-tag-different-formatting.php"
+        additional_parameters = "&delay=" + str(DELAY)
+    elif redirection_method == "js:document.location:<body> onload":
+        filename = "redirect-js-document-location-auto.php"
+    else:
+        raise ValueError("Unknown redirection method: \"{}\""
+                         .format(redirection_method))
+
+    origin_uri = (origin_pre_path + filename +
+                  "?pre_path=" + urllib.quote(dest_pre_path) +
+                  "&path=" + urllib.quote(dest_path) +
+                  additional_parameters)
+    if append_random_querystring:
+        origin_uri += "&random=" + _get_random_string()
+
+    if expected_dest_pre_path is None:
+        expected_dest_pre_path = (dest_pre_path if dest_pre_path != ""
+                                  else origin_pre_path)
+    dest_uri = expected_dest_pre_path + dest_path
+
+    return (origin_uri, dest_uri)
+
+def get_link_redirection_uri(redirection_method,
+        append_random_querystring=True,
+        origin_pre_path="http://www.maindomain.test/",
+        dest_pre_path="http://www.otherdomain.test/",  # set to "" to get a relative url
+        dest_path="",
+        expected_dest_pre_path=None):
+    if redirection_method == "js:document.location:<a> href":
+        filename = "redirect-js-document-location-link.php"
+
+        linkpage_uri = (origin_pre_path + filename +
+                    "?pre_path=" + urllib.quote(dest_pre_path) +
+                    "&path=" + urllib.quote(dest_path))
+
+        intermediate_uri = None
+
+        if expected_dest_pre_path is None:
+            expected_dest_pre_path = (dest_pre_path if dest_pre_path != ""
+                                      else origin_pre_path)
+        dest_uri = expected_dest_pre_path + dest_path
+    else:
+        (intermediate_uri, dest_uri) = get_auto_redirection_uri(
+            redirection_method,
+            append_random_querystring=append_random_querystring,
+            origin_pre_path=origin_pre_path,
+            dest_pre_path=dest_pre_path,
+            dest_path=dest_path,
+            expected_dest_pre_path=expected_dest_pre_path)
+        linkpage_uri = (origin_pre_path + "link.html?" +
+                        urllib.quote(intermediate_uri))
+    return (linkpage_uri, intermediate_uri, dest_uri)
+
+def get_info_for_redirection_method(redir_method):
+    info = {"redirection_method": redir_method}
+    if redir_method in ["<meta> delayed", "<meta> 2 delayed"]:
+        info["delay"] = DELAY
+    else:
+        info["delay"] = 0
+    return info
+
+def for_each_auto_redirection_uri(callback, base_info, *args, **kwargs):
+    def call(redir_method):
+        info = base_info.copy()
+        info.update(get_info_for_redirection_method(redir_method))
+        uris = get_auto_redirection_uri(redir_method, *args, **kwargs)
+        callback(uris, info=info)
+    call("http:location")
+    call("http:refresh")
+    call("<meta>")
+    call("<meta> delayed")
+    call("<meta> 2")
+    call("<meta> 2 delayed")
+    call("js:document.location:<body> onload")
+
+def for_each_link_redirection_uri(callback, base_info, *args, **kwargs):
+    def call(redir_method):
+        info = base_info.copy()
+        info.update(get_info_for_redirection_method(redir_method))
+        uris = get_link_redirection_uri(redir_method, *args, **kwargs)
+        callback(uris, info=info)
+    call("http:location")
+    call("http:refresh")
+    call("<meta>")
+    call("<meta> delayed")
+    call("<meta> 2")
+    call("<meta> 2 delayed")
+    call("js:document.location:<body> onload")
+    call("js:document.location:<a> href")
+
+def for_each_possible_redirection_scenario(callback, uri_type):
+    """Call a function for many (or all) redirection scenarios.
+
+    The callback gets the following data:
+      * uris: A tuple of either two or three values, depending on
+              the `uri_type`. It's the return value of
+              `get_auto_redirection_uri` or `get_link_redirection_uri`.
+      * info: A directory containing additional information:
+              * is_same_host (boolean)
+              * delay (integer; normally zero)
+              * redirection_method (string)
+    """
+
+    if uri_type == "auto":
+        for_each = for_each_auto_redirection_uri
+    elif uri_type == "link":
+        for_each = for_each_link_redirection_uri
+    else:
+        raise ValueError("Unknown redirection URI type: \"{}\""
+                         .format(uri_type))
+
+    def callback_wrapper(uris, info):
+        try:
+            callback(uris, info=info)
+        except:
+            print "info: " + str(info)
+            print "uris: " + str(uris)
+            raise
+
+    #---------------------------------------------------------------------------
+    # cross-site redirections
+    #---------------------------------------------------------------------------
+
+    # usual cross-site redirection
+    for_each(callback_wrapper,
+        base_info={"is_same_host": False, "is_relative_dest": False},
+        origin_pre_path="http://www.maindomain.test/",
+        dest_pre_path="http://www.otherdomain.test/",
+        dest_path="")
+
+    # destination relative to "http" scheme
+    for_each(callback_wrapper,
+        base_info={"is_same_host": False, "is_relative_dest": True},
+        origin_pre_path="http://www.maindomain.test/",
+        dest_pre_path="//www.otherdomain.test/",
+        dest_path="",
+        expected_dest_pre_path="http://www.otherdomain.test/")
+
+    # redirection to PNG image (test against #351)
+    for_each(callback_wrapper,
+        base_info={"is_same_host": False, "is_relative_dest": False},
+        origin_pre_path="http://www.maindomain.test/",
+        dest_pre_path="http://www.otherdomain.test/",
+        dest_path="subdirectory/flag-gray.png")
+
+    #---------------------------------------------------------------------------
+    # same-site redirections
+    #---------------------------------------------------------------------------
+
+    # destination relative to the host
+    for_each(callback_wrapper,
+        base_info={"is_same_host": True, "is_relative_dest": True},
+        origin_pre_path="http://www.maindomain.test/",
+        dest_pre_path="",
+        dest_path="subdirectory/")
+
+    # destination relative to the host, with slash
+    for_each(callback_wrapper,
+        base_info={"is_same_host": True, "is_relative_dest": True},
+        origin_pre_path="http://www.maindomain.test/",
+        dest_pre_path="/",
+        dest_path="subdirectory/",
+        expected_dest_pre_path="http://www.maindomain.test/")
+
+    # destination host and protocol specified
+    for_each(callback_wrapper,
+        base_info={"is_same_host": True, "is_relative_dest": False},
+        origin_pre_path="http://www.maindomain.test/",
+        dest_pre_path="http://www.maindomain.test/",
+        dest_path="")
+
+    # destination relative to "http" scheme, but same host
+    for_each(callback_wrapper,
+        base_info={"is_same_host": True, "is_relative_dest": True},
+        origin_pre_path="http://www.maindomain.test/",
+        dest_pre_path="//www.maindomain.test/",
+        dest_path="",
+        expected_dest_pre_path="http://www.maindomain.test/")
+
+def assert_url_does_not_load(testcase, url, expected_delay):
+    timeout = expected_delay + 0.25
+    testcase.assertRaises(TimeoutException, wait_until_url_load, testcase, url,
+                          timeout=timeout)
+
+def wait_until_url_load(testcase, url, message="", timeout=(DELAY + 0.25)):
+    with testcase.marionette.using_context("content"):
+        (
+            Wait(testcase.marionette, timeout=timeout)
+            .until(lambda m: m.get_url() == url, message=message)
+        )
diff --git a/tests/marionette/tests/redirections/test_auto_redirect.py b/tests/marionette/tests/redirections/test_auto_redirect.py
index 6f61421..f38086a 100644
--- a/tests/marionette/tests/redirections/test_auto_redirect.py
+++ b/tests/marionette/tests/redirections/test_auto_redirect.py
@@ -5,6 +5,7 @@
 from rp_ui_harness.testcases import RequestPolicyTestCase
 from marionette_driver.errors import TimeoutException
 from marionette import SkipTest
+from rp_ui_harness.utils import redirections
 
 
 PREF_DEFAULT_ALLOW = "extensions.requestpolicy.defaultPolicy.allow"
@@ -33,119 +34,93 @@ class TestAutoRedirect(RequestPolicyTestCase):
         hidden when it's not expected.
         """
 
-        def test_no_appear(path):
-            test_url = "http://www.maindomain.test/" + path
-            # FIXME: Remove the following line when #726 is fixed.
-            test_url += "?test_redirect_notification_appears_or_not"
-
+        def test_no_appear((test_url, dest_url), info):
             with self.marionette.using_context("content"):
                 self.marionette.navigate(test_url)
 
-            self.assertNotEqual(self.marionette.get_url(), test_url,
-                                "The URL in the urlbar has changed.")
+            # The page might redirect with a delay. There shouldn't be the
+            # notification neither before nor after the redirection.
+            self.assertFalse(self.redir.is_shown(),
+                             "There's no redirect notification.")
+            redirections.wait_until_url_load(self, dest_url,
+                                             "The location has changed.")
             self.assertFalse(self.redir.is_shown(),
                              "There's no redirect notification.")
 
-        def test_appear(path, navigate_args={}):
-            test_url = "http://www.maindomain.test/" + path
-            # FIXME: Remove the following line when #726 is fixed.
-            test_url += "?test_redirect_notification_appears_or_not"
-
-            initial_uri = self.marionette.get_url()
-
-            self._navigate_expecting_r21n(test_url, **navigate_args)
+        def test_appear((test_url, dest_url), info):
+            self._load_about_blank()
+            self._navigate_expecting_r21n(test_url)
 
             self.assertTrue(self.redir.is_shown(),
                             "The redirect notification has been displayed.")
-            self.assertIn(self.marionette.get_url(), [test_url, initial_uri],
-                          "The URL in the urlbar did not change.")
+            redirections.assert_url_does_not_load(self, dest_url,
+                expected_delay=info["delay"])
 
             self.redir.close()
 
-        test_appear("redirect-http-location-header.php",
-                    navigate_args={"on_e10s_use_locationbar": True})
-        test_appear("redirect-http-refresh-header.php",
-                    navigate_args={"on_e10s_use_locationbar": True})
-
-        test_appear("redirect-js-document-location-auto.html")
-        test_appear("redirect-meta-tag-01-immediate.html")
-        test_appear("redirect-meta-tag-02-delayed.html")
-        test_appear("redirect-meta-tag-03-multiple.html")
-        test_appear("redirect-meta-tag-08.html")
+        def test(uris, info):
+            if info["is_same_host"]:
+                test_no_appear(uris, info)
+            else:
+                test_appear(uris, info)
 
-        test_no_appear("redirect-meta-tag-04-relative-without-slash.html")
-        test_no_appear("redirect-meta-tag-05-relative-with-slash.html")
-        test_no_appear("redirect-meta-tag-06-different-formatting.html")
-        test_no_appear("redirect-meta-tag-07-different-formatting-delayed.html")
-        test_no_appear("redirect-meta-tag-09-relative.html")
+        redirections.for_each_possible_redirection_scenario(test, "auto")
 
     def test_allow(self):
+        def test((test_url, dest_uri), info):
+            if info["is_same_host"]:
+                # the notification won't appear
+                return
 
-        def test(path, dest_uri, navigate_args={}):
-            test_url = "http://www.maindomain.test/" + path
-            # FIXME: Remove the following line when #726 is fixed.
-            test_url += "?test_allow"
-
-            self._navigate_expecting_r21n(test_url, **navigate_args)
+            self._navigate_expecting_r21n(test_url)
             self.assertTrue(self.redir.is_shown())
             self.redir.allow()
             self.assertFalse(self.redir.is_shown())
             with self.marionette.using_context("content"):
                 self.assertEqual(self.marionette.get_url(), dest_uri)
 
-        # Header redirection
-        test("redirect-http-location-header.php",
-             "http://www.otherdomain.test/",
-             navigate_args={"on_e10s_use_locationbar": True})
-        # JavaScript redirection
-        test("redirect-js-document-location-auto.html",
-             "http://www.otherdomain.test/")
-        # <meta> redirection
-        test("redirect-meta-tag-01-immediate.html",
-             ("http://www.otherdomain.test/destination.html?"
-              "redirect-meta-tag-01%20redirected%20here."))
+        redirections.for_each_possible_redirection_scenario(test, "auto")
 
     def test_r21n_appears_again_after_allow(self):
         raise SkipTest("Skipping due to issue #726.")
 
-        def test(path, navigate_args={}):
-            test_url = "http://www.maindomain.test/" + path
+        def test((test_url, dest_url), info):
+            if info["is_same_host"]:
+                # the notification won't appear
+                return
 
-            self._navigate_expecting_r21n(test_url, **navigate_args)
+            self._load_about_blank()
+            self._navigate_expecting_r21n(test_url)
             self.assertTrue(self.redir.is_shown())
+            redirections.assert_url_does_not_load(self, dest_url,
+                expected_delay=info["delay"])
             self.redir.allow()
 
-            self._navigate_expecting_r21n(test_url, **navigate_args)
+            self._load_about_blank()
+            self._navigate_expecting_r21n(test_url)
             self.assertTrue(self.redir.is_shown())
+            redirections.assert_url_does_not_load(self, dest_url,
+                expected_delay=info["delay"])
             self.redir.close()
 
-        # Header redirection
-        test("redirect-http-location-header.php",
-             navigate_args={"on_e10s_use_locationbar": True})
-        # JavaScript redirection
-        test("redirect-js-document-location-auto.html")
-        # <meta> redirection
-        test("redirect-meta-tag-01-immediate.html")
+        redirections.for_each_possible_redirection_scenario(test, "auto")
 
     ##########################
     # Private Helper Methods #
     ##########################
 
-    def _navigate_expecting_r21n(self, url, on_e10s_use_locationbar=False):
+    def _navigate_expecting_r21n(self, url):
         """Navigate to a URL, catching all expected exceptions."""
 
         if self.browser_info.e10s_enabled:
             # On E10s there's no TimeoutException raised.
 
-            if on_e10s_use_locationbar:
-                # In some cases `navigate()` raises an IOError. The workaround
-                # is to use the location-bar.
-                # For details see Mozilla Bug 1219969 / Issue #727.
-                self.browser.navbar.locationbar.load_url(url)
-                self.tabs.wait_until_loaded(self.browser.tabbar.tabs[0])
-            else:
-                with self.marionette.using_context("content"):
-                    self.marionette.navigate(url)
+            # In case of HTTP header redirections `marionette.navigate()`
+            # raises an IOError. The workaround is to use the location-bar
+            # instead.
+            # For details see Mozilla Bug 1219969 / Issue #727.
+            self.browser.navbar.locationbar.load_url(url)
+            self.tabs.wait_until_loaded(self.browser.tabbar.tabs[0])
         else:
             with self.marionette.using_context("content"):
                 # On non-E10s, expect a TimeoutException, because when
@@ -159,3 +134,11 @@ class TestAutoRedirect(RequestPolicyTestCase):
                                   url)
 
                 self.marionette.timeouts("page load", 20000)
+
+    def _get_url(self):
+        with self.marionette.using_context("content"):
+            return self.marionette.get_url()
+
+    def _load_about_blank(self):
+        with self.marionette.using_context("content"):
+                self.marionette.navigate("about:blank")
diff --git a/tests/marionette/tests/redirections/test_inline_redirect.py b/tests/marionette/tests/redirections/test_inline_redirect.py
index 74a0cf4..0d1afe0 100644
--- a/tests/marionette/tests/redirections/test_inline_redirect.py
+++ b/tests/marionette/tests/redirections/test_inline_redirect.py
@@ -3,7 +3,6 @@
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 from rp_ui_harness.testcases import RequestPolicyTestCase
-import time
 
 
 PREF_DEFAULT_ALLOW = "extensions.requestpolicy.defaultPolicy.allow"
@@ -22,6 +21,10 @@ class TestInlineRedirect(RequestPolicyTestCase):
         finally:
             super(TestInlineRedirect, self).tearDown()
 
+    ################
+    # Test Methods #
+    ################
+
     def test_redirect_notification_doesnt_appear(self):
         """This test ensures that the redirect notification is _not_ shown
         when the URL of an inline element, such as <img>, causes a redirection.
@@ -38,4 +41,3 @@ class TestInlineRedirect(RequestPolicyTestCase):
 
         test_no_appear("redirect-inline-image.html")
         test_no_appear("redirect-iframe.html")
-
diff --git a/tests/marionette/tests/redirections/test_link_click_redirect.py b/tests/marionette/tests/redirections/test_link_click_redirect.py
index c7de255..5fbc389 100644
--- a/tests/marionette/tests/redirections/test_link_click_redirect.py
+++ b/tests/marionette/tests/redirections/test_link_click_redirect.py
@@ -4,7 +4,7 @@
 
 from rp_ui_harness.testcases import RequestPolicyTestCase
 from marionette import SkipTest
-import random
+from rp_ui_harness.utils import redirections
 
 
 PREF_DEFAULT_ALLOW = "extensions.requestpolicy.defaultPolicy.allow"
@@ -25,22 +25,12 @@ class TestLinkClickRedirect(RequestPolicyTestCase):
     def test_r21n_appears_or_not__no_rules(self):
         self.prefs.set_pref(PREF_DEFAULT_ALLOW, False)
 
-        self._test_appear(self._get_url("redirect-js-document-location-link.html",
-                                        generate_page_with_link=False))
-
-        self._test_appear(self._get_url("redirect-http-location-header.php"))
-        self._test_appear(self._get_url("redirect-http-refresh-header.php"))
-        self._test_appear(self._get_url("redirect-js-document-location-auto.html"))
-        self._test_appear(self._get_url("redirect-meta-tag-01-immediate.html"))
-        self._test_appear(self._get_url("redirect-meta-tag-02-delayed.html"))
-        self._test_appear(self._get_url("redirect-meta-tag-03-multiple.html"))
-        self._test_appear(self._get_url("redirect-meta-tag-08.html"))
-
-        self._test_no_appear(self._get_url("redirect-meta-tag-04-relative-without-slash.html"))
-        self._test_no_appear(self._get_url("redirect-meta-tag-05-relative-with-slash.html"))
-        self._test_no_appear(self._get_url("redirect-meta-tag-06-different-formatting.html"))
-        self._test_no_appear(self._get_url("redirect-meta-tag-07-different-formatting-delayed.html"))
-        self._test_no_appear(self._get_url("redirect-meta-tag-09-relative.html"))
+        def test((test_url, _, dest_url), info):
+            if info["is_same_host"]:
+                self._test_no_appear(test_url, dest_url, info)
+            else:
+                self._test_appear(test_url, dest_url, info)
+        redirections.for_each_possible_redirection_scenario(test, "link")
 
     def test_r21n_no_appears__conflicting_rules(self):
         self.prefs.set_pref(PREF_DEFAULT_ALLOW, True)
@@ -51,27 +41,16 @@ class TestLinkClickRedirect(RequestPolicyTestCase):
         self.rules.create_rule({"d": {"h": "*.otherdomain.test"}},
                                allow=False).add()
 
-        self._test_no_appear(self._get_url("redirect-js-document-location-link.html",
-                                           generate_page_with_link=False))
-        self._test_no_appear(self._get_url("redirect-http-location-header.php"))
-        self._test_no_appear(self._get_url("redirect-http-refresh-header.php"))
-        self._test_no_appear(self._get_url("redirect-js-document-location-auto.html"))
-        self._test_no_appear(self._get_url("redirect-meta-tag-01-immediate.html"))
-        self._test_no_appear(self._get_url("redirect-meta-tag-02-delayed.html"))
-        self._test_no_appear(self._get_url("redirect-meta-tag-03-multiple.html"))
-        self._test_no_appear(self._get_url("redirect-meta-tag-04-relative-without-slash.html"))
-        self._test_no_appear(self._get_url("redirect-meta-tag-05-relative-with-slash.html"))
-        self._test_no_appear(self._get_url("redirect-meta-tag-06-different-formatting.html"))
-        self._test_no_appear(self._get_url("redirect-meta-tag-07-different-formatting-delayed.html"))
-        self._test_no_appear(self._get_url("redirect-meta-tag-08.html"))
-        self._test_no_appear(self._get_url("redirect-meta-tag-09-relative.html"))
+        def test((test_url, _, dest_url), info):
+            self._test_no_appear(test_url, dest_url, info)
+        redirections.for_each_possible_redirection_scenario(test, "link")
 
         self.rules.remove_all()
 
     def test_r21n_appear_then_no_appear(self):
         raise SkipTest("FIXME")
-        # When fixed, remove the `append_random_querystring` option
-        # of `_get_url()`.
+        # When fixed, the `append_random_querystring` option in
+        # some redirections-utils functions can be removed.
 
         self.prefs.set_pref(PREF_DEFAULT_ALLOW, False)
 
@@ -79,39 +58,37 @@ class TestLinkClickRedirect(RequestPolicyTestCase):
                                        "d": {"h": "*.otherdomain.test"}},
                                       allow=True)
 
-        def test(test_filename):
-            test_url = self._get_url(test_filename,
-                                     append_random_querystring=False)
-            self._test_appear(test_url)
+        def test((test_url, _, dest_url), info):
+            if not info["is_same_host"]:
+                return
+
+            self._test_appear(test_url, dest_url, info)
             rule.add()
-            self._test_no_appear(test_url)
+            self._test_no_appear(test_url, dest_url, info)
             rule.remove()
 
-        test("redirect-http-location-header.php")
-        test("redirect-http-refresh-header.php")
-        test("redirect-js-document-location-auto.html")
-        test("redirect-meta-tag-01-immediate.html")
-        test("redirect-meta-tag-02-delayed.html")
-        test("redirect-meta-tag-03-multiple.html")
-        test("redirect-meta-tag-08.html")
+        redirections.for_each_possible_redirection_scenario(test, "link")
 
     ##########################
     # Private Helper Methods #
     ##########################
 
-    def _test_no_appear(self, test_url):
+    def _test_no_appear(self, test_url, dest_url, info):
         self._open_page_and_click_on_first_link(test_url)
 
-        self.assertNotEqual(self.marionette.get_url(), test_url,
-                            "The URL in the urlbar has changed.")
+        self.assertFalse(self.redir.is_shown(),
+                         "There's no redirect notification.")
+        redirections.wait_until_url_load(self, dest_url)
         self.assertFalse(self.redir.is_shown(),
                          "There's no redirect notification.")
 
-    def _test_appear(self, test_url):
+    def _test_appear(self, test_url, dest_url, info):
         self._open_page_and_click_on_first_link(test_url)
 
         self.assertTrue(self.redir.is_shown(),
                         "The redirect notification has been displayed.")
+        redirections.assert_url_does_not_load(self, dest_url,
+            expected_delay=info["delay"])
 
         self.redir.close()
 
@@ -120,11 +97,3 @@ class TestLinkClickRedirect(RequestPolicyTestCase):
             self.marionette.navigate(test_url)
             link = self.marionette.find_element("tag name", "a")
             link.click()
-
-    def _get_url(self, path, generate_page_with_link=True,
-                 append_random_querystring=True):
-        if generate_page_with_link:
-            path = "link.html?" + path
-            if append_random_querystring:
-                path = path + "?" + str(random.randint(1, 100))
-        return "http://www.maindomain.test/" + path
diff --git a/tests/marionette/tests/redirections/test_link_click_redirect_in_new_tab.py b/tests/marionette/tests/redirections/test_link_click_redirect_in_new_tab.py
index d6b7348..c25528b 100644
--- a/tests/marionette/tests/redirections/test_link_click_redirect_in_new_tab.py
+++ b/tests/marionette/tests/redirections/test_link_click_redirect_in_new_tab.py
@@ -6,6 +6,7 @@ from rp_ui_harness.testcases import RequestPolicyTestCase
 from marionette_driver.marionette import Actions
 from rp_puppeteer.errors import ElementNotDisplayedException
 from contextlib import contextmanager
+from rp_ui_harness.utils import redirections
 
 
 PREF_DEFAULT_ALLOW = "extensions.requestpolicy.defaultPolicy.allow"
@@ -24,20 +25,23 @@ class TestLinkClickRedirectInNewTab(RequestPolicyTestCase):
         finally:
             super(TestLinkClickRedirectInNewTab, self).tearDown()
 
+    ################
+    # Test Methods #
+    ################
+
     def test_redirect_notification_appears_or_not(self):
         tabbar = self.browser.tabbar
 
-        def test_no_appear(test_url, *args):
+        def test_no_appear(test_url, dest_url, info, *args):
             open_page_and_open_first_link_in_new_tab(test_url, *args)
 
             # Select the new tab
             tabbar.tabs[1].select()
 
-            # FIXME: Find a better way to ensures that the part of RP which
-            #        is responsible for showing the panel _really_ has
-            #        finished.
-            self.assertNotEqual(self.marionette.get_url(), test_url,
-                                "The URL in the urlbar has changed.")
+            self.assertFalse(self.redir.is_shown(),
+                             "There's no redirect notification in the "
+                             "destination tab.")
+            redirections.wait_until_url_load(self, dest_url)
             self.assertFalse(self.redir.is_shown(),
                              "There's no redirect notification in the "
                              "destination tab.")
@@ -49,7 +53,7 @@ class TestLinkClickRedirectInNewTab(RequestPolicyTestCase):
                              "There's no redirect notification in the "
                              "origin tab.")
 
-        def test_appear(test_url, *args):
+        def test_appear(test_url, dest_url, info, *args):
             open_page_and_open_first_link_in_new_tab(test_url, *args)
 
             # Select the new tab
@@ -58,6 +62,8 @@ class TestLinkClickRedirectInNewTab(RequestPolicyTestCase):
             self.assertTrue(self.redir.is_shown(),
                             "The redirect notification has been displayed "
                             "in the destination tab.")
+            redirections.assert_url_does_not_load(self, dest_url,
+                expected_delay=info["delay"])
 
             # Close the new tab.
             tabbar.close_tab()
@@ -83,51 +89,58 @@ class TestLinkClickRedirectInNewTab(RequestPolicyTestCase):
                 #       tabBrowser.openTab({method: "contextMenu", target: link});
                 #       ```
 
-
         def expand_url(path, option="page with link"):
             if option == "page with link":
                 path = "link.html?" + path
             return "http://www.maindomain.test/" + path
 
-        @contextmanager
-        def assert_raises_if(exc_class, condition):
-            """Wrap into `assertRaises()` if the condition evaluates to true."""
-
-            if condition:
-                with self.assertRaises(exc_class):
-                    yield
-            else:
-                yield
-
         def test_variant(*args):
-            # FIXME: Issue #725;  This test fails with E10s enabled.
-            #        When FxPuppeteer's `TabBar.get_handle_for_tab()` is
-            #        executed for the new tab with the test URL, the
-            #        `contentWindowAsCPOW` either is `null` or does not
-            #        have a `QueryInterface()` function.
-            if not self.browser_info.e10s_enabled:
-                with assert_raises_if(ElementNotDisplayedException,
-                                      args[0] == "contextMenu"):
+            def test(test_url, dest_url, info):
+                if info["redirection_method"] == "js:document.location:<a> href":
+                    # If the link URL is
+                    #     javascript:document.location = 'http://www.example.com/'
+                    # there should _always_ be a notification, regardless
+                    # of where that came from.
+                    test_appear(test_url, dest_url, info, *args)
+                elif info["is_same_host"]:
+                    test_no_appear(test_url, dest_url, info, *args)
+                else:
+                    test_appear(test_url, dest_url, info, *args)
+
+            def maybe_test((test_url, _, dest_url), info):
+                if info["redirection_method"] == "js:document.location:<a> href":
+                    if info["is_relative_dest"]:
+                        # Examplary relative href:
+                        #     javascript:document.location = '/index.html'
+                        # This works for a left-click, but not for
+                        # "open in new tab". In a new tab, an absolute URI
+                        # is needed.
+                        return
+
+                    # FIXME: Issue #725;  This test fails with E10s enabled.
+                    #        When FxPuppeteer's `TabBar.get_handle_for_tab()` is
+                    #        executed for the new tab with the test URL, the
+                    #        `contentWindowAsCPOW` either is `null` or does not
+                    #        have a `QueryInterface()` function.
+                    if self.browser_info.e10s_enabled:
+                        return
+
                     # The "Open Link in New Tab" context menu entry is not
                     # available for <a> elements with such hrefs containing
                     # JavaScript code.
-                    test_appear(expand_url("redirect-js-document-location-link.html",
-                                           option="no link creation"), *args)
-
-            test_appear(expand_url("redirect-http-location-header.php"), *args)
-            test_appear(expand_url("redirect-http-refresh-header.php"), *args)
-            test_appear(expand_url("redirect-js-document-location-auto.html"), *args)
-            test_appear(expand_url("redirect-meta-tag-01-immediate.html"), *args)
-            test_appear(expand_url("redirect-meta-tag-02-delayed.html"), *args)
-            test_appear(expand_url("redirect-meta-tag-03-multiple.html"), *args)
-            test_appear(expand_url("redirect-meta-tag-08.html"), *args)
-
-            test_no_appear(expand_url("redirect-meta-tag-04-relative-without-slash.html"), *args)
-            test_no_appear(expand_url("redirect-meta-tag-05-relative-with-slash.html"), *args)
-            test_no_appear(expand_url("redirect-meta-tag-06-different-formatting.html"), *args)
-            test_no_appear(expand_url("redirect-meta-tag-07-different-formatting-delayed.html"), *args)
-            test_no_appear(expand_url("redirect-meta-tag-09-relative.html"), *args)
-
+                    if args[0] == "contextMenu":
+                        with self.assertRaises(ElementNotDisplayedException):
+                            test(test_url, dest_url, info)
+                        return
+
+                test(test_url, dest_url, info)
+
+            try:
+                redirections.for_each_possible_redirection_scenario(maybe_test,
+                                                                    "link")
+            except:
+                print "test variant: " + str(args[0])
+                raise
 
         test_variant("middleClick")
         test_variant("contextMenu")

-- 
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