1# This Source Code Form is subject to the terms of the Mozilla Public
2# License, v. 2.0. If a copy of the MPL was not distributed with this file,
3# You can obtain one at http://mozilla.org/MPL/2.0/.
4
5import os
6import sys
7from unittest import skipIf
8
9from marionette_driver import By
10
11# add this directory to the path
12sys.path.append(os.path.dirname(__file__))
13
14from test_switch_window_content import TestSwitchToWindowContent
15
16
17class TestSwitchWindowChrome(TestSwitchToWindowContent):
18
19    def setUp(self):
20        super(TestSwitchWindowChrome, self).setUp()
21
22        self.marionette.set_context("chrome")
23
24    def tearDown(self):
25        self.close_all_windows()
26
27        super(TestSwitchWindowChrome, self).tearDown()
28
29    def open_window_in_background(self):
30        with self.marionette.using_context("chrome"):
31            self.marionette.execute_script("""
32              window.open("about:blank", null, "location=1,toolbar=1");
33              window.focus();
34            """)
35
36    def open_window_in_foreground(self):
37        with self.marionette.using_context("content"):
38            self.marionette.navigate(self.test_page)
39            link = self.marionette.find_element(By.ID, "new-window")
40            link.click()
41
42    @skipIf(sys.platform.startswith("linux"),
43            "Bug 1335457 - Fails to open a background window on Linux")
44    def test_switch_tabs_for_new_background_window_without_focus_change(self):
45        # Bug 1334981 - with testmode enabled getMostRecentWindow detects the wrong window
46        with self.marionette.using_prefs({"focusmanager.testmode": False}):
47            # Open an addition tab in the original window so we can better check
48            # the selected index in thew new window to be opened.
49            second_tab = self.open_tab(trigger=self.open_tab_in_foreground)
50            self.marionette.switch_to_window(second_tab, focus=True)
51            second_tab_index = self.get_selected_tab_index()
52            self.assertNotEqual(second_tab_index, self.selected_tab_index)
53
54            # Opens a new background window, but we are interested in the tab
55            tab_in_new_window = self.open_tab(trigger=self.open_window_in_background)
56            self.assertEqual(self.marionette.current_window_handle, second_tab)
57            self.assertEqual(self.marionette.current_chrome_window_handle, self.start_window)
58            self.assertEqual(self.get_selected_tab_index(), second_tab_index)
59            with self.marionette.using_context("content"):
60                self.assertEqual(self.marionette.get_url(), self.empty_page)
61
62            # Switch to the tab in the new window but don't focus it
63            self.marionette.switch_to_window(tab_in_new_window, focus=False)
64            self.assertEqual(self.marionette.current_window_handle, tab_in_new_window)
65            self.assertNotEqual(self.marionette.current_chrome_window_handle, self.start_window)
66            self.assertEqual(self.get_selected_tab_index(), second_tab_index)
67            with self.marionette.using_context("content"):
68                self.assertEqual(self.marionette.get_url(), "about:blank")
69
70    def test_switch_tabs_for_new_foreground_window_with_focus_change(self):
71        # Open an addition tab in the original window so we can better check
72        # the selected index in thew new window to be opened.
73        second_tab = self.open_tab(trigger=self.open_tab_in_foreground)
74        self.marionette.switch_to_window(second_tab, focus=True)
75        second_tab_index = self.get_selected_tab_index()
76        self.assertNotEqual(second_tab_index, self.selected_tab_index)
77
78        # Opens a new window, but we are interested in the tab
79        tab_in_new_window = self.open_tab(trigger=self.open_window_in_foreground)
80        self.assertEqual(self.marionette.current_window_handle, second_tab)
81        self.assertEqual(self.marionette.current_chrome_window_handle, self.start_window)
82        self.assertNotEqual(self.get_selected_tab_index(), second_tab_index)
83        with self.marionette.using_context("content"):
84            self.assertEqual(self.marionette.get_url(), self.test_page)
85
86        self.marionette.switch_to_window(tab_in_new_window)
87        self.assertEqual(self.marionette.current_window_handle, tab_in_new_window)
88        self.assertNotEqual(self.marionette.current_chrome_window_handle, self.start_window)
89        self.assertNotEqual(self.get_selected_tab_index(), second_tab_index)
90        with self.marionette.using_context("content"):
91            self.assertEqual(self.marionette.get_url(), self.empty_page)
92
93        self.marionette.switch_to_window(second_tab, focus=True)
94        self.assertEqual(self.marionette.current_window_handle, second_tab)
95        self.assertEqual(self.marionette.current_chrome_window_handle, self.start_window)
96        # Bug 1335085 - The focus doesn't change even as requested so.
97        # self.assertEqual(self.get_selected_tab_index(), second_tab_index)
98        with self.marionette.using_context("content"):
99            self.assertEqual(self.marionette.get_url(), self.test_page)
100
101    def test_switch_tabs_for_new_foreground_window_without_focus_change(self):
102        # Open an addition tab in the original window so we can better check
103        # the selected index in thew new window to be opened.
104        second_tab = self.open_tab(trigger=self.open_tab_in_foreground)
105        self.marionette.switch_to_window(second_tab, focus=True)
106        second_tab_index = self.get_selected_tab_index()
107        self.assertNotEqual(second_tab_index, self.selected_tab_index)
108
109        # Opens a new window, but we are interested in the tab which automatically
110        # gets the focus.
111        self.open_tab(trigger=self.open_window_in_foreground)
112        self.assertEqual(self.marionette.current_window_handle, second_tab)
113        self.assertEqual(self.marionette.current_chrome_window_handle, self.start_window)
114        self.assertNotEqual(self.get_selected_tab_index(), second_tab_index)
115        with self.marionette.using_context("content"):
116            self.assertEqual(self.marionette.get_url(), self.test_page)
117
118        # Switch to the second tab in the first window, but don't focus it.
119        self.marionette.switch_to_window(second_tab, focus=False)
120        self.assertEqual(self.marionette.current_window_handle, second_tab)
121        self.assertEqual(self.marionette.current_chrome_window_handle, self.start_window)
122        self.assertNotEqual(self.get_selected_tab_index(), second_tab_index)
123        with self.marionette.using_context("content"):
124            self.assertEqual(self.marionette.get_url(), self.test_page)
125