| 1 | /* |
| 2 | * Copyright (C) 2020 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 "LoadImageFromFileOperationTests.h" |
| 21 | |
| 22 | #include "src/gui/operations/LoadImageFromFileOperation.h" |
| 23 | |
| 24 | #include "tests/utils/TestRunner.h" |
| 25 | #include "tests/mocks/gui/NotificationServiceMock.h" |
| 26 | #include "tests/mocks/gui/ImageProcessorMock.h" |
| 27 | #include "tests/mocks/gui/fileService/FileServiceMock.h" |
| 28 | #include "tests/mocks/backend/recentImages/RecentImageServiceMock.h" |
| 29 | #include "tests/mocks/backend/config/ConfigMock.h" |
| 30 | |
| 31 | void LoadImageFromFileOperationTests::Execute_Should_ShowNotificationAndNotOpenImage_When_PathToImageCannotBeOpened() |
| 32 | { |
| 33 | // arrange |
| 34 | auto notificationServiceMock = QSharedPointer<NotificationServiceMock>(new NotificationServiceMock); |
| 35 | ImageProcessorMock imageProcessorMock; |
| 36 | auto recentImageServiceMock = QSharedPointer<RecentImageServiceMock>(new RecentImageServiceMock); |
| 37 | auto fileServiceMock = QSharedPointer<FileServiceMock>(new FileServiceMock); |
| 38 | auto configMock = QSharedPointer<ConfigMock>(new ConfigMock); |
| 39 | |
| 40 | EXPECT_CALL(*fileServiceMock, openPixmap(testing::_)) |
| 41 | .WillRepeatedly([=](const QString &path) { |
| 42 | return QPixmap(); |
| 43 | }); |
| 44 | |
| 45 | EXPECT_CALL(imageProcessorMock, processImage(testing::_)).Times(testing::Exactly(0)); |
| 46 | EXPECT_CALL(*recentImageServiceMock, storeImagePath(testing::_)).Times(testing::Exactly(0)); |
| 47 | EXPECT_CALL(*notificationServiceMock, showWarning(testing::_, QString("Unable to open image from path /path/image.png"), testing::_)) |
| 48 | .Times(testing::Exactly(1)); |
| 49 | |
| 50 | EXPECT_CALL(*configMock, trayIconNotificationsEnabled()) |
| 51 | .Times(testing::Exactly(1)) |
| 52 | .WillRepeatedly([=]() { |
| 53 | return true; |
| 54 | }); |
| 55 | |
| 56 | LoadImageFromFileOperation operation(QLatin1String("/path/image.png"), &imageProcessorMock, notificationServiceMock, recentImageServiceMock, fileServiceMock, configMock); |
| 57 | |
| 58 | // act |
| 59 | auto result = operation.execute(); |
| 60 | |
| 61 | // assert |
| 62 | QCOMPARE(result, false); |
| 63 | } |
| 64 | |
| 65 | void LoadImageFromFileOperationTests::Execute_Should_OpenImageAndNotShowNotification_When_PathToImageCanBeOpened() |
| 66 | { |
| 67 | // arrange |
| 68 | auto imagePath = QString("/path/image.png"); |
| 69 | auto notificationServiceMock = QSharedPointer<NotificationServiceMock>(new NotificationServiceMock); |
| 70 | ImageProcessorMock imageProcessorMock; |
| 71 | auto recentImageServiceMock = QSharedPointer<RecentImageServiceMock>(new RecentImageServiceMock); |
| 72 | auto fileServiceMock = QSharedPointer<FileServiceMock>(new FileServiceMock); |
| 73 | auto configMock = QSharedPointer<ConfigMock>(new ConfigMock); |
| 74 | |
| 75 | EXPECT_CALL(*fileServiceMock, openPixmap(imagePath)) |
| 76 | .WillRepeatedly([=](const QString &path) { |
| 77 | return QPixmap(100, 100); |
| 78 | }); |
| 79 | |
| 80 | EXPECT_CALL(imageProcessorMock, processImage(testing::_)).Times(testing::Exactly(1)); |
| 81 | EXPECT_CALL(*recentImageServiceMock, storeImagePath(imagePath)).Times(testing::Exactly(1)); |
| 82 | EXPECT_CALL(*notificationServiceMock, showWarning(testing::_, testing::_, testing::_)).Times(testing::Exactly(0)); |
| 83 | |
| 84 | EXPECT_CALL(*configMock, trayIconNotificationsEnabled()).Times(testing::Exactly(0)); |
| 85 | |
| 86 | LoadImageFromFileOperation operation(imagePath, &imageProcessorMock, notificationServiceMock, recentImageServiceMock, fileServiceMock, configMock); |
| 87 | |
| 88 | // act |
| 89 | auto result = operation.execute(); |
| 90 | |
| 91 | // assert |
| 92 | QCOMPARE(result, true); |
| 93 | } |
| 94 | |
| 95 | TEST_MAIN(LoadImageFromFileOperationTests) |