1*76b6b121SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*76b6b121SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*76b6b121SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*76b6b121SAndrew Rist  * distributed with this work for additional information
6*76b6b121SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*76b6b121SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*76b6b121SAndrew Rist  * "License"); you may not use this file except in compliance
9*76b6b121SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*76b6b121SAndrew Rist  *
11*76b6b121SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*76b6b121SAndrew Rist  *
13*76b6b121SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*76b6b121SAndrew Rist  * software distributed under the License is distributed on an
15*76b6b121SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*76b6b121SAndrew Rist  * KIND, either express or implied.  See the License for the
17*76b6b121SAndrew Rist  * specific language governing permissions and limitations
18*76b6b121SAndrew Rist  * under the License.
19*76b6b121SAndrew Rist  *
20*76b6b121SAndrew Rist  *************************************************************/
21*76b6b121SAndrew Rist 
22*76b6b121SAndrew Rist 
23cdf0e10cSrcweir package complex.desktop;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
27cdf0e10cSrcweir import com.sun.star.frame.XDesktop;
28cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
29cdf0e10cSrcweir import helper.OfficeProvider;
30cdf0e10cSrcweir //import complex.persistent_window_states.helper.DocumentHandle;
31cdf0e10cSrcweir 
32cdf0e10cSrcweir // ---------- junit imports -----------------
33cdf0e10cSrcweir import org.junit.After;
34cdf0e10cSrcweir import org.junit.AfterClass;
35cdf0e10cSrcweir import org.junit.Before;
36cdf0e10cSrcweir import org.junit.BeforeClass;
37cdf0e10cSrcweir import org.junit.Test;
38cdf0e10cSrcweir import org.openoffice.test.OfficeConnection;
39cdf0e10cSrcweir import static org.junit.Assert.*;
40cdf0e10cSrcweir // ------------------------------------------
41cdf0e10cSrcweir 
42cdf0e10cSrcweir /**
43cdf0e10cSrcweir  * Parameters:
44cdf0e10cSrcweir  * <ul>
45cdf0e10cSrcweir  *   <li>NoOffice=yes - StarOffice is not started initially.</li>
46cdf0e10cSrcweir  * </ul>
47cdf0e10cSrcweir  */
48cdf0e10cSrcweir public class DesktopTerminate
49cdf0e10cSrcweir {
50cdf0e10cSrcweir 
51cdf0e10cSrcweir     private XMultiServiceFactory xMSF;
52cdf0e10cSrcweir     private int iOfficeCloseTime = 1000;
53cdf0e10cSrcweir 
54cdf0e10cSrcweir     /**
55cdf0e10cSrcweir      * A frunction to tell the framework, which test functions are available.
56cdf0e10cSrcweir      * Right now, it's only 'checkPersistentWindowState'.
57cdf0e10cSrcweir      * @return All test methods.
58cdf0e10cSrcweir      */
59cdf0e10cSrcweir //    public String[] getTestMethodNames()
60cdf0e10cSrcweir //    {
61cdf0e10cSrcweir //        return new String[]
62cdf0e10cSrcweir //                {
63cdf0e10cSrcweir //                    "checkPersistentWindowState"
64cdf0e10cSrcweir //                };
65cdf0e10cSrcweir //    }
66cdf0e10cSrcweir 
67cdf0e10cSrcweir     /**
68cdf0e10cSrcweir      * Test if all available document types change the
69cdf0e10cSrcweir      * persistent Window Attributes
70cdf0e10cSrcweir      *
71cdf0e10cSrcweir      * The test follows basically these steps:
72cdf0e10cSrcweir      * - Create a configuration reader and a componentloader
73cdf0e10cSrcweir      * - Look for all document types in the configuration
74cdf0e10cSrcweir      * - Do for every doc type
75cdf0e10cSrcweir      *   - start office
76cdf0e10cSrcweir      *   - read configuration attibute settings
77cdf0e10cSrcweir      *   - create a new document
78cdf0e10cSrcweir      *   - resize the document and close it
79cdf0e10cSrcweir      *   - close office
80cdf0e10cSrcweir      *   - start office
81cdf0e10cSrcweir      *   - read configuration attribute settings
82cdf0e10cSrcweir      *   - create another new document
83cdf0e10cSrcweir      *   - compare old settings with new ones: should be different
84cdf0e10cSrcweir      *   - compare the document size with the resized document: should be equal
85cdf0e10cSrcweir      *   - close office
86cdf0e10cSrcweir      * - Test finished
87cdf0e10cSrcweir      */
checkPersistentWindowState()88cdf0e10cSrcweir     @Test public void checkPersistentWindowState()
89cdf0e10cSrcweir     {
90cdf0e10cSrcweir         try
91cdf0e10cSrcweir         {
92cdf0e10cSrcweir 
93cdf0e10cSrcweir             System.out.println("Connect the first time.");
94cdf0e10cSrcweir //            System.out.println("AppExecCommand: " + (String) param.get("AppExecutionCommand"));
95cdf0e10cSrcweir //            System.out.println("ConnString: " + (String) param.get("ConnectionString"));
96cdf0e10cSrcweir //            oProvider = new OfficeProvider();
97cdf0e10cSrcweir //            iOfficeCloseTime = param.getInt("OfficeCloseTime");
98cdf0e10cSrcweir //            if (iOfficeCloseTime == 0)
99cdf0e10cSrcweir //            {
100cdf0e10cSrcweir //                iOfficeCloseTime = 1000;
101cdf0e10cSrcweir //            }
102cdf0e10cSrcweir 
103cdf0e10cSrcweir             if (!connect())
104cdf0e10cSrcweir             {
105cdf0e10cSrcweir                 return;
106cdf0e10cSrcweir             }
107cdf0e10cSrcweir 
108cdf0e10cSrcweir             if (!disconnect())
109cdf0e10cSrcweir             {
110cdf0e10cSrcweir                 return;
111cdf0e10cSrcweir             }
112cdf0e10cSrcweir         }
113cdf0e10cSrcweir         catch (Exception e)
114cdf0e10cSrcweir         {
115cdf0e10cSrcweir             e.printStackTrace();
116cdf0e10cSrcweir         }
117cdf0e10cSrcweir     }
118cdf0e10cSrcweir 
connect()119cdf0e10cSrcweir     private boolean connect()
120cdf0e10cSrcweir     {
121cdf0e10cSrcweir         try
122cdf0e10cSrcweir         {
123cdf0e10cSrcweir             xMSF = getMSF();
124cdf0e10cSrcweir             try
125cdf0e10cSrcweir             {
126cdf0e10cSrcweir                 Thread.sleep(10000);
127cdf0e10cSrcweir             }
128cdf0e10cSrcweir             catch (java.lang.InterruptedException e)
129cdf0e10cSrcweir             {
130cdf0e10cSrcweir             }
131cdf0e10cSrcweir         }
132cdf0e10cSrcweir         catch (java.lang.Exception e)
133cdf0e10cSrcweir         {
134cdf0e10cSrcweir             System.out.println(e.getClass().getName());
135cdf0e10cSrcweir             System.out.println("Message: " + e.getMessage());
136cdf0e10cSrcweir             fail("Cannot connect the Office.");
137cdf0e10cSrcweir             return false;
138cdf0e10cSrcweir         }
139cdf0e10cSrcweir         return true;
140cdf0e10cSrcweir     }
141cdf0e10cSrcweir 
disconnect()142cdf0e10cSrcweir     private boolean disconnect()
143cdf0e10cSrcweir     {
144cdf0e10cSrcweir         try
145cdf0e10cSrcweir         {
146cdf0e10cSrcweir             XDesktop desk = null;
147cdf0e10cSrcweir             desk = UnoRuntime.queryInterface(XDesktop.class, xMSF.createInstance("com.sun.star.frame.Desktop"));
148cdf0e10cSrcweir             desk.terminate();
149cdf0e10cSrcweir             System.out.println("Waiting " + iOfficeCloseTime + " milliseconds for the Office to close down");
150cdf0e10cSrcweir             try
151cdf0e10cSrcweir             {
152cdf0e10cSrcweir                 Thread.sleep(iOfficeCloseTime);
153cdf0e10cSrcweir             }
154cdf0e10cSrcweir             catch (java.lang.InterruptedException e)
155cdf0e10cSrcweir             {
156cdf0e10cSrcweir             }
157cdf0e10cSrcweir             xMSF = null;
158cdf0e10cSrcweir         }
159cdf0e10cSrcweir         catch (java.lang.Exception e)
160cdf0e10cSrcweir         {
161cdf0e10cSrcweir             e.printStackTrace();
162cdf0e10cSrcweir             fail("Cannot dispose the Office.");
163cdf0e10cSrcweir             return false;
164cdf0e10cSrcweir         }
165cdf0e10cSrcweir         return true;
166cdf0e10cSrcweir     }
167cdf0e10cSrcweir 
168cdf0e10cSrcweir 
getMSF()169cdf0e10cSrcweir     private XMultiServiceFactory getMSF()
170cdf0e10cSrcweir     {
171cdf0e10cSrcweir         final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
172cdf0e10cSrcweir         return xMSF1;
173cdf0e10cSrcweir     }
174cdf0e10cSrcweir 
175cdf0e10cSrcweir     // setup and close connections
setUpConnection()176cdf0e10cSrcweir     @BeforeClass public static void setUpConnection() throws Exception {
177cdf0e10cSrcweir         System.out.println("setUpConnection()");
178cdf0e10cSrcweir         connection.setUp();
179cdf0e10cSrcweir     }
180cdf0e10cSrcweir 
tearDownConnection()181cdf0e10cSrcweir     @AfterClass public static void tearDownConnection()
182cdf0e10cSrcweir         throws InterruptedException, com.sun.star.uno.Exception
183cdf0e10cSrcweir     {
184cdf0e10cSrcweir         System.out.println("tearDownConnection()");
185cdf0e10cSrcweir         // don't do a tearDown here, desktop is already terminated.
186cdf0e10cSrcweir         // connection.tearDown();
187cdf0e10cSrcweir     }
188cdf0e10cSrcweir 
189cdf0e10cSrcweir     private static final OfficeConnection connection = new OfficeConnection();
190cdf0e10cSrcweir 
191cdf0e10cSrcweir }
192