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>
23 class MockedStyleSheet
: public SfxStyleSheetBase
26 MockedStyleSheet(const rtl::OUString
& name
, SfxStyleFamily fam
= SFX_STYLE_FAMILY_CHAR
)
27 : SfxStyleSheetBase(name
, NULL
, fam
, 0)
32 struct DummyPredicate
: public StyleSheetPredicate
{
33 bool Check(const SfxStyleSheetBase
& styleSheet
) SAL_OVERRIDE
{
34 (void)styleSheet
; // fix compiler warning
39 class IndexedStyleSheetsTest
: public CppUnit::TestFixture
41 void InstantiationWorks();
42 void AddedStylesheetsCanBeFoundAndRetrievedByPosition();
43 void AddingSameStylesheetTwiceHasNoEffect();
44 void RemovedStyleSheetIsNotFound();
45 void RemovingStyleSheetWhichIsNotAvailableHasNoEffect();
46 void StyleSheetsCanBeRetrievedByTheirName();
47 void KnowsThatItStoresAStyleSheet();
48 void PositionCanBeQueriedByFamily();
49 void OnlyOneStyleSheetIsReturnedWhenReturnFirstIsUsed();
51 // Adds code needed to register the test suite
52 CPPUNIT_TEST_SUITE(IndexedStyleSheetsTest
);
54 CPPUNIT_TEST(InstantiationWorks
);
55 CPPUNIT_TEST(AddedStylesheetsCanBeFoundAndRetrievedByPosition
);
56 CPPUNIT_TEST(AddingSameStylesheetTwiceHasNoEffect
);
57 CPPUNIT_TEST(RemovedStyleSheetIsNotFound
);
58 CPPUNIT_TEST(RemovingStyleSheetWhichIsNotAvailableHasNoEffect
);
59 CPPUNIT_TEST(StyleSheetsCanBeRetrievedByTheirName
);
60 CPPUNIT_TEST(KnowsThatItStoresAStyleSheet
);
61 CPPUNIT_TEST(PositionCanBeQueriedByFamily
);
62 CPPUNIT_TEST(OnlyOneStyleSheetIsReturnedWhenReturnFirstIsUsed
);
64 // End of test suite definition
65 CPPUNIT_TEST_SUITE_END();
69 void IndexedStyleSheetsTest::InstantiationWorks()
71 IndexedStyleSheets iss
;
74 void IndexedStyleSheetsTest::AddedStylesheetsCanBeFoundAndRetrievedByPosition()
76 rtl::OUString
name1("name1");
77 rtl::OUString
name2("name2");
78 rtl::Reference
<SfxStyleSheetBase
> sheet1(new MockedStyleSheet(name1
));
79 rtl::Reference
<SfxStyleSheetBase
> sheet2(new MockedStyleSheet(name2
));
80 IndexedStyleSheets iss
;
81 iss
.AddStyleSheet(sheet1
);
82 iss
.AddStyleSheet(sheet2
);
83 unsigned pos
= iss
.FindStyleSheetPosition(*sheet2
);
84 rtl::Reference
<SfxStyleSheetBase
> retrieved
= iss
.GetStyleSheetByPosition(pos
);
85 CPPUNIT_ASSERT_EQUAL_MESSAGE("retrieved sheet is that which has been inserted.", sheet2
.get(), retrieved
.get());
88 void IndexedStyleSheetsTest::AddingSameStylesheetTwiceHasNoEffect()
90 rtl::Reference
<SfxStyleSheetBase
> sheet1(new MockedStyleSheet(rtl::OUString("sheet1")));
91 IndexedStyleSheets iss
;
92 iss
.AddStyleSheet(sheet1
);
93 CPPUNIT_ASSERT_EQUAL(1u, iss
.GetNumberOfStyleSheets());
94 iss
.AddStyleSheet(sheet1
);
95 CPPUNIT_ASSERT_EQUAL(1u, iss
.GetNumberOfStyleSheets());
98 void IndexedStyleSheetsTest::RemovedStyleSheetIsNotFound()
100 rtl::OUString
name1("name1");
101 rtl::OUString
name2("name2");
102 rtl::Reference
<SfxStyleSheetBase
> sheet1(new MockedStyleSheet(name1
));
103 rtl::Reference
<SfxStyleSheetBase
> sheet2(new MockedStyleSheet(name2
));
104 IndexedStyleSheets iss
;
105 iss
.AddStyleSheet(sheet1
);
106 iss
.AddStyleSheet(sheet2
);
107 iss
.RemoveStyleSheet(sheet1
);
108 CPPUNIT_ASSERT_EQUAL_MESSAGE("Removed style sheet is not found.",
109 false, iss
.HasStyleSheet(sheet1
));
112 void IndexedStyleSheetsTest::RemovingStyleSheetWhichIsNotAvailableHasNoEffect()
114 rtl::Reference
<SfxStyleSheetBase
> sheet1(new MockedStyleSheet(rtl::OUString("sheet1")));
115 rtl::Reference
<SfxStyleSheetBase
> sheet2(new MockedStyleSheet(rtl::OUString("sheet2")));
116 IndexedStyleSheets iss
;
117 iss
.AddStyleSheet(sheet1
);
118 CPPUNIT_ASSERT_EQUAL(1u, iss
.GetNumberOfStyleSheets());
119 iss
.RemoveStyleSheet(sheet2
);
120 CPPUNIT_ASSERT_EQUAL(1u, iss
.GetNumberOfStyleSheets());
123 void IndexedStyleSheetsTest::StyleSheetsCanBeRetrievedByTheirName()
125 rtl::OUString
name1("name1");
126 rtl::OUString
name2("name2");
127 rtl::Reference
<SfxStyleSheetBase
> sheet1(new MockedStyleSheet(name1
));
128 rtl::Reference
<SfxStyleSheetBase
> sheet2(new MockedStyleSheet(name2
));
129 rtl::Reference
<SfxStyleSheetBase
> sheet3(new MockedStyleSheet(name1
));
130 IndexedStyleSheets iss
;
131 iss
.AddStyleSheet(sheet1
);
132 iss
.AddStyleSheet(sheet2
);
133 iss
.AddStyleSheet(sheet3
);
135 std::vector
<unsigned> r
= iss
.FindPositionsByName(name1
);
136 CPPUNIT_ASSERT_EQUAL_MESSAGE("Two style sheets are found by 'name1'",
137 2u, static_cast<unsigned>(r
.size()));
138 std::sort (r
.begin(), r
.end());
139 CPPUNIT_ASSERT_EQUAL(0u, r
.at(0));
140 CPPUNIT_ASSERT_EQUAL(2u, r
.at(1));
142 r
= iss
.FindPositionsByName(name2
);
143 CPPUNIT_ASSERT_EQUAL_MESSAGE("One style sheets is found by 'name2'",
144 1u, static_cast<unsigned>(r
.size()));
145 CPPUNIT_ASSERT_EQUAL(1u, r
.at(0));
148 void IndexedStyleSheetsTest::KnowsThatItStoresAStyleSheet()
150 rtl::OUString
name1("name1");
151 rtl::OUString
name2("name2");
152 rtl::Reference
<SfxStyleSheetBase
> sheet1(new MockedStyleSheet(name1
));
153 rtl::Reference
<SfxStyleSheetBase
> sheet2(new MockedStyleSheet(name1
));
154 rtl::Reference
<SfxStyleSheetBase
> sheet3(new MockedStyleSheet(name2
));
155 rtl::Reference
<SfxStyleSheetBase
> sheet4(new MockedStyleSheet(name1
));
156 IndexedStyleSheets iss
;
157 iss
.AddStyleSheet(sheet1
);
158 iss
.AddStyleSheet(sheet2
);
159 iss
.AddStyleSheet(sheet3
);
160 // do not add sheet 4
162 CPPUNIT_ASSERT_EQUAL_MESSAGE("Finds first stored style sheet even though two style sheets have the same name.",
163 true, iss
.HasStyleSheet(sheet1
));
164 CPPUNIT_ASSERT_EQUAL_MESSAGE("Finds second stored style sheet even though two style sheets have the same name.",
165 true, iss
.HasStyleSheet(sheet2
));
166 CPPUNIT_ASSERT_EQUAL_MESSAGE("Does not find style sheet which is not stored and has the same name as a stored.",
167 false, iss
.HasStyleSheet(sheet4
));
170 void IndexedStyleSheetsTest::PositionCanBeQueriedByFamily()
172 rtl::OUString
name1("name1");
173 rtl::OUString
name2("name2");
174 rtl::OUString
name3("name3");
175 rtl::Reference
<SfxStyleSheetBase
> sheet1(new MockedStyleSheet(name1
, SFX_STYLE_FAMILY_CHAR
));
176 rtl::Reference
<SfxStyleSheetBase
> sheet2(new MockedStyleSheet(name2
, SFX_STYLE_FAMILY_PARA
));
177 rtl::Reference
<SfxStyleSheetBase
> sheet3(new MockedStyleSheet(name3
, SFX_STYLE_FAMILY_CHAR
));
179 IndexedStyleSheets iss
;
180 iss
.AddStyleSheet(sheet1
);
181 iss
.AddStyleSheet(sheet2
);
182 iss
.AddStyleSheet(sheet3
);
184 const std::vector
<unsigned>& v
= iss
.GetStyleSheetPositionsByFamily(SFX_STYLE_FAMILY_CHAR
);
185 CPPUNIT_ASSERT_EQUAL_MESSAGE("Separation by family works.", static_cast<size_t>(2), v
.size());
187 const std::vector
<unsigned>& w
= iss
.GetStyleSheetPositionsByFamily(SFX_STYLE_FAMILY_ALL
);
188 CPPUNIT_ASSERT_EQUAL_MESSAGE("Wildcard works for family queries.", static_cast<size_t>(3), w
.size());
191 void IndexedStyleSheetsTest::OnlyOneStyleSheetIsReturnedWhenReturnFirstIsUsed()
193 rtl::OUString
name("name1");
194 rtl::Reference
<SfxStyleSheetBase
> sheet1(new MockedStyleSheet(name
, SFX_STYLE_FAMILY_CHAR
));
195 rtl::Reference
<SfxStyleSheetBase
> sheet2(new MockedStyleSheet(name
, SFX_STYLE_FAMILY_PARA
));
196 rtl::Reference
<SfxStyleSheetBase
> sheet3(new MockedStyleSheet(name
, SFX_STYLE_FAMILY_CHAR
));
198 IndexedStyleSheets iss
;
199 iss
.AddStyleSheet(sheet1
);
200 iss
.AddStyleSheet(sheet2
);
201 iss
.AddStyleSheet(sheet3
);
203 DummyPredicate predicate
; // returns always true, i.e., all style sheets match the predicate.
205 std::vector
<unsigned> v
= iss
.FindPositionsByNameAndPredicate(name
, predicate
,
206 IndexedStyleSheets::RETURN_FIRST
);
207 CPPUNIT_ASSERT_EQUAL_MESSAGE("Only one style sheet is returned.", static_cast<size_t>(1), v
.size());
209 std::vector
<unsigned> w
= iss
.FindPositionsByNameAndPredicate(name
, predicate
,
210 IndexedStyleSheets::RETURN_ALL
);
211 CPPUNIT_ASSERT_EQUAL_MESSAGE("All style sheets are returned.", static_cast<size_t>(3), w
.size());
214 CPPUNIT_TEST_SUITE_REGISTRATION(IndexedStyleSheetsTest
);
216 CPPUNIT_PLUGIN_IMPLEMENT();