Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / mod / _sw / ConditionalParagraphStyle.java
blobee7f2f4e4d276bd772888a54ad45fcf017bfbd7b
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.beans.PropertyAttribute;
21 import com.sun.star.beans.XPropertySet;
22 import com.sun.star.container.XIndexAccess;
23 import com.sun.star.container.XNameAccess;
24 import com.sun.star.container.XNameContainer;
25 import com.sun.star.lang.XMultiServiceFactory;
26 import com.sun.star.style.XStyle;
27 import com.sun.star.style.XStyleFamiliesSupplier;
28 import com.sun.star.text.XText;
29 import com.sun.star.text.XTextCursor;
30 import com.sun.star.text.XTextDocument;
31 import com.sun.star.uno.UnoRuntime;
32 import com.sun.star.uno.XInterface;
33 import java.io.PrintWriter;
34 import lib.Status;
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.ConditionalParagraphStyle</code>. <p>
46 * @see com.sun.star.style.ConditionalParagraphStyle
48 public class ConditionalParagraphStyle extends TestCase {
50 XTextDocument xTextDoc;
51 SOfficeFactory SOF = null;
53 /**
54 * Creates text document.
56 @Override
57 protected void initialize( TestParameters tParam, PrintWriter log ) {
58 SOF = SOfficeFactory.getFactory( tParam.getMSF() );
59 try {
60 log.println( "creating a textdocument" );
61 xTextDoc = SOF.createTextDoc( null );
62 } catch ( com.sun.star.uno.Exception e ) {
63 e.printStackTrace( log );
64 throw new StatusException( "Couldn't create document", e );
68 /**
69 * Disposes text document.
71 @Override
72 protected void cleanup( TestParameters tParam, PrintWriter log ) {
73 log.println( " disposing xTextDoc " );
74 DesktopTools.closeDoc(xTextDoc);
77 /**
78 * Create a new instance of the conditional
79 * paragraph style, and insert it into the document.
80 * @see com.sun.star.style.ConditionalParagraphStyle
81 * @param tParam The test parameters
82 * @param log The log writer.
84 @Override
85 protected TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
86 TestEnvironment tEnv = null;
87 XNameAccess oSFNA = null;
88 XStyle oStyle = null;
89 XStyle oMyStyle = null;
91 log.println("creating a test environment");
93 try {
94 log.println("getting style");
95 XStyleFamiliesSupplier oSFS = UnoRuntime.queryInterface(XStyleFamiliesSupplier.class,
96 xTextDoc);
97 XNameAccess oSF = oSFS.getStyleFamilies();
98 oSFNA = UnoRuntime.queryInterface(
99 XNameAccess.class,oSF.getByName("ParagraphStyles"));
100 XIndexAccess oSFIA = UnoRuntime.queryInterface(XIndexAccess.class, oSFNA);
101 oStyle = UnoRuntime.queryInterface(
102 XStyle.class,oSFIA.getByIndex(1));
103 } catch ( com.sun.star.lang.WrappedTargetException e ) {
104 log.println("Error: exception occurred.");
105 e.printStackTrace(log);
106 throw new StatusException( "Couldn't create environment ", e );
107 } catch ( com.sun.star.lang.IndexOutOfBoundsException e ) {
108 log.println("Error: exception occurred.");
109 e.printStackTrace(log);
110 throw new StatusException( "Couldn't create environment ", e );
111 } catch ( com.sun.star.container.NoSuchElementException e ) {
112 log.println("Error: exception occurred.");
113 e.printStackTrace(log);
114 throw new StatusException( "Couldn't create environment ", e );
117 try {
118 log.print("Creating a user-defined style... ");
119 XMultiServiceFactory oMSF = UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDoc);
120 XInterface oInt = (XInterface)
121 oMSF.createInstance("com.sun.star.style.ConditionalParagraphStyle");
122 oMyStyle = UnoRuntime.queryInterface(XStyle.class, oInt);
123 } catch ( com.sun.star.uno.Exception e ) {
124 log.println("Error: exception occurred.");
125 e.printStackTrace(log);
126 throw new StatusException( "Couldn't create environment ", e );
130 if (oMyStyle == null) {
131 log.println("FAILED");
132 throw new StatusException(Status.failed("Couldn't create an own style "));
134 else {
135 log.println("OK");
138 XNameContainer oSFNC = UnoRuntime.queryInterface(XNameContainer.class, oSFNA);
140 try {
141 if ( oSFNC.hasByName("My Style") )
142 oSFNC.removeByName("My Style");
143 oSFNC.insertByName("My Style", oMyStyle);
144 } catch ( com.sun.star.lang.WrappedTargetException e ) {
145 e.printStackTrace(log);
146 throw new StatusException( "Couldn't create environment ", e );
147 } catch ( com.sun.star.lang.IllegalArgumentException e ) {
148 e.printStackTrace(log);
149 throw new StatusException( "Couldn't create environment ", e );
150 } catch ( com.sun.star.container.NoSuchElementException e ) {
151 e.printStackTrace(log);
152 throw new StatusException( "Couldn't create environment ", e );
153 } catch ( com.sun.star.container.ElementExistException e ) {
154 e.printStackTrace(log);
155 throw new StatusException( "Couldn't create environment ", e );
158 XText oText = xTextDoc.getText();
159 XTextCursor oCursor = oText.createTextCursor();
160 XPropertySet xProp = UnoRuntime.queryInterface(XPropertySet.class, oCursor);
161 try {
162 xProp.setPropertyValue("ParaStyleName", oMyStyle.getName());
163 } catch ( com.sun.star.lang.WrappedTargetException e ) {
164 e.printStackTrace( log );
165 throw new StatusException( "Couldn't create environment ", e );
166 } catch ( com.sun.star.lang.IllegalArgumentException e ) {
167 e.printStackTrace( log );
168 throw new StatusException( "Couldn't create environment ", e );
169 } catch ( com.sun.star.beans.PropertyVetoException e ) {
170 e.printStackTrace( log );
171 throw new StatusException( "Couldn't create environment ", e );
172 } catch ( com.sun.star.beans.UnknownPropertyException e ) {
173 e.printStackTrace( log );
174 throw new StatusException( "Couldn't create environment ", e );
177 log.println("creating a new environment for object");
178 tEnv = new TestEnvironment(oMyStyle);
179 tEnv.addObjRelation("PoolStyle", oStyle);
181 XPropertySet xStyleProp = UnoRuntime.queryInterface(XPropertySet.class, oMyStyle);
182 short exclude = PropertyAttribute.READONLY;
183 String[] names = utils.getFilteredPropertyNames(xStyleProp, (short)0, exclude);
184 tEnv.addObjRelation("PropertyNames", names);
186 return tEnv;