Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : */
9 :
10 : #include <svl/IndexedStyleSheets.hxx>
11 :
12 : #include <svl/style.hxx>
13 :
14 : #include <cppunit/TestAssert.h>
15 : #include <cppunit/TestFixture.h>
16 : #include <cppunit/extensions/HelperMacros.h>
17 : #include <cppunit/plugin/TestPlugIn.h>
18 :
19 : using namespace svl;
20 :
21 80 : class MockedStyleSheet : public SfxStyleSheetBase
22 : {
23 : public:
24 40 : MockedStyleSheet(const rtl::OUString& name, SfxStyleFamily fam = SFX_STYLE_FAMILY_CHAR)
25 40 : : SfxStyleSheetBase(name, NULL, fam, 0)
26 40 : {;}
27 :
28 : };
29 :
30 2 : struct DummyPredicate : public StyleSheetPredicate {
31 8 : bool Check(const SfxStyleSheetBase& styleSheet) SAL_OVERRIDE {
32 : (void)styleSheet; // fix compiler warning
33 8 : return true;
34 : }
35 : };
36 :
37 54 : class IndexedStyleSheetsTest : public CppUnit::TestFixture
38 : {
39 : void InstantiationWorks();
40 : void AddedStylesheetsCanBeFoundAndRetrievedByPosition();
41 : void AddingSameStylesheetTwiceHasNoEffect();
42 : void RemovedStyleSheetIsNotFound();
43 : void RemovingStyleSheetWhichIsNotAvailableHasNoEffect();
44 : void StyleSheetsCanBeRetrievedByTheirName();
45 : void KnowsThatItStoresAStyleSheet();
46 : void PositionCanBeQueriedByFamily();
47 : void OnlyOneStyleSheetIsReturnedWhenReturnFirstIsUsed();
48 :
49 : // Adds code needed to register the test suite
50 4 : CPPUNIT_TEST_SUITE(IndexedStyleSheetsTest);
51 :
52 2 : CPPUNIT_TEST(InstantiationWorks);
53 2 : CPPUNIT_TEST(AddedStylesheetsCanBeFoundAndRetrievedByPosition);
54 2 : CPPUNIT_TEST(AddingSameStylesheetTwiceHasNoEffect);
55 2 : CPPUNIT_TEST(RemovedStyleSheetIsNotFound);
56 2 : CPPUNIT_TEST(RemovingStyleSheetWhichIsNotAvailableHasNoEffect);
57 2 : CPPUNIT_TEST(StyleSheetsCanBeRetrievedByTheirName);
58 2 : CPPUNIT_TEST(KnowsThatItStoresAStyleSheet);
59 2 : CPPUNIT_TEST(PositionCanBeQueriedByFamily);
60 2 : CPPUNIT_TEST(OnlyOneStyleSheetIsReturnedWhenReturnFirstIsUsed);
61 :
62 : // End of test suite definition
63 4 : CPPUNIT_TEST_SUITE_END();
64 :
65 : };
66 :
67 2 : void IndexedStyleSheetsTest::InstantiationWorks()
68 : {
69 2 : IndexedStyleSheets iss;
70 2 : }
71 :
72 2 : void IndexedStyleSheetsTest::AddedStylesheetsCanBeFoundAndRetrievedByPosition()
73 : {
74 2 : rtl::OUString name1("name1");
75 4 : rtl::OUString name2("name2");
76 4 : rtl::Reference<SfxStyleSheetBase> sheet1(new MockedStyleSheet(name1));
77 4 : rtl::Reference<SfxStyleSheetBase> sheet2(new MockedStyleSheet(name2));
78 4 : IndexedStyleSheets iss;
79 2 : iss.AddStyleSheet(sheet1);
80 2 : iss.AddStyleSheet(sheet2);
81 2 : unsigned pos = iss.FindStyleSheetPosition(*sheet2);
82 4 : rtl::Reference<SfxStyleSheetBase> retrieved = iss.GetStyleSheetByPosition(pos);
83 4 : CPPUNIT_ASSERT_EQUAL_MESSAGE("retrieved sheet is that which has been inserted.", sheet2.get(), retrieved.get());
84 2 : }
85 :
86 2 : void IndexedStyleSheetsTest::AddingSameStylesheetTwiceHasNoEffect()
87 : {
88 2 : rtl::Reference<SfxStyleSheetBase> sheet1(new MockedStyleSheet(rtl::OUString("sheet1")));
89 4 : IndexedStyleSheets iss;
90 2 : iss.AddStyleSheet(sheet1);
91 2 : CPPUNIT_ASSERT_EQUAL(1u, iss.GetNumberOfStyleSheets());
92 2 : iss.AddStyleSheet(sheet1);
93 4 : CPPUNIT_ASSERT_EQUAL(1u, iss.GetNumberOfStyleSheets());
94 2 : }
95 :
96 2 : void IndexedStyleSheetsTest::RemovedStyleSheetIsNotFound()
97 : {
98 2 : rtl::OUString name1("name1");
99 4 : rtl::OUString name2("name2");
100 4 : rtl::Reference<SfxStyleSheetBase> sheet1(new MockedStyleSheet(name1));
101 4 : rtl::Reference<SfxStyleSheetBase> sheet2(new MockedStyleSheet(name2));
102 4 : IndexedStyleSheets iss;
103 2 : iss.AddStyleSheet(sheet1);
104 2 : iss.AddStyleSheet(sheet2);
105 2 : iss.RemoveStyleSheet(sheet1);
106 4 : CPPUNIT_ASSERT_EQUAL_MESSAGE("Removed style sheet is not found.",
107 4 : false, iss.HasStyleSheet(sheet1));
108 2 : }
109 :
110 2 : void IndexedStyleSheetsTest::RemovingStyleSheetWhichIsNotAvailableHasNoEffect()
111 : {
112 2 : rtl::Reference<SfxStyleSheetBase> sheet1(new MockedStyleSheet(rtl::OUString("sheet1")));
113 4 : rtl::Reference<SfxStyleSheetBase> sheet2(new MockedStyleSheet(rtl::OUString("sheet2")));
114 4 : IndexedStyleSheets iss;
115 2 : iss.AddStyleSheet(sheet1);
116 2 : CPPUNIT_ASSERT_EQUAL(1u, iss.GetNumberOfStyleSheets());
117 2 : iss.RemoveStyleSheet(sheet2);
118 4 : CPPUNIT_ASSERT_EQUAL(1u, iss.GetNumberOfStyleSheets());
119 2 : }
120 :
121 2 : void IndexedStyleSheetsTest::StyleSheetsCanBeRetrievedByTheirName()
122 : {
123 2 : rtl::OUString name1("name1");
124 4 : rtl::OUString name2("name2");
125 4 : rtl::Reference<SfxStyleSheetBase> sheet1(new MockedStyleSheet(name1));
126 4 : rtl::Reference<SfxStyleSheetBase> sheet2(new MockedStyleSheet(name2));
127 4 : rtl::Reference<SfxStyleSheetBase> sheet3(new MockedStyleSheet(name1));
128 4 : IndexedStyleSheets iss;
129 2 : iss.AddStyleSheet(sheet1);
130 2 : iss.AddStyleSheet(sheet2);
131 2 : iss.AddStyleSheet(sheet3);
132 :
133 4 : std::vector<unsigned> r = iss.FindPositionsByName(name1);
134 4 : CPPUNIT_ASSERT_EQUAL_MESSAGE("Two style sheets are found by 'name1'",
135 2 : 2u, static_cast<unsigned>(r.size()));
136 2 : CPPUNIT_ASSERT_EQUAL(0u, r.at(0));
137 2 : CPPUNIT_ASSERT_EQUAL(2u, r.at(1));
138 :
139 2 : r = iss.FindPositionsByName(name2);
140 4 : CPPUNIT_ASSERT_EQUAL_MESSAGE("One style sheets is found by 'name2'",
141 2 : 1u, static_cast<unsigned>(r.size()));
142 4 : CPPUNIT_ASSERT_EQUAL(1u, r.at(0));
143 2 : }
144 :
145 2 : void IndexedStyleSheetsTest::KnowsThatItStoresAStyleSheet()
146 : {
147 2 : rtl::OUString name1("name1");
148 4 : rtl::OUString name2("name2");
149 4 : rtl::Reference<SfxStyleSheetBase> sheet1(new MockedStyleSheet(name1));
150 4 : rtl::Reference<SfxStyleSheetBase> sheet2(new MockedStyleSheet(name1));
151 4 : rtl::Reference<SfxStyleSheetBase> sheet3(new MockedStyleSheet(name2));
152 4 : rtl::Reference<SfxStyleSheetBase> sheet4(new MockedStyleSheet(name1));
153 4 : IndexedStyleSheets iss;
154 2 : iss.AddStyleSheet(sheet1);
155 2 : iss.AddStyleSheet(sheet2);
156 2 : iss.AddStyleSheet(sheet3);
157 : // do not add sheet 4
158 :
159 4 : CPPUNIT_ASSERT_EQUAL_MESSAGE("Finds first stored style sheet even though two style sheets have the same name.",
160 2 : true, iss.HasStyleSheet(sheet1));
161 4 : CPPUNIT_ASSERT_EQUAL_MESSAGE("Finds second stored style sheet even though two style sheets have the same name.",
162 2 : true, iss.HasStyleSheet(sheet2));
163 4 : CPPUNIT_ASSERT_EQUAL_MESSAGE("Does not find style sheet which is not stored and has the same name as a stored.",
164 4 : false, iss.HasStyleSheet(sheet4));
165 2 : }
166 :
167 2 : void IndexedStyleSheetsTest::PositionCanBeQueriedByFamily()
168 : {
169 2 : rtl::OUString name1("name1");
170 4 : rtl::OUString name2("name2");
171 4 : rtl::OUString name3("name3");
172 4 : rtl::Reference<SfxStyleSheetBase> sheet1(new MockedStyleSheet(name1, SFX_STYLE_FAMILY_CHAR));
173 4 : rtl::Reference<SfxStyleSheetBase> sheet2(new MockedStyleSheet(name2, SFX_STYLE_FAMILY_PARA));
174 4 : rtl::Reference<SfxStyleSheetBase> sheet3(new MockedStyleSheet(name3, SFX_STYLE_FAMILY_CHAR));
175 :
176 4 : IndexedStyleSheets iss;
177 2 : iss.AddStyleSheet(sheet1);
178 2 : iss.AddStyleSheet(sheet2);
179 2 : iss.AddStyleSheet(sheet3);
180 :
181 2 : const std::vector<unsigned>& v = iss.GetStyleSheetPositionsByFamily(SFX_STYLE_FAMILY_CHAR);
182 2 : CPPUNIT_ASSERT_EQUAL_MESSAGE("Separation by family works.", static_cast<size_t>(2), v.size());
183 :
184 2 : const std::vector<unsigned>& w = iss.GetStyleSheetPositionsByFamily(SFX_STYLE_FAMILY_ALL);
185 4 : CPPUNIT_ASSERT_EQUAL_MESSAGE("Wildcard works for family queries.", static_cast<size_t>(3), w.size());
186 2 : }
187 :
188 2 : void IndexedStyleSheetsTest::OnlyOneStyleSheetIsReturnedWhenReturnFirstIsUsed()
189 : {
190 2 : rtl::OUString name("name1");
191 4 : rtl::Reference<SfxStyleSheetBase> sheet1(new MockedStyleSheet(name, SFX_STYLE_FAMILY_CHAR));
192 4 : rtl::Reference<SfxStyleSheetBase> sheet2(new MockedStyleSheet(name, SFX_STYLE_FAMILY_PARA));
193 4 : rtl::Reference<SfxStyleSheetBase> sheet3(new MockedStyleSheet(name, SFX_STYLE_FAMILY_CHAR));
194 :
195 4 : IndexedStyleSheets iss;
196 2 : iss.AddStyleSheet(sheet1);
197 2 : iss.AddStyleSheet(sheet2);
198 2 : iss.AddStyleSheet(sheet3);
199 :
200 4 : DummyPredicate predicate; // returns always true, i.e., all style sheets match the predicate.
201 :
202 : std::vector<unsigned> v = iss.FindPositionsByNameAndPredicate(name, predicate,
203 4 : IndexedStyleSheets::RETURN_FIRST);
204 2 : CPPUNIT_ASSERT_EQUAL_MESSAGE("Only one style sheet is returned.", static_cast<size_t>(1), v.size());
205 :
206 : std::vector<unsigned> w = iss.FindPositionsByNameAndPredicate(name, predicate,
207 4 : IndexedStyleSheets::RETURN_ALL);
208 4 : CPPUNIT_ASSERT_EQUAL_MESSAGE("All style sheets are returned.", static_cast<size_t>(3), w.size());
209 2 : }
210 :
211 2 : CPPUNIT_TEST_SUITE_REGISTRATION(IndexedStyleSheetsTest);
212 :
213 8 : CPPUNIT_PLUGIN_IMPLEMENT();
|