From c853ffb5b2f75f5a889ed2e3ef89b818a736e87a Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 13 Apr 2024 13:50:49 +0200 Subject: Adding upstream version 1.3+ds. Signed-off-by: Daniel Baumann --- testfiles/src/rebase-hrefs-test.cpp | 135 ++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 testfiles/src/rebase-hrefs-test.cpp (limited to 'testfiles/src/rebase-hrefs-test.cpp') diff --git a/testfiles/src/rebase-hrefs-test.cpp b/testfiles/src/rebase-hrefs-test.cpp new file mode 100644 index 0000000..e1c91fb --- /dev/null +++ b/testfiles/src/rebase-hrefs-test.cpp @@ -0,0 +1,135 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/** + * @file + * Test rebasing URI attributes + */ +/* + * Authors: + * Thomas Holder + * + * Copyright (C) 2020 Authors + * + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ + +#include "xml/rebase-hrefs.h" + +#include +#include + +#include "object/sp-object.h" + +using namespace Inkscape::XML; + +#ifdef _WIN32 +#define BASE_DIR_DIFFERENT_ROOT "D:\\foo\\bar" +#define BASE_DIR "C:\\foo\\bar" +#define BASE_URL "file:///C:/foo/bar" +#else +#define BASE_DIR_DIFFERENT_ROOT "/different/root" +#define BASE_DIR "/foo/bar" +#define BASE_URL "file://" BASE_DIR +#endif + +static char const *const docString = R"""( + + + + + + + + + + + + + + + + + + + + +)"""; + +class ObjectTest : public DocPerCaseTest +{ +public: + std::unique_ptr doc; + + ObjectTest() { doc.reset(SPDocument::createNewDocFromMem(docString, strlen(docString), false)); } + + void assert_nonfile_unchanged() const + { + ASSERT_STREQ(doc->getObjectById("img03")->getAttribute("xlink:href"), "http://host/a.png"); + ASSERT_STREQ(doc->getObjectById("img04")->getAttribute("xlink:href"), "data:text/plain,xxx"); + + ASSERT_STREQ(doc->getObjectById("img05")->getAttribute("xlink:href"), ""); + ASSERT_STREQ(doc->getObjectById("img06")->getAttribute("xlink:href"), "#fragment"); + ASSERT_STREQ(doc->getObjectById("img07")->getAttribute("xlink:href"), "?query"); + ASSERT_STREQ(doc->getObjectById("img08")->getAttribute("xlink:href"), "/absolute/path"); + ASSERT_STREQ(doc->getObjectById("img09")->getAttribute("xlink:href"), "//network/path"); + } +}; + +TEST_F(ObjectTest, RebaseHrefs) +{ + rebase_hrefs(doc.get(), BASE_DIR G_DIR_SEPARATOR_S "c", false); + assert_nonfile_unchanged(); + ASSERT_STREQ(doc->getObjectById("img01")->getAttribute("xlink:href"), "../a.png"); + ASSERT_STREQ(doc->getObjectById("img02")->getAttribute("xlink:href"), "b/a.png"); + + // no base + rebase_hrefs(doc.get(), nullptr, false); + assert_nonfile_unchanged(); + ASSERT_STREQ(doc->getObjectById("img01")->getAttribute("xlink:href"), BASE_URL "/a.png"); + ASSERT_STREQ(doc->getObjectById("img02")->getAttribute("xlink:href"), BASE_URL "/c/b/a.png"); + + rebase_hrefs(doc.get(), BASE_DIR, false); + assert_nonfile_unchanged(); + ASSERT_STREQ(doc->getObjectById("img01")->getAttribute("xlink:href"), "a.png"); + ASSERT_STREQ(doc->getObjectById("img02")->getAttribute("xlink:href"), "c/b/a.png"); + + // base with different root + rebase_hrefs(doc.get(), BASE_DIR_DIFFERENT_ROOT, false); + assert_nonfile_unchanged(); + ASSERT_STREQ(doc->getObjectById("img01")->getAttribute("xlink:href"), BASE_URL "/a.png"); + ASSERT_STREQ(doc->getObjectById("img02")->getAttribute("xlink:href"), BASE_URL "/c/b/a.png"); +} + +static std::map rebase_attrs_test_helper(SPDocument *doc, char const *id, + char const *old_base, char const *new_base) +{ + std::map attributemap; + auto attributes = rebase_href_attrs(old_base, new_base, doc->getObjectById(id)->getRepr()->attributeList()); + for (const auto &item : attributes) { + attributemap[g_quark_to_string(item.key)] = item.value.pointer(); + } + return attributemap; +} + +TEST_F(ObjectTest, RebaseHrefAttrs) +{ + std::map amap; + + amap = rebase_attrs_test_helper(doc.get(), "img01", BASE_DIR, BASE_DIR G_DIR_SEPARATOR_S "c"); + ASSERT_STREQ(amap["xlink:href"].c_str(), "../a.png"); + amap = rebase_attrs_test_helper(doc.get(), "img02", BASE_DIR, BASE_DIR G_DIR_SEPARATOR_S "c"); + ASSERT_STREQ(amap["xlink:href"].c_str(), "b/a.png"); + amap = rebase_attrs_test_helper(doc.get(), "img06", BASE_DIR, BASE_DIR G_DIR_SEPARATOR_S "c"); + ASSERT_STREQ(amap["xlink:href"].c_str(), "#fragment"); + amap = rebase_attrs_test_helper(doc.get(), "img10", BASE_DIR, BASE_DIR G_DIR_SEPARATOR_S "c"); + ASSERT_STREQ(amap["xlink:href"].c_str(), "../b/a.png"); + + amap = rebase_attrs_test_helper(doc.get(), "a01", BASE_DIR, BASE_DIR G_DIR_SEPARATOR_S "c"); + ASSERT_STREQ(amap["xlink:href"].c_str(), "../other.svg"); + amap = rebase_attrs_test_helper(doc.get(), "a02", BASE_DIR, BASE_DIR G_DIR_SEPARATOR_S "c"); + ASSERT_STREQ(amap["xlink:href"].c_str(), "http://host/other.svg"); + + amap = rebase_attrs_test_helper(doc.get(), "img01", BASE_DIR, BASE_DIR_DIFFERENT_ROOT); + ASSERT_STREQ(amap["xlink:href"].c_str(), BASE_URL "/a.png"); +} + +// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 : -- cgit v1.2.3