summaryrefslogtreecommitdiffstats
path: root/svl/qa/unit/items
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--svl/qa/unit/items/stylepool.cxx82
-rw-r--r--svl/qa/unit/items/test_IndexedStyleSheets.cxx210
-rw-r--r--svl/qa/unit/items/test_itempool.cxx99
3 files changed, 391 insertions, 0 deletions
diff --git a/svl/qa/unit/items/stylepool.cxx b/svl/qa/unit/items/stylepool.cxx
new file mode 100644
index 000000000..94ff91aea
--- /dev/null
+++ b/svl/qa/unit/items/stylepool.cxx
@@ -0,0 +1,82 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#include <unotest/bootstrapfixturebase.hxx>
+
+#include <svl/itempool.hxx>
+#include <svl/itemset.hxx>
+#include <svl/stylepool.hxx>
+#include <svl/stritem.hxx>
+
+namespace
+{
+/// Tests svl StylePool.
+class StylePoolTest : public CppUnit::TestFixture
+{
+};
+
+CPPUNIT_TEST_FIXTURE(StylePoolTest, testIterationOrder)
+{
+ // Set up a style pool with multiple parents.
+ SfxStringItem aDefault1(1);
+ std::vector<SfxPoolItem*> aDefaults{ &aDefault1 };
+ SfxItemInfo const aItems[] = { { 2, false } };
+
+ rtl::Reference<SfxItemPool> pPool = new SfxItemPool("test", 1, 1, aItems);
+ pPool->SetDefaults(&aDefaults);
+ {
+ // Set up parents in mixed order to make sure we do not sort by pointer address.
+ SfxItemSet aParent1(*pPool, svl::Items<1, 1>);
+ SfxItemSet aChild1(*pPool, svl::Items<1, 1>);
+ aChild1.SetParent(&aParent1);
+ SfxStringItem aItem1(1, "Item1");
+ aChild1.Put(aItem1);
+
+ SfxItemSet aParent3(*pPool, svl::Items<1, 1>);
+ SfxItemSet aChild3(*pPool, svl::Items<1, 1>);
+ aChild3.SetParent(&aParent3);
+ SfxStringItem aItem3(1, "Item3");
+ aChild3.Put(aItem3);
+
+ SfxItemSet aParent2(*pPool, svl::Items<1, 1>);
+ SfxItemSet aChild2(*pPool, svl::Items<1, 1>);
+ aChild2.SetParent(&aParent2);
+ SfxStringItem aItem2(1, "Item2");
+ aChild2.Put(aItem2);
+
+ // Insert item sets in alphabetical order.
+ StylePool aStylePool;
+ OUString aChild1Name("Child1");
+ aStylePool.insertItemSet(aChild1, &aChild1Name);
+ OUString aChild3Name("Child3");
+ aStylePool.insertItemSet(aChild3, &aChild3Name);
+ OUString aChild2Name("Child2");
+ aStylePool.insertItemSet(aChild2, &aChild2Name);
+ std::unique_ptr<IStylePoolIteratorAccess> pIter = aStylePool.createIterator();
+ std::shared_ptr<SfxItemSet> pStyle1 = pIter->getNext();
+ CPPUNIT_ASSERT(pStyle1);
+ const SfxStringItem* pItem1 = static_cast<const SfxStringItem*>(pStyle1->GetItem(1));
+ CPPUNIT_ASSERT_EQUAL(OUString("Item1"), pItem1->GetValue());
+ std::shared_ptr<SfxItemSet> pStyle2 = pIter->getNext();
+ CPPUNIT_ASSERT(pStyle2);
+ const SfxStringItem* pItem2 = static_cast<const SfxStringItem*>(pStyle2->GetItem(1));
+ // Without the accompanying fix in place, this test would have failed with 'Expected: Item2;
+ // Actual: Item3'. The iteration order depended on the pointer address on the pointer
+ // address of the parents.
+ CPPUNIT_ASSERT_EQUAL(OUString("Item2"), pItem2->GetValue());
+ std::shared_ptr<SfxItemSet> pStyle3 = pIter->getNext();
+ CPPUNIT_ASSERT(pStyle3);
+ const SfxStringItem* pItem3 = static_cast<const SfxStringItem*>(pStyle3->GetItem(1));
+ CPPUNIT_ASSERT_EQUAL(OUString("Item3"), pItem3->GetValue());
+ CPPUNIT_ASSERT(!pIter->getNext());
+ }
+}
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/svl/qa/unit/items/test_IndexedStyleSheets.cxx b/svl/qa/unit/items/test_IndexedStyleSheets.cxx
new file mode 100644
index 000000000..5c12acc25
--- /dev/null
+++ b/svl/qa/unit/items/test_IndexedStyleSheets.cxx
@@ -0,0 +1,210 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#include <svl/IndexedStyleSheets.hxx>
+
+#include <svl/style.hxx>
+
+#include <cppunit/TestAssert.h>
+#include <cppunit/TestFixture.h>
+#include <cppunit/extensions/HelperMacros.h>
+#include <cppunit/plugin/TestPlugIn.h>
+
+#include <algorithm>
+
+using namespace svl;
+
+namespace {
+
+class MockedStyleSheet : public SfxStyleSheetBase
+{
+ public:
+ MockedStyleSheet(const OUString& name, SfxStyleFamily fam = SfxStyleFamily::Char)
+ : SfxStyleSheetBase(name, nullptr, fam, SfxStyleSearchBits::Auto)
+ {}
+
+};
+
+struct DummyPredicate : public StyleSheetPredicate {
+ bool Check(const SfxStyleSheetBase&) override {
+ return true;
+ }
+};
+
+}
+
+class IndexedStyleSheetsTest : public CppUnit::TestFixture
+{
+ void InstantiationWorks();
+ void AddedStylesheetsCanBeFoundAndRetrievedByPosition();
+ void AddingSameStylesheetTwiceHasNoEffect();
+ void RemovedStyleSheetIsNotFound();
+ void RemovingStyleSheetWhichIsNotAvailableHasNoEffect();
+ void StyleSheetsCanBeRetrievedByTheirName();
+ void KnowsThatItStoresAStyleSheet();
+ void PositionCanBeQueriedByFamily();
+ void OnlyOneStyleSheetIsReturnedWhenReturnFirstIsUsed();
+
+ // Adds code needed to register the test suite
+ CPPUNIT_TEST_SUITE(IndexedStyleSheetsTest);
+
+ CPPUNIT_TEST(InstantiationWorks);
+ CPPUNIT_TEST(AddedStylesheetsCanBeFoundAndRetrievedByPosition);
+ CPPUNIT_TEST(AddingSameStylesheetTwiceHasNoEffect);
+ CPPUNIT_TEST(RemovedStyleSheetIsNotFound);
+ CPPUNIT_TEST(RemovingStyleSheetWhichIsNotAvailableHasNoEffect);
+ CPPUNIT_TEST(StyleSheetsCanBeRetrievedByTheirName);
+ CPPUNIT_TEST(KnowsThatItStoresAStyleSheet);
+ CPPUNIT_TEST(PositionCanBeQueriedByFamily);
+ CPPUNIT_TEST(OnlyOneStyleSheetIsReturnedWhenReturnFirstIsUsed);
+
+ // End of test suite definition
+ CPPUNIT_TEST_SUITE_END();
+
+};
+
+void IndexedStyleSheetsTest::InstantiationWorks()
+{
+ IndexedStyleSheets iss;
+}
+
+void IndexedStyleSheetsTest::AddedStylesheetsCanBeFoundAndRetrievedByPosition()
+{
+ rtl::Reference<SfxStyleSheetBase> sheet1(new MockedStyleSheet("name1"));
+ rtl::Reference<SfxStyleSheetBase> sheet2(new MockedStyleSheet("name2"));
+ IndexedStyleSheets iss;
+ iss.AddStyleSheet(sheet1);
+ iss.AddStyleSheet(sheet2);
+ unsigned pos = iss.FindStyleSheetPosition(*sheet2);
+ rtl::Reference<SfxStyleSheetBase> retrieved = iss.GetStyleSheetByPosition(pos);
+ CPPUNIT_ASSERT_EQUAL_MESSAGE("retrieved sheet is that which has been inserted.", sheet2.get(), retrieved.get());
+}
+
+void IndexedStyleSheetsTest::AddingSameStylesheetTwiceHasNoEffect()
+{
+ rtl::Reference<SfxStyleSheetBase> sheet1(new MockedStyleSheet("sheet1"));
+ IndexedStyleSheets iss;
+ iss.AddStyleSheet(sheet1);
+ CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), iss.GetNumberOfStyleSheets());
+ iss.AddStyleSheet(sheet1);
+ CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), iss.GetNumberOfStyleSheets());
+}
+
+void IndexedStyleSheetsTest::RemovedStyleSheetIsNotFound()
+{
+ rtl::Reference<SfxStyleSheetBase> sheet1(new MockedStyleSheet("name1"));
+ rtl::Reference<SfxStyleSheetBase> sheet2(new MockedStyleSheet("name2"));
+ IndexedStyleSheets iss;
+ iss.AddStyleSheet(sheet1);
+ iss.AddStyleSheet(sheet2);
+ iss.RemoveStyleSheet(sheet1);
+ CPPUNIT_ASSERT_EQUAL_MESSAGE("Removed style sheet is not found.",
+ false, iss.HasStyleSheet(sheet1));
+}
+
+void IndexedStyleSheetsTest::RemovingStyleSheetWhichIsNotAvailableHasNoEffect()
+{
+ rtl::Reference<SfxStyleSheetBase> sheet1(new MockedStyleSheet("sheet1"));
+ rtl::Reference<SfxStyleSheetBase> sheet2(new MockedStyleSheet("sheet2"));
+ IndexedStyleSheets iss;
+ iss.AddStyleSheet(sheet1);
+ CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), iss.GetNumberOfStyleSheets());
+ iss.RemoveStyleSheet(sheet2);
+ CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), iss.GetNumberOfStyleSheets());
+}
+
+void IndexedStyleSheetsTest::StyleSheetsCanBeRetrievedByTheirName()
+{
+ OUString name1("name1");
+ OUString name2("name2");
+ rtl::Reference<SfxStyleSheetBase> sheet1(new MockedStyleSheet(name1));
+ rtl::Reference<SfxStyleSheetBase> sheet2(new MockedStyleSheet(name2));
+ rtl::Reference<SfxStyleSheetBase> sheet3(new MockedStyleSheet(name1));
+ IndexedStyleSheets iss;
+ iss.AddStyleSheet(sheet1);
+ iss.AddStyleSheet(sheet2);
+ iss.AddStyleSheet(sheet3);
+
+ std::vector<sal_Int32> r = iss.FindPositionsByName(name1);
+ CPPUNIT_ASSERT_EQUAL_MESSAGE("Two style sheets are found by 'name1'",
+ 2u, static_cast<unsigned>(r.size()));
+ std::sort (r.begin(), r.end());
+ CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), r.at(0));
+ CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(2), r.at(1));
+
+ r = iss.FindPositionsByName(name2);
+ CPPUNIT_ASSERT_EQUAL_MESSAGE("One style sheets is found by 'name2'",
+ 1u, static_cast<unsigned>(r.size()));
+ CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), r.at(0));
+}
+
+void IndexedStyleSheetsTest::KnowsThatItStoresAStyleSheet()
+{
+ OUString const name1("name1");
+ rtl::Reference<SfxStyleSheetBase> sheet1(new MockedStyleSheet(name1));
+ rtl::Reference<SfxStyleSheetBase> sheet2(new MockedStyleSheet(name1));
+ rtl::Reference<SfxStyleSheetBase> sheet3(new MockedStyleSheet("name2"));
+ rtl::Reference<SfxStyleSheetBase> sheet4(new MockedStyleSheet(name1));
+ IndexedStyleSheets iss;
+ iss.AddStyleSheet(sheet1);
+ iss.AddStyleSheet(sheet2);
+ iss.AddStyleSheet(sheet3);
+ // do not add sheet 4
+
+ CPPUNIT_ASSERT_EQUAL_MESSAGE("Finds first stored style sheet even though two style sheets have the same name.",
+ true, iss.HasStyleSheet(sheet1));
+ CPPUNIT_ASSERT_EQUAL_MESSAGE("Finds second stored style sheet even though two style sheets have the same name.",
+ true, iss.HasStyleSheet(sheet2));
+ CPPUNIT_ASSERT_EQUAL_MESSAGE("Does not find style sheet which is not stored and has the same name as a stored.",
+ false, iss.HasStyleSheet(sheet4));
+}
+
+void IndexedStyleSheetsTest::PositionCanBeQueriedByFamily()
+{
+ rtl::Reference<SfxStyleSheetBase> sheet1(new MockedStyleSheet("name1", SfxStyleFamily::Char));
+ rtl::Reference<SfxStyleSheetBase> sheet2(new MockedStyleSheet("name2", SfxStyleFamily::Para));
+ rtl::Reference<SfxStyleSheetBase> sheet3(new MockedStyleSheet("name3", SfxStyleFamily::Char));
+
+ IndexedStyleSheets iss;
+ iss.AddStyleSheet(sheet1);
+ iss.AddStyleSheet(sheet2);
+ iss.AddStyleSheet(sheet3);
+
+ const std::vector<sal_Int32>& v = iss.GetStyleSheetPositionsByFamily(SfxStyleFamily::Char);
+ CPPUNIT_ASSERT_EQUAL_MESSAGE("Separation by family works.", static_cast<size_t>(2), v.size());
+
+ const std::vector<sal_Int32>& w = iss.GetStyleSheetPositionsByFamily(SfxStyleFamily::All);
+ CPPUNIT_ASSERT_EQUAL_MESSAGE("Wildcard works for family queries.", static_cast<size_t>(3), w.size());
+}
+
+void IndexedStyleSheetsTest::OnlyOneStyleSheetIsReturnedWhenReturnFirstIsUsed()
+{
+ OUString name("name1");
+ rtl::Reference<SfxStyleSheetBase> sheet1(new MockedStyleSheet(name, SfxStyleFamily::Char));
+ rtl::Reference<SfxStyleSheetBase> sheet2(new MockedStyleSheet(name, SfxStyleFamily::Para));
+ rtl::Reference<SfxStyleSheetBase> sheet3(new MockedStyleSheet(name, SfxStyleFamily::Char));
+
+ IndexedStyleSheets iss;
+ iss.AddStyleSheet(sheet1);
+ iss.AddStyleSheet(sheet2);
+ iss.AddStyleSheet(sheet3);
+
+ DummyPredicate predicate; // returns always true, i.e., all style sheets match the predicate.
+
+ std::vector<sal_Int32> v = iss.FindPositionsByNameAndPredicate(name, predicate,
+ IndexedStyleSheets::SearchBehavior::ReturnFirst);
+ CPPUNIT_ASSERT_EQUAL_MESSAGE("Only one style sheet is returned.", static_cast<size_t>(1), v.size());
+
+ std::vector<sal_Int32> w = iss.FindPositionsByNameAndPredicate(name, predicate);
+ CPPUNIT_ASSERT_EQUAL_MESSAGE("All style sheets are returned.", static_cast<size_t>(3), w.size());
+}
+
+CPPUNIT_TEST_SUITE_REGISTRATION(IndexedStyleSheetsTest);
+
+CPPUNIT_PLUGIN_IMPLEMENT();
diff --git a/svl/qa/unit/items/test_itempool.cxx b/svl/qa/unit/items/test_itempool.cxx
new file mode 100644
index 000000000..6868999f0
--- /dev/null
+++ b/svl/qa/unit/items/test_itempool.cxx
@@ -0,0 +1,99 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#include <svl/itempool.hxx>
+#include <poolio.hxx>
+
+#include <cppunit/TestAssert.h>
+#include <cppunit/TestFixture.h>
+#include <cppunit/extensions/HelperMacros.h>
+#include <cppunit/plugin/TestPlugIn.h>
+
+class PoolItemTest : public CppUnit::TestFixture
+{
+ public:
+ PoolItemTest() {}
+
+ void testPool();
+
+ // Adds code needed to register the test suite
+ CPPUNIT_TEST_SUITE(PoolItemTest);
+
+ CPPUNIT_TEST(testPool);
+
+ // End of test suite definition
+ CPPUNIT_TEST_SUITE_END();
+};
+
+void PoolItemTest::testPool()
+{
+ SfxItemInfo const aItems[] =
+ { { 4, true },
+ { 3, false /* not poolable */ },
+ { 2, false },
+ { 1, false /* not poolable */}
+ };
+
+ rtl::Reference<SfxItemPool> pPool = new SfxItemPool("testpool", 1, 4, aItems);
+ SfxItemPool_Impl *pImpl = SfxItemPool_Impl::GetImpl(pPool.get());
+ CPPUNIT_ASSERT(pImpl != nullptr);
+ CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(4), pImpl->maPoolItemArrays.size());
+
+ // Poolable
+ SfxVoidItem aItemOne( 1 );
+ SfxVoidItem aNotherOne( 1 );
+
+ {
+ CPPUNIT_ASSERT(pImpl->maPoolItemArrays[0].empty());
+ const SfxPoolItem &rVal = pPool->Put(aItemOne);
+ CPPUNIT_ASSERT(bool(rVal == aItemOne));
+ CPPUNIT_ASSERT(!pImpl->maPoolItemArrays[0].empty());
+ const SfxPoolItem &rVal2 = pPool->Put(aNotherOne);
+ CPPUNIT_ASSERT(bool(rVal2 == rVal));
+ CPPUNIT_ASSERT_EQUAL(&rVal, &rVal2);
+
+ // Clones on Put ...
+ CPPUNIT_ASSERT(&rVal2 != &aItemOne);
+ CPPUNIT_ASSERT(&rVal2 != &aNotherOne);
+ CPPUNIT_ASSERT(&rVal != &aItemOne);
+ CPPUNIT_ASSERT(&rVal != &aNotherOne);
+ }
+
+ // non-poolable
+ SfxVoidItem aItemTwo( 2 );
+ SfxVoidItem aNotherTwo( 2 );
+ {
+ CPPUNIT_ASSERT(pImpl->maPoolItemArrays[1].empty());
+ const SfxPoolItem &rVal = pPool->Put(aItemTwo);
+ CPPUNIT_ASSERT(bool(rVal == aItemTwo));
+ CPPUNIT_ASSERT(!pImpl->maPoolItemArrays[1].empty());
+
+ const SfxPoolItem &rVal2 = pPool->Put(aNotherTwo);
+ CPPUNIT_ASSERT(bool(rVal2 == rVal));
+ CPPUNIT_ASSERT(&rVal2 != &rVal);
+ }
+
+ // Test removal.
+ SfxVoidItem aRemoveFour(4);
+ SfxVoidItem aNotherFour(4);
+ const SfxPoolItem &rKeyFour = pPool->Put(aRemoveFour);
+ pPool->Put(aNotherFour);
+ CPPUNIT_ASSERT(pImpl->maPoolItemArrays[3].size() > 0);
+ CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), pImpl->maPoolItemArrays[3].size());
+ pPool->Remove(rKeyFour);
+ CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), pImpl->maPoolItemArrays[3].size());
+ pPool->Put(aNotherFour);
+ CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), pImpl->maPoolItemArrays[3].size());
+
+}
+
+
+CPPUNIT_TEST_SUITE_REGISTRATION(PoolItemTest);
+
+CPPUNIT_PLUGIN_IMPLEMENT();