| 1 | #include "filesystemaction.h" |
| 2 | #include "dirmodel.h" |
| 3 | #include "tempfiles.h" |
| 4 | #include "externalfswatcher.h" |
| 5 | #include "dirselection.h" |
| 6 | #include "qtrashdir.h" |
| 7 | #include "location.h" |
| 8 | #include "locationurl.h" |
| 9 | #include "locationsfactory.h" |
| 10 | #include "disklocation.h" |
| 11 | #include "qtrashutilinfo.h" |
| 12 | #include "smbiteminfo.h" |
| 13 | #include "testqsambasuite.h" |
| 14 | #include "smbusershare.h" |
| 15 | #include "smblocationitemfile.h" |
| 16 | |
| 17 | #if defined(Q_OS_UNIX) |
| 18 | #include <stdio.h> |
| 19 | #include <sys/types.h> |
| 20 | #include <utime.h> |
| 21 | #include <sys/time.h> |
| 22 | #endif |
| 23 | |
| 24 | #ifndef DO_NOT_USE_TAG_LIB |
| 25 | #include <taglib/attachedpictureframe.h> |
| 26 | #include <taglib/id3v2tag.h> |
| 27 | #include <taglib/fileref.h> |
| 28 | #include <taglib/mpegfile.h> |
| 29 | #include <taglib/tag.h> |
| 30 | #include <taglib/audioproperties.h> |
| 31 | #endif |
| 32 | |
| 33 | #include <QApplication> |
| 34 | #include <QtCore/QString> |
| 35 | #include <QtTest/QtTest> |
| 36 | #include <QFileInfo> |
| 37 | #include <QDirIterator> |
| 38 | #include <QIcon> |
| 39 | #include <QPixmap> |
| 40 | #include <QFileIconProvider> |
| 41 | #include <QStandardPaths> |
| 42 | |
| 43 | #include <QMimeType> |
| 44 | #include <QMimeDatabase> |
| 45 | #include <QCryptographicHash> |
| 46 | #include <QDesktopServices> |
| 47 | #include <QFile> |
| 48 | #include <QTemporaryDir> |
| 49 | #include <QTemporaryFile> |
| 50 | |
| 51 | //files to generate |
| 52 | #include "testonly_pdf.h" |
| 53 | #include "sound_mp3.h" |
| 54 | #include "media_asx.h" |
| 55 | #include "media_xspf.h" |
| 56 | |
| 57 | #define TIME_TO_PROCESS 2300 |
| 58 | #define TIME_TO_REFRESH_DIR 90 |
| 59 | #define TIME_TO_REFRESH_REMOTE_DIR (TIME_TO_REFRESH_DIR * 25) // samba RPC may take longer compared with local disk access even working on localhost |
| 60 | |
| 61 | #if QT_VERSION >= 0x050000 |
| 62 | #define QSKIP_ALL_TESTS(statement) QSKIP(statement) |
| 63 | #else |
| 64 | #define QSKIP_ALL_TESTS(statement) QSKIP(statement,SkipAll) |
| 65 | #endif |
| 66 | |
| 67 | #define CHECK_IF_CAN_CREATE_SHARES() if (!SmbUserShare::canCreateShares()) \ |
| 68 | { qWarning() << Q_FUNC_INFO << "cannot be performed, it requires a Samba Server to create user shares"; return;} |
| 69 | |
| 70 | |
| 71 | class FakeDirItemInfoBigSize: public DirItemInfo |
| 72 | { |
| 73 | public: |
| 74 | FakeDirItemInfoBigSize(const DirItemInfo& original): DirItemInfo(original) |
| 75 | { |
| 76 | d_ptr->_size = 0x7fffffffffffffff; // higher qint64, 64 bits with huge size |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | |
| 81 | QByteArray md5FromIcon(const QIcon& icon); |
| 82 | QString createFileInTempDir(const QString& name, const char *content, qint64 size); |
| 83 | |
| 84 | class TestDirModel : public QObject |
| 85 | { |
| 86 | Q_OBJECT |
| 87 | |
| 88 | public: |
| 89 | TestDirModel(); |
| 90 | ~TestDirModel(); |
| 91 | |
| 92 | protected slots: |
| 93 | void slotFileAdded(const DirItemInfo& f) {m_filesAdded.append(f.absoluteFilePath()); } |
| 94 | void slotFileRemoved(const DirItemInfo& f) {m_filesRemoved.append(f.absoluteFilePath()); } |
| 95 | void slotPathChamged(QString path) { m_currentPath = path;} |
| 96 | void progress(int, int, int); |
| 97 | void slotRemoveFileWhenProgressArrive(int,int,int); |
| 98 | void cancel(int index, int, int percent); |
| 99 | void slotclipboardChanged(); |
| 100 | void slotError(QString title, QString message); |
| 101 | void slotExtFsWatcherPathModified(const QString&) { ++m_extFSWatcherPathModifiedCounter; } |
| 102 | void slotSelectionChanged(int counter) { m_selectedItemsCounter = counter; } |
| 103 | void slotSelectionModeChanged(int m) { m_selectionMode = m;} |
| 104 | void onDownloadTemporaryComplete(const QString& name) {m_temporaryDownloadName = name;} |
| 105 | |
| 106 | private Q_SLOTS: |
| 107 | void initTestCase(); //before all tests |
| 108 | void cleanupTestCase(); //after all tests |
| 109 | void init(); //before every test |
| 110 | void cleanup(); //after every test |
| 111 | |
| 112 | private Q_SLOTS: // test cases |
| 113 | void fsActionRemoveSingleFile(); |
| 114 | void fsActionRemoveSingleDir(); |
| 115 | void fsActionRemoveOneFileOneDir(); |
| 116 | void fsActionRemoveTwoFilesTwoDirs(); |
| 117 | void modelRemoveRecursiveDirByIndex(); |
| 118 | void modelRemoveMultiItemsByFullPathname(); |
| 119 | void modelRemoveMultiItemsByName(); |
| 120 | void modelCopyDirPasteIntoAnotherModel(); |
| 121 | void modelCopyManyItemsPasteIntoAnotherModel(); |
| 122 | void modelCopyTwoEmptyFiles(); |
| 123 | void modelCopyFileAndRemoveBeforePaste(); |
| 124 | void modelCopyPasteFileAndRemoveWhenFirstProgressSignalArrives(); |
| 125 | void modelCutManyItemsPasteIntoAnotherModel(); |
| 126 | void fsActionMoveItemsForcingCopyAndThenRemove(); |
| 127 | void modelCancelRemoveAction(); |
| 128 | void modelTestFileSize(); |
| 129 | void modelRemoveDirWithHiddenFilesAndLinks(); |
| 130 | void modelCancelCopyAction(); |
| 131 | void modelCopyPasteAndPasteAgain(); |
| 132 | void modelCutPasteIntoExistentItems(); |
| 133 | void modelCopyFileAndDirectoryLinks(); |
| 134 | void modelCopyAndPaste3Times(); |
| 135 | void modelCutAndPaste3Times(); |
| 136 | void modelCutAndPasteInTheSamePlace(); |
| 137 | void modelCopyAndPasteToBackupFiles(); |
| 138 | void diskCdIntoPathRelative(); |
| 139 | void diskCdIntoPathAbsolute(); |
| 140 | void trashCdIntoPathRelative(); |
| 141 | void trashCdIntoPathAbsolute(); |
| 142 | void fileIconProvider(); |
| 143 | void getThemeIcons(); |
| 144 | #ifndef DO_NOT_USE_TAG_LIB |
| 145 | void verifyMP3Metadata(); |
| 146 | #endif |
| 147 | void openPathAbsouluteAndRelative(); |
| 148 | void existsDirAnCanReadDir(); |
| 149 | void existsFileAndCanReadFile(); |
| 150 | void pathProperties(); |
| 151 | void watchExternalChanges(); |
| 152 | void extFsWatcherChangePathManyTimesModifyAllPathsLessLast(); // no notification |
| 153 | void extFsWatcherModifySamePathManyTimesWithInInterval(); // just one notification |
| 154 | void extFsWatcherSetPathAndModifyManyTimesWithInInterval();// just one notification |
| 155 | void extFsWatcherChangePathManyTimesModifyManyTimes(); // many notifications |
| 156 | #if defined(Q_OS_UNIX) |
| 157 | void extFsWatcherNoticeChangesWithSameTimestamp(); |
| 158 | #endif |
| 159 | |
| 160 | //define TEST_OPENFILES to test QDesktopServices::openUrl() for some files |
| 161 | #if defined(TEST_OPENFILES) |
| 162 | void openMP3(); |
| 163 | void openTXT(); |
| 164 | void openPDF(); |
| 165 | #endif |
| 166 | |
| 167 | void modelSingleSelection(); |
| 168 | void modelMultiSelection(); |
| 169 | void modelSelectionItemsRange(); |
| 170 | |
| 171 | void trashDiretories(); |
| 172 | |
| 173 | void locationFactory(); |
| 174 | void moveOneFileToTrashAndRestore(); |
| 175 | void restoreTrashWithMultipleSources(); |
| 176 | void emptyTrash(); |
| 177 | |
| 178 | // Samba test cases |
| 179 | private Q_SLOTS: |
| 180 | void smbCdIntoPathRelative(); |
| 181 | void smbCdIntoPathAbsolute(); |
| 182 | //tests for high level helper functions |
| 183 | void smbExistsDir(); |
| 184 | void smbCanReadDir(); |
| 185 | void smbExistsFile(); |
| 186 | void smbCanReadFile(); |
| 187 | //common filemanager tests for Samba |
| 188 | void smbMkdir(); |
| 189 | void smbMakeBackupNameForCurrentItem(); |
| 190 | void smbCopyFromSmb2Smb(); |
| 191 | void smbCopyFromSmb2LocalDisk(); |
| 192 | void smbCopyFromLocalDisk2Smb(); |
| 193 | void smbRemoveDirectory(); |
| 194 | void smbCutFromSmb2Smb(); |
| 195 | void smbCutFromSmb2LocalDisk(); |
| 196 | void smbCutFromLocalDisk2Smb(); |
| 197 | |
| 198 | private Q_SLOTS: // remote donwnload tests using samba |
| 199 | void smbDownloadEmptyFile(); |
| 200 | void smbDownloadBigFileWithNoSpace(); |
| 201 | void smbDownloadFile(); |
| 202 | void smbDownloadAsTemporary(); |
| 203 | void smbDownloadIntoStandardDownloadLocation(); |
| 204 | |
| 205 | |
| 206 | private: |
| 207 | bool createTempHomeTrashDir(const QString& existentDir); |
| 208 | void initDeepDirs(); |
| 209 | void cleanDeepDirs(); |
| 210 | void initModels(); |
| 211 | void cleanModels(); |
| 212 | bool compareDirectories(const QString& d1, |
| 213 | const QString& d2, bool comparePermissions = true); |
| 214 | |
| 215 | bool createLink(const QString& fullSouce, |
| 216 | const QString& link, |
| 217 | bool fullLink = false); |
| 218 | |
| 219 | bool createFileAndCheckIfIconIsExclisive(const QString& termination, |
| 220 | const unsigned char *content, |
| 221 | qint64 len); |
| 222 | |
| 223 | private: |
| 224 | FileSystemAction fsAction; |
| 225 | |
| 226 | QStringList m_filesAdded; |
| 227 | QStringList m_filesRemoved; |
| 228 | |
| 229 | DeepDir * m_deepDir_01; |
| 230 | DeepDir * m_deepDir_02; |
| 231 | DeepDir * m_deepDir_03; |
| 232 | |
| 233 | DirModel * m_dirModel_01; |
| 234 | DirModel * m_dirModel_02; |
| 235 | |
| 236 | int m_progressCounter; |
| 237 | int m_progressTotalItems; |
| 238 | int m_progressCurrentItem; |
| 239 | int m_progressPercentDone; |
| 240 | bool m_receivedClipboardChangesSignal; |
| 241 | bool m_receivedErrorSignal; |
| 242 | int m_progressNotificationsCounter; |
| 243 | bool m_visibleProgressMessages; |
| 244 | QHash<QByteArray, QString> m_md5IconsTable; |
| 245 | QFileIconProvider m_provider; |
| 246 | QString m_currentPath; |
| 247 | QString m_fileToRemoveInProgressSignal; |
| 248 | int m_extFSWatcherPathModifiedCounter; |
| 249 | int m_selectedItemsCounter; |
| 250 | int m_selectionMode; |
| 251 | QString m_temporaryDownloadName; |
| 252 | |
| 253 | }; |
| 254 | |
| 255 | TestDirModel::TestDirModel() : fsAction(new LocationsFactory(this), this) |
| 256 | ,m_deepDir_01(0) |
| 257 | ,m_deepDir_02(0) |
| 258 | ,m_deepDir_03(0) |
| 259 | ,m_dirModel_01(0) |
| 260 | ,m_dirModel_02(0) |
| 261 | { |
| 262 | connect(&fsAction, SIGNAL(added(DirItemInfo)), |
| 263 | this, SLOT(slotFileAdded(DirItemInfo))); |
| 264 | connect(&fsAction, SIGNAL(removed(DirItemInfo)), |
| 265 | this, SLOT(slotFileRemoved(DirItemInfo))); |
| 266 | |
| 267 | connect(&fsAction, SIGNAL(progress(int,int,int)), |
| 268 | this, SLOT(progress(int,int,int))); |
| 269 | } |
| 270 | |
| 271 | void TestDirModel::progress(int cur, int total, int percent) |
| 272 | { |
| 273 | m_progressCounter++; |
| 274 | m_progressCurrentItem = cur; |
| 275 | m_progressTotalItems = total; |
| 276 | m_progressPercentDone = percent; |
| 277 | if (m_visibleProgressMessages) |
| 278 | { |
| 279 | QString p; |
| 280 | p.sprintf("progress(cur=%d, total=%d, percent=%d)", cur,total,percent); |
| 281 | qDebug() << p; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | void TestDirModel::slotRemoveFileWhenProgressArrive(int cur, int total, int percent) |
| 286 | { |
| 287 | qDebug() << "removing file" << m_fileToRemoveInProgressSignal |
| 288 | << "ret" << QFile::remove(m_fileToRemoveInProgressSignal); |
| 289 | progress(cur,total,percent); |
| 290 | } |
| 291 | |
| 292 | bool TestDirModel::createLink(const QString &fullSouce, const QString &link, bool fullLink) |
| 293 | { |
| 294 | bool ret = false; |
| 295 | |
| 296 | QFileInfo source(fullSouce); |
| 297 | if (source.exists()) |
| 298 | { |
| 299 | if (fullLink) |
| 300 | { |
| 301 | QFileInfo lnk(link); |
| 302 | ret = QFile::link(source.absoluteFilePath(), lnk.absoluteFilePath()); |
| 303 | } |
| 304 | else |
| 305 | { |
| 306 | QString curDir = QDir::currentPath(); |
| 307 | if (QDir::setCurrent(source.absolutePath())) |
| 308 | { |
| 309 | QFileInfo lnk(link); |
| 310 | if (source.absolutePath() != lnk.absolutePath()) |
| 311 | { |
| 312 | if (lnk.isAbsolute()) |
| 313 | { |
| 314 | ret = QFile::link(source.absoluteFilePath(), link); |
| 315 | } |
| 316 | else |
| 317 | { |
| 318 | QDir relative(lnk.absolutePath()); |
| 319 | if (relative.exists() || relative.mkpath(lnk.absolutePath())) |
| 320 | { |
| 321 | int diff=0; |
| 322 | QStringList sourceDirs = source.absolutePath(). |
| 323 | split(QDir::separator(), QString::SkipEmptyParts); |
| 324 | QStringList targetDirs = lnk.absolutePath(). |
| 325 | split(QDir::separator(), QString::SkipEmptyParts); |
| 326 | while (diff < sourceDirs.count()) |
| 327 | { |
| 328 | if (sourceDirs.at(diff) != |
| 329 | targetDirs.at(diff)) |
| 330 | { |
| 331 | break; |
| 332 | } |
| 333 | diff++; |
| 334 | } |
| 335 | QString relativePath = sourceDirs.at(diff); |
| 336 | QString gap(QLatin1String("..") + QDir::separator()); |
| 337 | while (diff++ < targetDirs.count()) |
| 338 | { |
| 339 | relativePath.prepend(gap); |
| 340 | } |
| 341 | ret = QFile::link(relativePath |
| 342 | + QDir::separator() |
| 343 | + source.fileName(), |
| 344 | link); |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | else |
| 349 | { |
| 350 | ret = QFile::link(source.fileName(), link); |
| 351 | } |
| 352 | QDir::setCurrent(curDir); |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | return ret; |
| 357 | } |
| 358 | |
| 359 | bool TestDirModel::compareDirectories(const QString &d1, const QString &d2, bool comparePermissions ) |
| 360 | { |
| 361 | QDirIterator d1Info(d1, |
| 362 | QDir::Files | QDir::Hidden | QDir::System, |
| 363 | QDirIterator::Subdirectories); |
| 364 | |
| 365 | int len = d1.length(); |
| 366 | |
| 367 | while (d1Info.hasNext() && !d1Info.next().isEmpty()) |
| 368 | { |
| 369 | QString target(d2 + d1Info.fileInfo().absoluteFilePath().mid(len)); |
| 370 | |
| 371 | QFileInfo d2Info(target); |
| 372 | if (d1Info.fileName() != d2Info.fileName()) |
| 373 | { |
| 374 | qDebug() << "false name" << d1Info.fileName() << d2Info.fileName(); |
| 375 | return false; |
| 376 | } |
| 377 | if (d1Info.fileInfo().size() != d2Info.size()) |
| 378 | { |
| 379 | qDebug() << "false size" << d1Info.fileName() << d1Info.fileInfo().size() |
| 380 | << d2Info.fileName() << d2Info.size(); |
| 381 | return false; |
| 382 | } |
| 383 | if (comparePermissions && d1Info.fileInfo().permissions() != d2Info.permissions()) |
| 384 | { |
| 385 | qDebug() << "false permissions" << d1Info.fileName() << d2Info.fileName(); |
| 386 | return false; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | return true; |
| 391 | } |
| 392 | |
| 393 | void TestDirModel::cancel(int index, int, int percent) |
| 394 | { |
| 395 | DirModel * model = static_cast<DirModel*> (sender()); |
| 396 | if (index > 1 || percent > 1) |
| 397 | { |
| 398 | model->cancelAction(); |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | void TestDirModel::slotclipboardChanged() |
| 403 | { |
| 404 | m_receivedClipboardChangesSignal = true; |
| 405 | } |
| 406 | |
| 407 | void TestDirModel::slotError(QString title, QString message) |
| 408 | { |
| 409 | qWarning("Received Error: [title: %s] [message: %s]", qPrintable(title), qPrintable(message)); |
| 410 | m_receivedErrorSignal = true; |
| 411 | } |
| 412 | |
| 413 | TestDirModel::~TestDirModel() |
| 414 | { |
| 415 | |
| 416 | } |
| 417 | |
| 418 | void TestDirModel::initDeepDirs() |
| 419 | { |
| 420 | cleanDeepDirs(); |
| 421 | } |
| 422 | |
| 423 | |
| 424 | void TestDirModel::cleanDeepDirs() |
| 425 | { |
| 426 | if (m_deepDir_01) delete m_deepDir_01; |
| 427 | if (m_deepDir_02) delete m_deepDir_02; |
| 428 | if (m_deepDir_03) delete m_deepDir_03; |
| 429 | m_deepDir_01 = 0; |
| 430 | m_deepDir_02 = 0; |
| 431 | m_deepDir_03 = 0; |
| 432 | } |
| 433 | |
| 434 | |
| 435 | void TestDirModel::initModels() |
| 436 | { |
| 437 | cleanModels(); |
| 438 | m_dirModel_01 = new DirModel(); |
| 439 | m_dirModel_02 = new DirModel(); |
| 440 | |
| 441 | connect(m_dirModel_01->m_fsAction, SIGNAL(added(DirItemInfo)), |
| 442 | this, SLOT(slotFileAdded(DirItemInfo))); |
| 443 | connect(m_dirModel_01->m_fsAction, SIGNAL(removed(DirItemInfo)), |
| 444 | this, SLOT(slotFileRemoved(DirItemInfo))); |
| 445 | |
| 446 | connect(m_dirModel_02->m_fsAction, SIGNAL(added(DirItemInfo)), |
| 447 | this, SLOT(slotFileAdded(DirItemInfo))); |
| 448 | connect(m_dirModel_02->m_fsAction, SIGNAL(removed(DirItemInfo)), |
| 449 | this, SLOT(slotFileRemoved(DirItemInfo))); |
| 450 | |
| 451 | m_dirModel_01->setEnabledExternalFSWatcher(true); |
| 452 | m_dirModel_02->setEnabledExternalFSWatcher(true); |
| 453 | } |
| 454 | |
| 455 | |
| 456 | void TestDirModel::cleanModels() |
| 457 | { |
| 458 | if (m_dirModel_01) delete m_dirModel_01; |
| 459 | if (m_dirModel_02) delete m_dirModel_02; |
| 460 | m_dirModel_01 = 0; |
| 461 | m_dirModel_02 = 0; |
| 462 | } |
| 463 | |
| 464 | void TestDirModel::initTestCase() |
| 465 | { |
| 466 | DirModel::registerMetaTypes(); |
| 467 | } |
| 468 | |
| 469 | |
| 470 | void TestDirModel::cleanupTestCase() |
| 471 | { |
| 472 | cleanDeepDirs(); |
| 473 | cleanModels(); |
| 474 | } |
| 475 | |
| 476 | |
| 477 | void TestDirModel::init() |
| 478 | { |
| 479 | m_filesAdded.clear(); |
| 480 | m_filesRemoved.clear(); |
| 481 | initDeepDirs(); |
| 482 | initModels(); |
| 483 | m_progressCounter = 0; |
| 484 | m_progressTotalItems = 0; |
| 485 | m_progressCurrentItem = 0; |
| 486 | m_progressPercentDone = 0; |
| 487 | m_receivedClipboardChangesSignal = false; |
| 488 | m_receivedErrorSignal = false; |
| 489 | m_progressNotificationsCounter = 0; |
| 490 | m_visibleProgressMessages = false; |
| 491 | m_extFSWatcherPathModifiedCounter = 0; |
| 492 | m_selectedItemsCounter = 0; |
| 493 | m_selectionMode = -1; |
| 494 | } |
| 495 | |
| 496 | |
| 497 | |
| 498 | void TestDirModel::cleanup() |
| 499 | { |
| 500 | cleanDeepDirs(); |
| 501 | cleanModels(); |
| 502 | m_filesAdded.clear(); |
| 503 | m_filesRemoved.clear(); |
| 504 | m_progressCounter = 0; |
| 505 | m_progressTotalItems = 0; |
| 506 | m_progressCurrentItem = 0; |
| 507 | m_progressPercentDone = 0; |
| 508 | m_receivedClipboardChangesSignal = false; |
| 509 | m_receivedErrorSignal = false; |
| 510 | m_fileToRemoveInProgressSignal.clear(); |
| 511 | m_progressNotificationsCounter = 0; |
| 512 | m_visibleProgressMessages = false; |
| 513 | m_extFSWatcherPathModifiedCounter = 0; |
| 514 | m_selectedItemsCounter = 0; |
| 515 | m_selectionMode = -1; |
| 516 | } |
| 517 | |
| 518 | |
| 519 | void TestDirModel::fsActionRemoveSingleFile() |
| 520 | { |
| 521 | TempFiles file; |
| 522 | QCOMPARE(file.create("fsAtion_removeSingleFile") , true); |
| 523 | |
| 524 | fsAction.remove(file.createdList()); |
| 525 | QTest::qWait(TIME_TO_PROCESS); |
| 526 | QCOMPARE(m_filesRemoved.count() , 1); |
| 527 | |
| 528 | QCOMPARE(m_filesRemoved.at(0), file.lastFileCreated()); |
| 529 | QFileInfo now(file.lastFileCreated()); |
| 530 | QCOMPARE(now.exists(), false); |
| 531 | } |
| 532 | |
| 533 | |
| 534 | |
| 535 | void TestDirModel::fsActionRemoveSingleDir() |
| 536 | { |
| 537 | const int level = 10; |
| 538 | m_deepDir_01 = new DeepDir("removeSingleDir", level); |
| 539 | QCOMPARE( QFileInfo(m_deepDir_01->path()).exists(), true); |
| 540 | |
| 541 | QStringList myDeepDir(m_deepDir_01->path()); |
| 542 | fsAction.remove(myDeepDir); |
| 543 | QTest::qWait(TIME_TO_PROCESS); |
| 544 | |
| 545 | QCOMPARE(m_filesRemoved.count() , 1); |
| 546 | QCOMPARE( QFileInfo(m_deepDir_01->path()).exists(), false); |
| 547 | QVERIFY(m_progressCounter > 2); |
| 548 | } |
| 549 | |
| 550 | |
| 551 | void TestDirModel::fsActionRemoveOneFileOneDir() |
| 552 | { |
| 553 | const int level = 10; |
| 554 | m_deepDir_01 = new DeepDir("removeSingleDir", level); |
| 555 | QCOMPARE( QFileInfo(m_deepDir_01->path()).exists(), true); |
| 556 | |
| 557 | TempFiles file; |
| 558 | QCOMPARE(file.create("fsActionRemoveOneFileOneDir") , true); |
| 559 | |
| 560 | QStringList twoItems(m_deepDir_01->path()); |
| 561 | twoItems.append(file.lastFileCreated()); |
| 562 | fsAction.remove(twoItems); |
| 563 | QTest::qWait(TIME_TO_PROCESS); |
| 564 | |
| 565 | QCOMPARE(m_filesRemoved.count() , 2); |
| 566 | QCOMPARE( QFileInfo(m_deepDir_01->path()).exists(), false); |
| 567 | QCOMPARE(file.howManyExist(), 0); |
| 568 | QVERIFY(m_progressCounter > 2); |
| 569 | } |
| 570 | |
| 571 | |
| 572 | |
| 573 | void TestDirModel::fsActionRemoveTwoFilesTwoDirs() |
| 574 | { |
| 575 | const int level = 12; |
| 576 | m_deepDir_01 = new DeepDir("fsActionRemoveTwoFilesTwoDirs_01", level); |
| 577 | QCOMPARE( QFileInfo(m_deepDir_01->path()).exists(), true); |
| 578 | |
| 579 | m_deepDir_02 = new DeepDir("fsActionRemoveTwoFilesTwoDirs_02", level); |
| 580 | QCOMPARE( QFileInfo(m_deepDir_02->path()).exists(), true); |
| 581 | |
| 582 | TempFiles twoFiles; |
| 583 | QCOMPARE(twoFiles.create(2) , true); |
| 584 | |
| 585 | QStringList fourItems(m_deepDir_01->path()); |
| 586 | fourItems.append(m_deepDir_02->path()); |
| 587 | fourItems.append(twoFiles.createdList()); |
| 588 | |
| 589 | fsAction.remove(fourItems); |
| 590 | QTest::qWait(TIME_TO_PROCESS); |
| 591 | |
| 592 | QCOMPARE(m_filesRemoved.count() , 4); |
| 593 | QCOMPARE(QFileInfo(m_deepDir_01->path()).exists(), false); |
| 594 | QCOMPARE(QFileInfo(m_deepDir_02->path()).exists(), false); |
| 595 | QCOMPARE(twoFiles.howManyExist(), 0); |
| 596 | QVERIFY(m_progressCounter > 2); |
| 597 | } |
| 598 | |
| 599 | void TestDirModel::modelRemoveRecursiveDirByIndex() |
| 600 | { |
| 601 | const int level = 5; |
| 602 | m_deepDir_01 = new DeepDir("modelRemoveRecursiveDirByIndex", level); |
| 603 | QCOMPARE( QFileInfo(m_deepDir_01->path()).exists(), true); |
| 604 | |
| 605 | m_dirModel_01->setPath(m_deepDir_01->path()); |
| 606 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 607 | |
| 608 | QCOMPARE(m_dirModel_01->rowCount(), 1); |
| 609 | m_dirModel_01->removeIndex(0); |
| 610 | QTest::qWait(500); |
| 611 | |
| 612 | QCOMPARE(m_filesRemoved.count() , 1); |
| 613 | QCOMPARE(m_dirModel_01->rowCount(), 0); |
| 614 | } |
| 615 | |
| 616 | void TestDirModel::modelRemoveMultiItemsByFullPathname() |
| 617 | { |
| 618 | QString tmpDir("modelRemoveMultiItemsByFullPathame"); |
| 619 | m_deepDir_01 = new DeepDir(tmpDir, 0); |
| 620 | QCOMPARE( QFileInfo(m_deepDir_01->path()).exists(), true); |
| 621 | |
| 622 | const int filesToCreate = 2; |
| 623 | const int itemsToCreate = filesToCreate + 1; |
| 624 | |
| 625 | TempFiles files; |
| 626 | files.addSubDirLevel(tmpDir); |
| 627 | files.create(filesToCreate); |
| 628 | |
| 629 | QStringList items (files.createdList()); |
| 630 | |
| 631 | files.addSubDirLevel("subDir"); // + 1 item |
| 632 | files.create(1); |
| 633 | items.append(files.lastPath()); |
| 634 | |
| 635 | connect(m_dirModel_01, SIGNAL(progress(int,int,int)), |
| 636 | this, SLOT(progress(int,int,int))); |
| 637 | m_dirModel_01->setPath(m_deepDir_01->path()); |
| 638 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 639 | QCOMPARE(m_dirModel_01->rowCount(), itemsToCreate); |
| 640 | |
| 641 | m_dirModel_01->rm(items); |
| 642 | int steps = m_dirModel_01->getProgressCounter(); |
| 643 | QTest::qWait(500); |
| 644 | |
| 645 | QCOMPARE(m_filesRemoved.count() , itemsToCreate); |
| 646 | QCOMPARE(m_dirModel_01->rowCount(), 0); |
| 647 | QCOMPARE(files.howManyExist(), 0); |
| 648 | QCOMPARE(steps, m_progressCounter); |
| 649 | } |
| 650 | |
| 651 | |
| 652 | void TestDirModel::modelRemoveMultiItemsByName() |
| 653 | { |
| 654 | QString tmpDir("modelRemoveMultiItemsByName"); |
| 655 | const int filesToCreate = 4; |
| 656 | |
| 657 | TempFiles files; |
| 658 | files.addSubDirLevel(tmpDir); |
| 659 | files.create(4); |
| 660 | QCOMPARE(files.howManyExist(), filesToCreate); |
| 661 | |
| 662 | m_dirModel_01->setPath(files.lastPath()); |
| 663 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 664 | |
| 665 | QStringList items(files.createdNames()); |
| 666 | m_dirModel_01->removePaths(items); |
| 667 | QTest::qWait(500); |
| 668 | |
| 669 | QCOMPARE(m_filesRemoved.count() , filesToCreate); |
| 670 | QCOMPARE(m_dirModel_01->rowCount(), 0); |
| 671 | QCOMPARE(files.howManyExist(), 0); |
| 672 | |
| 673 | QDir().rmdir(m_dirModel_01->path()); |
| 674 | } |
| 675 | |
| 676 | |
| 677 | void TestDirModel::modelCopyDirPasteIntoAnotherModel() |
| 678 | { |
| 679 | QString orig("modelCopyDirToAnotherModel_orig"); |
| 680 | |
| 681 | m_deepDir_01 = new DeepDir(orig, 1); |
| 682 | m_dirModel_01->setPath(m_deepDir_01->path()); |
| 683 | connect(m_dirModel_01, SIGNAL(clipboardChanged()), |
| 684 | this, SLOT(slotclipboardChanged())); |
| 685 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 686 | QCOMPARE(m_dirModel_01->rowCount(), 1); |
| 687 | |
| 688 | QString target("modelCopyDirToAnotherModel_target"); |
| 689 | m_deepDir_02 = new DeepDir(target, 0); |
| 690 | connect(m_dirModel_02, SIGNAL(progress(int,int,int)), |
| 691 | this, SLOT(progress(int,int,int))); |
| 692 | m_dirModel_02->setPath(m_deepDir_02->path()); |
| 693 | |
| 694 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 695 | |
| 696 | QCOMPARE( QFileInfo(m_deepDir_02->path()).exists(), true); |
| 697 | QCOMPARE(m_dirModel_02->rowCount(), 0); |
| 698 | |
| 699 | m_dirModel_01->copyIndex(0); |
| 700 | m_visibleProgressMessages = true; |
| 701 | m_dirModel_02->paste(); |
| 702 | int steps = m_dirModel_02->getProgressCounter(); |
| 703 | QTest::qWait(TIME_TO_PROCESS); |
| 704 | |
| 705 | QCOMPARE(m_dirModel_02->rowCount(), 1); |
| 706 | QCOMPARE(m_progressPercentDone, 100); |
| 707 | QCOMPARE(compareDirectories(m_deepDir_01->path(), m_deepDir_02->path()), true); |
| 708 | QCOMPARE(m_receivedClipboardChangesSignal, true); |
| 709 | QCOMPARE(steps, m_progressCounter); |
| 710 | } |
| 711 | |
| 712 | |
| 713 | void TestDirModel::modelCopyManyItemsPasteIntoAnotherModel() |
| 714 | { |
| 715 | QString orig("modelCopyManyItemstoAnotherModel_orig"); |
| 716 | |
| 717 | m_deepDir_01 = new DeepDir(orig, 5); |
| 718 | connect(m_dirModel_01, SIGNAL(clipboardChanged()), |
| 719 | this, SLOT(slotclipboardChanged())); |
| 720 | const int filesCreated = 10; |
| 721 | int itemsCreated = filesCreated + 1; |
| 722 | |
| 723 | //create a big file to test copy loop |
| 724 | QByteArray buf(4096, 't'); |
| 725 | QFile big(m_deepDir_01->path() + QDir::separator() + "big.txt"); |
| 726 | QCOMPARE(big.open(QFile::WriteOnly), true); |
| 727 | for(int i=0; i < 106; i++) |
| 728 | { |
| 729 | int wrote = (int) big.write(buf); |
| 730 | QCOMPARE(wrote, buf.size()); |
| 731 | buf += "sdfsdsedccw121222"; |
| 732 | } |
| 733 | big.close(); |
| 734 | itemsCreated++; |
| 735 | |
| 736 | // create more temporary files |
| 737 | TempFiles tempFiles; |
| 738 | tempFiles.addSubDirLevel(orig); |
| 739 | tempFiles.create(filesCreated); |
| 740 | m_dirModel_01->setPath(m_deepDir_01->path()); |
| 741 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 742 | QCOMPARE(m_dirModel_01->rowCount(), itemsCreated); |
| 743 | |
| 744 | QString target("modelCopyManyItemstoAnotherModel_target"); |
| 745 | m_deepDir_02 = new DeepDir(target, 0); |
| 746 | m_dirModel_02->setPath(m_deepDir_02->path()); |
| 747 | connect(m_dirModel_02, SIGNAL(progress(int,int,int)), |
| 748 | this, SLOT(progress(int,int,int))); |
| 749 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 750 | |
| 751 | QCOMPARE( QFileInfo(m_deepDir_02->path()).exists(), true); |
| 752 | QCOMPARE(m_dirModel_02->rowCount(), 0); |
| 753 | |
| 754 | QStringList allFiles(m_deepDir_01->firstLevel()); |
| 755 | allFiles.append(tempFiles.createdList()); |
| 756 | allFiles.append(big.fileName()); |
| 757 | |
| 758 | m_dirModel_01->copyPaths(allFiles); |
| 759 | m_visibleProgressMessages = true; |
| 760 | m_dirModel_02->paste(); |
| 761 | int steps = m_dirModel_02->m_fsAction->m_curAction->steps; |
| 762 | |
| 763 | QTest::qWait(TIME_TO_PROCESS); |
| 764 | |
| 765 | QCOMPARE(m_dirModel_02->rowCount(), itemsCreated); |
| 766 | QCOMPARE(m_dirModel_01->rowCount(), itemsCreated); |
| 767 | QCOMPARE(m_progressPercentDone, 100); |
| 768 | QCOMPARE(compareDirectories(m_deepDir_01->path(), m_deepDir_02->path()), true); |
| 769 | QCOMPARE(m_receivedClipboardChangesSignal, true); |
| 770 | |
| 771 | QCOMPARE(steps, m_progressCounter); |
| 772 | } |
| 773 | |
| 774 | |
| 775 | void TestDirModel::modelCopyTwoEmptyFiles() |
| 776 | { |
| 777 | QString orig("modelCopyTwoEmptyFiles_orig"); |
| 778 | const int itemsCreated = 2; |
| 779 | m_deepDir_01 = new DeepDir(orig, 0); |
| 780 | TempFiles empty; |
| 781 | empty.addSubDirLevel(orig); |
| 782 | empty.touch(itemsCreated); |
| 783 | |
| 784 | m_dirModel_01->setPath(m_deepDir_01->path()); |
| 785 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 786 | QCOMPARE(m_dirModel_01->rowCount(), itemsCreated); |
| 787 | |
| 788 | QString target("modelCopyTwoEmptyFiles_target"); |
| 789 | m_deepDir_02 = new DeepDir(target, 0); |
| 790 | m_dirModel_02->setPath(m_deepDir_02->path()); |
| 791 | connect(m_dirModel_02, SIGNAL(progress(int,int,int)), |
| 792 | this, SLOT(progress(int,int,int))); |
| 793 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 794 | |
| 795 | |
| 796 | m_dirModel_01->copyPaths(empty.createdList()); |
| 797 | m_dirModel_02->paste(); |
| 798 | QTest::qWait(TIME_TO_PROCESS); |
| 799 | |
| 800 | QCOMPARE(m_dirModel_02->rowCount(), itemsCreated); |
| 801 | QCOMPARE(m_dirModel_01->rowCount(), itemsCreated); |
| 802 | QCOMPARE(m_progressPercentDone, 100); |
| 803 | QCOMPARE(m_progressCurrentItem, itemsCreated); |
| 804 | QCOMPARE(compareDirectories(m_deepDir_01->path(), m_deepDir_02->path()), true); |
| 805 | } |
| 806 | |
| 807 | /*! |
| 808 | * \brief TestDirModel::modelCopyFileAndRemoveBeforePaste() |
| 809 | * |
| 810 | * The file is removed before paste, that means at the moment of building the list of files |
| 811 | * it does not exist anymore, so the list of items must be empty and no files are going to be performed |
| 812 | */ |
| 813 | void TestDirModel::modelCopyFileAndRemoveBeforePaste() |
| 814 | { |
| 815 | QString orig("modelCopyFileAndRemoveBeforePaste_orig"); |
| 816 | m_deepDir_01 = new DeepDir(orig, 0); |
| 817 | TempFiles tempFile; |
| 818 | tempFile.addSubDirLevel(orig); |
| 819 | tempFile.create(); |
| 820 | |
| 821 | m_dirModel_01->setPath(m_deepDir_01->path()); |
| 822 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 823 | QCOMPARE(m_dirModel_01->rowCount(), 1); |
| 824 | |
| 825 | QString target("modelCopyFileAndRemoveBeforePaste_target"); |
| 826 | m_deepDir_02 = new DeepDir(target, 0); |
| 827 | m_dirModel_02->setPath(m_deepDir_02->path()); |
| 828 | connect(m_dirModel_02, SIGNAL(progress(int,int,int)), |
| 829 | this, SLOT(progress(int,int,int))); |
| 830 | connect(m_dirModel_02, SIGNAL(error(QString,QString)), |
| 831 | this, SLOT(slotError(QString,QString))); |
| 832 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 833 | |
| 834 | m_dirModel_01->copyPaths(tempFile.createdList()); |
| 835 | tempFile.removeAll(); |
| 836 | m_dirModel_02->paste(); |
| 837 | QTest::qWait(TIME_TO_PROCESS); |
| 838 | |
| 839 | QCOMPARE(m_dirModel_02->rowCount(), 0); |
| 840 | QCOMPARE(m_receivedErrorSignal, true); |
| 841 | } |
| 842 | |
| 843 | /*! |
| 844 | * \brief TestDirModel::modelCopyPasteFileAndRemoveWhenFirstProgressSignalArrives() |
| 845 | * |
| 846 | * The file is removed after pasting and before the copy itself starts. |
| 847 | * That means, the list of files to be copied is built, but at the moment of the open |
| 848 | * the file does not exist. |
| 849 | */ |
| 850 | void TestDirModel::modelCopyPasteFileAndRemoveWhenFirstProgressSignalArrives() |
| 851 | { |
| 852 | QString orig("modelCopyPasteFileAndRemoveWhenFirstProgressSignalArrives_orig"); |
| 853 | m_deepDir_01 = new DeepDir(orig, 0); |
| 854 | TempFiles tempFile; |
| 855 | tempFile.addSubDirLevel(orig); |
| 856 | tempFile.create(); |
| 857 | |
| 858 | m_dirModel_01->setPath(m_deepDir_01->path()); |
| 859 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 860 | QCOMPARE(m_dirModel_01->rowCount(), 1); |
| 861 | |
| 862 | QString target("modelCopyPasteFileAndRemoveWhenFirstProgressSignalArrives_target"); |
| 863 | m_deepDir_02 = new DeepDir(target, 0); |
| 864 | m_dirModel_02->setPath(m_deepDir_02->path()); |
| 865 | connect(m_dirModel_02, SIGNAL(progress(int,int,int)), |
| 866 | this, SLOT(slotRemoveFileWhenProgressArrive(int,int,int))); |
| 867 | connect(m_dirModel_02, SIGNAL(error(QString,QString)), |
| 868 | this, SLOT(slotError(QString,QString))); |
| 869 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 870 | |
| 871 | m_fileToRemoveInProgressSignal = tempFile.lastFileCreated(); |
| 872 | m_dirModel_01->copyPaths(tempFile.createdList()); |
| 873 | m_dirModel_02->paste(); |
| 874 | QTest::qWait(TIME_TO_PROCESS); |
| 875 | |
| 876 | QCOMPARE(m_dirModel_02->rowCount(), 0); |
| 877 | QCOMPARE(m_receivedErrorSignal, true); |
| 878 | QCOMPARE(m_progressTotalItems, 1); |
| 879 | QCOMPARE(m_progressCurrentItem, 0); |
| 880 | } |
| 881 | |
| 882 | |
| 883 | void TestDirModel::modelCutManyItemsPasteIntoAnotherModel() |
| 884 | { |
| 885 | QString orig("modelCutManyItemsPasteIntoAnotherModel_orig"); |
| 886 | |
| 887 | m_deepDir_01 = new DeepDir(orig, 5); |
| 888 | connect(m_dirModel_01, SIGNAL(clipboardChanged()), |
| 889 | this, SLOT(slotclipboardChanged())); |
| 890 | const int filesCreated = 10; |
| 891 | const int itemsCreated = filesCreated + 1; |
| 892 | |
| 893 | TempFiles tempFiles; |
| 894 | tempFiles.addSubDirLevel(orig); |
| 895 | tempFiles.create(filesCreated); |
| 896 | m_dirModel_01->setPath(m_deepDir_01->path()); |
| 897 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 898 | QCOMPARE(m_dirModel_01->rowCount(), itemsCreated); |
| 899 | |
| 900 | QString target("modelCutManyItemsPasteIntoAnotherModel_target"); |
| 901 | m_deepDir_02 = new DeepDir(target, 0); |
| 902 | connect(m_dirModel_02, SIGNAL(progress(int,int,int)), |
| 903 | this, SLOT(progress(int,int,int))); |
| 904 | connect(m_dirModel_02, SIGNAL(error(QString,QString)), |
| 905 | this, SLOT(slotError(QString,QString))); |
| 906 | m_dirModel_02->setPath(m_deepDir_02->path()); |
| 907 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 908 | |
| 909 | QCOMPARE( QFileInfo(m_deepDir_02->path()).exists(), true); |
| 910 | QCOMPARE(m_dirModel_02->rowCount(), 0); |
| 911 | |
| 912 | QStringList allFiles(m_deepDir_01->firstLevel()); |
| 913 | allFiles.append(tempFiles.createdList()); |
| 914 | |
| 915 | m_dirModel_01->cutPaths(allFiles); |
| 916 | m_visibleProgressMessages = true; |
| 917 | m_dirModel_02->paste(); |
| 918 | int steps = m_dirModel_02->getProgressCounter(); |
| 919 | QTest::qWait(TIME_TO_PROCESS); |
| 920 | |
| 921 | QCOMPARE(m_dirModel_02->rowCount(), itemsCreated); //pasted into |
| 922 | QCOMPARE(m_dirModel_01->rowCount(), 0); //cut from |
| 923 | QCOMPARE(m_receivedClipboardChangesSignal, true); |
| 924 | QCOMPARE(steps, m_progressCounter); |
| 925 | } |
| 926 | |
| 927 | void TestDirModel::fsActionMoveItemsForcingCopyAndThenRemove() |
| 928 | { |
| 929 | QString orig("fsActionMoveItemsForcingCopyAndThenRemove_orig"); |
| 930 | |
| 931 | m_deepDir_01 = new DeepDir(orig, 1); |
| 932 | |
| 933 | const int filesCreated = 4; |
| 934 | const int itemsCreated = filesCreated +1; |
| 935 | |
| 936 | TempFiles tempFiles; |
| 937 | tempFiles.addSubDirLevel(orig); |
| 938 | tempFiles.create(filesCreated); |
| 939 | m_dirModel_01->setPath(m_deepDir_01->path()); |
| 940 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 941 | QCOMPARE(m_dirModel_01->rowCount(), itemsCreated); |
| 942 | |
| 943 | QString target("fsActionMoveItemsForcingCopyAndThenRemove_target"); |
| 944 | m_deepDir_02 = new DeepDir(target, 0); |
| 945 | |
| 946 | m_dirModel_02->setPath(m_deepDir_02->path()); |
| 947 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 948 | |
| 949 | connect(m_dirModel_02, SIGNAL(progress(int,int,int)), |
| 950 | this, SLOT(progress(int,int,int))); |
| 951 | |
| 952 | QCOMPARE( QFileInfo(m_deepDir_02->path()).exists(), true); |
| 953 | QCOMPARE(m_dirModel_02->rowCount(), 0); |
| 954 | |
| 955 | QStringList allFiles(m_deepDir_01->firstLevel()); |
| 956 | allFiles.append(tempFiles.createdList()); |
| 957 | |
| 958 | m_dirModel_02->m_fsAction->m_forceUsingOtherFS = true; |
| 959 | m_dirModel_02->m_fsAction->createAndProcessAction(FileSystemAction::ActionMove, |
| 960 | allFiles); |
| 961 | |
| 962 | QTest::qWait(TIME_TO_PROCESS); |
| 963 | |
| 964 | QCOMPARE(m_dirModel_02->rowCount(), itemsCreated); //pasted into |
| 965 | QCOMPARE(m_dirModel_01->rowCount(), 0); //cut from |
| 966 | |
| 967 | int totalCopied = filesCreated + m_deepDir_01->itemsCreated(); |
| 968 | QCOMPARE(itemsCreated, m_filesAdded.count()); |
| 969 | QCOMPARE(totalCopied, m_progressTotalItems); |
| 970 | } |
| 971 | |
| 972 | void TestDirModel::modelCancelRemoveAction() |
| 973 | { |
| 974 | const int level = 30; |
| 975 | m_deepDir_01 = new DeepDir("modelCancelRemoveAction", level); |
| 976 | QCOMPARE( QFileInfo(m_deepDir_01->path()).exists(), true); |
| 977 | |
| 978 | m_dirModel_01->setPath(m_deepDir_01->path()); |
| 979 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 980 | |
| 981 | QCOMPARE(m_dirModel_01->rowCount(), 1); |
| 982 | connect(m_dirModel_01, SIGNAL(progress(int,int,int)), |
| 983 | this, SLOT(progress(int,int,int))); |
| 984 | connect(m_dirModel_01, SIGNAL(progress(int,int,int)), |
| 985 | this, SLOT(cancel(int,int,int))); |
| 986 | |
| 987 | m_dirModel_01->removeIndex(0); |
| 988 | QTest::qWait(TIME_TO_PROCESS); |
| 989 | QTest::qWait(5); |
| 990 | |
| 991 | QCOMPARE(m_filesRemoved.count() , 0); |
| 992 | QCOMPARE(m_dirModel_01->rowCount(), 1); |
| 993 | QVERIFY(m_progressCurrentItem > 0); // some file were performed |
| 994 | QVERIFY(m_progressPercentDone < 100); // |
| 995 | } |
| 996 | |
| 997 | void TestDirModel::modelTestFileSize() |
| 998 | { |
| 999 | QCOMPARE(m_dirModel_01->fileSize(0), QString("0 Bytes")); |
| 1000 | QCOMPARE(m_dirModel_01->fileSize(1023), QString("1023 Bytes")); |
| 1001 | QCOMPARE(m_dirModel_01->fileSize(1024), QString("1.0 kB")); |
| 1002 | QCOMPARE(m_dirModel_01->fileSize(1000*1000), |
| 1003 | QString("1.0 MB")); |
| 1004 | QCOMPARE(m_dirModel_01->fileSize(1000*1000*1000), |
| 1005 | QString("1.0 GB")); |
| 1006 | } |
| 1007 | |
| 1008 | |
| 1009 | |
| 1010 | void TestDirModel::modelRemoveDirWithHiddenFilesAndLinks() |
| 1011 | { |
| 1012 | const int level = 3; |
| 1013 | m_deepDir_01 = new DeepDir("modelRemoveDirWithHiddenFilesAndLinks", level); |
| 1014 | QCOMPARE( QFileInfo(m_deepDir_01->path()).exists(), true); |
| 1015 | |
| 1016 | m_dirModel_01->setShowHiddenFiles(true); |
| 1017 | m_dirModel_01->setPath(m_deepDir_01->path()); |
| 1018 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1019 | |
| 1020 | QCOMPARE(m_dirModel_01->rowCount(), 1); |
| 1021 | |
| 1022 | connect(m_dirModel_01, SIGNAL(progress(int,int,int)), |
| 1023 | this, SLOT(progress(int,int,int))); |
| 1024 | |
| 1025 | QModelIndex filepathIdx = m_dirModel_01->index(0, DirModel::FilePathRole - DirModel::FileNameRole); |
| 1026 | QString firstItemFullPath(m_dirModel_01->data(filepathIdx).toString()); |
| 1027 | |
| 1028 | // hidden files and links are created under first item |
| 1029 | QFile hiddenFile(firstItemFullPath |
| 1030 | + QDir::separator() |
| 1031 | + QLatin1String(".hidden.txt")); |
| 1032 | |
| 1033 | QCOMPARE(hiddenFile.open(QFile::WriteOnly) ,true); |
| 1034 | hiddenFile.close(); |
| 1035 | QString link_to_hiddenFile(firstItemFullPath |
| 1036 | + QDir::separator() |
| 1037 | + QLatin1String("link_to_hiddenFile")); |
| 1038 | QCOMPARE(hiddenFile.link(link_to_hiddenFile), true); |
| 1039 | |
| 1040 | QString hiddenFolder(firstItemFullPath |
| 1041 | + QDir::separator() |
| 1042 | + QLatin1String(".hiddenFolder")); |
| 1043 | |
| 1044 | QCOMPARE(QDir().mkdir(hiddenFolder) , true); |
| 1045 | QString link_to_hidden_folder(firstItemFullPath |
| 1046 | + QDir::separator() |
| 1047 | + QLatin1String("link_to_hidden_folder")); |
| 1048 | QCOMPARE(QFile(hiddenFolder).link(link_to_hidden_folder), true); |
| 1049 | |
| 1050 | m_dirModel_01->removeIndex(0); |
| 1051 | QTest::qWait(TIME_TO_PROCESS); |
| 1052 | |
| 1053 | QCOMPARE(m_dirModel_01->rowCount(), 0); |
| 1054 | QCOMPARE(m_progressPercentDone, 100); |
| 1055 | } |
| 1056 | |
| 1057 | |
| 1058 | void TestDirModel::modelCancelCopyAction() |
| 1059 | { |
| 1060 | QString orig("modelCancelCopyAction_orig"); |
| 1061 | m_deepDir_01 = new DeepDir(orig, 0); |
| 1062 | |
| 1063 | //create a big file to test copy loop |
| 1064 | QByteArray buf(4096, 't'); |
| 1065 | QFile big(m_deepDir_01->path() + QDir::separator() + "big.txt"); |
| 1066 | QCOMPARE(big.open(QFile::WriteOnly), true); |
| 1067 | for(int i=0; i < 186; i++) |
| 1068 | { |
| 1069 | int wrote = (int) big.write(buf); |
| 1070 | QCOMPARE(wrote, buf.size()); |
| 1071 | buf += "sdfsdsedccw121222"; |
| 1072 | } |
| 1073 | big.close(); |
| 1074 | |
| 1075 | m_dirModel_01->setPath(m_deepDir_01->path()); |
| 1076 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1077 | QCOMPARE(m_dirModel_01->rowCount(), 1); |
| 1078 | |
| 1079 | QString target("modelCancelCopyAction_target"); |
| 1080 | m_deepDir_02 = new DeepDir(target, 0); |
| 1081 | |
| 1082 | connect(m_dirModel_02, SIGNAL(progress(int,int,int)), |
| 1083 | this, SLOT(progress(int,int,int))); |
| 1084 | connect(m_dirModel_02, SIGNAL(progress(int,int,int)), |
| 1085 | this, SLOT(cancel(int,int,int))); |
| 1086 | m_dirModel_02->setPath(m_deepDir_02->path()); |
| 1087 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1088 | |
| 1089 | m_dirModel_01->copyIndex(0); |
| 1090 | m_dirModel_02->paste(); |
| 1091 | QTest::qWait(TIME_TO_PROCESS); |
| 1092 | |
| 1093 | QCOMPARE( QFileInfo(m_deepDir_02->path()).exists(), true); |
| 1094 | QCOMPARE(m_dirModel_02->rowCount(), 0); |
| 1095 | } |
| 1096 | |
| 1097 | void TestDirModel::modelCopyFileAndDirectoryLinks() |
| 1098 | { |
| 1099 | QString orig("modelCopyFileAndDirectoryLinks_orig"); |
| 1100 | m_deepDir_01 = new DeepDir(orig, 1); |
| 1101 | |
| 1102 | //create a link in the same directory to the directory created at first level |
| 1103 | QString firstDir = m_deepDir_01->lastLevel(); |
| 1104 | QString link_to_firstDir = m_deepDir_01->path() + QDir::separator() |
| 1105 | + QLatin1String("link_to_firstDir"); |
| 1106 | QCOMPARE(createLink(firstDir, link_to_firstDir), true); |
| 1107 | |
| 1108 | QDir d(firstDir); |
| 1109 | QFileInfoList files = d.entryInfoList(QDir::Files); |
| 1110 | QVERIFY(files.count() > 0); |
| 1111 | QCOMPARE(createLink(files.at(0).absoluteFilePath(), QLatin1String("link_to_file")), true); |
| 1112 | |
| 1113 | m_dirModel_01->setPath(m_deepDir_01->path()); |
| 1114 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1115 | QCOMPARE(m_dirModel_01->rowCount(), 2); |
| 1116 | |
| 1117 | |
| 1118 | QString target("modelCopyFileAndDirectoryLinks_target"); |
| 1119 | m_deepDir_02 = new DeepDir(target, 0); |
| 1120 | connect(m_dirModel_02, SIGNAL(progress(int,int,int)), |
| 1121 | this, SLOT(progress(int,int,int))); |
| 1122 | |
| 1123 | m_dirModel_02->setPath(m_deepDir_02->path()); |
| 1124 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1125 | |
| 1126 | QStringList items(firstDir); |
| 1127 | items.append(link_to_firstDir); |
| 1128 | |
| 1129 | m_dirModel_01->copyPaths(items); |
| 1130 | QTest::qWait(10); |
| 1131 | m_dirModel_02->paste(); |
| 1132 | QTest::qWait(TIME_TO_PROCESS); |
| 1133 | |
| 1134 | QCOMPARE(m_dirModel_01->rowCount(), 2); |
| 1135 | QCOMPARE(m_dirModel_02->rowCount(), 2); |
| 1136 | QCOMPARE(compareDirectories(m_deepDir_01->path(), m_deepDir_02->path()), true); |
| 1137 | QCOMPARE(m_progressPercentDone, 100); |
| 1138 | QCOMPARE(m_progressCurrentItem, m_progressTotalItems); |
| 1139 | } |
| 1140 | |
| 1141 | |
| 1142 | void TestDirModel::modelCutAndPasteInTheSamePlace() |
| 1143 | { |
| 1144 | QString orig("modelCutAndPasteInTheSamePlace_orig"); |
| 1145 | m_deepDir_01 = new DeepDir (orig,0); // just to be removed |
| 1146 | const int files_to_create = 4; |
| 1147 | |
| 1148 | TempFiles files; |
| 1149 | files.addSubDirLevel(orig); |
| 1150 | files.create(files_to_create); |
| 1151 | |
| 1152 | QFileInfo info_before[files_to_create]; |
| 1153 | QStringList created_files(files.createdList()); |
| 1154 | |
| 1155 | QCOMPARE(created_files.count(), files_to_create); |
| 1156 | int counter = 0; |
| 1157 | for(counter=0; counter < files_to_create; counter++) |
| 1158 | { |
| 1159 | info_before[counter] = QFileInfo(created_files.at(counter)); |
| 1160 | } |
| 1161 | |
| 1162 | m_dirModel_01->setPath(files.lastPath()); |
| 1163 | connect(m_dirModel_01, SIGNAL(error(QString,QString)), |
| 1164 | this, SLOT(slotError(QString,QString))); |
| 1165 | |
| 1166 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1167 | QCOMPARE(m_dirModel_01->rowCount(), files_to_create); |
| 1168 | |
| 1169 | m_dirModel_01->cutPaths(created_files); |
| 1170 | m_dirModel_01->paste(); |
| 1171 | QTest::qWait(TIME_TO_PROCESS); |
| 1172 | m_dirModel_01->refresh(); |
| 1173 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1174 | |
| 1175 | |
| 1176 | QCOMPARE(m_dirModel_01->rowCount(), files_to_create); |
| 1177 | for(counter=0; counter < files_to_create; counter++) |
| 1178 | { |
| 1179 | //files exist and did not were touched |
| 1180 | QCOMPARE( QFileInfo(created_files.at(counter)).lastModified(), |
| 1181 | info_before[counter].lastModified()) ; |
| 1182 | } |
| 1183 | |
| 1184 | QCOMPARE(m_receivedErrorSignal, true); |
| 1185 | } |
| 1186 | |
| 1187 | |
| 1188 | void TestDirModel::modelCopyAndPasteToBackupFiles() |
| 1189 | { |
| 1190 | QString orig("modelBackupFiles_orig"); |
| 1191 | m_deepDir_01 = new DeepDir (orig,5); // just to be removed |
| 1192 | const int files_to_create = 12; |
| 1193 | |
| 1194 | TempFiles files; |
| 1195 | files.addSubDirLevel(orig); |
| 1196 | files.create(files_to_create); |
| 1197 | |
| 1198 | QStringList created_files(files.createdList()); |
| 1199 | QCOMPARE(created_files.count(), files_to_create); |
| 1200 | |
| 1201 | m_dirModel_01->setPath(files.lastPath()); |
| 1202 | |
| 1203 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1204 | QCOMPARE(m_dirModel_01->rowCount(), files_to_create + 1); |
| 1205 | created_files.append(m_deepDir_01->firstLevel()); |
| 1206 | |
| 1207 | m_dirModel_01->copyPaths(created_files); |
| 1208 | connect(m_dirModel_02, SIGNAL(error(QString,QString)), |
| 1209 | this, SLOT(slotError(QString,QString))); |
| 1210 | |
| 1211 | m_dirModel_02->setPath(m_deepDir_01->path()); |
| 1212 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1213 | m_dirModel_02->paste(); |
| 1214 | QTest::qWait(TIME_TO_PROCESS); |
| 1215 | |
| 1216 | QCOMPARE(m_dirModel_02->rowCount(), (files_to_create + 1) * 2); |
| 1217 | QCOMPARE(m_receivedErrorSignal, false); |
| 1218 | |
| 1219 | m_dirModel_02->paste(); |
| 1220 | QTest::qWait(TIME_TO_PROCESS); |
| 1221 | |
| 1222 | QCOMPARE(m_dirModel_02->rowCount(), (files_to_create + 1) * 3); |
| 1223 | QCOMPARE(m_receivedErrorSignal, false); |
| 1224 | } |
| 1225 | |
| 1226 | |
| 1227 | |
| 1228 | void TestDirModel::modelCopyAndPaste3Times() |
| 1229 | { |
| 1230 | QString orig("modelCopyAndPaste3Times_orig"); |
| 1231 | m_deepDir_01 = new DeepDir(orig, 5); |
| 1232 | |
| 1233 | TempFiles moreFiles; |
| 1234 | QCOMPARE(moreFiles.addSubDirLevel(orig), true); |
| 1235 | QCOMPARE(moreFiles.create(10), true); |
| 1236 | |
| 1237 | QStringList items(m_deepDir_01->firstLevel()); |
| 1238 | items.append(moreFiles.createdList()); |
| 1239 | |
| 1240 | QString target1("modelCopyAndPaste3Times_target1"); |
| 1241 | QString target2("modelCopyAndPaste3Times_target2"); |
| 1242 | QString target3("modelCopyAndPaste3Times_target3"); |
| 1243 | |
| 1244 | DeepDir t1(target1,0); |
| 1245 | DeepDir t2(target2,0); |
| 1246 | DeepDir t3(target3,0); |
| 1247 | |
| 1248 | m_dirModel_01->setPath(m_deepDir_01->path()); |
| 1249 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1250 | QCOMPARE(items.count(), m_dirModel_01->rowCount()); |
| 1251 | m_dirModel_01->copyPaths(items); |
| 1252 | |
| 1253 | DirModel model1; |
| 1254 | model1.setPath(t1.path()); |
| 1255 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1256 | QCOMPARE(model1.rowCount() , 0); |
| 1257 | model1.paste(); |
| 1258 | QTest::qWait(TIME_TO_PROCESS); |
| 1259 | QCOMPARE(model1.rowCount(), items.count()); |
| 1260 | QCOMPARE(compareDirectories(m_deepDir_01->path(), t1.path()), true); |
| 1261 | |
| 1262 | DirModel model2; |
| 1263 | model2.setPath(t2.path()); |
| 1264 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1265 | QCOMPARE(model2.rowCount() , 0); |
| 1266 | model2.paste(); |
| 1267 | QTest::qWait(TIME_TO_PROCESS); |
| 1268 | QCOMPARE(model2.rowCount(), items.count()); |
| 1269 | QCOMPARE(compareDirectories(m_deepDir_01->path(), t2.path()), true); |
| 1270 | |
| 1271 | DirModel model3; |
| 1272 | model3.setPath(t3.path()); |
| 1273 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1274 | QCOMPARE(model3.rowCount() , 0); |
| 1275 | model3.paste(); |
| 1276 | QTest::qWait(TIME_TO_PROCESS); |
| 1277 | QCOMPARE(model3.rowCount(), items.count()); |
| 1278 | QCOMPARE(compareDirectories(m_deepDir_01->path(), t3.path()), true); |
| 1279 | } |
| 1280 | |
| 1281 | |
| 1282 | void TestDirModel::modelCutAndPaste3Times() |
| 1283 | { |
| 1284 | QString orig("modelCutAndPaste3Times_orig"); |
| 1285 | m_deepDir_01 = new DeepDir(orig, 5); |
| 1286 | |
| 1287 | TempFiles moreFiles; |
| 1288 | QCOMPARE(moreFiles.addSubDirLevel(orig), true); |
| 1289 | QCOMPARE(moreFiles.create(10), true); |
| 1290 | |
| 1291 | QStringList items(m_deepDir_01->firstLevel()); |
| 1292 | items.append(moreFiles.createdList()); |
| 1293 | |
| 1294 | QString target1("modelCutAndPaste3Times_target1"); |
| 1295 | QString target2("modelCutAndPaste3Times_target2"); |
| 1296 | QString target3("modelCutAndPaste3Times_target3"); |
| 1297 | |
| 1298 | DeepDir t1(target1,0); |
| 1299 | DeepDir t2(target2,0); |
| 1300 | DeepDir t3(target3,0); |
| 1301 | |
| 1302 | m_dirModel_01->setPath(m_deepDir_01->path()); |
| 1303 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1304 | QCOMPARE(items.count(), m_dirModel_01->rowCount()); |
| 1305 | m_dirModel_01->cutPaths(items); |
| 1306 | |
| 1307 | DirModel model1; |
| 1308 | model1.setPath(t1.path()); |
| 1309 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1310 | QCOMPARE(model1.rowCount() , 0); |
| 1311 | model1.paste(); |
| 1312 | QTest::qWait(TIME_TO_PROCESS); |
| 1313 | QCOMPARE(model1.rowCount(), items.count()); |
| 1314 | QCOMPARE(m_dirModel_01->rowCount() , 0 ); |
| 1315 | |
| 1316 | DirModel model2; |
| 1317 | model2.setPath(t2.path()); |
| 1318 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1319 | QCOMPARE(model2.rowCount() , 0); |
| 1320 | model2.paste(); |
| 1321 | QTest::qWait(TIME_TO_PROCESS); |
| 1322 | QCOMPARE(model2.rowCount(), items.count()); |
| 1323 | |
| 1324 | DirModel model3; |
| 1325 | model3.setPath(t3.path()); |
| 1326 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1327 | QCOMPARE(model3.rowCount() , 0); |
| 1328 | model3.paste(); |
| 1329 | QTest::qWait(TIME_TO_PROCESS); |
| 1330 | QCOMPARE(model3.rowCount(), items.count()); |
| 1331 | } |
| 1332 | |
| 1333 | |
| 1334 | void TestDirModel::modelCopyPasteAndPasteAgain() |
| 1335 | { |
| 1336 | QString orig("modelCopyPasteAndPasteAgain_orig"); |
| 1337 | m_deepDir_01 = new DeepDir(orig, 1); |
| 1338 | connect(m_dirModel_01, SIGNAL(error(QString,QString)), |
| 1339 | this, SLOT(slotError(QString,QString))); |
| 1340 | |
| 1341 | m_dirModel_01->setPath(m_deepDir_01->path()); |
| 1342 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1343 | QCOMPARE(m_dirModel_01->rowCount(), 1); |
| 1344 | |
| 1345 | |
| 1346 | QString target("modelCopyPasteAndPasteAgain_target"); |
| 1347 | m_deepDir_02 = new DeepDir(target, 0); |
| 1348 | connect(m_dirModel_02, SIGNAL(error(QString,QString)), |
| 1349 | this, SLOT(slotError(QString,QString))); |
| 1350 | |
| 1351 | m_dirModel_02->setPath(m_deepDir_02->path()); |
| 1352 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1353 | QCOMPARE(m_dirModel_02->rowCount(), 0); |
| 1354 | |
| 1355 | m_dirModel_01->copyIndex(0); |
| 1356 | |
| 1357 | //first time |
| 1358 | m_dirModel_02->paste(); |
| 1359 | QTest::qWait(TIME_TO_PROCESS); |
| 1360 | QCOMPARE(m_filesRemoved.count(), 0); |
| 1361 | QCOMPARE(m_filesAdded.count(), 1); |
| 1362 | |
| 1363 | //second time |
| 1364 | m_dirModel_02->paste(); |
| 1365 | QTest::qWait(TIME_TO_PROCESS); |
| 1366 | |
| 1367 | QCOMPARE(compareDirectories(m_deepDir_01->path(), m_deepDir_02->path()), true); |
| 1368 | QCOMPARE(m_receivedErrorSignal, false); |
| 1369 | |
| 1370 | //when items already exist, changed signal is emitted |
| 1371 | QCOMPARE(m_filesRemoved.count(), 0); |
| 1372 | QCOMPARE(m_filesAdded.count(), 1); |
| 1373 | } |
| 1374 | |
| 1375 | |
| 1376 | void TestDirModel::modelCutPasteIntoExistentItems() |
| 1377 | { |
| 1378 | QString orig("modelCutPasteIntoExistentItems_orig"); |
| 1379 | m_deepDir_01 = new DeepDir(orig, 0); |
| 1380 | QString moreOneLevel("MoreOneLevel"); |
| 1381 | TempFiles tempFiles_01; |
| 1382 | TempFiles tempDir_01; |
| 1383 | |
| 1384 | const int createCounterTopLevel = 8; |
| 1385 | const int createCounterSubLevel = 2; |
| 1386 | |
| 1387 | tempFiles_01.addSubDirLevel(orig); |
| 1388 | tempFiles_01.create(createCounterTopLevel); |
| 1389 | tempDir_01.addSubDirLevel(orig); |
| 1390 | tempDir_01.addSubDirLevel(moreOneLevel); |
| 1391 | tempDir_01.create(2); |
| 1392 | |
| 1393 | connect(m_dirModel_01, SIGNAL(error(QString,QString)), |
| 1394 | this, SLOT(slotError(QString,QString))); |
| 1395 | m_dirModel_01->setPath(tempFiles_01.lastPath()); |
| 1396 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1397 | QCOMPARE(m_dirModel_01->rowCount(), createCounterTopLevel+1); //those created top level plus the subfolder |
| 1398 | |
| 1399 | QString target("modelCutPasteIntoExistentItems_target"); |
| 1400 | m_deepDir_02 = new DeepDir(target, 0); |
| 1401 | TempFiles tempFiles_02; |
| 1402 | TempFiles tempDir_02; |
| 1403 | |
| 1404 | tempFiles_02.addSubDirLevel(target); |
| 1405 | tempFiles_02.create(createCounterTopLevel); |
| 1406 | tempDir_02.addSubDirLevel(target); |
| 1407 | tempDir_02.addSubDirLevel(moreOneLevel); |
| 1408 | tempDir_02.create(createCounterSubLevel); |
| 1409 | |
| 1410 | connect(m_dirModel_02, SIGNAL(error(QString,QString)), |
| 1411 | this, SLOT(slotError(QString,QString))); |
| 1412 | connect(m_dirModel_02, SIGNAL(progress(int,int,int)), |
| 1413 | this, SLOT(progress(int,int,int))); |
| 1414 | m_dirModel_02->setPath(tempFiles_02.lastPath()); |
| 1415 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1416 | |
| 1417 | QCOMPARE(m_dirModel_02->rowCount(), createCounterTopLevel+1); //those created top level plus the subfolder |
| 1418 | |
| 1419 | //both directories have the same content |
| 1420 | QCOMPARE(compareDirectories(tempFiles_01.lastPath(), tempFiles_02.lastPath()), true); |
| 1421 | |
| 1422 | //cut from first Model |
| 1423 | QStringList items(tempFiles_01.createdList()); |
| 1424 | items.append(tempDir_01.lastPath()); |
| 1425 | m_dirModel_01->cutPaths(items); |
| 1426 | //paste into the second model |
| 1427 | m_dirModel_02->paste(); |
| 1428 | QTest::qWait(TIME_TO_PROCESS *3); |
| 1429 | |
| 1430 | QCOMPARE(m_receivedErrorSignal, false); |
| 1431 | |
| 1432 | //only one directory that already exists will be removed using another Action |
| 1433 | QCOMPARE(m_filesRemoved.count(), 1); |
| 1434 | //when items being copied from COPY or renamed from CUT already exist, they are not added |
| 1435 | QCOMPARE(m_filesAdded.count(), 0); |
| 1436 | } |
| 1437 | |
| 1438 | |
| 1439 | void TestDirModel::openPathAbsouluteAndRelative() |
| 1440 | { |
| 1441 | QString orig("openPathAbsouluteAndRelative"); |
| 1442 | m_deepDir_01 = new DeepDir(orig, 1); |
| 1443 | |
| 1444 | connect(m_dirModel_01, SIGNAL(error(QString,QString)), |
| 1445 | this, SLOT(slotError(QString,QString))); |
| 1446 | |
| 1447 | connect(m_dirModel_01, SIGNAL(pathChanged(QString)), |
| 1448 | this, SLOT(slotPathChamged(QString))); |
| 1449 | |
| 1450 | m_currentPath.clear(); |
| 1451 | bool ret = m_dirModel_01->openPath(QLatin1String("_ItDoesNotExist_")); |
| 1452 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1453 | |
| 1454 | QCOMPARE(ret, false); |
| 1455 | QCOMPARE(m_currentPath.isEmpty(), true); |
| 1456 | |
| 1457 | ret = m_dirModel_01->openPath(m_deepDir_01->path()); |
| 1458 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1459 | QCOMPARE(ret, true); |
| 1460 | QCOMPARE(m_currentPath, m_deepDir_01->path()); |
| 1461 | QCOMPARE(m_dirModel_01->rowCount(), 1); |
| 1462 | QCOMPARE(m_receivedErrorSignal, false); |
| 1463 | |
| 1464 | ret = m_dirModel_01->openPath(QLatin1String("..")); |
| 1465 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1466 | QCOMPARE(ret, true); |
| 1467 | QCOMPARE(m_currentPath, QDir::tempPath()); |
| 1468 | QCOMPARE(m_receivedErrorSignal, false); |
| 1469 | |
| 1470 | ret = m_dirModel_01->openPath(orig); |
| 1471 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1472 | QCOMPARE(ret, true); |
| 1473 | QCOMPARE(m_currentPath, m_deepDir_01->path()); |
| 1474 | QCOMPARE(m_receivedErrorSignal, false); |
| 1475 | |
| 1476 | // trash -------------------------------------- |
| 1477 | TempFiles files; |
| 1478 | QCOMPARE(files.addSubDirLevel(m_deepDir_01->path()), true); |
| 1479 | |
| 1480 | createTempHomeTrashDir(m_deepDir_01->path()); |
| 1481 | |
| 1482 | QTrashDir tempTrash; |
| 1483 | QString homeTrashDir = tempTrash.homeTrash(); |
| 1484 | QCOMPARE(files.addSubDirLevel(homeTrashDir), true); |
| 1485 | QString filesHomeTrashDir = QTrashUtilInfo::filesTrashDir(homeTrashDir); |
| 1486 | QCOMPARE(files.addSubDirLevel(filesHomeTrashDir), true); |
| 1487 | |
| 1488 | QString level1("Level1"); |
| 1489 | QCOMPARE(files.addSubDirLevel(level1), true); |
| 1490 | |
| 1491 | QTrashUtilInfo trashInfo; |
| 1492 | trashInfo.setInfo(tempTrash.homeTrash(), level1); |
| 1493 | QCOMPARE(trashInfo.existsFile() , true); |
| 1494 | QFile infoFile(trashInfo.absInfo); |
| 1495 | QCOMPARE(infoFile.open(QFile::WriteOnly), true); |
| 1496 | infoFile.close(); |
| 1497 | |
| 1498 | //check if "Level1" is valid item under trash |
| 1499 | QCOMPARE(trashInfo.existsInfoFile() , true); |
| 1500 | |
| 1501 | QString level2("level2"); |
| 1502 | QString level3("level3"); |
| 1503 | |
| 1504 | QCOMPARE(files.addSubDirLevel(level2), true); |
| 1505 | QCOMPARE(files.addSubDirLevel(level3), true); |
| 1506 | |
| 1507 | files.create(1); |
| 1508 | |
| 1509 | // using trash:/// |
| 1510 | ret = m_dirModel_01->openPath(LocationUrl::TrashRootURL); |
| 1511 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1512 | QCOMPARE(ret, true); |
| 1513 | QCOMPARE(m_currentPath, LocationUrl::TrashRootURL); |
| 1514 | QCOMPARE(m_receivedErrorSignal, false); |
| 1515 | |
| 1516 | // using relative "Level1" |
| 1517 | ret = m_dirModel_01->openPath(level1); |
| 1518 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1519 | QCOMPARE(ret, true); |
| 1520 | QCOMPARE(m_currentPath, QString(LocationUrl::TrashRootURL + level1) ); |
| 1521 | QCOMPARE(m_receivedErrorSignal, false); |
| 1522 | |
| 1523 | //using trash:///Level1/Level2/Level3 |
| 1524 | QString deep(LocationUrl::TrashRootURL + level1 + QDir::separator() + level2 + QDir::separator() + level3); |
| 1525 | ret = m_dirModel_01->openPath(deep); |
| 1526 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1527 | QCOMPARE(ret, true); |
| 1528 | QCOMPARE(m_receivedErrorSignal, false); |
| 1529 | |
| 1530 | //using ../ to go up into Level2 |
| 1531 | ret = m_dirModel_01->openPath("../"); |
| 1532 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1533 | QCOMPARE(ret, true); |
| 1534 | QCOMPARE(m_receivedErrorSignal, false); |
| 1535 | |
| 1536 | //using .. to go up into Level1 |
| 1537 | ret = m_dirModel_01->openPath(".."); |
| 1538 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1539 | QCOMPARE(ret, true); |
| 1540 | QCOMPARE(m_receivedErrorSignal, false); |
| 1541 | |
| 1542 | //back to trash:/// |
| 1543 | ret = m_dirModel_01->openPath(".."); |
| 1544 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1545 | QCOMPARE(ret, true); |
| 1546 | QCOMPARE(m_currentPath, LocationUrl::TrashRootURL); |
| 1547 | QCOMPARE(m_receivedErrorSignal, false); |
| 1548 | |
| 1549 | //now it must fail |
| 1550 | ret = m_dirModel_01->openPath(".."); |
| 1551 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1552 | QCOMPARE(ret, false); |
| 1553 | QCOMPARE(m_receivedErrorSignal, false); |
| 1554 | |
| 1555 | ret = m_dirModel_01->openPath("file:///"); |
| 1556 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1557 | QCOMPARE(ret, true); |
| 1558 | QCOMPARE(m_currentPath, QDir::rootPath()); |
| 1559 | QCOMPARE(m_receivedErrorSignal, false); |
| 1560 | |
| 1561 | ret = m_dirModel_01->openPath("file://"); |
| 1562 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1563 | QCOMPARE(ret, true); |
| 1564 | QCOMPARE(m_currentPath, QDir::rootPath()); |
| 1565 | QCOMPARE(m_receivedErrorSignal, false); |
| 1566 | |
| 1567 | ret = m_dirModel_01->openPath("file:/"); |
| 1568 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1569 | QCOMPARE(ret, true); |
| 1570 | QCOMPARE(m_currentPath, QDir::rootPath()); |
| 1571 | QCOMPARE(m_receivedErrorSignal, false); |
| 1572 | } |
| 1573 | |
| 1574 | |
| 1575 | void TestDirModel::existsDirAnCanReadDir() |
| 1576 | { |
| 1577 | QString orig("existsDirAnCanReadDir"); |
| 1578 | m_deepDir_01 = new DeepDir(orig, 1); |
| 1579 | |
| 1580 | connect(m_dirModel_01, SIGNAL(error(QString,QString)), |
| 1581 | this, SLOT(slotError(QString,QString))); |
| 1582 | |
| 1583 | connect(m_dirModel_01, SIGNAL(pathChanged(QString)), |
| 1584 | this, SLOT(slotPathChamged(QString))); |
| 1585 | |
| 1586 | m_currentPath.clear(); |
| 1587 | m_dirModel_01->goHome(); |
| 1588 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1589 | |
| 1590 | //test absoulute path |
| 1591 | QCOMPARE(m_dirModel_01->existsDir(QDir::tempPath()), true); |
| 1592 | QCOMPARE(m_dirModel_01->canReadDir(QDir::tempPath()), true); |
| 1593 | |
| 1594 | //relative does not exist |
| 1595 | QString relativeDoesNoEist("__IT_May_Not_Exist_in_Home__HAHA"); |
| 1596 | QCOMPARE(m_dirModel_01->existsDir(relativeDoesNoEist), false); |
| 1597 | QCOMPARE(m_dirModel_01->canReadDir(relativeDoesNoEist), false); |
| 1598 | |
| 1599 | //relative exists |
| 1600 | m_dirModel_01->setPath(QDir::tempPath()); |
| 1601 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1602 | QCOMPARE(m_dirModel_01->existsDir(orig), true); |
| 1603 | QCOMPARE(m_dirModel_01->canReadDir(orig), true); |
| 1604 | |
| 1605 | //check permissions from canReadDir() |
| 1606 | bool ok = QFile::setPermissions(m_deepDir_01->path(), QFileDevice::WriteOwner); |
| 1607 | QCOMPARE(ok, true); |
| 1608 | QCOMPARE(m_dirModel_01->existsDir(orig), true); |
| 1609 | QCOMPARE(m_dirModel_01->canReadDir(orig), false); |
| 1610 | QFile::setPermissions(m_deepDir_01->path(), QFileDevice::ReadOwner | QFileDevice::WriteOwner); |
| 1611 | |
| 1612 | ok = QFile::setPermissions(m_deepDir_01->path(), |
| 1613 | QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ExeOwner ); |
| 1614 | QCOMPARE(ok, true); |
| 1615 | QCOMPARE(m_dirModel_01->existsDir(orig), true); |
| 1616 | QCOMPARE(m_dirModel_01->canReadDir(orig), true); |
| 1617 | QCOMPARE(m_dirModel_01->existsFile(orig), false); |
| 1618 | } |
| 1619 | |
| 1620 | void TestDirModel::existsFileAndCanReadFile() |
| 1621 | { |
| 1622 | QString orig("existsFileAndCanReadFile"); |
| 1623 | m_deepDir_01 = new DeepDir(orig, 1); |
| 1624 | |
| 1625 | m_dirModel_01->setPath(m_deepDir_01->path()); |
| 1626 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1627 | |
| 1628 | QString fileName("myFile.txt"); |
| 1629 | QCOMPARE(m_dirModel_01->rowCount(), 1) ; |
| 1630 | QCOMPARE(m_dirModel_01->existsFile(fileName), false); |
| 1631 | QCOMPARE(m_dirModel_01->canReadFile(fileName), false); |
| 1632 | |
| 1633 | TempFiles temp; |
| 1634 | temp.addSubDirLevel(orig); |
| 1635 | temp.create(fileName,1); |
| 1636 | m_dirModel_01->refresh(); |
| 1637 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1638 | QCOMPARE(m_dirModel_01->rowCount(), 2) ; |
| 1639 | |
| 1640 | // it has a counter in the name like "myFile.txt_00 |
| 1641 | fileName = temp.lastNameCreated(); |
| 1642 | |
| 1643 | //relative |
| 1644 | QCOMPARE(m_dirModel_01->existsFile(fileName), true); |
| 1645 | QCOMPARE(m_dirModel_01->canReadFile(fileName), true); |
| 1646 | QCOMPARE(m_dirModel_01->existsDir(fileName), false); |
| 1647 | |
| 1648 | //absolute |
| 1649 | QCOMPARE(m_dirModel_01->existsFile(temp.lastFileCreated()), true); |
| 1650 | QCOMPARE(m_dirModel_01->canReadFile(temp.lastFileCreated()), true); |
| 1651 | |
| 1652 | QCOMPARE(m_dirModel_01->existsDir(temp.lastFileCreated()), false); |
| 1653 | QCOMPARE(m_dirModel_01->canReadDir(temp.lastFileCreated()), false); |
| 1654 | |
| 1655 | bool ok = m_dirModel_01->cdIntoPath( m_deepDir_01->lastLevel()); |
| 1656 | QCOMPARE(ok, true); |
| 1657 | |
| 1658 | //relative |
| 1659 | QString fileIsUp = QLatin1String("..") + QDir::separator() + fileName; |
| 1660 | QCOMPARE(m_dirModel_01->existsFile(fileIsUp), true); |
| 1661 | QCOMPARE(m_dirModel_01->canReadFile(fileIsUp), true); |
| 1662 | |
| 1663 | ok = QFile::setPermissions(temp.lastFileCreated(), QFileDevice::WriteOwner); |
| 1664 | QCOMPARE(ok, true); |
| 1665 | QCOMPARE(m_dirModel_01->existsFile(fileIsUp), true); |
| 1666 | QCOMPARE(m_dirModel_01->canReadFile(fileIsUp), false); |
| 1667 | } |
| 1668 | |
| 1669 | |
| 1670 | void TestDirModel::pathProperties() |
| 1671 | { |
| 1672 | QString orig("pathProperties"); |
| 1673 | m_deepDir_01 = new DeepDir(orig, 0); |
| 1674 | |
| 1675 | QTest::qWait(1000); // wait one second |
| 1676 | |
| 1677 | TempFiles temp; |
| 1678 | temp.addSubDirLevel(orig); |
| 1679 | temp.create(1); |
| 1680 | |
| 1681 | m_dirModel_01->setPath(m_deepDir_01->path()); |
| 1682 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1683 | |
| 1684 | QCOMPARE(m_dirModel_01->curPathIsWritable(), true); |
| 1685 | bool ok = QFile::setPermissions(m_deepDir_01->path(), |
| 1686 | QFileDevice::ReadOwner | QFileDevice::ExeOwner ); |
| 1687 | |
| 1688 | QCOMPARE(ok, true); |
| 1689 | QCOMPARE(m_dirModel_01->curPathIsWritable(), false); |
| 1690 | |
| 1691 | ok = QFile::setPermissions(m_deepDir_01->path(), |
| 1692 | QFileDevice::ReadOwner | QFileDevice::ExeOwner | QFileDevice::WriteOwner); |
| 1693 | |
| 1694 | QCOMPARE(ok, true); |
| 1695 | qWarning("created %s", m_dirModel_01->curPathCreatedDateLocaleShort().toLocal8Bit().constData()); |
| 1696 | qWarning("modified %s", m_dirModel_01->curPathModifiedDateLocaleShort().toLocal8Bit().constData()); |
| 1697 | |
| 1698 | } |
| 1699 | |
| 1700 | |
| 1701 | /** description: |
| 1702 | * |
| 1703 | * 1. 2 file managers in /tmp/watchExternalChanges, m_dirModel_01 and m_dirModel_02 |
| 1704 | * 2. level_01 will be created under /tmp/watchExternalChanges, |
| 1705 | * so both m_dirModel_01 and m_dirModel_02 start with 1 item |
| 1706 | * 3. Under /tmp/watchExternalChanges/level_01 10 items "from_level_01.cut_nn" are created |
| 1707 | * 4 A third File manager is created pointing to /tmp/watchExternalChanges/level_01 |
| 1708 | * 5 A loop is created to move all files under /tmp/watchExternalChanges/level_01 |
| 1709 | * to /tmp/watchExternalChanges where there are the 2 File Manager instances /tmp/watchExternalChanges/level_01 |
| 1710 | * inside the loop: |
| 1711 | * 5.1 files are created using createdOutsideName which is a TemFiles object |
| 1712 | * 5.2 from m_dirModel_01 a file can be removed depending on the current time |
| 1713 | */ |
| 1714 | void TestDirModel::watchExternalChanges() |
| 1715 | { |
| 1716 | QString orig("watchExternalChanges"); |
| 1717 | m_deepDir_01 = new DeepDir(orig, 0); |
| 1718 | TempFiles tempUnderFirstLevel; |
| 1719 | tempUnderFirstLevel.addSubDirLevel(orig); |
| 1720 | tempUnderFirstLevel.addSubDirLevel(QLatin1String("level_01")); |
| 1721 | const int cut_items = 10; |
| 1722 | tempUnderFirstLevel.create("from_level_01.cut", cut_items); |
| 1723 | |
| 1724 | DirModel thirdFM; |
| 1725 | thirdFM.setPath(tempUnderFirstLevel.lastPath()); |
| 1726 | |
| 1727 | |
| 1728 | connect(m_dirModel_01, SIGNAL(error(QString,QString)), |
| 1729 | this, SLOT(slotError(QString,QString))); |
| 1730 | m_dirModel_01->setPath(m_deepDir_01->path()); |
| 1731 | |
| 1732 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1733 | |
| 1734 | connect(m_dirModel_02, SIGNAL(error(QString,QString)), |
| 1735 | this, SLOT(slotError(QString,QString))); |
| 1736 | m_dirModel_02->setPath(m_deepDir_01->path()); |
| 1737 | |
| 1738 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 1739 | |
| 1740 | QCOMPARE(thirdFM.rowCount(), cut_items); |
| 1741 | QCOMPARE(m_dirModel_01->rowCount(), 1); |
| 1742 | QCOMPARE(m_dirModel_02->rowCount(), 1); |
| 1743 | |
| 1744 | qDebug() << "dirModelObjs:" << m_dirModel_01 << m_dirModel_02 << &thirdFM; |
| 1745 | |
| 1746 | QString createdOutsideName; |
| 1747 | TempFiles createdOutsideFiles; |
| 1748 | createdOutsideFiles.addSubDirLevel(orig); |
| 1749 | int total_removed = 0; |
| 1750 | int odd=0; |
| 1751 | |
| 1752 | thirdFM.setEnabledExternalFSWatcher(true); |
| 1753 | |
| 1754 | for ( int counter = 0; counter < cut_items; ++counter ) |
| 1755 | { |
| 1756 | thirdFM.cutIndex(0); |
| 1757 | if ( (odd^=1) ) |
| 1758 | { |
| 1759 | m_dirModel_01->paste(); |
| 1760 | } |
| 1761 | else |
| 1762 | { |
| 1763 | m_dirModel_02->paste(); |
| 1764 | } |
| 1765 | QTest::qWait(TIME_TO_PROCESS); |
| 1766 | //at least one file is removed |
| 1767 | if (m_dirModel_02->rowCount() > 1 && |
| 1768 | (!total_removed || (QDateTime::currentDateTime().time().msec() % 100) == 0)) |
| 1769 | { |
| 1770 | //using index 1 because 0 is a directory and the items are being moved up |
| 1771 | m_dirModel_02->removeIndex(1); |
| 1772 | QTest::qWait(TIME_TO_PROCESS); |
| 1773 | ++total_removed; |
| 1774 | } |
| 1775 | createdOutsideName.sprintf("created_%d", counter); |
| 1776 | createdOutsideFiles.create(createdOutsideName); |
| 1777 | } |
| 1778 | int total_created = 1 + cut_items + createdOutsideFiles.created() - total_removed; |
| 1779 | QTest::qWait(EX_FS_WATCHER_TIMER_INTERVAL * 2); |
| 1780 | qWarning("using 2 instances [cut_items]=%d [created outside]=%d, [removed outside]=%d", cut_items, createdOutsideFiles.created(), total_removed); |
| 1781 | |
| 1782 | QCOMPARE(m_dirModel_01->rowCount(), total_created); |
| 1783 | if (m_dirModel_02->getEnabledExternalFSWatcher()) |
| 1784 | { |
| 1785 | QCOMPARE(m_dirModel_02->rowCount(), total_created); |
| 1786 | } |
| 1787 | } |
| 1788 | |
| 1789 | |
| 1790 | void TestDirModel::getThemeIcons() |
| 1791 | { |
| 1792 | QStringList mimesToTest = QStringList() |
| 1793 | << "text/plain" |
| 1794 | << "text/x-c++src" |
| 1795 | << "text/x-csrc" |
| 1796 | << "inode/directory" |
| 1797 | << "application/msword" |
| 1798 | << "application/octet-stream" |
| 1799 | << "application/pdf" |
| 1800 | << "application/postscript" |
| 1801 | << "application/x-bzip-compressed-tar" |
| 1802 | << "application/x-executable" |
| 1803 | << "application/x-gzip" |
| 1804 | << "application/x-shellscript" |
| 1805 | ; |
| 1806 | QMimeDatabase mimeBase; |
| 1807 | QString msg; |
| 1808 | QHash<QByteArray, QString> md5IconsTable; |
| 1809 | |
| 1810 | qDebug() << "QIcon::themeSearchPaths()" << QIcon::themeSearchPaths(); |
| 1811 | qDebug() << "QIcon::themeName()" << QIcon::themeName(); |
| 1812 | |
| 1813 | |
| 1814 | for (int counter=0; counter < mimesToTest.count(); counter++) |
| 1815 | { |
| 1816 | QMimeType mimetype = mimeBase.mimeTypeForName(mimesToTest.at(counter)); |
| 1817 | |
| 1818 | msg = QLatin1String("invalid mimetype ") + mimesToTest.at(counter); |
| 1819 | if (!mimetype.isValid()) |
| 1820 | { |
| 1821 | QSKIP_ALL_TESTS(qPrintable(msg)); |
| 1822 | } |
| 1823 | QString iconName = mimetype.iconName(); |
| 1824 | if (!QIcon::hasThemeIcon(iconName) && QIcon::hasThemeIcon(mimetype.genericIconName())) |
| 1825 | { |
| 1826 | iconName = mimetype.genericIconName(); |
| 1827 | } |
| 1828 | QIcon icon = QIcon::fromTheme(iconName); |
| 1829 | msg = QLatin1String("invalid QIcon::fromTheme ") + iconName; |
| 1830 | if (icon.isNull()) |
| 1831 | { |
| 1832 | QSKIP_ALL_TESTS(qPrintable(msg)); |
| 1833 | } |
| 1834 | |
| 1835 | QPixmap pix = icon.pixmap(QSize(48,48)); |
| 1836 | msg = QLatin1String("invalid QPixmap from icon ") + iconName; |
| 1837 | if (pix.isNull()) |
| 1838 | { |
| 1839 | QSKIP_ALL_TESTS(qPrintable(msg)); |
| 1840 | } |
| 1841 | |
| 1842 | QImage image = pix.toImage(); |
| 1843 | msg = QLatin1String("invalid QImage from QPixmap/QIcon ") + iconName; |
| 1844 | if (image.isNull()) |
| 1845 | { |
| 1846 | QSKIP_ALL_TESTS(qPrintable(msg)); |
| 1847 | } |
| 1848 | |
| 1849 | const uchar *bits = image.bits(); |
| 1850 | const char *bytes = reinterpret_cast<const char*> (bits); |
| 1851 | QByteArray bytesArray(bytes, image.byteCount()); |
| 1852 | QByteArray md5 = QCryptographicHash::hash(bytesArray, QCryptographicHash::Md5); |
| 1853 | bool ret = !md5IconsTable.contains(md5); |
| 1854 | qWarning("%s icon using QIcon::fromTheme() for mime type %s", |
| 1855 | ret ? "GOOD" : "BAD ", |
| 1856 | mimetype.name().toLocal8Bit().constData()); |
| 1857 | md5IconsTable.insert(md5, mimesToTest.at(counter)); |
| 1858 | } |
| 1859 | } |
| 1860 | |
| 1861 | |
| 1862 | bool TestDirModel::createFileAndCheckIfIconIsExclisive(const QString& termination, |
| 1863 | const unsigned char *content, |
| 1864 | qint64 len) |
| 1865 | { |
| 1866 | QString myFile = createFileInTempDir(QString("tst_folderlistmodel_test.") + termination, |
| 1867 | reinterpret_cast<const char*> (content), |
| 1868 | len); |
| 1869 | bool ret = false; |
| 1870 | bool triedIcon = false; |
| 1871 | if (!myFile.isEmpty()) |
| 1872 | { |
| 1873 | QFileInfo myFileInfo(myFile); |
| 1874 | if (myFileInfo.exists()) |
| 1875 | { |
| 1876 | triedIcon = true; |
| 1877 | QIcon icon = m_provider.icon(myFileInfo); |
| 1878 | QFile::remove(myFile); |
| 1879 | QByteArray md5 = md5FromIcon(icon); |
| 1880 | ret = !m_md5IconsTable.contains(md5); |
| 1881 | qWarning("%s icon from QFileIconProvider::icon() for %s", ret ? "GOOD" : "QFileIconProvider::File or a BAD", qPrintable(myFile)); |
| 1882 | } |
| 1883 | } |
| 1884 | if(!triedIcon) |
| 1885 | { |
| 1886 | qDebug() << "ERROR: could not get icon for" << myFile; |
| 1887 | } |
| 1888 | return ret; |
| 1889 | } |
| 1890 | |
| 1891 | |
| 1892 | //generate some files and test if there is a icon available in |
| 1893 | void TestDirModel::fileIconProvider() |
| 1894 | { |
| 1895 | QIcon commonIcon = m_provider.icon(QFileIconProvider::File); |
| 1896 | if (commonIcon.isNull()) |
| 1897 | { |
| 1898 | QFAIL("No QFileIconProvider::File available"); |
| 1899 | } |
| 1900 | |
| 1901 | QByteArray commonIconMd5 = md5FromIcon(commonIcon); |
| 1902 | m_md5IconsTable.insert(commonIconMd5 , "commonIcon"); |
| 1903 | |
| 1904 | //generate PDF file |
| 1905 | createFileAndCheckIfIconIsExclisive("mp3", sound_44100_mp3_data, sound_44100_mp3_data_len); |
| 1906 | createFileAndCheckIfIconIsExclisive("pdf", testonly_pdf_data, testonly_pdf_len); |
| 1907 | createFileAndCheckIfIconIsExclisive("asx", media_asx, media_asx_len); |
| 1908 | createFileAndCheckIfIconIsExclisive("xspf", media_xspf, media_xspf_len); |
| 1909 | } |
| 1910 | |
| 1911 | #ifndef DO_NOT_USE_TAG_LIB |
| 1912 | //generate mp3 and verify its metadata |
| 1913 | void TestDirModel::verifyMP3Metadata() |
| 1914 | { |
| 1915 | QString mp3File = createFileInTempDir("tst_folderlistmodel_for_media_read.mp3", |
| 1916 | reinterpret_cast<const char*>(sound_44100_mp3_data), |
| 1917 | sound_44100_mp3_data_len); |
| 1918 | QCOMPARE(mp3File.isEmpty(), false); |
| 1919 | |
| 1920 | m_dirModel_01->setReadsMediaMetadata(true); |
| 1921 | QFileInfo fi(mp3File); |
| 1922 | |
| 1923 | QString title = m_dirModel_01->getAudioMetaData(fi, DirModel::TrackTitleRole).toString(); |
| 1924 | QString artist = m_dirModel_01->getAudioMetaData(fi, DirModel::TrackArtistRole).toString(); |
| 1925 | QString album = m_dirModel_01->getAudioMetaData(fi, DirModel::TrackAlbumRole).toString(); |
| 1926 | QString year = m_dirModel_01->getAudioMetaData(fi, DirModel::TrackYearRole).toString(); |
| 1927 | QString track = m_dirModel_01->getAudioMetaData(fi, DirModel::TrackNumberRole).toString(); |
| 1928 | QString genre = m_dirModel_01->getAudioMetaData(fi, DirModel::TrackGenreRole).toString(); |
| 1929 | |
| 1930 | QFile::remove(mp3File); |
| 1931 | |
| 1932 | QCOMPARE(title, QString("TitleTest")); |
| 1933 | QCOMPARE(artist, QString("ArtistTest")); |
| 1934 | QCOMPARE(album, QString("AlbumTest")); |
| 1935 | QCOMPARE(year, QString::number(2013)); |
| 1936 | QCOMPARE(track, QString::number(99)); |
| 1937 | QCOMPARE(genre, QString("GenreTest")); |
| 1938 | } |
| 1939 | #endif |
| 1940 | |
| 1941 | |
| 1942 | #if defined(TEST_OPENFILES) |
| 1943 | void TestDirModel::TestDirModel::openMP3() |
| 1944 | { |
| 1945 | QString mp3File = createFileInTempDir("tst_folderlistmodel_for_open.mp3", |
| 1946 | reinterpret_cast<const char*>(sound_44100_mp3_data), |
| 1947 | sound_44100_mp3_data_len); |
| 1948 | QCOMPARE(mp3File.isEmpty(), false); |
| 1949 | QUrl mp3Url = QUrl::fromLocalFile(mp3File); |
| 1950 | bool ret = QDesktopServices::openUrl(mp3Url); |
| 1951 | if (ret) |
| 1952 | { |
| 1953 | QTest::qWait(TIME_TO_PROCESS *5); |
| 1954 | qWarning("GOOD: QDesktopServices::openUrl() works for MP3 files"); |
| 1955 | } |
| 1956 | else { |
| 1957 | qWarning("BAD: QDesktopServices::openUrl() does NOT work for MP3 files"); |
| 1958 | } |
| 1959 | QCOMPARE(QFile::remove(mp3File), true); |
| 1960 | } |
| 1961 | |
| 1962 | void TestDirModel::openTXT() |
| 1963 | { |
| 1964 | QByteArray text("This is a test only\n"); |
| 1965 | QString txtFile = createFileInTempDir("tst_folderlistmodel_for_open.txt", |
| 1966 | text.constData(), |
| 1967 | text.size()); |
| 1968 | QCOMPARE(txtFile.isEmpty(), false); |
| 1969 | QUrl txtUrl = QUrl::fromLocalFile(txtFile); |
| 1970 | bool ret = QDesktopServices::openUrl(txtUrl); |
| 1971 | if (ret) |
| 1972 | { |
| 1973 | QTest::qWait(TIME_TO_PROCESS *5); |
| 1974 | qWarning("GOOD: QDesktopServices::openUrl() works for TEXT files"); |
| 1975 | } |
| 1976 | else { |
| 1977 | qWarning("BAD: QDesktopServices::openUrl() does NOT work for TEXT files"); |
| 1978 | } |
| 1979 | QCOMPARE(QFile::remove(txtFile), true); |
| 1980 | } |
| 1981 | |
| 1982 | void TestDirModel::openPDF() |
| 1983 | { |
| 1984 | QString pdfFile = createFileInTempDir("tst_folderlistmodel_for_open.pdf", |
| 1985 | reinterpret_cast<const char*>(testonly_pdf_data), |
| 1986 | testonly_pdf_len); |
| 1987 | QCOMPARE(pdfFile.isEmpty(), false); |
| 1988 | QUrl pdfUrl = QUrl::fromLocalFile(pdfFile); |
| 1989 | bool ret = QDesktopServices::openUrl(pdfUrl); |
| 1990 | if (ret) |
| 1991 | { |
| 1992 | QTest::qWait(TIME_TO_PROCESS *5); |
| 1993 | ("GOOD: QDesktopServices::openUrl() works for PDF files"); |
| 1994 | } |
| 1995 | else { |
| 1996 | qWarning("BAD: QDesktopServices::openUrl() does NOT work for PDF files"); |
| 1997 | } |
| 1998 | QCOMPARE(QFile::remove(pdfFile), true); |
| 1999 | } |
| 2000 | #endif |
| 2001 | |
| 2002 | |
| 2003 | /*! |
| 2004 | * \brief TestDirModel::extFsWatcherChangePathManyTimesModifyAllPathsLessLast() expects no Notification |
| 2005 | * |
| 2006 | * As the current path changes in a small interval of the time and the last path set |
| 2007 | * is not modified, the signal pathModified() MUST NOT be fired. |
| 2008 | */ |
| 2009 | void TestDirModel::extFsWatcherChangePathManyTimesModifyAllPathsLessLast() |
| 2010 | { |
| 2011 | ExternalFSWatcher watcher; |
| 2012 | connect(&watcher, SIGNAL(pathModified(QString)), |
| 2013 | this, SLOT(slotExtFsWatcherPathModified(QString))); |
| 2014 | |
| 2015 | const int items = 150; |
| 2016 | QVector<DeepDir *> deepDirs; |
| 2017 | deepDirs.reserve(items); |
| 2018 | |
| 2019 | for (int counter=1; counter <= items ; ++counter) |
| 2020 | { |
| 2021 | QString dirName(QString("extModifyAllLessLast") + QString::number(counter)); |
| 2022 | DeepDir *d = new DeepDir(dirName,0); |
| 2023 | QTest::qWait(1); |
| 2024 | watcher.setCurrentPath(d->path()); |
| 2025 | if (counter < items) // last dir does not receive a file |
| 2026 | { |
| 2027 | TempFiles file; |
| 2028 | file.addSubDirLevel(dirName); |
| 2029 | file.create(); |
| 2030 | QTest::qWait(1); |
| 2031 | } |
| 2032 | deepDirs.append(d); |
| 2033 | } |
| 2034 | QTest::qWait(TIME_TO_PROCESS); |
| 2035 | qDeleteAll(deepDirs); |
| 2036 | |
| 2037 | QCOMPARE(m_extFSWatcherPathModifiedCounter, 0); |
| 2038 | } |
| 2039 | |
| 2040 | |
| 2041 | /*! |
| 2042 | * \brief TestDirModel::extFsWatcherChangePathManyTimesModifyManyTimes |
| 2043 | * |
| 2044 | * Change path many times, force one notification at each path |
| 2045 | */ |
| 2046 | void TestDirModel::extFsWatcherChangePathManyTimesModifyManyTimes() |
| 2047 | { |
| 2048 | ExternalFSWatcher watcher; |
| 2049 | connect(&watcher, SIGNAL(pathModified(QString)), |
| 2050 | this, SLOT(slotExtFsWatcherPathModified(QString))); |
| 2051 | |
| 2052 | const int items = 50; |
| 2053 | QVector<DeepDir *> deepDirs; |
| 2054 | deepDirs.reserve(items); |
| 2055 | |
| 2056 | for (int counter=0; counter < items ; ++counter) |
| 2057 | { |
| 2058 | QString dirName(QString("extFsWatcherChangePathManyTimesModifyManyTimes") |
| 2059 | + QString::number(counter)); |
| 2060 | DeepDir *d = new DeepDir(dirName,0); |
| 2061 | watcher.setCurrentPath(d->path()); |
| 2062 | TempFiles file; |
| 2063 | file.addSubDirLevel(dirName); |
| 2064 | file.create(20); |
| 2065 | QTest::qWait(watcher.getIntervalToNotifyChanges() + 20); |
| 2066 | deepDirs.append(d); |
| 2067 | } |
| 2068 | QTest::qWait(TIME_TO_PROCESS); |
| 2069 | qDeleteAll(deepDirs); |
| 2070 | |
| 2071 | QCOMPARE(m_extFSWatcherPathModifiedCounter, items); |
| 2072 | } |
| 2073 | |
| 2074 | /*! |
| 2075 | * \brief TestDirModel::extFsWatcherModifySamePathManyTimesWithInInterval() expects just one Notification |
| 2076 | * |
| 2077 | * A path is modified many times in a small time interval than ExternalFSWatcher class notifies changes, |
| 2078 | * so only one Notification is expected. |
| 2079 | */ |
| 2080 | void TestDirModel::extFsWatcherModifySamePathManyTimesWithInInterval() |
| 2081 | { |
| 2082 | ExternalFSWatcher watcher; |
| 2083 | connect(&watcher, SIGNAL(pathModified(QString)), |
| 2084 | this, SLOT(slotExtFsWatcherPathModified(QString))); |
| 2085 | |
| 2086 | QString dirName("extFsWatcher_expects_just_one_signal"); |
| 2087 | m_deepDir_01 = new DeepDir(dirName,0); |
| 2088 | watcher.setCurrentPath(m_deepDir_01->path()); |
| 2089 | int loop = 10; |
| 2090 | int waitTime = watcher.getIntervalToNotifyChanges() / loop - 10; |
| 2091 | while (loop--) |
| 2092 | { |
| 2093 | TempFiles file; |
| 2094 | file.addSubDirLevel(dirName); |
| 2095 | file.create(QString("file_") + QString::number(loop), 1); |
| 2096 | QTest::qWait(waitTime); |
| 2097 | } |
| 2098 | |
| 2099 | QTest::qWait(TIME_TO_PROCESS); |
| 2100 | QCOMPARE(m_extFSWatcherPathModifiedCounter, 1 ); |
| 2101 | } |
| 2102 | |
| 2103 | |
| 2104 | /*! |
| 2105 | * \brief TestDirModel::extFsWatcherSetPathAndModifyManyTimesWithInInterval() expects just one Notification |
| 2106 | */ |
| 2107 | void TestDirModel::extFsWatcherSetPathAndModifyManyTimesWithInInterval() |
| 2108 | { |
| 2109 | ExternalFSWatcher watcher; |
| 2110 | connect(&watcher, SIGNAL(pathModified(QString)), |
| 2111 | this, SLOT(slotExtFsWatcherPathModified(QString))); |
| 2112 | |
| 2113 | QList<DeepDir *> deepDirs; |
| 2114 | |
| 2115 | int loop = 5; |
| 2116 | int waitTime = watcher.getIntervalToNotifyChanges() / loop - 5; |
| 2117 | |
| 2118 | for (int counter=1; counter <= loop ; ++counter) |
| 2119 | { |
| 2120 | QString dirName(QString("extModifyAll") + QString::number(counter)); |
| 2121 | DeepDir *d = new DeepDir(dirName,0); |
| 2122 | watcher.setCurrentPath(d->path()); |
| 2123 | TempFiles file; |
| 2124 | file.addSubDirLevel(dirName); |
| 2125 | file.create(); |
| 2126 | QTest::qWait(waitTime); |
| 2127 | deepDirs.append(d); |
| 2128 | } |
| 2129 | QTest::qWait(TIME_TO_PROCESS); |
| 2130 | qDeleteAll(deepDirs); |
| 2131 | |
| 2132 | QCOMPARE(m_extFSWatcherPathModifiedCounter, 1); |
| 2133 | } |
| 2134 | |
| 2135 | #if defined(Q_OS_UNIX) |
| 2136 | /*! |
| 2137 | * \brief TestDirModel::extFsWatcherNoticeChangesWithSameTimestamp() |
| 2138 | * |
| 2139 | * Test if it gets notification from \ref ExternalFSWatcher for a file changed/created that generates the same |
| 2140 | * timestamp as the last modification which had generated previous notification. |
| 2141 | * |
| 2142 | * \note To do this the "last modification time" is forced with utimes(2) system call. |
| 2143 | */ |
| 2144 | void TestDirModel::extFsWatcherNoticeChangesWithSameTimestamp() |
| 2145 | { |
| 2146 | bool updateAndSetModificationTime(const QString& filename, QDateTime& desiredTime); |
| 2147 | |
| 2148 | connect( m_dirModel_01->getExternalFSWatcher(), SIGNAL(pathModified(QString)), |
| 2149 | this, SLOT(slotExtFsWatcherPathModified(QString))); |
| 2150 | |
| 2151 | QString dirName("extFsWatcher_generate_fileswithsameTimeStamp"); |
| 2152 | m_deepDir_01 = new DeepDir(dirName,0); |
| 2153 | //create 2 files |
| 2154 | TempFiles tmp1, tmp2; |
| 2155 | tmp1.addSubDirLevel(dirName); |
| 2156 | tmp2.addSubDirLevel(dirName); |
| 2157 | tmp1.create(QLatin1String("first"), 1); |
| 2158 | tmp2.create(QLatin1String("second"), 1); |
| 2159 | QString firstPathName = tmp1.createdList().at(0); |
| 2160 | QString secondPathName = tmp2.createdList().at(0); |
| 2161 | |
| 2162 | m_dirModel_01->setPath(m_deepDir_01->path()); |
| 2163 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2164 | //make sure model has 2 files |
| 2165 | QCOMPARE(m_dirModel_01->rowCount(), 2) ; |
| 2166 | |
| 2167 | QDateTime timeStamp = QDateTime::currentDateTime(); |
| 2168 | QTime time = timeStamp.time(); |
| 2169 | //set msecs to 0 |
| 2170 | time.setHMS(time.hour(), time.minute(), time.second()); |
| 2171 | timeStamp.setTime(time); |
| 2172 | |
| 2173 | //wait some time to have a different timestamp |
| 2174 | QTest::qWait(2200); |
| 2175 | |
| 2176 | //update first time and set its modification time to timeStamp |
| 2177 | bool ret = updateAndSetModificationTime(firstPathName, timeStamp); |
| 2178 | QCOMPARE(ret, true); |
| 2179 | QFileInfo firstFile(firstPathName); |
| 2180 | QCOMPARE(firstFile.lastModified(), timeStamp); |
| 2181 | |
| 2182 | const int maxWait = m_dirModel_01->getExternalFSWatcher()->getIntervalToNotifyChanges() + 10; |
| 2183 | int counter = 0; |
| 2184 | |
| 2185 | //make sure ExternalFSWatcher has not notified any change |
| 2186 | QCOMPARE(m_extFSWatcherPathModifiedCounter, 0); |
| 2187 | //wait for notification on first file |
| 2188 | for (counter = 0; m_extFSWatcherPathModifiedCounter == 0 && counter < maxWait; ++counter) |
| 2189 | { |
| 2190 | QTest::qWait(1); |
| 2191 | } |
| 2192 | //make sure as notification was sent, it must be one |
| 2193 | QCOMPARE(m_extFSWatcherPathModifiedCounter, 1); |
| 2194 | |
| 2195 | //now update the second file and set the mofication time the same as the first file |
| 2196 | //this second file is one what matters because the first change has just been notified |
| 2197 | ret = updateAndSetModificationTime(secondPathName, timeStamp); |
| 2198 | QCOMPARE(ret, true); |
| 2199 | QFileInfo secondFile(secondPathName); |
| 2200 | QCOMPARE(secondFile.lastModified(), timeStamp); |
| 2201 | for (counter = 0; m_extFSWatcherPathModifiedCounter == 1 && counter < maxWait; ++counter) |
| 2202 | { |
| 2203 | QTest::qWait(1); |
| 2204 | } |
| 2205 | //make sure as notification was sent, it must be 2 |
| 2206 | QCOMPARE(m_extFSWatcherPathModifiedCounter, 2); |
| 2207 | |
| 2208 | //this comparation is not necessary since both last modification files were compared to timeStamp |
| 2209 | QCOMPARE(secondFile.lastModified(), firstFile.lastModified()); |
| 2210 | } |
| 2211 | #endif //Q_OS_UNIX |
| 2212 | |
| 2213 | |
| 2214 | void TestDirModel::modelSingleSelection() |
| 2215 | { |
| 2216 | DirSelection *selection = m_dirModel_01->selectionObject(); |
| 2217 | QVERIFY(selection != 0); |
| 2218 | |
| 2219 | connect(selection, SIGNAL(selectionChanged(int)), |
| 2220 | this, SLOT(slotSelectionChanged(int))); |
| 2221 | |
| 2222 | connect(selection, SIGNAL(modeChanged(int)), |
| 2223 | this, SLOT(slotSelectionModeChanged(int))); |
| 2224 | |
| 2225 | QString dirName("modelSingleSelection"); |
| 2226 | m_deepDir_01 = new DeepDir(dirName,0); |
| 2227 | //create 2 files |
| 2228 | TempFiles tmp1, tmp2, tmp3; |
| 2229 | tmp1.addSubDirLevel(dirName); |
| 2230 | tmp2.addSubDirLevel(dirName); |
| 2231 | tmp3.addSubDirLevel(dirName); |
| 2232 | tmp1.create(QLatin1String("01first"), 1); |
| 2233 | tmp2.create(QLatin1String("02second"), 1); |
| 2234 | tmp3.create(QLatin1String("03third"), 1); |
| 2235 | QString firstFile = tmp1.createdNames().at(0); |
| 2236 | QString secondFile = tmp2.createdList().at(0); |
| 2237 | QString thirdFile = tmp3.createdNames().at(0); |
| 2238 | |
| 2239 | m_dirModel_01->setPath(m_deepDir_01->path()); |
| 2240 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2241 | QCOMPARE(m_dirModel_01->rowCount(), 3) ; |
| 2242 | |
| 2243 | //relative path = name in the file manager |
| 2244 | QCOMPARE(QFileInfo(firstFile).isAbsolute() , false); |
| 2245 | //using full path |
| 2246 | QCOMPARE(QFileInfo(secondFile).isAbsolute(), true); |
| 2247 | |
| 2248 | selection->setMode(DirSelection::Multi); |
| 2249 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2250 | QVERIFY(m_selectionMode != DirSelection::Single); |
| 2251 | selection->setMode(DirSelection::Single); |
| 2252 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2253 | QCOMPARE(m_selectionMode, (int)DirSelection::Single); |
| 2254 | |
| 2255 | QModelIndex firstIdx = m_dirModel_01->index(0, DirModel::IsSelectedRole - DirModel::FileNameRole); |
| 2256 | QModelIndex secondIdx = m_dirModel_01->index(1, DirModel::IsSelectedRole - DirModel::FileNameRole); |
| 2257 | QModelIndex thirdIdx = m_dirModel_01->index(2, DirModel::IsSelectedRole - DirModel::FileNameRole); |
| 2258 | |
| 2259 | //toggle first item selection using index |
| 2260 | selection->toggleIndex(0); |
| 2261 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2262 | QCOMPARE(m_dirModel_01->data(firstIdx).toBool(), true); |
| 2263 | QCOMPARE(m_dirModel_01->data(secondIdx).toBool(), false); |
| 2264 | QCOMPARE(m_dirModel_01->data(thirdIdx).toBool(), false); |
| 2265 | QCOMPARE(m_selectedItemsCounter, 1); |
| 2266 | |
| 2267 | //toggle second item selection |
| 2268 | selection->toggleIndex(1); |
| 2269 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2270 | QCOMPARE(m_dirModel_01->data(firstIdx).toBool(), false); |
| 2271 | QCOMPARE(m_dirModel_01->data(secondIdx).toBool(), true); |
| 2272 | QCOMPARE(m_dirModel_01->data(thirdIdx).toBool(), false); |
| 2273 | QCOMPARE(m_selectedItemsCounter, 1); |
| 2274 | |
| 2275 | //toggle third item selection |
| 2276 | selection->toggleIndex(2); |
| 2277 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2278 | QCOMPARE(m_dirModel_01->data(firstIdx).toBool(), false); |
| 2279 | QCOMPARE(m_dirModel_01->data(secondIdx).toBool(), false); |
| 2280 | QCOMPARE(m_dirModel_01->data(thirdIdx).toBool(), true); |
| 2281 | QCOMPARE(m_selectedItemsCounter, 1); |
| 2282 | |
| 2283 | selection->selectAll(); |
| 2284 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2285 | QCOMPARE(m_selectedItemsCounter, 3); |
| 2286 | |
| 2287 | //all items are selected, if one item is unselected, other 2 items remain selected |
| 2288 | selection->toggleIndex(0); |
| 2289 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2290 | QCOMPARE(m_dirModel_01->data(firstIdx).toBool(), false); |
| 2291 | QCOMPARE(m_dirModel_01->data(secondIdx).toBool(), true); |
| 2292 | QCOMPARE(m_dirModel_01->data(thirdIdx).toBool(), true); |
| 2293 | QCOMPARE(m_selectedItemsCounter, 2); |
| 2294 | |
| 2295 | //compare results |
| 2296 | QList<int> selectedIndexes = selection->selectedIndexes(); |
| 2297 | QCOMPARE(selectedIndexes.count(), 2); |
| 2298 | QCOMPARE(selectedIndexes.at(0), 1); |
| 2299 | QCOMPARE(selectedIndexes.at(1), 2); |
| 2300 | QStringList selectedNames = selection->selectedNames(); |
| 2301 | QCOMPARE(selectedNames.count(), 2); |
| 2302 | // as secondFile is absolute path |
| 2303 | QCOMPARE(selectedNames.at(0), QFileInfo(secondFile).fileName()); |
| 2304 | QCOMPARE(selectedNames.at(1), thirdFile); |
| 2305 | QStringList pathNames = selection->selectedAbsFilePaths(); |
| 2306 | QCOMPARE(pathNames.count(), 2); |
| 2307 | QCOMPARE(pathNames.at(0), tmp2.createdList().first()); |
| 2308 | QCOMPARE(pathNames.at(1), tmp3.createdList().first()); |
| 2309 | |
| 2310 | //now as the first item is unselected, toggle |
| 2311 | selection->toggleIndex(0); |
| 2312 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2313 | QCOMPARE(m_dirModel_01->data(firstIdx).toBool(), true); |
| 2314 | QCOMPARE(m_dirModel_01->data(secondIdx).toBool(), false); |
| 2315 | QCOMPARE(m_dirModel_01->data(thirdIdx).toBool(), false); |
| 2316 | QCOMPARE(m_selectedItemsCounter, 1); |
| 2317 | } |
| 2318 | |
| 2319 | |
| 2320 | void TestDirModel::modelMultiSelection() |
| 2321 | { |
| 2322 | DirSelection *selection = m_dirModel_01->selectionObject(); |
| 2323 | QVERIFY(selection != 0); |
| 2324 | |
| 2325 | connect(selection, SIGNAL(selectionChanged(int)), |
| 2326 | this, SLOT(slotSelectionChanged(int))); |
| 2327 | |
| 2328 | connect(selection, SIGNAL(modeChanged(int)), |
| 2329 | this, SLOT(slotSelectionModeChanged(int))); |
| 2330 | |
| 2331 | QString dirName("modelMultiSelection"); |
| 2332 | const int createdFiles = 10; |
| 2333 | |
| 2334 | m_deepDir_01 = new DeepDir(dirName,0); |
| 2335 | TempFiles tmpFiles; |
| 2336 | tmpFiles.addSubDirLevel(dirName); |
| 2337 | tmpFiles.create(createdFiles); |
| 2338 | |
| 2339 | m_dirModel_01->setPath(m_deepDir_01->path()); |
| 2340 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2341 | QCOMPARE(m_dirModel_01->rowCount(), createdFiles) ; |
| 2342 | |
| 2343 | selection->setMode(DirSelection::Multi); |
| 2344 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2345 | QCOMPARE(selection->mode(), DirSelection::Multi); |
| 2346 | |
| 2347 | QStringList createdNames = tmpFiles.createdNames(); |
| 2348 | QList<int> handledIndexes; |
| 2349 | handledIndexes << 0 << 3 << 5 << 8 << 4; |
| 2350 | int counter = handledIndexes.count(); |
| 2351 | |
| 2352 | QCOMPARE(m_selectedItemsCounter, 0); |
| 2353 | while (counter--) |
| 2354 | { |
| 2355 | int item = handledIndexes.at(counter); |
| 2356 | selection->setIndex(item, true); |
| 2357 | QTest::qWait(10); |
| 2358 | } |
| 2359 | QCOMPARE(m_selectedItemsCounter, handledIndexes.count()); |
| 2360 | |
| 2361 | counter = handledIndexes.count(); |
| 2362 | while (counter--) |
| 2363 | { |
| 2364 | int item = handledIndexes.at(counter); |
| 2365 | QModelIndex index = m_dirModel_01->index(item, |
| 2366 | DirModel::IsSelectedRole - DirModel::FileNameRole); |
| 2367 | QCOMPARE(m_dirModel_01->data(index).toBool(), true); |
| 2368 | } |
| 2369 | |
| 2370 | //remove selected Items |
| 2371 | m_dirModel_01->removeIndex(handledIndexes.takeAt(2)); |
| 2372 | QTest::qWait(TIME_TO_PROCESS); |
| 2373 | m_dirModel_01->removeIndex(handledIndexes.takeLast()); |
| 2374 | QTest::qWait(TIME_TO_PROCESS); |
| 2375 | QCOMPARE(m_selectedItemsCounter, handledIndexes.count()); |
| 2376 | |
| 2377 | selection->selectAll(); |
| 2378 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2379 | QCOMPARE(m_selectedItemsCounter, m_dirModel_01->rowCount()); |
| 2380 | |
| 2381 | selection->clear(); |
| 2382 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2383 | QCOMPARE(m_selectedItemsCounter, 0); |
| 2384 | } |
| 2385 | |
| 2386 | |
| 2387 | void TestDirModel::modelSelectionItemsRange() |
| 2388 | { |
| 2389 | QString dirName("modelSelectionItemsRange"); |
| 2390 | m_deepDir_01 = new DeepDir(dirName,0); |
| 2391 | TempFiles tmpFiles; |
| 2392 | const int createdFiles = 11; |
| 2393 | tmpFiles.addSubDirLevel(dirName); |
| 2394 | tmpFiles.create(createdFiles); |
| 2395 | |
| 2396 | m_dirModel_01->setPath(m_deepDir_01->path()); |
| 2397 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2398 | QCOMPARE(m_dirModel_01->rowCount(), createdFiles); |
| 2399 | |
| 2400 | DirSelection *selection = m_dirModel_01->selectionObject(); |
| 2401 | QVERIFY(selection != 0); |
| 2402 | |
| 2403 | connect(selection, SIGNAL(selectionChanged(int)), |
| 2404 | this, SLOT(slotSelectionChanged(int))); |
| 2405 | |
| 2406 | |
| 2407 | selection->setIndex(3, true); |
| 2408 | QTest::qWait(TIME_TO_PROCESS); |
| 2409 | QCOMPARE(m_selectedItemsCounter, 1); |
| 2410 | |
| 2411 | //try to do range with same index (expected nothing to happen) |
| 2412 | selection->selectRange(3); |
| 2413 | QTest::qWait(TIME_TO_PROCESS); |
| 2414 | QCOMPARE(m_selectedItemsCounter, 1); |
| 2415 | |
| 2416 | //try item under selected item |
| 2417 | selection->selectRange(8); |
| 2418 | QTest::qWait(TIME_TO_PROCESS); |
| 2419 | QCOMPARE(m_selectedItemsCounter, 6); |
| 2420 | |
| 2421 | //try item above selected item |
| 2422 | selection->selectRange(0); |
| 2423 | QTest::qWait(TIME_TO_PROCESS); |
| 2424 | QCOMPARE(m_selectedItemsCounter, 9); |
| 2425 | } |
| 2426 | |
| 2427 | void TestDirModel::trashDiretories() |
| 2428 | { |
| 2429 | QTrashDir trash; |
| 2430 | QString dirName("trashDiretories"); |
| 2431 | m_deepDir_01 = new DeepDir(dirName,0); |
| 2432 | |
| 2433 | // there is not .Trash dir |
| 2434 | QCOMPARE(trash.isMountPointSharedWithStickBit(dirName), false); |
| 2435 | |
| 2436 | //create .Trash in it |
| 2437 | QString trashDir(m_deepDir_01->path() + QDir::separator() + ".Trash"); |
| 2438 | QCOMPARE(QDir().mkpath(trashDir), true); |
| 2439 | |
| 2440 | |
| 2441 | //still faling |
| 2442 | QCOMPARE(trash.isMountPointSharedWithStickBit(dirName), false); |
| 2443 | |
| 2444 | //turn stick bit on |
| 2445 | QString cmd("chmod +t " + trashDir); |
| 2446 | QCOMPARE(system(cmd.toLocal8Bit().constData()), 0); |
| 2447 | QCOMPARE(trash.isMountPointSharedWithStickBit((m_deepDir_01->path())), true); |
| 2448 | |
| 2449 | //test validate Trash Dir() |
| 2450 | QCOMPARE(trash.validate(trashDir, false), false); |
| 2451 | |
| 2452 | //test permissions |
| 2453 | QFile f(trashDir); |
| 2454 | bool permOk = f.setPermissions(QFile::ReadOwner | |
| 2455 | QFile::WriteOwner | |
| 2456 | QFile::ExeOwner); |
| 2457 | QCOMPARE(permOk, true); |
| 2458 | QCOMPARE(trash.checkUserDirPermissions(trashDir), true); |
| 2459 | |
| 2460 | //create files in Trash |
| 2461 | QCOMPARE(trash.createUserDir(QTrashUtilInfo::filesTrashDir(trashDir)), true); |
| 2462 | //create info in Trash |
| 2463 | QCOMPARE(trash.createUserDir(QTrashUtilInfo::infoTrashDir(trashDir)), true); |
| 2464 | //test validate Trash Dir(), now it MUST pass |
| 2465 | QCOMPARE(trash.validate(trashDir, false), true); |
| 2466 | |
| 2467 | //invalidate Trash dir |
| 2468 | QFile *f1 = new QFile(trashDir) ; |
| 2469 | permOk = f1->setPermissions(QFile::ReadOwner | QFile::ReadOther | |
| 2470 | QFile::WriteOwner | QFile::WriteGroup| |
| 2471 | QFile::ExeOwner); |
| 2472 | QCOMPARE(permOk, true); |
| 2473 | f1->flush(); |
| 2474 | delete f1; |
| 2475 | QCOMPARE(trash.validate(trashDir, false), false); |
| 2476 | |
| 2477 | QString xdgTrash("xdg_Trash"); |
| 2478 | m_deepDir_02 = new DeepDir(xdgTrash,0); |
| 2479 | |
| 2480 | if (::qgetenv("XDG_DATA_HOME").size() == 0) |
| 2481 | { |
| 2482 | QString myTrash(QDir::homePath() + "/.local/share/Trash"); |
| 2483 | QCOMPARE(trash.homeTrash(), myTrash); |
| 2484 | } |
| 2485 | |
| 2486 | //test XDG Home Trash |
| 2487 | ::setenv("XDG_DATA_HOME", m_deepDir_02->path().toLocal8Bit().constData(), true ); |
| 2488 | QString xdgTrashDir(trash.homeTrash()); |
| 2489 | QCOMPARE(trash.validate(xdgTrashDir, false), true); |
| 2490 | QCOMPARE(trash.homeTrash() , xdgTrashDir); |
| 2491 | |
| 2492 | QCOMPARE(trash.getMountPoint(QDir::rootPath()), QDir::rootPath()); |
| 2493 | |
| 2494 | QStringList mountedPoints = trash.mountedPoints(); |
| 2495 | foreach (const QString& mp, mountedPoints) |
| 2496 | { |
| 2497 | //attemp to create a temporary folder inside the mount point |
| 2498 | QTemporaryDir dir(mp + QDir::separator() ); |
| 2499 | if (dir.isValid()) |
| 2500 | { |
| 2501 | QFileInfo fiDir(dir.path()); |
| 2502 | if (fiDir.exists()) |
| 2503 | { |
| 2504 | qDebug() << "temp dir" << dir.path(); |
| 2505 | QCOMPARE(trash.getMountPoint(fiDir.canonicalFilePath()), mp); |
| 2506 | //creates a temporary file inside temporary dir |
| 2507 | QTemporaryFile file (fiDir.canonicalFilePath() + QDir::separator()); |
| 2508 | QCOMPARE(file.open(), true); |
| 2509 | file.close(); |
| 2510 | QCOMPARE(trash.getMountPoint(file.fileName()), mp); |
| 2511 | } |
| 2512 | } |
| 2513 | } |
| 2514 | |
| 2515 | //file under home |
| 2516 | QTemporaryFile homeFile (QDir::homePath() + QDir::separator()); |
| 2517 | QCOMPARE(homeFile.open(), true); |
| 2518 | homeFile.close(); |
| 2519 | QString homeTrashDir = trash.suitableTrash(homeFile.fileName()); |
| 2520 | QCOMPARE(homeTrashDir, trash.homeTrash()); |
| 2521 | |
| 2522 | //home |
| 2523 | homeTrashDir = trash.suitableTrash(QDir::homePath()); |
| 2524 | QVERIFY(homeTrashDir != trash.homeTrash()); |
| 2525 | |
| 2526 | //file under home Trash |
| 2527 | QTemporaryFile cannotMoveTotrash(trash.homeTrash() + QDir::separator()); |
| 2528 | QCOMPARE(cannotMoveTotrash.open(), true); |
| 2529 | cannotMoveTotrash.close(); |
| 2530 | QCOMPARE(trash.suitableTrash(cannotMoveTotrash.fileName()).isEmpty(), true); |
| 2531 | |
| 2532 | QStringList trashes = trash.allTrashes(); |
| 2533 | qWarning() << "return from allTrashes():" << trashes; |
| 2534 | |
| 2535 | QVERIFY(trashes.count() > 0); |
| 2536 | |
| 2537 | qDebug() << "QStandardPaths::CacheLocation" << QStandardPaths::standardLocations(QStandardPaths::CacheLocation); |
| 2538 | qDebug() << "QStandardPaths::DataLocation" << QStandardPaths::standardLocations(QStandardPaths::DataLocation); |
| 2539 | qDebug() << "QStandardPaths::GenericCacheLocation" << QStandardPaths::standardLocations(QStandardPaths::GenericCacheLocation); |
| 2540 | qDebug() << "QStandardPaths::GenericDataLocation" << QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation); |
| 2541 | } |
| 2542 | |
| 2543 | |
| 2544 | void TestDirModel::locationFactory() |
| 2545 | { |
| 2546 | LocationsFactory factoryLocations(this); |
| 2547 | const Location *location = 0; |
| 2548 | |
| 2549 | QString validTrashURL(LocationUrl::TrashRootURL); |
| 2550 | |
| 2551 | #if 0 // "sheme:/" (single slash) is no longer supported |
| 2552 | location = factoryLocations.setNewPath("trash:/"); |
| 2553 | QVERIFY(location); |
| 2554 | QVERIFY(location->isTrashDisk()); |
| 2555 | QCOMPARE(location->info()->absoluteFilePath(), validTrashURL); |
| 2556 | QCOMPARE(location->urlPath(), validTrashURL); |
| 2557 | QCOMPARE(location->isRoot(), true); |
| 2558 | #endif |
| 2559 | |
| 2560 | location = factoryLocations.setNewPath("trash://"); |
| 2561 | QVERIFY(location); |
| 2562 | QVERIFY(location->isTrashDisk()); |
| 2563 | QCOMPARE(location->info()->absoluteFilePath(), validTrashURL); |
| 2564 | QCOMPARE(location->urlPath(), validTrashURL); |
| 2565 | QCOMPARE(location->isRoot(), true); |
| 2566 | |
| 2567 | location = factoryLocations.setNewPath("trash:///"); |
| 2568 | QVERIFY(location); |
| 2569 | QVERIFY(location->isTrashDisk()); |
| 2570 | QCOMPARE(location->info()->absoluteFilePath(), validTrashURL); |
| 2571 | QCOMPARE(location->urlPath(), validTrashURL); |
| 2572 | QCOMPARE(location->isRoot(), true); |
| 2573 | |
| 2574 | location = factoryLocations.setNewPath("trash://////"); |
| 2575 | QVERIFY(location); |
| 2576 | QVERIFY(location->isTrashDisk()); |
| 2577 | QCOMPARE(location->info()->absoluteFilePath(), validTrashURL); |
| 2578 | QCOMPARE(location->urlPath(), validTrashURL); |
| 2579 | QCOMPARE(location->isRoot(), true); |
| 2580 | |
| 2581 | #if 0 // "sheme:/" (single slash) is no longer supported |
| 2582 | QString myDir("___myDir_must_NOT_EXIST___"); |
| 2583 | location = factoryLocations.setNewPath(QString("trash:/") + myDir); |
| 2584 | QVERIFY(location == 0); |
| 2585 | #endif |
| 2586 | |
| 2587 | location = factoryLocations.setNewPath("file://////"); |
| 2588 | QVERIFY(location); |
| 2589 | QVERIFY(location->isLocalDisk()); |
| 2590 | QCOMPARE(location->info()->absoluteFilePath(), QDir::rootPath()); |
| 2591 | QCOMPARE(location->urlPath(), QDir::rootPath()); |
| 2592 | QCOMPARE(location->isRoot(), true); |
| 2593 | |
| 2594 | location = factoryLocations.setNewPath("/"); |
| 2595 | QVERIFY(location); |
| 2596 | QVERIFY(location->isLocalDisk()); |
| 2597 | QCOMPARE(location->info()->absoluteFilePath(), QDir::rootPath()); |
| 2598 | QCOMPARE(location->urlPath(), QDir::rootPath()); |
| 2599 | QCOMPARE(location->isRoot(), true); |
| 2600 | |
| 2601 | location = factoryLocations.setNewPath("//"); |
| 2602 | QVERIFY(location); |
| 2603 | QVERIFY(location->isLocalDisk()); |
| 2604 | QCOMPARE(location->info()->absoluteFilePath(), QDir::rootPath()); |
| 2605 | QCOMPARE(location->urlPath(), QDir::rootPath()); |
| 2606 | QCOMPARE(location->isRoot(), true); |
| 2607 | |
| 2608 | location = factoryLocations.setNewPath("//bin"); |
| 2609 | QVERIFY(location); |
| 2610 | QVERIFY(location->isLocalDisk()); |
| 2611 | QCOMPARE(location->info()->absoluteFilePath(), QLatin1String("/bin")); |
| 2612 | QCOMPARE(location->urlPath(), QLatin1String("/bin")); |
| 2613 | QCOMPARE(location->isRoot(), false); |
| 2614 | |
| 2615 | QTrashDir trash; |
| 2616 | QString dirName("trashDirectory"); |
| 2617 | m_deepDir_01 = new DeepDir(dirName,0); |
| 2618 | |
| 2619 | //create a Trash to have |
| 2620 | |
| 2621 | ::setenv("XDG_DATA_HOME", m_deepDir_01->path().toLocal8Bit().constData(), true ); |
| 2622 | QString xdgTrashDir(trash.homeTrash()); |
| 2623 | QCOMPARE(trash.validate(xdgTrashDir, true), true); |
| 2624 | QCOMPARE(trash.homeTrash() , xdgTrashDir); |
| 2625 | |
| 2626 | QString trash3("trash:///Dir1/Dir2/Dir3"); |
| 2627 | QString trash2("trash:///Dir1/Dir2"); |
| 2628 | QString trash1("trash:///Dir1"); |
| 2629 | |
| 2630 | QCOMPARE(QDir().mkpath(QTrashUtilInfo::filesTrashDir(xdgTrashDir) + "/Dir1/Dir2/Dir3"), true); |
| 2631 | |
| 2632 | |
| 2633 | //create a empty .trashinfo file to validate the Trash |
| 2634 | QFile trashinfo (QTrashUtilInfo::infoTrashDir(xdgTrashDir) + "/Dir1.trashinfo"); |
| 2635 | QCOMPARE(trashinfo.open(QFile::WriteOnly), true); |
| 2636 | trashinfo.close(); |
| 2637 | |
| 2638 | location = factoryLocations.setNewPath(trash3); |
| 2639 | Location * myLocation = const_cast<Location*> (location); |
| 2640 | QCOMPARE(myLocation->becomeParent(), true); |
| 2641 | QCOMPARE(location->urlPath(), trash2); |
| 2642 | QCOMPARE(location->isRoot(), false); |
| 2643 | |
| 2644 | location = factoryLocations.setNewPath(trash2); |
| 2645 | myLocation = const_cast<Location*> (location); |
| 2646 | QCOMPARE(myLocation->becomeParent(), true); |
| 2647 | QCOMPARE(location->urlPath(), trash1); |
| 2648 | QCOMPARE(location->isRoot(), false); |
| 2649 | |
| 2650 | myLocation = const_cast<Location*> (location); |
| 2651 | QCOMPARE(myLocation->becomeParent(), true); |
| 2652 | QCOMPARE(location->urlPath(), LocationUrl::TrashRootURL); |
| 2653 | QCOMPARE(location->isRoot(), true); |
| 2654 | } |
| 2655 | |
| 2656 | |
| 2657 | |
| 2658 | void TestDirModel::moveOneFileToTrashAndRestore() |
| 2659 | { |
| 2660 | QString orig("moveFilesTrash"); |
| 2661 | m_deepDir_01 = new DeepDir(orig, 0); |
| 2662 | const int createdFiles = 4; |
| 2663 | TempFiles tempfiles; |
| 2664 | tempfiles.addSubDirLevel(orig); |
| 2665 | tempfiles.create(createdFiles); |
| 2666 | |
| 2667 | m_dirModel_01->setPath(m_deepDir_01->path()); |
| 2668 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2669 | QCOMPARE(m_dirModel_01->rowCount(), createdFiles); |
| 2670 | |
| 2671 | QString tempTrash("tempTrashDir"); |
| 2672 | m_deepDir_02 = new DeepDir(tempTrash, 0); |
| 2673 | createTempHomeTrashDir(m_deepDir_02->path()); |
| 2674 | |
| 2675 | // move item to Trash |
| 2676 | m_dirModel_01->moveIndexToTrash(0); |
| 2677 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2678 | QCOMPARE(m_dirModel_01->rowCount(), createdFiles -1); |
| 2679 | |
| 2680 | //use another DirModel instance and point it to Trash |
| 2681 | m_dirModel_02->goTrash(); |
| 2682 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2683 | QCOMPARE(m_dirModel_02->rowCount(), 1); |
| 2684 | |
| 2685 | // now restore from Trash |
| 2686 | m_dirModel_02->restoreIndexFromTrash(0); |
| 2687 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2688 | QCOMPARE(m_dirModel_02->rowCount(), 0); |
| 2689 | |
| 2690 | //using refresh to not depend from External File System Watcher |
| 2691 | if (m_dirModel_01->rowCount() != createdFiles) |
| 2692 | { |
| 2693 | qWarning("using refresh() external File System Watcher did not get it right"); |
| 2694 | m_dirModel_01->refresh(); |
| 2695 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2696 | } |
| 2697 | QCOMPARE(m_dirModel_01->rowCount(), createdFiles); |
| 2698 | } |
| 2699 | |
| 2700 | |
| 2701 | void TestDirModel::restoreTrashWithMultipleSources() |
| 2702 | { |
| 2703 | DeepDir d_01("folder_01", 0); |
| 2704 | DeepDir d_02("folder_02", 0); |
| 2705 | DeepDir d_03("folder_03", 0); |
| 2706 | |
| 2707 | //create a temp trash |
| 2708 | QString tempTrash("tempTrashDir"); |
| 2709 | m_deepDir_02 = new DeepDir(tempTrash, 0); |
| 2710 | createTempHomeTrashDir(m_deepDir_02->path()); |
| 2711 | |
| 2712 | const int dirsCounter = 3; |
| 2713 | DeepDir * dirs [dirsCounter] = {&d_01, &d_02, &d_03}; |
| 2714 | int counter = 0; |
| 2715 | |
| 2716 | //move items from different sources to trash |
| 2717 | for(counter = 0; counter < dirsCounter; counter++) |
| 2718 | { |
| 2719 | TempFiles files; |
| 2720 | files.addSubDirLevel(dirs[counter]->path()); |
| 2721 | QString name = QString("file_from_dir%1.txt").arg(counter); |
| 2722 | files.create(name, 1); |
| 2723 | DirModel model; |
| 2724 | model.setPath(dirs[counter]->path()); |
| 2725 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2726 | QCOMPARE(model.rowCount(), 1); |
| 2727 | model.selectionObject()->selectAll(); |
| 2728 | model.moveSelectionToTrash(); |
| 2729 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2730 | QCOMPARE(model.rowCount(), 0); |
| 2731 | } |
| 2732 | |
| 2733 | //another model points to temporary Trash |
| 2734 | connect(m_dirModel_02, SIGNAL(error(QString,QString)), |
| 2735 | this, SLOT(slotError(QString,QString))); |
| 2736 | m_dirModel_02->goTrash(); |
| 2737 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2738 | QCOMPARE(m_dirModel_02->rowCount(), dirsCounter); |
| 2739 | m_dirModel_02->restoreTrash(); |
| 2740 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2741 | QCOMPARE(m_dirModel_02->rowCount(), 0); |
| 2742 | |
| 2743 | //now look into info directories from trash, check if it is empty |
| 2744 | m_dirModel_02->setPath(m_deepDir_02->path()); |
| 2745 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2746 | QCOMPARE(m_dirModel_02->rowCount(), 1); |
| 2747 | QCOMPARE(m_dirModel_02->openPath("Trash"), true); |
| 2748 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2749 | QCOMPARE(m_dirModel_02->openPath("info"), true); |
| 2750 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2751 | QCOMPARE(m_dirModel_02->rowCount(), 0); |
| 2752 | |
| 2753 | //test it items were moved back to their sources |
| 2754 | for(counter = 0; counter < dirsCounter; counter++) |
| 2755 | { |
| 2756 | DirModel model; |
| 2757 | model.setPath(dirs[counter]->path()); |
| 2758 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2759 | QCOMPARE(model.rowCount(), 1); |
| 2760 | } |
| 2761 | |
| 2762 | QCOMPARE(m_receivedErrorSignal, false); |
| 2763 | } |
| 2764 | |
| 2765 | |
| 2766 | void TestDirModel::emptyTrash() |
| 2767 | { |
| 2768 | DeepDir d_01("folder_01", 0); |
| 2769 | DeepDir d_02("folder_02", 0); |
| 2770 | DeepDir d_03("folder_03", 0); |
| 2771 | |
| 2772 | //create a temp trash |
| 2773 | QString tempTrash("tempTrashDir"); |
| 2774 | m_deepDir_02 = new DeepDir(tempTrash, 0); |
| 2775 | createTempHomeTrashDir(m_deepDir_02->path()); |
| 2776 | |
| 2777 | const int dirsCounter = 3; |
| 2778 | DeepDir * dirs [dirsCounter] = {&d_01, &d_02, &d_03}; |
| 2779 | int counter = 0; |
| 2780 | |
| 2781 | //move items from different sources to trash |
| 2782 | for(counter = 0; counter < dirsCounter; counter++) |
| 2783 | { |
| 2784 | TempFiles files; |
| 2785 | files.addSubDirLevel(dirs[counter]->path()); |
| 2786 | QString name = QString("file_from_dir%1.txt").arg(counter); |
| 2787 | files.create(name, 1); |
| 2788 | DirModel model; |
| 2789 | model.setPath(dirs[counter]->path()); |
| 2790 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2791 | QCOMPARE(model.rowCount(), 1); |
| 2792 | model.selectionObject()->selectAll(); |
| 2793 | model.moveSelectionToTrash(); |
| 2794 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2795 | QCOMPARE(model.rowCount(), 0); |
| 2796 | } |
| 2797 | |
| 2798 | //another model points to temporary Trash |
| 2799 | connect(m_dirModel_02, SIGNAL(error(QString,QString)), |
| 2800 | this, SLOT(slotError(QString,QString))); |
| 2801 | m_dirModel_02->goTrash(); |
| 2802 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2803 | QCOMPARE(m_dirModel_02->rowCount(), dirsCounter); |
| 2804 | m_dirModel_02->emptyTrash(); |
| 2805 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2806 | QCOMPARE(m_dirModel_02->rowCount(), 0); |
| 2807 | |
| 2808 | //now look into info directories from trash, check if it is empty |
| 2809 | m_dirModel_02->setPath(m_deepDir_02->path()); |
| 2810 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2811 | QCOMPARE(m_dirModel_02->rowCount(), 1); |
| 2812 | QCOMPARE(m_dirModel_02->openPath("Trash"), true); |
| 2813 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2814 | QCOMPARE(m_dirModel_02->openPath("info"), true); |
| 2815 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2816 | QCOMPARE(m_dirModel_02->rowCount(), 0); |
| 2817 | |
| 2818 | //test if original items folder is still empty |
| 2819 | for(counter = 0; counter < dirsCounter; counter++) |
| 2820 | { |
| 2821 | DirModel model; |
| 2822 | model.setPath(dirs[counter]->path()); |
| 2823 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2824 | QCOMPARE(model.rowCount(), 0); |
| 2825 | } |
| 2826 | |
| 2827 | QCOMPARE(m_receivedErrorSignal, false); |
| 2828 | } |
| 2829 | |
| 2830 | |
| 2831 | |
| 2832 | void TestDirModel::diskCdIntoPathAbsolute() |
| 2833 | { |
| 2834 | QString dirName("diskCdIntoPathAbsolute"); |
| 2835 | m_deepDir_01 = new DeepDir(dirName,0); |
| 2836 | m_dirModel_01->setPath(m_deepDir_01->path()); |
| 2837 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2838 | bool ret = m_dirModel_01->cdIntoPath("/IT_MUST_NOT_EXIST/____IT_MUST_NOT_EXIST__"); |
| 2839 | QCOMPARE(ret, false); |
| 2840 | ret = m_dirModel_01->cdIntoPath(QDir::homePath()); |
| 2841 | QCOMPARE(ret , true); |
| 2842 | QCOMPARE(m_dirModel_01->path(), QDir::homePath()); |
| 2843 | } |
| 2844 | |
| 2845 | void TestDirModel::diskCdIntoPathRelative() |
| 2846 | { |
| 2847 | QString dirName("diskCdIntoPathRelative"); |
| 2848 | m_deepDir_01 = new DeepDir(dirName,1); |
| 2849 | m_dirModel_01->setPath(m_deepDir_01->path()); |
| 2850 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2851 | |
| 2852 | QModelIndex firstIdx = m_dirModel_01->index(0, 0); |
| 2853 | QString subdir = m_dirModel_01->data(firstIdx).toString(); |
| 2854 | QString fullSubdir = m_deepDir_01->path() + QDir::separator() + subdir; |
| 2855 | bool ret = m_dirModel_01->cdIntoPath(subdir); |
| 2856 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2857 | QCOMPARE(ret, true); |
| 2858 | QCOMPARE(m_dirModel_01->path(), fullSubdir); |
| 2859 | ret = m_dirModel_01->cdIntoPath(".."); |
| 2860 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2861 | QCOMPARE(ret, true); |
| 2862 | QCOMPARE(m_dirModel_01->path(), m_deepDir_01->path()); |
| 2863 | } |
| 2864 | |
| 2865 | |
| 2866 | |
| 2867 | void TestDirModel::trashCdIntoPathAbsolute() |
| 2868 | { |
| 2869 | QString orig("trashCdIntoPathAbsolute"); |
| 2870 | m_deepDir_01 = new DeepDir(orig, 1); |
| 2871 | m_dirModel_01->setPath(m_deepDir_01->path()); |
| 2872 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2873 | QCOMPARE(m_dirModel_01->rowCount() , 1); |
| 2874 | |
| 2875 | QString tempTrash("tempTrashDir"); |
| 2876 | m_deepDir_02 = new DeepDir(tempTrash, 0); |
| 2877 | createTempHomeTrashDir(m_deepDir_02->path()); |
| 2878 | |
| 2879 | QModelIndex firstIdx = m_dirModel_01->index(0, 0); |
| 2880 | |
| 2881 | QString subdirInTrash = LocationUrl::TrashRootURL + |
| 2882 | m_dirModel_01->data(firstIdx).toString(); |
| 2883 | |
| 2884 | // move item to Trash |
| 2885 | m_dirModel_01->moveIndexToTrash(0); |
| 2886 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2887 | |
| 2888 | |
| 2889 | bool ret = m_dirModel_01->cdIntoPath(subdirInTrash); |
| 2890 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2891 | QCOMPARE(ret, true); |
| 2892 | QCOMPARE(m_dirModel_01->path(), subdirInTrash); |
| 2893 | } |
| 2894 | |
| 2895 | |
| 2896 | void TestDirModel::trashCdIntoPathRelative() |
| 2897 | { |
| 2898 | QString orig("trashCdIntoPathRelative"); |
| 2899 | m_deepDir_01 = new DeepDir(orig, 1); |
| 2900 | m_dirModel_01->setPath(m_deepDir_01->path()); |
| 2901 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2902 | QCOMPARE(m_dirModel_01->rowCount() , 1); |
| 2903 | |
| 2904 | QString tempTrash("tempTrashDir"); |
| 2905 | m_deepDir_02 = new DeepDir(tempTrash, 0); |
| 2906 | createTempHomeTrashDir(m_deepDir_02->path()); |
| 2907 | |
| 2908 | QModelIndex firstIdx = m_dirModel_01->index(0, 0); |
| 2909 | QString subdir = m_dirModel_01->data(firstIdx).toString(); |
| 2910 | QString subdirInTrash = LocationUrl::TrashRootURL + subdir; |
| 2911 | |
| 2912 | |
| 2913 | // move item to Trash |
| 2914 | m_dirModel_01->moveIndexToTrash(0); |
| 2915 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2916 | |
| 2917 | bool ret = m_dirModel_01->cdIntoPath(LocationUrl::TrashRootURL); |
| 2918 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2919 | QCOMPARE(ret, true); |
| 2920 | QCOMPARE(m_dirModel_01->path(), LocationUrl::TrashRootURL); |
| 2921 | |
| 2922 | //cd relative |
| 2923 | ret = m_dirModel_01->cdIntoPath(subdir); |
| 2924 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 2925 | QCOMPARE(ret, true); |
| 2926 | QCOMPARE(m_dirModel_01->path(), subdirInTrash); |
| 2927 | } |
| 2928 | |
| 2929 | |
| 2930 | |
| 2931 | void TestDirModel::smbCdIntoPathAbsolute() |
| 2932 | { |
| 2933 | SmbItemInfo smbOK(LocationUrl::SmbURL); |
| 2934 | QCOMPARE(smbOK.isValid(), true); |
| 2935 | QCOMPARE(smbOK.isRoot(), true); |
| 2936 | QCOMPARE(smbOK.needsAuthentication(), false); |
| 2937 | QCOMPARE(smbOK.urlPath(), LocationUrl::SmbURL); |
| 2938 | |
| 2939 | if (SmbUserShare::canCreateShares()) |
| 2940 | { |
| 2941 | QString shareName("smbShareCdIntoPathAbsolute"); |
| 2942 | |
| 2943 | TestQSambaSuite smbTest(this); |
| 2944 | |
| 2945 | ShareCreationStatus tmpShare(smbTest.createTempShare(shareName)); |
| 2946 | if (tmpShare.tempDir) |
| 2947 | { |
| 2948 | tmpShare.tempDir->setAutoRemove(true); |
| 2949 | } |
| 2950 | QString createdDir("createdDir"); |
| 2951 | QString createDirFullPath = tmpShare.sharedDirPath + QDir::separator() + createdDir; |
| 2952 | bool ret = QDir().mkdir(createDirFullPath); |
| 2953 | QCOMPARE(ret , true); |
| 2954 | QString absUrl = LocationUrl::SmbURL + "localhost" + |
| 2955 | QDir::separator() + tmpShare.shareName + |
| 2956 | QDir::separator() + createdDir; |
| 2957 | |
| 2958 | ret = m_dirModel_01->cdIntoPath(absUrl); |
| 2959 | QTest::qWait(TIME_TO_PROCESS); |
| 2960 | QCOMPARE(ret, true); |
| 2961 | QCOMPARE(m_dirModel_01->path(), absUrl ); |
| 2962 | } |
| 2963 | } |
| 2964 | |
| 2965 | void TestDirModel::smbCdIntoPathRelative() |
| 2966 | { |
| 2967 | SmbItemInfo smbOK(LocationUrl::SmbURL); |
| 2968 | QCOMPARE(smbOK.isValid(), true); |
| 2969 | QCOMPARE(smbOK.isRoot(), true); |
| 2970 | QCOMPARE(smbOK.needsAuthentication(), false); |
| 2971 | QCOMPARE(smbOK.urlPath(), LocationUrl::SmbURL); |
| 2972 | if (SmbUserShare::canCreateShares()) |
| 2973 | { |
| 2974 | QString shareName("smbsmbCdIntoPathRelative"); |
| 2975 | |
| 2976 | TestQSambaSuite smbTest(this); |
| 2977 | |
| 2978 | ShareCreationStatus tmpShare(smbTest.createTempShare(shareName)); |
| 2979 | if (tmpShare.tempDir) |
| 2980 | { |
| 2981 | tmpShare.tempDir->setAutoRemove(true); |
| 2982 | } |
| 2983 | QString createdDir1("createdDir1"); |
| 2984 | QString createdDir2("createdDir2"); |
| 2985 | QString createDirFullPath = tmpShare.sharedDirPath + QDir::separator() + createdDir1 |
| 2986 | + QDir::separator() + createdDir2; |
| 2987 | bool ret = QDir().mkpath(createDirFullPath); |
| 2988 | QCOMPARE(ret , true); |
| 2989 | QString absUrl = LocationUrl::SmbURL + "localhost" + |
| 2990 | QDir::separator() + tmpShare.shareName + |
| 2991 | QDir::separator() + createdDir1; |
| 2992 | |
| 2993 | ret = m_dirModel_01->cdIntoPath(absUrl); |
| 2994 | QTest::qWait(TIME_TO_PROCESS); |
| 2995 | QCOMPARE(ret, true); |
| 2996 | QCOMPARE(m_dirModel_01->path(), absUrl ); |
| 2997 | QCOMPARE(m_dirModel_01->rowCount() , 1); |
| 2998 | |
| 2999 | //cd relative |
| 3000 | absUrl += QDir::separator() + createdDir2; |
| 3001 | ret = m_dirModel_01->cdIntoPath(createdDir2); |
| 3002 | QTest::qWait(TIME_TO_PROCESS); |
| 3003 | QCOMPARE(ret, true); |
| 3004 | QCOMPARE(m_dirModel_01->path(), absUrl ); |
| 3005 | QCOMPARE(m_dirModel_01->rowCount() , 0); |
| 3006 | } |
| 3007 | } |
| 3008 | |
| 3009 | /*! |
| 3010 | * \brief TestDirModel::smbExistsDir() Tests for DirModel::existsDir() regarding Samba |
| 3011 | */ |
| 3012 | void TestDirModel::smbExistsDir() |
| 3013 | { |
| 3014 | CHECK_IF_CAN_CREATE_SHARES(); |
| 3015 | |
| 3016 | QString shareName("smbExistsDir"); |
| 3017 | TestQSambaSuite smbTest(this); |
| 3018 | ShareCreationStatus tmpShare(smbTest.createTempShare(shareName)); |
| 3019 | if (tmpShare.tempDir) |
| 3020 | { |
| 3021 | tmpShare.tempDir->setAutoRemove(true); |
| 3022 | } |
| 3023 | QCOMPARE(tmpShare.status, true); |
| 3024 | |
| 3025 | //create a folder inside this share |
| 3026 | QString folderName("folder"); |
| 3027 | SmbSharedPathAccess folder = tmpShare.createPathForItem(folderName); |
| 3028 | QCOMPARE(QDir().mkpath(folder.diskPathname), true); |
| 3029 | QCOMPARE(QFileInfo(folder.diskPathname).exists(), true); |
| 3030 | //set file mananer to browse the temp share |
| 3031 | m_dirModel_01->setPath(tmpShare.url); |
| 3032 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 3033 | QCOMPARE(m_dirModel_01->rowCount() , 2); //temp shares are created with a file in it |
| 3034 | // check if the folder exists using relative name only |
| 3035 | QCOMPARE(m_dirModel_01->existsDir(folderName), true); |
| 3036 | // check it again using the full samba url |
| 3037 | QCOMPARE(m_dirModel_01->existsDir(folder.smbUrl), true); |
| 3038 | //negative tests |
| 3039 | // check for a relative folder that does not exist |
| 3040 | QString notExist("_I_Hope_it_does_not_exist"); |
| 3041 | QCOMPARE(m_dirModel_01->existsDir(notExist), false); |
| 3042 | QString fullSmbUrl(tmpShare.url + QDir::separator() + notExist); |
| 3043 | QCOMPARE(m_dirModel_01->existsDir(fullSmbUrl), false); |
| 3044 | } |
| 3045 | |
| 3046 | /*! |
| 3047 | * \brief TestDirModel::smbCanReadDir() Tests for DirModel::canReadDir() regading Samba |
| 3048 | */ |
| 3049 | void TestDirModel::smbCanReadDir() |
| 3050 | { |
| 3051 | CHECK_IF_CAN_CREATE_SHARES(); |
| 3052 | |
| 3053 | QString shareName("smbCanReadDir"); |
| 3054 | TestQSambaSuite smbTest(this); |
| 3055 | ShareCreationStatus tmpShare(smbTest.createTempShare(shareName)); |
| 3056 | if (tmpShare.tempDir) |
| 3057 | { |
| 3058 | tmpShare.tempDir->setAutoRemove(true); |
| 3059 | } |
| 3060 | QCOMPARE(tmpShare.status, true); |
| 3061 | |
| 3062 | //create a folder inside this share |
| 3063 | QString folderName("folder"); |
| 3064 | SmbSharedPathAccess folder = tmpShare.createPathForItem(folderName); |
| 3065 | QCOMPARE(QDir().mkpath(folder.diskPathname), true); |
| 3066 | QCOMPARE(QFileInfo(folder.diskPathname).exists(), true); |
| 3067 | //set file mananer to browse the temp share |
| 3068 | m_dirModel_01->setPath(tmpShare.url); |
| 3069 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 3070 | QCOMPARE(m_dirModel_01->rowCount() , 2); //temp shares are created with a file in it |
| 3071 | QFile::Permissions folderOriginalPermissions = QFileInfo(folder.diskPathname).permissions(); |
| 3072 | //first negative tests |
| 3073 | //change the permission to No Read |
| 3074 | bool ok = QFile::setPermissions(folder.diskPathname, QFile::WriteOwner | QFile::ExeOwner); |
| 3075 | QCOMPARE(ok, true); |
| 3076 | // check using relative name only |
| 3077 | QCOMPARE(m_dirModel_01->canReadDir(folderName), false); |
| 3078 | // check it again using the full samba url |
| 3079 | QCOMPARE(m_dirModel_01->canReadDir(folder.smbUrl), false); |
| 3080 | //negative tests |
| 3081 | // check for a relative folder that does not exist |
| 3082 | QString notExist("_I_Hope_it_does_not_exist"); |
| 3083 | QCOMPARE(m_dirModel_01->canReadDir(notExist), false); |
| 3084 | QString fullSmbUrl(tmpShare.url + QDir::separator() + notExist); |
| 3085 | QCOMPARE(m_dirModel_01->canReadDir(fullSmbUrl), false); |
| 3086 | //now positive tests |
| 3087 | //give Read permission |
| 3088 | ok = QFile::setPermissions(folder.diskPathname, folderOriginalPermissions); |
| 3089 | QCOMPARE(ok, true); |
| 3090 | // check using relative name only |
| 3091 | QCOMPARE(m_dirModel_01->canReadDir(folderName), true); |
| 3092 | // check it again using the full samba url |
| 3093 | QCOMPARE(m_dirModel_01->canReadDir(folder.smbUrl), true); |
| 3094 | } |
| 3095 | |
| 3096 | |
| 3097 | /*! |
| 3098 | * \brief TestDirModel::smbExistsFile() Tests for DirModel::existsFile() regading Samba |
| 3099 | */ |
| 3100 | void TestDirModel::smbExistsFile() |
| 3101 | { |
| 3102 | CHECK_IF_CAN_CREATE_SHARES(); |
| 3103 | |
| 3104 | QString shareName("smbExistsFile"); |
| 3105 | TestQSambaSuite smbTest(this); |
| 3106 | ShareCreationStatus tmpShare(smbTest.createTempShare(shareName)); |
| 3107 | if (tmpShare.tempDir) |
| 3108 | { |
| 3109 | tmpShare.tempDir->setAutoRemove(true); |
| 3110 | } |
| 3111 | QCOMPARE(tmpShare.status, true); |
| 3112 | //create a file inside this share |
| 3113 | QString fileName("fileTest.txt"); |
| 3114 | SmbSharedPathAccess file = tmpShare.createPathForItem(fileName); |
| 3115 | QCOMPARE(QFileInfo(file.diskPathname).exists(), false); |
| 3116 | //set file mananer to browse the temp share |
| 3117 | m_dirModel_01->setPath(tmpShare.url); |
| 3118 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 3119 | QCOMPARE(m_dirModel_01->rowCount() , 1); //temp shares are created with a file in it |
| 3120 | //negative tests, file does not exist |
| 3121 | //using relative path |
| 3122 | QCOMPARE(m_dirModel_01->existsFile(fileName), false); |
| 3123 | //using absolute path |
| 3124 | QCOMPARE(m_dirModel_01->existsFile(file.smbUrl), false); |
| 3125 | //positive tests, file exists |
| 3126 | m_dirModel_01->goHome(); |
| 3127 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 3128 | //now create the file |
| 3129 | QFile f(file.diskPathname); |
| 3130 | QCOMPARE(f.open(QFile::WriteOnly), true); |
| 3131 | f.close(); |
| 3132 | //set file mananer to browse the temp share |
| 3133 | m_dirModel_01->setPath(tmpShare.url); |
| 3134 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 3135 | QCOMPARE(m_dirModel_01->rowCount() , 2); //temp shares are created with a file in it |
| 3136 | //using relative path |
| 3137 | QCOMPARE(m_dirModel_01->existsFile(fileName), true); |
| 3138 | //using absolute path |
| 3139 | QCOMPARE(m_dirModel_01->existsFile(file.smbUrl), true); |
| 3140 | } |
| 3141 | |
| 3142 | |
| 3143 | void TestDirModel::smbCanReadFile() |
| 3144 | { |
| 3145 | CHECK_IF_CAN_CREATE_SHARES(); |
| 3146 | |
| 3147 | QString shareName("smbCanReadFile"); |
| 3148 | TestQSambaSuite smbTest(this); |
| 3149 | ShareCreationStatus tmpShare(smbTest.createTempShare(shareName)); |
| 3150 | if (tmpShare.tempDir) |
| 3151 | { |
| 3152 | tmpShare.tempDir->setAutoRemove(true); |
| 3153 | } |
| 3154 | QCOMPARE(tmpShare.status, true); |
| 3155 | |
| 3156 | //create a file inside this share |
| 3157 | QString fileName("fileTest.txt"); |
| 3158 | SmbSharedPathAccess file = tmpShare.createPathForItem(fileName); |
| 3159 | QFile f(file.diskPathname); |
| 3160 | QCOMPARE(f.open(QFile::WriteOnly), true); |
| 3161 | f.close(); |
| 3162 | QCOMPARE(QFileInfo(file.diskPathname).exists(), true); |
| 3163 | //set file mananer to browse the temp share |
| 3164 | m_dirModel_01->setPath(tmpShare.url); |
| 3165 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 3166 | QCOMPARE(m_dirModel_01->rowCount() , 2); //temp shares are created with a file in it |
| 3167 | QFile::Permissions fileOriginalPermissions = QFileInfo(file.diskPathname).permissions(); |
| 3168 | //first negative tests |
| 3169 | //change the permission to No Read |
| 3170 | bool ok = QFile::setPermissions(file.diskPathname, QFile::WriteOwner | QFile::ExeOwner); |
| 3171 | QCOMPARE(ok, true); |
| 3172 | |
| 3173 | // this block was commented due to libsmbclient always bring "read" flag in its stat() functions |
| 3174 | #if 0 |
| 3175 | // check using relative name only |
| 3176 | QCOMPARE(m_dirModel_01->canReadFile(fileName), false); |
| 3177 | // check it again using the full samba url |
| 3178 | QCOMPARE(m_dirModel_01->canReadFile(file.smbUrl), false); |
| 3179 | //negative tests |
| 3180 | #endif |
| 3181 | |
| 3182 | // check for a relative folder that does not exist |
| 3183 | QString notExist("_I_Hope_it_does_not_exist"); |
| 3184 | QCOMPARE(m_dirModel_01->canReadFile(notExist), false); |
| 3185 | QString fullSmbUrl(tmpShare.url + QDir::separator() + notExist); |
| 3186 | QCOMPARE(m_dirModel_01->canReadFile(fullSmbUrl), false); |
| 3187 | //now positive tests |
| 3188 | //give Read permission |
| 3189 | ok = QFile::setPermissions(file.diskPathname, fileOriginalPermissions); |
| 3190 | QCOMPARE(ok, true); |
| 3191 | // check using relative name only |
| 3192 | QCOMPARE(m_dirModel_01->canReadFile(fileName), true); |
| 3193 | // check it again using the full samba url |
| 3194 | QCOMPARE(m_dirModel_01->canReadFile(file.smbUrl), true); |
| 3195 | } |
| 3196 | |
| 3197 | /*! |
| 3198 | * \brief TestDirModel::smbMkdir() Tests for DirModel::mkdir() regarding Samba |
| 3199 | */ |
| 3200 | void TestDirModel::smbMkdir() |
| 3201 | { |
| 3202 | CHECK_IF_CAN_CREATE_SHARES(); |
| 3203 | |
| 3204 | QString shareName("smbMkdir"); |
| 3205 | TestQSambaSuite smbTest(this); |
| 3206 | ShareCreationStatus tmpShare(smbTest.createTempShare(shareName)); |
| 3207 | if (tmpShare.tempDir) |
| 3208 | { |
| 3209 | tmpShare.tempDir->setAutoRemove(true); |
| 3210 | } |
| 3211 | QCOMPARE(tmpShare.status, true); |
| 3212 | //set file mananer to browse the temp share |
| 3213 | m_dirModel_01->setPath(tmpShare.url); |
| 3214 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 3215 | QCOMPARE(m_dirModel_01->rowCount() , 1); |
| 3216 | |
| 3217 | QString folder1Str("folder1"); |
| 3218 | QString folder2Str("folder2"); |
| 3219 | SmbSharedPathAccess folder1 = tmpShare.createPathForItem(folder1Str); |
| 3220 | SmbSharedPathAccess folder2 = tmpShare.createPathForItem(folder2Str); |
| 3221 | //using disk make sure folder1 does not exist |
| 3222 | QCOMPARE(QFileInfo(folder1.diskPathname).exists(), false); |
| 3223 | //create folder1 using relative path |
| 3224 | QCOMPARE(m_dirModel_01->mkdir(folder1Str), true); |
| 3225 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 3226 | QCOMPARE(m_dirModel_01->rowCount() , 2); |
| 3227 | QCOMPARE(QFileInfo(folder1.diskPathname).isDir(), true); |
| 3228 | //folder2 is created using full url |
| 3229 | QCOMPARE(QFileInfo(folder2.diskPathname).exists(), false); |
| 3230 | //create folder2 using full Samba URL |
| 3231 | QCOMPARE(m_dirModel_01->mkdir(folder2.smbUrl), true); |
| 3232 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 3233 | QCOMPARE(m_dirModel_01->rowCount() , 3); |
| 3234 | QCOMPARE(QFileInfo(folder2.diskPathname).isDir(), true); |
| 3235 | //check for full URL in the model items |
| 3236 | int counter = m_dirModel_01->rowCount() ; |
| 3237 | int found = 0; // look for folder1 and folder2 full url |
| 3238 | while (counter--) |
| 3239 | { |
| 3240 | const DirItemInfo & item = m_dirModel_01->mDirectoryContents[counter]; |
| 3241 | if (item.urlPath() == folder1.smbUrl || item.urlPath() == folder2.smbUrl) |
| 3242 | { |
| 3243 | ++found; |
| 3244 | } |
| 3245 | } |
| 3246 | QCOMPARE(found, 2); //both folder1 and folder2 URLs are present |
| 3247 | } |
| 3248 | |
| 3249 | /*! |
| 3250 | * \brief TestDirModel::smbMakeBackupNameForCurrentItem() Tests making backup of items using copy |
| 3251 | * |
| 3252 | * The similar test for item in local disk is modelCopyAndPasteToBackupFiles() |
| 3253 | */ |
| 3254 | void TestDirModel::smbMakeBackupNameForCurrentItem() |
| 3255 | { |
| 3256 | CHECK_IF_CAN_CREATE_SHARES(); |
| 3257 | |
| 3258 | QString shareName("smbMakeBackupNameForCurrentItem"); |
| 3259 | TestQSambaSuite smbTest(this); |
| 3260 | ShareCreationStatus tmpShare(smbTest.createTempShare(shareName)); |
| 3261 | if (tmpShare.tempDir) |
| 3262 | { |
| 3263 | tmpShare.tempDir->setAutoRemove(true); |
| 3264 | } |
| 3265 | QCOMPARE(tmpShare.status, true); |
| 3266 | //set file mananer to browse the temp share |
| 3267 | m_dirModel_01->setPath(tmpShare.url); |
| 3268 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 3269 | QCOMPARE(m_dirModel_01->rowCount() , 1); |
| 3270 | //copy existent file in the Samba share, index 0 refers to tmpShare.fileContent.smbUrl |
| 3271 | m_dirModel_01->copyIndex(0); |
| 3272 | //pasting in the same directory must create item to Copy(1) |
| 3273 | m_dirModel_01->paste(); |
| 3274 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 3275 | QCOMPARE(m_dirModel_01->rowCount() , 2); |
| 3276 | //pasting again must create item to Copy(2) |
| 3277 | m_dirModel_01->paste(); |
| 3278 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 3279 | QCOMPARE(m_dirModel_01->rowCount() , 3); |
| 3280 | int counter = m_dirModel_01->rowCount() ; |
| 3281 | int found = 0; |
| 3282 | QFile fileContent(tmpShare.fileContent.diskPathname); |
| 3283 | QCOMPARE(fileContent.open(QFile::ReadOnly), true); |
| 3284 | QByteArray fileContentBytes(fileContent.readAll()); |
| 3285 | fileContent.close(); |
| 3286 | char smbBuffer [fileContentBytes.size()]; |
| 3287 | qint64 size = (qint64)fileContentBytes.size(); |
| 3288 | while (counter--) |
| 3289 | { |
| 3290 | const DirItemInfo & item = m_dirModel_01->mDirectoryContents[counter]; |
| 3291 | //compare size content with the original file |
| 3292 | QCOMPARE(item.size(), size); |
| 3293 | SmbLocationItemFile smbFile(item.urlPath()); |
| 3294 | QCOMPARE(smbFile.open(QFile::ReadOnly), true); |
| 3295 | QCOMPARE(smbFile.read(smbBuffer, size), size); |
| 3296 | smbFile.close(); |
| 3297 | QByteArray smbContentBytes((const char*)&smbBuffer, fileContentBytes.size()); |
| 3298 | //files must have the same content |
| 3299 | QCOMPARE(smbContentBytes, fileContentBytes); |
| 3300 | //look for backuped names |
| 3301 | if (item.urlPath().contains("Copy(1)") || item.urlPath().contains("Copy(2)")) |
| 3302 | { |
| 3303 | ++found; |
| 3304 | } |
| 3305 | } |
| 3306 | QCOMPARE(found, 2); //both backuped files were found |
| 3307 | } |
| 3308 | |
| 3309 | /*! |
| 3310 | * \brief TestDirModel::smbCopyFromSmb2Smb() |
| 3311 | * |
| 3312 | * Creates a Samba Share in the local disk |
| 3313 | * Creates a directory tree with some files under smb://localhost/<tempShareName>/source |
| 3314 | * Copy its content to smb://localhost/<tempShareName>/target |
| 3315 | * Compares both directories using local disk correspondent path |
| 3316 | */ |
| 3317 | void TestDirModel::smbCopyFromSmb2Smb() |
| 3318 | { |
| 3319 | CHECK_IF_CAN_CREATE_SHARES(); |
| 3320 | |
| 3321 | QString shareName("smbCopyFromSmb2Smb"); |
| 3322 | TestQSambaSuite smbTest(this); |
| 3323 | ShareCreationStatus tmpShare(smbTest.createTempShare(shareName)); |
| 3324 | if (tmpShare.tempDir) |
| 3325 | { |
| 3326 | tmpShare.tempDir->setAutoRemove(true); |
| 3327 | } |
| 3328 | QCOMPARE(tmpShare.status, true); |
| 3329 | QString sourceFolderName("source"); |
| 3330 | QString targetFolderName("target"); |
| 3331 | SmbSharedPathAccess sourceFolder = tmpShare.createPathForItem(sourceFolderName); |
| 3332 | SmbSharedPathAccess targeFolder = tmpShare.createPathForItem(targetFolderName); |
| 3333 | //create a soruce directory tree in the share |
| 3334 | DeepDir sdir(sourceFolder.diskPathname, 3); |
| 3335 | //create the destination folder |
| 3336 | DeepDir tdir(targeFolder.diskPathname, 0); // 0 level, just create the root directory |
| 3337 | Q_UNUSED(sdir); |
| 3338 | Q_UNUSED(tdir); |
| 3339 | /* |
| 3340 | * set file mananer to browse the source directory using Samba url |
| 3341 | */ |
| 3342 | m_dirModel_01->setPath(sourceFolder.smbUrl); |
| 3343 | QTest::qWait(TIME_TO_REFRESH_REMOTE_DIR); |
| 3344 | QVERIFY(m_dirModel_01->rowCount() != 0); |
| 3345 | DirSelection *selection = m_dirModel_01->selectionObject(); |
| 3346 | QVERIFY(selection != 0); |
| 3347 | selection->selectAll(); |
| 3348 | QVERIFY(selection->counter() != 0); |
| 3349 | //copy the source folder using Samba URL |
| 3350 | m_dirModel_01->copySelection(); |
| 3351 | //change directory to the target folder |
| 3352 | m_dirModel_01->setPath(targeFolder.smbUrl); |
| 3353 | QTest::qWait(TIME_TO_REFRESH_REMOTE_DIR); |
| 3354 | QCOMPARE(m_dirModel_01->rowCount() , 0); // so far it is empty |
| 3355 | m_dirModel_01->paste(); |
| 3356 | QTest::qWait(TIME_TO_REFRESH_REMOTE_DIR); |
| 3357 | QVERIFY(m_dirModel_01->rowCount() != 0); //no longer empty |
| 3358 | //now compare directories source and target, permissions are not the same due to Samba Configuration |
| 3359 | QCOMPARE(compareDirectories(sourceFolder.diskPathname, targeFolder.diskPathname, false), true); |
| 3360 | } |
| 3361 | |
| 3362 | |
| 3363 | void TestDirModel::smbCopyFromSmb2LocalDisk() |
| 3364 | { |
| 3365 | CHECK_IF_CAN_CREATE_SHARES(); |
| 3366 | |
| 3367 | QString shareName("smbCopyFromSmb2LocalDisk"); |
| 3368 | TestQSambaSuite smbTest(this); |
| 3369 | ShareCreationStatus tmpShare(smbTest.createTempShare(shareName)); |
| 3370 | if (tmpShare.tempDir) |
| 3371 | { |
| 3372 | tmpShare.tempDir->setAutoRemove(true); |
| 3373 | } |
| 3374 | QCOMPARE(tmpShare.status, true); |
| 3375 | QString sourceFolderName("source"); |
| 3376 | QString targetFolderName("target"); |
| 3377 | SmbSharedPathAccess sourceFolder = tmpShare.createPathForItem(sourceFolderName); |
| 3378 | //create a soruce directory tree in the share |
| 3379 | DeepDir sdir(sourceFolder.diskPathname, 3); |
| 3380 | //create the destination folder in the /tmp |
| 3381 | DeepDir targetLocalDisk(targetFolderName, 0); // 0 level, just create the root directory |
| 3382 | Q_UNUSED(sdir); |
| 3383 | /* |
| 3384 | * set file mananer to browse the source directory using Samba url |
| 3385 | */ |
| 3386 | m_dirModel_01->setPath(sourceFolder.smbUrl); |
| 3387 | QTest::qWait(TIME_TO_REFRESH_REMOTE_DIR); |
| 3388 | QVERIFY(m_dirModel_01->rowCount() != 0); |
| 3389 | DirSelection *selection = m_dirModel_01->selectionObject(); |
| 3390 | QVERIFY(selection != 0); |
| 3391 | selection->selectAll(); |
| 3392 | QVERIFY(selection->counter() != 0); |
| 3393 | //copy the source folder using Samba URL |
| 3394 | m_dirModel_01->copySelection(); |
| 3395 | //change directory to the target folder in the local disk |
| 3396 | m_dirModel_01->setPath(targetLocalDisk.path()); |
| 3397 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 3398 | QCOMPARE(m_dirModel_01->rowCount() , 0); // so far it is empty |
| 3399 | m_dirModel_01->paste(); |
| 3400 | QTest::qWait(TIME_TO_REFRESH_REMOTE_DIR); |
| 3401 | QVERIFY(m_dirModel_01->rowCount() != 0); //no longer empty |
| 3402 | //now compare directories source and target, permissions are not the same due to Samba Configuration |
| 3403 | QCOMPARE(compareDirectories(sourceFolder.diskPathname, targetLocalDisk.path(), false), true); |
| 3404 | } |
| 3405 | |
| 3406 | |
| 3407 | void TestDirModel::smbCopyFromLocalDisk2Smb() |
| 3408 | { |
| 3409 | CHECK_IF_CAN_CREATE_SHARES(); |
| 3410 | |
| 3411 | QString shareName("smbCopyFromLocalDisk2Smb"); |
| 3412 | TestQSambaSuite smbTest(this); |
| 3413 | ShareCreationStatus tmpShare(smbTest.createTempShare(shareName)); |
| 3414 | if (tmpShare.tempDir) |
| 3415 | { |
| 3416 | tmpShare.tempDir->setAutoRemove(true); |
| 3417 | } |
| 3418 | QCOMPARE(tmpShare.status, true); |
| 3419 | QString sourceFolderName("source"); |
| 3420 | QString targetFolderName("target"); |
| 3421 | SmbSharedPathAccess targeFolder = tmpShare.createPathForItem(targetFolderName); |
| 3422 | //create a soruce directory tree in /tmp |
| 3423 | DeepDir diskDir(sourceFolderName, 3); |
| 3424 | //create the destination folder in the share |
| 3425 | DeepDir tdir(targeFolder.diskPathname, 0); // 0 level, just create the root directory |
| 3426 | Q_UNUSED(tdir); |
| 3427 | /* |
| 3428 | * set file mananer to browse the source in the /tmp |
| 3429 | */ |
| 3430 | m_dirModel_01->setPath(diskDir.path()); |
| 3431 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 3432 | QVERIFY(m_dirModel_01->rowCount() != 0); |
| 3433 | DirSelection *selection = m_dirModel_01->selectionObject(); |
| 3434 | QVERIFY(selection != 0); |
| 3435 | selection->selectAll(); |
| 3436 | QVERIFY(selection->counter() != 0); |
| 3437 | //copy the source folder using file:// URLs |
| 3438 | m_dirModel_01->copySelection(); |
| 3439 | //change directory to the target folder |
| 3440 | m_dirModel_01->setPath(targeFolder.smbUrl); |
| 3441 | QTest::qWait(TIME_TO_REFRESH_REMOTE_DIR); |
| 3442 | QCOMPARE(m_dirModel_01->rowCount() , 0); // so far it is empty |
| 3443 | m_dirModel_01->paste(); |
| 3444 | QTest::qWait(TIME_TO_REFRESH_REMOTE_DIR); |
| 3445 | QVERIFY(m_dirModel_01->rowCount() != 0); //no longer empty |
| 3446 | //now compare directories source and target, permissions are not the same due to Samba Configuration |
| 3447 | QCOMPARE(compareDirectories(diskDir.path(), targeFolder.diskPathname, false), true); |
| 3448 | } |
| 3449 | |
| 3450 | |
| 3451 | void TestDirModel::smbRemoveDirectory() |
| 3452 | { |
| 3453 | CHECK_IF_CAN_CREATE_SHARES(); |
| 3454 | |
| 3455 | QString shareName("smbRemoveDirectory"); |
| 3456 | TestQSambaSuite smbTest(this); |
| 3457 | ShareCreationStatus tmpShare(smbTest.createTempShare(shareName)); |
| 3458 | if (tmpShare.tempDir) |
| 3459 | { |
| 3460 | tmpShare.tempDir->setAutoRemove(true); |
| 3461 | } |
| 3462 | QCOMPARE(tmpShare.status, true); |
| 3463 | SmbSharedPathAccess sourceFolder = tmpShare.createPathForItem("source"); |
| 3464 | //create a directory tree with items in the local disk shared as Samba share |
| 3465 | DeepDir diskDir(sourceFolder.diskPathname, 3); |
| 3466 | Q_UNUSED(diskDir); |
| 3467 | /* |
| 3468 | * set file mananer to browse the source in the Samba share |
| 3469 | */ |
| 3470 | m_dirModel_01->setPath(sourceFolder.smbUrl); |
| 3471 | QTest::qWait(TIME_TO_REFRESH_REMOTE_DIR); |
| 3472 | QVERIFY(m_dirModel_01->rowCount() != 0); |
| 3473 | DirSelection *selection = m_dirModel_01->selectionObject(); |
| 3474 | QVERIFY(selection != 0); |
| 3475 | selection->selectAll(); |
| 3476 | QVERIFY(selection->counter() != 0); |
| 3477 | m_dirModel_01->removeSelection(); |
| 3478 | QTest::qWait(TIME_TO_REFRESH_REMOTE_DIR); |
| 3479 | QCOMPARE(m_dirModel_01->rowCount() , 0); // now it is empty |
| 3480 | } |
| 3481 | |
| 3482 | |
| 3483 | void TestDirModel::smbCutFromSmb2Smb() |
| 3484 | { |
| 3485 | CHECK_IF_CAN_CREATE_SHARES(); |
| 3486 | |
| 3487 | QString shareName("smbCutFromSmb2Smb"); |
| 3488 | TestQSambaSuite smbTest(this); |
| 3489 | ShareCreationStatus tmpShare(smbTest.createTempShare(shareName)); |
| 3490 | if (tmpShare.tempDir) |
| 3491 | { |
| 3492 | tmpShare.tempDir->setAutoRemove(true); |
| 3493 | } |
| 3494 | QCOMPARE(tmpShare.status, true); |
| 3495 | QString sourceFolderName("source"); |
| 3496 | QString targetFolderName("target"); |
| 3497 | SmbSharedPathAccess sourceFolder = tmpShare.createPathForItem(sourceFolderName); |
| 3498 | SmbSharedPathAccess targeFolder = tmpShare.createPathForItem(targetFolderName); |
| 3499 | //create a soruce directory tree in the share |
| 3500 | DeepDir sdir(sourceFolder.diskPathname, 3); |
| 3501 | //create the destination folder |
| 3502 | DeepDir tdir(targeFolder.diskPathname, 0); // 0 level, just create the root directory |
| 3503 | Q_UNUSED(sdir); |
| 3504 | Q_UNUSED(tdir); |
| 3505 | /* |
| 3506 | * set file mananer to browse the source directory using Samba url |
| 3507 | */ |
| 3508 | m_dirModel_01->setPath(sourceFolder.smbUrl); |
| 3509 | QTest::qWait(TIME_TO_REFRESH_REMOTE_DIR); |
| 3510 | QVERIFY(m_dirModel_01->rowCount() != 0); |
| 3511 | DirSelection *selection = m_dirModel_01->selectionObject(); |
| 3512 | QVERIFY(selection != 0); |
| 3513 | selection->selectAll(); |
| 3514 | QVERIFY(selection->counter() != 0); |
| 3515 | //cut the source folder using Samba URL |
| 3516 | m_dirModel_01->cutSelection(); |
| 3517 | //change directory to the target folder |
| 3518 | m_dirModel_01->setPath(targeFolder.smbUrl); |
| 3519 | QTest::qWait(TIME_TO_REFRESH_REMOTE_DIR); |
| 3520 | QCOMPARE(m_dirModel_01->rowCount() , 0); // so far it is empty |
| 3521 | //paste |
| 3522 | m_dirModel_01->paste(); |
| 3523 | QTest::qWait(TIME_TO_REFRESH_REMOTE_DIR); |
| 3524 | //check target directory is not empty |
| 3525 | QVERIFY(m_dirModel_01->rowCount() != 0); //no longer empty |
| 3526 | //go back to the source samba url to make sure it is empty |
| 3527 | m_dirModel_01->setPath(sourceFolder.smbUrl); |
| 3528 | QTest::qWait(TIME_TO_REFRESH_REMOTE_DIR); |
| 3529 | QCOMPARE(m_dirModel_01->rowCount(), 0); |
| 3530 | } |
| 3531 | |
| 3532 | |
| 3533 | void TestDirModel::smbCutFromSmb2LocalDisk() |
| 3534 | { |
| 3535 | CHECK_IF_CAN_CREATE_SHARES(); |
| 3536 | |
| 3537 | QString shareName("smbCutFromSmb2LocalDisk"); |
| 3538 | TestQSambaSuite smbTest(this); |
| 3539 | ShareCreationStatus tmpShare(smbTest.createTempShare(shareName)); |
| 3540 | if (tmpShare.tempDir) |
| 3541 | { |
| 3542 | tmpShare.tempDir->setAutoRemove(true); |
| 3543 | } |
| 3544 | QCOMPARE(tmpShare.status, true); |
| 3545 | QString sourceFolderName("source"); |
| 3546 | QString targetFolderName("diskTarget"); |
| 3547 | SmbSharedPathAccess sourceFolder = tmpShare.createPathForItem(sourceFolderName); |
| 3548 | //create a soruce directory tree in the share |
| 3549 | DeepDir sdir(sourceFolder.diskPathname, 3); |
| 3550 | //create the destination folder |
| 3551 | DeepDir diskTarget(targetFolderName, 0); // 0 level, just create the root directory |
| 3552 | Q_UNUSED(sdir); |
| 3553 | /* |
| 3554 | * set file mananer to browse the source directory using Samba url |
| 3555 | */ |
| 3556 | m_dirModel_01->setPath(sourceFolder.smbUrl); |
| 3557 | QTest::qWait(TIME_TO_REFRESH_REMOTE_DIR); |
| 3558 | QVERIFY(m_dirModel_01->rowCount() != 0); |
| 3559 | DirSelection *selection = m_dirModel_01->selectionObject(); |
| 3560 | QVERIFY(selection != 0); |
| 3561 | selection->selectAll(); |
| 3562 | QVERIFY(selection->counter() != 0); |
| 3563 | //cut the source folder using Samba URL |
| 3564 | m_dirModel_01->cutSelection(); |
| 3565 | //change directory to the target folder in the local disk |
| 3566 | m_dirModel_01->setPath(diskTarget.path()); |
| 3567 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 3568 | QCOMPARE(m_dirModel_01->rowCount() , 0); // so far it is empty |
| 3569 | //paste |
| 3570 | m_dirModel_01->paste(); |
| 3571 | QTest::qWait(TIME_TO_REFRESH_REMOTE_DIR); //removes from Samba, needs more time |
| 3572 | //check target directory is not empty |
| 3573 | QVERIFY(m_dirModel_01->rowCount() != 0); //no longer empty |
| 3574 | //go back to the source samba url to make sure it is empty |
| 3575 | m_dirModel_01->setPath(sourceFolder.smbUrl); |
| 3576 | QTest::qWait(TIME_TO_REFRESH_REMOTE_DIR); |
| 3577 | QCOMPARE(m_dirModel_01->rowCount(), 0); |
| 3578 | } |
| 3579 | |
| 3580 | |
| 3581 | void TestDirModel::smbCutFromLocalDisk2Smb() |
| 3582 | { |
| 3583 | CHECK_IF_CAN_CREATE_SHARES(); |
| 3584 | |
| 3585 | QString shareName("smbCutFromLocalDisk2Smb"); |
| 3586 | TestQSambaSuite smbTest(this); |
| 3587 | ShareCreationStatus tmpShare(smbTest.createTempShare(shareName)); |
| 3588 | if (tmpShare.tempDir) |
| 3589 | { |
| 3590 | tmpShare.tempDir->setAutoRemove(true); |
| 3591 | } |
| 3592 | QCOMPARE(tmpShare.status, true); |
| 3593 | QString sourceFolderName("source"); |
| 3594 | QString targetFolderName("sambaTarget"); |
| 3595 | SmbSharedPathAccess targetFolder = tmpShare.createPathForItem(targetFolderName); |
| 3596 | //create a soruce directory tree in the share |
| 3597 | DeepDir sdir(sourceFolderName, 3); |
| 3598 | //create the destination folder |
| 3599 | DeepDir smbTarget(targetFolder.diskPathname, 0); // 0 level, just create the root directory |
| 3600 | Q_UNUSED(smbTarget); |
| 3601 | /* |
| 3602 | * set file mananer to browse the source directory in the local disk |
| 3603 | */ |
| 3604 | m_dirModel_01->setPath(sdir.path()); |
| 3605 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 3606 | QVERIFY(m_dirModel_01->rowCount() != 0); |
| 3607 | DirSelection *selection = m_dirModel_01->selectionObject(); |
| 3608 | QVERIFY(selection != 0); |
| 3609 | selection->selectAll(); |
| 3610 | QVERIFY(selection->counter() != 0); |
| 3611 | //cut the source folder using local disk |
| 3612 | m_dirModel_01->cutSelection(); |
| 3613 | //change directory to the target folder in Samba share |
| 3614 | m_dirModel_01->setPath(targetFolder.smbUrl); |
| 3615 | QTest::qWait(TIME_TO_REFRESH_REMOTE_DIR); |
| 3616 | QCOMPARE(m_dirModel_01->rowCount() , 0); // so far it is empty |
| 3617 | //paste |
| 3618 | m_dirModel_01->paste(); |
| 3619 | QTest::qWait(TIME_TO_REFRESH_REMOTE_DIR); |
| 3620 | //check target directory is not empty |
| 3621 | QVERIFY(m_dirModel_01->rowCount() != 0); //no longer empty |
| 3622 | //go back to the source local disk make sure it is empty |
| 3623 | m_dirModel_01->setPath(sdir.path()); |
| 3624 | QTest::qWait(TIME_TO_REFRESH_DIR); |
| 3625 | QCOMPARE(m_dirModel_01->rowCount(), 0); |
| 3626 | } |
| 3627 | |
| 3628 | |
| 3629 | void TestDirModel::smbDownloadEmptyFile() |
| 3630 | { |
| 3631 | CHECK_IF_CAN_CREATE_SHARES(); |
| 3632 | |
| 3633 | QString shareName("smbDownloadEmptyFile"); |
| 3634 | TestQSambaSuite smbTest(this); |
| 3635 | ShareCreationStatus tmpShare(smbTest.createTempShare(shareName)); |
| 3636 | if (tmpShare.tempDir) |
| 3637 | { |
| 3638 | tmpShare.tempDir->setAutoRemove(true); |
| 3639 | } |
| 3640 | QCOMPARE(tmpShare.status, true); |
| 3641 | |
| 3642 | // temp shares are created with a unique file there with some content |
| 3643 | // this test just rewrite this file with no content |
| 3644 | QFile empty (tmpShare.fileContent.diskPathname) ; |
| 3645 | QCOMPARE(empty.open(QFile::WriteOnly | QFile::Truncate), true); |
| 3646 | empty.close(); |
| 3647 | //make sure the file is empty |
| 3648 | QFileInfo sourceInfo(tmpShare.fileContent.diskPathname); |
| 3649 | QCOMPARE(sourceInfo.size(), (qint64)0); |
| 3650 | |
| 3651 | m_dirModel_01->setPath(tmpShare.url); |
| 3652 | QTest::qWait(TIME_TO_REFRESH_REMOTE_DIR); |
| 3653 | QVERIFY(m_dirModel_01->rowCount() != 0); |
| 3654 | |
| 3655 | const DirItemInfo & item = m_dirModel_01->mDirectoryContents.at(0); |
| 3656 | QCOMPARE(item.isFile(), true); |
| 3657 | QVERIFY(item.size() == 0); |
| 3658 | |
| 3659 | QString tmpFile(QDir::tempPath() + QDir::separator() + "_empty_file.ttt"); |
| 3660 | |
| 3661 | QCOMPARE(m_dirModel_01->downloadAndSaveAs(0, tmpFile), true); |
| 3662 | QTest::qWait(TIME_TO_PROCESS); |
| 3663 | |
| 3664 | QFileInfo targetInfo(tmpFile); |
| 3665 | QCOMPARE(targetInfo.exists(), true); |
| 3666 | QCOMPARE(targetInfo.size(), (qint64)0); |
| 3667 | } |
| 3668 | |
| 3669 | |
| 3670 | void TestDirModel::smbDownloadBigFileWithNoSpace() |
| 3671 | { |
| 3672 | CHECK_IF_CAN_CREATE_SHARES(); |
| 3673 | |
| 3674 | QString shareName("smbDownloadBigFileWithNoSpace"); |
| 3675 | TestQSambaSuite smbTest(this); |
| 3676 | ShareCreationStatus tmpShare(smbTest.createTempShare(shareName)); |
| 3677 | if (tmpShare.tempDir) |
| 3678 | { |
| 3679 | tmpShare.tempDir->setAutoRemove(true); |
| 3680 | } |
| 3681 | QCOMPARE(tmpShare.status, true); |
| 3682 | |
| 3683 | m_dirModel_01->setPath(tmpShare.url); |
| 3684 | QTest::qWait(TIME_TO_REFRESH_REMOTE_DIR); |
| 3685 | QVERIFY(m_dirModel_01->rowCount() != 0); |
| 3686 | |
| 3687 | const DirItemInfo & item = m_dirModel_01->mDirectoryContents.at(0); |
| 3688 | QCOMPARE(item.isFile(), true); |
| 3689 | QVERIFY(item.size() > 0); |
| 3690 | |
| 3691 | FakeDirItemInfoBigSize bigSize(item); |
| 3692 | |
| 3693 | QString tmpFile(QDir::tempPath() + QDir::separator() + "_it_does_not_have_space.ttt"); |
| 3694 | QCOMPARE(m_dirModel_01->m_fsAction->downloadAndSaveAs(bigSize, tmpFile) , false); |
| 3695 | } |
| 3696 | |
| 3697 | |
| 3698 | void TestDirModel::smbDownloadFile() |
| 3699 | { |
| 3700 | CHECK_IF_CAN_CREATE_SHARES(); |
| 3701 | |
| 3702 | QString shareName("smbDownloadFile"); |
| 3703 | TestQSambaSuite smbTest(this); |
| 3704 | ShareCreationStatus tmpShare(smbTest.createTempShare(shareName)); |
| 3705 | if (tmpShare.tempDir) |
| 3706 | { |
| 3707 | tmpShare.tempDir->setAutoRemove(true); |
| 3708 | } |
| 3709 | QCOMPARE(tmpShare.status, true); |
| 3710 | |
| 3711 | // temp shares are created with a unique file there with some content |
| 3712 | QFileInfo sourceInfo(tmpShare.fileContent.diskPathname); |
| 3713 | |
| 3714 | m_dirModel_01->setPath(tmpShare.url); |
| 3715 | QTest::qWait(TIME_TO_REFRESH_REMOTE_DIR); |
| 3716 | QVERIFY(m_dirModel_01->rowCount() != 0); |
| 3717 | |
| 3718 | const DirItemInfo & item = m_dirModel_01->mDirectoryContents.at(0); |
| 3719 | QCOMPARE(item.isFile(), true); |
| 3720 | QVERIFY(item.size() > 0); |
| 3721 | |
| 3722 | QString tmpFile(QDir::tempPath() + QDir::separator() + "_it_can_exist_always_overwritten.ttt"); |
| 3723 | |
| 3724 | QCOMPARE(m_dirModel_01->downloadAndSaveAs(0, tmpFile), true); |
| 3725 | QTest::qWait(TIME_TO_PROCESS); |
| 3726 | |
| 3727 | QFileInfo targetInfo(tmpFile); |
| 3728 | QCOMPARE(targetInfo.exists(), true); |
| 3729 | |
| 3730 | QCOMPARE(sourceInfo.size(), targetInfo.size()); |
| 3731 | } |
| 3732 | |
| 3733 | |
| 3734 | void TestDirModel::smbDownloadAsTemporary() |
| 3735 | { |
| 3736 | CHECK_IF_CAN_CREATE_SHARES(); |
| 3737 | |
| 3738 | QString shareName("smbDownloadAsTemporary"); |
| 3739 | TestQSambaSuite smbTest(this); |
| 3740 | ShareCreationStatus tmpShare(smbTest.createTempShare(shareName)); |
| 3741 | if (tmpShare.tempDir) |
| 3742 | { |
| 3743 | tmpShare.tempDir->setAutoRemove(true); |
| 3744 | } |
| 3745 | QCOMPARE(tmpShare.status, true); |
| 3746 | |
| 3747 | // temp shares are created with a unique file there with some content |
| 3748 | QFileInfo sourceInfo(tmpShare.fileContent.diskPathname); |
| 3749 | |
| 3750 | m_temporaryDownloadName.clear(); |
| 3751 | connect(m_dirModel_01, SIGNAL(downloadTemporaryComplete(QString)), |
| 3752 | this, SLOT(onDownloadTemporaryComplete(QString))); |
| 3753 | |
| 3754 | m_dirModel_01->setPath(tmpShare.url); |
| 3755 | QTest::qWait(TIME_TO_REFRESH_REMOTE_DIR); |
| 3756 | QVERIFY(m_dirModel_01->rowCount() != 0); |
| 3757 | |
| 3758 | QCOMPARE(m_dirModel_01->downloadAsTemporaryFile(0), true); |
| 3759 | QTest::qWait(TIME_TO_PROCESS); |
| 3760 | //signal received |
| 3761 | QCOMPARE(m_temporaryDownloadName.isEmpty(), false); |
| 3762 | |
| 3763 | QFileInfo targetInfo(m_temporaryDownloadName); |
| 3764 | QCOMPARE(targetInfo.exists(), true); |
| 3765 | QCOMPARE(sourceInfo.size(), targetInfo.size()); |
| 3766 | qDebug() << "downloaded temp file as:" << targetInfo.absoluteFilePath(); |
| 3767 | } |
| 3768 | |
| 3769 | |
| 3770 | void TestDirModel::smbDownloadIntoStandardDownloadLocation() |
| 3771 | { |
| 3772 | CHECK_IF_CAN_CREATE_SHARES(); |
| 3773 | |
| 3774 | QString shareName("smbDownloadIntoStandardDownloadLocation"); |
| 3775 | TestQSambaSuite smbTest(this); |
| 3776 | ShareCreationStatus tmpShare(smbTest.createTempShare(shareName)); |
| 3777 | if (tmpShare.tempDir) |
| 3778 | { |
| 3779 | tmpShare.tempDir->setAutoRemove(true); |
| 3780 | } |
| 3781 | QCOMPARE(tmpShare.status, true); |
| 3782 | |
| 3783 | // temp shares are created with a unique file there with some content |
| 3784 | QFileInfo sourceInfo(tmpShare.fileContent.diskPathname); |
| 3785 | |
| 3786 | QString dowloadFullPath(QStandardPaths::writableLocation(QStandardPaths::DownloadLocation) + |
| 3787 | QDir::separator() + sourceInfo.fileName() ); |
| 3788 | |
| 3789 | QFile f(dowloadFullPath); |
| 3790 | f.remove(); |
| 3791 | |
| 3792 | //make sure target does not exist |
| 3793 | QCOMPARE(QFileInfo(dowloadFullPath).exists(), false); |
| 3794 | |
| 3795 | m_dirModel_01->setPath(tmpShare.url); |
| 3796 | QTest::qWait(TIME_TO_REFRESH_REMOTE_DIR); |
| 3797 | QVERIFY(m_dirModel_01->rowCount() != 0); |
| 3798 | |
| 3799 | QCOMPARE(m_dirModel_01->download(0), true); |
| 3800 | QTest::qWait(TIME_TO_PROCESS); |
| 3801 | |
| 3802 | QFileInfo targetInfo(dowloadFullPath); |
| 3803 | QCOMPARE(targetInfo.exists(), true); |
| 3804 | QCOMPARE(sourceInfo.size(), targetInfo.size()); |
| 3805 | qDebug() << "downloaded:" << targetInfo.absoluteFilePath(); |
| 3806 | } |
| 3807 | |
| 3808 | int main(int argc, char *argv[]) |
| 3809 | { |
| 3810 | QApplication app(argc, argv); |
| 3811 | TestDirModel tc; |
| 3812 | QStringList args = QCoreApplication::arguments(); |
| 3813 | int count = args.count(); |
| 3814 | while (count--) |
| 3815 | { |
| 3816 | //Ubuntu Touch parameter |
| 3817 | if (args.at(count).startsWith(QLatin1String("--desktop_file_hint"))) |
| 3818 | { |
| 3819 | args.removeAt(count); |
| 3820 | break; |
| 3821 | } |
| 3822 | } |
| 3823 | int ret = QTest::qExec(&tc, args); |
| 3824 | //if not running specific tests, run Samba test suite |
| 3825 | if (args.count() == 1) |
| 3826 | { |
| 3827 | if (SmbUserShare::canCreateShares()) |
| 3828 | { |
| 3829 | printf("\n********* Samba tests ********\n"); |
| 3830 | TestQSambaSuite smbTest; |
| 3831 | ret |= QTest::qExec(&smbTest, args); |
| 3832 | } |
| 3833 | else |
| 3834 | { |
| 3835 | printf("\n\n********* Make sure Samba server is installed and up to run specific Samba tests ********\n"); |
| 3836 | } |
| 3837 | } |
| 3838 | return ret; |
| 3839 | } |
| 3840 | |
| 3841 | |
| 3842 | |
| 3843 | |
| 3844 | QByteArray md5FromIcon(const QIcon& icon) |
| 3845 | { |
| 3846 | QByteArray ret; |
| 3847 | if (!icon.isNull()) |
| 3848 | { |
| 3849 | QPixmap pix = icon.pixmap(QSize(48,48)); |
| 3850 | QImage image = pix.toImage(); |
| 3851 | if (!image.isNull()) |
| 3852 | { |
| 3853 | const uchar *bits = image.bits(); |
| 3854 | const char *bytes = reinterpret_cast<const char*> (bits); |
| 3855 | QByteArray bytesArray(bytes, image.byteCount()); |
| 3856 | ret = QCryptographicHash::hash(bytesArray, QCryptographicHash::Md5); |
| 3857 | } |
| 3858 | } |
| 3859 | return ret; |
| 3860 | } |
| 3861 | |
| 3862 | QString createFileInTempDir(const QString& name, const char *content, qint64 size) |
| 3863 | { |
| 3864 | QString ret; |
| 3865 | QString fullName(QDir::tempPath() + QDir::separator() + name); |
| 3866 | QFile file(fullName); |
| 3867 | if (file.open(QFile::WriteOnly)) |
| 3868 | { |
| 3869 | if (file.write(content, size) == size) |
| 3870 | { |
| 3871 | ret = fullName; |
| 3872 | } |
| 3873 | file.close(); |
| 3874 | } |
| 3875 | return ret; |
| 3876 | } |
| 3877 | |
| 3878 | |
| 3879 | bool TestDirModel::createTempHomeTrashDir(const QString& existentDir) |
| 3880 | { |
| 3881 | QDir d(existentDir); |
| 3882 | bool ret = false; |
| 3883 | if (existentDir.startsWith(QDir::tempPath()) && (d.exists() || d.mkpath(existentDir))) |
| 3884 | { |
| 3885 | QTrashDir trash; |
| 3886 | ::setenv("XDG_DATA_HOME", existentDir.toLocal8Bit().constData(), true ); |
| 3887 | QString xdgTrashDir(trash.homeTrash()); |
| 3888 | ret = trash.validate(xdgTrashDir, true); |
| 3889 | } |
| 3890 | return ret; |
| 3891 | } |
| 3892 | |
| 3893 | #if defined(Q_OS_UNIX) |
| 3894 | /*! |
| 3895 | * \brief updateAndSetModificationTime() |
| 3896 | * updates the file content and sets its last modification time to another time |
| 3897 | * \param filename |
| 3898 | * \param desiredTime some time less than current time |
| 3899 | * \return true if it could be performed, false otherwise |
| 3900 | */ |
| 3901 | bool updateAndSetModificationTime(const QString& filename, QDateTime& desiredTime) |
| 3902 | { |
| 3903 | bool ret = false; |
| 3904 | QFile f(filename); |
| 3905 | if (f.open(QFile::Append)) |
| 3906 | { |
| 3907 | QByteArray data("1234"); |
| 3908 | if (f.write(data) == (qint64)data.size()) |
| 3909 | { |
| 3910 | f.close(); |
| 3911 | struct timeval times[2] = |
| 3912 | { |
| 3913 | {(long)desiredTime.toTime_t(), (long)desiredTime.time().msec()}, |
| 3914 | {(long)desiredTime.toTime_t(), (long)desiredTime.time().msec()} |
| 3915 | }; |
| 3916 | QFileInfo info(filename); |
| 3917 | qDebug() << "last modification of" << info.fileName() << info.lastModified() |
| 3918 | << "forcing it to" << desiredTime; |
| 3919 | if (::utimes( QFile::encodeName(filename).constData(), times) == 0) |
| 3920 | { |
| 3921 | ret = true; |
| 3922 | } |
| 3923 | } |
| 3924 | } |
| 3925 | return ret; |
| 3926 | } |
| 3927 | #endif //Q_OS_UNIX |
| 3928 | |
| 3929 | #include "tst_folderlistmodel.moc" |