bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / tests / java / mod / _sw / SwXTextDocument.java
blob829e1d6853d60323d9ee7db2e4d06cafd20df58e
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 package mod._sw;
20 import com.sun.star.container.XIndexAccess;
21 import com.sun.star.container.XNameAccess;
22 import com.sun.star.container.XNamed;
23 import com.sun.star.frame.XController;
24 import com.sun.star.frame.XModel;
25 import com.sun.star.lang.XMultiServiceFactory;
26 import com.sun.star.text.ControlCharacter;
27 import com.sun.star.text.XText;
28 import com.sun.star.text.XTextContent;
29 import com.sun.star.text.XTextCursor;
30 import com.sun.star.text.XTextDocument;
31 import com.sun.star.text.XTextFrame;
32 import com.sun.star.text.XTextSection;
33 import com.sun.star.text.XTextSectionsSupplier;
34 import com.sun.star.text.XTextTable;
35 import com.sun.star.text.XWordCursor;
36 import com.sun.star.uno.UnoRuntime;
37 import com.sun.star.uno.XInterface;
38 import com.sun.star.view.XSelectionSupplier;
40 import java.io.PrintWriter;
42 import lib.StatusException;
43 import lib.TestCase;
44 import lib.TestEnvironment;
45 import lib.TestParameters;
46 import util.DesktopTools;
48 import util.SOfficeFactory;
49 import util.WriterTools;
52 public class SwXTextDocument extends TestCase {
53 XTextDocument xTextDoc;
54 XTextDocument xSecondTextDoc;
56 protected void initialize(TestParameters tParam, PrintWriter log) {
59 protected void cleanup(TestParameters tParam, PrintWriter log) {
60 log.println(" disposing xTextDoc ");
61 DesktopTools.closeDoc(xSecondTextDoc);
62 DesktopTools.closeDoc(xTextDoc);
65 /**
66 * creating a Testenvironment for the interfaces to be tested
68 public synchronized TestEnvironment createTestEnvironment(TestParameters Param,
69 PrintWriter log)
70 throws StatusException {
71 TestEnvironment tEnv = null;
72 XTextTable the_table = null;
74 try {
75 log.println("creating a textdocument");
76 xTextDoc = WriterTools.createTextDoc(
77 (XMultiServiceFactory) Param.getMSF());
79 XText oText = xTextDoc.getText();
80 XTextCursor oCursor = oText.createTextCursor();
82 log.println("inserting some lines");
84 try {
85 for (int i = 0; i < 5; i++) {
86 oText.insertString(oCursor, "Paragraph Number: " + i,
87 false);
88 oText.insertString(oCursor,
89 " The quick brown fox jumps over the lazy Dog: SwXParagraph",
90 false);
91 oText.insertControlCharacter(oCursor,
92 ControlCharacter.PARAGRAPH_BREAK,
93 false);
94 oText.insertString(oCursor,
95 "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG: SwXParagraph",
96 false);
97 oText.insertControlCharacter(oCursor,
98 ControlCharacter.PARAGRAPH_BREAK,
99 false);
100 oText.insertControlCharacter(oCursor,
101 ControlCharacter.LINE_BREAK,
102 false);
104 for (int i = 0; i < 11; i++) {
105 oText.insertString(oCursor, "xTextDoc ", false);
108 } catch (com.sun.star.lang.IllegalArgumentException e) {
109 e.printStackTrace(log);
110 throw new StatusException("Couldn't insert lines", e);
113 //insert two sections parent and child
114 XMultiServiceFactory oDocMSF = UnoRuntime.queryInterface(
115 XMultiServiceFactory.class,
116 xTextDoc);
118 XInterface oTS;
119 XTextSection xTS;
121 XTextSectionsSupplier oTSSupp = UnoRuntime.queryInterface(
122 XTextSectionsSupplier.class,
123 xTextDoc);
124 XNameAccess oTSSuppName = oTSSupp.getTextSections();
126 oTS = (XInterface) oDocMSF.createInstance(
127 "com.sun.star.text.TextSection");
129 XTextContent oTSC = UnoRuntime.queryInterface(
130 XTextContent.class, oTS);
131 oText.insertTextContent(oCursor, oTSC, false);
133 XWordCursor oWordC = UnoRuntime.queryInterface(
134 XWordCursor.class, oCursor);
135 oCursor.setString("End of TextSection");
136 oCursor.gotoStart(false);
137 oCursor.setString("Start of TextSection ");
138 oWordC.gotoEndOfWord(false);
140 XInterface oTS2 = (XInterface) oDocMSF.createInstance(
141 "com.sun.star.text.TextSection");
142 oTSC = UnoRuntime.queryInterface(XTextContent.class,
143 oTS2);
144 oText.insertTextContent(oCursor, oTSC, false);
146 XIndexAccess oTSSuppIndex = UnoRuntime.queryInterface(
147 XIndexAccess.class,
148 oTSSuppName);
149 log.println(
150 "getting a TextSection with the XTextSectionSupplier()");
151 xTS = UnoRuntime.queryInterface(XTextSection.class,
152 oTSSuppIndex.getByIndex(0));
154 XNamed xTSName = UnoRuntime.queryInterface(XNamed.class,
155 xTS);
156 xTSName.setName("SwXTextSection");
158 log.println(" adding TextTable");
159 the_table = SOfficeFactory.createTextTable(xTextDoc, 6, 4);
161 XNamed the_name = UnoRuntime.queryInterface(XNamed.class,
162 the_table);
163 the_name.setName("SwXTextDocument");
164 SOfficeFactory.insertTextContent(xTextDoc,
165 the_table);
167 log.println(" adding ReferenceMark");
169 XInterface aMark = (XInterface) oDocMSF.createInstance(
170 "com.sun.star.text.ReferenceMark");
171 the_name = UnoRuntime.queryInterface(XNamed.class, aMark);
172 the_name.setName("SwXTextDocument");
174 XTextContent oTC = UnoRuntime.queryInterface(
175 XTextContent.class, aMark);
176 SOfficeFactory.insertTextContent(xTextDoc, oTC);
178 log.println(" adding TextGraphic");
179 WriterTools.insertTextGraphic(xTextDoc, oDocMSF, 5200, 4200, 4400,
180 4000, "space-metal.jpg",
181 "SwXTextDocument");
183 log.println(" adding EndNote");
185 XInterface aEndNote = (XInterface) oDocMSF.createInstance(
186 "com.sun.star.text.Endnote");
187 oTC = UnoRuntime.queryInterface(XTextContent.class,
188 aEndNote);
189 SOfficeFactory.insertTextContent(xTextDoc, oTC);
191 log.println(" adding Bookmark");
192 SOfficeFactory.insertTextContent(xTextDoc,
193 SOfficeFactory.createBookmark(
194 xTextDoc));
196 log.println(" adding DocumentIndex");
197 oTC = SOfficeFactory.createIndex(xTextDoc,
198 "com.sun.star.text.DocumentIndex");
199 SOfficeFactory.insertTextContent(xTextDoc, oTC);
201 log.println(" adding TextFrame");
203 XTextFrame frame = SOfficeFactory.createTextFrame(xTextDoc, 500,
204 500);
205 oTC = UnoRuntime.queryInterface(XTextContent.class,
206 frame);
207 SOfficeFactory.insertTextContent(xTextDoc, oTC);
209 log.println("creating a second textdocument");
210 xSecondTextDoc = WriterTools.createTextDoc(
211 (XMultiServiceFactory) Param.getMSF());
212 } catch (com.sun.star.uno.Exception e) {
213 // Some exception occurs.FAILED
214 e.printStackTrace(log);
215 throw new StatusException("Couldn't create document", e);
218 if (xTextDoc != null) {
219 log.println("Creating instance...");
221 XText oText = xTextDoc.getText();
222 XTextCursor oTextCursor = oText.createTextCursor();
224 for (int i = 0; i < 11; i++) {
225 oText.insertString(oTextCursor, "xTextDoc ", false);
228 tEnv = new TestEnvironment(xTextDoc);
229 } else {
230 log.println("Failed to create instance.");
232 return tEnv;
235 XModel model1 = UnoRuntime.queryInterface(XModel.class,
236 xTextDoc);
237 XModel model2 = UnoRuntime.queryInterface(XModel.class,
238 xSecondTextDoc);
240 XController cont1 = model1.getCurrentController();
241 XController cont2 = model2.getCurrentController();
243 cont1.getFrame().setName("cont1");
244 cont2.getFrame().setName("cont2");
246 XSelectionSupplier sel = UnoRuntime.queryInterface(
247 XSelectionSupplier.class, cont1);
249 log.println("Adding SelectionSupplier and Shape to select for XModel");
250 tEnv.addObjRelation("SELSUPP", sel);
251 tEnv.addObjRelation("TOSELECT", the_table);
253 log.println("adding Controller as ObjRelation for XModel");
254 tEnv.addObjRelation("CONT1", cont1);
255 tEnv.addObjRelation("CONT2", cont2);
257 return tEnv;
259 } // finish class SwXTextDocument