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 : // for SfxStyleSheetBase
13 : #include <svl/style.hxx>
14 :
15 : #include <cppunit/TestAssert.h>
16 : #include <cppunit/TestFixture.h>
17 : #include <cppunit/extensions/HelperMacros.h>
18 : #include <cppunit/plugin/TestPlugIn.h>
19 :
20 : using namespace svl;
21 :
22 28 : class MockedStyleSheet : public SfxStyleSheetBase
23 : {
24 : public:
25 14 : MockedStyleSheet(const rtl::OUString& name)
26 14 : : SfxStyleSheetBase(name, NULL, SFX_STYLE_FAMILY_CHAR, 0)
27 14 : {;}
28 :
29 : };
30 :
31 21 : class IndexedStyleSheetsTest : public CppUnit::TestFixture
32 : {
33 : void InstantiationWorks();
34 : void AddedStylesheetsCanBeFoundAndRetrievedByPosition();
35 : void AddingSameStylesheetTwiceHasNoEffect();
36 : void RemovedStyleSheetIsNotFound();
37 : void RemovingStyleSheetWhichIsNotAvailableHasNoEffect();
38 : void StyleSheetsCanBeRetrievedByTheirName();
39 : void KnowsThatItStoresAStyleSheet();
40 :
41 : // Adds code needed to register the test suite
42 2 : CPPUNIT_TEST_SUITE(IndexedStyleSheetsTest);
43 :
44 1 : CPPUNIT_TEST(InstantiationWorks);
45 1 : CPPUNIT_TEST(AddedStylesheetsCanBeFoundAndRetrievedByPosition);
46 1 : CPPUNIT_TEST(AddingSameStylesheetTwiceHasNoEffect);
47 1 : CPPUNIT_TEST(RemovedStyleSheetIsNotFound);
48 1 : CPPUNIT_TEST(RemovingStyleSheetWhichIsNotAvailableHasNoEffect);
49 1 : CPPUNIT_TEST(StyleSheetsCanBeRetrievedByTheirName);
50 1 : CPPUNIT_TEST(KnowsThatItStoresAStyleSheet);
51 :
52 : // End of test suite definition
53 2 : CPPUNIT_TEST_SUITE_END();
54 :
55 : };
56 :
57 1 : void IndexedStyleSheetsTest::InstantiationWorks()
58 : {
59 1 : IndexedStyleSheets iss;
60 1 : }
61 :
62 1 : void IndexedStyleSheetsTest::AddedStylesheetsCanBeFoundAndRetrievedByPosition()
63 : {
64 1 : rtl::OUString name1("name1");
65 2 : rtl::OUString name2("name2");
66 2 : rtl::Reference<SfxStyleSheetBase> sheet1(new MockedStyleSheet(name1));
67 2 : rtl::Reference<SfxStyleSheetBase> sheet2(new MockedStyleSheet(name2));
68 2 : IndexedStyleSheets iss;
69 1 : iss.AddStyleSheet(sheet1);
70 1 : iss.AddStyleSheet(sheet2);
71 1 : unsigned pos = iss.FindStyleSheetPosition(*sheet2);
72 2 : rtl::Reference<SfxStyleSheetBase> retrieved = iss.GetStyleSheetByPosition(pos);
73 2 : CPPUNIT_ASSERT_EQUAL_MESSAGE("retrieved sheet is that which has been inserted.", sheet2.get(), retrieved.get());
74 1 : }
75 :
76 1 : void IndexedStyleSheetsTest::AddingSameStylesheetTwiceHasNoEffect()
77 : {
78 1 : rtl::Reference<SfxStyleSheetBase> sheet1(new MockedStyleSheet(rtl::OUString("sheet1")));
79 2 : IndexedStyleSheets iss;
80 1 : iss.AddStyleSheet(sheet1);
81 1 : CPPUNIT_ASSERT_EQUAL(1u, iss.GetNumberOfStyleSheets());
82 1 : iss.AddStyleSheet(sheet1);
83 2 : CPPUNIT_ASSERT_EQUAL(1u, iss.GetNumberOfStyleSheets());
84 1 : }
85 :
86 1 : void IndexedStyleSheetsTest::RemovedStyleSheetIsNotFound()
87 : {
88 1 : rtl::OUString name1("name1");
89 2 : rtl::OUString name2("name2");
90 2 : rtl::Reference<SfxStyleSheetBase> sheet1(new MockedStyleSheet(name1));
91 2 : rtl::Reference<SfxStyleSheetBase> sheet2(new MockedStyleSheet(name2));
92 2 : IndexedStyleSheets iss;
93 1 : iss.AddStyleSheet(sheet1);
94 1 : iss.AddStyleSheet(sheet2);
95 1 : iss.RemoveStyleSheet(sheet1);
96 2 : CPPUNIT_ASSERT_EQUAL_MESSAGE("Removed style sheet is not found.",
97 2 : false, iss.HasStyleSheet(sheet1));
98 1 : }
99 :
100 1 : void IndexedStyleSheetsTest::RemovingStyleSheetWhichIsNotAvailableHasNoEffect()
101 : {
102 1 : rtl::Reference<SfxStyleSheetBase> sheet1(new MockedStyleSheet(rtl::OUString("sheet1")));
103 2 : rtl::Reference<SfxStyleSheetBase> sheet2(new MockedStyleSheet(rtl::OUString("sheet2")));
104 2 : IndexedStyleSheets iss;
105 1 : iss.AddStyleSheet(sheet1);
106 1 : CPPUNIT_ASSERT_EQUAL(1u, iss.GetNumberOfStyleSheets());
107 1 : iss.RemoveStyleSheet(sheet2);
108 2 : CPPUNIT_ASSERT_EQUAL(1u, iss.GetNumberOfStyleSheets());
109 1 : }
110 :
111 1 : void IndexedStyleSheetsTest::StyleSheetsCanBeRetrievedByTheirName()
112 : {
113 1 : rtl::OUString name1("name1");
114 2 : rtl::OUString name2("name2");
115 2 : rtl::Reference<SfxStyleSheetBase> sheet1(new MockedStyleSheet(name1));
116 2 : rtl::Reference<SfxStyleSheetBase> sheet2(new MockedStyleSheet(name2));
117 2 : rtl::Reference<SfxStyleSheetBase> sheet3(new MockedStyleSheet(name1));
118 2 : IndexedStyleSheets iss;
119 1 : iss.AddStyleSheet(sheet1);
120 1 : iss.AddStyleSheet(sheet2);
121 1 : iss.AddStyleSheet(sheet3);
122 :
123 2 : std::vector<unsigned> r = iss.FindPositionsByName(name1);
124 2 : CPPUNIT_ASSERT_EQUAL_MESSAGE("Two style sheets are found by 'name1'",
125 1 : 2u, static_cast<unsigned>(r.size()));
126 1 : CPPUNIT_ASSERT_EQUAL(0u, r.at(0));
127 1 : CPPUNIT_ASSERT_EQUAL(2u, r.at(1));
128 :
129 1 : r = iss.FindPositionsByName(name2);
130 2 : CPPUNIT_ASSERT_EQUAL_MESSAGE("One style sheets is found by 'name2'",
131 1 : 1u, static_cast<unsigned>(r.size()));
132 2 : CPPUNIT_ASSERT_EQUAL(1u, r.at(0));
133 1 : }
134 :
135 1 : void IndexedStyleSheetsTest::KnowsThatItStoresAStyleSheet()
136 : {
137 1 : rtl::OUString name1("name1");
138 2 : rtl::OUString name2("name2");
139 2 : rtl::Reference<SfxStyleSheetBase> sheet1(new MockedStyleSheet(name1));
140 2 : rtl::Reference<SfxStyleSheetBase> sheet2(new MockedStyleSheet(name1));
141 2 : rtl::Reference<SfxStyleSheetBase> sheet3(new MockedStyleSheet(name2));
142 2 : rtl::Reference<SfxStyleSheetBase> sheet4(new MockedStyleSheet(name1));
143 2 : IndexedStyleSheets iss;
144 1 : iss.AddStyleSheet(sheet1);
145 1 : iss.AddStyleSheet(sheet2);
146 1 : iss.AddStyleSheet(sheet3);
147 : // do not add sheet 4
148 :
149 2 : CPPUNIT_ASSERT_EQUAL_MESSAGE("Finds first stored style sheet even though two style sheets have the same name.",
150 1 : true, iss.HasStyleSheet(sheet1));
151 2 : CPPUNIT_ASSERT_EQUAL_MESSAGE("Finds second stored style sheet even though two style sheets have the same name.",
152 1 : true, iss.HasStyleSheet(sheet2));
153 2 : CPPUNIT_ASSERT_EQUAL_MESSAGE("Does not find style sheet which is not stored and has the same name as a stored.",
154 2 : false, iss.HasStyleSheet(sheet4));
155 :
156 1 : }
157 :
158 1 : CPPUNIT_TEST_SUITE_REGISTRATION(IndexedStyleSheetsTest);
159 :
160 4 : CPPUNIT_PLUGIN_IMPLEMENT();
|