diff options
| author | Karsten Heimrich <karsten.heimrich@qt.io> | 2025-12-01 13:19:49 +0100 |
|---|---|---|
| committer | Miguel Costa <miguel.costa@qt.io> | 2025-12-01 18:09:25 +0000 |
| commit | ddb96a69eafadd8834a7c20db6ef2c52de26a9d3 (patch) | |
| tree | a729a5c4da4703a9a232e5d71223937f844a9cbf | |
| parent | 2900226485ab15db899783a8c2a50dbde55d1436 (diff) | |
Tests: Expose complex user-defined types to QML
Change-Id: I8882068d8711f596817bce2d5a71cd6ccf4a49c3
Reviewed-by: Miguel Costa <miguel.costa@qt.io>
5 files changed, 216 insertions, 0 deletions
diff --git a/tests/Test_Qt.DotNet.Project/StructuredData/Program.cs b/tests/Test_Qt.DotNet.Project/StructuredData/Program.cs new file mode 100644 index 0000000..c3d03c1 --- /dev/null +++ b/tests/Test_Qt.DotNet.Project/StructuredData/Program.cs @@ -0,0 +1,75 @@ +/*************************************************************************************************** + Copyright (C) 2025 The Qt Company Ltd. + SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only +***************************************************************************************************/ + +using System; +using System.Collections.Generic; + +using Qt.Quick; + +[assembly: Qt.Generate(Packages = "QuickTest", Libraries = "Qt6::QuickTest")] + +namespace Test_StructuredData +{ + internal class Program + { + static int Main(string[] args) + { + Qml.WaitForExit(); + return 0; + } + } + + public sealed class Person + { + public int Age { get; set; } + public string Name { get; set; } = ""; + } + + public sealed class Team + { + public string Name { get; set; } = ""; + public List<Person> Members { get; set; } = new List<Person>(); + } + + public class StructuredData + { + public Person LastPerson { get; private set; } = + new() { Name = "Alice", Age = 42 }; + + public Team LastTeam { get; private set; } = + new() + { + Name = "Awesome Team", + Members = new List<Person> + { + new Person { Name = "Alice", Age = 42 }, + new Person { Name = "Bob", Age = 21 }, + } + }; + + // Host -> QML + public Person CreateSamplePerson() => + new() { Name = "Alice", Age = 42 }; + + public Team CreateSampleTeam() + { + var t = new Team + { + Name = "Awesome Team", + Members = new List<Person> + { + new Person { Name = "Alice", Age = 42 }, + new Person { Name = "Bob", Age = 21 } + } + }; + return t; + } + + // QML -> Host + public void AcceptTeam(Team team) => LastTeam = team; + public void AcceptPerson(Person person) => LastPerson = person; + } +} + diff --git a/tests/Test_Qt.DotNet.Project/StructuredData/Test_StructuredData.cs b/tests/Test_Qt.DotNet.Project/StructuredData/Test_StructuredData.cs new file mode 100644 index 0000000..440e2a4 --- /dev/null +++ b/tests/Test_Qt.DotNet.Project/StructuredData/Test_StructuredData.cs @@ -0,0 +1,40 @@ +/*************************************************************************************************** + Copyright (C) 2025 The Qt Company Ltd. + SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only +***************************************************************************************************/ + +using System.IO; +using Test_Qt.DotNet.Project.Shared; + +namespace Test_Qt.DotNet.Project.StructuredData +{ + [TestClass] + public class Test_StructuredData : ManagedTestBase + { + [TestMethod] + public async Task StructuredData_QmlRoundtrip_Works() + { + using var temp = new TempProject(); + + var options = CreateQtQuickTestOptions(@"StructuredData\main.cpp"); + await InitializeAndBuildAsync(temp, options, + project => { + project.CopyFile("Program.cs", @"StructuredData\Program.cs"); + project.CopyFile("tst_structureddata.qml", @"StructuredData\tst_structureddata.qml"); + }); + + var run = await temp.RunAsync(new() { + Args = ["-input", Path.Combine(temp.ExeDir, "Application", "tst_structureddata.qml")], + EnvVars = [("QT_FORCE_STDERR_LOGGING", "1")], + StdErr = Redirect.StdOut + }); + + Assert.IsLessThanOrEqualTo((int)ExitCode.QTestFailure, run.ExitCode, + ExitCodeHelper.ToString(run.ExitCode)); + + var passPrefix = "PASS : Test_StructuredData::tst_structureddata::"; + Assert.Contains(passPrefix + "test_person_roundtrip()", run.StdOut); + Assert.Contains(passPrefix + "test_team_roundtrip()", run.StdOut); + } + } +} diff --git a/tests/Test_Qt.DotNet.Project/StructuredData/main.cpp b/tests/Test_Qt.DotNet.Project/StructuredData/main.cpp new file mode 100644 index 0000000..51f39c4 --- /dev/null +++ b/tests/Test_Qt.DotNet.Project/StructuredData/main.cpp @@ -0,0 +1,19 @@ +/*************************************************************************************************** + Copyright (C) 2025 The Qt Company Ltd. + SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only +***************************************************************************************************/ + +#include <QCoreApplication> +#include <QDebug> +#include <QQmlEngine> +#include <QtQuickTest> + +#include "QtQuickTestSetup.h" + +class Setup : public QtQuickTestSetup +{ + Q_OBJECT +}; + +QUICK_TEST_MAIN_WITH_SETUP(Test_StructuredData, Setup) +#include "main.moc" diff --git a/tests/Test_Qt.DotNet.Project/StructuredData/tst_structureddata.qml b/tests/Test_Qt.DotNet.Project/StructuredData/tst_structureddata.qml new file mode 100644 index 0000000..b569aa9 --- /dev/null +++ b/tests/Test_Qt.DotNet.Project/StructuredData/tst_structureddata.qml @@ -0,0 +1,70 @@ +/*************************************************************************************************** + Copyright (C) 2025 The Qt Company Ltd. + SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only +***************************************************************************************************/ + +import QtQuick +import QtTest +import Application + +TestCase { + name: "tst_structureddata" + + StructuredData { + id: data + } + + // simple roundtrip, single person + function test_person_roundtrip() + { + // Host -> QML + var p = data.createSamplePerson() + compare(p.name, "Alice") + compare(p.age, 42) + + // Mutation in QML + p.name = "Bob" + p.age = 21 + + // QML -> Host + data.acceptPerson(p) + + // Host -> QML (Roundtrip) + var back = data.lastPerson + compare(back.name, "Bob") + compare(back.age, 21) + } + + // roundtrip with nested structure (Team + Member list) + function test_team_roundtrip() + { + // Host -> QML + var intitialTeam = data.createSampleTeam() + compare(intitialTeam.name, "Awesome Team") + + var intitialMembers = intitialTeam.members; + compare(intitialMembers.count, 2) + compare(intitialMembers.item(0).name, "Alice") + compare(intitialMembers.item(1).name, "Bob") + + // Mutation in QML -> Host + var newPerson = Qt.createQmlObject( + 'import Application 1.0; Person { name: "Eve"; age: 60 }', + this + ) + intitialTeam.name = "Renamed Team" + intitialMembers.add(newPerson) + data.acceptTeam(intitialTeam) + + // Host -> QML (Roundtrip) + var updatedTeam = data.lastTeam + compare(updatedTeam.name, "Renamed Team") + + var updatedMembers = updatedTeam.members; + compare(updatedMembers.count, 3) + compare(updatedMembers.item(0).name, "Alice") + compare(updatedMembers.item(1).name, "Bob") + compare(updatedMembers.item(2).name, "Eve") + compare(updatedMembers.item(2).age, 60) + } +} diff --git a/tests/Test_Qt.DotNet.Project/Test_Qt.DotNet.Project.csproj b/tests/Test_Qt.DotNet.Project/Test_Qt.DotNet.Project.csproj index d80453b..41fafcb 100644 --- a/tests/Test_Qt.DotNet.Project/Test_Qt.DotNet.Project.csproj +++ b/tests/Test_Qt.DotNet.Project/Test_Qt.DotNet.Project.csproj @@ -19,8 +19,10 @@ <ItemGroup> <Compile Remove="QtQuickTest\Program.cs" /> <Compile Remove="QtTest\Program.cs" /> + <Compile Remove="StructuredData\Program.cs" /> <None Remove="QtQuickTest\main.cpp" /> <None Remove="QtTest\main.cpp" /> + <None Remove="StructuredData\main.cpp" /> </ItemGroup> <ItemGroup> @@ -48,6 +50,16 @@ <Content Include="Shared\QtQuickTestSetup.cpp"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content> + + <Content Include="StructuredData\main.cpp"> + <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> + </Content> + <Content Include="StructuredData\tst_structureddata.qml"> + <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> + </Content> + <Content Include="StructuredData\Program.cs"> + <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> + </Content> </ItemGroup> <ItemGroup> |
