1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package mod._sw;
25 
26 import com.sun.star.beans.PropertyAttribute;
27 import com.sun.star.beans.XPropertySet;
28 import com.sun.star.container.XIndexAccess;
29 import com.sun.star.container.XNameAccess;
30 import com.sun.star.container.XNameContainer;
31 import com.sun.star.lang.XMultiServiceFactory;
32 import com.sun.star.style.XStyle;
33 import com.sun.star.style.XStyleFamiliesSupplier;
34 import com.sun.star.text.XText;
35 import com.sun.star.text.XTextCursor;
36 import com.sun.star.text.XTextDocument;
37 import com.sun.star.uno.UnoRuntime;
38 import com.sun.star.uno.XInterface;
39 import java.io.PrintWriter;
40 import lib.StatusException;
41 import lib.TestCase;
42 import lib.TestEnvironment;
43 import lib.TestParameters;
44 import util.DesktopTools;
45 import util.SOfficeFactory;
46 import util.utils;
47 
48 /**
49  * Test for object which is represented by service
50  * <code>com.sun.star.style.CharacterStyle</code>. <p>
51  * @see com.sun.star.style.CharacterStyle
52  */
53 public class CharacterStyle extends TestCase {
54     private XTextDocument xTextDoc;
55 
56     /**
57     * Creates text document.
58     */
initialize( TestParameters tParam, PrintWriter log )59     protected void initialize( TestParameters tParam, PrintWriter log ) {
60         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
61         try {
62             log.println( "creating a textdocument" );
63             xTextDoc = SOF.createTextDoc( null );
64         } catch ( com.sun.star.uno.Exception e ) {
65             e.printStackTrace( log );
66             throw new StatusException( "Couldn't create document", e );
67         }
68     }
69 
70     /**
71     * Disposes text document.
72     */
cleanup( TestParameters tParam, PrintWriter log )73     protected void cleanup( TestParameters tParam, PrintWriter log ) {
74         log.println( "    disposing xTextDoc " );
75         DesktopTools.closeDoc(xTextDoc);
76     }
77 
78     /**
79     * Creating a Testenvironment for the interfaces to be tested.
80     * At first style families are gotten from a text document using
81     * <code>XStyleFamiliesSupplier</code> interface, then family indexed '0' is
82     * gotten from this style family using <code>XIndexAccess</code> interface.
83     * Next, method creates an instance of the service
84     * <code>com.sun.star.style.CharacterStyle</code> and inserts it to a
85     * previously obtained style family using <code>XNameContainer</code>
86     * interface. Finally, method creates a cursor of a major text of text
87     * document and sets it's property 'CharStyleName' value to the name of
88     * previously created style's name.
89     *     Object relations created :
90     * <ul>
91     *  <li> <code>'PoolStyle'</code> for
92     *      {@link ifc.style._XStyle} : style indexed '10' obtained from
93     *  StyleFamily indexed '0' from text document using
94     *  <code>XIndexAccess</code> interface.</li>
95     * </ul>
96     */
createTestEnvironment(TestParameters tParam, PrintWriter log)97     protected TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
98 
99         TestEnvironment tEnv = null;
100         XNameAccess oSFNA = null;
101         XStyle oStyle = null;
102         XStyle oMyStyle = null;
103 
104         log.println("creating a test environment");
105 
106         try {
107             log.println("getting style");
108             XStyleFamiliesSupplier oSFS = (XStyleFamiliesSupplier)
109                 UnoRuntime.queryInterface(XStyleFamiliesSupplier.class,
110                 xTextDoc);
111             XNameAccess oSF = oSFS.getStyleFamilies();
112             XIndexAccess oSFsIA = (XIndexAccess)
113                 UnoRuntime.queryInterface(XIndexAccess.class, oSF);
114             oSFNA = (XNameAccess) UnoRuntime.queryInterface(
115                             XNameAccess.class,oSFsIA.getByIndex(0));
116             XIndexAccess oSFIA = (XIndexAccess)
117                 UnoRuntime.queryInterface(XIndexAccess.class, oSFNA);
118             oStyle = (XStyle) UnoRuntime.queryInterface(
119                             XStyle.class,oSFIA.getByIndex(0));
120         } catch ( com.sun.star.lang.WrappedTargetException e ) {
121             log.println("Error: exception occured.");
122             e.printStackTrace(log);
123             throw new StatusException( "Couldn't create environment ", e );
124         } catch ( com.sun.star.lang.IndexOutOfBoundsException e ) {
125             log.println("Error: exception occured.");
126             e.printStackTrace(log);
127             throw new StatusException( "Couldn't create environment ", e );
128         }
129 
130         try {
131             log.print("Creating a user-defined style... ");
132             XMultiServiceFactory oMSF = (XMultiServiceFactory)
133                 UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDoc);
134             XInterface oInt = (XInterface)
135                 oMSF.createInstance("com.sun.star.style.CharacterStyle");
136             oMyStyle = (XStyle) UnoRuntime.queryInterface(XStyle.class, oInt);
137         } catch ( com.sun.star.uno.Exception e ) {
138             log.println("Error: exception occured.");
139             e.printStackTrace(log);
140             throw new StatusException( "Couldn't create environment ", e );
141         }
142 
143 
144         if (oMyStyle == null)
145             log.println("FAILED");
146         else
147             log.println("OK");
148             XNameContainer oSFNC = (XNameContainer)
149             UnoRuntime.queryInterface(XNameContainer.class, oSFNA);
150 
151         try {
152             if ( oSFNC.hasByName("My Style") )
153                 oSFNC.removeByName("My Style");
154             oSFNC.insertByName("My Style", oMyStyle);
155         } catch ( com.sun.star.lang.WrappedTargetException e ) {
156             e.printStackTrace(log);
157             throw new StatusException( "Couldn't create environment ", e );
158         } catch     ( com.sun.star.lang.IllegalArgumentException e ) {
159             e.printStackTrace(log);
160             throw new StatusException( "Couldn't create environment ", e );
161         } catch ( com.sun.star.container.NoSuchElementException e ) {
162             e.printStackTrace(log);
163             throw new StatusException( "Couldn't create environment ", e );
164         } catch ( com.sun.star.container.ElementExistException e ) {
165             e.printStackTrace(log);
166             throw new StatusException( "Couldn't create environment ", e );
167         }
168 
169         XText oText = xTextDoc.getText();
170         XTextCursor oCursor = oText.createTextCursor();
171         XPropertySet xProp = (XPropertySet)
172             UnoRuntime.queryInterface(XPropertySet.class, oCursor);
173 
174         try {
175             xProp.setPropertyValue("CharStyleName", oMyStyle.getName());
176         } catch ( com.sun.star.lang.WrappedTargetException e ) {
177             e.printStackTrace( log );
178             throw new StatusException( "Couldn't create environment ", e );
179         } catch ( com.sun.star.lang.IllegalArgumentException e ) {
180             e.printStackTrace( log );
181             throw new StatusException( "Couldn't create environment ", e );
182         } catch ( com.sun.star.beans.PropertyVetoException e ) {
183             e.printStackTrace( log );
184             throw new StatusException( "Couldn't create environment ", e );
185         } catch ( com.sun.star.beans.UnknownPropertyException e ) {
186             e.printStackTrace( log );
187             throw new StatusException( "Couldn't create environment ", e );
188         }
189 
190         log.println("creating a new environment for object");
191         tEnv = new TestEnvironment(oMyStyle);
192         tEnv.addObjRelation("PoolStyle", oStyle);
193 
194         XPropertySet xStyleProp = (XPropertySet)
195             UnoRuntime.queryInterface(XPropertySet.class, oMyStyle);
196 
197         short exclude = PropertyAttribute.MAYBEVOID + PropertyAttribute.READONLY;
198         tEnv.addObjRelation("PropertyNames",utils.getFilteredPropertyNames(xStyleProp, (short)0, exclude));
199 
200         return tEnv;
201     }
202 
203 }
204