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.desktop;
24 
25 
26 import com.sun.star.lang.XMultiServiceFactory;
27 import com.sun.star.frame.XDesktop;
28 import com.sun.star.uno.UnoRuntime;
29 import helper.OfficeProvider;
30 //import complex.persistent_window_states.helper.DocumentHandle;
31 
32 // ---------- junit imports -----------------
33 import org.junit.After;
34 import org.junit.AfterClass;
35 import org.junit.Before;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.openoffice.test.OfficeConnection;
39 import static org.junit.Assert.*;
40 // ------------------------------------------
41 
42 /**
43  * Parameters:
44  * <ul>
45  *   <li>NoOffice=yes - StarOffice is not started initially.</li>
46  * </ul>
47  */
48 public class DesktopTerminate
49 {
50 
51     private XMultiServiceFactory xMSF;
52     private int iOfficeCloseTime = 1000;
53 
54     /**
55      * A frunction to tell the framework, which test functions are available.
56      * Right now, it's only 'checkPersistentWindowState'.
57      * @return All test methods.
58      */
59 //    public String[] getTestMethodNames()
60 //    {
61 //        return new String[]
62 //                {
63 //                    "checkPersistentWindowState"
64 //                };
65 //    }
66 
67     /**
68      * Test if all available document types change the
69      * persistent Window Attributes
70      *
71      * The test follows basically these steps:
72      * - Create a configuration reader and a componentloader
73      * - Look for all document types in the configuration
74      * - Do for every doc type
75      *   - start office
76      *   - read configuration attibute settings
77      *   - create a new document
78      *   - resize the document and close it
79      *   - close office
80      *   - start office
81      *   - read configuration attribute settings
82      *   - create another new document
83      *   - compare old settings with new ones: should be different
84      *   - compare the document size with the resized document: should be equal
85      *   - close office
86      * - Test finished
87      */
88     @Test public void checkPersistentWindowState()
89     {
90         try
91         {
92 
93             System.out.println("Connect the first time.");
94 //            System.out.println("AppExecCommand: " + (String) param.get("AppExecutionCommand"));
95 //            System.out.println("ConnString: " + (String) param.get("ConnectionString"));
96 //            oProvider = new OfficeProvider();
97 //            iOfficeCloseTime = param.getInt("OfficeCloseTime");
98 //            if (iOfficeCloseTime == 0)
99 //            {
100 //                iOfficeCloseTime = 1000;
101 //            }
102 
103             if (!connect())
104             {
105                 return;
106             }
107 
108             if (!disconnect())
109             {
110                 return;
111             }
112         }
113         catch (Exception e)
114         {
115             e.printStackTrace();
116         }
117     }
118 
119     private boolean connect()
120     {
121         try
122         {
123             xMSF = getMSF();
124             try
125             {
126                 Thread.sleep(10000);
127             }
128             catch (java.lang.InterruptedException e)
129             {
130             }
131         }
132         catch (java.lang.Exception e)
133         {
134             System.out.println(e.getClass().getName());
135             System.out.println("Message: " + e.getMessage());
136             fail("Cannot connect the Office.");
137             return false;
138         }
139         return true;
140     }
141 
142     private boolean disconnect()
143     {
144         try
145         {
146             XDesktop desk = null;
147             desk = UnoRuntime.queryInterface(XDesktop.class, xMSF.createInstance("com.sun.star.frame.Desktop"));
148             desk.terminate();
149             System.out.println("Waiting " + iOfficeCloseTime + " milliseconds for the Office to close down");
150             try
151             {
152                 Thread.sleep(iOfficeCloseTime);
153             }
154             catch (java.lang.InterruptedException e)
155             {
156             }
157             xMSF = null;
158         }
159         catch (java.lang.Exception e)
160         {
161             e.printStackTrace();
162             fail("Cannot dispose the Office.");
163             return false;
164         }
165         return true;
166     }
167 
168 
169     private XMultiServiceFactory getMSF()
170     {
171         final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
172         return xMSF1;
173     }
174 
175     // setup and close connections
176     @BeforeClass public static void setUpConnection() throws Exception {
177         System.out.println("setUpConnection()");
178         connection.setUp();
179     }
180 
181     @AfterClass public static void tearDownConnection()
182         throws InterruptedException, com.sun.star.uno.Exception
183     {
184         System.out.println("tearDownConnection()");
185         // don't do a tearDown here, desktop is already terminated.
186         // connection.tearDown();
187     }
188 
189     private static final OfficeConnection connection = new OfficeConnection();
190 
191 }
192