1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: OOoBeanTest.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
33 import complexlib
.ComplexTestCase
;
35 import java
.awt
.Rectangle
;
36 import java
.awt
.Insets
;
37 import java
.awt
.BorderLayout
;
38 import java
.awt
.Dimension
;
39 import java
.awt
.image
.BufferedImage
;
40 import java
.awt
.image
.PixelGrabber
;
41 import java
.awt
.GridBagLayout
;
42 import java
.awt
.GridBagConstraints
;
43 import java
.awt
.event
.*;
44 import java
.awt
.Frame
;
45 import java
.awt
.Toolkit
;
46 import java
.awt
.Robot
;
47 import java
.awt
.event
.KeyEvent
;
48 import java
.awt
.Button
;
49 import javax
.imageio
.ImageIO
;
50 import javax
.imageio
.stream
.FileImageOutputStream
;
51 import com
.sun
.star
.comp
.beans
.OOoBean
;
52 import com
.sun
.star
.uno
.UnoRuntime
;
53 import com
.sun
.star
.text
.XTextDocument
;
57 public class OOoBeanTest
extends ComplexTestCase
60 public String
[] getTestMethodNames()
62 // TODO think about trigger of sub-tests from outside
77 /** For X-Windows we need to prolong the time between painting windows. Because
78 it takes longer than on Windows.
80 int getSleepTime(int time
)
83 if (isWindows() == false)
88 /** If it cannot be determined if we run on Windows then we assume
94 String os
= System
.getProperty("os.name");
98 if (os
.indexOf("Win") == 0)
104 public String
getText(OOoBean bean
) throws Exception
106 com
.sun
.star
.frame
.XModel model
= (com
.sun
.star
.frame
.XModel
)bean
.getDocument();
107 com
.sun
.star
.text
.XTextDocument myDoc
=
108 (XTextDocument
) UnoRuntime
.queryInterface(com
.sun
.star
.text
.XTextDocument
.class,
110 com
.sun
.star
.text
.XText xText
= myDoc
.getText();
111 return xText
.getString();
114 /** 1.Create a Java frame
115 * 2.Add OOoBean (no document loaded yet)
119 public void test1() throws Exception
121 WriterFrame f
= null;
124 f
= new WriterFrame(100 ,100, 500 ,400, false);
125 f
.setText("OOoBean test.");
137 public void test2() throws Exception
139 WriterFrame f
= null;
140 ScreenComparer capturer
= null;
143 f
= new WriterFrame(100, 100, 500,500, false);
144 if (f
.checkUnoFramePosition() == false)
145 failed("Sizing error: Client are of Java frame does not match the UNO window.", true);
146 capturer
= new ScreenComparer(100, 100, 500, 500);
148 //Minimize Window and back
152 for (int i
= 0; i
< 3; i
++)
155 capturer
.grabOne(f
.getClientArea());
156 f
.setExtendedState(Frame
.ICONIFIED
);
157 Thread
.sleep(getSleepTime(200));
158 if (f
.checkUnoFramePosition() == false)
159 failed("Sizing error: Frame was iconified.", true);
160 f
.setExtendedState(Frame
.NORMAL
);
161 Thread
.sleep(getSleepTime(200));
162 if (f
.checkUnoFramePosition() == false)
163 failed("Sizing error: Frame size set back to normal after it was iconified.", true);
164 capturer
.grabTwo(f
.getClientArea());
165 if (capturer
.compare() == false)
167 failed("Painting error: Minimize (iconify) frame and back to normal size.", true);
168 capturer
.writeImages();
172 //Maximize Window and back to normal
173 for (int i
= 0; i
< 3; i
++)
176 capturer
.grabOne(f
.getClientArea());
177 f
.setExtendedState(Frame
.MAXIMIZED_BOTH
);
178 Thread
.sleep(getSleepTime(200));
179 if (f
.checkUnoFramePosition() == false)
180 failed("Sizing error: Frame maximized.", true);
181 f
.setExtendedState(Frame
.NORMAL
);
182 Thread
.sleep(getSleepTime(200));
183 if (f
.checkUnoFramePosition() == false)
184 failed("Sizing error: Frame set from maximized to normal.", true);
185 capturer
.grabTwo(f
.getClientArea());
186 if (capturer
.compare() == false)
188 failed("Painting error: Maximize frame and back to normal size", true);
189 capturer
.writeImages();
193 //move Window top left
195 capturer
.grabOne(f
.getClientArea());
196 Rectangle oldPosition
= f
.getBounds();
197 f
.setBounds(0, 0, oldPosition
.width
, oldPosition
.height
);
198 Thread
.sleep(getSleepTime(200));
199 if (f
.checkUnoFramePosition() == false)
200 failed("Sizing error: Frame moved.", true);
202 capturer
.grabTwo(f
.getClientArea());
203 if (capturer
.compare() == false)
205 failed("Painting error: Move frame to a different position.", true);
206 capturer
.writeImages();
210 Dimension dim
= Toolkit
.getDefaultToolkit().getScreenSize();
211 int maxY
= dim
.height
- f
.getBounds().height
;
217 capturer
.grabOne(f
.getClientArea());
218 oldPosition
= f
.getBounds();
219 f
.setBounds(0, curY
, oldPosition
.width
, oldPosition
.height
);
220 capturer
.grabTwo(f
.getClientArea());
221 if (capturer
.compare() == false)
223 failed("Painting error: Move frame to a different position.", true);
224 capturer
.writeImages();
227 Thread
.sleep(getSleepTime(200));
230 //obscure the window and make it visible again
232 oldPosition
= f
.getBounds();
234 Rectangle pos
= new Rectangle(oldPosition
.x
- 50, oldPosition
.y
- 50,
235 oldPosition
.width
, oldPosition
.height
);
236 Frame coverFrame
= new Frame();
237 coverFrame
.setBounds(pos
);
239 capturer
.grabOne(f
.getClientArea());
241 for (int i
= 0; i
< 3; i
++)
243 coverFrame
.setVisible(true);
244 Thread
.sleep(getSleepTime(200));
246 Thread
.sleep(getSleepTime(200));
247 if (f
.checkUnoFramePosition() == false)
248 failed("Sizing error: Frame moved from back to front.", true);
250 capturer
.grabTwo(f
.getClientArea());
251 if (capturer
.compare() == false)
253 failed("Painting error: Move frame to back and to front.", true);
254 capturer
.writeImages();
258 coverFrame
.dispose();
270 3. Create Frame (do not show yet)
271 4. Add OOoBean to Frame
274 public void test3() throws Exception
276 WriterFrame f
= null;
279 f
= new WriterFrame(100, 100, 500, 300, true);
280 if (f
.checkUnoFramePosition() == false)
281 failed("Sizing error.", true);
291 /** Test repeated OOoBean.aquireSystemWindow and OOoBean.releaseSystemWindow
294 public void test4() throws Exception
296 WriterFrame f
= null;
299 f
= new WriterFrame(100, 100, 500, 300, false);
300 OOoBean b
= f
.getBean();
301 for (int i
= 0; i
< 100; i
++)
303 b
.releaseSystemWindow();
304 b
.aquireSystemWindow();
306 if (f
.checkUnoFramePosition() == false)
307 failed("Sizing error.", true);
313 if (isWindows() == false)
318 /** Adding and removing the bean to a Java frame multiple times.
319 * Test painting and sizing.
321 public void test5() throws Exception
323 WriterFrame f
= null;
326 f
= new WriterFrame(100, 100, 500, 400, false);
331 ScreenComparer capturer
= new ScreenComparer(100,100,500,400);
333 for (int i
= 0; i
< 100; i
++)
341 Thread
.sleep(getSleepTime(200));
344 if (capturer
.compare() == false)
346 failed("Painting error: adding and removing OOoBean " +
347 "repeatedly to java.lang.Frame.", true);
348 capturer
.writeImages();
351 if (f
.checkUnoFramePosition() == false)
352 failed("Sizing error.", true);
359 if (isWindows() == false)
365 /** Test focus (i49454). After repeatedly adding and removing the bean to a window
366 * it should still be possible to enter text in the window. This does not
367 * work all the time on Windows. This is probably a timing problem. When using
368 * Thread.sleep (position #1) then it should work.
370 public void test6() throws Exception
372 for (int j
= 0; j
< 10; j
++)
374 final OOoBean bean
= new OOoBean();
375 java
.awt
.Frame frame
= null;
376 bean
.setOOoCallTimeOut(10000);
378 frame
= new java
.awt
.Frame("OpenOffice.org Demo");
379 frame
.add(bean
, BorderLayout
.CENTER
);
381 frame
.setSize(600,300);
383 bean
.loadFromURL("private:factory/swriter", null);
387 StringBuffer buf
= new StringBuffer(1000);
388 for (int i
= 0; i
< 1; i
++)
390 // Thread.sleep(1000);
391 bean
.releaseSystemWindow();
394 // Thread.sleep(1000);
395 frame
.add(bean
, BorderLayout
.CENTER
);
396 bean
.aquireSystemWindow();
400 if (isWindows() == false)
403 Robot roby
= new Robot();
404 roby
.keyPress(KeyEvent
.VK_H
);
405 roby
.keyRelease(KeyEvent
.VK_H
);
408 String s
= getText(bean
);
409 if ( ! s
.equals(buf
.toString()))
411 failed("Focus error: After removing and adding the bean, the" +
412 "office window does not receive keyboard input.\n" +
413 "Try typing in the window, you've got 30s!!! This " +
414 "test may not work with Linux/Solaris", true);
424 bean
.stopOOoConnection();
430 /** Tests focus problem just like test6, but the implementation is a little
431 * different. The bean is added and removed from withing the event dispatch
432 * thread. Using Thread.sleep at various points (#1, #2, #3) seems to workaround
435 public void test6a() throws Exception
437 for (int j
= 0; j
< 50; j
++)
439 final OOoBean bean
= new OOoBean();
440 final java
.awt
.Frame frame
= new Frame("Openoffice.org");
441 bean
.setOOoCallTimeOut(10000);
444 frame
.add(bean
, BorderLayout
.CENTER
);
446 frame
.setSize(600,400);
448 bean
.loadFromURL("private:factory/swriter", null);
452 StringBuffer buf
= new StringBuffer(1000);
457 EventQueue q
= Toolkit
.getDefaultToolkit().getSystemEventQueue();
458 q
.invokeAndWait( new Runnable() {
462 bean
.releaseSystemWindow();
466 } catch (Exception e
) {
474 q
.invokeAndWait( new Runnable() {
478 frame
.add(bean
, BorderLayout
.CENTER
);
479 bean
.aquireSystemWindow();
481 } catch (Exception e
) {
491 if (isWindows() == false)
494 Robot roby
= new Robot();
495 roby
.mouseMove(300, 200);
497 roby
.mousePress(InputEvent
.BUTTON1_MASK
);
499 roby
.mouseRelease(InputEvent
.BUTTON1_MASK
);
501 roby
.keyPress(KeyEvent
.VK_H
);
503 roby
.keyRelease(KeyEvent
.VK_H
);
508 String s
= getText(bean
);
509 System
.out
.println(" getText: " + s
);
510 if ( ! s
.equals(buf
.toString()))
512 roby
.mousePress(InputEvent
.BUTTON1_MASK
);
514 roby
.mouseRelease(InputEvent
.BUTTON1_MASK
);
516 roby
.keyPress(KeyEvent
.VK_H
);
518 roby
.keyRelease(KeyEvent
.VK_H
);
523 String s2
= getText(bean
);
525 if ( ! sH
.equals(s2
))
527 failed("Focus error: After removing and adding the bean, the" +
528 "office window does not receive keyboard input.\n" +
529 "Try typing in the window, you've got 30s!!! This " +
530 "test may not work with Linux/Solaris", true);
531 System
.out
.println("j: " + j
+ " i: " + i
);
538 // Thread.sleep(2000);
542 bean
.stopOOoConnection();
548 /** Repeatedly loading a document in one and the same OOoBean instance.
550 public void test7() throws Exception
552 WriterFrame f
= null;
555 f
= new WriterFrame(100 ,100, 500 ,400, false);
556 String text
= "OOoBean test.";
558 for (int i
= 0; i
< 10; i
++)
561 f
.getBean().loadFromURL("private:factory/swriter", null);
566 if (text
.equals(f
.getText()) == false)
567 failed("Repeated loading of a document failed.");
578 /** Using multiple instances of OOoBean at the same time
580 public void test8() throws Exception
582 OOoBean bean1
= new OOoBean();
583 BeanPanel bp1
= new BeanPanel(bean1
);
584 OOoBean bean2
= new OOoBean();
585 BeanPanel bp2
= new BeanPanel(bean2
);
586 OOoBean bean3
= new OOoBean();
587 BeanPanel bp3
= new BeanPanel(bean3
);
588 OOoBean bean4
= new OOoBean();
589 BeanPanel bp4
= new BeanPanel(bean4
);
593 Frame f
= new Frame("OOoBean example with several instances");
594 f
.setLayout(new GridBagLayout());
595 GridBagConstraints c
= new GridBagConstraints();
596 c
.fill
= GridBagConstraints
.HORIZONTAL
;
599 c
.insets
= new Insets(0, 0, 0, 10);
605 c
.insets
= new Insets(0, 0, 0, 0);
610 c
.insets
= new Insets(10, 0, 0, 10);
615 c
.insets
= new Insets(10, 0, 0, 0);
619 f
.setBounds(0, 0, 1000, 600);
622 bean1
.loadFromURL("private:factory/swriter", null);
623 bean2
.loadFromURL("private:factory/swriter", null);
624 bean3
.loadFromURL("private:factory/swriter", null);
625 bean4
.loadFromURL("private:factory/swriter", null);
626 } catch( Exception e
)
636 bean1
.stopOOoConnection();
637 bean2
.stopOOoConnection();
638 bean3
.stopOOoConnection();
639 bean4
.stopOOoConnection();
643 class BeanPanel
extends Panel
645 public BeanPanel(OOoBean b
)
647 setLayout(new BorderLayout());
648 add(b
, BorderLayout
.CENTER
);
650 public Dimension
getPreferredSize()
652 Container c
= getParent();
653 return new Dimension(200, 200);