From 447c377267e44ec5fb02c9efccd367a2b0696447 Mon Sep 17 00:00:00 2001 From: Thomas Hartmann Date: Wed, 23 Oct 2024 17:52:38 +0200 Subject: [PATCH] QmlDesigner: Fix file url for windows Change-Id: Ifb16b62bb4f8fa40240a6c44a65fb1173f11cfff Reviewed-by: Thomas Hartmann --- .../libs/designercore/model/model.cpp | 2 +- .../unit/tests/unittests/model/model-test.cpp | 43 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/plugins/qmldesigner/libs/designercore/model/model.cpp b/src/plugins/qmldesigner/libs/designercore/model/model.cpp index 7dcea5e098a..d9e5bc9deb3 100644 --- a/src/plugins/qmldesigner/libs/designercore/model/model.cpp +++ b/src/plugins/qmldesigner/libs/designercore/model/model.cpp @@ -281,7 +281,7 @@ void ModelPrivate::setFileUrl(const QUrl &fileUrl) if (oldPath != fileUrl) { m_fileUrl = fileUrl; if constexpr (useProjectStorage()) { - auto path = fileUrl.path(); + auto path = fileUrl.toString(QUrl::PreferLocalFile); m_sourceId = pathCache->sourceId(SourcePath{path}); auto found = std::find(path.rbegin(), path.rend(), u'/').base(); m_localPath = Utils::PathString{QStringView{path.begin(), std::prev(found)}}; diff --git a/tests/unit/tests/unittests/model/model-test.cpp b/tests/unit/tests/unittests/model/model-test.cpp index 396206e561c..edd2539e7fe 100644 --- a/tests/unit/tests/unittests/model/model-test.cpp +++ b/tests/unit/tests/unittests/model/model-test.cpp @@ -909,6 +909,32 @@ TEST_F(Model_Imports, change_imports_is_synchronizing_imports_with_project_stora model.changeImports({qtQuickImport, qtQmlModelsImport}, {}); } +TEST_F(Model_Imports, change_imports_with_windows_file_url) +{ + QmlDesigner::SourcePath windowsFilePath = "c:/path/foo.qml"; + QUrl windowsFilePathUrl = windowsFilePath.toQString(); + SourceId windowsSourceId = pathCacheMock.createSourceId(windowsFilePath); + model.setFileUrl(windowsFilePathUrl); + QmlDesigner::SourceId directoryPathId = QmlDesigner::SourceId::create(2); + ON_CALL(pathCacheMock, sourceId(Eq("c:/path/foo/."))).WillByDefault(Return(directoryPathId)); + auto qtQuickModuleId = projectStorageMock.moduleId("QtQuick", ModuleKind::QmlLibrary); + auto qtQmlModelsModuleId = projectStorageMock.moduleId("QtQml.Models", ModuleKind::QmlLibrary); + auto localPathModuleId = projectStorageMock.createModule("c:/path", + QmlDesigner::Storage::ModuleKind::PathLibrary); + auto qtQuickImport = QmlDesigner::Import::createLibraryImport("QtQuick", "2.1"); + auto qtQmlModelsImport = QmlDesigner::Import::createLibraryImport("QtQml.Models"); + auto directoryImport = QmlDesigner::Import::createFileImport("foo"); + + EXPECT_CALL(projectStorageMock, + synchronizeDocumentImports( + UnorderedElementsAre(IsImport(qtQuickModuleId, windowsSourceId, 2, 1), + IsImport(qtQmlModelsModuleId, windowsSourceId, -1, -1), + IsImport(localPathModuleId, windowsSourceId, -1, -1)), + windowsSourceId)); + + model.changeImports({qtQuickImport, qtQmlModelsImport}, {}); +} + TEST_F(Model_Imports, change_imports_is_not_synchronizing_imports_with_project_storage_if_no_new_imports_are_added) { @@ -1366,6 +1392,9 @@ protected: QmlDesigner::SourcePath barFilePath = "/path/bar.qml"; QUrl barFilePathUrl = barFilePath.toQString(); SourceId barSourceId = pathCacheMock.createSourceId(barFilePath); + QmlDesigner::SourcePath windowsFilePath = "c:/path/bar.qml"; + QUrl windowsFilePathUrl = windowsFilePath.toQString(); + SourceId windowsSourceId = pathCacheMock.createSourceId(windowsFilePath); }; TEST_F(Model_FileUrl, set_file_url) @@ -1375,6 +1404,13 @@ TEST_F(Model_FileUrl, set_file_url) ASSERT_THAT(model.fileUrl(), barFilePathUrl); } +TEST_F(Model_FileUrl, set_windows_file_url) +{ + model.setFileUrl(windowsFilePathUrl); + + ASSERT_THAT(model.fileUrl(), windowsFilePathUrl); +} + TEST_F(Model_FileUrl, set_file_url_sets_source_id_too) { model.setFileUrl(barFilePathUrl); @@ -1382,6 +1418,13 @@ TEST_F(Model_FileUrl, set_file_url_sets_source_id_too) ASSERT_THAT(model.fileUrlSourceId(), barSourceId); } +TEST_F(Model_FileUrl, set_windows_file_url_sets_source_id_too) +{ + model.setFileUrl(windowsFilePathUrl); + + ASSERT_THAT(model.fileUrlSourceId(), windowsSourceId); +} + TEST_F(Model_FileUrl, notifies_change) { EXPECT_CALL(viewMock, fileUrlChanged(Eq(fileUrl), Eq(barFilePathUrl)));