1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #include <svl/IndexedStyleSheets.hxx>
12 #include <svl/style.hxx>
14 #include <cppunit/TestAssert.h>
15 #include <cppunit/TestFixture.h>
16 #include <cppunit/extensions/HelperMacros.h>
17 #include <cppunit/plugin/TestPlugIn.h>
25 class MockedStyleSheet
: public SfxStyleSheetBase
28 MockedStyleSheet(const OUString
& name
, SfxStyleFamily fam
= SfxStyleFamily::Char
)
29 : SfxStyleSheetBase(name
, nullptr, fam
, SfxStyleSearchBits::Auto
, u
""_ustr
)
34 struct DummyPredicate
: public StyleSheetPredicate
{
35 bool Check(const SfxStyleSheetBase
&) override
{
42 class IndexedStyleSheetsTest
: public CppUnit::TestFixture
44 void InstantiationWorks();
45 void AddedStylesheetsCanBeFoundAndRetrievedByPosition();
46 void AddingSameStylesheetTwiceHasNoEffect();
47 void RemovedStyleSheetIsNotFound();
48 void RemovingStyleSheetWhichIsNotAvailableHasNoEffect();
49 void StyleSheetsCanBeRetrievedByTheirName();
50 void KnowsThatItStoresAStyleSheet();
51 void PositionCanBeQueriedByFamily();
52 void OnlyOneStyleSheetIsReturnedWhenReturnFirstIsUsed();
54 // Adds code needed to register the test suite
55 CPPUNIT_TEST_SUITE(IndexedStyleSheetsTest
);
57 CPPUNIT_TEST(InstantiationWorks
);
58 CPPUNIT_TEST(AddedStylesheetsCanBeFoundAndRetrievedByPosition
);
59 CPPUNIT_TEST(AddingSameStylesheetTwiceHasNoEffect
);
60 CPPUNIT_TEST(RemovedStyleSheetIsNotFound
);
61 CPPUNIT_TEST(RemovingStyleSheetWhichIsNotAvailableHasNoEffect
);
62 CPPUNIT_TEST(StyleSheetsCanBeRetrievedByTheirName
);
63 CPPUNIT_TEST(KnowsThatItStoresAStyleSheet
);
64 CPPUNIT_TEST(PositionCanBeQueriedByFamily
);
65 CPPUNIT_TEST(OnlyOneStyleSheetIsReturnedWhenReturnFirstIsUsed
);
67 // End of test suite definition
68 CPPUNIT_TEST_SUITE_END();
72 void IndexedStyleSheetsTest::InstantiationWorks()
74 IndexedStyleSheets iss
;
77 void IndexedStyleSheetsTest::AddedStylesheetsCanBeFoundAndRetrievedByPosition()
79 rtl::Reference
<SfxStyleSheetBase
> sheet1(new MockedStyleSheet(u
"name1"_ustr
));
80 rtl::Reference
<SfxStyleSheetBase
> sheet2(new MockedStyleSheet(u
"name2"_ustr
));
81 IndexedStyleSheets iss
;
82 iss
.AddStyleSheet(sheet1
);
83 iss
.AddStyleSheet(sheet2
);
84 unsigned pos
= iss
.FindStyleSheetPosition(*sheet2
);
85 rtl::Reference
<SfxStyleSheetBase
> retrieved
= iss
.GetStyleSheetByPosition(pos
);
86 CPPUNIT_ASSERT_EQUAL_MESSAGE("retrieved sheet is that which has been inserted.", sheet2
.get(), retrieved
.get());
89 void IndexedStyleSheetsTest::AddingSameStylesheetTwiceHasNoEffect()
91 rtl::Reference
<SfxStyleSheetBase
> sheet1(new MockedStyleSheet(u
"sheet1"_ustr
));
92 IndexedStyleSheets iss
;
93 iss
.AddStyleSheet(sheet1
);
94 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(1), iss
.GetNumberOfStyleSheets());
95 iss
.AddStyleSheet(sheet1
);
96 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(1), iss
.GetNumberOfStyleSheets());
99 void IndexedStyleSheetsTest::RemovedStyleSheetIsNotFound()
101 rtl::Reference
<SfxStyleSheetBase
> sheet1(new MockedStyleSheet(u
"name1"_ustr
));
102 rtl::Reference
<SfxStyleSheetBase
> sheet2(new MockedStyleSheet(u
"name2"_ustr
));
103 IndexedStyleSheets iss
;
104 iss
.AddStyleSheet(sheet1
);
105 iss
.AddStyleSheet(sheet2
);
106 iss
.RemoveStyleSheet(sheet1
);
107 CPPUNIT_ASSERT_EQUAL_MESSAGE("Removed style sheet is not found.",
108 false, iss
.HasStyleSheet(sheet1
));
111 void IndexedStyleSheetsTest::RemovingStyleSheetWhichIsNotAvailableHasNoEffect()
113 rtl::Reference
<SfxStyleSheetBase
> sheet1(new MockedStyleSheet(u
"sheet1"_ustr
));
114 rtl::Reference
<SfxStyleSheetBase
> sheet2(new MockedStyleSheet(u
"sheet2"_ustr
));
115 IndexedStyleSheets iss
;
116 iss
.AddStyleSheet(sheet1
);
117 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(1), iss
.GetNumberOfStyleSheets());
118 iss
.RemoveStyleSheet(sheet2
);
119 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(1), iss
.GetNumberOfStyleSheets());
122 void IndexedStyleSheetsTest::StyleSheetsCanBeRetrievedByTheirName()
124 OUString
name1(u
"name1"_ustr
);
125 OUString
name2(u
"name2"_ustr
);
126 rtl::Reference
<SfxStyleSheetBase
> sheet1(new MockedStyleSheet(name1
));
127 rtl::Reference
<SfxStyleSheetBase
> sheet2(new MockedStyleSheet(name2
));
128 rtl::Reference
<SfxStyleSheetBase
> sheet3(new MockedStyleSheet(name1
));
129 IndexedStyleSheets iss
;
130 iss
.AddStyleSheet(sheet1
);
131 iss
.AddStyleSheet(sheet2
);
132 iss
.AddStyleSheet(sheet3
);
134 std::vector
<sal_Int32
> r
= iss
.FindPositionsByName(name1
);
135 CPPUNIT_ASSERT_EQUAL_MESSAGE("Two style sheets are found by 'name1'",
136 2u, static_cast<unsigned>(r
.size()));
137 std::sort (r
.begin(), r
.end());
138 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(0), r
.at(0));
139 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(2), r
.at(1));
141 r
= iss
.FindPositionsByName(name2
);
142 CPPUNIT_ASSERT_EQUAL_MESSAGE("One style sheets is found by 'name2'",
143 1u, static_cast<unsigned>(r
.size()));
144 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(1), r
.at(0));
147 void IndexedStyleSheetsTest::KnowsThatItStoresAStyleSheet()
149 static constexpr OUString
name1(u
"name1"_ustr
);
150 rtl::Reference
<SfxStyleSheetBase
> sheet1(new MockedStyleSheet(name1
));
151 rtl::Reference
<SfxStyleSheetBase
> sheet2(new MockedStyleSheet(name1
));
152 rtl::Reference
<SfxStyleSheetBase
> sheet3(new MockedStyleSheet(u
"name2"_ustr
));
153 rtl::Reference
<SfxStyleSheetBase
> sheet4(new MockedStyleSheet(name1
));
154 IndexedStyleSheets iss
;
155 iss
.AddStyleSheet(sheet1
);
156 iss
.AddStyleSheet(sheet2
);
157 iss
.AddStyleSheet(sheet3
);
158 // do not add sheet 4
160 CPPUNIT_ASSERT_EQUAL_MESSAGE("Finds first stored style sheet even though two style sheets have the same name.",
161 true, iss
.HasStyleSheet(sheet1
));
162 CPPUNIT_ASSERT_EQUAL_MESSAGE("Finds second stored style sheet even though two style sheets have the same name.",
163 true, iss
.HasStyleSheet(sheet2
));
164 CPPUNIT_ASSERT_EQUAL_MESSAGE("Does not find style sheet which is not stored and has the same name as a stored.",
165 false, iss
.HasStyleSheet(sheet4
));
168 void IndexedStyleSheetsTest::PositionCanBeQueriedByFamily()
170 rtl::Reference
<SfxStyleSheetBase
> sheet1(new MockedStyleSheet(u
"name1"_ustr
, SfxStyleFamily::Char
));
171 rtl::Reference
<SfxStyleSheetBase
> sheet2(new MockedStyleSheet(u
"name2"_ustr
, SfxStyleFamily::Para
));
172 rtl::Reference
<SfxStyleSheetBase
> sheet3(new MockedStyleSheet(u
"name3"_ustr
, SfxStyleFamily::Char
));
174 IndexedStyleSheets iss
;
175 iss
.AddStyleSheet(sheet1
);
176 iss
.AddStyleSheet(sheet2
);
177 iss
.AddStyleSheet(sheet3
);
179 const std::vector
<SfxStyleSheetBase
*>& v
= iss
.GetStyleSheetsByFamily(SfxStyleFamily::Char
);
180 CPPUNIT_ASSERT_EQUAL_MESSAGE("Separation by family works.", static_cast<size_t>(2), v
.size());
182 const std::vector
<SfxStyleSheetBase
*>& w
= iss
.GetStyleSheetsByFamily(SfxStyleFamily::Para
);
183 CPPUNIT_ASSERT_EQUAL_MESSAGE("Wildcard works for family queries.", static_cast<size_t>(1), w
.size());
186 void IndexedStyleSheetsTest::OnlyOneStyleSheetIsReturnedWhenReturnFirstIsUsed()
188 OUString
name(u
"name1"_ustr
);
189 rtl::Reference
<SfxStyleSheetBase
> sheet1(new MockedStyleSheet(name
, SfxStyleFamily::Char
));
190 rtl::Reference
<SfxStyleSheetBase
> sheet2(new MockedStyleSheet(name
, SfxStyleFamily::Para
));
191 rtl::Reference
<SfxStyleSheetBase
> sheet3(new MockedStyleSheet(name
, SfxStyleFamily::Char
));
193 IndexedStyleSheets iss
;
194 iss
.AddStyleSheet(sheet1
);
195 iss
.AddStyleSheet(sheet2
);
196 iss
.AddStyleSheet(sheet3
);
198 DummyPredicate predicate
; // returns always true, i.e., all style sheets match the predicate.
200 std::vector
<sal_Int32
> v
= iss
.FindPositionsByNameAndPredicate(name
, predicate
,
201 IndexedStyleSheets::SearchBehavior::ReturnFirst
);
202 CPPUNIT_ASSERT_EQUAL_MESSAGE("Only one style sheet is returned.", static_cast<size_t>(1), v
.size());
204 std::vector
<sal_Int32
> w
= iss
.FindPositionsByNameAndPredicate(name
, predicate
);
205 CPPUNIT_ASSERT_EQUAL_MESSAGE("All style sheets are returned.", static_cast<size_t>(3), w
.size());
208 CPPUNIT_TEST_SUITE_REGISTRATION(IndexedStyleSheetsTest
);
210 CPPUNIT_PLUGIN_IMPLEMENT();