1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package mod._sw;
29 
30 import java.io.PrintWriter;
31 
32 import lib.StatusException;
33 import lib.TestCase;
34 import lib.TestEnvironment;
35 import lib.TestParameters;
36 import util.SOfficeFactory;
37 
38 import com.sun.star.beans.Property;
39 import com.sun.star.beans.PropertyAttribute;
40 import com.sun.star.beans.XPropertySet;
41 import com.sun.star.container.XIndexAccess;
42 import com.sun.star.container.XNameAccess;
43 import com.sun.star.container.XNameContainer;
44 import com.sun.star.lang.XMultiServiceFactory;
45 import com.sun.star.style.XStyle;
46 import com.sun.star.style.XStyleFamiliesSupplier;
47 import com.sun.star.text.XText;
48 import com.sun.star.text.XTextCursor;
49 import com.sun.star.text.XTextDocument;
50 import com.sun.star.uno.AnyConverter;
51 import com.sun.star.uno.Type;
52 import com.sun.star.uno.UnoRuntime;
53 import com.sun.star.uno.XInterface;
54 
55 /**
56  * Test for object which is represented by service
57  * <code>com.sun.star.style.Style</code>. <p>
58  * Object implements the following interfaces :
59  * <ul>
60  *  <li> <code>com::sun::star::container::XNamed</code></li>
61  *  <li> <code>com::sun::star::style::Style</code></li>
62  *  <li> <code>com::sun::star::style::XStyle</code></li>
63  * </ul> <p>
64  * This object test <b> is NOT </b> designed to be run in several
65  * threads concurently.
66  * @see com.sun.star.container.XNamed
67  * @see com.sun.star.style.Style
68  * @see com.sun.star.style.XStyle
69  * @see ifc.container._XNamed
70  * @see ifc.style._Style
71  * @see ifc.style._XStyle
72  */
73 public class SwXStyle extends TestCase {
74     XTextDocument xTextDoc;
75     SOfficeFactory SOF = null;
76 
77     /**
78     * Creates text document.
79     */
80     protected void initialize( TestParameters tParam, PrintWriter log ) {
81         SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
82         try {
83             log.println( "creating a textdocument" );
84             xTextDoc = SOF.createTextDoc( null );
85         } catch ( com.sun.star.uno.Exception e ) {
86             e.printStackTrace( log );
87             throw new StatusException( "Couldn't create document", e );
88         }
89     }
90 
91     /**
92     * Disposes text document.
93     */
94     protected void cleanup( TestParameters tParam, PrintWriter log ) {
95         log.println( "    disposing xTextDoc " );
96         util.DesktopTools.closeDoc(xTextDoc);
97     }
98 
99     /**
100     * Creating a Testenvironment for the interfaces to be tested.
101     * At first style families are gotten from a text document using
102     * <code>XStyleFamiliesSupplier</code> interface, then family indexed '0' is
103     * gotten from this style family using <code>XIndexAccess</code> interface.
104     * Next, method creates an instance of the service
105     * <code>com.sun.star.style.CharacterStyle</code> and inserts it to a
106     * previously obtained style family using <code>XNameContainer</code>
107     * interface. Finally, method creates a cursor of a major text of text
108     * document and sets it's property 'CharStyleName' value to the name of
109     * previously created style's name.
110     *     Object relations created :
111     * <ul>
112     *  <li> <code>'PoolStyle'</code> for
113     *      {@link ifc.style._XStyle} : slyle indexed '10' obtained from
114     *  StyleFamily indexed '0' from text document using
115     *  <code>XIndexAccess</code> interface.</li>
116     * </ul>
117     */
118     protected synchronized TestEnvironment createTestEnvironment
119             (TestParameters Param, PrintWriter log) {
120 
121         TestEnvironment tEnv = null;
122         XNameAccess oSFNA = null;
123         XStyle oStyle = null;
124         XStyle oMyStyle = null;
125 
126         log.println("creating a test environment");
127 
128         try {
129             log.println("getting style");
130             XStyleFamiliesSupplier oSFS = (XStyleFamiliesSupplier)
131                 UnoRuntime.queryInterface(XStyleFamiliesSupplier.class,
132                 xTextDoc);
133             XNameAccess oSF = oSFS.getStyleFamilies();
134             XIndexAccess oSFsIA = (XIndexAccess)
135                 UnoRuntime.queryInterface(XIndexAccess.class, oSF);
136             oSFNA = (XNameAccess) AnyConverter.toObject(
137                         new Type(XNameAccess.class),oSFsIA.getByIndex(0));
138             XIndexAccess oSFIA = (XIndexAccess)
139                 UnoRuntime.queryInterface(XIndexAccess.class, oSFNA);
140             oStyle = (XStyle) AnyConverter.toObject(
141                     new Type(XStyle.class),oSFIA.getByIndex(10));
142         } catch ( com.sun.star.lang.WrappedTargetException e ) {
143             log.println("Error: exception occured.");
144             e.printStackTrace(log);
145             throw new StatusException( "Couldn't create environment ", e );
146         } catch ( com.sun.star.lang.IndexOutOfBoundsException e ) {
147             log.println("Error: exception occured.");
148             e.printStackTrace(log);
149             throw new StatusException( "Couldn't create environment ", e );
150         } catch ( com.sun.star.lang.IllegalArgumentException e ) {
151             log.println("Error: exception occured.");
152             e.printStackTrace(log);
153             throw new StatusException( "Couldn't create environment ", e );
154         }
155 
156         try {
157             log.print("Creating a user-defined style... ");
158             XMultiServiceFactory oMSF = (XMultiServiceFactory)
159                 UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDoc);
160             XInterface oInt = (XInterface)
161                 oMSF.createInstance("com.sun.star.style.CharacterStyle");
162             oMyStyle = (XStyle) UnoRuntime.queryInterface(XStyle.class, oInt);
163         } catch ( com.sun.star.uno.Exception e ) {
164             log.println("Error: exception occured.");
165             e.printStackTrace(log);
166             throw new StatusException( "Couldn't create environment ", e );
167         }
168 
169 
170         if (oMyStyle == null)
171             log.println("FAILED");
172         else
173             log.println("OK");
174             XNameContainer oSFNC = (XNameContainer)
175             UnoRuntime.queryInterface(XNameContainer.class, oSFNA);
176 
177         try {
178             if ( oSFNC.hasByName("My Style") )
179                 oSFNC.removeByName("My Style");
180             oSFNC.insertByName("My Style", oMyStyle);
181         } catch ( com.sun.star.lang.WrappedTargetException e ) {
182             e.printStackTrace(log);
183             throw new StatusException( "Couldn't create environment ", e );
184         } catch     ( com.sun.star.lang.IllegalArgumentException e ) {
185             e.printStackTrace(log);
186             throw new StatusException( "Couldn't create environment ", e );
187         } catch ( com.sun.star.container.NoSuchElementException e ) {
188             e.printStackTrace(log);
189             throw new StatusException( "Couldn't create environment ", e );
190         } catch ( com.sun.star.container.ElementExistException e ) {
191             e.printStackTrace(log);
192             throw new StatusException( "Couldn't create environment ", e );
193         }
194 
195         XText oText = xTextDoc.getText();
196         XTextCursor oCursor = oText.createTextCursor();
197         XPropertySet xProp = (XPropertySet)
198             UnoRuntime.queryInterface(XPropertySet.class, oCursor);
199 
200         try {
201             xProp.setPropertyValue("CharStyleName", oMyStyle.getName());
202         } catch ( com.sun.star.lang.WrappedTargetException e ) {
203             e.printStackTrace( log );
204             throw new StatusException( "Couldn't create environment ", e );
205         } catch ( com.sun.star.lang.IllegalArgumentException e ) {
206             e.printStackTrace( log );
207             throw new StatusException( "Couldn't create environment ", e );
208         } catch ( com.sun.star.beans.PropertyVetoException e ) {
209             e.printStackTrace( log );
210             throw new StatusException( "Couldn't create environment ", e );
211         } catch ( com.sun.star.beans.UnknownPropertyException e ) {
212             e.printStackTrace( log );
213             throw new StatusException( "Couldn't create environment ", e );
214         }
215 
216         log.println("creating a new environment for object");
217         tEnv = new TestEnvironment(oMyStyle);
218         tEnv.addObjRelation("PoolStyle", oStyle);
219 
220         XPropertySet xStyleProp = (XPropertySet)
221             UnoRuntime.queryInterface(XPropertySet.class, oMyStyle);
222         tEnv.addObjRelation("PropertyNames",getPropertyNames(xStyleProp));
223 
224         return tEnv;
225     }
226 
227     public String[] getPropertyNames(XPropertySet props) {
228         Property[] the_props = props.getPropertySetInfo().getProperties();
229         String[] names = new String[the_props.length];
230         String placebo = "";
231         for (int i=0;i<the_props.length;i++) {
232             boolean isWritable =
233                 ((the_props[i].Attributes & PropertyAttribute.READONLY) == 0);
234             if (isWritable) placebo=the_props[i].Name;
235         }
236         for (int i=0;i<the_props.length;i++) {
237             boolean isWritable =
238                 ((the_props[i].Attributes & PropertyAttribute.READONLY) == 0);
239             if (isWritable) {
240                 names[i]=the_props[i].Name;
241             } else {
242                 names[i] = placebo;
243             }
244         }
245         return names;
246     }
247 
248 }    // finish class SwXStyle
249