diff options
Diffstat (limited to 'sources/pyside6/tests/QtQml/registersingletontype.py')
-rw-r--r-- | sources/pyside6/tests/QtQml/registersingletontype.py | 131 |
1 files changed, 94 insertions, 37 deletions
diff --git a/sources/pyside6/tests/QtQml/registersingletontype.py b/sources/pyside6/tests/QtQml/registersingletontype.py index 30844ff8c..6beca1131 100644 --- a/sources/pyside6/tests/QtQml/registersingletontype.py +++ b/sources/pyside6/tests/QtQml/registersingletontype.py @@ -1,30 +1,5 @@ -############################################################################# -## -## Copyright (C) 2020 The Qt Company Ltd. -## Contact: https://www.qt.io/licensing/ -## -## This file is part of the test suite of Qt for Python. -## -## $QT_BEGIN_LICENSE:GPL-EXCEPT$ -## Commercial License Usage -## Licensees holding valid commercial Qt licenses may use this file in -## accordance with the commercial license agreement provided with the -## Software or, alternatively, in accordance with the terms contained in -## a written agreement between you and The Qt Company. For licensing terms -## and conditions see https://www.qt.io/terms-conditions. For further -## information use the contact form at https://www.qt.io/contact-us. -## -## GNU General Public License Usage -## Alternatively, this file may be used under the terms of the GNU -## General Public License version 3 as published by the Free Software -## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -## included in the packaging of this file. Please review the following -## information to ensure the GNU General Public License requirements will -## be met: https://www.gnu.org/licenses/gpl-3.0.html. -## -## $QT_END_LICENSE$ -## -############################################################################# +# 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 @@ -37,12 +12,18 @@ init_test_paths(False) from helper.helper import quickview_errorstring -from PySide6.QtCore import Property, Signal, QTimer, QUrl, QObject +from PySide6.QtCore import Property, Signal, QTimer, QUrl, QObject, Slot from PySide6.QtGui import QGuiApplication -from PySide6.QtQml import qmlRegisterSingletonType +from PySide6.QtQml import (qmlRegisterSingletonType, qmlRegisterSingletonInstance, + QmlElement, QmlSingleton, QJSValue) from PySide6.QtQuick import QQuickView + +URI = "Singletons" + + finalResult = 0 +qObjectQmlTypeId = 0 class SingletonQObject(QObject): @@ -70,26 +51,102 @@ def singletonQJSValueCallback(engine): return engine.evaluate("new Object({data: 50})") +QML_IMPORT_NAME = URI +QML_IMPORT_MAJOR_VERSION = 1 + + +@QmlElement +@QmlSingleton +class DecoratedSingletonQObject(QObject): + def __init__(self, parent=None): + super().__init__(parent) + self._data = 200 + + def getData(self): + return self._data + + def setData(self, data): + self._data = data + + data = Property(int, getData, setData) + + +@QmlElement +@QmlSingleton +class DecoratedSingletonWithCreate(QObject): + def __init__(self, data, parent=None): + super().__init__(parent) + self._data = data + + @staticmethod + def create(engine): + return DecoratedSingletonWithCreate(400) + + def getData(self): + return self._data + + def setData(self, data): + self._data = data + + data = Property(int, getData, setData) + + +class TestQuickView(QQuickView): + def __init__(self, parent=None): + super().__init__(parent) + self._singleton_instance_qobject_int = False + self._singleton_instance_qobject_str = False + self._singleton_instance_jsvalue_int = False + + @Slot() + def testSlot(self): + engine = self.engine() + instance = engine.singletonInstance(qObjectQmlTypeId) + if instance is not None and isinstance(instance, QObject): + self._singleton_instance_qobject_int = True + instance = engine.singletonInstance(URI, 'SingletonQObjectNoCallback') + if instance is not None and isinstance(instance, QObject): + self._singleton_instance_qobject_str = True + instance = engine.singletonInstance(URI, 'SingletonQJSValue') + if instance is not None and isinstance(instance, QJSValue): + self._singleton_instance_jsvalue_int = True + self.close() + + class TestQmlSupport(unittest.TestCase): def testIt(self): app = QGuiApplication([]) - qmlRegisterSingletonType(SingletonQObject, 'Singletons', 1, 0, 'SingletonQObjectNoCallback') - qmlRegisterSingletonType(SingletonQObject, 'Singletons', 1, 0, 'SingletonQObjectCallback', + qObjectQmlTypeId = qmlRegisterSingletonType(SingletonQObject, URI, 1, 0, + 'SingletonQObjectNoCallback') + qmlRegisterSingletonType(SingletonQObject, URI, 1, 0, 'SingletonQObjectCallback', singletonQObjectCallback) - qmlRegisterSingletonType('Singletons', 1, 0, 'SingletonQJSValue', singletonQJSValueCallback) + qmlRegisterSingletonType(URI, 1, 0, 'SingletonQJSValue', singletonQJSValueCallback) + + # Accepts only QObject derived types + l = [1, 2] + with self.assertRaises(TypeError): + qmlRegisterSingletonInstance(SingletonQObject, URI, 1, 0, 'SingletonInstance', l) + + # Modify value on the instance + s = SingletonQObject() + s.setData(99) + qmlRegisterSingletonInstance(SingletonQObject, URI, 1, 0, 'SingletonInstance', s) - view = QQuickView() + view = TestQuickView() file = Path(__file__).resolve().parent / 'registersingletontype.qml' self.assertTrue(file.is_file()) view.setSource(QUrl.fromLocalFile(file)) self.assertTrue(view.rootObject(), quickview_errorstring(view)) + view.resize(200, 200) view.show() - QTimer.singleShot(250, view.close) + QTimer.singleShot(250, view.testSlot) app.exec() - self.assertEqual(finalResult, 200) + self.assertEqual(finalResult, 899) + self.assertTrue(view._singleton_instance_qobject_int) + self.assertTrue(view._singleton_instance_qobject_str) + self.assertTrue(view._singleton_instance_jsvalue_int) -if __name__ == '__main__': - unittest.main() +if __name__ == '__main__': unittest.main() |