tor-browser

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

test_chrome_sendkeys_menupopup.py (4250B)


      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
      3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
      4 
      5 import os
      6 import sys
      7 
      8 from marionette_driver import By, errors, Wait
      9 from marionette_driver.keys import Keys
     10 from marionette_harness import (
     11    MarionetteTestCase,
     12    WindowManagerMixin,
     13 )
     14 
     15 # add this directory to the path
     16 sys.path.append(os.path.dirname(__file__))
     17 
     18 from chrome_handler_mixin import ChromeHandlerMixin
     19 
     20 
     21 class TestSendkeysMenupopup(ChromeHandlerMixin, WindowManagerMixin, MarionetteTestCase):
     22    def setUp(self):
     23        super(TestSendkeysMenupopup, self).setUp()
     24 
     25        self.marionette.set_context("chrome")
     26        new_window = self.open_chrome_window(self.chrome_base_url + "test_xul.xhtml")
     27        self.marionette.switch_to_window(new_window)
     28 
     29        self.click_el = self.marionette.find_element(By.ID, "options-button")
     30        self.disabled_menuitem_el = self.marionette.find_element(
     31            By.ID, "option-disabled"
     32        )
     33        self.hidden_menuitem_el = self.marionette.find_element(By.ID, "option-hidden")
     34        self.menuitem_el = self.marionette.find_element(By.ID, "option-enabled")
     35        self.menupopup_el = self.marionette.find_element(By.ID, "options-menupopup")
     36        self.testwindow_el = self.marionette.find_element(By.ID, "remote-window")
     37 
     38    def context_menu_state(self):
     39        return self.menupopup_el.get_property("state")
     40 
     41    def open_context_menu(self):
     42        def attempt_open_context_menu():
     43            self.assertEqual(self.context_menu_state(), "closed")
     44            self.click_el.click()
     45            Wait(self.marionette).until(
     46                lambda _: self.context_menu_state() == "open",
     47                message="Context menu did not open",
     48            )
     49 
     50        try:
     51            attempt_open_context_menu()
     52        except errors.TimeoutException:
     53            # If the first attempt timed out, try a second time.
     54            # On Linux, the test will intermittently fail if we click too
     55            # early on the button. Retrying fixes the issue. See Bug 1686769.
     56            attempt_open_context_menu()
     57 
     58    def wait_for_context_menu_closed(self):
     59        Wait(self.marionette).until(
     60            lambda _: self.context_menu_state() == "closed",
     61            message="Context menu did not close",
     62        )
     63 
     64    def tearDown(self):
     65        try:
     66            self.close_all_windows()
     67        finally:
     68            super(TestSendkeysMenupopup, self).tearDown()
     69 
     70    def test_sendkeys_menuitem(self):
     71        # Try closing the context menu by sending ESCAPE to a visible context menu item.
     72        self.open_context_menu()
     73 
     74        self.menuitem_el.send_keys(Keys.ESCAPE)
     75        self.wait_for_context_menu_closed()
     76 
     77    def test_sendkeys_menupopup(self):
     78        # Try closing the context menu by sending ESCAPE to the context menu.
     79        self.open_context_menu()
     80 
     81        self.menupopup_el.send_keys(Keys.ESCAPE)
     82        self.wait_for_context_menu_closed()
     83 
     84    def test_sendkeys_window(self):
     85        # Try closing the context menu by sending ESCAPE to the main window.
     86        self.open_context_menu()
     87 
     88        self.testwindow_el.send_keys(Keys.ESCAPE)
     89        self.wait_for_context_menu_closed()
     90 
     91    def test_sendkeys_closed_menu(self):
     92        # send_keys should throw for the menupopup if the contextmenu is closed.
     93        with self.assertRaises(errors.ElementNotInteractableException):
     94            self.menupopup_el.send_keys(Keys.ESCAPE)
     95 
     96        # send_keys should throw for the menuitem if the contextmenu is closed.
     97        with self.assertRaises(errors.ElementNotInteractableException):
     98            self.menuitem_el.send_keys(Keys.ESCAPE)
     99 
    100    def test_sendkeys_hidden_disabled_menuitem(self):
    101        self.open_context_menu()
    102 
    103        # send_keys should throw for a disabled menuitem in an opened contextmenu.
    104        with self.assertRaises(errors.ElementNotInteractableException):
    105            self.disabled_menuitem_el.send_keys(Keys.ESCAPE)
    106 
    107        # send_keys should throw for a hidden menuitem in an opened contextmenu.
    108        with self.assertRaises(errors.ElementNotInteractableException):
    109            self.hidden_menuitem_el.send_keys(Keys.ESCAPE)