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