1 /* 2 * This file is part of the LibreOffice project. 3 * 4 * This Source Code Form is subject to the terms of the Mozilla Public 5 * License, v. 2.0. If a copy of the MPL was not distributed with this 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 * 8 * This file incorporates work covered by the following license notice: 9 * 10 * Licensed to the Apache Software Foundation (ASF) under one or more 11 * contributor license agreements. See the NOTICE file distributed 12 * with this work for additional information regarding copyright 13 * ownership. The ASF licenses this file to you under the Apache 14 * License, Version 2.0 (the "License"); you may not use this file 15 * except in compliance with the License. You may obtain a copy of 16 * the License at http://www.apache.org/licenses/LICENSE-2.0 . 17 */ 18 package complex.bean; 19 20 21 import java.awt.event.*; 22 23 import com.sun.star.comp.beans.OOoBean; 24 import com.sun.star.uno.UnoRuntime; 25 26 import java.awt.*; 27 28 import org.junit.AfterClass; 29 import org.junit.BeforeClass; 30 import org.junit.Test; 31 import org.openoffice.test.OfficeConnection; 32 33 import static org.junit.Assert.*; 34 35 /** 36 * This only here to expose the protected constructor. 37 */ 38 class PrivateLocalOfficeConnection extends com.sun.star.comp.beans.LocalOfficeConnection 39 { PrivateLocalOfficeConnection(com.sun.star.uno.XComponentContext xContext)40 public PrivateLocalOfficeConnection(com.sun.star.uno.XComponentContext xContext) 41 { 42 super(xContext); 43 } 44 } 45 46 @Deprecated 47 public class OOoBeanTest 48 { 49 50 /** For X-Windows we need to prolong the time between painting windows. Because 51 it takes longer than on Windows. 52 */ getSleepTime(int time)53 private int getSleepTime(int time) 54 { 55 if (!isWindows()) 56 { 57 return time * 5; 58 } 59 return time; 60 } 61 62 /** If it cannot be determined if we run on Windows then we assume 63 that we do not. 64 */ isWindows()65 private boolean isWindows() 66 { 67 boolean ret = false; 68 String os = System.getProperty("os.name"); 69 if (os != null) 70 { 71 os = os.trim(); 72 if (os.toLowerCase().indexOf("win") == 0) 73 { 74 ret = true; 75 } 76 } 77 return ret; 78 } 79 getText(OOoBean bean)80 private String getText(OOoBean bean) throws Exception 81 { 82 com.sun.star.frame.XModel model = bean.getDocument(); 83 com.sun.star.text.XTextDocument myDoc = 84 UnoRuntime.queryInterface(com.sun.star.text.XTextDocument.class, model); 85 com.sun.star.text.XText xText = myDoc.getText(); 86 return xText.getString(); 87 } 88 89 /** 1.Create a Java frame 90 * 2.Add OOoBean (no document loaded yet) 91 * 3.Show frame 92 * 4.Load document 93 */ test1()94 @Test public void test1() throws Exception 95 { 96 WriterFrame f = null; 97 try 98 { 99 f = new WriterFrame(100 ,100, 500 ,400, false, connection.getComponentContext()); 100 f.setText("OOoBean test."); 101 Thread.sleep(1000); 102 } 103 finally 104 { 105 if (f != null) 106 { 107 f.dispose(); 108 } 109 } 110 } 111 112 /** Sizing, painting 113 */ test2()114 @Test public void test2() throws Exception 115 { 116 WriterFrame f = null; 117 ScreenComparer capturer = null; 118 try 119 { 120 f = new WriterFrame(100, 100, 500,500, false, connection.getComponentContext()); 121 if (!f.checkUnoFramePosition()) 122 { 123 fail("Sizing error: Client are of Java frame does not match the UNO window."); 124 } 125 capturer = new ScreenComparer(100, 100, 500, 500); 126 127 //Minimize Window and back 128 f.goToStart(); 129 f.pageDown(); 130 Thread.sleep(1000); 131 for (int i = 0; i < 3; i++) 132 { 133 capturer.reset(); 134 capturer.grabOne(f.getClientArea()); 135 f.setExtendedState(Frame.ICONIFIED); 136 Thread.sleep(getSleepTime(200)); 137 if (!f.checkUnoFramePosition()) 138 { 139 fail("Sizing error: Frame was iconified."); 140 } 141 f.setExtendedState(Frame.NORMAL); 142 Thread.sleep(getSleepTime(200)); 143 if (!f.checkUnoFramePosition()) 144 { 145 fail("Sizing error: Frame size set back to normal after it was iconified."); 146 } 147 capturer.grabTwo(f.getClientArea()); 148 if (!capturer.compare()) 149 { 150 fail("Painting error: Minimize (iconify) frame and back to normal size."); 151 capturer.writeImages(); 152 } 153 } 154 155 //Maximize Window and back to normal 156 for (int i = 0; i < 3; i++) 157 { 158 capturer.reset(); 159 capturer.grabOne(f.getClientArea()); 160 f.setExtendedState(Frame.MAXIMIZED_BOTH); 161 Thread.sleep(getSleepTime(200)); 162 if (!f.checkUnoFramePosition()) 163 { 164 fail("Sizing error: Frame maximized."); 165 } 166 f.setExtendedState(Frame.NORMAL); 167 Thread.sleep(getSleepTime(200)); 168 if (!f.checkUnoFramePosition()) 169 { 170 fail("Sizing error: Frame set from maximized to normal."); 171 } 172 capturer.grabTwo(f.getClientArea()); 173 if (!capturer.compare()) 174 { 175 fail("Painting error: Maximize frame and back to normal size"); 176 capturer.writeImages(); 177 } 178 } 179 180 //move Window top left 181 capturer.reset(); 182 capturer.grabOne(f.getClientArea()); 183 Rectangle oldPosition = f.getBounds(); 184 f.setBounds(0, 0, oldPosition.width, oldPosition.height); 185 Thread.sleep(getSleepTime(200)); 186 if (!f.checkUnoFramePosition()) 187 { 188 fail("Sizing error: Frame moved."); 189 } 190 191 capturer.grabTwo(f.getClientArea()); 192 if (!capturer.compare()) 193 { 194 fail("Painting error: Move frame to a different position."); 195 capturer.writeImages(); 196 } 197 198 //move Window down 199 Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); 200 int maxY = dim.height - f.getBounds().height; 201 202 int curY = 0; 203 while (curY < maxY) 204 { 205 capturer.reset(); 206 capturer.grabOne(f.getClientArea()); 207 oldPosition = f.getBounds(); 208 f.setBounds(0, curY, oldPosition.width, oldPosition.height); 209 capturer.grabTwo(f.getClientArea()); 210 if (!capturer.compare()) 211 { 212 fail("Painting error: Move frame to a different position."); 213 capturer.writeImages(); 214 } 215 curY+= 50; 216 Thread.sleep(getSleepTime(200)); 217 } 218 219 //obscure the window and make it visible again 220 221 oldPosition = f.getBounds(); 222 223 Rectangle pos = new Rectangle(oldPosition.x - 50, oldPosition.y - 50, 224 oldPosition.width, oldPosition.height); 225 Frame coverFrame = new Frame(); 226 coverFrame.setBounds(pos); 227 capturer.reset(); 228 capturer.grabOne(f.getClientArea()); 229 230 for (int i = 0; i < 3; i++) 231 { 232 coverFrame.setVisible(true); 233 Thread.sleep(getSleepTime(200)); 234 f.toFront(); 235 Thread.sleep(getSleepTime(200)); 236 if (!f.checkUnoFramePosition()) 237 { 238 fail("Sizing error: Frame moved from back to front."); 239 } 240 241 capturer.grabTwo(f.getClientArea()); 242 if (!capturer.compare()) 243 { 244 fail("Painting error: Move frame to back and to front."); 245 capturer.writeImages(); 246 } 247 } 248 249 coverFrame.dispose(); 250 } 251 finally 252 { 253 if (f != null) 254 { 255 f.dispose(); 256 } 257 } 258 } 259 260 /** 261 1. Create an OOoBean 262 2. Load a document 263 3. Create Frame (do not show yet) 264 4. Add OOoBean to Frame 265 5. Show Frame 266 */ test3()267 @Test public void test3() throws Exception 268 { 269 WriterFrame f = null; 270 try 271 { 272 f = new WriterFrame(100, 100, 500, 300, true, connection.getComponentContext()); 273 if (!f.checkUnoFramePosition()) 274 { 275 fail("Sizing error."); 276 } 277 278 } 279 finally 280 { 281 if (f != null) 282 { 283 f.dispose(); 284 } 285 } 286 } 287 288 /** Test repeated OOoBean.aquireSystemWindow and OOoBean.releaseSystemWindow 289 * calls. 290 */ test4()291 @Test public void test4() throws Exception 292 { 293 WriterFrame f = null; 294 try 295 { 296 f = new WriterFrame(100, 100, 500, 300, false, connection.getComponentContext()); 297 OOoBean b = f.getBean(); 298 for (int i = 0; i < 100; i++) 299 { 300 b.releaseSystemWindow(); 301 b.aquireSystemWindow(); 302 } 303 if (!f.checkUnoFramePosition()) 304 { 305 fail("Sizing error."); 306 } 307 } 308 finally 309 { 310 if (f != null) 311 { 312 f.dispose(); 313 } 314 if (!isWindows()) 315 { 316 Thread.sleep(10000); 317 } 318 } 319 } 320 321 /** Adding and removing the bean to a Java frame multiple times. 322 * Test painting and sizing. 323 */ test5()324 @Test public void test5() throws Exception 325 { 326 WriterFrame f = null; 327 try 328 { 329 f = new WriterFrame(100, 100, 500, 400, false, connection.getComponentContext()); 330 f.goToStart(); 331 f.pageDown(); 332 Thread.sleep(1000); 333 334 ScreenComparer capturer = new ScreenComparer(100,100,500,400); 335 capturer.grabOne(); 336 for (int i = 0; i < 100; i++) 337 { 338 f.removeOOoBean(); 339 f.addOOoBean(); 340 } 341 342 f.goToStart(); 343 f.pageDown(); 344 Thread.sleep(getSleepTime(200)); 345 capturer.grabTwo(); 346 347 if (!capturer.compare()) 348 { 349 fail("Painting error: adding and removing OOoBean " + 350 "repeatedly to java.lang.Frame."); 351 capturer.writeImages(); 352 } 353 354 if (!f.checkUnoFramePosition()) 355 { 356 fail("Sizing error."); 357 } 358 359 } 360 finally 361 { 362 if (f != null) 363 { 364 f.dispose(); 365 } 366 if (!isWindows()) 367 { 368 Thread.sleep(10000); 369 } 370 } 371 } 372 373 374 /** Test focus (i49454). After repeatedly adding and removing the bean to a window 375 * it should still be possible to enter text in the window. This does not 376 * work all the time on Windows. This is probably a timing problem. When using 377 * Thread.sleep (position #1) then it should work. 378 */ test6()379 @Test public void test6() throws Exception 380 { 381 for (int j = 0; j < 10; j++) 382 { 383 final OOoBean bean = new OOoBean(new PrivateLocalOfficeConnection(connection.getComponentContext())); 384 java.awt.Frame frame = null; 385 bean.setOOoCallTimeOut(10000); 386 try { 387 frame = new java.awt.Frame("OpenOffice.org Demo"); 388 frame.add(bean, BorderLayout.CENTER); 389 frame.pack(); 390 frame.setSize(600,300); 391 frame.show(); 392 bean.loadFromURL("private:factory/swriter", null); 393 // #1 394 Thread.sleep(1000); 395 396 StringBuffer buf = new StringBuffer(1000); 397 for (int i = 0; i < 1; i++) 398 { 399 bean.releaseSystemWindow(); 400 frame.remove(bean); 401 frame.add(bean, BorderLayout.CENTER); 402 bean.aquireSystemWindow(); 403 } 404 405 if (!isWindows()) 406 { 407 Thread.sleep(5000); 408 } 409 410 Robot roby = new Robot(); 411 roby.keyPress(KeyEvent.VK_H); 412 roby.keyRelease(KeyEvent.VK_H); 413 buf.append("h"); 414 415 String s = getText(bean); 416 if ( ! s.equals(buf.toString())) 417 { 418 fail("Focus error: After removing and adding the bean, the" + 419 "office window does not receive keyboard input.\n" + 420 "Try typing in the window, you've got 30s!!! This " + 421 "test may not work with Linux/Solaris"); 422 Thread.sleep(30000); 423 break; 424 } 425 else 426 { 427 Thread.sleep(2000); 428 } 429 430 } finally { 431 bean.stopOOoConnection(); 432 frame.dispose(); 433 } 434 } 435 } 436 437 /** Tests focus problem just like test6, but the implementation is a little 438 * different. The bean is added and removed from within the event dispatch 439 * thread. Using Thread.sleep at various points (#1, #2, #3) seems to workaround 440 * the problem. 441 */ test6a()442 @Test public void test6a() throws Exception 443 { 444 for (int j = 0; j < 50; j++) 445 { 446 final OOoBean bean = new OOoBean(new PrivateLocalOfficeConnection(connection.getComponentContext())); 447 final java.awt.Frame frame = new Frame("Openoffice.org"); 448 bean.setOOoCallTimeOut(10000); 449 450 try { 451 frame.add(bean, BorderLayout.CENTER); 452 frame.pack(); 453 frame.setSize(600,400); 454 frame.show(); 455 bean.loadFromURL("private:factory/swriter", null); 456 frame.validate(); 457 // #1 458 Thread.sleep(1000); 459 StringBuffer buf = new StringBuffer(1000); 460 int i = 0; 461 462 for (; i < 1; i++) 463 { 464 EventQueue.invokeAndWait( new Runnable() { 465 public void run() { 466 try { 467 468 bean.releaseSystemWindow(); 469 frame.remove(bean); 470 frame.validate(); 471 472 } catch (Exception e) { 473 e.printStackTrace(); 474 } 475 476 } 477 }); 478 // #2 479 Thread.sleep(1000); 480 EventQueue.invokeAndWait( new Runnable() { 481 public void run() { 482 try { 483 484 frame.add(bean, BorderLayout.CENTER); 485 bean.aquireSystemWindow(); 486 frame.validate(); 487 } catch (Exception e) { 488 e.printStackTrace(); 489 } 490 } 491 }); 492 493 // #3 494 Thread.sleep(1000); 495 } 496 497 if (!isWindows()) 498 { 499 Thread.sleep(5000); 500 } 501 502 Robot roby = new Robot(); 503 roby.mouseMove(300, 200); 504 roby.waitForIdle(); 505 roby.mousePress(InputEvent.BUTTON1_MASK); 506 roby.waitForIdle(); 507 roby.mouseRelease(InputEvent.BUTTON1_MASK); 508 roby.waitForIdle(); 509 roby.keyPress(KeyEvent.VK_H); 510 roby.waitForIdle(); 511 roby.keyRelease(KeyEvent.VK_H); 512 roby.waitForIdle(); 513 514 buf.append('h'); 515 Thread.sleep(1000); 516 String s = getText(bean); 517 System.out.println(" getText: " + s); 518 if ( ! s.equals(buf.toString())) 519 { 520 roby.mousePress(InputEvent.BUTTON1_MASK); 521 roby.waitForIdle(); 522 roby.mouseRelease(InputEvent.BUTTON1_MASK); 523 roby.waitForIdle(); 524 roby.keyPress(KeyEvent.VK_H); 525 roby.waitForIdle(); 526 roby.keyRelease(KeyEvent.VK_H); 527 roby.waitForIdle(); 528 529 String sH = "h"; 530 Thread.sleep(1000); 531 String s2 = getText(bean); 532 533 if ( ! sH.equals(s2)) 534 { 535 fail("Focus error: After removing and adding the bean, the" + 536 "office window does not receive keyboard input.\n" + 537 "Try typing in the window, you've got 30s!!! This " + 538 "test may not work with Linux/Solaris"); 539 System.out.println("j: " + j + " i: " + i); 540 Thread.sleep(30000); 541 break; 542 } 543 } 544 545 } finally { 546 bean.stopOOoConnection(); 547 frame.dispose(); 548 } 549 } 550 } 551 552 /** Repeatedly loading a document in one and the same OOoBean instance. 553 */ test7()554 @Test public void test7() throws Exception 555 { 556 WriterFrame f = null; 557 try 558 { 559 f = new WriterFrame(100 ,100, 500 ,400, false, connection.getComponentContext()); 560 String text = "OOoBean test."; 561 562 for (int i = 0; i < 10; i++) 563 { 564 f.getBean().clear(); 565 f.getBean().loadFromURL("private:factory/swriter", null); 566 f.setText(text); 567 f.goToStart(); 568 f.validate(); 569 570 if (!text.equals(f.getText())) 571 { 572 fail("Repeated loading of a document failed."); 573 } 574 Thread.sleep(1000); 575 } 576 } 577 finally 578 { 579 if (f != null) 580 { 581 f.dispose(); 582 } 583 } 584 } 585 586 /** Using multiple instances of OOoBean at the same time 587 */ test8()588 @Test public void test8() throws Exception 589 { 590 OOoBean bean1 = new OOoBean(new PrivateLocalOfficeConnection(connection.getComponentContext())); 591 BeanPanel bp1 = new BeanPanel(bean1); 592 OOoBean bean2 = new OOoBean(new PrivateLocalOfficeConnection(connection.getComponentContext())); 593 BeanPanel bp2 = new BeanPanel(bean2); 594 OOoBean bean3 = new OOoBean(new PrivateLocalOfficeConnection(connection.getComponentContext())); 595 BeanPanel bp3 = new BeanPanel(bean3); 596 OOoBean bean4 = new OOoBean(new PrivateLocalOfficeConnection(connection.getComponentContext())); 597 BeanPanel bp4 = new BeanPanel(bean4); 598 599 try 600 { 601 Frame f = new Frame("OOoBean example with several instances"); 602 f.setLayout(new GridBagLayout()); 603 GridBagConstraints c = new GridBagConstraints(); 604 c.fill = GridBagConstraints.HORIZONTAL; 605 c.weightx = 0.5; 606 607 c.insets = new Insets(0, 0, 0, 10); 608 c.gridx = 0; 609 c.gridy = 0; 610 f.add(bp1, c); 611 612 c.gridx = 1; 613 c.insets = new Insets(0, 0, 0, 0); 614 f.add(bp2, c); 615 616 c.gridx = 0; 617 c.gridy = 1; 618 c.insets = new Insets(10, 0, 0, 10); 619 f.add(bp3, c); 620 621 c.gridx = 1; 622 c.gridy = 1; 623 c.insets = new Insets(10, 0, 0, 0); 624 f.add(bp4, c); 625 626 f.pack(); 627 f.setBounds(0, 0, 1000, 600); 628 f.setVisible(true); 629 try { 630 bean1.loadFromURL("private:factory/swriter", null); 631 bean2.loadFromURL("private:factory/swriter", null); 632 bean3.loadFromURL("private:factory/swriter", null); 633 bean4.loadFromURL("private:factory/swriter", null); 634 } catch( Exception e) 635 { 636 e.printStackTrace(); 637 } 638 f.validate(); 639 640 Thread.sleep(10000); 641 } 642 finally 643 { 644 bean1.stopOOoConnection(); 645 bean2.stopOOoConnection(); 646 bean3.stopOOoConnection(); 647 bean4.stopOOoConnection(); 648 } 649 } 650 651 private class BeanPanel extends Panel 652 { BeanPanel(OOoBean b)653 private BeanPanel(OOoBean b) 654 { 655 setLayout(new BorderLayout()); 656 add(b, BorderLayout.CENTER); 657 } 658 @Override getPreferredSize()659 public Dimension getPreferredSize() 660 { 661 return new Dimension(200, 200); 662 } 663 } 664 665 666 // setup and close connections setUpConnection()667 @BeforeClass public static void setUpConnection() throws Exception { 668 System.out.println("setUpConnection()"); 669 connection.setUp(); 670 } 671 tearDownConnection()672 @AfterClass public static void tearDownConnection() 673 throws InterruptedException, com.sun.star.uno.Exception 674 { 675 System.out.println("tearDownConnection()"); 676 connection.tearDown(); 677 } 678 679 private static final OfficeConnection connection = new OfficeConnection(); 680 681 682 } 683 684 685