summaryrefslogtreecommitdiff
path: root/vcl/qa/complex/persistent_window_states/PersistentWindowTest.java
blob: edceeeafd88328cc31d6a0fb91a8e996ce110ca7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/
package complex.persistent_window_states;


import com.sun.star.lang.XServiceInfo;
import com.sun.star.lang.XInitialization;
import com.sun.star.uno.Type;
import com.sun.star.uno.Any;
import com.sun.star.lang.XTypeProvider;
import com.sun.star.lang.XSingleServiceFactory;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.lang.XComponent;
import com.sun.star.frame.XDesktop;
import com.sun.star.frame.XFramesSupplier;
import com.sun.star.frame.XFrames;
import com.sun.star.registry.XRegistryKey;
import com.sun.star.comp.loader.FactoryHelper;
import com.sun.star.container.XIndexAccess;
import com.sun.star.beans.XPropertySet;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.AnyConverter;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.awt.Rectangle;
import com.sun.star.util.XCloseable;
import helper.ConfigurationRead;
import complexlib.ComplexTestCase;
import helper.OfficeProvider;
import complex.persistent_window_states.DocumentHandle;

/**
 * Parameters:
 * <ul>
 *   <li>NoOffice=yes - StarOffice is not started initially.</li>
 * </ul>
 */
public class PersistentWindowTest extends ComplexTestCase {

    private XMultiServiceFactory xMSF;
    private OfficeProvider oProvider;
    private int iOfficeCloseTime = 0;

    /**
     * A frunction to tell the framework, which test functions are available.
     * Right now, it's only 'checkPersistentWindowState'.
     * @return All test methods.
     */
    public String[] getTestMethodNames() {
        return new String[]{"checkPersistentWindowState"};
    }

