summaryrefslogtreecommitdiffstats
path: root/accessible/tests/browser/atk/a11y_setup.py
blob: c5fe3481ff46d03d73807e2942573428cd6f0bc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 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/.

"""Python environment for ATK a11y browser tests.
"""

import os
import subprocess
import sys

import psutil

# pyatspi can't be installed using pip. Rely on the system installation.
# Get the path to the system installation of pyatspi.
pyatspiFile = subprocess.check_output(
    (
        os.path.join(sys.base_prefix, "bin", "python3"),
        "-c",
        "import pyatspi; print(pyatspi.__file__)",
    ),
    encoding="utf-8",
).rstrip()
sys.path.append(os.path.dirname(os.path.dirname(pyatspiFile)))
import pyatspi

sys.path.pop()
del pyatspiFile


def getDoc():
    """Get the Accessible for the document being tested."""
    # We can compare the parent process ids to find the Firefox started by the
    # test harness.
    commonPid = psutil.Process().ppid()
    for app in pyatspi.Registry.getDesktop(0):
        if (
            app.name == "Firefox"
            and psutil.Process(app.get_process_id()).ppid() == commonPid
        ):
            break
    else:
        raise LookupError("Couldn't find Firefox application Accessible")
    root = app[0]
    for embeds in root.getRelationSet():
        if embeds.getRelationType() == pyatspi.RELATION_EMBEDS:
            break
    else:
        raise LookupError("Firefox root doesn't have RELATION_EMBEDS")
    doc = embeds.getTarget(0)
    child = doc[0]
    if child.get_attributes().get("id") == "default-iframe-id":
        # This is an iframe or remoteIframe test.
        doc = child[0]
    return doc


def findByDomId(root, id):
    for child in root:
        if child.get_attributes().get("id") == id:
            return child
        descendant = findByDomId(child, id)
        if descendant:
            return descendant