summaryrefslogtreecommitdiff
path: root/sw/qa/api/XDocumentIndexTest.hxx
blob: c181a10ac76fd405123af70975d112c4e58750d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#ifndef INCLUDED_SW_QA_API_XDOCUMENTINDEXTEST_HXX
#define INCLUDED_SW_QA_API_XDOCUMENTINDEXTEST_HXX

#include "ApiTestBase.hxx"

#include <cppunit/TestAssert.h>

#include <test/unoapi_property_testers.hxx>

#include <com/sun/star/text/XTextDocument.hpp>
#include <com/sun/star/text/XTextContent.hpp>
#include <com/sun/star/text/XText.hpp>
#include <com/sun/star/text/XDocumentIndex.hpp>

#include <com/sun/star/lang/XMultiServiceFactory.hpp>

namespace apitest
{
/**
 * Testing <code>com.sun.star.text.XDocumentIndex</code>
 *
 * @see com.sun.star.text.XDocumentIndex
 */
class XDocumentIndexTest : public ApiTestBase
{
public:
    /**
     * Has <b>OK</b> status if the returned service name
     * is equal to 'com.sun.star.text.DocumentIndex'.
     */
    void testGetServiceName()
    {
        auto inputMap = init();

        css::uno::Reference<css::text::XDocumentIndex> xDocumentIndex(
            inputMap["text::XDocumentIndex"], css::uno::UNO_QUERY_THROW);
        CPPUNIT_ASSERT_EQUAL(OUString("com.sun.star.text.DocumentIndex"),
                             xDocumentIndex->getServiceName());
    }

    /**
     * Gets the document from relation and insert a new index mark.
     * Then it stores the text content of document index before
     * update and after.<p>
     *
     * Has <b> OK </b> status if index content is changed and
     * new index contains index mark inserted. <p>
     */
    void testUpdate()
    {
        auto inputMap = init();

        css::uno::Reference<css::text::XDocumentIndex> xDocumentIndex(
            inputMap["text::XDocumentIndex"], css::uno::UNO_QUERY_THROW);
        css::uno::Reference<css::text::XTextDocument> xTextDocument(inputMap["text::XTextDocument"],
                                                                    css::uno::UNO_QUERY_THROW);

        bool bOK = true;
        try
        {
            css::uno::Reference<css::text::XText> xText = xTextDocument->getText();
            css::uno::Reference<css::text::XTextRange> xTextRange = xText->getEnd();
            xTextRange->setString("IndexMark");
            css::uno::Reference<css::lang::XMultiServiceFactory> xFactory(
                xTextDocument, css::uno::UNO_QUERY_THROW);
            css::uno::Reference<css::text::XTextContent> xTextContentMark(
                xFactory->createInstance("com.sun.star.text.DocumentIndexMark"),
                css::uno::UNO_QUERY_THROW);
            xText->insertTextContent(xTextRange, xTextContentMark, true);
        }
        catch (css::uno::Exception /*exception*/)
        {
            bOK = false;
        }

        CPPUNIT_ASSERT_MESSAGE("Couldn't create the document index mark", bOK);

        OUString sContentBefore = xDocumentIndex->getAnchor()->getString();
        xDocumentIndex->update();
        OUString sContentAfter = xDocumentIndex->getAnchor()->getString();

        CPPUNIT_ASSERT_MESSAGE("Before and after shouldn't be equal",
                               sContentBefore != sContentAfter);
        CPPUNIT_ASSERT_MESSAGE("Content after should contain string 'IndexMark'",
                               sContentAfter.indexOf("IndexMark") >= 0);
    }
};
}

#endif

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */