aboutsummaryrefslogtreecommitdiffstats
path: root/sources/pyside6/tests/QtQml/listproperty.py
blob: 884600d29ad350527e10e5bff42f6c1671189c35 (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
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
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

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  # noqa: E402
init_test_paths(False)

from helper.usesqapplication import UsesQApplication  # noqa: E402, F401

from PySide6.QtCore import QObject, QUrl, Property, qInstallMessageHandler  # noqa: E402
from PySide6.QtQml import ListProperty, QmlElement  # noqa: E402
from PySide6.QtQuick import QQuickView  # noqa: E402


QML_IMPORT_NAME = "test.ListPropertyTest"
QML_IMPORT_MAJOR_VERSION = 1

output_messages = []


def message_handler(mode, context, message):
    global output_messages
    output_messages.append(f"{message}")


class InheritsQObject(QObject):
    pass


def dummyFunc():
    pass


@QmlElement
class Person(QObject):
    def __init__(self, parent=None):
        super().__init__(parent=None)
        self._name = ''
        self._friends = []

    def appendFriend(self, friend):
        self._friends.append(friend)

    def friendCount(self):
        return len(self._friends)

    def friend(self, index):
        return self._friends[index]

    def removeLastItem(self):
        if len(self._friends) > 0:
            self._friends.pop()

    def replace(self, index, friend):
        if 0 <= index < len(self._friends):
            self._friends[index] = friend

    def clear(self):
        self._friends.clear()

    @Property(str, final=True)
    def name(self):
        return self._name

    @name.setter
    def name(self, value):
        self._name = value

    friends = ListProperty(QObject, append=appendFriend, count=friendCount, at=friend,
                           removeLast=removeLastItem, replace=replace, clear=clear)


class TestListProperty(UsesQApplication):
    def testIt(self):

        # Verify that type checking works properly
        type_check_error = False

        try:
            ListProperty(QObject)
            ListProperty(InheritsQObject)
        except Exception:
            type_check_error = True

        self.assertFalse(type_check_error)

        try:
            ListProperty(int)
        except TypeError:
            type_check_error = True

        self.assertTrue(type_check_error)

        # Verify that method validation works properly
        method_check_error = False

        try:
            ListProperty(QObject, append=None, at=None, count=None, replace=None, clear=None,
                         removeLast=None)  # Explicitly setting None
            ListProperty(QObject, append=dummyFunc)
            ListProperty(QObject, count=dummyFunc, at=dummyFunc)
        except Exception:
            method_check_error = True

        self.assertFalse(method_check_error)

        try:
            ListProperty(QObject, append=QObject())
        except Exception:
            method_check_error = True

        self.assertTrue(method_check_error)

    def testListPropParameters(self):
        global output_messages
        qInstallMessageHandler(message_handler)
        view = QQuickView()
        file = Path(__file__).resolve().parent / 'listproperty.qml'
        self.assertTrue(file.is_file())
        view.setSource(QUrl.fromLocalFile(file))
        view.show()
        self.assertEqual(output_messages[0], "List length: 3")
        self.assertEqual(output_messages[1], "First element: Alice")
        self.assertEqual(output_messages[2], "Removing last item: Charlie")
        self.assertEqual(output_messages[3], "Replacing last item: Bob")
        self.assertEqual(output_messages[4], "Replaced last item: David")
        self.assertEqual(output_messages[5], "List length after clearing: 0")


if __name__ == '__main__':
    unittest.main()