1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 package complex.desktop;
28 
29 
30 import com.sun.star.lang.XMultiServiceFactory;
31 import com.sun.star.frame.XDesktop;
32 import com.sun.star.uno.UnoRuntime;
33 import helper.OfficeProvider;
34 //import complex.persistent_window_states.helper.DocumentHandle;
35 
36 // ---------- junit imports -----------------
37 import org.junit.After;
38 import org.junit.AfterClass;
39 import org.junit.Before;
40 import org.junit.BeforeClass;
41 import org.junit.Test;
42 import org.openoffice.test.OfficeConnection;
43 import static org.junit.Assert.*;
44 // ------------------------------------------
45 
46 /**
47  * Parameters:
48  * <ul>
49  *   <li>NoOffice=yes - StarOffice is not started initially.</li>
50  * </ul>
51  */
52 public class DesktopTerminate
53 {
54 
55     private XMultiServiceFactory xMSF;
56     private int iOfficeCloseTime = 1000;
57 
58     /**
59      * A frunction to tell the framework, which test functions are available.
60      * Right now, it's only 'checkPersistentWindowState'.
61      * @return All test methods.
62      */
63 //    public String[] getTestMethodNames()
64 //    {
65 //        return new String[]
66 //                {
67 //                    "checkPersistentWindowState"
68 //                };
69 //    }
70 
71     /**
72      * Test if all available document types change the
73      * persistent Window Attributes
74      *
75      * The test follows basically these steps:
76      * - Create a configuration reader and a componentloader
77      * - Look for all document types in the configuration
78      * - Do for every doc type
79      *   - start office
80      *   - read configuration attibute settings
81      *   - create a new document
82      *   - resize the document and close it
83      *   - close office
84      *   - start office
85      *   - read configuration attribute settings
86      *   - create another new document
87      *   - compare old settings with new ones: should be different
88      *   - compare the document size with the resized document: should be equal
89      *   - close office
90      * - Test finished
91      */
92     @Test public void checkPersistentWindowState()
93     {
94         try
95         {
96 
97             System.out.println("Connect the first time.");
98 //            System.out.println("AppExecCommand: " + (String) param.get("AppExecutionCommand"));
99 //            System.out.println("ConnString: " + (String) param.get("ConnectionString"));
100 //            oProvider = new OfficeProvider();
101 //            iOfficeCloseTime = param.getInt("OfficeCloseTime");
102 //            if (iOfficeCloseTime == 0)
103 //            {
104 //                iOfficeCloseTime = 1000;
105 //            }
106 
107             if (!connect())
108             {
109                 return;
110             }
111 
112             if (!disconnect())
113             {
114                 return;
115             }
116         }
117         catch (Exception e)
118         {
119             e.printStackTrace();
120         }
121     }
122 
123     private boolean connect()
124     {
125         try
126         {
127             xMSF = getMSF();
128             try
129             {
130                 Thread.sleep(10000);
131             }
132             catch (java.lang.InterruptedException e)
133             {
134             }
135         }
136         catch (java.lang.Exception e)
137         {
138             System.out.println(e.getClass().getName());
139             System.out.println("Message: " + e.getMessage());
140             fail("Cannot connect the Office.");
141             return false;
142         }
143         return true;
144     }
145 
146     private boolean disconnect()
147     {
148         try
149         {
150             XDesktop desk = null;
151             desk = UnoRuntime.queryInterface(XDesktop.class, xMSF.createInstance("com.sun.star.frame.Desktop"));
152             desk.terminate();
153             System.out.println("Waiting " + iOfficeCloseTime + " milliseconds for the Office to close down");
154             try
155             {
156                 Thread.sleep(iOfficeCloseTime);
157             }
158             catch (java.lang.InterruptedException e)
159             {
160             }
161             xMSF = null;
162         }
163         catch (java.lang.Exception e)
164         {
165             e.printStackTrace();
166             fail("Cannot dispose the Office.");
167             return false;
168         }
169         return true;
170     }
171 
172 
173     private XMultiServiceFactory getMSF()
174     {
175         final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
176         return xMSF1;
177     }
178 
179     // setup and close connections
180     @BeforeClass public static void setUpConnection() throws Exception {
181         System.out.println("setUpConnection()");
182         connection.setUp();
183     }
184 
185     @AfterClass public static void tearDownConnection()
186         throws InterruptedException, com.sun.star.uno.Exception
187     {
188         System.out.println("tearDownConnection()");
189         // don't do a tearDown here, desktop is already terminated.
190         // connection.tearDown();
191     }
192 
193     private static final OfficeConnection connection = new OfficeConnection();
194 
195 }
196