tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_modal_dialogs.py (5672B)


      1 from marionette_driver.by import By
      2 from marionette_driver.expected import element_present
      3 from marionette_driver import errors
      4 from marionette_driver.marionette import Alert
      5 from marionette_driver.wait import Wait
      6 
      7 from marionette_harness import MarionetteTestCase, parameterized, WindowManagerMixin
      8 
      9 
     10 class TestModalDialogs(WindowManagerMixin, MarionetteTestCase):
     11    def setUp(self):
     12        super(TestModalDialogs, self).setUp()
     13        self.new_tab = self.open_tab()
     14        self.marionette.switch_to_window(self.new_tab)
     15 
     16        self.http_auth_pref = (
     17            "network.auth.non-web-content-triggered-resources-http-auth-allow"
     18        )
     19 
     20    def tearDown(self):
     21        # Ensure to close all possible remaining tab modal dialogs
     22        try:
     23            while True:
     24                alert = self.marionette.switch_to_alert()
     25                alert.dismiss()
     26        except errors.NoAlertPresentException:
     27            pass
     28 
     29        self.close_all_tabs()
     30        self.close_all_windows()
     31 
     32        super(TestModalDialogs, self).tearDown()
     33 
     34    @property
     35    def alert_present(self):
     36        try:
     37            Alert(self.marionette).text
     38            return True
     39        except errors.NoAlertPresentException:
     40            return False
     41 
     42    def wait_for_alert(self, timeout=None):
     43        Wait(self.marionette, timeout=timeout).until(lambda _: self.alert_present)
     44 
     45    def open_custom_prompt(self, modal_type, delay=0):
     46        browsing_context_id = self.marionette.execute_script(
     47            """
     48            return window.browsingContext.id;
     49        """,
     50            sandbox="system",
     51        )
     52 
     53        with self.marionette.using_context("chrome"):
     54            self.marionette.execute_script(
     55                """
     56                const [ modalType, browsingContextId, delay ] = arguments;
     57 
     58                const modalTypes = {
     59                  1: Services.prompt.MODAL_TYPE_CONTENT,
     60                  2: Services.prompt.MODAL_TYPE_TAB,
     61                  3: Services.prompt.MODAL_TYPE_WINDOW,
     62                  4: Services.prompt.MODAL_TYPE_INTERNAL_WINDOW,
     63                }
     64 
     65                window.setTimeout(() => {
     66                  Services.prompt.alertBC(
     67                    BrowsingContext.get(browsingContextId),
     68                    modalTypes[modalType],
     69                    "title",
     70                    "text"
     71                  );
     72                }, delay);
     73            """,
     74                script_args=(modal_type, browsing_context_id, delay * 1000),
     75            )
     76 
     77    @parameterized("content", 1)
     78    @parameterized("tab", 2)
     79    @parameterized("window", 3)
     80    @parameterized("internal_window", 4)
     81    def test_detect_modal_type_in_current_tab_for_type(self, type):
     82        self.open_custom_prompt(type)
     83        self.wait_for_alert()
     84 
     85        self.assertTrue(self.alert_present)
     86 
     87        # Restart the session to ensure we still find the formerly left-open dialog.
     88        self.marionette.delete_session()
     89        self.marionette.start_session()
     90 
     91        alert = self.marionette.switch_to_alert()
     92        alert.dismiss()
     93 
     94    @parameterized("content", 1)
     95    @parameterized("tab", 2)
     96    def test_dont_detect_content_and_tab_modal_type_in_another_tab_for_type(self, type):
     97        self.open_custom_prompt(type, delay=0.25)
     98 
     99        self.marionette.switch_to_window(self.start_tab)
    100        with self.assertRaises(errors.TimeoutException):
    101            self.wait_for_alert(2)
    102 
    103        self.marionette.switch_to_window(self.new_tab)
    104        alert = self.marionette.switch_to_alert()
    105        alert.dismiss()
    106 
    107    @parameterized("window", 3)
    108    @parameterized("internal_window", 4)
    109    def test_detect_window_modal_type_in_another_tab_for_type(self, type):
    110        self.open_custom_prompt(type, delay=0.25)
    111 
    112        self.marionette.switch_to_window(self.start_tab)
    113        self.wait_for_alert()
    114 
    115        alert = self.marionette.switch_to_alert()
    116        alert.dismiss()
    117 
    118        self.marionette.switch_to_window(self.new_tab)
    119        self.assertFalse(self.alert_present)
    120 
    121    @parameterized("window", 3)
    122    @parameterized("internal_window", 4)
    123    def test_detect_window_modal_type_in_another_window_for_type(self, type):
    124        self.new_window = self.open_window()
    125 
    126        self.marionette.switch_to_window(self.new_window)
    127 
    128        self.open_custom_prompt(type, delay=0.25)
    129 
    130        self.marionette.switch_to_window(self.new_tab)
    131        with self.assertRaises(errors.TimeoutException):
    132            self.wait_for_alert(2)
    133 
    134        self.marionette.switch_to_window(self.new_window)
    135        alert = self.marionette.switch_to_alert()
    136        alert.dismiss()
    137 
    138        self.marionette.switch_to_window(self.new_tab)
    139        self.assertFalse(self.alert_present)
    140 
    141    def test_http_auth_dismiss(self):
    142        with self.marionette.using_prefs({self.http_auth_pref: True}):
    143            self.marionette.navigate(self.marionette.absolute_url("http_auth"))
    144            self.wait_for_alert(timeout=self.marionette.timeout.page_load)
    145 
    146            alert = self.marionette.switch_to_alert()
    147            alert.dismiss()
    148 
    149            status = Wait(
    150                self.marionette, timeout=self.marionette.timeout.page_load
    151            ).until(element_present(By.ID, "status"))
    152            self.assertEqual(status.text, "restricted")
    153 
    154    def test_http_auth_send_keys(self):
    155        with self.marionette.using_prefs({self.http_auth_pref: True}):
    156            self.marionette.navigate(self.marionette.absolute_url("http_auth"))
    157            self.wait_for_alert(timeout=self.marionette.timeout.page_load)
    158 
    159            alert = self.marionette.switch_to_alert()
    160            with self.assertRaises(errors.UnsupportedOperationException):
    161                alert.send_keys("foo")