From c2d18d73b0e1a74525ec0cda36a2e2e7e5b4ff4c Mon Sep 17 00:00:00 2001 From: aqua Date: Sun, 14 Aug 2022 18:47:37 +0300 Subject: Import BookmarkModel from poi --- src/bookmarks/test/xbel.cpp | 163 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 src/bookmarks/test/xbel.cpp (limited to 'src/bookmarks/test/xbel.cpp') diff --git a/src/bookmarks/test/xbel.cpp b/src/bookmarks/test/xbel.cpp new file mode 100644 index 00000000..a836dd9a --- /dev/null +++ b/src/bookmarks/test/xbel.cpp @@ -0,0 +1,163 @@ +#include "bookmarks/bookmarkstreeformats.hpp" +#include "bookmarks/bookmarkstreeitem.hpp" +#include +#include +#include + +// clazy:excludeall=non-pod-global-static + +constexpr auto *title = "Test Title"; +constexpr auto *desc = "Test Description"; + +constexpr auto *id = "id"; +constexpr auto *added = "1970-01-01T12:00:00Z"; + +constexpr auto *href = "https://some.example.com"; +constexpr auto *visited = "1970-01-01T12:00:00Z"; +constexpr auto *modified = "1970-01-01T12:00:00Z"; + +auto *newBookmarksTreeItem() +{ + // Attributes: Title, Href, Description, Id, Added, Visited, Modified + auto *item = new BookmarksTreeItem(BookmarksTreeItem::Bookmark, + {title, QUrl::fromUserInput(href), QDateTime::fromString(added, Qt::ISODate), + QDateTime::fromString(visited, Qt::ISODate), + QDateTime::fromString(modified, Qt::ISODate), desc, id}, + nullptr); + item->metadata = QStringList{"alpha", "beta", "gamma"}; + return item; +} + +bool operator==(const BookmarksTreeItem &lhs, const BookmarksTreeItem &rhs) +{ + EXPECT_EQ(lhs.type(), rhs.type()); + EXPECT_EQ(lhs.isExpanded(), rhs.isExpanded()); + + for (int i = 0; i < BookmarksTreeItem::AttributeCount; ++i) + EXPECT_TRUE(lhs.data(static_cast(i)) == + rhs.data(static_cast(i))); + + EXPECT_EQ(lhs.metadata, rhs.metadata); + EXPECT_EQ(lhs.childCount(), rhs.childCount()); + for (int i = 0; i < lhs.childCount(); ++i) EXPECT_EQ(*lhs.child(i), *rhs.child(i)); + + return true; +} + +[[nodiscard]] bool encode(const BookmarksTreeItem *root, QByteArray &data) +{ + QBuffer buffer(&data); + if (!buffer.open(QIODevice::WriteOnly | QIODevice::Text)) return false; + return xbel::write(&buffer, {root}); +} + +[[nodiscard]] bool decode(BookmarksTreeItem *root, QByteArray &data) +{ + QBuffer buffer(&data); + if (!buffer.open(QIODevice::ReadOnly | QIODevice::Text)) return false; + return xbel::read(&buffer, root); +} + +namespace { +TEST(XbelFormat, RootEncodeDecode) +{ + auto *original = new BookmarksTreeItem(BookmarksTreeItem::Root, {}, nullptr); + original->metadata = QStringList{"one", "two", "three"}; // info + original->setData(BookmarksTreeItem::Description, "xbel root item description"); // desc + original->setData(BookmarksTreeItem::Id, "root"); // node.att.id + original->setData(BookmarksTreeItem::Added, QDateTime::fromString(QDateTime::currentDateTime().toString(Qt::ISODate), + Qt::ISODate)); // node.att.added + + QByteArray encdodedData; + EXPECT_TRUE(encode(original, encdodedData)); + + auto *root = new BookmarksTreeItem(BookmarksTreeItem::Root, {}, nullptr); + EXPECT_TRUE(decode(root, encdodedData)); + + EXPECT_EQ(*original, *root); + + // cleanuo + delete original; + delete root; +} + +TEST(XbelFormat, FolderEncodeDecode) +{ + QByteArray encodedData; + + auto *folder = new BookmarksTreeItem(BookmarksTreeItem::Folder, {.title = "Folder"}, nullptr); + folder->appendChild(newBookmarksTreeItem()); + folder->appendChild(newBookmarksTreeItem()); + EXPECT_TRUE(encode(folder, encodedData)); + + auto *root = new BookmarksTreeItem(BookmarksTreeItem::Root, {}, nullptr); + EXPECT_TRUE(decode(root, encodedData)); + + EXPECT_EQ(root->childCount(), 1); + auto *item = root->child(0); + EXPECT_TRUE(*folder == *item); + + // cleanup + delete folder; + delete root; +} + +TEST(XbelFormat, BookmarkEncodeDecode) +{ + QByteArray encodedData; + + auto *original = newBookmarksTreeItem(); + EXPECT_TRUE(encode(original, encodedData)); + + auto *root = new BookmarksTreeItem(BookmarksTreeItem::Root, {}, nullptr); + EXPECT_TRUE(decode(root, encodedData)); + + EXPECT_EQ(root->childCount(), 1); + auto *item = root->child(0); + EXPECT_EQ(*original, *item); + + // cleanup + delete original; + delete root; +} + +TEST(XbelFormat, SeparatorEncodeDecode) +{ + QByteArray encodedData; + + auto *original = new BookmarksTreeItem(BookmarksTreeItem::Separator, {}, nullptr); + EXPECT_TRUE(encode(original, encodedData)); + + auto *root = new BookmarksTreeItem(BookmarksTreeItem::Root, {}, nullptr); + EXPECT_TRUE(decode(root, encodedData)); + + EXPECT_EQ(root->childCount(), 1); + auto *item = root->child(0); + EXPECT_EQ(*original, *item); + + // cleanup + delete original; + delete root; +} + +TEST(XbelFormat, AliasEncodeDecode) +{ + QByteArray encodedData; + + auto *original = new BookmarksTreeItem(BookmarksTreeItem::Alias, {}, nullptr); + original->setData(BookmarksTreeItem::Id, "aliased_id"); + EXPECT_TRUE(encode(original, encodedData)); + + auto *root = new BookmarksTreeItem(BookmarksTreeItem::Root, {}, nullptr); + EXPECT_TRUE(decode(root, encodedData)); + + EXPECT_EQ(root->childCount(), 1); + auto *item = root->child(0); + EXPECT_EQ(*original, *item); + + // cleanup + delete original; + delete root; +} + +} // namespace -- cgit v1.2.1