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._fwk;
25 
26 import com.sun.star.beans.XPropertySet;
27 import com.sun.star.frame.XFrame;
28 import com.sun.star.lang.XMultiServiceFactory;
29 import com.sun.star.uno.UnoRuntime;
30 import com.sun.star.uno.XInterface;
31 import java.io.PrintWriter;
32 import com.sun.star.text.XText;
33 import com.sun.star.text.XTextCursor;
34 import com.sun.star.text.XTextDocument;
35 import com.sun.star.util.XCloseable;
36 import lib.StatusException;
37 import lib.TestCase;
38 import lib.TestEnvironment;
39 import lib.TestParameters;
40 import util.WriterTools;
41 
42 /**
43  */
44 public class LayoutManager extends TestCase {
45     XInterface xManager = null;
46     XTextDocument xTextDoc;
47 
48     /**
49      * Cleanup: close the created document
50      * @param tParam The test parameters.
51      * @param The log writer.
52      * @return The test environment.
53      */
54     protected void cleanup(TestParameters tParam, PrintWriter log) {
55         log.println("    disposing xTextDoc ");
56 
57         try {
58             XCloseable closer = (XCloseable) UnoRuntime.queryInterface(
59             XCloseable.class, xTextDoc);
60             closer.close(true);
61         } catch (com.sun.star.util.CloseVetoException e) {
62             log.println("couldn't close document");
63         } catch (com.sun.star.lang.DisposedException e) {
64             log.println("couldn't close document");
65         }
66     }
67 
68     /**
69      * Create test environment:
70      * <ul>
71      * <li>Create test doc</li>
72      * <li>Get the frame</li>
73      * <li>Get the LayoutManager from the frame</li>
74      * </ul>
75      * @param tParam The test parameters.
76      * @param The log writer.
77      * @return The test environment.
78      */
79     protected TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
80         TestEnvironment tEnv = null;
81         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF();
82 
83         log.println("Creating instance...");
84 
85         xTextDoc = WriterTools.createTextDoc(xMSF);
86 
87         XText xText = xTextDoc.getText();
88         XTextCursor xTextCursor = xText.createTextCursor();
89 
90         for (int i = 0; i < 11; i++) {
91             xText.insertString(xTextCursor, "A sample text and why not? ", false);
92         }
93 
94         XFrame xFrame = xTextDoc.getCurrentController().getFrame();
95         XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xFrame);
96         try {
97             Object any = xProp.getPropertyValue("LayoutManager");
98             xManager = (XInterface)UnoRuntime.queryInterface(XInterface.class, any);
99         }
100         catch(com.sun.star.beans.UnknownPropertyException e) {
101             e.printStackTrace(log);
102             throw new StatusException("Could not get property 'LayoutManager' from the current frame.", e);
103         }
104         catch(com.sun.star.lang.WrappedTargetException e) {
105             e.printStackTrace(log);
106             throw new StatusException("Could not get property 'LayoutManager' from the current frame.", e);
107         }
108 
109         // just to make sure, it's the right one.
110         log.println("TestObject: " + util.utils.getImplName(xManager));
111         tEnv = new TestEnvironment(xManager);
112 
113         tEnv.addObjRelation("XLayoutManager.TextDoc", xTextDoc);
114         tEnv.addObjRelation("XLayoutManager.Frame",xFrame);
115 
116         return tEnv;
117     }
118 }
119 
120 
121