Update ooo320-m1
[ooovba.git] / vcl / qa / complex / persistent_window_states / PersistentWindowTest.java
blobabf04b94a59e50439e5ed928ab2c554efefc0ebb
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: PersistentWindowTest.java,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 package complex.persistent_window_states;
33 import com.sun.star.lang.XServiceInfo;
34 import com.sun.star.lang.XInitialization;
35 import com.sun.star.uno.Type;
36 import com.sun.star.uno.Any;
37 import com.sun.star.lang.XTypeProvider;
38 import com.sun.star.lang.XSingleServiceFactory;
39 import com.sun.star.lang.XMultiServiceFactory;
40 import com.sun.star.lang.XComponent;
41 import com.sun.star.frame.XDesktop;
42 import com.sun.star.frame.XFramesSupplier;
43 import com.sun.star.frame.XFrames;
44 import com.sun.star.registry.XRegistryKey;
45 import com.sun.star.comp.loader.FactoryHelper;
46 import com.sun.star.container.XIndexAccess;
47 import com.sun.star.beans.XPropertySet;
48 import com.sun.star.uno.UnoRuntime;
49 import com.sun.star.uno.AnyConverter;
50 import com.sun.star.frame.XComponentLoader;
51 import com.sun.star.awt.Rectangle;
52 import com.sun.star.util.XCloseable;
53 import helper.ConfigurationRead;
54 import complexlib.ComplexTestCase;
55 import helper.OfficeProvider;
56 import complex.persistent_window_states.DocumentHandle;
58 /**
59 * Parameters:
60 * <ul>
61 * <li>NoOffice=yes - StarOffice is not started initially.</li>
62 * </ul>
64 public class PersistentWindowTest extends ComplexTestCase {
66 private XMultiServiceFactory xMSF;
67 private OfficeProvider oProvider;
68 private int iOfficeCloseTime = 0;
70 /**
71 * A frunction to tell the framework, which test functions are available.
72 * Right now, it's only 'checkPersistentWindowState'.
73 * @return All test methods.
75 public String[] getTestMethodNames() {
76 return new String[]{"checkPersistentWindowState"};
79 /**
80 * Test if all available document types change the
81 * persistent Window Attributes
83 * The test follows basically these steps:
84 * - Create a configuration reader and a componentloader
85 * - Look for all document types in the configuration
86 * - Do for every doc type
87 * - start office
88 * - read configuration attibute settings
89 * - create a new document
90 * - resize the document and close it
91 * - close office
92 * - start office
93 * - read configuration attribute settings
94 * - create another new document
95 * - compare old settings with new ones: should be different
96 * - compare the document size with the resized document: should be equal
97 * - close office
98 * - Test finished
100 public void checkPersistentWindowState()
102 try {
104 log.println("Connect the first time.");
105 log.println("AppExecCommand: " + (String)param.get("AppExecutionCommand"));
106 log.println("ConnString: " + (String)param.get("ConnectionString"));
107 oProvider = new OfficeProvider();
108 iOfficeCloseTime = param.getInt("OfficeCloseTime");
109 if ( iOfficeCloseTime == 0 ) {
110 iOfficeCloseTime = 1000;
113 if (!connect()) return;
115 // create the configuration provider
116 Object o = null;
117 try {
118 o = xMSF.createInstance(
119 "com.sun.star.configuration.ConfigurationProvider");
121 catch(com.sun.star.uno.Exception e) {
122 failed("Cannot create \"com.sun.star.configuration."+
123 "ConfigurationProvider\"");
124 return;
127 // fetch the multi service factory for setup
128 XMultiServiceFactory xCP = (XMultiServiceFactory)
129 UnoRuntime.queryInterface(XMultiServiceFactory.class, o);
131 // create the configuration reader
132 ConfigurationRead cfgRead = new ConfigurationRead(xCP);
134 // just test the wrong ones, not all.
135 String [] els = new String[]{
136 "Office/Factories/com.sun.star.drawing.DrawingDocument",
137 "Office/Factories/com.sun.star.formula.FormulaProperties",
138 //"Office/Factories/com.sun.star.presentation.PresentationDocument",
139 "Office/Factories/com.sun.star.sheet.SpreadsheetDocument",
140 "Office/Factories/com.sun.star.text.GlobalDocument",
141 "Office/Factories/com.sun.star.text.TextDocument",
142 "Office/Factories/com.sun.star.text.WebDocument",
144 // uncomment the following line for all doc types
145 // String [] els = cfgRead.getSubNodeNames("Office/Factories");
147 log.println("Found "+ els.length + " document types to test.\n");
148 if (!disconnect()) return;
150 // for all types
151 for(int i=0; i<els.length; i++) {
152 log.println("\tStart test for document type " + i + ": " + els[i]);
153 // exclude chart documents: cannot be created this way.
154 if ( els[i].indexOf("ChartDocument") != -1) {
155 log.println("Skipping chart document: cannot be create like this.");
156 continue;
159 // start an office
160 if (!connect()) return;
162 // get configuration
163 String[] settings = getConfigurationAndLoader(xMSF, els[i]);
164 if (settings == null) {
165 log.println("Skipping document type " + els[i]);
166 disconnect();
167 continue;
169 String cfg = settings[1];
171 // load a document
172 DocumentHandle handle = loadDocument(xMSF, settings[0]);
174 // first size
175 Rectangle rect1 = handle.getDocumentPosSize();
177 // resize
178 handle.resizeDocument();
179 // after resize
180 Rectangle rect2 = handle.getDocumentPosSize();
182 // disposeManager and start a new office
183 if (!disconnect()) return;
185 if (!connect()) return;
187 // get configuration
188 settings = getConfigurationAndLoader(xMSF, els[i]);
190 String newCfg = settings[1];
192 // load a document
193 handle = loadDocument(xMSF, settings[0]);
195 Rectangle newRect = handle.getDocumentPosSize();
197 // print the settings and window sizes
198 log.println("----------------------------");
199 log.println("Initial Config String : " + cfg);
200 log.println("Config String after restart: " + newCfg);
202 log.println("----------------------------");
203 log.println("Initial window (X,Y,Width,Height): "
204 +rect1.X+";"+rect1.Y+";"+ rect1.Width+";"+rect1.Height);
205 log.println("Window after resize (X,Y,Width,Height): "
206 +rect2.X+";"+rect2.Y+";"+ rect2.Width+";"+rect2.Height);
207 log.println("Window after restart (X,Y,Width,Height): "
208 +newRect.X+";"+newRect.Y+";"+newRect.Width+";"
209 +newRect.Height);
211 // compare to see if resize worked
212 log.println("----------------------------");
213 assure("Resize values for "+ els[i] +
214 " are equal.", !compareRectangles(rect1, rect2), true);
215 // compare settings and sizes
216 assure("Config settings for "+ els[i] +
217 " were not changed.", !cfg.equals(newCfg), true);
218 assure("Resized and restarted window for "+ els[i] +
219 " are not equal.", compareRectangles(rect2, newRect), true);
220 log.println("----------------------------");
222 // disposeManager
223 if (!disconnect()) return;
225 log.println("\tFinish test for document type " + i + ": " + els[i]);
229 catch(Exception e) {
230 e.printStackTrace();
235 * Get the configuration settings and the document loader
236 * @param xMSF A MultiServiceFactory from an office
237 * @param cfgString A configuration string
238 * @return Settings and Loader
240 private static String[] getConfigurationAndLoader(XMultiServiceFactory xMSF,
241 String cfgString) {
242 String[] conf = new String[2];
244 try {
245 Object o = xMSF.createInstance(
246 "com.sun.star.configuration.ConfigurationProvider");
248 // fetch the multi service factory for setup
249 XMultiServiceFactory xCP = (XMultiServiceFactory)
250 UnoRuntime.queryInterface(XMultiServiceFactory.class, o);
252 // create the configuration reader
253 ConfigurationRead cfgRead = new ConfigurationRead(xCP);
255 // get the document loader
256 String loader = getStringFromObject(
257 cfgRead.getByHierarchicalName(cfgString + "/ooSetupFactoryEmptyDocumentURL"));
259 if (loader == null) return null;
260 log.println("\tLoader: " + loader);
262 // read attributes
263 String hierchName = cfgString + "/ooSetupFactoryWindowAttributes";
264 String setupSettings = getStringFromObject(cfgRead.getByHierarchicalName(hierchName));
265 // remove slots: just plain document types have to start
266 if ( loader.indexOf("?slot") != -1 ) {
267 loader = loader.substring(0, loader.indexOf("?slot"));
268 System.out.println("Loader: "+loader);
271 conf[0] = loader;
272 conf[1] = setupSettings;
274 catch(com.sun.star.uno.Exception e) {}
275 return conf;
279 * Load a document
280 * @param xMSF A MultiServiceFactory from an office
281 * @param docLoader A documet loader
282 * @return A handle to the document
284 private DocumentHandle loadDocument(XMultiServiceFactory xMSF,
285 String docLoader) {
286 DocumentHandle docHandle = null;
287 try {
288 // create component loaader
289 XComponentLoader xCompLoader = (XComponentLoader)
290 UnoRuntime.queryInterface(
291 XComponentLoader.class, xMSF.createInstance(
292 "com.sun.star.frame.Desktop"));
293 XFramesSupplier xFrameSupp = (XFramesSupplier)UnoRuntime.queryInterface(XFramesSupplier.class, xCompLoader);
294 // close all existing frames
295 XFrames xFrames = xFrameSupp.getFrames();
296 XIndexAccess xAcc = (XIndexAccess)UnoRuntime.queryInterface(XIndexAccess.class, xFrames);
297 for ( int i=0; i<xAcc.getCount(); i++ ) {
298 XCloseable xClose = (XCloseable)UnoRuntime.queryInterface(XCloseable.class, xAcc.getByIndex(i));
299 try {
300 if ( xClose != null ) {
301 xClose.close(false);
303 else {
304 failed("Could not query frame for XCloseable!");
307 catch( com.sun.star.uno.Exception e ) {
308 e.printStackTrace((java.io.PrintWriter)log);
309 failed("Could not query frame for XCloseable!");
312 docHandle = new DocumentHandle(xCompLoader);
313 docHandle.loadDocument(docLoader, false);
315 catch(com.sun.star.uno.Exception e) {
316 e.printStackTrace();
318 catch(java.lang.Exception e) {
319 e.printStackTrace();
321 return docHandle;
324 private boolean connect() {
325 try {
326 xMSF = (XMultiServiceFactory)oProvider.getManager(param);
327 try {
328 Thread.sleep(10000);
330 catch(java.lang.InterruptedException e) {}
332 catch (java.lang.Exception e) {
333 log.println(e.getClass().getName());
334 log.println("Message: " + e.getMessage());
335 failed("Cannot connect the Office.");
336 return false;
338 return true;
341 private boolean disconnect() {
342 try {
343 XDesktop desk = null;
344 desk = (XDesktop) UnoRuntime.queryInterface(
345 XDesktop.class, xMSF.createInstance(
346 "com.sun.star.frame.Desktop"));
347 xMSF = null;
348 desk.terminate();
349 log.println("Waiting " + iOfficeCloseTime + " milliseconds for the Office to close down");
350 try {
351 Thread.sleep(iOfficeCloseTime);
353 catch(java.lang.InterruptedException e) {}
355 catch (java.lang.Exception e) {
356 e.printStackTrace();
357 failed("Cannot dispose the Office.");
358 return false;
360 return true;
363 private static String getStringFromObject(Object oName) {
364 if (oName instanceof String)
365 return (String)oName;
366 String value = null;
367 if (oName instanceof Any) {
368 try {
369 value = AnyConverter.toString(oName);
370 if (value == null) {
371 log.println("Got a void css.uno.Any as loading string.");
374 catch(Exception e) {
375 log.println("This document type cannot be opened directly.");
378 return value;
382 * Compare two rectangles. Return true, if both are equal, false
383 * otherwise.
384 * @param rect1 First Rectangle.
385 * @param rect2 Second Rectangle.
386 * @return True, if the rectangles are equal.
388 private boolean compareRectangles(Rectangle rect1, Rectangle rect2) {
389 boolean result = true;
390 result &= (rect1.X==rect2.X);
391 result &= (rect1.Y==rect2.Y);
392 result &= (rect1.Width==rect2.Width);
393 result &= (rect1.Height==rect2.Height);
394 return result;