update dev300-m58
[ooovba.git] / bean / qa / complex / OOoBeanTest.java
blob394d88015661d0b26b4a7682edd5b64a1712ce4c
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: OOoBeanTest.java,v $
10 * $Revision: 1.4 $
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 ************************************************************************/
30 package complex;
33 import complexlib.ComplexTestCase;
34 import java.io.*;
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;
55 import java.awt.*;
57 public class OOoBeanTest extends ComplexTestCase
60 public String[] getTestMethodNames()
62 // TODO think about trigger of sub-tests from outside
63 return new String[]
65 "test1",
66 "test2",
67 "test3",
68 "test4",
69 "test5",
70 "test6",
71 "test6a",
72 "test7",
73 "test8"
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)
82 int ret = time;
83 if (isWindows() == false)
84 return time * 5;
85 return time;
88 /** If it cannot be determined if we run on Windows then we assume
89 that we do not.
91 boolean isWindows()
93 boolean ret = false;
94 String os = System.getProperty("os.name");
95 if (os != null)
97 os = os.trim();
98 if (os.indexOf("Win") == 0)
99 ret = true;
101 return ret;
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,
109 model);
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)
116 * 3.Show frame
117 * 4.Load document
119 public void test1() throws Exception
121 WriterFrame f = null;
124 f = new WriterFrame(100 ,100, 500 ,400, false);
125 f.setText("OOoBean test.");
126 Thread.sleep(1000);
128 finally
130 if (f != null)
131 f.dispose();
135 /** Sizing, painting
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
149 f.goToStart();
150 f.pageDown();
151 Thread.sleep(1000);
152 for (int i = 0; i < 3; i++)
154 capturer.reset();
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++)
175 capturer.reset();
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
194 capturer.reset();
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();
209 //move Window down
210 Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
211 int maxY = dim.height - f.getBounds().height;
213 int curY = 0;
214 while (curY < maxY)
216 capturer.reset();
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();
226 curY+= 50;
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);
238 capturer.reset();
239 capturer.grabOne(f.getClientArea());
241 for (int i = 0; i < 3; i++)
243 coverFrame.setVisible(true);
244 Thread.sleep(getSleepTime(200));
245 f.toFront();
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();
260 finally
262 if (f != null)
263 f.dispose();
268 1. Create a OOoBean
269 2. Load a document
270 3. Create Frame (do not show yet)
271 4. Add OOoBean to Frame
272 5. Show 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);
284 finally
286 if (f != null)
287 f.dispose();
291 /** Test repeated OOoBean.aquireSystemWindow and OOoBean.releaseSystemWindow
292 * calls.
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);
309 finally
311 if (f != null)
312 f.dispose();
313 if (isWindows() == false)
314 Thread.sleep(10000);
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);
327 f.goToStart();
328 f.pageDown();
329 Thread.sleep(1000);
331 ScreenComparer capturer = new ScreenComparer(100,100,500,400);
332 capturer.grabOne();
333 for (int i = 0; i < 100; i++)
335 f.removeOOoBean();
336 f.addOOoBean();
339 f.goToStart();
340 f.pageDown();
341 Thread.sleep(getSleepTime(200));
342 capturer.grabTwo();
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);
355 finally
357 if (f != null)
358 f.dispose();
359 if (isWindows() == false)
360 Thread.sleep(10000);
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);
377 try {
378 frame = new java.awt.Frame("OpenOffice.org Demo");
379 frame.add(bean, BorderLayout.CENTER);
380 frame.pack();
381 frame.setSize(600,300);
382 frame.show();
383 bean.loadFromURL("private:factory/swriter", null);
384 // #1
385 Thread.sleep(1000);
387 StringBuffer buf = new StringBuffer(1000);
388 for (int i = 0; i < 1; i++)
390 // Thread.sleep(1000);
391 bean.releaseSystemWindow();
392 frame.remove(bean);
393 // frame.validate();
394 // Thread.sleep(1000);
395 frame.add(bean, BorderLayout.CENTER);
396 bean.aquireSystemWindow();
397 // frame.validate();
400 if (isWindows() == false)
401 Thread.sleep(5000);
403 Robot roby = new Robot();
404 roby.keyPress(KeyEvent.VK_H);
405 roby.keyRelease(KeyEvent.VK_H);
406 buf.append("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);
415 Thread.sleep(30000);
416 break;
418 else
420 Thread.sleep(2000);
423 } finally {
424 bean.stopOOoConnection();
425 frame.dispose();
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
433 * the problem.
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);
443 try {
444 frame.add(bean, BorderLayout.CENTER);
445 frame.pack();
446 frame.setSize(600,400);
447 frame.show();
448 bean.loadFromURL("private:factory/swriter", null);
449 frame.validate();
450 // #1
451 Thread.sleep(1000);
452 StringBuffer buf = new StringBuffer(1000);
453 int i = 0;
455 for (; i < 1; i++)
457 EventQueue q = Toolkit.getDefaultToolkit().getSystemEventQueue();
458 q.invokeAndWait( new Runnable() {
459 public void run() {
460 try {
462 bean.releaseSystemWindow();
463 frame.remove(bean);
464 frame.validate();
466 } catch (Exception e) {
467 e.printStackTrace();
472 // #2
473 Thread.sleep(1000);
474 q.invokeAndWait( new Runnable() {
475 public void run() {
476 try {
478 frame.add(bean, BorderLayout.CENTER);
479 bean.aquireSystemWindow();
480 frame.validate();
481 } catch (Exception e) {
482 e.printStackTrace();
487 // #3
488 Thread.sleep(1000);
491 if (isWindows() == false)
492 Thread.sleep(5000);
494 Robot roby = new Robot();
495 roby.mouseMove(300, 200);
496 roby.waitForIdle();
497 roby.mousePress(InputEvent.BUTTON1_MASK);
498 roby.waitForIdle();
499 roby.mouseRelease(InputEvent.BUTTON1_MASK);
500 roby.waitForIdle();
501 roby.keyPress(KeyEvent.VK_H);
502 roby.waitForIdle();
503 roby.keyRelease(KeyEvent.VK_H);
504 roby.waitForIdle();
506 buf.append("h");
507 Thread.sleep(1000);
508 String s = getText(bean);
509 System.out.println(" getText: " + s);
510 if ( ! s.equals(buf.toString()))
512 roby.mousePress(InputEvent.BUTTON1_MASK);
513 roby.waitForIdle();
514 roby.mouseRelease(InputEvent.BUTTON1_MASK);
515 roby.waitForIdle();
516 roby.keyPress(KeyEvent.VK_H);
517 roby.waitForIdle();
518 roby.keyRelease(KeyEvent.VK_H);
519 roby.waitForIdle();
521 String sH = "h";
522 Thread.sleep(1000);
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);
532 Thread.sleep(30000);
533 break;
536 else
538 // Thread.sleep(2000);
541 } finally {
542 bean.stopOOoConnection();
543 frame.dispose();
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++)
560 f.getBean().clear();
561 f.getBean().loadFromURL("private:factory/swriter", null);
562 f.setText(text);
563 f.goToStart();
564 f.validate();
566 if (text.equals(f.getText()) == false)
567 failed("Repeated loading of a document failed.");
568 Thread.sleep(1000);
571 finally
573 if (f != null)
574 f.dispose();
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;
597 c.weightx = 0.5;
599 c.insets = new Insets(0, 0, 0, 10);
600 c.gridx = 0;
601 c.gridy = 0;
602 f.add(bp1, c);
604 c.gridx = 1;
605 c.insets = new Insets(0, 0, 0, 0);
606 f.add(bp2, c);
608 c.gridx = 0;
609 c.gridy = 1;
610 c.insets = new Insets(10, 0, 0, 10);
611 f.add(bp3, c);
613 c.gridx = 1;
614 c.gridy = 1;
615 c.insets = new Insets(10, 0, 0, 0);
616 f.add(bp4, c);
618 f.pack();
619 f.setBounds(0, 0, 1000, 600);
620 f.setVisible(true);
621 try {
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)
628 e.printStackTrace();
630 f.validate();
632 Thread.sleep(10000);
634 finally
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);