// Copyright (C) 2021 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 #include "googletest.h" #include "imagecachecollectormock.h" #include "mockimagecachestorage.h" #include "mocktimestampprovider.h" #include namespace { MATCHER_P(IsIcon, icon, std::string(negation ? "isn't " : "is ") + PrintToString(icon)) { const QIcon &other = arg; return icon.availableSizes() == other.availableSizes(); } class SynchronousImageCache : public testing::Test { protected: SynchronousImageCache() { ON_CALL(mockTimeStampProvider, timeStamp(Eq("/path/to/Component.qml"))) .WillByDefault(Return(Sqlite::TimeStamp{123})); ON_CALL(mockStorage, fetchImage(Eq("/path/to/Component.qml"), Eq(Sqlite::TimeStamp{123}))) .WillByDefault(Return(QmlDesigner::ImageCacheStorageInterface::ImageEntry{image1})); ON_CALL(mockStorage, fetchImage(Eq("/path/to/Component.qml+extraId1"), Eq(Sqlite::TimeStamp{123}))) .WillByDefault(Return(QmlDesigner::ImageCacheStorageInterface::ImageEntry{image2})); ON_CALL(mockStorage, fetchMidSizeImage(Eq("/path/to/Component.qml"), Eq(Sqlite::TimeStamp{123}))) .WillByDefault(Return(QmlDesigner::ImageCacheStorageInterface::ImageEntry{midSizeImage1})); ON_CALL(mockStorage, fetchSmallImage(Eq("/path/to/Component.qml"), Eq(Sqlite::TimeStamp{123}))) .WillByDefault(Return(QmlDesigner::ImageCacheStorageInterface::ImageEntry{smallImage1})); ON_CALL(mockStorage, fetchMidSizeImage(Eq("/path/to/Component.qml+extraId1"), Eq(Sqlite::TimeStamp{123}))) .WillByDefault(Return(QmlDesigner::ImageCacheStorageInterface::ImageEntry{midSizeImage2})); ON_CALL(mockStorage, fetchSmallImage(Eq("/path/to/Component.qml+extraId1"), Eq(Sqlite::TimeStamp{123}))) .WillByDefault(Return(QmlDesigner::ImageCacheStorageInterface::ImageEntry{smallImage2})); ON_CALL(mockStorage, fetchIcon(Eq("/path/to/Component.qml"), Eq(Sqlite::TimeStamp{123}))) .WillByDefault(Return(QmlDesigner::ImageCacheStorageInterface::IconEntry{icon1})); ON_CALL(mockStorage, fetchIcon(Eq("/path/to/Component.qml+extraId1"), Eq(Sqlite::TimeStamp{123}))) .WillByDefault(Return(QmlDesigner::ImageCacheStorageInterface::IconEntry{icon2})); ON_CALL(mockCollector, createImage(Eq("/path/to/Component.qml"), Eq("extraId1"), _)) .WillByDefault(Return(std::make_tuple(image3, midSizeImage3, smallImage3))); ON_CALL(mockCollector, createIcon(Eq("/path/to/Component.qml"), Eq("extraId1"), _)) .WillByDefault(Return(icon3)); } protected: NiceMock> mockCaptureCallback; NiceMock mockStorage; NiceMock mockCollector; NiceMock mockTimeStampProvider; QmlDesigner::SynchronousImageCache cache{mockStorage, mockTimeStampProvider, mockCollector}; QImage image1{1, 1, QImage::Format_ARGB32}; QImage image2{1, 2, QImage::Format_ARGB32}; QImage image3{1, 3, QImage::Format_ARGB32}; QImage midSizeImage1{2, 1, QImage::Format_ARGB32}; QImage midSizeImage2{2, 2, QImage::Format_ARGB32}; QImage midSizeImage3{2, 3, QImage::Format_ARGB32}; QImage smallImage1{3, 1, QImage::Format_ARGB32}; QImage smallImage2{3, 1, QImage::Format_ARGB32}; QImage smallImage3{3, 1, QImage::Format_ARGB32}; QIcon icon1{QPixmap::fromImage(image1)}; QIcon icon2{QPixmap::fromImage(image2)}; QIcon icon3{QPixmap::fromImage(image3)}; }; TEST_F(SynchronousImageCache, GetImageFromStorage) { auto image = cache.image("/path/to/Component.qml"); ASSERT_THAT(image, image1); } TEST_F(SynchronousImageCache, GetImageWithExtraIdFromStorage) { auto image = cache.image("/path/to/Component.qml", "extraId1"); ASSERT_THAT(image, image2); } TEST_F(SynchronousImageCache, GetImageWithOutdatedTimeStampFromCollector) { ON_CALL(mockTimeStampProvider, timeStamp(Eq("/path/to/Component.qml"))) .WillByDefault(Return(Sqlite::TimeStamp{124})); auto image = cache.image("/path/to/Component.qml", "extraId1"); ASSERT_THAT(image, image3); } TEST_F(SynchronousImageCache, GetImageWithOutdatedTimeStampStored) { ON_CALL(mockTimeStampProvider, timeStamp(Eq("/path/to/Component.qml"))) .WillByDefault(Return(Sqlite::TimeStamp{124})); EXPECT_CALL(mockStorage, storeImage(Eq("/path/to/Component.qml+extraId1"), Eq(Sqlite::TimeStamp{124}), Eq(image3), Eq(midSizeImage3), Eq(smallImage3))); auto image = cache.image("/path/to/Component.qml", "extraId1"); } TEST_F(SynchronousImageCache, GetMidSizeImageFromStorage) { auto image = cache.midSizeImage("/path/to/Component.qml"); ASSERT_THAT(image, midSizeImage1); } TEST_F(SynchronousImageCache, GetMidSizeImageWithExtraIdFromStorage) { auto image = cache.midSizeImage("/path/to/Component.qml", "extraId1"); ASSERT_THAT(image, midSizeImage2); } TEST_F(SynchronousImageCache, GetMidSizeImageWithOutdatedTimeStampFromCollector) { ON_CALL(mockTimeStampProvider, timeStamp(Eq("/path/to/Component.qml"))) .WillByDefault(Return(Sqlite::TimeStamp{124})); auto image = cache.midSizeImage("/path/to/Component.qml", "extraId1"); ASSERT_THAT(image, midSizeImage3); } TEST_F(SynchronousImageCache, GetMidSizeImageWithOutdatedTimeStampStored) { ON_CALL(mockTimeStampProvider, timeStamp(Eq("/path/to/Component.qml"))) .WillByDefault(Return(Sqlite::TimeStamp{124})); EXPECT_CALL(mockStorage, storeImage(Eq("/path/to/Component.qml+extraId1"), Eq(Sqlite::TimeStamp{124}), Eq(image3), Eq(midSizeImage3), Eq(smallImage3))); auto image = cache.midSizeImage("/path/to/Component.qml", "extraId1"); } TEST_F(SynchronousImageCache, GetSmallImageFromStorage) { auto image = cache.smallImage("/path/to/Component.qml"); ASSERT_THAT(image, smallImage1); } TEST_F(SynchronousImageCache, GetSmallImageWithExtraIdFromStorage) { auto image = cache.smallImage("/path/to/Component.qml", "extraId1"); ASSERT_THAT(image, smallImage2); } TEST_F(SynchronousImageCache, GetSmallImageWithOutdatedTimeStampFromCollector) { ON_CALL(mockTimeStampProvider, timeStamp(Eq("/path/to/Component.qml"))) .WillByDefault(Return(Sqlite::TimeStamp{124})); auto image = cache.smallImage("/path/to/Component.qml", "extraId1"); ASSERT_THAT(image, smallImage3); } TEST_F(SynchronousImageCache, GetSmallImageWithOutdatedTimeStampStored) { ON_CALL(mockTimeStampProvider, timeStamp(Eq("/path/to/Component.qml"))) .WillByDefault(Return(Sqlite::TimeStamp{124})); EXPECT_CALL(mockStorage, storeImage(Eq("/path/to/Component.qml+extraId1"), Eq(Sqlite::TimeStamp{124}), Eq(image3), Eq(midSizeImage3), Eq(smallImage3))); auto image = cache.smallImage("/path/to/Component.qml", "extraId1"); } TEST_F(SynchronousImageCache, GetIconFromStorage) { auto icon = cache.icon("/path/to/Component.qml"); ASSERT_THAT(icon, IsIcon(icon1)); } TEST_F(SynchronousImageCache, GetIconWithExtraIdFromStorage) { auto icon = cache.icon("/path/to/Component.qml", "extraId1"); ASSERT_THAT(icon, IsIcon(icon2)); } TEST_F(SynchronousImageCache, GetIconWithOutdatedTimeStampFromCollector) { ON_CALL(mockTimeStampProvider, timeStamp(Eq("/path/to/Component.qml"))) .WillByDefault(Return(Sqlite::TimeStamp{124})); auto icon = cache.icon("/path/to/Component.qml", "extraId1"); ASSERT_THAT(icon, IsIcon(icon3)); } TEST_F(SynchronousImageCache, GetIconWithOutdatedTimeStampStored) { using QmlDesigner::ImageCache::FontCollectorSizesAuxiliaryData; std::vector sizes{{20, 11}}; ON_CALL(mockTimeStampProvider, timeStamp(Eq("/path/to/Component.qml"))) .WillByDefault(Return(Sqlite::TimeStamp{124})); EXPECT_CALL(mockStorage, storeIcon(Eq("/path/to/Component.qml+extraId1"), Eq(Sqlite::TimeStamp{124}), IsIcon(icon3))); auto icon = cache.icon("/path/to/Component.qml", "extraId1", FontCollectorSizesAuxiliaryData{sizes, "color", "text"}); } TEST_F(SynchronousImageCache, IconCallsCollectorWithAuxiliaryData) { using QmlDesigner::ImageCache::FontCollectorSizesAuxiliaryData; std::vector sizes{{20, 11}}; ON_CALL(mockTimeStampProvider, timeStamp(Eq("/path/to/Component.qml"))) .WillByDefault(Return(Sqlite::TimeStamp{124})); EXPECT_CALL(mockCollector, createIcon(Eq("/path/to/Component.qml"), Eq("extraId1"), VariantWith( AllOf(Field(&FontCollectorSizesAuxiliaryData::sizes, ElementsAre(QSize{20, 11})), Field(&FontCollectorSizesAuxiliaryData::colorName, Eq(u"color")))))); auto icon = cache.icon("/path/to/Component.qml", "extraId1", FontCollectorSizesAuxiliaryData{sizes, "color", "text"}); } TEST_F(SynchronousImageCache, ImageCallsCollectorWithAuxiliaryData) { using QmlDesigner::ImageCache::FontCollectorSizesAuxiliaryData; std::vector sizes{{20, 11}}; ON_CALL(mockTimeStampProvider, timeStamp(Eq("/path/to/Component.qml"))) .WillByDefault(Return(Sqlite::TimeStamp{124})); EXPECT_CALL(mockCollector, createImage(Eq("/path/to/Component.qml"), Eq("extraId1"), VariantWith( AllOf(Field(&FontCollectorSizesAuxiliaryData::sizes, ElementsAre(QSize{20, 11})), Field(&FontCollectorSizesAuxiliaryData::colorName, Eq(u"color")))))); auto icon = cache.image("/path/to/Component.qml", "extraId1", FontCollectorSizesAuxiliaryData{sizes, "color", "text"}); } TEST_F(SynchronousImageCache, SmallImageCallsCollectorWithAuxiliaryData) { using QmlDesigner::ImageCache::FontCollectorSizesAuxiliaryData; std::vector sizes{{20, 11}}; ON_CALL(mockTimeStampProvider, timeStamp(Eq("/path/to/Component.qml"))) .WillByDefault(Return(Sqlite::TimeStamp{124})); EXPECT_CALL(mockCollector, createImage(Eq("/path/to/Component.qml"), Eq("extraId1"), VariantWith( AllOf(Field(&FontCollectorSizesAuxiliaryData::sizes, ElementsAre(QSize{20, 11})), Field(&FontCollectorSizesAuxiliaryData::colorName, Eq(u"color")))))); auto icon = cache.smallImage("/path/to/Component.qml", "extraId1", FontCollectorSizesAuxiliaryData{sizes, "color", "text"}); } } // namespace