Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / mod / _sw / SwXTextSection.java
blobbb6a5804b56a26ac810efc481067679a02de0147
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 .
19 package mod._sw;
21 import java.io.PrintWriter;
23 import lib.StatusException;
24 import lib.TestCase;
25 import lib.TestEnvironment;
26 import lib.TestParameters;
27 import util.InstCreator;
28 import util.SOfficeFactory;
29 import util.TableDsc;
31 import com.sun.star.container.XIndexAccess;
32 import com.sun.star.container.XNameAccess;
33 import com.sun.star.container.XNamed;
34 import com.sun.star.lang.XComponent;
35 import com.sun.star.lang.XMultiServiceFactory;
36 import com.sun.star.text.XText;
37 import com.sun.star.text.XTextColumns;
38 import com.sun.star.text.XTextContent;
39 import com.sun.star.text.XTextCursor;
40 import com.sun.star.text.XTextDocument;
41 import com.sun.star.text.XTextSection;
42 import com.sun.star.text.XTextSectionsSupplier;
43 import com.sun.star.text.XWordCursor;
44 import com.sun.star.uno.AnyConverter;
45 import com.sun.star.uno.Type;
46 import com.sun.star.uno.UnoRuntime;
47 import com.sun.star.uno.XInterface;
49 /**
51 * initial description
52 * @see com.sun.star.text.XText
56 public class SwXTextSection extends TestCase {
57 XTextDocument xTextDoc;
59 @Override
60 protected void initialize( TestParameters tParam, PrintWriter log ) {
61 SOfficeFactory SOF = SOfficeFactory.getFactory( tParam.getMSF() );
63 try {
64 log.println( "creating a textdocument" );
65 xTextDoc = SOF.createTextDoc( null );
66 } catch ( com.sun.star.uno.Exception e ) {
67 // Some exception occurs.FAILED
68 e.printStackTrace( log );
69 throw new StatusException( "Couldn't create document", e );
73 @Override
74 protected void cleanup( TestParameters tParam, PrintWriter log ) {
75 log.println( " disposing xTextDoc " );
76 util.DesktopTools.closeDoc(xTextDoc);
79 /**
80 * creating a Testenvironment for the interfaces to be tested
82 @Override
83 protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
85 XInterface oObj = null;
86 XInterface oTS = null;
87 XTextSection xTS = null;
88 XText oText = null;
89 XTextColumns TC = null;
90 Object instance = null;
92 log.println( "creating a test environment" );
94 oText = xTextDoc.getText();
95 XTextCursor oCursor = oText.createTextCursor();
97 XMultiServiceFactory oDocMSF = UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDoc);
99 try {
100 XTextSectionsSupplier oTSSupp = UnoRuntime.queryInterface
101 ( XTextSectionsSupplier.class, xTextDoc );
102 XNameAccess oTSSuppName = oTSSupp.getTextSections();
104 //cleanup if necessary
105 if (oTSSuppName.hasByName("SwXTextSection")) {
106 XTextSection old = (XTextSection) AnyConverter.toObject(
107 new Type(XTextSection.class),
108 oTSSuppName.getByName("SwXTextSection"));
109 XComponent oldC = UnoRuntime.queryInterface(XComponent.class,old);
110 oldC.dispose();
111 oText.setString("");
114 //insert two sections parent and child
115 oTS = (XInterface) oDocMSF.createInstance
116 ("com.sun.star.text.TextSection");
117 instance = oDocMSF.createInstance("com.sun.star.text.TextSection");
118 XTextContent oTSC = UnoRuntime.queryInterface(XTextContent.class, oTS);
119 oText.insertTextContent(oCursor, oTSC, false);
120 XWordCursor oWordC = UnoRuntime.queryInterface(XWordCursor.class, oCursor);
121 oCursor.setString("End of TextSection");
122 oCursor.gotoStart(false);
123 oCursor.setString("Start of TextSection ");
124 oWordC.gotoEndOfWord(false);
125 XInterface oTS2 = (XInterface) oDocMSF.createInstance
126 ("com.sun.star.text.TextSection");
127 oTSC = UnoRuntime.queryInterface(XTextContent.class, oTS2);
128 oText.insertTextContent(oCursor, oTSC, false);
130 XIndexAccess oTSSuppIndex = UnoRuntime.queryInterface(XIndexAccess.class, oTSSuppName);
132 log.println( "getting a TextSection with the XTextSectionSupplier()" );
133 xTS = (XTextSection) AnyConverter.toObject(
134 new Type(XTextSection.class),oTSSuppIndex.getByIndex(0));
135 XNamed xTSName = UnoRuntime.queryInterface( XNamed.class, xTS);
136 xTSName.setName("SwXTextSection");
138 catch(Exception e){
139 System.out.println("Couldn't get Textsection " + e);
142 oObj = xTS;
144 log.println( "creating a new environment for TextSection object" );
145 TestEnvironment tEnv = new TestEnvironment( oObj );
147 log.println( "adding InstDescriptor object" );
148 TableDsc tDsc = new TableDsc( 6, 4 );
150 log.println( "adding InstCreator object" );
151 tEnv.addObjRelation( "XTEXTINFO", new InstCreator( xTextDoc, tDsc ) );
153 tEnv.addObjRelation("TRO",Boolean.TRUE);
155 try {
156 TC = UnoRuntime.queryInterface(XTextColumns.class,
157 oDocMSF.createInstance("com.sun.star.text.TextColumns"));
158 } catch ( com.sun.star.uno.Exception e ) {
159 e.printStackTrace(log);
160 throw new StatusException
161 ("Couldn't create instance of service TextColumns", e );
163 tEnv.addObjRelation("TC",TC);
165 tEnv.addObjRelation("CONTENT", UnoRuntime.queryInterface(XTextContent.class,instance));
166 tEnv.addObjRelation("RANGE", xTextDoc.getText().createTextCursor());
168 return tEnv;
169 } // finish method getTestEnvironment
171 }// finish class SwXTextSection