Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / sw / qa / core / test_ToxLinkProcessor.cxx
blobc4998751a640909544bc5ac0b342ea3c03196a9a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
10 #include <stdexcept>
12 #include <sal/types.h>
14 #include <rtl/ustring.hxx>
16 #include <ToxLinkProcessor.hxx>
18 #include <cppunit/TestAssert.h>
19 #include <cppunit/extensions/HelperMacros.h>
20 #include <cppunit/plugin/TestPlugIn.h>
21 #include <test/bootstrapfixture.hxx>
23 #include <swdll.hxx>
24 #include <swtypes.hxx>
25 #include <SwStyleNameMapper.hxx>
27 using namespace sw;
29 class ToxLinkProcessorTest : public test::BootstrapFixture
31 void NoExceptionIsThrownIfTooManyLinksAreClosed();
32 void AddingAndClosingTwoOverlappingLinksResultsInOneClosedLink();
33 void LinkIsCreatedCorrectly();
34 void LinkSequenceIsPreserved();
36 CPPUNIT_TEST_SUITE(ToxLinkProcessorTest);
37 CPPUNIT_TEST(NoExceptionIsThrownIfTooManyLinksAreClosed);
38 CPPUNIT_TEST(AddingAndClosingTwoOverlappingLinksResultsInOneClosedLink);
39 CPPUNIT_TEST(LinkIsCreatedCorrectly);
40 CPPUNIT_TEST(LinkSequenceIsPreserved);
41 CPPUNIT_TEST_SUITE_END();
42 public:
43 void setUp() override {
44 BootstrapFixture::setUp();
45 SwGlobals::ensure();
48 static const OUString STYLE_NAME_1;
49 static const OUString STYLE_NAME_2;
50 static const sal_uInt16 POOL_ID_1;
51 static const sal_uInt16 POOL_ID_2;
52 static const OUString URL_1;
53 static const OUString URL_2;
56 const OUString ToxLinkProcessorTest::STYLE_NAME_1 = "anyStyle1";
57 const OUString ToxLinkProcessorTest::STYLE_NAME_2 = "anyStyle2";
58 const OUString ToxLinkProcessorTest::URL_1 = "anyUrl1";
59 const OUString ToxLinkProcessorTest::URL_2 = "anyUrl2";
60 const sal_uInt16 ToxLinkProcessorTest::POOL_ID_1 = 42;
61 const sal_uInt16 ToxLinkProcessorTest::POOL_ID_2 = 43;
63 void
64 ToxLinkProcessorTest::NoExceptionIsThrownIfTooManyLinksAreClosed()
66 ToxLinkProcessor sut;
67 sut.StartNewLink(0, STYLE_NAME_1);
68 sut.CloseLink(1, URL_1);
69 // fdo#85872 actually it turns out the UI does something like this
70 // so an exception must not be thrown!
71 // should not succeed either (for backward compatibility)
72 sut.CloseLink(2, URL_1);
73 CPPUNIT_ASSERT_EQUAL(1u, static_cast<unsigned>(sut.m_ClosedLinks.size()));
74 CPPUNIT_ASSERT_EQUAL(1u, static_cast<unsigned>(sut.m_ClosedLinks.at(0)->mEndTextPos));
75 CPPUNIT_ASSERT_MESSAGE("no links are open", !sut.m_pStartedLink);
78 void
79 ToxLinkProcessorTest::AddingAndClosingTwoOverlappingLinksResultsInOneClosedLink()
81 ToxLinkProcessor sut;
82 sut.StartNewLink(0, STYLE_NAME_1);
83 sut.StartNewLink(0, STYLE_NAME_2);
84 sut.CloseLink(1, URL_1);
85 // this should not cause an error, and should not succeed either
86 // (for backward compatibility)
87 sut.CloseLink(1, URL_2);
88 CPPUNIT_ASSERT_EQUAL(1u, static_cast<unsigned>(sut.m_ClosedLinks.size()));
89 CPPUNIT_ASSERT_MESSAGE("no links are open", !sut.m_pStartedLink);
90 // backward compatibility: the last start is closed by the first end
91 CPPUNIT_ASSERT_EQUAL(STYLE_NAME_2, sut.m_ClosedLinks[0]->mINetFormat.GetINetFormat());
92 CPPUNIT_ASSERT_EQUAL(URL_1, sut.m_ClosedLinks[0]->mINetFormat.GetValue());
95 class ToxLinkProcessorWithOverriddenObtainPoolId : public ToxLinkProcessor {
96 public:
97 virtual sal_uInt16
98 ObtainPoolId(const OUString& characterStyle) const override {
99 if (characterStyle == ToxLinkProcessorTest::STYLE_NAME_1) {
100 return ToxLinkProcessorTest::POOL_ID_1;
102 if (characterStyle == ToxLinkProcessorTest::STYLE_NAME_2) {
103 return ToxLinkProcessorTest::POOL_ID_2;
105 return 0;
109 void
110 ToxLinkProcessorTest::LinkIsCreatedCorrectly()
112 // obtainpoolid needs to be overridden to check what we are
113 ToxLinkProcessorWithOverriddenObtainPoolId sut;
115 sut.StartNewLink(0, STYLE_NAME_1);
116 sut.CloseLink(1, URL_1);
118 CPPUNIT_ASSERT_EQUAL_MESSAGE("Style is stored correctly in link", STYLE_NAME_1, sut.m_ClosedLinks.at(0)->mINetFormat.GetVisitedFormat());
119 CPPUNIT_ASSERT_EQUAL_MESSAGE("Url is stored correctly in link", URL_1, sut.m_ClosedLinks.at(0)->mINetFormat.GetValue());
122 void
123 ToxLinkProcessorTest::LinkSequenceIsPreserved()
126 // obtainpoolid needs to be overridden to check what we are
127 ToxLinkProcessorWithOverriddenObtainPoolId sut;
129 sut.StartNewLink(0, STYLE_NAME_2);
130 sut.CloseLink(1, URL_2);
131 sut.StartNewLink(1, STYLE_NAME_1);
132 sut.CloseLink(2, URL_1);
134 // check first closed element
135 CPPUNIT_ASSERT_EQUAL_MESSAGE("Style is stored correctly in link",
136 STYLE_NAME_2, sut.m_ClosedLinks.at(0)->mINetFormat.GetVisitedFormat());
137 CPPUNIT_ASSERT_EQUAL_MESSAGE("Pool id is stored correctly in link",
138 POOL_ID_2, sut.m_ClosedLinks.at(0)->mINetFormat.GetINetFormatId());
139 CPPUNIT_ASSERT_EQUAL_MESSAGE("Url is stored correctly in link",
140 URL_2, sut.m_ClosedLinks.at(0)->mINetFormat.GetValue());
141 // check second closed element
142 CPPUNIT_ASSERT_EQUAL_MESSAGE("Style is stored correctly in link",
143 STYLE_NAME_1, sut.m_ClosedLinks.at(1)->mINetFormat.GetVisitedFormat());
144 CPPUNIT_ASSERT_EQUAL_MESSAGE("Pool id is stored correctly in link",
145 POOL_ID_1, sut.m_ClosedLinks.at(1)->mINetFormat.GetINetFormatId());
146 CPPUNIT_ASSERT_EQUAL_MESSAGE("Url is stored correctly in link",
147 URL_1, sut.m_ClosedLinks.at(1)->mINetFormat.GetValue());
150 // Put the test suite in the registry
151 CPPUNIT_TEST_SUITE_REGISTRATION(ToxLinkProcessorTest);
153 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */