diff options
author | aqua <aqua@iserlohn-fortress.net> | 2022-08-14 18:47:37 +0300 |
---|---|---|
committer | aqua <aqua@iserlohn-fortress.net> | 2022-09-14 08:51:09 +0300 |
commit | c2d18d73b0e1a74525ec0cda36a2e2e7e5b4ff4c (patch) | |
tree | d5b9febb29ef801faa08fce4e56c4f453ddb38d0 /src/bookmarks/test | |
parent | Add clazy cmake preset (diff) | |
download | rekonq-c2d18d73b0e1a74525ec0cda36a2e2e7e5b4ff4c.tar.xz |
Import BookmarkModel from poi
Diffstat (limited to 'src/bookmarks/test')
-rw-r--r-- | src/bookmarks/test/xbel-1.0.dtd | 94 | ||||
-rw-r--r-- | src/bookmarks/test/xbel.cpp | 163 |
2 files changed, 257 insertions, 0 deletions
diff --git a/src/bookmarks/test/xbel-1.0.dtd b/src/bookmarks/test/xbel-1.0.dtd new file mode 100644 index 00000000..59013c22 --- /dev/null +++ b/src/bookmarks/test/xbel-1.0.dtd @@ -0,0 +1,94 @@ +<!-- This is the XML Bookmarks Exchange Language, version 1.0. It + should be used with the formal public identifier: + + +//IDN python.org//DTD XML Bookmark Exchange Language 1.0//EN//XML + + One valid system identifier at which this DTD will remain + available is: + + http://pyxml.sourceforge.net/topics/dtds/xbel-1.0.dtd + + More information on the DTD, including reference + documentation, is available at: + + http://www.python.org/topics/xml/xbel/ + + Attributes which take date/time values should encode the value + according to the W3C NOTE on date/time formats: + + http://www.w3.org/TR/NOTE-datetime + --> + + +<!-- Customization entities. Define these before "including" this DTD + to create "subclassed" DTDs. + --> +<!ENTITY % local.node.att ""> +<!ENTITY % local.url.att ""> +<!ENTITY % local.nodes.mix ""> + +<!ENTITY % node.att "id ID #IMPLIED + added CDATA #IMPLIED + %local.node.att;"> + +<!ENTITY % url.att "href CDATA #REQUIRED + visited CDATA #IMPLIED + modified CDATA #IMPLIED + %local.url.att;"> + +<!ENTITY % nodes.mix "bookmark|folder|alias|separator + %local.nodes.mix;"> + + +<!ELEMENT xbel (title?, info?, desc?, (%nodes.mix;)*)> +<!ATTLIST xbel + %node.att; + version CDATA #FIXED "1.0" +> +<!ELEMENT title (#PCDATA)> + +<!--=================== Info ======================================--> + +<!ELEMENT info (metadata+)> + +<!ELEMENT metadata EMPTY> +<!ATTLIST metadata + owner CDATA #REQUIRED +> + +<!--=================== Folder ====================================--> + +<!ELEMENT folder (title?, info?, desc?, (%nodes.mix;)*)> +<!ATTLIST folder + %node.att; + folded (yes|no) 'yes' +> + +<!--=================== Bookmark ==================================--> + +<!ELEMENT bookmark (title?, info?, desc?)> +<!ATTLIST bookmark + %node.att; + %url.att; +> + +<!ELEMENT desc (#PCDATA)> + +<!--=================== Separator =================================--> + +<!ELEMENT separator EMPTY> + +<!--=================== Alias =====================================--> + +<!-- <alias> elements correspond to Netscape bookmark aliases. The + required "ref" attribute must refer to a <bookmark> or <folder> + element. Note that MSIE aliases can refer to folders, so that is + supported in XBEL. Applications must be careful about traversing + aliases to folders to avoid improper recursion through circular + data structures. + --> + +<!ELEMENT alias EMPTY> +<!ATTLIST alias + ref IDREF #REQUIRED +> 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 <QBuffer> +#include <QDateTime> +#include <gtest/gtest.h> + +// 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<BookmarksTreeItem::Attributes>(i)) == + rhs.data(static_cast<BookmarksTreeItem::Attributes>(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 |