diff --git a/tests/testlib/akonadifakedataxmlloader.cpp b/tests/testlib/akonadifakedataxmlloader.cpp --- a/tests/testlib/akonadifakedataxmlloader.cpp +++ b/tests/testlib/akonadifakedataxmlloader.cpp @@ -40,9 +40,9 @@ Akonadi::XmlDocument doc(fileName); Q_ASSERT(doc.isValid()); - Akonadi::Tag::Id tagId = 1; - Akonadi::Collection::Id collectionId = 1; - Akonadi::Item::Id itemId = 1; + Akonadi::Tag::Id tagId = m_data->maxTagId() + 1; + Akonadi::Collection::Id collectionId = m_data->maxCollectionId() + 1; + Akonadi::Item::Id itemId = m_data->maxItemId() + 1; QHash tagByRid; @@ -55,14 +55,32 @@ QHash collectionByRid; - foreach (const Akonadi::Collection &collection, doc.collections()) { + foreach (const Akonadi::Collection &c, doc.collections()) { + collectionByRid[c.remoteId()] = c; + } + + std::function depth + = [&depth, &collectionByRid] (const Akonadi::Collection &c) { + if (c.parentCollection().remoteId().isEmpty()) { + return 0; + } + + auto parent = collectionByRid.value(c.parentCollection().remoteId()); + return depth(parent) + 1; + }; + + auto depthLessThan = [&depth] (const Akonadi::Collection &left, const Akonadi::Collection &right) { + return depth(left) < depth(right); + }; + + auto collectionsByDepth = doc.collections(); + std::sort(collectionsByDepth.begin(), collectionsByDepth.end(), depthLessThan); + + foreach (const Akonadi::Collection &collection, collectionsByDepth) { auto c = collection; c.setId(collectionId++); collectionByRid[c.remoteId()] = c; - } - foreach (const Akonadi::Collection &collection, doc.collections()) { - auto c = collectionByRid.value(collection.remoteId()); c.setParentCollection(collectionByRid.value(c.parentCollection().remoteId())); m_data->createCollection(c); diff --git a/tests/units/testlib/akonadifakedataxmlloadertest.cpp b/tests/units/testlib/akonadifakedataxmlloadertest.cpp --- a/tests/units/testlib/akonadifakedataxmlloadertest.cpp +++ b/tests/units/testlib/akonadifakedataxmlloadertest.cpp @@ -34,6 +34,13 @@ { // GIVEN auto data = Testlib::AkonadiFakeData(); + + // Put a first collection with an idea to make sure it isn't lost on loading + auto searchCollection = Akonadi::Collection(1); + searchCollection.setParentCollection(Akonadi::Collection::root()); + searchCollection.setName("Search"); + data.createCollection(searchCollection); + auto loader = Testlib::AkonadiFakeDataXmlLoader(&data); // WHEN @@ -48,20 +55,26 @@ // the xml loading will be extensively used in the AkonadiFakeStorageTest // and AkonadiFakeData has quite a few asserts. - QCOMPARE(data.childCollections(Akonadi::Collection::root().id()).size(), 1); - const auto res = data.childCollections(Akonadi::Collection::root().id()).at(0); + QCOMPARE(data.childCollections(Akonadi::Collection::root().id()).size(), 2); + const auto res = data.childCollections(Akonadi::Collection::root().id()).at(1); + QCOMPARE(res.id(), qint64(2)); const auto children = data.childCollections(res.id()); QCOMPARE(children.size(), 5); QCOMPARE(children.at(0).name(), QString("Calendar1")); + QCOMPARE(children.at(0).id(), qint64(3)); QCOMPARE(children.at(0).remoteId(), QString("{cdc229c7-a9b5-4d37-989d-a28e372be2a9}")); QCOMPARE(children.at(1).name(), QString("Emails")); + QCOMPARE(children.at(1).id(), qint64(4)); QCOMPARE(children.at(1).remoteId(), QString("{14096930-7bfe-46ca-8fba-7c04d3b62ec8}")); QCOMPARE(children.at(2).name(), QString("Contacts")); + QCOMPARE(children.at(2).id(), qint64(5)); QCOMPARE(children.at(2).remoteId(), QString("{36bf43ac-0988-4435-b602-d6c29e606630}")); QCOMPARE(children.at(3).name(), QString("Destroy me!")); + QCOMPARE(children.at(3).id(), qint64(6)); QCOMPARE(children.at(3).remoteId(), QString("{1f78b360-a01b-4785-9187-75450190342c}")); QCOMPARE(children.at(4).name(), QString("Change me!")); + QCOMPARE(children.at(4).id(), qint64(7)); QCOMPARE(children.at(4).remoteId(), QString("{28ef9f03-4ebc-4e33-970f-f379775894f9}")); } };