summaryrefslogtreecommitdiff
path: root/sw/qa/complex/writer/CheckFields.java
blob: f92438e59e32614e50811143e88163d50d6a643d (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* -*- 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/.
 */

package complex.writer;

import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XServiceInfo;
import com.sun.star.beans.XPropertySet;
import com.sun.star.beans.PropertyValue;
import com.sun.star.container.XEnumeration;
import com.sun.star.util.XCloseable;
import com.sun.star.text.XText;
import com.sun.star.text.XTextContent;
import com.sun.star.text.XTextDocument;
import com.sun.star.text.XTextField;
import com.sun.star.text.XTextFieldsSupplier;
import com.sun.star.text.XTextRange;
import com.sun.star.text.XTextCursor;

import org.openoffice.test.OfficeConnection;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

import java.util.Set;
import java.util.HashSet;


public class CheckFields
{
    private static final OfficeConnection connection = new OfficeConnection();

    @BeforeClass public static void setUpConnection() throws Exception {
        connection.setUp();
//Thread.sleep(5000);
    }

    @AfterClass public static void tearDownConnection()
        throws InterruptedException, com.sun.star.uno.Exception
    {
        connection.tearDown();
    }

    private XMultiServiceFactory m_xMSF = null;
    private XComponentContext m_xContext = null;
    private XTextDocument m_xDoc = null;

    @Before public void before() throws Exception
    {
        m_xMSF = UnoRuntime.queryInterface(
            XMultiServiceFactory.class,
            connection.getComponentContext().getServiceManager());
        m_xContext = connection.getComponentContext();
        assertNotNull("could not get component context.", m_xContext);
        m_xDoc = util.WriterTools.createTextDoc(m_xMSF);
    }

    @After public void after()
    {
        util.DesktopTools.closeDoc(m_xDoc);
    }

    @Test
    public void test_fdo39694_load() throws Exception
    {
        PropertyValue[] loadProps = new PropertyValue[2];
        loadProps[0] = new PropertyValue();
        loadProps[0].Name = "AsTemplate";
        loadProps[0].Value = new Boolean(true);
        loadProps[1] = new PropertyValue();
        loadProps[1].Name = "Hidden";
        loadProps[1].Value = new Boolean(true);

        String file = TestDocument.getUrl("fdo39694.ott");
        XComponent xComp = util.DesktopTools.loadDoc(m_xMSF, file, loadProps);
        XTextDocument xTextDoc = UnoRuntime.queryInterface(XTextDocument.class, xComp);

        XTextFieldsSupplier xTFS = UnoRuntime.queryInterface(XTextFieldsSupplier.class, xComp);
        XEnumeration xEnum = xTFS.getTextFields().createEnumeration();
        Set<String> placeholders = new HashSet<String>();
        placeholders.add("<Kadr1>");
        placeholders.add("<Kadr2>");
        placeholders.add("<Kadr3>");
        placeholders.add("<Kadr4>");
        placeholders.add("<Pnname>");
        placeholders.add("<Pvname>");
        placeholders.add("<Pgeboren>");

        while (xEnum.hasMoreElements())
        {
            Object xField = xEnum.nextElement();
            XServiceInfo xSI = UnoRuntime.queryInterface(XServiceInfo.class, xField);
            if (xSI.supportsService("com.sun.star.text.TextField.JumpEdit"))
            {
                XTextContent xContent = UnoRuntime.queryInterface(XTextContent.class, xField);
                XTextRange xAnchor = xContent.getAnchor();
                String readContent = xAnchor.getString();
                if (!placeholders.remove(readContent))
                {
                    System.out.println("field not found: " + readContent);
                    assertFalse(true);
                }
            }
        }
        assertTrue(placeholders.isEmpty());
        XCloseable xClos = UnoRuntime.queryInterface(
                    XCloseable.class, xComp);
        if (xClos != null) {
            xClos.close(true);
        }
    }

    @Test
    public void test_fdo42073() throws Exception
    {
        XMultiServiceFactory xDocFactory = UnoRuntime.queryInterface(XMultiServiceFactory.class, m_xDoc);
        Object xField =
            xDocFactory.createInstance("com.sun.star.text.TextField.Input");

        XText xBodyText = m_xDoc.getText();
        XTextCursor xCursor = xBodyText.createTextCursor();
        XTextField xTextField = UnoRuntime.queryInterface(XTextField.class, xField);
        xBodyText.insertTextContent(xCursor, xTextField, true);

        XPropertySet xPropSet = UnoRuntime.queryInterface(XPropertySet.class, xField);
        {
            Object readContent = xPropSet.getPropertyValue("Content");
            assertEquals("", readContent);
        }

        String content = "this is not surprising";
        xPropSet.setPropertyValue("Content", content);
        Object readContent = xPropSet.getPropertyValue("Content");
        assertEquals(content, readContent);
        readContent = xTextField.getPresentation(false);
        assertEquals(content, readContent);
    }
}

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