testallfilesdeleted.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * This software is in the public domain, furnished "as is", without technical
  3. * support, and with no warranty, express or implied, as to its usefulness for
  4. * any purpose.
  5. *
  6. */
  7. #include <QtTest>
  8. #include "syncenginetestutils.h"
  9. #include <syncengine.h>
  10. using namespace OCC;
  11. /*
  12. * This test ensure that the SyncEngine::aboutToRemoveAllFiles is correctly called and that when
  13. * we the user choose to remove all files SyncJournalDb::clearFileTable makes works as expected
  14. */
  15. class TestAllFilesDeleted : public QObject
  16. {
  17. Q_OBJECT
  18. private slots:
  19. void testAllFilesDeletedKeep_data()
  20. {
  21. QTest::addColumn<bool>("deleteOnRemote");
  22. QTest::newRow("local") << false;
  23. QTest::newRow("remote") << true;
  24. }
  25. /*
  26. * In this test, all files are deleted in the client, or the server, and we simulate
  27. * that the users press "keep"
  28. */
  29. void testAllFilesDeletedKeep()
  30. {
  31. QFETCH(bool, deleteOnRemote);
  32. FakeFolder fakeFolder{FileInfo::A12_B12_C12_S12()};
  33. //Just set a blacklist so we can check it is still there. This directory does not exists but
  34. // that does not matter for our purposes.
  35. QStringList selectiveSyncBlackList = { "Q/" };
  36. fakeFolder.syncEngine().journal()->setSelectiveSyncList(SyncJournalDb::SelectiveSyncBlackList,
  37. selectiveSyncBlackList);
  38. auto initialState = fakeFolder.currentLocalState();
  39. int aboutToRemoveAllFilesCalled = 0;
  40. QObject::connect(&fakeFolder.syncEngine(), &SyncEngine::aboutToRemoveAllFiles,
  41. [&](SyncFileItem::Direction dir, bool *cancel) {
  42. QCOMPARE(aboutToRemoveAllFilesCalled, 0);
  43. aboutToRemoveAllFilesCalled++;
  44. QCOMPARE(dir, deleteOnRemote ? SyncFileItem::Down : SyncFileItem::Up);
  45. *cancel = true;
  46. fakeFolder.syncEngine().journal()->clearFileTable(); // That's what Folder is doing
  47. });
  48. auto &modifier = deleteOnRemote ? fakeFolder.remoteModifier() : fakeFolder.localModifier();
  49. for (const auto &s : fakeFolder.currentRemoteState().children.keys())
  50. modifier.remove(s);
  51. QVERIFY(!fakeFolder.syncOnce()); // Should fail because we cancel the sync
  52. QCOMPARE(aboutToRemoveAllFilesCalled, 1);
  53. // Next sync should recover all files
  54. QVERIFY(fakeFolder.syncOnce());
  55. QCOMPARE(fakeFolder.currentLocalState(), initialState);
  56. QCOMPARE(fakeFolder.currentRemoteState(), initialState);
  57. // The selective sync blacklist should be not have been deleted.
  58. bool ok = true;
  59. QCOMPARE(fakeFolder.syncEngine().journal()->getSelectiveSyncList(SyncJournalDb::SelectiveSyncBlackList, &ok),
  60. selectiveSyncBlackList);
  61. }
  62. void testAllFilesDeletedDelete_data()
  63. {
  64. testAllFilesDeletedKeep_data();
  65. }
  66. /*
  67. * This test is like the previous one but we simulate that the user presses "delete"
  68. */
  69. void testAllFilesDeletedDelete()
  70. {
  71. QFETCH(bool, deleteOnRemote);
  72. FakeFolder fakeFolder{FileInfo::A12_B12_C12_S12()};
  73. int aboutToRemoveAllFilesCalled = 0;
  74. QObject::connect(&fakeFolder.syncEngine(), &SyncEngine::aboutToRemoveAllFiles,
  75. [&](SyncFileItem::Direction dir, bool *cancel) {
  76. QCOMPARE(aboutToRemoveAllFilesCalled, 0);
  77. aboutToRemoveAllFilesCalled++;
  78. QCOMPARE(dir, deleteOnRemote ? SyncFileItem::Down : SyncFileItem::Up);
  79. *cancel = false;
  80. });
  81. auto &modifier = deleteOnRemote ? fakeFolder.remoteModifier() : fakeFolder.localModifier();
  82. for (const auto &s : fakeFolder.currentRemoteState().children.keys())
  83. modifier.remove(s);
  84. QVERIFY(fakeFolder.syncOnce()); // Should succeed, and all files must then be deleted
  85. QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
  86. QCOMPARE(fakeFolder.currentLocalState().children.count(), 0);
  87. // Try another sync to be sure.
  88. QVERIFY(fakeFolder.syncOnce()); // Should succeed (doing nothing)
  89. QCOMPARE(aboutToRemoveAllFilesCalled, 1); // should not have been called.
  90. QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());
  91. QCOMPARE(fakeFolder.currentLocalState().children.count(), 0);
  92. }
  93. };
  94. QTEST_GUILESS_MAIN(TestAllFilesDeleted)
  95. #include "testallfilesdeleted.moc"