| 1 | /* |
| 2 | * Copyright (C) 2021 Damir Porobic <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | #include "RecentImagesPathStoreTests.h" |
| 21 | |
| 22 | #include "src/backend/recentImages/RecentImagesPathStore.h" |
| 23 | |
| 24 | #include "tests/utils/TestRunner.h" |
| 25 | #include "tests/mocks/backend/recentImages/ImagePathStorageMock.h" |
| 26 | |
| 27 | void RecentImagesPathStoreTests::GetRecentImagesPath_Should_ReturnEmptyStringList_When_Initialized() |
| 28 | { |
| 29 | // arrange |
| 30 | auto imagePathStorageMock = QSharedPointer<ImagePathStorageMock>(new ImagePathStorageMock); |
| 31 | |
| 32 | EXPECT_CALL(*imagePathStorageMock, count()); |
| 33 | |
| 34 | RecentImagesPathStore recentImagesPathStore(imagePathStorageMock); |
| 35 | |
| 36 | |
| 37 | // act |
| 38 | auto recentImagesPath = recentImagesPathStore.getRecentImagesPath(); |
| 39 | |
| 40 | // assert |
| 41 | QCOMPARE(recentImagesPath.size(), 0); |
| 42 | } |
| 43 | |
| 44 | void RecentImagesPathStoreTests::GetRecentImagesPath_Should_ReturnNonEmptyStringList_When_ImagesAreStored() |
| 45 | { |
| 46 | // arrange |
| 47 | auto imagePathStorageMock = QSharedPointer<ImagePathStorageMock>(new ImagePathStorageMock); |
| 48 | |
| 49 | EXPECT_CALL(*imagePathStorageMock, count()); |
| 50 | EXPECT_CALL(*imagePathStorageMock, store(testing::_, testing::_)).Times(testing::AnyNumber()); |
| 51 | |
| 52 | RecentImagesPathStore recentImagesPathStore(imagePathStorageMock); |
| 53 | recentImagesPathStore.storeImagePath("/path/image.png"); |
| 54 | recentImagesPathStore.storeImagePath("/path/image2.png"); |
| 55 | |
| 56 | // act |
| 57 | auto recentImagesPath = recentImagesPathStore.getRecentImagesPath(); |
| 58 | |
| 59 | // assert |
| 60 | QCOMPARE(recentImagesPath.size(), 2); |
| 61 | QCOMPARE(recentImagesPath.at(0), QStringLiteral("/path/image2.png")); |
| 62 | QCOMPARE(recentImagesPath.at(1), QStringLiteral("/path/image.png")); |
| 63 | } |
| 64 | |
| 65 | void RecentImagesPathStoreTests::GetRecentImagesPath_Should_ReturnListOfEntriesInReversedOrder() |
| 66 | { |
| 67 | // arrange |
| 68 | auto imagePathStorageMock = QSharedPointer<ImagePathStorageMock>(new ImagePathStorageMock); |
| 69 | |
| 70 | EXPECT_CALL(*imagePathStorageMock, count()); |
| 71 | EXPECT_CALL(*imagePathStorageMock, store(testing::_, testing::_)).Times(testing::AnyNumber());; |
| 72 | |
| 73 | RecentImagesPathStore recentImagesPathStore(imagePathStorageMock); |
| 74 | recentImagesPathStore.storeImagePath("/path/image1.png"); |
| 75 | recentImagesPathStore.storeImagePath("/path/image2.png"); |
| 76 | recentImagesPathStore.storeImagePath("/path/image3.png"); |
| 77 | recentImagesPathStore.storeImagePath("/path/image4.png"); |
| 78 | recentImagesPathStore.storeImagePath("/path/image5.png"); |
| 79 | recentImagesPathStore.storeImagePath("/path/image6.png"); |
| 80 | recentImagesPathStore.storeImagePath("/path/image7.png"); |
| 81 | recentImagesPathStore.storeImagePath("/path/image8.png"); |
| 82 | recentImagesPathStore.storeImagePath("/path/image9.png"); |
| 83 | recentImagesPathStore.storeImagePath("/path/image10.png"); |
| 84 | |
| 85 | // act |
| 86 | auto recentImagesPath = recentImagesPathStore.getRecentImagesPath(); |
| 87 | |
| 88 | // assert |
| 89 | QCOMPARE(recentImagesPath.size(), 10); |
| 90 | QCOMPARE(recentImagesPath[0], QStringLiteral("/path/image10.png")); |
| 91 | QCOMPARE(recentImagesPath[1], QStringLiteral("/path/image9.png")); |
| 92 | QCOMPARE(recentImagesPath[2], QStringLiteral("/path/image8.png")); |
| 93 | QCOMPARE(recentImagesPath[3], QStringLiteral("/path/image7.png")); |
| 94 | QCOMPARE(recentImagesPath[4], QStringLiteral("/path/image6.png")); |
| 95 | QCOMPARE(recentImagesPath[5], QStringLiteral("/path/image5.png")); |
| 96 | QCOMPARE(recentImagesPath[6], QStringLiteral("/path/image4.png")); |
| 97 | QCOMPARE(recentImagesPath[7], QStringLiteral("/path/image3.png")); |
| 98 | QCOMPARE(recentImagesPath[8], QStringLiteral("/path/image2.png")); |
| 99 | QCOMPARE(recentImagesPath[9], QStringLiteral("/path/image1.png")); |
| 100 | } |
| 101 | |
| 102 | void RecentImagesPathStoreTests::StoreImagesPath_Should_NotSavePath_When_PathAlreadyStored() |
| 103 | { |
| 104 | // arrange |
| 105 | auto path1 = QStringLiteral("/path/image.png"); |
| 106 | auto imagePathStorageMock = QSharedPointer<ImagePathStorageMock>(new ImagePathStorageMock); |
| 107 | |
| 108 | EXPECT_CALL(*imagePathStorageMock, count()); |
| 109 | EXPECT_CALL(*imagePathStorageMock, store(path1, 0)).Times(testing::Exactly(1)); |
| 110 | |
| 111 | RecentImagesPathStore recentImagesPathStore(imagePathStorageMock); |
| 112 | |
| 113 | // act |
| 114 | recentImagesPathStore.storeImagePath(path1); |
| 115 | recentImagesPathStore.storeImagePath(path1); |
| 116 | |
| 117 | // assert |
| 118 | auto recentImagesPath = recentImagesPathStore.getRecentImagesPath(); |
| 119 | QCOMPARE(recentImagesPath.size(), 1); |
| 120 | } |
| 121 | |
| 122 | void RecentImagesPathStoreTests::StoreImagesPath_Should_DropOlderPaths_When_MoreThenTenPathsAdded() |
| 123 | { |
| 124 | // arrange |
| 125 | auto imagePathStorageMock = QSharedPointer<ImagePathStorageMock>(new ImagePathStorageMock); |
| 126 | |
| 127 | EXPECT_CALL(*imagePathStorageMock, count()); |
| 128 | EXPECT_CALL(*imagePathStorageMock, store(testing::_, testing::_)).Times(testing::AnyNumber());; |
| 129 | |
| 130 | RecentImagesPathStore recentImagesPathStore(imagePathStorageMock); |
| 131 | recentImagesPathStore.storeImagePath("/path/image1.png"); |
| 132 | recentImagesPathStore.storeImagePath("/path/image2.png"); |
| 133 | recentImagesPathStore.storeImagePath("/path/image3.png"); |
| 134 | recentImagesPathStore.storeImagePath("/path/image4.png"); |
| 135 | recentImagesPathStore.storeImagePath("/path/image5.png"); |
| 136 | recentImagesPathStore.storeImagePath("/path/image6.png"); |
| 137 | recentImagesPathStore.storeImagePath("/path/image7.png"); |
| 138 | recentImagesPathStore.storeImagePath("/path/image8.png"); |
| 139 | recentImagesPathStore.storeImagePath("/path/image9.png"); |
| 140 | recentImagesPathStore.storeImagePath("/path/image10.png"); |
| 141 | |
| 142 | // act |
| 143 | recentImagesPathStore.storeImagePath("/path/image11.png"); |
| 144 | recentImagesPathStore.storeImagePath("/path/image12.png"); |
| 145 | |
| 146 | // assert |
| 147 | auto recentImagesPath = recentImagesPathStore.getRecentImagesPath(); |
| 148 | QCOMPARE(recentImagesPath.size(), 10); |
| 149 | QCOMPARE(recentImagesPath[0], QStringLiteral("/path/image12.png")); |
| 150 | QCOMPARE(recentImagesPath[1], QStringLiteral("/path/image11.png")); |
| 151 | QCOMPARE(recentImagesPath[2], QStringLiteral("/path/image10.png")); |
| 152 | QCOMPARE(recentImagesPath[3], QStringLiteral("/path/image9.png")); |
| 153 | QCOMPARE(recentImagesPath[4], QStringLiteral("/path/image8.png")); |
| 154 | QCOMPARE(recentImagesPath[5], QStringLiteral("/path/image7.png")); |
| 155 | QCOMPARE(recentImagesPath[6], QStringLiteral("/path/image6.png")); |
| 156 | QCOMPARE(recentImagesPath[7], QStringLiteral("/path/image5.png")); |
| 157 | QCOMPARE(recentImagesPath[8], QStringLiteral("/path/image4.png")); |
| 158 | QCOMPARE(recentImagesPath[9], QStringLiteral("/path/image3.png")); |
| 159 | } |
| 160 | |
| 161 | TEST_MAIN(RecentImagesPathStoreTests) |