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