// SPDX-License-Identifier: GPL-2.0-or-later /* * Unit tests migrated from cxxtest * * Authors: * Adrian Boguszewski * * Copyright (C) 2018 Authors * * Released under GNU GPL v2+, read the file 'COPYING' for more information. */ #include #include #include #include using namespace Inkscape; using namespace Inkscape::XML; class ObjectTest: public DocPerCaseTest { public: ObjectTest() { // Sample document // svg:svg // svg:defs // svg:path // svg:linearGradient // svg:stop // svg:filter // svg:feGaussianBlur (feel free to implement for other filters) // svg:clipPath // svg:rect // svg:g // svg:use // svg:circle // svg:ellipse // svg:text // svg:polygon // svg:polyline // svg:image // svg:line char const *docString = R"A( SVG test TEST )A"; doc.reset(SPDocument::createNewDocFromMem(docString, static_cast(strlen(docString)), false)); } ~ObjectTest() override = default; std::unique_ptr doc; }; TEST_F(ObjectTest, Clones) { ASSERT_TRUE(doc != nullptr); ASSERT_TRUE(doc->getRoot() != nullptr); SPRoot *root = doc->getRoot(); ASSERT_TRUE(root->getRepr() != nullptr); ASSERT_TRUE(root->hasChildren()); SPPath *path = dynamic_cast(doc->getObjectById("P")); ASSERT_TRUE(path != nullptr); Node *node = path->getRepr(); ASSERT_TRUE(node != nullptr); Document *xml_doc = node->document(); ASSERT_TRUE(xml_doc != nullptr); Node *parent = node->parent(); ASSERT_TRUE(parent != nullptr); const size_t num_clones = 1000; std::string href = std::string("#") + std::string(path->getId()); std::vector clones(num_clones, nullptr); // Create num_clones clones of this path and stick them in the document for (size_t i = 0; i < num_clones; ++i) { Node *clone = xml_doc->createElement("svg:use"); Inkscape::GC::release(clone); clone->setAttribute("xlink:href", href); parent->addChild(clone, node); clones[i] = clone; } // Remove those clones for (size_t i = 0; i < num_clones; ++i) { parent->removeChild(clones[i]); } } TEST_F(ObjectTest, Grouping) { ASSERT_TRUE(doc != nullptr); ASSERT_TRUE(doc->getRoot() != nullptr); SPRoot *root = doc->getRoot(); ASSERT_TRUE(root->getRepr() != nullptr); ASSERT_TRUE(root->hasChildren()); SPGroup *group = dynamic_cast(doc->getObjectById("G")); ASSERT_TRUE(group != nullptr); Node *node = group->getRepr(); ASSERT_TRUE(node != nullptr); Document *xml_doc = node->document(); ASSERT_TRUE(xml_doc != nullptr); const size_t num_elements = 1000; Node *new_group = xml_doc->createElement("svg:g"); Inkscape::GC::release(new_group); node->addChild(new_group, nullptr); std::vector elements(num_elements, nullptr); for (size_t i = 0; i < num_elements; ++i) { Node *circle = xml_doc->createElement("svg:circle"); Inkscape::GC::release(circle); circle->setAttribute("cx", "2048"); circle->setAttribute("cy", "1024"); circle->setAttribute("r", "1.5"); new_group->addChild(circle, nullptr); elements[i] = circle; } SPGroup *n_group = dynamic_cast(group->get_child_by_repr(new_group)); ASSERT_TRUE(n_group != nullptr); std::vector ch; sp_item_group_ungroup(n_group, ch, false); // Remove those elements for (size_t i = 0; i < num_elements; ++i) { elements[i]->parent()->removeChild(elements[i]); } } TEST_F(ObjectTest, Objects) { ASSERT_TRUE(doc != nullptr); ASSERT_TRUE(doc->getRoot() != nullptr); SPRoot *root = doc->getRoot(); ASSERT_TRUE(root->getRepr() != nullptr); ASSERT_TRUE(root->hasChildren()); SPPath *path = dynamic_cast(doc->getObjectById("P")); ASSERT_TRUE(path != nullptr); // Test parent behavior SPObject *child = root->firstChild(); ASSERT_TRUE(child != nullptr); EXPECT_EQ(root, child->parent); EXPECT_EQ(doc.get(), child->document); EXPECT_TRUE(root->isAncestorOf(child)); // Test list behavior SPObject *next = child->getNext(); SPObject *prev = next; EXPECT_EQ(child, next->getPrev()); prev = next; next = next->getNext(); while (next != nullptr) { // Walk the list EXPECT_EQ(prev, next->getPrev()); prev = next; next = next->getNext(); } // Test hrefcount EXPECT_TRUE(path->isReferenced()); }