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 java.io.PrintWriter;
27 
28 import lib.StatusException;
29 import lib.TestCase;
30 import lib.TestEnvironment;
31 import lib.TestParameters;
32 import util.SOfficeFactory;
33 
34 import com.sun.star.beans.XPropertySet;
35 import com.sun.star.container.XIndexAccess;
36 import com.sun.star.container.XNameAccess;
37 import com.sun.star.lang.XMultiServiceFactory;
38 import com.sun.star.style.XStyle;
39 import com.sun.star.style.XStyleFamiliesSupplier;
40 import com.sun.star.text.ControlCharacter;
41 import com.sun.star.text.XText;
42 import com.sun.star.text.XTextColumns;
43 import com.sun.star.text.XTextCursor;
44 import com.sun.star.text.XTextDocument;
45 import com.sun.star.uno.AnyConverter;
46 import com.sun.star.uno.Type;
47 import com.sun.star.uno.UnoRuntime;
48 import com.sun.star.uno.XInterface;
49 
50 /**
51  * Test for object which is represented by service
52  * <code>com.sun.star.text.TextColumns</code>. <p>
53  * Object implements the following interfaces :
54  * <ul>
55  *  <li> <code>com::sun::star::text::XTextColumns</code></li>
56  *  <li> <code>com::sun::star::text::TextColumns</code></li>
57  * </ul> <p>
58  * This object test <b> is NOT </b> designed to be run in several
59  * threads concurently.
60  * @see com.sun.star.text.XTextColumns
61  * @see com.sun.star.text.TextColumns
62  * @see ifc.text._XTextColumns
63  * @see ifc.text._TextColumns
64  */
65 public class SwXTextColumns extends TestCase {
66     XTextDocument xTextDoc;
67 
68     /**
69     * Creates text document.
70     */
initialize( TestParameters tParam, PrintWriter log )71     protected void initialize( TestParameters tParam, PrintWriter log ) {
72         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
73         try {
74             log.println( "creating a textdocument" );
75             xTextDoc = SOF.createTextDoc( null );
76         } catch ( com.sun.star.uno.Exception e ) {
77             e.printStackTrace( log );
78             throw new StatusException( "Couldn't create document", e );
79         }
80     }
81 
82     /**
83     * Disposes text document.
84     */
cleanup( TestParameters tParam, PrintWriter log )85     protected void cleanup( TestParameters tParam, PrintWriter log ) {
86         log.println( "    disposing xTextDoc " );
87         util.DesktopTools.closeDoc(xTextDoc);
88     }
89 
90     /**
91     * Creating a Testenvironment for the interfaces to be tested. After style
92     * families are gotten from text document using
93     * <code>XStyleFamiliesSupplier</code> interface, style family indexed '2'
94     * is obtained using <code>XIndexAccess</code> interface. Then style
95     * named 'Standard' is gotten from previously obtained style family using
96     * <code>XNameAccess</code> interface, and 'TextColumns' property value
97     * of this style is returned as a test component. Finally, several
98     * paragraphs within the text are inserted to a text document.
99     */
createTestEnvironment(TestParameters tParam, PrintWriter log)100     protected TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
101         XInterface oObj = null;
102         TestEnvironment tEnv = null;
103         XStyle oStyle = null;
104 
105         log.println( "creating a test environment" );
106         log.println("getting PageStyle");
107         XStyleFamiliesSupplier oSFS = (XStyleFamiliesSupplier)
108             UnoRuntime.queryInterface(XStyleFamiliesSupplier.class, xTextDoc);
109         XNameAccess oSF = oSFS.getStyleFamilies();
110         XIndexAccess oSFIA = (XIndexAccess)
111             UnoRuntime.queryInterface(XIndexAccess.class, oSF);
112 
113         try {
114             XNameAccess oSFNA = (XNameAccess) AnyConverter.toObject(
115                         new Type(XNameAccess.class),oSFIA.getByIndex(2));
116             oStyle = (XStyle) AnyConverter.toObject(
117                     new Type(XStyle.class),oSFNA.getByName("Standard"));
118         } catch ( com.sun.star.lang.WrappedTargetException e ) {
119             log.println("Error, exception occurred while getting style.");
120             e.printStackTrace(log);
121         } catch ( com.sun.star.lang.IndexOutOfBoundsException e ) {
122             log.println("Error, exception occurred while getting style.");
123             e.printStackTrace(log);
124         } catch ( com.sun.star.container.NoSuchElementException e ) {
125             log.println("Error, exception occurred while getting style.");
126             e.printStackTrace(log);
127         } catch ( com.sun.star.lang.IllegalArgumentException e ) {
128             log.println("Error, exception occurred while getting style.");
129             e.printStackTrace(log);
130         }
131 
132         try {
133             log.println("Getting property ('TextColumns') value of style "
134                 + oStyle.getName());
135             XPropertySet xProps = (XPropertySet)
136                 UnoRuntime.queryInterface(XPropertySet.class,oStyle);
137             oObj = (XTextColumns) AnyConverter.toObject(
138                 new Type(XTextColumns.class),xProps.getPropertyValue("TextColumns"));
139         } catch ( com.sun.star.lang.WrappedTargetException e ) {
140             log.println("Exception occurred while getting style property");
141             e.printStackTrace(log);
142         } catch ( com.sun.star.beans.UnknownPropertyException e ) {
143             log.println("Exception occurred while getting style property");
144             e.printStackTrace(log);
145         } catch ( com.sun.star.lang.IllegalArgumentException e ) {
146             log.println("Exception occurred while getting style property");
147             e.printStackTrace(log);
148         }
149 
150         XText oText = xTextDoc.getText();
151         XTextCursor oCursor = oText.createTextCursor();
152 
153         log.println( "inserting some text to text document..." );
154         try {
155             for (int i =0; i < 5; i++){
156                 oText.insertString( oCursor,"Paragraph Number: " + i, false);
157                 oText.insertString( oCursor,
158                     "The quick brown fox jumps over the lazy Dog: SwXParagraph",
159                     false);
160                 oText.insertControlCharacter( oCursor,
161                     ControlCharacter.PARAGRAPH_BREAK, false );
162                 oText.insertString( oCursor,
163                     "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG: SwXParagraph",
164                     false);
165                 oText.insertControlCharacter( oCursor,
166                     ControlCharacter.PARAGRAPH_BREAK, false );
167                 oText.insertControlCharacter( oCursor,
168                     ControlCharacter.LINE_BREAK, false );
169             }
170         } catch ( com.sun.star.lang.IllegalArgumentException e ){
171             log.println("Exception occurred while inserting Text");
172             e.printStackTrace(log);
173         }
174 
175         log.println("creating a new environment for object");
176         tEnv = new TestEnvironment(oObj);
177         return tEnv;
178     } // finish method getTestEnvironment
179 
180 }    // finish class SwXTextColumns
181