| 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 "ActionProcessorTests.h" |
| 21 | |
| 22 | #include "src/gui/actions/ActionProcessor.h" |
| 23 | |
| 24 | #include "tests/utils/TestRunner.h" |
| 25 | |
| 26 | void ActionProcessorTests::Process_Should_TriggerCapture_When_CaptureEnabled() |
| 27 | { |
| 28 | // arrange |
| 29 | Action action; |
| 30 | action.setIsCaptureEnabled(true); |
| 31 | action.setCaptureDelay(2000); |
| 32 | action.setIncludeCursor(true); |
| 33 | action.setCaptureMode(CaptureModes::CurrentScreen); |
| 34 | ActionProcessor processor; |
| 35 | QSignalSpy spy(&processor, &ActionProcessor::triggerCapture); |
| 36 | |
| 37 | // act |
| 38 | processor.process(action); |
| 39 | |
| 40 | // assert |
| 41 | QCOMPARE(spy.count(), 1); |
| 42 | QCOMPARE(qvariant_cast<CaptureModes>(spy.at(0).at(0)), CaptureModes::CurrentScreen); |
| 43 | QCOMPARE(qvariant_cast<bool>(spy.at(0).at(1)), true); |
| 44 | QCOMPARE(qvariant_cast<int>(spy.at(0).at(2)), 2000); |
| 45 | } |
| 46 | |
| 47 | void ActionProcessorTests::Process_Should_NotTriggerCapture_When_CaptureDisabled() |
| 48 | { |
| 49 | // arrange |
| 50 | Action action; |
| 51 | action.setIsCaptureEnabled(false); |
| 52 | action.setCaptureDelay(2000); |
| 53 | action.setIncludeCursor(true); |
| 54 | action.setCaptureMode(CaptureModes::CurrentScreen); |
| 55 | ActionProcessor processor; |
| 56 | QSignalSpy spy(&processor, &ActionProcessor::triggerCapture); |
| 57 | |
| 58 | // act |
| 59 | processor.process(action); |
| 60 | |
| 61 | // assert |
| 62 | QCOMPARE(spy.count(), 0); |
| 63 | } |
| 64 | |
| 65 | void ActionProcessorTests::Process_Should_StartPostProcessing_When_CaptureDisabledAndPostProcessingEnabled() |
| 66 | { |
| 67 | // arrange |
| 68 | Action action; |
| 69 | action.setIsCaptureEnabled(false); |
| 70 | action.setIsSaveEnabled(true); |
| 71 | ActionProcessor processor; |
| 72 | processor.setPostProcessingEnabled(true); |
| 73 | QSignalSpy spy(&processor, &ActionProcessor::triggerSave); |
| 74 | |
| 75 | // act |
| 76 | processor.process(action); |
| 77 | |
| 78 | // assert |
| 79 | QCOMPARE(spy.count(), 1); |
| 80 | } |
| 81 | |
| 82 | void ActionProcessorTests::Process_Should_NotStartPostProcessing_When_CaptureDisabledAndPostProcessingDisabled() |
| 83 | { |
| 84 | // arrange |
| 85 | Action action; |
| 86 | action.setIsCaptureEnabled(false); |
| 87 | action.setIsSaveEnabled(true); |
| 88 | ActionProcessor processor; |
| 89 | processor.setPostProcessingEnabled(false); |
| 90 | QSignalSpy spy(&processor, &ActionProcessor::triggerSave); |
| 91 | |
| 92 | // act |
| 93 | processor.process(action); |
| 94 | |
| 95 | // assert |
| 96 | QCOMPARE(spy.count(), 0); |
| 97 | } |
| 98 | |
| 99 | void ActionProcessorTests::Process_Should_NotStartPostProcessing_When_CaptureEnabledAndPostProcessingDisabled() |
| 100 | { |
| 101 | // arrange |
| 102 | Action action; |
| 103 | action.setIsCaptureEnabled(true); |
| 104 | action.setIsSaveEnabled(true); |
| 105 | ActionProcessor processor; |
| 106 | processor.setPostProcessingEnabled(false); |
| 107 | QSignalSpy spy(&processor, &ActionProcessor::triggerSave); |
| 108 | |
| 109 | // act |
| 110 | processor.process(action); |
| 111 | |
| 112 | // assert |
| 113 | QCOMPARE(spy.count(), 0); |
| 114 | } |
| 115 | |
| 116 | void ActionProcessorTests::Process_Should_NotStartPostProcessing_When_CaptureEnabledAndPostProcessingEnabled() |
| 117 | { |
| 118 | // arrange |
| 119 | Action action; |
| 120 | action.setIsCaptureEnabled(true); |
| 121 | action.setIsSaveEnabled(true); |
| 122 | ActionProcessor processor; |
| 123 | processor.setPostProcessingEnabled(true); |
| 124 | QSignalSpy spy(&processor, &ActionProcessor::triggerSave); |
| 125 | |
| 126 | // act |
| 127 | processor.process(action); |
| 128 | |
| 129 | // assert |
| 130 | QCOMPARE(spy.count(), 0); |
| 131 | } |
| 132 | |
| 133 | void ActionProcessorTests::CaptureFinished_Should_StartPostProcessing_When_CaptureEnabledAndPostProcessingEnabled() |
| 134 | { |
| 135 | // arrange |
| 136 | Action action; |
| 137 | action.setIsCaptureEnabled(true); |
| 138 | action.setIsSaveEnabled(true); |
| 139 | ActionProcessor processor; |
| 140 | processor.setPostProcessingEnabled(true); |
| 141 | QSignalSpy spy(&processor, &ActionProcessor::triggerSave); |
| 142 | processor.process(action); |
| 143 | |
| 144 | // act |
| 145 | processor.captureFinished(); |
| 146 | |
| 147 | // assert |
| 148 | QCOMPARE(spy.count(), 1); |
| 149 | } |
| 150 | |
| 151 | void ActionProcessorTests::CaptureFinished_Should_StartPostProcessing_When_CaptureEnabledAndPostProcessingDisabled() |
| 152 | { |
| 153 | // arrange |
| 154 | Action action; |
| 155 | action.setIsCaptureEnabled(true); |
| 156 | action.setIsSaveEnabled(true); |
| 157 | ActionProcessor processor; |
| 158 | processor.setPostProcessingEnabled(false); |
| 159 | QSignalSpy spy(&processor, &ActionProcessor::triggerSave); |
| 160 | processor.process(action); |
| 161 | |
| 162 | // act |
| 163 | processor.captureFinished(); |
| 164 | |
| 165 | // assert |
| 166 | QCOMPARE(spy.count(), 1); |
| 167 | } |
| 168 | |
| 169 | void ActionProcessorTests::CaptureFinished_Should_SendSignalsForAllSelectedActions() |
| 170 | { |
| 171 | // arrange |
| 172 | Action action; |
| 173 | action.setIsCaptureEnabled(true); |
| 174 | action.setIsSaveEnabled(true); |
| 175 | action.setIsCopyToClipboardEnabled(true); |
| 176 | action.setIsOpenDirectoryEnabled(true); |
| 177 | action.setIsUploadEnabled(true); |
| 178 | action.setIsPinImageEnabled(true); |
| 179 | ActionProcessor processor; |
| 180 | processor.setPostProcessingEnabled(false); |
| 181 | QSignalSpy saveSignalSpy(&processor, &ActionProcessor::triggerSave); |
| 182 | QSignalSpy copySignalSpy(&processor, &ActionProcessor::triggerCopyToClipboard); |
| 183 | QSignalSpy openSignalSpy(&processor, &ActionProcessor::triggerOpenDirectory); |
| 184 | QSignalSpy uploadSignalSpy(&processor, &ActionProcessor::triggerUpload); |
| 185 | QSignalSpy pinSignalSpy(&processor, &ActionProcessor::triggerPinImage); |
| 186 | processor.process(action); |
| 187 | |
| 188 | // act |
| 189 | processor.captureFinished(); |
| 190 | |
| 191 | // assert |
| 192 | QCOMPARE(saveSignalSpy.count(), 1); |
| 193 | QCOMPARE(copySignalSpy.count(), 1); |
| 194 | QCOMPARE(openSignalSpy.count(), 1); |
| 195 | QCOMPARE(uploadSignalSpy.count(), 1); |
| 196 | QCOMPARE(pinSignalSpy.count(), 1); |
| 197 | } |
| 198 | |
| 199 | void ActionProcessorTests::CaptureFinished_Should_NotSendSignalsForNotSelectedActions() |
| 200 | { |
| 201 | // arrange |
| 202 | Action action; |
| 203 | action.setIsCaptureEnabled(true); |
| 204 | action.setIsSaveEnabled(false); |
| 205 | action.setIsCopyToClipboardEnabled(false); |
| 206 | action.setIsOpenDirectoryEnabled(false); |
| 207 | action.setIsUploadEnabled(false); |
| 208 | action.setIsPinImageEnabled(false); |
| 209 | ActionProcessor processor; |
| 210 | processor.setPostProcessingEnabled(false); |
| 211 | QSignalSpy saveSignalSpy(&processor, &ActionProcessor::triggerSave); |
| 212 | QSignalSpy copySignalSpy(&processor, &ActionProcessor::triggerCopyToClipboard); |
| 213 | QSignalSpy openSignalSpy(&processor, &ActionProcessor::triggerOpenDirectory); |
| 214 | QSignalSpy uploadSignalSpy(&processor, &ActionProcessor::triggerUpload); |
| 215 | QSignalSpy pinSignalSpy(&processor, &ActionProcessor::triggerPinImage); |
| 216 | processor.process(action); |
| 217 | |
| 218 | // act |
| 219 | processor.captureFinished(); |
| 220 | |
| 221 | // assert |
| 222 | QCOMPARE(saveSignalSpy.count(), 0); |
| 223 | QCOMPARE(copySignalSpy.count(), 0); |
| 224 | QCOMPARE(openSignalSpy.count(), 0); |
| 225 | QCOMPARE(uploadSignalSpy.count(), 0); |
| 226 | QCOMPARE(pinSignalSpy.count(), 0); |
| 227 | } |
| 228 | |
| 229 | void ActionProcessorTests::Process_Should_MarkActionAsInProgress_When_CaptureEnabled() |
| 230 | { |
| 231 | // arrange |
| 232 | Action action; |
| 233 | action.setIsCaptureEnabled(true); |
| 234 | ActionProcessor processor; |
| 235 | |
| 236 | // act |
| 237 | processor.process(action); |
| 238 | |
| 239 | // assert |
| 240 | QCOMPARE(processor.isActionInProgress(), true); |
| 241 | } |
| 242 | |
| 243 | void ActionProcessorTests::CaptureFinished_Should_MarkActionAsNotInProgress_When_CaptureEnabled() |
| 244 | { |
| 245 | // arrange |
| 246 | Action action; |
| 247 | action.setIsCaptureEnabled(true); |
| 248 | ActionProcessor processor; |
| 249 | processor.process(action); |
| 250 | |
| 251 | // act |
| 252 | processor.captureFinished(); |
| 253 | |
| 254 | // assert |
| 255 | QCOMPARE(processor.isActionInProgress(), false); |
| 256 | } |
| 257 | |
| 258 | void ActionProcessorTests::CaptureFinished_Should_SendShowSignalWithMinimizedSetToTrue_When_HideSelected() |
| 259 | { |
| 260 | // arrange |
| 261 | Action action; |
| 262 | action.setIsCaptureEnabled(true); |
| 263 | action.setIsHideMainWindowEnabled(true); |
| 264 | ActionProcessor processor; |
| 265 | processor.setPostProcessingEnabled(true); |
| 266 | QSignalSpy spy(&processor, &ActionProcessor::triggerShow); |
| 267 | processor.process(action); |
| 268 | |
| 269 | // act |
| 270 | processor.captureFinished(); |
| 271 | |
| 272 | // assert |
| 273 | QCOMPARE(spy.count(), 1); |
| 274 | QCOMPARE(qvariant_cast<bool>(spy.at(0).at(0)), true); |
| 275 | } |
| 276 | |
| 277 | void ActionProcessorTests::CaptureFinished_Should_SendShowSignalWithMinimizedSetToFalse_When_HideNotSelected() |
| 278 | { |
| 279 | // arrange |
| 280 | Action action; |
| 281 | action.setIsCaptureEnabled(true); |
| 282 | action.setIsHideMainWindowEnabled(false); |
| 283 | ActionProcessor processor; |
| 284 | processor.setPostProcessingEnabled(true); |
| 285 | QSignalSpy spy(&processor, &ActionProcessor::triggerShow); |
| 286 | processor.process(action); |
| 287 | |
| 288 | // act |
| 289 | processor.captureFinished(); |
| 290 | |
| 291 | // assert |
| 292 | QCOMPARE(spy.count(), 1); |
| 293 | QCOMPARE(qvariant_cast<bool>(spy.at(0).at(0)), false); |
| 294 | } |
| 295 | |
| 296 | void ActionProcessorTests::Process_Should_NotSendShowSignal_When_HideNotSelectedAndCaptureNotSelected() |
| 297 | { |
| 298 | // arrange |
| 299 | Action action; |
| 300 | action.setIsCaptureEnabled(false); |
| 301 | action.setIsHideMainWindowEnabled(false); |
| 302 | ActionProcessor processor; |
| 303 | processor.setPostProcessingEnabled(true); |
| 304 | QSignalSpy spy(&processor, &ActionProcessor::triggerShow); |
| 305 | |
| 306 | // act |
| 307 | processor.process(action); |
| 308 | |
| 309 | // assert |
| 310 | QCOMPARE(spy.count(), 0); |
| 311 | } |
| 312 | |
| 313 | void ActionProcessorTests::Process_Should_SendShowSignalWithMinimizedSetToTrue_When_HideSelectedAndCaptureNotSelected() |
| 314 | { |
| 315 | // arrange |
| 316 | Action action; |
| 317 | action.setIsCaptureEnabled(false); |
| 318 | action.setIsHideMainWindowEnabled(true); |
| 319 | ActionProcessor processor; |
| 320 | processor.setPostProcessingEnabled(true); |
| 321 | QSignalSpy spy(&processor, &ActionProcessor::triggerShow); |
| 322 | |
| 323 | // act |
| 324 | processor.process(action); |
| 325 | |
| 326 | // assert |
| 327 | QCOMPARE(spy.count(), 1); |
| 328 | QCOMPARE(qvariant_cast<bool>(spy.at(0).at(0)), true); |
| 329 | } |
| 330 | |
| 331 | TEST_MAIN(ActionProcessorTests) |