From bab7025b02eb4647d6a87992d7b10eb1dd3d0a09 Mon Sep 17 00:00:00 2001 From: Miikka Heikkinen Date: Mon, 3 Feb 2025 15:20:41 +0200 Subject: [PATCH] QmlDesigner: Cast string property values to urls in PropertyMetaInfo PropertyMetaInfo::castedValue() now casts string values to urls properly. This fixes UrlChooser controls in property view. Adjusted the unit tests accordingly. Fixes: QDS-14663 Change-Id: I2c3a47bcedb8b19af03a49a67b31d43d29e63b7e Reviewed-by: Marco Bubke --- .../libs/designercore/metainfo/nodemetainfo.cpp | 2 ++ .../metainfo/propertymetainfo-test.cpp | 17 +++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/plugins/qmldesigner/libs/designercore/metainfo/nodemetainfo.cpp b/src/plugins/qmldesigner/libs/designercore/metainfo/nodemetainfo.cpp index 1f99fd20302..7e1bb11f846 100644 --- a/src/plugins/qmldesigner/libs/designercore/metainfo/nodemetainfo.cpp +++ b/src/plugins/qmldesigner/libs/designercore/metainfo/nodemetainfo.cpp @@ -4600,6 +4600,8 @@ QVariant PropertyMetaInfo::castedValue(const QVariant &value) const } else if (typeId == m_projectStorage->builtinTypeId()) { if (isType(value.metaType(), qUrlType)) return value; + else if (isType(value.metaType(), qStringType)) + return value.toUrl(); else return QUrl{}; } else if (typeId == m_projectStorage->builtinTypeId()) { diff --git a/tests/unit/tests/unittests/metainfo/propertymetainfo-test.cpp b/tests/unit/tests/unittests/metainfo/propertymetainfo-test.cpp index d78c9ee8739..b3e466de06c 100644 --- a/tests/unit/tests/unittests/metainfo/propertymetainfo-test.cpp +++ b/tests/unit/tests/unittests/metainfo/propertymetainfo-test.cpp @@ -689,16 +689,29 @@ TEST_F(PropertyMetaInfo, cast_url_to_url) ASSERT_THAT(castedValue, IsQVariant(url)); } -TEST_F(PropertyMetaInfo, cast_string_to_empty_url) +TEST_F(PropertyMetaInfo, cast_empty_string_to_empty_url) +{ + auto propertyTypeInfo = createNodeMetaInfo("QML", ModuleKind::QmlLibrary, "url", {}); + projectStorageMock.createProperty(nodeInfo.id(), "bar", {}, propertyTypeInfo.id()); + auto propertyInfo = nodeInfo.property("bar"); + auto value = QVariant::fromValue(QString{}); + + auto castedValue = propertyInfo.castedValue(value); + + ASSERT_THAT(castedValue, IsQVariant(IsEmpty())); +} + +TEST_F(PropertyMetaInfo, cast_string_to_url) { auto propertyTypeInfo = createNodeMetaInfo("QML", ModuleKind::QmlLibrary, "url", {}); projectStorageMock.createProperty(nodeInfo.id(), "bar", {}, propertyTypeInfo.id()); auto propertyInfo = nodeInfo.property("bar"); auto value = QVariant::fromValue(QString{"http://www.qt.io/future"}); + auto url = QUrl{"http://www.qt.io/future"}; auto castedValue = propertyInfo.castedValue(value); - ASSERT_THAT(castedValue, IsQVariant(IsEmpty())); + ASSERT_THAT(castedValue, IsQVariant(url)); } TEST_F(PropertyMetaInfo, cast_default_to_empty_url)