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.lang.XMultiServiceFactory;
35 import com.sun.star.text.XText;
36 import com.sun.star.text.XTextCursor;
37 import com.sun.star.text.XTextDocument;
38 import com.sun.star.text.XTextFrame;
39 import com.sun.star.text.XTextFramesSupplier;
40 import com.sun.star.uno.UnoRuntime;
41 import com.sun.star.uno.XInterface;
42 
43 /**
44  * Object implements the following interfaces :
45  * <ul>
46  *  <li> <code>com::sun::star::container::XContainer</code></li>
47  *  <li> <code>com::sun::star::container::XNameAccess</code></li>
48  *  <li> <code>com::sun::star::container::XIndexAccess</code></li>
49  *  <li> <code>com::sun::star::container::XElementAccess</code></li>
50  * </ul> <p>
51  * This object test <b> is NOT </b> designed to be run in several
52  * threads concurently.
53  * @see com.sun.star.container.XContainer
54  * @see com.sun.star.container.XNameAccess
55  * @see com.sun.star.container.XIndexAccess
56  * @see com.sun.star.container.XElementAccess
57  * @see ifc.container._XContainer
58  * @see ifc.container._XNameAccess
59  * @see ifc.container._XIndexAccess
60  * @see ifc.container._XElementAccess
61  */
62 public class SwXFrames extends TestCase {
63     XTextDocument xTextDoc;
64 
65     /**
66     * Creates text document.
67     */
initialize( TestParameters tParam, PrintWriter log )68     protected void initialize( TestParameters tParam, PrintWriter log ) {
69         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
70         try {
71             log.println( "creating a textdocument" );
72             xTextDoc = SOF.createTextDoc( null );
73         } catch ( com.sun.star.uno.Exception e ) {
74             e.printStackTrace( log );
75             throw new StatusException( "Couldn't create document", e );
76         }
77     }
78 
79     /**
80     * Disposes text document.
81     */
cleanup( TestParameters tParam, PrintWriter log )82     protected void cleanup( TestParameters tParam, PrintWriter log ) {
83         log.println( "    disposing xTextDoc " );
84         util.DesktopTools.closeDoc(xTextDoc);
85     }
86 
87     /**
88     * Creating a Testenvironment for the interfaces to be tested.
89     * Creates an instance of the service
90     * <code>com.sun.star.text.TextFrame</code>. Then inserts created text frame
91     * to the text, and finally gets all frames of text document using
92     * <code>XTextFramesSupplier</code> interface.<br>
93     */
createTestEnvironment( TestParameters Param, PrintWriter log )94     public synchronized TestEnvironment createTestEnvironment(
95             TestParameters Param, PrintWriter log ) throws StatusException {
96         XInterface oObj = null;
97         XTextFrame oFrame1 = null;
98         XText oText = null;
99         XTextCursor oCursor = null;
100         XMultiServiceFactory oDocMSF = null;
101         XTextFramesSupplier oInterface = null;
102 
103         log.println( "creating a test environment" );
104         try {
105             oDocMSF = (XMultiServiceFactory)
106                 UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDoc);
107             Object oInt = oDocMSF.createInstance("com.sun.star.text.TextFrame");
108             oFrame1 = (XTextFrame)
109                 UnoRuntime.queryInterface( XTextFrame.class, oInt );
110         } catch ( com.sun.star.uno.Exception e ) {
111             e.printStackTrace(log);
112             throw new StatusException
113                 ("Couldn't create instance of TextFrame", e);
114         }
115 
116         oText = xTextDoc.getText();
117         oCursor = oText.createTextCursor();
118 
119         try {
120             oText.insertTextContent(oCursor, oFrame1, false);
121         } catch ( com.sun.star.lang.IllegalArgumentException e ) {
122             e.printStackTrace(log);
123             throw new StatusException
124                 ("Error: can't insert text content to text document", e);
125         }
126 
127         oInterface = (XTextFramesSupplier)
128             UnoRuntime.queryInterface( XTextFramesSupplier.class, xTextDoc );
129 
130         oObj = oInterface.getTextFrames();
131 
132         log.println( "creating a new environment for Frame object" );
133         TestEnvironment tEnv = new TestEnvironment( oObj );
134 
135         return tEnv;
136     } // finish method getTestEnvironment
137 
138 }    // finish class SwXFrames
139 
140