Branch libreoffice-5-0-4
[LibreOffice.git] / sw / qa / core / test_ToxLinkProcessor.cxx
blobe768c210ce2b4ae9fc69aa301a97f88884b9fa71
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>
25 using namespace sw;
27 class ToxLinkProcessorTest : public test::BootstrapFixture
29 void ExceptionIsThrownIfTooManyLinksAreClosed();
30 void AddingAndClosingTwoLinksResultsInTwoClosedLinks();
31 void LinkIsCreatedCorrectly();
32 void LinkSequenceIsPreserved();
34 CPPUNIT_TEST_SUITE(ToxLinkProcessorTest);
35 CPPUNIT_TEST(ExceptionIsThrownIfTooManyLinksAreClosed);
36 CPPUNIT_TEST(AddingAndClosingTwoLinksResultsInTwoClosedLinks);
37 CPPUNIT_TEST(LinkIsCreatedCorrectly);
38 CPPUNIT_TEST(LinkSequenceIsPreserved);
39 CPPUNIT_TEST_SUITE_END();
40 public:
41 void setUp() SAL_OVERRIDE {
42 BootstrapFixture::setUp();
43 SwGlobals::ensure();
46 static const OUString STYLE_NAME_1;
47 static const OUString STYLE_NAME_2;
48 static const sal_uInt16 POOL_ID_1;
49 static const sal_uInt16 POOL_ID_2;
50 static const OUString URL_1;
51 static const OUString URL_2;
54 const OUString ToxLinkProcessorTest::STYLE_NAME_1 = "anyStyle1";
55 const OUString ToxLinkProcessorTest::STYLE_NAME_2 = "anyStyle2";
56 const OUString ToxLinkProcessorTest::URL_1 = "anyUrl1";
57 const OUString ToxLinkProcessorTest::URL_2 = "anyUrl2";
58 const sal_uInt16 ToxLinkProcessorTest::POOL_ID_1 = 42;
59 const sal_uInt16 ToxLinkProcessorTest::POOL_ID_2 = 43;
61 void
62 ToxLinkProcessorTest::ExceptionIsThrownIfTooManyLinksAreClosed()
64 ToxLinkProcessor sut;
65 sut.StartNewLink(0, STYLE_NAME_1);
66 sut.CloseLink(1, URL_1);
67 // fdo#85872 actually it turns out the UI does something like this
68 // so an exception must not be thrown!
69 sut.CloseLink(1, URL_1);
72 void
73 ToxLinkProcessorTest::AddingAndClosingTwoLinksResultsInTwoClosedLinks()
75 ToxLinkProcessor sut;
76 sut.StartNewLink(0, STYLE_NAME_1);
77 sut.StartNewLink(0, STYLE_NAME_2);
78 sut.CloseLink(1, URL_1);
79 sut.CloseLink(1, URL_2);
80 CPPUNIT_ASSERT_EQUAL(2u, static_cast<unsigned>(sut.mClosedLinks.size()));
81 CPPUNIT_ASSERT_MESSAGE("no links are open", sut.mStartedLinks.empty());
84 class ToxLinkProcessorWithOverriddenObtainPoolId : public ToxLinkProcessor {
85 public:
86 virtual sal_uInt16
87 ObtainPoolId(const OUString& characterStyle) const SAL_OVERRIDE {
88 if (characterStyle == ToxLinkProcessorTest::STYLE_NAME_1) {
89 return ToxLinkProcessorTest::POOL_ID_1;
91 if (characterStyle == ToxLinkProcessorTest::STYLE_NAME_2) {
92 return ToxLinkProcessorTest::POOL_ID_2;
94 return 0;
98 void
99 ToxLinkProcessorTest::LinkIsCreatedCorrectly()
101 // obtainpoolid needs to be overridden to check what we are
102 ToxLinkProcessorWithOverriddenObtainPoolId sut;
104 sut.StartNewLink(0, STYLE_NAME_1);
105 sut.CloseLink(1, URL_1);
107 CPPUNIT_ASSERT_EQUAL_MESSAGE("Style is stored correctly in link", STYLE_NAME_1, sut.mClosedLinks.at(0).mINetFormat.GetVisitedFormat());
108 CPPUNIT_ASSERT_EQUAL_MESSAGE("Url is stored correctly in link", URL_1, sut.mClosedLinks.at(0).mINetFormat.GetValue());
111 void
112 ToxLinkProcessorTest::LinkSequenceIsPreserved()
115 // obtainpoolid needs to be overridden to check what we are
116 ToxLinkProcessorWithOverriddenObtainPoolId sut;
118 sut.StartNewLink(0, STYLE_NAME_1);
119 sut.StartNewLink(0, STYLE_NAME_2);
120 sut.CloseLink(1, URL_2);
121 sut.CloseLink(1, URL_1);
123 // check first closed element
124 CPPUNIT_ASSERT_EQUAL_MESSAGE("Style is stored correctly in link",
125 STYLE_NAME_2, sut.mClosedLinks.at(0).mINetFormat.GetVisitedFormat());
126 CPPUNIT_ASSERT_EQUAL_MESSAGE("Pool id is stored correctly in link",
127 POOL_ID_2, sut.mClosedLinks.at(0).mINetFormat.GetINetFormatId());
128 CPPUNIT_ASSERT_EQUAL_MESSAGE("Url is stored correctly in link",
129 URL_2, sut.mClosedLinks.at(0).mINetFormat.GetValue());
130 // check second closed element
131 CPPUNIT_ASSERT_EQUAL_MESSAGE("Style is stored correctly in link",
132 STYLE_NAME_1, sut.mClosedLinks.at(1).mINetFormat.GetVisitedFormat());
133 CPPUNIT_ASSERT_EQUAL_MESSAGE("Pool id is stored correctly in link",
134 POOL_ID_1, sut.mClosedLinks.at(1).mINetFormat.GetINetFormatId());
135 CPPUNIT_ASSERT_EQUAL_MESSAGE("Url is stored correctly in link",
136 URL_1, sut.mClosedLinks.at(1).mINetFormat.GetValue());
139 // Put the test suite in the registry
140 CPPUNIT_TEST_SUITE_REGISTRATION(ToxLinkProcessorTest);
142 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */