Get the style color and number just once
[LibreOffice.git] / sw / qa / core / test_ToxLinkProcessor.cxx
bloba47762912c6c0f4d3efd44c3badcea6cde54611b
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 <sal/types.h>
12 #include <rtl/ustring.hxx>
14 #include <ToxLinkProcessor.hxx>
16 #include <cppunit/TestAssert.h>
17 #include <cppunit/extensions/HelperMacros.h>
18 #include <test/bootstrapfixture.hxx>
20 #include <swdll.hxx>
22 using namespace sw;
24 class ToxLinkProcessorTest : public test::BootstrapFixture
26 void NoExceptionIsThrownIfTooManyLinksAreClosed();
27 void AddingAndClosingTwoOverlappingLinksResultsInOneClosedLink();
28 void LinkIsCreatedCorrectly();
29 void LinkSequenceIsPreserved();
31 CPPUNIT_TEST_SUITE(ToxLinkProcessorTest);
32 CPPUNIT_TEST(NoExceptionIsThrownIfTooManyLinksAreClosed);
33 CPPUNIT_TEST(AddingAndClosingTwoOverlappingLinksResultsInOneClosedLink);
34 CPPUNIT_TEST(LinkIsCreatedCorrectly);
35 CPPUNIT_TEST(LinkSequenceIsPreserved);
36 CPPUNIT_TEST_SUITE_END();
37 public:
38 void setUp() override {
39 BootstrapFixture::setUp();
40 SwGlobals::ensure();
43 static constexpr OUString STYLE_NAME_1 = u"anyStyle1"_ustr;
44 static constexpr OUString STYLE_NAME_2 = u"anyStyle2"_ustr;
45 static const sal_uInt16 POOL_ID_1;
46 static const sal_uInt16 POOL_ID_2;
47 static constexpr OUString URL_1 = u"#anyUrl1"_ustr;
48 static constexpr OUString URL_2 = u"#anyUrl2"_ustr;
51 const sal_uInt16 ToxLinkProcessorTest::POOL_ID_1 = 42;
52 const sal_uInt16 ToxLinkProcessorTest::POOL_ID_2 = 43;
54 void
55 ToxLinkProcessorTest::NoExceptionIsThrownIfTooManyLinksAreClosed()
57 ToxLinkProcessor sut;
58 sut.StartNewLink(0, STYLE_NAME_1);
59 sut.CloseLink(1, URL_1, /*bRelative=*/true);
60 // fdo#85872 actually it turns out the UI does something like this
61 // so an exception must not be thrown!
62 // should not succeed either (for backward compatibility)
63 sut.CloseLink(2, URL_1, /*bRelative=*/true);
64 CPPUNIT_ASSERT_EQUAL(1u, static_cast<unsigned>(sut.m_ClosedLinks.size()));
65 CPPUNIT_ASSERT_EQUAL(1u, static_cast<unsigned>(sut.m_ClosedLinks.at(0)->mEndTextPos));
66 CPPUNIT_ASSERT_MESSAGE("no links are open", !sut.m_oStartedLink);
69 void
70 ToxLinkProcessorTest::AddingAndClosingTwoOverlappingLinksResultsInOneClosedLink()
72 ToxLinkProcessor sut;
73 sut.StartNewLink(0, STYLE_NAME_1);
74 sut.StartNewLink(0, STYLE_NAME_2);
75 sut.CloseLink(1, URL_1, /*bRelative=*/true);
76 // this should not cause an error, and should not succeed either
77 // (for backward compatibility)
78 sut.CloseLink(1, URL_2, /*bRelative=*/true);
79 CPPUNIT_ASSERT_EQUAL(1u, static_cast<unsigned>(sut.m_ClosedLinks.size()));
80 CPPUNIT_ASSERT_MESSAGE("no links are open", !sut.m_oStartedLink);
81 // backward compatibility: the last start is closed by the first end
82 CPPUNIT_ASSERT_EQUAL(STYLE_NAME_2, sut.m_ClosedLinks[0]->mINetFormat.GetINetFormat());
83 CPPUNIT_ASSERT_EQUAL(URL_1, sut.m_ClosedLinks[0]->mINetFormat.GetValue());
86 namespace {
88 class ToxLinkProcessorWithOverriddenObtainPoolId : public ToxLinkProcessor {
89 public:
90 virtual sal_uInt16
91 ObtainPoolId(const OUString& characterStyle) const override {
92 if (characterStyle == ToxLinkProcessorTest::STYLE_NAME_1) {
93 return ToxLinkProcessorTest::POOL_ID_1;
95 if (characterStyle == ToxLinkProcessorTest::STYLE_NAME_2) {
96 return ToxLinkProcessorTest::POOL_ID_2;
98 return 0;
104 void
105 ToxLinkProcessorTest::LinkIsCreatedCorrectly()
107 // obtainpoolid needs to be overridden to check what we are
108 ToxLinkProcessorWithOverriddenObtainPoolId sut;
110 sut.StartNewLink(0, STYLE_NAME_1);
111 sut.CloseLink(1, URL_1, /*bRelative=*/true);
113 CPPUNIT_ASSERT_EQUAL_MESSAGE("Style is stored correctly in link", STYLE_NAME_1, sut.m_ClosedLinks.at(0)->mINetFormat.GetVisitedFormat());
114 CPPUNIT_ASSERT_EQUAL_MESSAGE("Url is stored correctly in link", URL_1, sut.m_ClosedLinks.at(0)->mINetFormat.GetValue());
117 void
118 ToxLinkProcessorTest::LinkSequenceIsPreserved()
121 // obtainpoolid needs to be overridden to check what we are
122 ToxLinkProcessorWithOverriddenObtainPoolId sut;
124 sut.StartNewLink(0, STYLE_NAME_2);
125 sut.CloseLink(1, URL_2, /*bRelative=*/true);
126 sut.StartNewLink(1, STYLE_NAME_1);
127 sut.CloseLink(2, URL_1, /*bRelative=*/true);
129 // check first closed element
130 CPPUNIT_ASSERT_EQUAL_MESSAGE("Style is stored correctly in link",
131 STYLE_NAME_2, sut.m_ClosedLinks.at(0)->mINetFormat.GetVisitedFormat());
132 CPPUNIT_ASSERT_EQUAL_MESSAGE("Pool id is stored correctly in link",
133 POOL_ID_2, sut.m_ClosedLinks.at(0)->mINetFormat.GetINetFormatId());
134 CPPUNIT_ASSERT_EQUAL_MESSAGE("Url is stored correctly in link",
135 URL_2, sut.m_ClosedLinks.at(0)->mINetFormat.GetValue());
136 // check second closed element
137 CPPUNIT_ASSERT_EQUAL_MESSAGE("Style is stored correctly in link",
138 STYLE_NAME_1, sut.m_ClosedLinks.at(1)->mINetFormat.GetVisitedFormat());
139 CPPUNIT_ASSERT_EQUAL_MESSAGE("Pool id is stored correctly in link",
140 POOL_ID_1, sut.m_ClosedLinks.at(1)->mINetFormat.GetINetFormatId());
141 CPPUNIT_ASSERT_EQUAL_MESSAGE("Url is stored correctly in link",
142 URL_1, sut.m_ClosedLinks.at(1)->mINetFormat.GetValue());
145 // Put the test suite in the registry
146 CPPUNIT_TEST_SUITE_REGISTRATION(ToxLinkProcessorTest);
148 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */