tor-browser

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

a11y_setup.py (2132B)


      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 """Python environment for ATK a11y browser tests."""
      6 
      7 import os
      8 import subprocess
      9 import sys
     10 
     11 import psutil
     12 
     13 # pyatspi can't be installed using pip. Rely on the system installation.
     14 # Get the path to the system installation of pyatspi.
     15 # Some systems have pyatspi and gi in different locations, so get both.
     16 extraPaths = eval(
     17    subprocess.check_output(
     18        (
     19            os.path.join(sys.base_prefix, "bin", "python3"),
     20            "-c",
     21            "import pyatspi, gi; print(repr([pyatspi.__file__, gi.__file__]))",
     22        ),
     23        encoding="utf-8",
     24    ).rstrip()
     25 )
     26 
     27 sys.path += [os.path.dirname(os.path.dirname(p)) for p in extraPaths]
     28 import pyatspi
     29 
     30 del sys.path[-len(extraPaths) :]
     31 del extraPaths
     32 
     33 
     34 def setup():
     35    # We do all the setup we need at module level.
     36    pass
     37 
     38 
     39 def getDoc():
     40    """Get the Accessible for the document being tested."""
     41    # We can compare the parent process ids to find the Firefox started by the
     42    # test harness.
     43    commonPid = psutil.Process().ppid()
     44    for app in pyatspi.Registry.getDesktop(0):
     45        if (
     46            app.name == "Firefox"
     47            and psutil.Process(app.get_process_id()).ppid() == commonPid
     48        ):
     49            break
     50    else:
     51        raise LookupError("Couldn't find Firefox application Accessible")
     52    root = app[0]
     53    for embeds in root.getRelationSet():
     54        if embeds.getRelationType() == pyatspi.RELATION_EMBEDS:
     55            break
     56    else:
     57        raise LookupError("Firefox root doesn't have RELATION_EMBEDS")
     58    doc = embeds.getTarget(0)
     59    child = doc[0]
     60    if child.get_attributes().get("id") == "default-iframe-id":
     61        # This is an iframe or remoteIframe test.
     62        doc = child[0]
     63    return doc
     64 
     65 
     66 def findByDomId(root, id):
     67    for child in root:
     68        if child.get_attributes().get("id") == id:
     69            return child
     70        descendant = findByDomId(child, id)
     71        if descendant:
     72            return descendant