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