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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
from __future__ import annotations
'''Test cases for QAccessible::installFactory().'''
import os
import sys
import unittest
from pathlib import Path
sys.path.append(os.fspath(Path(__file__).resolve().parents[1]))
from init_paths import init_test_paths
init_test_paths(False)
from PySide6.QtCore import QCoreApplication, Qt
from PySide6.QtGui import QAccessible, QAccessibleInterface, QColor
from PySide6.QtWidgets import QWidget, QLineEdit, QVBoxLayout
from helper.usesqapplication import UsesQApplication
class LineEditAccessible(QAccessibleInterface):
"""Mimick a QAccessibleInterface implementation for QLineEdit."""
instance_count = 0
def __init__(self, widget):
super().__init__()
LineEditAccessible.instance_count += 1
self._widget = widget
self._name = self._widget.objectName()
print('LineEditAccessible', self._name)
def __del__(self):
LineEditAccessible.instance_count -= 1
print('~LineEditAccessible', self._name)
def actionInterface(self):
return None
def backgroundColor(self):
return QColor(Qt.GlobalColor.white)
def child(self, index):
return None
def childAt(self, x, y):
return None
def childCount(self):
return 0
def focusChild(self):
return None
def foregroundColor(self):
return QColor(Qt.GlobalColor.black)
def indexOfChild(self, child):
return -1
def isValid(self):
return True
def object(self):
return self._widget
def parent(self):
return None
def rect(self):
return self._widget.geometry()
def role(self):
return QAccessible.EditableText
def setText(self, t, text):
pass
def state(self):
return QAccessible.State()
def tableCellInterface(self):
return None
def tableInterface(self):
return None
def text(self, t):
return self._widget.text() if t == QAccessible.Value else ''
def textInterface(self):
return None
def valueInterface(self):
return None
def window(self):
return self._widget.window().windowHandle()
def accessible_factory(key, obj):
"""Factory function for QAccessibleInterface for QLineEdit's."""
if obj.metaObject().className() == 'QLineEdit':
return LineEditAccessible(obj)
return None
class Window(QWidget):
"""Test window with 2 QLineEdit's."""
def __init__(self):
super().__init__()
self.setObjectName('top')
layout = QVBoxLayout(self)
self.m_line_edit1 = QLineEdit("bla")
layout.addWidget(self.m_line_edit1)
self.m_line_edit2 = QLineEdit("bla")
layout.addWidget(self.m_line_edit2)
class QAccessibleTest(UsesQApplication):
"""Test that LineEditAccessible instances are created for QLineEdit's."""
def setUp(self):
super().setUp()
QAccessible.installFactory(accessible_factory)
window = Window() # noqa: F841
def testLineEdits(self):
window = Window()
window.show()
while not window.windowHandle().isExposed():
QCoreApplication.processEvents()
self.assertEqual(LineEditAccessible.instance_count, 2)
if __name__ == "__main__":
unittest.main()
|