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