Bug 1330323 - Fix test failures in test_execute_script for Fennec. r=ato, a=test-only
authorHenrik Skupin <mail@hskupin.info>
Wed, 11 Jan 2017 16:43:39 +0100
changeset 353558 36f85907a16e97144c904396c79511d458557227
parent 353557 e06ca5fc05b8d08cf13e0645eec1d746f73055a2
child 353559 58c9ae2f0faf7e459ca74d8563c565ae5ef28c75
push id6795
push userjlund@mozilla.com
push dateMon, 23 Jan 2017 14:19:46 +0000
treeherdermozilla-esr52@76101b503191 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersato, test-only
bugs1330323
milestone52.0a2
Bug 1330323 - Fix test failures in test_execute_script for Fennec. r=ato, a=test-only A bit of refactoring has to be done to make the tests all working in Fennec. Those which absolutely cannot be run, have been marked as skipped. Further some other code has been found which wasn't clearly testing what it should have been. MozReview-Commit-ID: I5fcfl5YEkq
testing/marionette/harness/marionette_harness/tests/unit/test_execute_script.py
--- a/testing/marionette/harness/marionette_harness/tests/unit/test_execute_script.py
+++ b/testing/marionette/harness/marionette_harness/tests/unit/test_execute_script.py
@@ -4,17 +4,17 @@
 
 import os
 import urllib
 
 from marionette_driver import By, errors
 from marionette_driver.marionette import HTMLElement
 from marionette_driver.wait import Wait
 
-from marionette_harness import MarionetteTestCase, WindowManagerMixin
+from marionette_harness import MarionetteTestCase, skip_if_mobile, WindowManagerMixin
 
 
 def inline(doc):
     return "data:text/html;charset=utf-8,{}".format(urllib.quote(doc))
 
 
 elements = inline("<p>foo</p> <p>bar</p>")
 
@@ -38,22 +38,28 @@ class TestExecuteSimpleTestContent(Mario
                 """, filename="file.js")
             self.assertFalse(True)
         except errors.JavascriptException as e:
             self.assertIn("throwHere is not defined", e.message)
             self.assertIn("@file.js:2", e.stacktrace)
 
 
 class TestExecuteContent(MarionetteTestCase):
+
+    def assert_is_defined(self, property, sandbox="default"):
+        self.assertTrue(self.marionette.execute_script(
+            "return typeof arguments[0] != 'undefined'", [property], sandbox=sandbox),
+            "property {} is undefined".format(property))
+
     def test_return_number(self):
         self.assertEqual(1, self.marionette.execute_script("return 1"))
         self.assertEqual(1.5, self.marionette.execute_script("return 1.5"))
 
     def test_return_boolean(self):
-        self.assertEqual(True, self.marionette.execute_script("return true"))
+        self.assertTrue(self.marionette.execute_script("return true"))
 
     def test_return_string(self):
         self.assertEqual("foo", self.marionette.execute_script("return 'foo'"))
 
     def test_return_array(self):
         self.assertEqual(
             [1, 2], self.marionette.execute_script("return [1, 2]"))
         self.assertEqual(
@@ -74,53 +80,46 @@ class TestExecuteContent(MarionetteTestC
             {"foo": 1.5}, self.marionette.execute_script("return {foo: 1.5}"))
         self.assertEqual(
             {"foo": True}, self.marionette.execute_script("return {foo: true}"))
         self.assertEqual(
             {"foo": "bar"}, self.marionette.execute_script("return {foo: 'bar'}"))
         self.assertEqual(
             {"foo": [1, 2]}, self.marionette.execute_script("return {foo: [1, 2]}"))
         self.assertEqual(
-            {"foo": {"bar": [1, 2]}}, self.marionette.execute_script("return {foo: {bar: [1, 2]}}"))
+            {"foo": {"bar": [1, 2]}},
+            self.marionette.execute_script("return {foo: {bar: [1, 2]}}"))
 
     def test_no_return_value(self):
         self.assertIsNone(self.marionette.execute_script("true"))
 
     def test_argument_null(self):
-        self.assertEqual(
-            None, self.marionette.execute_script("return arguments[0]", [None]))
+        self.assertIsNone(self.marionette.execute_script("return arguments[0]", [None]))
 
     def test_argument_number(self):
         self.assertEqual(
             1, self.marionette.execute_script("return arguments[0]", [1]))
         self.assertEqual(
             1.5, self.marionette.execute_script("return arguments[0]", [1.5]))
 
     def test_argument_boolean(self):
-        self.assertEqual(
-            True, self.marionette.execute_script("return arguments[0]", [True]))
+        self.assertTrue(self.marionette.execute_script("return arguments[0]", [True]))
 
     def test_argument_string(self):
         self.assertEqual(
             "foo", self.marionette.execute_script("return arguments[0]", ["foo"]))
 
     def test_argument_array(self):
         self.assertEqual(
             [1, 2], self.marionette.execute_script("return arguments[0]", [[1, 2]]))
 
     def test_argument_object(self):
         self.assertEqual({"foo": 1}, self.marionette.execute_script(
             "return arguments[0]", [{"foo": 1}]))
 
-    def assert_is_defined(self, property, sandbox="default"):
-        self.assertTrue(self.marionette.execute_script(
-            "return typeof {} != 'undefined'".format(property),
-            sandbox=sandbox),
-                        "property {} is undefined".format(property))
-
     def test_globals(self):
         for property in globals:
             self.assert_is_defined(property)
         self.assert_is_defined("Components")
         self.assert_is_defined("window.wrappedJSObject")
 
     def test_system_globals(self):
         for property in globals:
@@ -128,31 +127,30 @@ class TestExecuteContent(MarionetteTestC
         self.assert_is_defined("Components", sandbox="system")
         self.assert_is_defined("window.wrappedJSObject")
 
     def test_exception(self):
         self.assertRaises(errors.JavascriptException,
                           self.marionette.execute_script, "return foo")
 
     def test_stacktrace(self):
-        try:
+        with self.assertRaises(errors.JavascriptException) as cm:
             self.marionette.execute_script("return b")
-            self.assertFalse(True)
-        except errors.JavascriptException as e:
-            # by default execute_script pass the name of the python file
-            self.assertIn(
-                os.path.basename(__file__.replace(".pyc", ".py")), e.stacktrace)
-            self.assertIn("b is not defined", e.message)
-            self.assertIn("return b", e.stacktrace)
+
+        # by default execute_script pass the name of the python file
+        self.assertIn(os.path.basename(__file__.replace(".pyc", ".py")),
+                      cm.exception.stacktrace)
+        self.assertIn("b is not defined", cm.exception.message)
+        self.assertIn("return b", cm.exception.stacktrace)
 
     def test_permission(self):
         with self.assertRaises(errors.JavascriptException):
             self.marionette.execute_script("""
-                let prefs = Components.classes["@mozilla.org/preferences-service;1"]
-                    .getService(Components.interfaces.nsIPrefBranch)""")
+                var c = Components.classes["@mozilla.org/preferences-service;1"];
+            """)
 
     def test_return_web_element(self):
         self.marionette.navigate(elements)
         expected = self.marionette.find_element(By.TAG_NAME, "p")
         actual = self.marionette.execute_script(
             "return document.querySelector('p')")
         self.assertEqual(expected, actual)
 
