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 24 package complex.framework.recovery; 25 26 import com.sun.star.accessibility.XAccessible; 27 import com.sun.star.accessibility.XAccessibleContext; 28 import com.sun.star.awt.Rectangle; 29 import com.sun.star.awt.Size; 30 import com.sun.star.awt.XDialog; 31 import com.sun.star.awt.XExtendedToolkit; 32 import com.sun.star.awt.XWindow; 33 import com.sun.star.beans.XPropertySet; 34 import com.sun.star.container.XEnumeration; 35 import com.sun.star.container.XIndexAccess; 36 import com.sun.star.container.XNameContainer; 37 import com.sun.star.drawing.XDrawPage; 38 import com.sun.star.drawing.XDrawPages; 39 import com.sun.star.drawing.XDrawPagesSupplier; 40 import com.sun.star.drawing.XShape; 41 import com.sun.star.drawing.XShapes; 42 import com.sun.star.frame.XController; 43 import com.sun.star.frame.XDesktop; 44 import com.sun.star.frame.XDispatch; 45 import com.sun.star.frame.XDispatchProvider; 46 import com.sun.star.frame.XFrame; 47 import com.sun.star.frame.XModel; 48 import com.sun.star.lang.XComponent; 49 import com.sun.star.lang.XMultiServiceFactory; 50 import com.sun.star.lang.XSingleServiceFactory; 51 import com.sun.star.presentation.XCustomPresentationSupplier; 52 import com.sun.star.presentation.XPresentationSupplier; 53 import com.sun.star.sheet.XSheetCellRange; 54 import com.sun.star.sheet.XSpreadsheet; 55 import com.sun.star.sheet.XSpreadsheetDocument; 56 import com.sun.star.sheet.XSpreadsheets; 57 import com.sun.star.style.XStyle; 58 import com.sun.star.table.XCellRange; 59 import com.sun.star.text.ControlCharacter; 60 import com.sun.star.text.XText; 61 import com.sun.star.text.XTextCursor; 62 import com.sun.star.text.XTextDocument; 63 import com.sun.star.uno.AnyConverter; 64 import com.sun.star.uno.Type; 65 import com.sun.star.uno.UnoRuntime; 66 import com.sun.star.uno.XInterface; 67 import com.sun.star.view.XSelectionSupplier; 68 import helper.OfficeProvider; 69 import java.awt.Dimension; 70 import java.awt.Point; 71 import java.awt.Toolkit; 72 import java.io.PrintWriter; 73 import java.util.Hashtable; 74 import java.util.Random; 75 import stats.SimpleLogWriter; 76 import util.DesktopTools; 77 import util.PropertyName; 78 import util.SOfficeFactory; 79 import util.UITools; 80 import util.utils; 81 82 // ---------- junit imports ----------------- 83 import org.junit.After; 84 import org.junit.AfterClass; 85 import org.junit.Before; 86 import org.junit.BeforeClass; 87 import org.junit.Test; 88 import org.openoffice.test.Argument; 89 import org.openoffice.test.OfficeConnection; 90 import static org.junit.Assert.*; 91 // ------------------------------------------ 92 93 public class RecoveryTest { 94 private static OfficeConnection officeConnection = new OfficeConnection(); 95 static XMultiServiceFactory xMSF; 96 static SOfficeFactory SOF; 97 static RecoveryTools rt; 98 /** 99 * If you divide the screen in four parts in the first of them the office 100 * windows should be placed. The range of the first quarter is stored in the variable. 101 */ 102 static Point windowMaxPosition; 103 /** 104 * The office windows starts in the first quarter of the screen. In this variable 105 * the maximum size for the windows was stored so the windows can be placed 106 * visible on the screen. 107 */ 108 static Size windowMaxSize; 109 110 /** 111 * All office windows will be placed by this test on randomized positions. 112 * This positions was stored in this Hashmap. The keys are the frame names 113 * and the values are com sun.star.awt.Rectangle. 114 * @see com.sun.star.awt.Rectangle 115 */ 116 private Hashtable windowsPosSize = new Hashtable(); 117 118 /** Create the environment for following tests. 119 * Use either a component loader from desktop or 120 * from frame 121 * @throws Exception Exception 122 */ 123 @BeforeClass beforeClass()124 public static void beforeClass() throws Exception { 125 officeConnection.setUp(); 126 } 127 128 @AfterClass afterClass()129 public static void afterClass() throws Exception { 130 officeConnection.tearDown(); 131 } 132 normalCrash()133 public void normalCrash(){ 134 cleanRecoveryData(); 135 startOffice(); 136 generateDesktop(); 137 makeCrash(); 138 int expectedDocumentCount = windowsPosSize.size() + 1; 139 handleRecoveryDialogAfterCrash(expectedDocumentCount); 140 startOffice(); 141 handleRecoveryDialog_QuickExit(expectedDocumentCount); 142 handleCrashReporterDialog(true, true); 143 checkDocumentCount(expectedDocumentCount); 144 } 145 146 @Test testCrash()147 public void testCrash(){ 148 cleanRecoveryData(); 149 restoreBackupRecoveryData(); 150 startOffice(); 151 int expectedDocumentCount = 3; 152 // handleRecoveryDialog_QuickExit(expectedDocumentCount); 153 handleRecoveryDialog_QuickExitAndSave(expectedDocumentCount); 154 //handleCrashReporterDialog(true, true); 155 //checkDocumentCount(expectedDocumentCount); 156 } 157 158 @Before before()159 public void before() throws Exception { 160 161 String msg ="\n\n\tPATH TO OFFICE BINARY MISSING!\n"; 162 msg +="\tPlease run your command with the following parameter:\n\n"; 163 msg +="\t-AppExecutionCommand=OFFICEBINARY CONNECTIONSTRING\n\n"; 164 msg +="Example Windows:\n"; 165 msg +="-AppExecutionCommand=C:\\office\\soffice.exe -accept=socket,host=localhost,port=8101;urp;\n\n"; 166 msg +="Example UNIX:\n"; 167 msg +="-AppExecutionCommand=/office/soffice \"-accept=socket,host=localhost,port=8101;urp;\"\n\n"; 168 msg+="NOTE: on UNIX be sure to have the connection string inside quotation mark!\n"; 169 170 // assertTrue(msg, param.get("AppExecutionCommand") != null && ! param.get("AppExecutionCommand").equals("")); 171 // System.out.println("HALLO" + param.get("AppExecutionCommand")); 172 msg = "\n\nONE PARAMETER IS MISSING!\n"; 173 msg += "Please append to your command the following parameter:\n\n"; 174 msg += "\t-NoOffice=true"; 175 // assertTrue(msg, param.getBool("NoOffice")); 176 177 xMSF = UnoRuntime.queryInterface(XMultiServiceFactory.class, officeConnection.getComponentContext().getServiceManager()); 178 rt = new RecoveryTools(xMSF); 179 180 rt.removeParametersFromAppExecutionCommand(); 181 182 System.out.println("start the office to test recovery feature..."); 183 184 // make window ranges 185 makeWindowPositionRage(); 186 187 //makeRecoveryData(); 188 } 189 makeRecoveryData()190 private void makeRecoveryData(){ 191 cleanRecoveryData(); 192 startOffice(); 193 generateDesktop(); 194 makeCrash(); 195 int expectedDocumentCount = windowsPosSize.size() + 1; 196 handleRecoveryDialogAfterCrash(expectedDocumentCount); 197 backupRecoveryData(); 198 cleanRecoveryData(); 199 } 200 startOffice()201 private void startOffice(){ 202 assertTrue("Could not connect to office", connect()); 203 } 204 205 checkDocumentCount(int expectedDocumentCount)206 private void checkDocumentCount(int expectedDocumentCount){ 207 XEnumeration allComp = DesktopTools.getAllComponents(xMSF); 208 int documentCount = 0; 209 210 try{ 211 while (allComp.hasMoreElements()){ 212 allComp.nextElement(); 213 documentCount ++; 214 } 215 } 216 catch ( com.sun.star.container.NoSuchElementException e){} 217 catch ( com.sun.star.lang.WrappedTargetException e){} 218 219 String msg ="The amount of documents to recover is different form the expected amount:\n"; 220 msg += "\texpected:\t" + expectedDocumentCount + "\n"; 221 msg += "\tto recover:\t" + documentCount; 222 223 assertTrue(msg, expectedDocumentCount == documentCount); 224 } 225 226 /** 227 * This function starts an office instance. It uses the AppExecutionCommad parameter. 228 * @return TRUE if office is connected otherwise FALSE 229 */ connect()230 private boolean connect(){ 231 try { 232 233 SOF = SOfficeFactory.getFactory(xMSF); 234 235 } 236 catch (java.lang.Exception e) { 237 System.out.println(e.getClass().getName()); 238 System.out.println("Message: " + e.getMessage()); 239 fail("Cannot connect the office."); 240 return false; 241 } 242 return true; 243 } 244 245 /** 246 * While creating the test environment the positions and sizes of the frames 247 * was saved. After the office has recovered the documents, this functions 248 * compares the saved positions and sizes with the current frame. 249 */ compareWindowPositions()250 private void compareWindowPositions(){ 251 System.out.println("all frames:########"); 252 System.out.println(windowsPosSize.entrySet().toString()); 253 254 XEnumeration allComp = DesktopTools.getAllComponents(xMSF); 255 256 String msg=null; 257 258 while (allComp.hasMoreElements()){ 259 try{ 260 // get all components from the desktop 261 XComponent xComponent = (XComponent) UnoRuntime.queryInterface( 262 XComponent.class, allComp.nextElement()); 263 264 XModel xModel = (XModel) UnoRuntime.queryInterface(XModel.class, xComponent); 265 266 String frameName = xModel.getCurrentController().getFrame().getName(); 267 268 // check if this frame was used in creation of test environment 269 if (windowsPosSize.containsKey(frameName)){ 270 271 Rectangle oldRect = (Rectangle) windowsPosSize.get(frameName); 272 273 XWindow xWindow = xModel.getCurrentController().getFrame().getContainerWindow(); 274 Rectangle newRect = xWindow.getPosSize(); 275 276 277 boolean ok = oldRect.Height == newRect.Height; 278 ok &= oldRect.Width == newRect.Width; 279 ok &= oldRect.X == newRect.X; 280 ok &= oldRect.Y == newRect.Y; 281 282 if (!ok){ 283 msg = "The frame '" + frameName + "' has a different position/size:\n"; 284 msg += "original value -> restored value:\n"; 285 msg += "X : " + oldRect.X + " -> " + newRect.X + "\n"; 286 msg += "Y : " + oldRect.Y + " -> " + newRect.Y + "\n"; 287 msg += "Height: " + oldRect.Height + " -> " + newRect.Height + "\n"; 288 msg += "Width : " + oldRect.Width + " -> " + newRect.Width + "\n"; 289 } 290 291 assertTrue(msg, ok); 292 293 } 294 } catch (com.sun.star.container.NoSuchElementException e) { 295 } catch ( com.sun.star.lang.WrappedTargetException e) {} 296 } 297 298 } 299 300 /** 301 * This function crashes the office 302 */ makeCrash()303 private void makeCrash(){ 304 // get all documents 305 Object[] allDocs = DesktopTools.getAllOpenDocuments(xMSF); 306 307 // get one of them for dispatching 308 XComponent xDoc = (XComponent) allDocs[0]; 309 System.out.println("make the crash in second thread"); 310 311 CrashThread crash = new CrashThread(xDoc, xMSF); 312 crash.start(); 313 rt.pause(); 314 rt.pause(); 315 } 316 317 /** 318 * This function uses accessibility to handle the dialog which appears while the 319 * office is crashed. It click the button "OK" to continue. 320 */ handleRecoveryDialogAfterCrash(int expectedDocumentCount)321 private void handleRecoveryDialogAfterCrash(int expectedDocumentCount){ 322 try{ 323 324 // if the office crashes, the recovery feature needs some time 325 // to save all docs. Therefore the recovery dialog could need some 326 // time to pop up. 327 System.out.println("waiting for recovery dialog..."); 328 329 int counter = 0; 330 int maximum = Integer.parseInt(Argument.get("THREAD_TIME_OUT")) / Integer.parseInt(Argument.get("SHORT_WAIT")); 331 332 XDialog oDialog = rt.getActiveDialog(xMSF); 333 334 while ( oDialog == null && (counter < maximum)) 335 { 336 rt.pause(); 337 oDialog = rt.getActiveDialog(xMSF); 338 counter ++; 339 } 340 341 assertTrue("could not get Recovery Window",(oDialog != null)); 342 343 XWindow xWindow = (XWindow) UnoRuntime.queryInterface(XWindow.class, oDialog); 344 345 UITools oUITools = new UITools(xMSF, xWindow); 346 347 oUITools.printAccessibleTree((PrintWriter) new SimpleLogWriter(), Boolean.parseBoolean(Argument.get("DEBUG_IS_ACTIVE"))); 348 349 String[] documents = oUITools.getListBoxItems("The following files will be recovered"); 350 System.out.println("there are " + documents.length + " documents to save"); 351 352 String msg ="The amount of documents to recover is different form the expected amount:\n"; 353 msg += "\texpected:\t" + expectedDocumentCount + "\n"; 354 msg += "\tto recover:\t" + documents.length; 355 356 assertTrue(msg, expectedDocumentCount == documents.length); 357 358 System.out.println("disable automatically launch of Office"); 359 oUITools.setCheckBoxValue("Launch OpenOffice automatically", new Integer(0)); 360 361 System.out.println("start saving..."); 362 oUITools.clickButton("OK"); 363 364 rt.waitForClosedOffice(); 365 366 } catch (Exception e){ 367 e.printStackTrace(); 368 fail("Could not handle crash-dialog: " + e.toString()); 369 } 370 } 371 handleCrashReporterDialog(boolean cancel, boolean YesNo)372 private void handleCrashReporterDialog(boolean cancel, boolean YesNo){ 373 try{ 374 375 System.out.println("try to get Crash Reporter Dialog..."); 376 377 XDialog oDialog = rt.getActiveDialog(xMSF); 378 assertTrue("could not get CrashReporter Dialog", oDialog != null); 379 380 XWindow xWindow = (XWindow) UnoRuntime.queryInterface(XWindow.class, oDialog); 381 382 System.out.println(oDialog.getTitle()); 383 384 UITools oUITools = new UITools(xMSF, xWindow); 385 386 if (cancel) { 387 System.out.println("clicking 'Cancel' button..."); 388 389 try{ 390 rt.clickThreadButton(xMSF, xWindow, "Cancel"); 391 } catch (com.sun.star.accessibility.IllegalAccessibleComponentStateException e){ 392 fail("Could not click 'Cancel' at CrashReporter Dialog"); 393 } 394 395 } 396 else { 397 System.out.println("clicking 'Next' button..."); 398 oUITools.clickButton("Next >"); 399 } 400 401 } catch (Exception e){ 402 fail("Could not handle CrashReporter Dialog: " + e.toString()); 403 } 404 } 405 handleRecoveryDialog_QuickExit(int expectedDocumentCount)406 private void handleRecoveryDialog_QuickExit(int expectedDocumentCount){ 407 System.out.println("handle Recovery Dialog at restart: quick exit"); 408 handleRecoveryDialogAtRestart(expectedDocumentCount, false, true); 409 handleAreYouSureDialog(true); 410 handleSaveDocumentsDialog(false); 411 412 } handleRecoveryDialog_QuickExitAndSave(int expectedDocumentCount)413 private void handleRecoveryDialog_QuickExitAndSave(int expectedDocumentCount){ 414 System.out.println("handle Recovery Dialog at restart: quick exit"); 415 handleRecoveryDialogAtRestart(expectedDocumentCount, false, true); 416 handleAreYouSureDialog(true); 417 handleSaveDocumentsDialog(true); 418 } handleRecoveryDialog_Recover(int expectedDocumentCount)419 private void handleRecoveryDialog_Recover(int expectedDocumentCount){ 420 421 } handleRecoveryDialog_RecoverAndCrashreporter(int expectedDocumentCount)422 private void handleRecoveryDialog_RecoverAndCrashreporter(int expectedDocumentCount){ 423 424 } 425 /** 426 * This function uses accessibility to handle the dialog which appears while the 427 * office is started after a crash. It waits until the "Next" button is enabled 428 * and click it then to continue. 429 * @param expectedDocumentCount the amount of documents which must be displayed in the recovery dialog 430 * @param recover If the document should be recovered this variable must be true. If it is false 431 * the recovery process was stopped and the button cancel was clicked. 432 * @param cancel If the recovery is finished, this parameter decides to click the "Next" button 433 * or the click cancel. If the value is true, the cancel button was clicked. 434 */ handleRecoveryDialogAtRestart(int expectedDocumentCount, boolean recover, boolean cancel)435 private void handleRecoveryDialogAtRestart(int expectedDocumentCount, boolean recover, boolean cancel){ 436 try{ 437 438 System.out.println("try to get Recovery Dialog..."); 439 440 XDialog oDialog = null; 441 oDialog = rt.getActiveDialogAfterStartup(xMSF); 442 443 assertTrue("could not get Recovery Dialog at start of office", (oDialog != null)); 444 445 XWindow xWindow = (XWindow) UnoRuntime.queryInterface(XWindow.class, oDialog); 446 System.out.println("got the following dialog: '" +oDialog.getTitle() + "'"); 447 448 UITools oUITools = new UITools(xMSF, xWindow); 449 450 String listBoxName = "Status of recovered documents"; 451 String[] documents = oUITools.getListBoxItems(listBoxName); 452 System.out.println("there are " + documents.length + " documents to recover"); 453 System.out.println("The following files will be recovered:"); 454 for (int i=0;i<documents.length;i++){ 455 System.out.println(documents[i]); 456 } 457 458 String msg ="The amount of documents to recover is different form the expected amount:\n"; 459 msg += "\texpected:\t" + expectedDocumentCount + "\n"; 460 msg += "\tto recover:\t" + documents.length; 461 462 assertTrue(msg, expectedDocumentCount ==documents.length); 463 464 if (recover){ 465 466 System.out.println("clicking 'Start Recovery' button..."); 467 oUITools.clickButton("Start Recovery >"); 468 469 rt.pause(); 470 471 //XAccessibleContext oButton = oUITools.getButton("Start Recovery >"); 472 int counter = 0; 473 int maximum = Integer.parseInt(Argument.get("THREAD_TIME_OUT")) / Integer.parseInt(Argument.get("SHORT_WAIT")); 474 //boolean enabeld = oButton.getAccessibleStateSet().contains(com.sun.star.accessibility.AccessibleStateType.ENABLED); 475 476 XAccessibleContext oButton = null; 477 while ((oButton == null) && (counter < maximum)){ 478 System.out.println("recovering..."); 479 480 try{ 481 oButton = oUITools.getButton("Next >"); 482 } catch (java.lang.NullPointerException e){ 483 // no fault: The title "Start Recovery" switches to "Next" 484 // while all documents are recovered 485 } 486 rt.pause(); 487 counter++; 488 } 489 490 if (cancel) { 491 System.out.println("clicking 'Cancel' button..."); 492 493 try{ 494 rt.clickThreadButton(xMSF, xWindow, "Cancel"); 495 } catch (com.sun.star.accessibility.IllegalAccessibleComponentStateException e){ 496 fail("Could not click 'Cancel' at recovery-dialog."); 497 } 498 499 } 500 else { 501 System.out.println("clicking 'Next' button..."); 502 oUITools.clickButton("Next >"); 503 } 504 505 rt.pause(); 506 507 } else { 508 System.out.println("do not recover: clicking 'Cancel' button..."); 509 510 try{ 511 rt.clickThreadButton(xMSF, xWindow, "Cancel"); 512 } catch (com.sun.star.accessibility.IllegalAccessibleComponentStateException e){ 513 fail("Could not click 'Cancel' at recovery-dialog"); 514 } 515 } 516 517 } catch (Exception e){ 518 fail("Could not handle recovery-dialog at restart: " + e.toString()); 519 } 520 521 } 522 523 /** 524 * This function uses accessibility to handle the dialog "Are you sure". 525 * It click "Yes" or "No", depending on the value of the parameter <CODE>Yes</CODE> 526 * @param yes If value is <CODE>TRUE</CODE> the button "Yes" was clicked, otherwise the button 527 * "No". 528 */ handleAreYouSureDialog(boolean yes)529 private void handleAreYouSureDialog(boolean yes) 530 { 531 try{ 532 if (yes){ 533 rt.handleModalDialog(xMSF, "Yes"); 534 } else{ 535 rt.handleModalDialog(xMSF, "Cancel"); 536 } 537 } catch (com.sun.star.accessibility.IllegalAccessibleComponentStateException e){ 538 fail("Could not handle 'Are you sure' dialog."); 539 } 540 } 541 542 /** 543 * This function uses accessibility to handle the dialog "Are you sure". 544 * It click "Yes" or "No", depending on the value of the parameter <CODE>Yes</CODE> 545 * @param saveDocuments If value is <CODE>TRUE</CODE> the button "Yes" was clicked, otherwise the button 546 * "No". 547 */ handleSaveDocumentsDialog(boolean saveDocuments)548 private void handleSaveDocumentsDialog(boolean saveDocuments) 549 { 550 try{ 551 if (!saveDocuments){ 552 rt.handleModalDialog(xMSF, "Cancel"); 553 } else{ 554 XWindow oDialog = null; 555 oDialog = rt.getActiveWindow(xMSF); 556 557 assertTrue("could not get 'Save Documents' Dialog: ", (oDialog != null)); 558 559 UITools oUITools = new UITools(xMSF, oDialog); 560 561 oUITools.printAccessibleTree((PrintWriter) new SimpleLogWriter(), Boolean.parseBoolean(Argument.get("DEBUG_IS_ACTIVE"))); 562 563 String listBoxName = "Documents"; 564 String[] documents = null; 565 try{ 566 documents = oUITools.getListBoxItems(listBoxName); 567 } catch (java.lang.Exception e){ 568 fail("could not get the document names from the 'Save Documents' dialog"); 569 } 570 System.out.println("there are " + documents.length + " documents to save"); 571 System.out.println("The following documents will be saved:"); 572 for (int i=0;i<documents.length;i++){ 573 System.out.println(documents[i]); 574 } 575 String tempURL = utils.getOfficeTempDir(xMSF); 576 577 System.out.println("the destination for saving is: " + tempURL); 578 try{ 579 oUITools.setTextEditFiledText("Save to", tempURL); 580 } catch (java.lang.Exception e){ 581 fail("could not set target directory for saving documents at 'Save Documents' dialog"); 582 } 583 try{ 584 oUITools.clickButton("OK"); 585 } catch (java.lang.Exception e){ 586 fail("could not click 'OK' at 'Save Documents' dialog"); 587 } 588 } 589 } catch (com.sun.star.accessibility.IllegalAccessibleComponentStateException e){ 590 fail("Could not handle 'Are you sure' dialog."); 591 } 592 } 593 594 /** 595 * This function gets the current screen size and calculate the first 596 * quarter of it. This quarter was used to position to office windows. 597 * Further this function calculates the maximum window size so the window 598 * is visible if it placed on extreme position. 599 */ makeWindowPositionRage()600 private void makeWindowPositionRage(){ 601 Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize(); 602 Point pos = new Point(); 603 Size size = new Size(); 604 605 // get the max position of the first quarter of the screen 606 pos.x = screenDim.width / 2; 607 pos.y = screenDim.height / 2; 608 windowMaxPosition = pos; 609 610 // get the max size of the windows while they placed in windowMaxPosition 611 // range and not outside the visibility 612 size.Height = screenDim.height; 613 size.Width = screenDim.width; 614 windowMaxSize = size; 615 } 616 generateDesktop()617 private void generateDesktop(){ 618 619 // create some documents with content 620 makeWriterDoc("WriterDoc1", true); 621 // makeCalcDoc("CalcDoc1", true); 622 // makeDrawDoc("DrawDoc1", true); 623 // makeImpressDoc("ImpressDoc1", true); 624 // makeMathDoc("MathDoc1", true); 625 626 // create some documents without content 627 // makeMathDoc("_blank_math", false); 628 // makeDrawDoc("_blank_draw", false); 629 // makeCalcDoc("_blank_calc", false); 630 // makeWriterDoc("_blank_writer", false); 631 // makeImpressDoc("_blank_impress", false); 632 633 // makeMathDoc("MathDocEmpty", false); 634 // makeDrawDoc("DrawDocEmpty", false); 635 // makeCalcDoc("CalcDocEmpty", false); 636 makeWriterDoc("WriterDocEmpty", false); 637 // makeImpressDoc("ImpressDocEmpty", false); 638 639 System.out.println("Test object successfully created."); 640 641 } 642 makeImpressDoc(String frameName, boolean withContent)643 private void makeImpressDoc(String frameName, boolean withContent){ 644 System.out.println("creating Impress document '" + frameName + "'"); 645 XComponent xImpressDoc = createNewImpressDoc(frameName); 646 if (withContent) fillImpressDocWithContent(xImpressDoc); 647 positioningDocument((XModel) UnoRuntime.queryInterface(XModel.class, 648 xImpressDoc)); 649 } 650 makeDrawDoc(String frameName, boolean withContent)651 private void makeDrawDoc(String frameName, boolean withContent){ 652 System.out.println("creating Draw document '" + frameName + "'"); 653 XComponent xDrawDoc = createNewDrawDoc(frameName); 654 if (withContent) fillDrawDocWithContent(xDrawDoc); 655 positioningDocument((XModel) UnoRuntime.queryInterface(XModel.class, 656 xDrawDoc)); 657 } 658 makeCalcDoc(String frameName, boolean withContent)659 private void makeCalcDoc(String frameName, boolean withContent){ 660 System.out.println("creating Calc document '" + frameName + "'"); 661 XSpreadsheetDocument xSpreadsheetDoc = createNewCalcDoc(frameName); 662 if (withContent) fillCalcDocWithContent(xSpreadsheetDoc); 663 positioningDocument((XModel) UnoRuntime.queryInterface(XModel.class, 664 xSpreadsheetDoc)); 665 } 666 positioningDocument(XModel model)667 private void positioningDocument(XModel model){ 668 669 XWindow xWindow = model.getCurrentController().getFrame().getContainerWindow(); 670 String frameName = model.getCurrentController().getFrame().getName(); 671 672 // get randomized position and size 673 Rectangle posSize = makePosZize(); 674 675 // save position and size 676 windowsPosSize.put(frameName, posSize); 677 678 xWindow.setPosSize(posSize.X, posSize.Y, posSize.Width, posSize.Height, 679 com.sun.star.awt.PosSize.POSSIZE); 680 Rectangle test = xWindow.getPosSize(); 681 System.out.println("x: "+test.X+" y:"+test.Y+" width:"+test.Width+" height:"+test.Height); 682 } 683 makePosZize()684 private Rectangle makePosZize(){ 685 686 Rectangle posSize = new Rectangle(); 687 Random rand = new Random(); 688 689 // Random integers that range from from 0 to n 690 posSize.X = rand.nextInt(windowMaxPosition.x + 1); 691 posSize.Y = rand.nextInt(windowMaxPosition.y + 1); 692 693 int maxHeight = windowMaxSize.Height-posSize.X; 694 int maxWidth = windowMaxSize.Width-posSize.Y; 695 int height = rand.nextInt(maxHeight + 1); 696 int width = rand.nextInt((windowMaxSize.Width-posSize.Y) + 1); 697 698 // be sure that the new size is greater then the half of windowMaxSize 699 posSize.Height = (height < (maxHeight / 2)) ? height + (maxHeight / 2) : height; 700 posSize.Width = (width < (maxWidth / 2)) ? width + (maxWidth / 2) : width; 701 702 return posSize; 703 } 704 makeMathDoc(String frameName, boolean withContent)705 private void makeMathDoc(String frameName, boolean withContent){ 706 System.out.println("creating Math document '" + frameName + "'"); 707 XComponent xMathDoc = createNewMathDoc(frameName); 708 if (withContent) fillMathDocWithContent(xMathDoc); 709 positioningDocument((XModel) UnoRuntime.queryInterface(XModel.class, 710 xMathDoc)); 711 } 712 createNewMathDoc(String frameName)713 private XComponent createNewMathDoc(String frameName){ 714 XComponent xMathDoc = null; 715 try{ 716 xMathDoc = SOF.createMathDoc(frameName); 717 } catch (com.sun.star.uno.Exception e) { 718 System.out.println("Exception occurred while creating Math document '"+frameName+"':"); 719 fail("Couldn't create test environment"); 720 } 721 return xMathDoc; 722 } 723 fillMathDocWithContent(XComponent xMathDoc)724 private void fillMathDocWithContent(XComponent xMathDoc){ 725 // setting a formula in document 726 final String expFormula = "sum a cdot b"; 727 final XPropertySet xPS = (XPropertySet) UnoRuntime.queryInterface 728 (XPropertySet.class, xMathDoc); 729 try { 730 xPS.setPropertyValue("Formula", expFormula); 731 } catch(com.sun.star.lang.WrappedTargetException e) { 732 System.out.println("Exception occurred while filling Math document with content."); 733 fail("Couldn't create test environment"); 734 } catch(com.sun.star.lang.IllegalArgumentException e) { 735 System.out.println("Exception occurred while filling Math document with content."); 736 fail("Couldn't create test environment"); 737 } catch(com.sun.star.beans.PropertyVetoException e) { 738 System.out.println("Exception occurred while filling Math document with content."); 739 fail("Couldn't create test environment"); 740 } catch(com.sun.star.beans.UnknownPropertyException e) { 741 System.out.println("Exception occurred while filling Math document with content."); 742 fail("Couldn't create test environment"); 743 } 744 } 745 createNewImpressDoc(String frameName)746 private XComponent createNewImpressDoc(String frameName){ 747 XComponent xImpressDoc = null; 748 try{ 749 xImpressDoc = SOF.createImpressDoc(frameName); 750 } catch (com.sun.star.uno.Exception e) { 751 System.out.println("Exception occurred while creating Impress document '"+frameName+"':"); 752 fail("Couldn't create test environment"); 753 } 754 return xImpressDoc; 755 } 756 757 fillImpressDocWithContent(XComponent xImpressDoc)758 private void fillImpressDocWithContent(XComponent xImpressDoc){ 759 760 System.out.println( "get presentation" ); 761 XPresentationSupplier oPS = (XPresentationSupplier) 762 UnoRuntime.queryInterface(XPresentationSupplier.class, xImpressDoc); 763 XInterface oObj = oPS.getPresentation(); 764 765 System.out.println( "get custom presentation" ); 766 XCustomPresentationSupplier oCPS = (XCustomPresentationSupplier) 767 UnoRuntime.queryInterface( 768 XCustomPresentationSupplier.class, xImpressDoc); 769 XNameContainer xCP = oCPS.getCustomPresentations(); 770 771 XInterface oInstance = null; 772 773 XSingleServiceFactory oSingleMSF = (XSingleServiceFactory) 774 UnoRuntime.queryInterface(XSingleServiceFactory.class, xCP); 775 776 try{ 777 oInstance = (XInterface) oSingleMSF.createInstance(); 778 } catch (com.sun.star.uno.Exception e) { 779 System.out.println("Could not create custom presentation while filling Impress document with content."); 780 fail("Couldn't create test environment"); 781 } 782 783 try { 784 xCP.insertByName("FirstPresentation",oInstance); 785 } catch (com.sun.star.lang.WrappedTargetException e) { 786 System.out.println("Could not insert custom presentation while filling Impress document with content."); 787 fail("Couldn't create test environment"); 788 } catch (com.sun.star.container.ElementExistException e) { 789 System.out.println("Could not insert custom presentation while filling Impress document with content."); 790 fail("Couldn't create test environment"); 791 } catch (com.sun.star.lang.IllegalArgumentException e) { 792 System.out.println("Could not insert custom presentation while filling Impress document with content."); 793 fail("Couldn't create test environment"); 794 } 795 } 796 createNewDrawDoc(String frameName)797 private XComponent createNewDrawDoc(String frameName){ 798 XComponent xDrawDoc = null; 799 try{ 800 xDrawDoc = SOF.createDrawDoc(frameName); 801 } catch (com.sun.star.uno.Exception e) { 802 System.out.println("Exception occurred while creating Draw document '"+frameName+"':"); 803 fail("Couldn't create test environment"); 804 } 805 return xDrawDoc; 806 } 807 fillDrawDocWithContent(XComponent xDrawDoc)808 private void fillDrawDocWithContent(XComponent xDrawDoc){ 809 XDrawPagesSupplier oDPS = (XDrawPagesSupplier) 810 UnoRuntime.queryInterface(XDrawPagesSupplier.class, xDrawDoc); 811 XDrawPages oDPn = oDPS.getDrawPages(); 812 XIndexAccess oDPi = (XIndexAccess) 813 UnoRuntime.queryInterface(XIndexAccess.class, oDPn); 814 XDrawPage oDP = null; 815 try { 816 oDP = (XDrawPage) AnyConverter.toObject( 817 new Type(XDrawPage.class),oDPi.getByIndex(0)); 818 } catch (com.sun.star.lang.WrappedTargetException e) { 819 System.out.println("Could not get Draw pages while filling Draw document with content."); 820 fail("Couldn't create test environment"); 821 } catch (com.sun.star.lang.IndexOutOfBoundsException e) { 822 System.out.println("Could not get Draw pages while filling Draw document with content."); 823 fail("Couldn't create test environment"); 824 } catch (com.sun.star.lang.IllegalArgumentException e) { 825 System.out.println("Could not get Draw pages while filling Draw document with content."); 826 fail("Couldn't create test environment"); 827 } 828 829 // get a Shape 830 System.out.println( "getting Shape" ); 831 XShapes oShapes = (XShapes) UnoRuntime.queryInterface 832 (XShapes.class, oDP); 833 XInterface oObj = SOF.createShape 834 (xDrawDoc, 5000, 3500, 7500, 5000, "Rectangle"); 835 for (int i=0; i < 10; i++) { 836 oShapes.add( 837 SOF.createShape(xDrawDoc, 838 5000, 3500, 7510 + 10 * i, 5010 + 10 * i, "Rectangle")); 839 } 840 XShape oShape = SOF.createShape 841 (xDrawDoc, 3000, 4500, 15000, 1000, "Ellipse"); 842 oShapes.add((XShape) oObj); 843 oShapes.add((XShape) oShape); 844 845 846 XPropertySet oShapeProps = (XPropertySet) 847 UnoRuntime.queryInterface(XPropertySet.class, oObj); 848 XStyle aStyle = null; 849 try { 850 aStyle = (XStyle) AnyConverter.toObject( 851 new Type(XStyle.class),oShapeProps.getPropertyValue("Style")); 852 oShapeProps.setPropertyValue("ZOrder", new Integer(1)); 853 } catch (com.sun.star.lang.WrappedTargetException e) { 854 System.out.println("Exception occurred while setting or getting property value while filling Draw document with content."); 855 fail("Couldn't create test environment"); 856 } catch (com.sun.star.beans.UnknownPropertyException e) { 857 System.out.println("Exception occurred while setting or getting property value while filling Draw document with content."); 858 fail("Couldn't create test environment"); 859 } catch (com.sun.star.lang.IllegalArgumentException e) { 860 System.out.println("Exception occurred while setting or getting property value while filling Draw document with content."); 861 fail("Couldn't create test environment"); 862 } catch (com.sun.star.beans.PropertyVetoException e) { 863 System.out.println("Exception occurred while setting or getting property value while filling Draw document with content."); 864 fail("Couldn't create test environment"); 865 } 866 } 867 makeWriterDoc(String frameName, boolean withContent)868 private void makeWriterDoc(String frameName, boolean withContent){ 869 System.out.println("creating Writer document '" + frameName + "'"); 870 XTextDocument xTextDoc = createNewWriterDoc(frameName); 871 if (withContent) fillWriterDocWithContent(xTextDoc); 872 positioningDocument((XModel) UnoRuntime.queryInterface(XModel.class, 873 xTextDoc)); 874 } 875 createNewWriterDoc(String frameName)876 private XTextDocument createNewWriterDoc(String frameName){ 877 XTextDocument xTextDoc = null; 878 try { 879 xTextDoc = SOF.createTextDoc(frameName); 880 } catch (com.sun.star.uno.Exception e) { 881 System.out.println("Exception occurred while creating text document '"+frameName+"':"); 882 fail("Couldn't create test environment"); 883 } 884 return xTextDoc; 885 } 886 fillWriterDocWithContent(XTextDocument xTextDoc)887 private void fillWriterDocWithContent(XTextDocument xTextDoc){ 888 try{ 889 System.out.println( "inserting some lines" ); 890 XText oText = xTextDoc.getText(); 891 XTextCursor oCursor = oText.createTextCursor(); 892 for (int i=0; i<5; i++){ 893 oText.insertString( oCursor,"Paragraph Number: " + i, false); 894 oText.insertString( oCursor, 895 " The quick brown fox jumps over the lazy Dog: SwXParagraph", 896 false); 897 oText.insertControlCharacter( 898 oCursor, ControlCharacter.PARAGRAPH_BREAK, false ); 899 oText.insertString( oCursor, 900 "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG: SwXParagraph", 901 false); 902 oText.insertControlCharacter(oCursor, 903 ControlCharacter.PARAGRAPH_BREAK, false ); 904 oText.insertControlCharacter( 905 oCursor, ControlCharacter.LINE_BREAK, false ); 906 } 907 } catch ( com.sun.star.lang.IllegalArgumentException e ){ 908 System.out.println("Exception occurred while filling text document with content."); 909 fail("Couldn't create test environment"); 910 } catch (com.sun.star.uno.Exception e) { 911 System.out.println("Exception occurred while filling text document with content."); 912 fail("Couldn't create test environment"); 913 } 914 } 915 createNewCalcDoc(String frameName)916 private XSpreadsheetDocument createNewCalcDoc(String frameName){ 917 918 XSpreadsheetDocument xSheetDoc = null; 919 920 try { 921 xSheetDoc = SOF.createCalcDoc(frameName); 922 } catch (com.sun.star.uno.Exception e) { 923 System.out.println("Exception occurred while creating Calc document '"+frameName+"':"); 924 fail("Couldn't create test environment"); 925 } 926 return xSheetDoc; 927 } 928 fillCalcDocWithContent(XSpreadsheetDocument xSpreadsheetDoc)929 private void fillCalcDocWithContent(XSpreadsheetDocument xSpreadsheetDoc){ 930 931 try{ 932 XSpreadsheets oSpreadsheets = xSpreadsheetDoc.getSheets(); 933 934 XSpreadsheet oSheet = (XSpreadsheet) AnyConverter.toObject( 935 new Type(XSpreadsheet.class), 936 oSpreadsheets.getByName( 937 oSpreadsheets.getElementNames()[0])); 938 939 XCellRange testRange = oSheet.getCellRangeByName("$A$1:$D$4"); 940 941 XSheetCellRange testSheetRange = (XSheetCellRange) UnoRuntime.queryInterface( 942 XSheetCellRange.class, 943 testRange); 944 oSheet.getCellByPosition(1, 1).setValue(1); 945 oSheet.getCellByPosition(4, 5).setValue(1); 946 oSheet.getCellByPosition(3, 2).setFormula("xTextDoc"); 947 oSheet.getCellByPosition(3, 3).setFormula("xTextDoc"); 948 } catch (com.sun.star.lang.WrappedTargetException e) { 949 System.out.println("Exception occurred while filling Calc document with content."); 950 fail("Couldn't create test environment"); 951 } catch (com.sun.star.container.NoSuchElementException e) { 952 System.out.println("Exception occurred while filling Calc document with content."); 953 fail("Couldn't create test environment"); 954 } catch (com.sun.star.lang.IndexOutOfBoundsException e) { 955 System.out.println("Exception occurred while filling Calc document with content."); 956 fail("Couldn't create test environment"); 957 } catch (com.sun.star.lang.IllegalArgumentException e) { 958 System.out.println("Exception occurred while filling Calc document with content."); 959 fail("Couldn't create test environment"); 960 } catch (com.sun.star.uno.Exception e) { 961 System.out.println("Exception occurred while filling Calc document with content."); 962 fail("Couldn't create test environment"); 963 } 964 } 965 966 /** 967 * copies all files from the backup folder into a folder called backup.recoveryTest 968 * and copies the Recovery.xcu to recovery.xcu.recoveryTest 969 */ backupRecoveryData()970 private void backupRecoveryData() 971 { 972 System.out.println("backup recovery data..."); 973 try{ 974 rt.copyRecoveryData(true); 975 }catch (com.sun.star.io.IOException e){ 976 fail("could not copy recovery data: " + e.toString()); 977 }catch (java.io.IOException e){ 978 fail("could not copy recovery data: " + e.toString()); 979 } 980 } 981 982 /** 983 * copies all files from the backup.recoveryTest folder into the backup folder 984 * and copies the Recovery.xcu.recoveryTest to recovery.xcu 985 */ restoreBackupRecoveryData()986 private void restoreBackupRecoveryData() 987 { 988 System.out.println("restore backup recovery data..."); 989 try{ 990 rt.copyRecoveryData(false); 991 }catch (com.sun.star.io.IOException e){ 992 fail("could not copy recovery data: " + e.toString()); 993 }catch (java.io.IOException e){ 994 fail("could not copy recovery data: " + e.toString()); 995 } 996 } 997 cleanRecoveryData()998 private void cleanRecoveryData(){ 999 try{ 1000 System.out.println("bootstrapping the office to get user path to remove old recovery data..."); 1001 1002 rt.cleanRecoveryData(); 1003 1004 } catch (com.sun.star.io.IOException e){ 1005 fail("could not clean recovery data: " + e.toString()); 1006 } 1007 } 1008 } 1009