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 package complex.persistent_window_states;
24 
25 import com.sun.star.uno.Any;
26 import com.sun.star.lang.XMultiServiceFactory;
27 import com.sun.star.frame.XFramesSupplier;
28 import com.sun.star.frame.XFrames;
29 import com.sun.star.container.XIndexAccess;
30 import com.sun.star.uno.UnoRuntime;
31 import com.sun.star.uno.AnyConverter;
32 import com.sun.star.frame.XComponentLoader;
33 import com.sun.star.awt.Rectangle;
34 import com.sun.star.util.XCloseable;
35 import helper.ConfigurationRead;
36 
37 
38 
39 // import org.junit.After;
40 import org.junit.AfterClass;
41 // import org.junit.Before;
42 import org.junit.BeforeClass;
43 import org.junit.Test;
44 import org.openoffice.test.OfficeConnection;
45 import static org.junit.Assert.*;
46 
47 /**
48  * Parameters:
49  * <ul>
50  *   <li>NoOffice=yes - StarOffice is not started initially.</li>
51  * </ul>
52  */
53 public class PersistentWindowTest
54 {
55 
56     // private XMultiServiceFactory xMSF;
57 //    private OfficeProvider oProvider;
58     private int iOfficeCloseTime = 0;
59 
60     /**
61      * A frunction to tell the framework, which test functions are available.
62      * Right now, it's only 'checkPersistentWindowState'.
63      * @return All test methods.
64      */
65 //    public String[] getTestMethodNames()
66 //    {
67 //        return new String[]
68 //                {
69 //                    "checkPersistentWindowState"
70 //                };
71 //    }
72 
73    /**
74      * The test parameters
75      */
76   //  private static TestParameters param = null;
77 
78     /**
79      * Test if all available document types change the
80      * persistent Window Attributes
81      *
82      * The test follows basically these steps:
83      * - Create a configuration reader and a componentloader
84      * - Look for all document types in the configuration
85      * - Do for every doc type
86      *   - start office
87      *   - read configuration attibute settings
88      *   - create a new document
89      *   - resize the document and close it
90      *   - close office
91      *   - start office
92      *   - read configuration attribute settings
93      *   - create another new document
94      *   - compare old settings with new ones: should be different
95      *   - compare the document size with the resized document: should be equal
96      *   - close office
97      * - Test finished
98      */
checkPersistentWindowState()99     @Test public void checkPersistentWindowState()
100     {
101         // final XMultiServiceFactory xMsf = getMSF();
102 
103         // some Tests need the qadevOOo TestParameters, it is like a Hashmap for Properties.
104 //        param = new TestParameters();
105 //        param.put("ServiceFactory", xMsf); // some qadevOOo functions need the ServiceFactory
106 
107         try
108         {
109 
110             // At first we are already connected
111             // if (!connect())
112             // {
113             //     return;
114             // }
115 
116             // fetch the multi service factory for setup
117             // XMultiServiceFactory xCP = getMSF();
118 
119             // create the configuration reader
120             // ConfigurationRead cfgRead = new ConfigurationRead(xCP);
121 
122             // just test the wrong ones, not all.
123             String[] els = new String[]
124             {
125                 "Office/Factories/com.sun.star.drawing.DrawingDocument",
126                 "Office/Factories/com.sun.star.formula.FormulaProperties",
127                 //"Office/Factories/com.sun.star.presentation.PresentationDocument",
128                 "Office/Factories/com.sun.star.sheet.SpreadsheetDocument",
129                 "Office/Factories/com.sun.star.text.GlobalDocument",
130                 "Office/Factories/com.sun.star.text.TextDocument",
131                 "Office/Factories/com.sun.star.text.WebDocument",
132             };
133             // uncomment the following line for all doc types
134             // String [] els = cfgRead.getSubNodeNames("Office/Factories");
135 
136             System.out.println("Found " + els.length + " document types to test.\n");
137             disconnect();
138 
139             // for all types
140             for (int i = 0; i < els.length; i++)
141             {
142                 System.out.println("\tStart test for document type " + i + ": " + els[i]);
143                 // exclude chart documents: cannot be created this way.
144                 if (els[i].indexOf("ChartDocument") != -1)
145                 {
146                     System.out.println("Skipping chart document: cannot be create like this.");
147                     continue;
148                 }
149 
150                 // start an office
151                 connect();
152 
153                 // get configuration
154                 String[] settings = getConfigurationAndLoader(getMSF(), els[i]);
155                 if (settings == null)
156                 {
157                     System.out.println("Skipping document type " + els[i]);
158                     disconnect();
159                     continue;
160                 }
161                 String cfg = settings[1];
162 
163                 // load a document
164                 DocumentHandle handle = loadDocument(getMSF(), settings[0]);
165 
166                 // first size
167                 Rectangle rect1 = handle.getDocumentPosSize();
168 
169                 // resize
170                 handle.resizeDocument();
171                 // after resize
172                 Rectangle rect2 = handle.getDocumentPosSize();
173 
174                 // disposeManager and start a new office
175                 disconnect();
176 
177                 connect();
178 
179                 // get configuration
180                 settings = getConfigurationAndLoader(getMSF(), els[i]);
181 
182                 String newCfg = settings[1];
183 
184                 // load a document
185                 handle = loadDocument(getMSF(), settings[0]);
186 
187                 Rectangle newRect = handle.getDocumentPosSize();
188 
189                 // print the settings and window sizes
190                 System.out.println("----------------------------");
191                 System.out.println("Initial Config String      : " + cfg);
192                 System.out.println("Config String after restart: " + newCfg);
193 
194                 System.out.println("----------------------------");
195                 System.out.println("Initial window       (X,Y,Width,Height): "
196                         + rect1.X + ";" + rect1.Y + ";" + rect1.Width + ";" + rect1.Height);
197                 System.out.println("Window after resize  (X,Y,Width,Height): "
198                         + rect2.X + ";" + rect2.Y + ";" + rect2.Width + ";" + rect2.Height);
199                 System.out.println("Window after restart (X,Y,Width,Height): "
200                         + newRect.X + ";" + newRect.Y + ";" + newRect.Width + ";"
201                         + newRect.Height);
202 
203                 // compare to see if resize worked
204                 System.out.println("----------------------------");
205                 if (els[i].indexOf("SpreadsheetDocument") == -1 &&
206                     els[i].indexOf("DrawingDocument") == -1)
207                 {
208                     // leave out Spreadsheet- and DrawingDocumnt
209                     assertTrue("Resize values for " + els[i] + " are equal.", !compareRectangles(rect1, rect2));
210                 }
211                 // compare settings and sizes
212                 assertTrue("Config settings for " + els[i] + " were not changed.", !cfg.equals(newCfg));
213                 assertTrue("Resized and restarted window for " + els[i] + " are not equal.", compareRectangles(rect2, newRect));
214                 System.out.println("----------------------------");
215 
216                 // disposeManager
217                 disconnect();
218 
219                 System.out.println("\tFinish test for document type " + i + ": " + els[i]);
220 
221             }
222         }
223         catch (Exception e)
224         {
225             e.printStackTrace();
226         }
227     }
228 
229     /**
230      * Get the configuration settings and the document loader
231      * @param  xMSF A MultiServiceFactory from an office
232      * @param cfgString A configuration string
233      * @return Settings and Loader
234      */
getConfigurationAndLoader(XMultiServiceFactory xMSF, String cfgString)235     private static String[] getConfigurationAndLoader(XMultiServiceFactory xMSF,
236             String cfgString)
237     {
238         String[] conf = new String[2];
239 
240         try
241         {
242             Object o = xMSF.createInstance(
243                     "com.sun.star.configuration.ConfigurationProvider");
244 
245             // fetch the multi service factory for setup
246             XMultiServiceFactory xCP = UnoRuntime.queryInterface(XMultiServiceFactory.class, o);
247 
248             // create the configuration reader
249             ConfigurationRead cfgRead = new ConfigurationRead(xCP);
250 
251             // get the document loader
252             String loader = getStringFromObject(
253                     cfgRead.getByHierarchicalName(cfgString + "/ooSetupFactoryEmptyDocumentURL"));
254 
255             if (loader == null)
256             {
257                 return null;
258             }
259             System.out.println("\tLoader: " + loader);
260 
261             // read attributes
262             String hierchName = cfgString + "/ooSetupFactoryWindowAttributes";
263             String setupSettings = getStringFromObject(cfgRead.getByHierarchicalName(hierchName));
264             // remove slots: just plain document types have to start
265             if (loader.indexOf("?slot") != -1)
266             {
267                 loader = loader.substring(0, loader.indexOf("?slot"));
268                 System.out.println("Loader: " + loader);
269             }
270 
271             conf[0] = loader;
272             conf[1] = setupSettings;
273         }
274         catch (com.sun.star.uno.Exception e)
275         {
276         }
277         return conf;
278     }
279 
280     /**
281      * Load a document
282      * @param xMSF A MultiServiceFactory from an office
283      * @param docLoader A documet loader
284      * @return A handle to the document
285      */
loadDocument(XMultiServiceFactory xMSF, String docLoader)286     private DocumentHandle loadDocument(XMultiServiceFactory xMSF,
287             String docLoader)
288     {
289         DocumentHandle docHandle = null;
290         try
291         {
292             // create component loaader
293             XComponentLoader xCompLoader = UnoRuntime.queryInterface(XComponentLoader.class, xMSF.createInstance("com.sun.star.frame.Desktop"));
294             XFramesSupplier xFrameSupp = UnoRuntime.queryInterface(XFramesSupplier.class, xCompLoader);
295             // close all existing frames
296             XFrames xFrames = xFrameSupp.getFrames();
297             XIndexAccess xAcc = UnoRuntime.queryInterface(XIndexAccess.class, xFrames);
298             for (int i = 0; i < xAcc.getCount(); i++)
299             {
300                 XCloseable xClose = UnoRuntime.queryInterface(XCloseable.class, xAcc.getByIndex(i));
301                 try
302                 {
303                     if (xClose != null)
304                     {
305                         xClose.close(false);
306                     }
307                     else
308                     {
309                         fail("Could not query frame for XCloseable!");
310                     }
311                 }
312                 catch (com.sun.star.uno.Exception e)
313                 {
314                     e.printStackTrace();
315                     fail("Could not query frame for XCloseable!");
316                 }
317             }
318             docHandle = new DocumentHandle(xCompLoader);
319             docHandle.loadDocument(docLoader, false);
320         }
321         catch (com.sun.star.uno.Exception e)
322         {
323             e.printStackTrace();
324         }
325         catch (java.lang.Exception e)
326         {
327             e.printStackTrace();
328         }
329         return docHandle;
330     }
331 
connect()332     private boolean connect()
333     {
334         try
335         {
336             connection.setUp();
337         }
338         catch (java.lang.InterruptedException e)
339         {
340             fail("can't connect.");
341         }
342         catch (Exception e)
343         {
344             fail("can't connect.");
345         }
346         return true;
347     }
348 
disconnect()349     private boolean disconnect()
350     {
351         try
352         {
353             connection.tearDown();
354         }
355         catch (java.lang.InterruptedException e)
356         {
357             fail("can't disconnect.");
358         }
359         catch (Exception e)
360         {
361             fail("can't disconnect.");
362         }
363         return true;
364     }
365 
getStringFromObject(Object oName)366     private static String getStringFromObject(Object oName)
367     {
368         if (oName instanceof String)
369         {
370             return (String) oName;
371         }
372         String value = null;
373         if (oName instanceof Any)
374         {
375             try
376             {
377                 value = AnyConverter.toString(oName);
378                 if (value == null)
379                 {
380                     System.out.println("Got a void css.uno.Any as loading string.");
381                 }
382             }
383             catch (Exception e)
384             {
385                 System.out.println("This document type cannot be opened directly.");
386             }
387         }
388         return value;
389     }
390 
391     /**
392      * Compare two rectangles. Return true, if both are equal, false
393      * otherwise.
394      * @param rect1 First Rectangle.
395      * @param rect2 Second Rectangle.
396      * @return True, if the rectangles are equal.
397      */
compareRectangles(Rectangle rect1, Rectangle rect2)398     private boolean compareRectangles(Rectangle rect1, Rectangle rect2)
399     {
400         boolean result = true;
401         result &= (rect1.X == rect2.X);
402         result &= (rect1.Y == rect2.Y);
403         result &= (rect1.Width == rect2.Width);
404         result &= (rect1.Height == rect2.Height);
405         return result;
406     }
407 
408 
409 
getMSF()410         private XMultiServiceFactory getMSF()
411     {
412         final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
413         return xMSF1;
414     }
415 
416     // setup and close connections
setUpConnection()417     @BeforeClass public static void setUpConnection() throws Exception {
418         System.out.println("setUpConnection()");
419         connection.setUp();
420     }
421 
tearDownConnection()422     @AfterClass public static void tearDownConnection()
423         throws InterruptedException, com.sun.star.uno.Exception
424     {
425         System.out.println("tearDownConnection()");
426         connection.tearDown();
427     }
428 
429     private static final OfficeConnection connection = new OfficeConnection();
430 
431 }
432