| 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 "DeleteImageOperationTests.h" |
| 21 | |
| 22 | #include "src/gui/operations/DeleteImageOperation.h" |
| 23 | |
| 24 | #include "tests/utils/TestRunner.h" |
| 25 | #include "tests/mocks/gui/messageBoxService/MessageBoxServiceMock.h" |
| 26 | #include "tests/mocks/gui/fileService/FileServiceMock.h" |
| 27 | |
| 28 | void DeleteImageOperationTests::Execute_Should_ReturnFalse_When_MessageBoxResponseWasCancel() |
| 29 | { |
| 30 | // arrange |
| 31 | auto path = QString("/la/la"); |
| 32 | MessageBoxServiceMock messageBoxServiceMock; |
| 33 | FileServiceMock fileServiceMock; |
| 34 | |
| 35 | EXPECT_CALL(messageBoxServiceMock, okCancel(testing::_, testing::_)) |
| 36 | .WillRepeatedly([=](const QString &title, const QString &question) { |
| 37 | return false; |
| 38 | }); |
| 39 | |
| 40 | DeleteImageOperation operation(path, &fileServiceMock, &messageBoxServiceMock); |
| 41 | |
| 42 | EXPECT_CALL(fileServiceMock, remove(path)).Times(testing::Exactly(0)); |
| 43 | |
| 44 | // act |
| 45 | auto result = operation.execute(); |
| 46 | |
| 47 | // assert |
| 48 | QCOMPARE(result, false); |
| 49 | } |
| 50 | |
| 51 | void DeleteImageOperationTests::Execute_Should_ReturnTrue_When_MessageBoxResponseWasTrue_And_FileServiceSaveSuccessfully() |
| 52 | { |
| 53 | // arrange |
| 54 | auto path = QString("/la/la"); |
| 55 | MessageBoxServiceMock messageBoxServiceMock; |
| 56 | FileServiceMock fileServiceMock; |
| 57 | |
| 58 | EXPECT_CALL(messageBoxServiceMock, okCancel(testing::_, testing::_)) |
| 59 | .WillRepeatedly([=](const QString &title, const QString &question) { |
| 60 | return true; |
| 61 | }); |
| 62 | |
| 63 | EXPECT_CALL(fileServiceMock, remove(path)) |
| 64 | .Times(testing::Exactly(1)) |
| 65 | .WillRepeatedly([=](const QString &path) { |
| 66 | return true; |
| 67 | }); |
| 68 | |
| 69 | DeleteImageOperation operation(path, &fileServiceMock, &messageBoxServiceMock); |
| 70 | |
| 71 | // act |
| 72 | auto result = operation.execute(); |
| 73 | |
| 74 | // assert |
| 75 | QCOMPARE(result, true); |
| 76 | } |
| 77 | |
| 78 | void DeleteImageOperationTests::Execute_Should_ReturnFalse_When_MessageBoxResponseWasTrue_And_FileServiceSaveFailed() |
| 79 | { |
| 80 | // arrange |
| 81 | auto path = QString("/la/la"); |
| 82 | MessageBoxServiceMock messageBoxServiceMock; |
| 83 | FileServiceMock fileServiceMock; |
| 84 | |
| 85 | EXPECT_CALL(messageBoxServiceMock, okCancel(testing::_, testing::_)) |
| 86 | .WillRepeatedly([=](const QString &title, const QString &question) { |
| 87 | return true; |
| 88 | }); |
| 89 | |
| 90 | EXPECT_CALL(fileServiceMock, remove(path)) |
| 91 | .Times(testing::Exactly(1)) |
| 92 | .WillRepeatedly([=](const QString &path) { |
| 93 | return false; |
| 94 | }); |
| 95 | |
| 96 | DeleteImageOperation operation(path, &fileServiceMock, &messageBoxServiceMock); |
| 97 | |
| 98 | // act |
| 99 | auto result = operation.execute(); |
| 100 | |
| 101 | // assert |
| 102 | QCOMPARE(result, false); |
| 103 | } |
| 104 | |
| 105 | TEST_MAIN(DeleteImageOperationTests) |