Bug 1311350 - Add unit tests for close window commands. draft
authorHenrik Skupin <mail@hskupin.info>
Mon, 09 Jan 2017 23:06:00 +0100
changeset 458399 9fefaf3b5377b40ce697f6afdd7f326010d2b4f3
parent 458398 f86d6c3986d0221e8becda742b6bdf35b643bf1f
child 541635 f9e437c7eb39f50850c7a5d0aa73093cac10a958
push id40942
push userbmo:hskupin@gmail.com
push dateTue, 10 Jan 2017 07:43:51 +0000
bugs1311350
milestone53.0a1
Bug 1311350 - Add unit tests for close window commands. MozReview-Commit-ID: Ldsx5M1vxCe
testing/marionette/harness/marionette_harness/__init__.py
testing/marionette/harness/marionette_harness/runner/mixins/window_manager.py
testing/marionette/harness/marionette_harness/tests/unit/test_window_close_chrome.py
testing/marionette/harness/marionette_harness/tests/unit/test_window_close_content.py
testing/marionette/harness/marionette_harness/tests/unit/unit-tests.ini
--- a/testing/marionette/harness/marionette_harness/__init__.py
+++ b/testing/marionette/harness/marionette_harness/__init__.py
@@ -3,16 +3,17 @@
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 __version__ = '4.0.0'
 
 from .marionette_test import (
     CommonTestCase,
     expectedFailure,
     MarionetteTestCase,
+    parameterized,
     run_if_e10s,
     run_if_manage_instance,
     skip,
     skip_if_chrome,
     skip_if_desktop,
     skip_if_e10s,
     skip_if_mobile,
     SkipTest,
--- a/testing/marionette/harness/marionette_harness/runner/mixins/window_manager.py
+++ b/testing/marionette/harness/marionette_harness/runner/mixins/window_manager.py
@@ -91,17 +91,17 @@ class WindowManagerMixin(object):
             [new_tab] = list(set(self.marionette.window_handles) - set(current_tabs))
 
             return new_tab
 
     def open_window(self, trigger=None):
         current_windows = self.marionette.chrome_window_handles
 
         def loaded(handle):
-            with self.marionette.using_context('chrome'):
+            with self.marionette.using_context("chrome"):
                 return self.marionette.execute_script("""
                   Components.utils.import("resource://gre/modules/Services.jsm");
 
                   let win = Services.wm.getOuterWindowWithId(Number(arguments[0]));
                   return win.document.readyState == "complete";
                 """, script_args=[handle])
 
         try:
new file mode 100644
--- /dev/null
+++ b/testing/marionette/harness/marionette_harness/tests/unit/test_window_close_chrome.py
@@ -0,0 +1,69 @@
+# 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_harness import MarionetteTestCase, WindowManagerMixin
+
+
+class TestCloseWindow(WindowManagerMixin, MarionetteTestCase):
+
+    def setUp(self):
+        super(TestCloseWindow, self).setUp()
+        self.marionette.set_context("chrome")
+
+    def tearDown(self):
+        self.close_all_windows()
+        self.close_all_tabs()
+        super(TestCloseWindow, self).tearDown()
+
+    def test_close_chrome_window_handle(self):
+        def open_window_with_js():
+            self.marionette.execute_script("""
+              window.open('chrome://marionette/content/test.xul',
+                          'foo', 'chrome,centerscreen');
+            """)
+
+        win = self.open_window(trigger=open_window_with_js)
+        self.marionette.switch_to_window(win)
+
+        self.assertEqual(2, len(self.marionette.chrome_window_handles))
+        chrome_window_handles = self.marionette.close_chrome_window()
+        self.assertNotIn(win, chrome_window_handles)
+        self.assertListEqual(self.marionette.chrome_window_handles, chrome_window_handles)
+
+    def test_close_chrome_window_last_handle(self):
+        self.assertEqual(1, len(self.marionette.chrome_window_handles))
+        self.assertListEqual([], self.marionette.close_chrome_window())
+        self.assertIsNotNone(self.marionette.session)
+
+    def test_close_window_handle(self):
+        # Test closing a second tab from a new browser window
+        self.assertEqual(1, len(self.marionette.chrome_window_handles))
+        self.assertEqual(1, len(self.marionette.window_handles))
+
+        win = self.open_window()
+        self.marionette.switch_to_window(win)
+        tab = self.open_tab()
+        self.marionette.switch_to_window(tab)
+
+        self.assertEqual(2, len(self.marionette.chrome_window_handles))
+        self.assertEqual(3, len(self.marionette.window_handles))
+        window_handles = self.marionette.close()
+        self.assertNotIn(tab, window_handles)
+        self.assertListEqual(self.marionette.window_handles, window_handles)
+        self.assertEqual(2, len(self.marionette.chrome_window_handles))
+        self.assertEqual(2, len(self.marionette.window_handles))
+
+        self.marionette.switch_to_window(win)
+
+        self.assertEqual(2, len(self.marionette.window_handles))
+        window_handles = self.marionette.close()
+        self.assertNotIn(win, window_handles)
+        self.assertListEqual(self.marionette.window_handles, window_handles)
+        self.assertEqual(1, len(self.marionette.chrome_window_handles))
+        self.assertEqual(1, len(self.marionette.window_handles))
+
+    def test_close_window_last_handle(self):
+        self.assertEqual(1, len(self.marionette.window_handles))
+        self.assertListEqual([], self.marionette.close())
+        self.assertIsNotNone(self.marionette.session)
new file mode 100644
--- /dev/null
+++ b/testing/marionette/harness/marionette_harness/tests/unit/test_window_close_content.py
@@ -0,0 +1,70 @@
+# 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_harness import MarionetteTestCase, skip_if_mobile, WindowManagerMixin
+
+
+class TestCloseWindow(WindowManagerMixin, MarionetteTestCase):
+
+    def tearDown(self):
+        self.close_all_windows()
+        self.close_all_tabs()
+        super(TestCloseWindow, self).tearDown()
+
+    @skip_if_mobile("Interacting with chrome windows not available for Fennec")
+    def test_close_chrome_window_handle(self):
+
+        def open_window_with_js():
+            with self.marionette.using_context("chrome"):
+                self.marionette.execute_script("""
+                  window.open('chrome://marionette/content/test.xul',
+                              'foo', 'chrome,centerscreen');
+                """)
+
+        win = self.open_window(trigger=open_window_with_js)
+        self.marionette.switch_to_window(win)
+
+        self.assertEqual(2, len(self.marionette.chrome_window_handles))
+        chrome_window_handles = self.marionette.close_chrome_window()
+        self.assertNotIn(win, chrome_window_handles)
+        self.assertListEqual(self.marionette.chrome_window_handles, chrome_window_handles)
+
+    @skip_if_mobile("Interacting with chrome windows not available for Fennec")
+    def test_close_chrome_window_last_handle(self):
+        self.assertEqual(1, len(self.marionette.chrome_window_handles))
+        self.assertListEqual([], self.marionette.close_chrome_window())
+        self.assertIsNotNone(self.marionette.session)
+
+    @skip_if_mobile("Needs application independent method to open a new tab")
+    def test_close_window_handle(self):
+        # Test closing a second tab from a new browser window
+        self.assertEqual(1, len(self.marionette.chrome_window_handles))
+        self.assertEqual(1, len(self.marionette.window_handles))
+
+        win = self.open_window()
+        self.marionette.switch_to_window(win)
+        tab = self.open_tab()
+        self.marionette.switch_to_window(tab)
+
+        self.assertEqual(2, len(self.marionette.chrome_window_handles))
+        self.assertEqual(3, len(self.marionette.window_handles))
+        window_handles = self.marionette.close()
+        self.assertNotIn(tab, window_handles)
+        self.assertListEqual(self.marionette.window_handles, window_handles)
+        self.assertEqual(2, len(self.marionette.chrome_window_handles))
+        self.assertEqual(2, len(self.marionette.window_handles))
+
+        self.marionette.switch_to_window(win)
+
+        self.assertEqual(2, len(self.marionette.window_handles))
+        window_handles = self.marionette.close()
+        self.assertNotIn(win, window_handles)
+        self.assertListEqual(self.marionette.window_handles, window_handles)
+        self.assertEqual(1, len(self.marionette.chrome_window_handles))
+        self.assertEqual(1, len(self.marionette.window_handles))
+
+    def test_close_window_last_handle(self):
+        self.assertEqual(1, len(self.marionette.window_handles))
+        self.assertListEqual([], self.marionette.close())
+        self.assertIsNotNone(self.marionette.session)
--- a/testing/marionette/harness/marionette_harness/tests/unit/unit-tests.ini
+++ b/testing/marionette/harness/marionette_harness/tests/unit/unit-tests.ini
@@ -65,16 +65,19 @@ skip-if = appname == 'fennec'
 [test_pagesource_chrome.py]
 skip-if = appname == 'fennec'
 
 [test_visibility.py]
 [test_window_switching.py]
 skip-if = appname == 'fennec'
 [test_window_management.py]
 skip-if = appname == 'fennec'
+[test_window_close_chrome.py]
+skip-if = appname == 'fennec'
+[test_window_close_content.py]
 [test_window_position.py]
 skip-if = appname == 'fennec'
 [test_window_handles.py]
 skip-if = appname == 'fennec'
 
 [test_screenshot.py]
 [test_cookies.py]
 [test_window_title.py]