@@ -186,25 +184,28 @@ class TestExecuteContent(MarionetteTestC
 
     def test_sandbox_refresh_arguments(self):
         self.marionette.execute_script(
             "this.foobar = [arguments[0], arguments[1]]", [23, 42])
         self.assertEqual(self.marionette.execute_script(
             "return this.foobar", new_sandbox=False), [23, 42])
 
     def test_wrappedjsobject(self):
-        self.marionette.execute_script("window.wrappedJSObject.foo = 3")
-        self.assertEqual(
-            3, self.marionette.execute_script("return window.wrappedJSObject.foo"))
+        try:
+            self.marionette.execute_script("window.wrappedJSObject.foo = 3")
+            self.assertEqual(
+                self.marionette.execute_script("return window.wrappedJSObject.foo"), 3)
+        finally:
+            self.marionette.execute_script("delete window.wrappedJSObject.foo")
 
     def test_system_sandbox_wrappedjsobject(self):
         self.marionette.execute_script(
             "window.wrappedJSObject.foo = 4", sandbox="system")
-        self.assertEqual(4, self.marionette.execute_script(
-            "return window.wrappedJSObject.foo", sandbox="system"))
+        self.assertEqual(self.marionette.execute_script(
+            "return window.wrappedJSObject.foo", sandbox="system"), 4)
 
     def test_system_dead_object(self):
         self.marionette.execute_script(
             "window.wrappedJSObject.foo = function() { return 'yo' }",
             sandbox="system")
         self.marionette.execute_script(
             "dump(window.wrappedJSObject.foo)", sandbox="system")
 
@@ -225,20 +226,19 @@ class TestExecuteContent(MarionetteTestC
 
         send("window.foo = 1")
         foo = send("return window.foo")
         self.assertEqual(1, foo)
 
         for property in globals:
             exists = send("return typeof {} != 'undefined'".format(property))
             self.assertTrue(exists, "property {} is undefined".format(property))
-        # TODO(ato): For some reason this fails, probably Sandbox bug?
-        # self.assertTrue(send("return typeof Components == 'undefined'"))
-        self.assertTrue(
-            send("return typeof window.wrappedJSObject == 'undefined'"))
+
+        self.assertTrue(send("return typeof Components.utils == 'undefined'"))
+        self.assertTrue(send("return typeof window.wrappedJSObject == 'undefined'"))
 
     def test_no_callback(self):
         self.assertTrue(self.marionette.execute_script(
             "return typeof arguments[0] == 'undefined'"))
 
     def test_window_set_timeout_is_not_cancelled(self):
         def content_timeout_triggered(mn):
             return mn.execute_script("return window.n", sandbox=None) > 0
@@ -265,54 +265,67 @@ class TestExecuteContent(MarionetteTestC
 
 class TestExecuteChrome(WindowManagerMixin, TestExecuteContent):
 
     def setUp(self):
         super(TestExecuteChrome, self).setUp()
 
         self.marionette.set_context("chrome")
 
+    def tearDown(self):
+        super(TestExecuteChrome, self).tearDown()
+
+    def test_permission(self):
+        self.assertEqual(1, self.marionette.execute_script("""
+            var c = Components.classes["@mozilla.org/preferences-service;1"]; return 1;"""))
+
+    @skip_if_mobile("New windows not supported in Fennec")
+    def test_unmarshal_element_collection(self):
+
         def open_window_with_js():
             self.marionette.execute_script(
                 "window.open('chrome://marionette/content/test.xul', 'xul', 'chrome');")
 
-        new_window = self.open_window(trigger=open_window_with_js)
-        self.marionette.switch_to_window(new_window)
-
-    def tearDown(self):
-        self.close_all_windows()
-        super(TestExecuteChrome, self).tearDown()
+        try:
+            win = self.open_window(trigger=open_window_with_js)
+            self.marionette.switch_to_window(win)
 
-    def test_permission(self):
-        self.assertEqual(1, self.marionette.execute_script(
-            "var c = Components.classes; return 1;"))
+            expected = self.marionette.find_elements(By.TAG_NAME, "textbox")
+            actual = self.marionette.execute_script(
+                "return document.querySelectorAll('textbox')")
+            self.assertEqual(expected, actual)
 
-    def test_unmarshal_element_collection(self):
-        expected = self.marionette.find_elements(By.TAG_NAME, "textbox")
-        actual = self.marionette.execute_script(
-            "return document.querySelectorAll('textbox')")
-        self.assertEqual(expected, actual)
+        finally:
+            self.close_all_windows()
 
     def test_async_script_timeout(self):
         with self.assertRaises(errors.ScriptTimeoutException):
             self.marionette.execute_async_script("""
                 var cb = arguments[arguments.length - 1];
                 setTimeout(function() { cb() }, 250);
                 """, script_timeout=100)
 
+    @skip_if_mobile("New windows not supported in Fennec")
     def test_invalid_chrome_handle(self):
-        # Close second chrome window and don't switch back to the original one
-        self.marionette.close_chrome_window()
-        self.assertEqual(len(self.marionette.chrome_window_handles), 1)
+        try:
+            win = self.open_window()
+            self.marionette.switch_to_window(win)
 
-        # Call execute_script on an invalid chrome handle
-        with self.marionette.using_context('chrome'):
-            self.marionette.execute_script("""
-                return true;
-            """)
+            # Close new window and don't switch back to the original one
+            self.marionette.close_chrome_window()
+            self.assertNotEqual(self.start_window, win)
+
+            # Call execute_script on an invalid chrome handle
+            with self.marionette.using_context('chrome'):
+                self.marionette.execute_script("""
+                    return true;
+                """)
+
+        finally:
+            self.close_all_windows()
 
     def test_lasting_side_effects(self):
         pass
 
     def test_return_web_element(self):
         pass
 
     def test_return_web_element_array(self):
@@ -321,16 +334,17 @@ class TestExecuteChrome(WindowManagerMix
     def test_return_web_element_nodelist(self):
         pass
 
     def test_window_set_timeout_is_not_cancelled(self):
         pass
 
 
 class TestElementCollections(MarionetteTestCase):
+
     def assertSequenceIsInstance(self, seq, typ):
         for item in seq:
             self.assertIsInstance(item, typ)
 
     def test_array(self):
         self.marionette.navigate(inline("<p>foo <p>bar"))
         els = self.marionette.execute_script("return Array.from(document.querySelectorAll('p'))")
         self.assertIsInstance(els, list)