Skip to content

Commit 0416656

Browse files
committed
chore: add test for syncing specific folders on fileid notification
Signed-off-by: Jyrki Gadinger <nilsding@nilsding.org>
1 parent 511900b commit 0416656

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

test/testfolderman.cpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "syncenginetestutils.h"
2323
#include "testhelper.h"
2424

25+
using namespace Qt::StringLiterals;
2526
using namespace OCC;
2627

2728
static QByteArray fake400Response = R"(
@@ -450,6 +451,105 @@ private slots:
450451
QCOMPARE(folderman->findGoodPathForNewSyncFolder(dirPath + "/ownCloud2", url, FolderMan::GoodPathStrategy::AllowOnlyNewPath),
451452
QString(dirPath + "/ownCloud22"));
452453
}
454+
455+
void testProcessingFileIdsPushNotification()
456+
{
457+
_fm.reset({});
458+
_fm.reset(new FolderMan{});
459+
460+
QTemporaryDir tempDir;
461+
ConfigFile::setConfDir(tempDir.path()); // we don't want to pollute the user's config file
462+
QVERIFY(tempDir.isValid());
463+
QDir dir(tempDir.path());
464+
QVERIFY(dir.mkpath("user1_root"));
465+
QVERIFY(dir.mkpath("user1_subfolder"));
466+
QVERIFY(dir.mkpath("user2_root"));
467+
const auto dirPath = dir.canonicalPath();
468+
469+
const auto createAccount = [](const QString &userName) -> AccountState * {
470+
auto account = Account::create();
471+
auto credentials = new FakeCredentials{new FakeQNAM({})};
472+
credentials->setUserName(userName);
473+
account->setCredentials(credentials);
474+
account->setUrl(QUrl{"http://nextcloud.test"});
475+
return new FakeAccountState(account);
476+
};
477+
478+
auto user1 = createAccount("user1");
479+
auto user2 = createAccount("user2");
480+
481+
const auto addFolderForTesting = [this, &dirPath](AccountState * const account, const QString &alias, const QString &localPath, const QString &targetPath, const int rootFileId, const QList<qint64> &fileIds) -> void {
482+
FolderDefinition definition;
483+
definition.alias = alias;
484+
definition.localPath = dirPath + localPath;
485+
definition.targetPath = targetPath;
486+
487+
auto folder = _fm->addFolder(account, definition);
488+
QVERIFY(folder);
489+
490+
Q_EMIT folder->syncEngine().rootFileIdReceived(rootFileId);
491+
492+
auto journal = folder->journalDb();
493+
for (const auto &fileId : fileIds) {
494+
SyncJournalFileRecord record;
495+
record._fileId = u"%1oc123xyz987e"_s.arg(fileId, 8, 10, '0'_L1).toLocal8Bit();
496+
record._modtime = QDateTime::currentSecsSinceEpoch();
497+
record._path = u"item%1"_s.arg(fileId).toLocal8Bit();
498+
record._type = ItemTypeFile;
499+
record._etag = "etag"_ba;
500+
QVERIFY(journal->setFileRecord(record));
501+
}
502+
};
503+
504+
const auto verifyFolderSyncChangesOnReceivedFileIdNotification = [this](AccountState * const user, const QList<qint64> &fileIds, const QStringList &expectedFolderAliasesToSync) -> void {
505+
QStringList folderAliasesToBeSynced = {};
506+
507+
_fm->_scheduledFolders.clear();
508+
QSignalSpy spy(_fm.get(), &FolderMan::folderSyncStateChange);
509+
510+
// the account received a push notification about for specific file ids
511+
_fm->slotProcessFileIdsPushNotification(user->account().get(), fileIds);
512+
513+
// expect the sync state for all folders of that account containing this file id to change
514+
QCOMPARE(spy.size(), expectedFolderAliasesToSync.size());
515+
516+
for (const auto &signalParameters : std::as_const(spy)) {
517+
QVERIFY(signalParameters.size() == 1);
518+
const auto folderAlias = signalParameters.front().value<Folder *>()->alias();
519+
QVERIFY2(
520+
expectedFolderAliasesToSync.contains(folderAlias),
521+
qPrintable("Unexpected folder alias '%1'; expected were [%2]"_L1.arg(folderAlias, expectedFolderAliasesToSync.join(", ")))
522+
);
523+
folderAliasesToBeSynced.append(folderAlias);
524+
}
525+
526+
// all expected folders received a sync request!
527+
folderAliasesToBeSynced.sort();
528+
QCOMPARE(folderAliasesToBeSynced, expectedFolderAliasesToSync);
529+
};
530+
531+
addFolderForTesting(user1, "0", "/user1_root", "/", 10, {11, 12, 13, 50});
532+
addFolderForTesting(user1, "1", "/user1_subfolder", "/subfolder", 15, {16, 17, 18, 50});
533+
addFolderForTesting(user2, "2", "/user2_root", "/", 20, {21, 22, 23, 50});
534+
535+
verifyFolderSyncChangesOnReceivedFileIdNotification(user1, {10}, {"0"});
536+
verifyFolderSyncChangesOnReceivedFileIdNotification(user1, {11}, {"0"});
537+
verifyFolderSyncChangesOnReceivedFileIdNotification(user1, {13, 11}, {"0"});
538+
verifyFolderSyncChangesOnReceivedFileIdNotification(user1, {15}, {"1"});
539+
verifyFolderSyncChangesOnReceivedFileIdNotification(user1, {16}, {"1"});
540+
verifyFolderSyncChangesOnReceivedFileIdNotification(user1, {18, 16}, {"1"});
541+
verifyFolderSyncChangesOnReceivedFileIdNotification(user1, {15, 11}, {"0", "1"});
542+
verifyFolderSyncChangesOnReceivedFileIdNotification(user1, {11, 16, 21}, {"0", "1"});
543+
verifyFolderSyncChangesOnReceivedFileIdNotification(user1, {50}, {"0", "1"});
544+
verifyFolderSyncChangesOnReceivedFileIdNotification(user1, {20, 21, 22, 23, 404}, {});
545+
546+
verifyFolderSyncChangesOnReceivedFileIdNotification(user2, {20}, {"2"});
547+
verifyFolderSyncChangesOnReceivedFileIdNotification(user2, {21}, {"2"});
548+
verifyFolderSyncChangesOnReceivedFileIdNotification(user2, {23, 21}, {"2"});
549+
verifyFolderSyncChangesOnReceivedFileIdNotification(user2, {11, 16, 21}, {"2"});
550+
verifyFolderSyncChangesOnReceivedFileIdNotification(user2, {50}, {"2"});
551+
verifyFolderSyncChangesOnReceivedFileIdNotification(user2, {10, 11, 17, 18, 404}, {});
552+
}
453553
};
454554

455555
QTEST_GUILESS_MAIN(TestFolderMan)

0 commit comments

Comments
 (0)