    /**
     * Test if all available document types change the
     * persistent Window Attributes
     *
     * The test follows basically these steps:
     * - Create a configuration reader and a componentloader
     * - Look for all document types in the configuration
     * - Do for every doc type
     *   - start office
     *   - read configuration attibute settings
     *   - create a new document
     *   - resize the document and close it
     *   - close office
     *   - start office
     *   - read configuration attribute settings
     *   - create another new document
     *   - compare old settings with new ones: should be different
     *   - compare the document size with the resized document: should be equal
     *   - close office
     * - Test finished
     */
    public void checkPersistentWindowState()
    {
        try {

        log.println("Connect the first time.");
    log.println("AppExecCommand: " + (String)param.get("AppExecutionCommand"));
    log.println("ConnString: " + (String)param.get("ConnectionString"));
        oProvider = new OfficeProvider();
        iOfficeCloseTime = param.getInt("OfficeCloseTime");
        if ( iOfficeCloseTime == 0 ) {
            iOfficeCloseTime = 1000;
        }

        if (!connect()) return;

        // create the configuration provider
        Object o = null;
        try {
            o = xMSF.createInstance(
                        "com.sun.star.configuration.ConfigurationProvider");
        }
        catch(com.sun.star.uno.Exception e) {
            failed("Cannot create \"com.sun.star.configuration."+
                                            "ConfigurationProvider\"");
            return;
        }

        // fetch the multi service factory for setup
        XMultiServiceFactory xCP = (XMultiServiceFactory)
                    UnoRuntime.queryInterface(XMultiServiceFactory.class, o);

        // create the configuration reader
        ConfigurationRead cfgRead = new ConfigurationRead(xCP);

        // just test the wrong ones, not all.
        String [] els = new String[]{
                "Office/Factories/com.sun.star.drawing.DrawingDocument",
                "Office/Factories/com.sun.star.formula.FormulaProperties",
                //"Office/Factories/com.sun.star.presentation.PresentationDocument",
                "Office/Factories/com.sun.star.sheet.SpreadsheetDocument",
                "Office/Factories/com.sun.star.text.GlobalDocument",
                "Office/Factories/com.sun.star.text.TextDocument",
                "Office/Factories/com.sun.star.text.WebDocument",
            };
        // uncomment the following line for all doc types
//        String [] els = cfgRead.getSubNodeNames("Office/Factories");

        log.println("Found "+ els.length + " document types to test.\n");
        if (!disconnect()) return;

        // for all types
        for(int i=0; i<els.length; i++) {
            log.println("\tStart test for document type " + i + ": " + els[i]);
            // exclude chart documents: cannot be created this way.
            if ( els[i].indexOf("ChartDocument") != -1) {
                log.println("Skipping chart document: cannot be create like this.");
                continue;
            }

            // start an office
            if (!connect()) return;

            // get configuration
            String[] settings = getConfigurationAndLoader(xMSF, els[i]);
            if (settings == null) {
                log.println("Skipping document type " + els[i]);
                disconnect();
                continue;
            }
            String cfg = settings[1];

            // load a document
            DocumentHandle handle = loadDocument(xMSF, settings[0]);

            // first size
            Rectangle rect1 = handle.getDocumentPosSize();

            // resize
            handle.resizeDocument();
            // after resize
            Rectangle rect2 = handle.getDocumentPosSize();

            // disposeManager and start a new office
            if (!disconnect()) return;

            if (!connect()) return;

            // get configuration
            settings = getConfigurationAndLoader(xMSF, els[i]);

            String newCfg = settings[1];

            // load a document
            handle = loadDocument(xMSF, settings[0]);

            Rectangle newRect = handle.getDocumentPosSize();

            // print the settings and window sizes
            log.println("----------------------------");
            log.println("Initial Config String      : " + cfg);
            log.println("Config String after restart: " + newCfg);

            log.println("----------------------------");
            log.println("Initial window       (X,Y,Width,Height): "
                        +rect1.X+";"+rect1.Y+";"+ rect1.Width+";"+rect1.Height);
            log.println("Window after resize  (X,Y,Width,Height): "
                        +rect2.X+";"+rect2.Y+";"+ rect2.Width+";"+rect2.Height);
            log.println("Window after restart (X,Y,Width,Height): "
                        +newRect.X+";"+newRect.Y+";"+newRect.Width+";"
                        +newRect.Height);

            // compare to see if resize worked
            log.println("----------------------------");
            assure("Resize values for "+ els[i] +
                    " are equal.", !compareRectangles(rect1, rect2), true);
            // compare settings and sizes
            assure("Config settings for "+ els[i] +
                    " were not changed.", !cfg.equals(newCfg), true);
            assure("Resized and restarted window for "+ els[i] +
                    " are not equal.", compareRectangles(rect2, newRect), true);
            log.println("----------------------------");

            // disposeManager
            if (!disconnect()) return;

            log.println("\tFinish test for document type " + i + ": " + els[i]);

        }
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Get the configuration settings and the document loader
     * @param  xMSF A MultiServiceFactory from an office
     * @param cfgString A configuration string
     * @return Settings and Loader
     */
    private static String[] getConfigurationAndLoader(XMultiServiceFactory xMSF,
                                                            String cfgString) {
        String[] conf = new String[2];

        try {
            Object o = xMSF.createInstance(
                            "com.sun.star.configuration.ConfigurationProvider");

            // fetch the multi service factory for setup
            XMultiServiceFactory xCP = (XMultiServiceFactory)
                        UnoRuntime.queryInterface(XMultiServiceFactory.class, o);

            // create the configuration reader
            ConfigurationRead cfgRead = new ConfigurationRead(xCP);

            // get the document loader
            String loader = getStringFromObject(
                    cfgRead.getByHierarchicalName(cfgString + "/ooSetupFactoryEmptyDocumentURL"));

            if (loader == null) return null;
            log.println("\tLoader: " + loader);

            // read attributes
            String hierchName = cfgString + "/ooSetupFactoryWindowAttributes";
            String setupSettings = getStringFromObject(cfgRead.getByHierarchicalName(hierchName));
            // remove slots: just plain document types have to start
            if ( loader.indexOf("?slot") != -1 ) {
                loader = loader.substring(0, loader.indexOf("?slot"));
                System.out.println("Loader: "+loader);
            }

            conf[0] = loader;
            conf[1] = setupSettings;
        }
        catch(com.sun.star.uno.Exception e) {}
        return conf;
    }

    /**
     * Load a document
     * @param xMSF A MultiServiceFactory from an office
     * @param docLoader A documet loader
     * @return A handle to the document
     */
    private DocumentHandle loadDocument(XMultiServiceFactory xMSF,
                                                            String docLoader) {
        DocumentHandle docHandle = null;
        try {
            // create component loaader
            XComponentLoader xCompLoader = (XComponentLoader)
                                UnoRuntime.queryInterface(
                                XComponentLoader.class, xMSF.createInstance(
                                "com.sun.star.frame.Desktop"));
            XFramesSupplier xFrameSupp = (XFramesSupplier)UnoRuntime.queryInterface(XFramesSupplier.class, xCompLoader);
            // close all existing frames
            XFrames xFrames = xFrameSupp.getFrames();
            XIndexAccess xAcc = (XIndexAccess)UnoRuntime.queryInterface(XIndexAccess.class, xFrames);
            for ( int i=0; i<xAcc.getCount(); i++ ) {
                XCloseable xClose = (XCloseable)UnoRuntime.queryInterface(XCloseable.class, xAcc.getByIndex(i));
                try {
                    if ( xClose != null ) {
                        xClose.close(false);
                    }
                    else  {
                        failed("Could not query frame for XCloseable!");
                    }
                }
                catch( com.sun.star.uno.Exception e ) {
                    e.printStackTrace((java.io.PrintWriter)log);
                    failed("Could not query frame for XCloseable!");
                }
            }
            docHandle = new DocumentHandle(xCompLoader);
            docHandle.loadDocument(docLoader, false);
        }
        catch(com.sun.star.uno.Exception e) {
            e.printStackTrace();
        }
        catch(java.lang.Exception e) {
            e.printStackTrace();
        }
        return docHandle;
    }

    private boolean connect() {
        try {
            xMSF = (XMultiServiceFactory)oProvider.getManager(param);
            try {
                Thread.sleep(10000);
            }
            catch(java.lang.InterruptedException e) {}
        }
        catch (java.lang.Exception e) {
            log.println(e.getClass().getName());
            log.println("Message: " + e.getMessage());
            failed("Cannot connect the Office.");
            return false;
        }
        return true;
    }

    private boolean disconnect() {
        try {
            XDesktop desk = null;
            desk = (XDesktop) UnoRuntime.queryInterface(
                    XDesktop.class, xMSF.createInstance(
                    "com.sun.star.frame.Desktop"));
            xMSF = null;
            desk.terminate();
            log.println("Waiting " + iOfficeCloseTime + " milliseconds for the Office to close down");
            try {
                Thread.sleep(iOfficeCloseTime);
            }
            catch(java.lang.InterruptedException e) {}
        }
        catch (java.lang.Exception e) {
            e.printStackTrace();
            failed("Cannot dispose the Office.");
            return false;
        }
        return true;
    }

    private static String getStringFromObject(Object oName) {
        if (oName instanceof String)
            return (String)oName;
        String value = null;
        if (oName instanceof Any) {
            try {
                value = AnyConverter.toString(oName);
                if (value == null) {
                    log.println("Got a void css.uno.Any as loading string.");
                }
            }
            catch(Exception e) {
                log.println("This document type cannot be opened directly.");
            }
        }
        return value;
    }

    /**
     * Compare two rectangles. Return true, if both are equal, false
     * otherwise.
     * @param rect1 First Rectangle.
     * @param rect2 Second Rectangle.
     * @return True, if the rectangles are equal.
     */
    private boolean compareRectangles(Rectangle rect1, Rectangle rect2) {
        boolean result = true;
        result &= (rect1.X==rect2.X);
        result &= (rect1.Y==rect2.Y);
        result &= (rect1.Width==rect2.Width);
        result &= (rect1.Height==rect2.Height);
        return result;
    }
}