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