From b2e1bcc4e5a5fa9a9db51986d55930c92428b1f3 Mon Sep 17 00:00:00 2001 From: Marco Bubke Date: Thu, 6 Jan 2022 12:00:45 +0100 Subject: [PATCH] QmlDesigner: Add fetchHasImage to ImageCacheStorage Change-Id: Ieb148abb02527bb84eda4f00af85a66d98731f03 Reviewed-by: Qt CI Bot Reviewed-by: Thomas Hartmann --- src/libs/sqlite/sqlitebasestatement.h | 2 -- .../imagecache/imagecachestorage.h | 7 +++++ .../imagecache/imagecachestorageinterface.h | 1 + .../unit/unittest/imagecachestorage-test.cpp | 27 +++++++++++++++++++ tests/unit/unittest/mockimagecachestorage.h | 1 + tests/unit/unittest/sqlitereadstatementmock.h | 3 +++ 6 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/libs/sqlite/sqlitebasestatement.h b/src/libs/sqlite/sqlitebasestatement.h index ef098d42244..20b06c59af3 100644 --- a/src/libs/sqlite/sqlitebasestatement.h +++ b/src/libs/sqlite/sqlitebasestatement.h @@ -210,8 +210,6 @@ public: template auto value(const QueryTypes &...queryValues) { - static_assert(!std::is_fundamental_v, - "Use optionalValue(...) instead of value(...) for fundamental types!"); Resetter resetter{this}; ResultType resultValue{}; diff --git a/src/plugins/qmldesigner/designercore/imagecache/imagecachestorage.h b/src/plugins/qmldesigner/designercore/imagecache/imagecachestorage.h index c630a5c4775..e5112719d1a 100644 --- a/src/plugins/qmldesigner/designercore/imagecache/imagecachestorage.h +++ b/src/plugins/qmldesigner/designercore/imagecache/imagecachestorage.h @@ -165,6 +165,11 @@ public: return selectModifiedImageTimeStatement.template valueWithTransaction(name); } + bool fetchHasImage(Utils::SmallStringView name) const override + { + return selectHasImageStatement.template valueWithTransaction(name); + } + private: class Initializer { @@ -296,6 +301,8 @@ public: database}; mutable ReadStatement<1, 1> selectModifiedImageTimeStatement{ "SELECT mtime FROM images WHERE name=?1", database}; + mutable ReadStatement<1, 1> selectHasImageStatement{ + "SELECT image IS NOT NULL FROM images WHERE name=?1", database}; }; } // namespace QmlDesigner diff --git a/src/plugins/qmldesigner/designercore/imagecache/imagecachestorageinterface.h b/src/plugins/qmldesigner/designercore/imagecache/imagecachestorageinterface.h index 80ade8da03f..67b194cba55 100644 --- a/src/plugins/qmldesigner/designercore/imagecache/imagecachestorageinterface.h +++ b/src/plugins/qmldesigner/designercore/imagecache/imagecachestorageinterface.h @@ -53,6 +53,7 @@ public: virtual void storeIcon(Utils::SmallStringView name, Sqlite::TimeStamp newTimeStamp, const QIcon &icon) = 0; virtual void walCheckpointFull() = 0; virtual Sqlite::TimeStamp fetchModifiedImageTime(Utils::SmallStringView name) const = 0; + virtual bool fetchHasImage(Utils::SmallStringView name) const = 0; protected: ~ImageCacheStorageInterface() = default; diff --git a/tests/unit/unittest/imagecachestorage-test.cpp b/tests/unit/unittest/imagecachestorage-test.cpp index 285177c4276..f544432aee6 100644 --- a/tests/unit/unittest/imagecachestorage-test.cpp +++ b/tests/unit/unittest/imagecachestorage-test.cpp @@ -444,4 +444,31 @@ TEST_F(ImageCacheStorageSlowTest, FetchInvalidModifiedImageTimeForNoEntry) ASSERT_THAT(timeStamp, Eq(Sqlite::TimeStamp{})); } + +TEST_F(ImageCacheStorageSlowTest, FetchHasImage) +{ + storage.storeImage("/path/to/component", {123}, image1, smallImage1); + + auto hasImage = storage.fetchHasImage("/path/to/component"); + + ASSERT_TRUE(hasImage); +} + +TEST_F(ImageCacheStorageSlowTest, FetchHasImageForNullImage) +{ + storage.storeImage("/path/to/component", {123}, QImage{}, QImage{}); + + auto hasImage = storage.fetchHasImage("/path/to/component"); + + ASSERT_FALSE(hasImage); +} + +TEST_F(ImageCacheStorageSlowTest, FetchHasImageForNoEntry) +{ + storage.storeImage("/path/to/component", {123}, QImage{}, QImage{}); + + auto hasImage = storage.fetchHasImage("/path/to/component"); + + ASSERT_FALSE(hasImage); +} } // namespace diff --git a/tests/unit/unittest/mockimagecachestorage.h b/tests/unit/unittest/mockimagecachestorage.h index 50808934066..479cd1cfa26 100644 --- a/tests/unit/unittest/mockimagecachestorage.h +++ b/tests/unit/unittest/mockimagecachestorage.h @@ -65,4 +65,5 @@ public: fetchModifiedImageTime, (Utils::SmallStringView name), (const, override)); + MOCK_METHOD(bool, fetchHasImage, (Utils::SmallStringView name), (const, override)); }; diff --git a/tests/unit/unittest/sqlitereadstatementmock.h b/tests/unit/unittest/sqlitereadstatementmock.h index 29540d95daa..18b976c198c 100644 --- a/tests/unit/unittest/sqlitereadstatementmock.h +++ b/tests/unit/unittest/sqlitereadstatementmock.h @@ -98,6 +98,7 @@ public: (int) ); MOCK_METHOD(Sqlite::TimeStamp, valueWithTransactionReturnsTimeStamp, (Utils::SmallStringView), ()); + MOCK_METHOD(int, valueWithTransactionReturnsInt, (Utils::SmallStringView), ()); MOCK_METHOD(QmlDesigner::SourceContextId, valueReturnsSourceContextId, (Utils::SmallStringView), ()); MOCK_METHOD(QmlDesigner::SourceContextId, valueWithTransactionReturnsSourceContextId, (int), ()); @@ -206,6 +207,8 @@ public: return valueWithTransactionReturnsSourceContextId(queryValues...); else if constexpr (std::is_same_v) return valueWithTransactionReturnsTimeStamp(queryValues...); + else if constexpr (std::is_same_v) + return valueWithTransactionReturnsInt(queryValues...); else static_assert(!std::is_same_v, "SqliteReadStatementMock::value does not handle result type!");