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