Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / mod / _sw / CharacterStyle.java
blobeb74ef78f2670584f6c3a05c61a13f4a3d3ab2a9
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 com.sun.star.beans.PropertyAttribute;
22 import com.sun.star.beans.XPropertySet;
23 import com.sun.star.container.XIndexAccess;
24 import com.sun.star.container.XNameAccess;
25 import com.sun.star.container.XNameContainer;
26 import com.sun.star.lang.XMultiServiceFactory;
27 import com.sun.star.style.XStyle;
28 import com.sun.star.style.XStyleFamiliesSupplier;
29 import com.sun.star.text.XText;
30 import com.sun.star.text.XTextCursor;
31 import com.sun.star.text.XTextDocument;
32 import com.sun.star.uno.UnoRuntime;
33 import com.sun.star.uno.XInterface;
34 import java.io.PrintWriter;
35 import lib.StatusException;
36 import lib.TestCase;
37 import lib.TestEnvironment;
38 import lib.TestParameters;
39 import util.DesktopTools;
40 import util.SOfficeFactory;
41 import util.utils;
43 /**
44 * Test for object which is represented by service
45 * <code>com.sun.star.style.CharacterStyle</code>. <p>
46 * @see com.sun.star.style.CharacterStyle
48 public class CharacterStyle extends TestCase {
49 private XTextDocument xTextDoc;
51 /**
52 * Creates text document.
54 @Override
55 protected void initialize( TestParameters tParam, PrintWriter log ) {
56 SOfficeFactory SOF = SOfficeFactory.getFactory( tParam.getMSF() );
57 try {
58 log.println( "creating a textdocument" );
59 xTextDoc = SOF.createTextDoc( null );
60 } catch ( com.sun.star.uno.Exception e ) {
61 e.printStackTrace( log );
62 throw new StatusException( "Couldn't create document", e );
66 /**
67 * Disposes text document.
69 @Override
70 protected void cleanup( TestParameters tParam, PrintWriter log ) {
71 log.println( " disposing xTextDoc " );
72 DesktopTools.closeDoc(xTextDoc);
75 /**
76 * Creating a Testenvironment for the interfaces to be tested.
77 * At first style families are gotten from a text document using
78 * <code>XStyleFamiliesSupplier</code> interface, then family indexed '0' is
79 * gotten from this style family using <code>XIndexAccess</code> interface.
80 * Next, method creates an instance of the service
81 * <code>com.sun.star.style.CharacterStyle</code> and inserts it to a
82 * previously obtained style family using <code>XNameContainer</code>
83 * interface. Finally, method creates a cursor of a major text of text
84 * document and sets it's property 'CharStyleName' value to the name of
85 * previously created style's name.
86 * Object relations created :
87 * <ul>
88 * <li> <code>'PoolStyle'</code> for
89 * {@link ifc.style._XStyle} : style indexed '10' obtained from
90 * StyleFamily indexed '0' from text document using
91 * <code>XIndexAccess</code> interface.</li>
92 * </ul>
94 @Override
95 protected TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
97 TestEnvironment tEnv = null;
98 XNameAccess oSFNA = null;
99 XStyle oStyle = null;
100 XStyle oMyStyle = null;
102 log.println("creating a test environment");
104 try {
105 log.println("getting style");
106 XStyleFamiliesSupplier oSFS = UnoRuntime.queryInterface(XStyleFamiliesSupplier.class,
107 xTextDoc);
108 XNameAccess oSF = oSFS.getStyleFamilies();
109 XIndexAccess oSFsIA = UnoRuntime.queryInterface(XIndexAccess.class, oSF);
110 oSFNA = UnoRuntime.queryInterface(
111 XNameAccess.class,oSFsIA.getByIndex(0));
112 XIndexAccess oSFIA = UnoRuntime.queryInterface(XIndexAccess.class, oSFNA);
113 oStyle = UnoRuntime.queryInterface(
114 XStyle.class,oSFIA.getByIndex(0));
115 } catch ( com.sun.star.lang.WrappedTargetException e ) {
116 log.println("Error: exception occurred.");
117 e.printStackTrace(log);
118 throw new StatusException( "Couldn't create environment ", e );
119 } catch ( com.sun.star.lang.IndexOutOfBoundsException e ) {
120 log.println("Error: exception occurred.");
121 e.printStackTrace(log);
122 throw new StatusException( "Couldn't create environment ", e );
125 try {
126 log.print("Creating a user-defined style... ");
127 XMultiServiceFactory oMSF = UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDoc);
128 XInterface oInt = (XInterface)
129 oMSF.createInstance("com.sun.star.style.CharacterStyle");
130 oMyStyle = UnoRuntime.queryInterface(XStyle.class, oInt);
131 } catch ( com.sun.star.uno.Exception e ) {
132 log.println("Error: exception occurred.");
133 e.printStackTrace(log);
134 throw new StatusException( "Couldn't create environment ", e );
138 if (oMyStyle == null)
139 log.println("FAILED");
140 else
141 log.println("OK");
142 XNameContainer oSFNC = UnoRuntime.queryInterface(XNameContainer.class, oSFNA);
144 try {
145 if ( oSFNC.hasByName("My Style") )
146 oSFNC.removeByName("My Style");
147 oSFNC.insertByName("My Style", oMyStyle);
148 } catch ( com.sun.star.lang.WrappedTargetException e ) {
149 e.printStackTrace(log);
150 throw new StatusException( "Couldn't create environment ", e );
151 } catch ( com.sun.star.lang.IllegalArgumentException e ) {
152 e.printStackTrace(log);
153 throw new StatusException( "Couldn't create environment ", e );
154 } catch ( com.sun.star.container.NoSuchElementException e ) {
155 e.printStackTrace(log);
156 throw new StatusException( "Couldn't create environment ", e );
157 } catch ( com.sun.star.container.ElementExistException e ) {
158 e.printStackTrace(log);
159 throw new StatusException( "Couldn't create environment ", e );
162 XText oText = xTextDoc.getText();
163 XTextCursor oCursor = oText.createTextCursor();
164 XPropertySet xProp = UnoRuntime.queryInterface(XPropertySet.class, oCursor);
166 try {
167 xProp.setPropertyValue("CharStyleName", oMyStyle.getName());
168 } catch ( com.sun.star.lang.WrappedTargetException e ) {
169 e.printStackTrace( log );
170 throw new StatusException( "Couldn't create environment ", e );
171 } catch ( com.sun.star.lang.IllegalArgumentException e ) {
172 e.printStackTrace( log );
173 throw new StatusException( "Couldn't create environment ", e );
174 } catch ( com.sun.star.beans.PropertyVetoException e ) {
175 e.printStackTrace( log );
176 throw new StatusException( "Couldn't create environment ", e );
177 } catch ( com.sun.star.beans.UnknownPropertyException e ) {
178 e.printStackTrace( log );
179 throw new StatusException( "Couldn't create environment ", e );
182 log.println("creating a new environment for object");
183 tEnv = new TestEnvironment(oMyStyle);
184 tEnv.addObjRelation("PoolStyle", oStyle);
186 XPropertySet xStyleProp = UnoRuntime.queryInterface(XPropertySet.class, oMyStyle);
188 short exclude = PropertyAttribute.READONLY;
189 tEnv.addObjRelation("PropertyNames",utils.getFilteredPropertyNames(xStyleProp, (short)0, exclude));
191 return tEnv;