xref: /AOO41X/main/framework/qa/complex/framework/recovery/RecoveryTools.java (revision 4fdbb183368db051e8813299211bce4ed650046d)
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 
24 package complex.framework.recovery;
25 
26 import com.sun.star.awt.XDialog;
27 import com.sun.star.awt.XExtendedToolkit;
28 import com.sun.star.awt.XWindow;
29 import com.sun.star.beans.NamedValue;
30 import com.sun.star.frame.XController;
31 import com.sun.star.frame.XDesktop;
32 import com.sun.star.frame.XDispatch;
33 import com.sun.star.frame.XDispatchProvider;
34 import com.sun.star.frame.XModel;
35 import com.sun.star.lang.XComponent;
36 import com.sun.star.lang.XMultiServiceFactory;
37 import com.sun.star.ucb.XSimpleFileAccess;
38 import com.sun.star.uno.UnoRuntime;
39 import com.sun.star.uno.XInterface;
40 import com.sun.star.util.URL;
41 import com.sun.star.util.XURLTransformer;
42 import helper.FileTools;
43 import helper.OfficeProvider;
44 import helper.UnoProvider;
45 import java.io.File;
46 import java.io.PrintWriter;
47 import java.util.HashMap;
48 import lib.TestParameters;
49 import share.LogWriter;
50 import util.PropertyName;
51 import util.UITools;
52 import util.utils;
53 
54 /**
55  * this class supports the <CODE>RecoverTest</CODE>. You will find here some helper
56  * functions.
57  */
58 public class RecoveryTools {
59 
60     private final TestParameters param;
61     private final LogWriter log;
62 
63     /**
64      * Creates new OfficeWatcher
65      * @param param the test parameter
66      * @param log a log writer
67      */
68     public RecoveryTools(TestParameters param, LogWriter log) {
69         this.param = param;
70         this.log = log;
71 
72     }
73 
74     /**
75      * get the active dialog from the top of the desktop
76      * @param xToolKit xToolKit the <CODE> XExtendedToolkit</CODE> to get the dialog from the top of the desktop.
77      * @return a <CODE>XDialog</CODE> interface of the dialog
78      */
79     public XDialog getActiveDialog( XMultiServiceFactory xMSF){
80         XWindow xWin = getActiveWindow(xMSF);
81         return (XDialog) UnoRuntime.queryInterface(XDialog.class, xWin);
82     }
83 
84     public XWindow getActiveWindow( XMultiServiceFactory xMSF){
85         XInterface xToolKit = null;
86         try {
87             xToolKit = (XInterface) xMSF.createInstance("com.sun.star.awt.Toolkit") ;
88         } catch (com.sun.star.uno.Exception e) {
89           return null;
90         }
91 
92         XExtendedToolkit tk = (XExtendedToolkit)
93             UnoRuntime.queryInterface(XExtendedToolkit.class, xToolKit);
94         Object atw = tk.getActiveTopWindow();
95         return (XWindow) UnoRuntime.queryInterface(XWindow.class, atw);
96     }
97 
98     /**
99      * After a crash the office start with a recovery diaolg. It could be that the office
100      * is connectable but not all services to get the dialog a loaded. This function
101      * tries to get the dialog until the <CODE>OfficeWatcher</CODE> kills the office.
102      * @param xToolKit the <CODE> XExtendedToolkit</CODE> to get the dialog from the top of the desktop.
103      * @return a <CODE>XDialog</CODE> interface of the dialog
104      */
105     public XDialog getActiveDialogAfterStartup(XMultiServiceFactory xMSF){
106         // while the office starts it takes some time to get the dialog.
107 
108         // the dialog is accessible AFTER the office has recoverd all documents.
109         // This could consumes more time then the TimeOut allow.
110         int counter = 0;
111         int multi = 5;
112         int pause = param.getInt(PropertyName.SHORT_WAIT)*10;
113         int timeOut = param.getInt(PropertyName.THREAD_TIME_OUT)*5;
114         int maximum = (timeOut / pause) * multi;
115 
116         XDialog oDialog = getActiveDialog(xMSF);
117 
118         while (oDialog == null && (counter < maximum)){
119             log.println("waiting until the office has recoverd... remaining " + (timeOut * multi - pause * counter)/1000 + " seconds");
120             pause(pause);
121             oDialog = getActiveDialog(xMSF);
122             counter ++;
123         }
124         return oDialog;
125     }
126 
127     /**
128      * halt the thread for some time
129      */
130     public void pause(){
131        pause(param.getInt(PropertyName.SHORT_WAIT));
132     }
133 
134     /**
135      * halt the thread for some time
136      */
137     public void pause(int sleepTime){
138         sleep(sleepTime);
139     }
140 
141     private void sleep(long millis){
142         try{
143             Thread.sleep(millis);
144         }catch (java.lang.InterruptedException e){}
145     }
146 
147     /**
148      * remove the content of the user backup folder and removes the Recovery.xcu. This
149      * was done from the Office via XSimpleFileAccess
150      * @param xMSF a <CODE>XMultiServiceFactory</CODE> to get <CODE>XSimpleFileAccess</CODE>
151      * @throws com.sun.star.io.IOException the exception was thrown if something goes wrong.
152      */
153     public void cleanRecoveryData()
154         throws com.sun.star.io.IOException
155     {
156         try{
157             HashMap recFiles = getRecoveryFiles();
158 
159             String recoveryFolder = (String) recFiles.get("recoveryFolder");
160             String recoveryXCU = (String) recFiles.get("recoveryXCU");
161 
162             log.println("try to remove content of '" + recoveryFolder + "'");
163 
164             File rf = new File(recoveryFolder);
165 
166             boolean success = FileTools.cleanDir(rf);
167             log.println("removed " + recoveryFolder + ": " + success);
168 
169             log.println("try to remove '" + recoveryXCU + "'");
170 
171             File xcu = new File(recoveryXCU);
172             if (xcu.isFile()){
173                 success = xcu.delete();
174                 log.println("removed " + recoveryXCU + " : " + success);
175             }
176 
177         } catch (Exception e){
178             throw new com.sun.star.io.IOException("could not remove old recovery data: " + e.toString());
179         }
180     }
181 
182     public HashMap getRecoveryFiles()
183         throws com.sun.star.io.IOException
184     {
185         try{
186             log.println("try to get UnoProvider...");
187             UnoProvider unoProv = new UnoProvider();
188             XMultiServiceFactory xMSF = (XMultiServiceFactory) unoProv.getManager(param);
189 
190             String userPath = utils.expandMacro(xMSF, "${$ORIGIN/bootstraprc:UserInstallation}");
191             System.out.println("userPath:'" + userPath + "'");
192 
193             if (userPath.equals(""))userPath = utils.expandMacro(xMSF, "${$ORIGIN/bootstrap.ini:UserInstallation}");
194             System.out.println("userPath:'" + userPath + "'");
195 
196             if (userPath.equals("")) throw new com.sun.star.io.IOException("could not get user path at bootstraping");
197 
198             String recoveryFolder = utils.getSystemURL(userPath + "/user/backup");
199 
200             String recoveryXCU = utils.getSystemURL(userPath + "/user/registry/data/org/openoffice/Office/Recovery.xcu");
201 
202             HashMap recFiles = new HashMap();
203 
204             recFiles.put("recoveryFolder", recoveryFolder);
205             recFiles.put("recoveryXCU", recoveryXCU);
206             return recFiles;
207 
208         } catch (Exception e){
209             throw new com.sun.star.io.IOException("could not get recovery folder: " + e.toString());
210         }
211 
212     }
213     /**
214      * This function close the office while calling terminate on the desktop. If
215      * this failed, the <CODE>ProcessHandler</CODE> kills the process.
216      * @param xMSF the <CODE>XMultiServiceFactory</CODE>
217      * @return <CODE>TRUE</CODE> if no exception was thrown, otherwise <CODE>FALSE</CODE>
218      */
219     public boolean closeOffice(XMultiServiceFactory xMSF) {
220         try {
221             XDesktop desk = (XDesktop) UnoRuntime.queryInterface(
222                     XDesktop.class, xMSF.createInstance(
223                     "com.sun.star.frame.Desktop"));
224             xMSF = null;
225 
226             desk.terminate();
227             log.println("Waiting until ProcessHandler loose the office...");
228 
229         }
230         catch (java.lang.Exception e) {
231             e.printStackTrace();
232             return false;
233         }
234         waitForClosedOffice();
235         return true;
236     }
237 
238     /**
239      * This function waits until the office is closed. If the closing time reach
240      * the value of parameter <CODE>THREAD_TIME_OUT</CODE> the office was killed.
241      */
242     public void waitForClosedOffice(){
243         // check for the office process
244         helper.ProcessHandler ph = (helper.ProcessHandler) param.get("AppProvider");
245 
246         int timeOut = param.getInt(PropertyName.THREAD_TIME_OUT)*5;
247         int pause = param.getInt(PropertyName.SHORT_WAIT)*20;
248         int multi = 0;
249         while ((ph != null) && (ph.getExitCode()<0) && (pause*multi < timeOut)) {
250             log.println("waiting until the office is closed... remaining " + (timeOut - pause * multi)/1000 + " seconds");
251             pause(pause);
252             multi ++;
253         }
254 
255         // be shure that office is closed
256         if (ph != null) ph.kill();
257     }
258 
259     public void killOffice(){
260         helper.ProcessHandler ph = (helper.ProcessHandler) param.get("AppProvider");
261         ph.kill();
262     }
263 
264     /**
265      * The office must be started WITH restore and crashreporter functionality.
266      * Therefore the parmater '<CODE>-norestore</CODE>' and '<CODE>-nocrashreport</CODE>'
267      * was removed from the <CODE>AppExecutionCommand</CODE> parameter
268      */
269     public void removeParametersFromAppExecutionCommand(){
270 
271         //remove some params to start office
272         String office = (String) param.get("AppExecutionCommand");
273         String[] params = {"-norestore", "-nocrashreport"};
274 
275         for (int i = 0; i < params.length; i++){
276             int index = office.indexOf(params[i]);
277             int length = params[i].length();
278             if (index != -1){
279                 office = office.substring(0, index) + office.substring(index + length);
280                 log.println("removed '" + params[i] + "' from AppExecutionCommand: " + office);
281             }
282         }
283         param.put("AppExecutionCommand", office);
284         log.println("connect: " + (String) param.get("AppExecutionCommand"));
285 
286     }
287 
288     /**
289      * This function uses accessibility to handle modal dialogs like the
290      * "Are you sure" dialog.
291      * It cklick the named button given in parameter <CODE>buttonName</CODE>
292      * @param buttonName the name of the button wich should be chlicked
293      */
294     public void handleModalDialog(XMultiServiceFactory xMSF, String buttonName)
295                 throws com.sun.star.accessibility.IllegalAccessibleComponentStateException
296     {
297 
298         log.println("try to get modal Dialog...");
299 
300         pause();
301 
302         XWindow oDialog = getActiveWindow(xMSF);
303 
304         if (oDialog == null) throw new com.sun.star.accessibility.IllegalAccessibleComponentStateException("could not get modal Dialog");
305 
306 
307         UITools oUITools = new UITools(xMSF, oDialog);
308         oUITools.printAccessibleTree((PrintWriter) log, param.getBool(PropertyName.DEBUG_IS_ACTIVE));
309 
310         try{
311             log.println("click ' " + buttonName + "' button..");
312             oUITools.clickButton(buttonName);
313         } catch ( java.lang.Exception e){
314             throw new com.sun.star.accessibility.IllegalAccessibleComponentStateException("Could not klick '"+buttonName +"' at modal dialog: " + e.toString());
315         }
316         pause();
317     }
318 
319     public void clickThreadButton(XMultiServiceFactory xMSF, XWindow xWindow, String buttonName)
320                 throws com.sun.star.accessibility.IllegalAccessibleComponentStateException
321     {
322         KlickButtonThread kbt = new KlickButtonThread(xMSF, xWindow, buttonName);
323         kbt.start();
324         pause(param.getInt(PropertyName.SHORT_WAIT) * 10);
325     }
326 
327     public void copyRecoveryData(boolean backup)
328         throws com.sun.star.io.IOException, java.io.IOException
329     {
330         HashMap recFiles = null;
331 
332         try{
333             recFiles = getRecoveryFiles();
334         } catch ( com.sun.star.io.IOException e){
335             throw new  com.sun.star.io.IOException("Could not get recovery files: " + e.toString());
336         }
337 
338         try{
339             String recoveryFolder = (String) recFiles.get("recoveryFolder");
340             String recoveryXCU = (String) recFiles.get("recoveryXCU");
341 
342             File recFolder = new File(recoveryFolder);
343             File recFolderBackup = new File(recoveryFolder+".recoveryTest");
344 
345             File recXCU = new File(recoveryXCU);
346             File recXCUBackup = new File(recoveryXCU + ".recoveryTest");
347 
348             if (backup){
349                 FileTools.copyDirectory(recFolder, recFolderBackup);
350                 FileTools.copyFile(recXCU, recXCUBackup);
351             } else {
352                 FileTools.copyDirectory(recFolderBackup, recFolder);
353                 FileTools.copyFile(recXCUBackup, recXCU);
354 
355             }
356         } catch (java.io.IOException e){
357             throw new java.io.IOException("Could not copy recovery files: " + e.toString());
358         }
359    }
360 
361 
362 }