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