Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: add qmlloader fixture #474

Open
nrbnlulu opened this issue Feb 1, 2023 · 6 comments · May be fixed by #476
Open

Proposal: add qmlloader fixture #474

nrbnlulu opened this issue Feb 1, 2023 · 6 comments · May be fixed by #476

Comments

@nrbnlulu
Copy link

nrbnlulu commented Feb 1, 2023

Hi I created this nice fixture and wanted to ask if it would be usefull to contribute it here?
It supports loading QML from strings or from file and retrieving the loaded item with ease.

T = TypeVar("T")

@define(slots=False)
class QmlTestCase:
    bot: QtBot
    engine: QQmlApplicationEngine = field(factory=QQmlApplicationEngine)

    def __attrs_post_init__(self):
        main = Path(__file__).parent / "qmltester.qml"
        self.engine.load(main.resolve(True))

    @property
    def _loader(self) -> QQuickItem:
        self.root = self.engine.rootObjects()[0]
        return self.root.findChild(QQuickItem, "contentloader")

    def load(self, path: Path) -> QQuickItem:
        self.bot.wait(100)
        self._loader.setProperty("source", str(path.resolve(True)))
        return self._loader.property("item")

    def loads(self, content: str) -> QQuickItem:
        self.comp = QQmlComponent(self.engine)
        self.comp.setData(content.encode("utf-8"), QUrl())
        self._loader.setProperty("source", "")
        self._loader.setProperty("sourceComponent", self.comp)
        return self._loader.property("item")

    def find(self, objectname: str, type: T = QQuickItem) -> T:
        return self.window.findChild(type, objectname)


@pytest.fixture()
def qmlloader(qtbot):
    return QmlTestCase(qtbot)

Usage:

@pytest.mark.parametrize("status", iter(object_with_enum.Status))
def test_accessible_from_qml(qmlloader, status):
    qml = (
        """
import QtQuick
import QtGql 1.0 as GQL

Rectangle {
    property int enumValue: GQL.Enums.%s
}
"""
        % status.name
    )

    EnumTestCase.compile()
    item = qmlloader.loads(qml)
    assert item.property("enumValue") == status.value
@nrbnlulu nrbnlulu changed the title Proposal: add qmltester fixture Proposal: add qmlloader fixture Feb 1, 2023
@nicoddemus
Copy link
Member

That's interesting!

I have never used QtQuick myself, but this looks like a welcome addition to pytest-qt.

I think we should name it qmlbot (or something like this) in case we want to add more helper functions to it. Other than that, this would be a great contribution.

@nrbnlulu
Copy link
Author

nrbnlulu commented Feb 1, 2023

BTW why not use qtpy to manage the diffrent bindings?

@nicoddemus
Copy link
Member

Did not know about it when pytest-qt was created, never bothered to depend on qtpy.

@nrbnlulu
Copy link
Author

nrbnlulu commented Feb 1, 2023

They support type hints, I would suggest to migrate to qtpy, I can help if you want

@nrbnlulu nrbnlulu linked a pull request Feb 1, 2023 that will close this issue
1 task
@nicoddemus
Copy link
Member

They support type hints, I would suggest to migrate to qtpy, I can help if you want

Not against it, however we need to keep the existing support of selecting backends via environment variables (did not look into qtpy to see how to accomplish that).

@nicoddemus
Copy link
Member

Created #477 to track this. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants