summaryrefslogtreecommitdiffstats
path: root/framework/qa/complex/XTitle/CheckXTitle.java
blob: 2e982fab586fed04c5b7f1afbfecefdce8d18b20 (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
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

package complex.XTitle;

import static org.junit.Assert.*;
import util.utils;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openoffice.test.OfficeConnection;
import com.sun.star.beans.PropertyValue;
import com.sun.star.frame.Desktop;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.frame.XFrame2;
import com.sun.star.frame.XModel;
import com.sun.star.frame.XTitle;
import com.sun.star.frame.XController;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.util.URLTransformer;
import com.sun.star.util.XURLTransformer;
import com.sun.star.util.URL;
import com.sun.star.frame.XDispatchProvider;
import com.sun.star.frame.XDispatch;

/** @short Check the interface XTitle

    @descr These tests check in various cases the correctness of
           the title in the ui window.
*/

public class CheckXTitle
{

    // some const
    private static final String DOCUMENT_TITLE                    = "documentTitle";
    private static final String UNO_URL_FOR_PRINT_PREVIEW         = ".uno:PrintPreview";
    private static final String UNO_URL_FOR_CLOSING_PRINT_PREVIEW = ".uno:ClosePreview";
    private static final String UNO_URL_FOR_CLOSING_DOC           = ".uno:CloseWin";

    // members

    /** will be set to xDesktop OR xFrame. */
    private XComponentLoader m_xLoader = null;

    /** provides uno service manager */
    private XMultiServiceFactory m_xMSF = null;

    /** used for parsing uno query URLs */
    private XURLTransformer m_xParser = null;

    // test environment
    /** @short  Create the environment for following tests.

        @descr  Use either a component loader from desktop or
                from frame
     */
    @Before public void before() throws Exception
    {
        // get uno service manager from global test environment
        /* points to the global uno service manager. */
        m_xMSF = getMSF();

        // define default loader
        m_xLoader = Desktop.create(connection.getComponentContext());

        // get URL parser
        m_xParser = URLTransformer.create(connection.getComponentContext());
    }

    /** @short  close the environment.
     */
    @After public void after() throws Exception
    {
        m_xLoader = null;
        m_xParser = null;
        m_xMSF    = null;
    }

    private URL parseURL(String unoURL)
    {
        URL[] aParseURL = new URL[] { new URL() };
        aParseURL[0].Complete = unoURL;
        m_xParser.parseStrict(aParseURL);
        return aParseURL[0];
    }

    private void waitUntilDispatcherAvailable(XModel xModel, String unoURL)
        throws InterruptedException
    {
        utils.waitForEventIdle(m_xMSF);

        // On Windows, some events such as focus changes are handled
        // asynchronously using PostMessage (Windows message queue)
        // so the previous UI transition may still not have completed yet
        // even though we called waitForEventIdle.
        //
        // Loop a few times until the desired dispatcher is available, which
        // is a better indication that the UI transition has completed.

        XDispatchProvider xDisProv;
        XDispatch xDispatcher = null;
        URL parsed_url = parseURL(unoURL);

        for (int ntries = 1; ntries < 5; ++ntries) {
            xDisProv = UnoRuntime.queryInterface(
                XDispatchProvider.class, xModel.getCurrentController() );
            xDispatcher = xDisProv.queryDispatch(parsed_url, "", 0);
            if (xDispatcher != null)
                break;
            Thread.sleep(250);
        }
        assertNotNull("Can not obtain dispatcher for query: " + unoURL, xDispatcher);
    }


    // prepare an uno URL query and dispatch it
    private void prepareQueryAndDispatch(XDispatchProvider xDisProv, String unoURL)
    {
        XDispatch xDispatcher = null;
        URL parsed_url = parseURL(unoURL);

        xDispatcher = xDisProv.queryDispatch(parsed_url, "", 0);
        assertNotNull("Can not obtain dispatcher for query: " + unoURL, xDispatcher);
        xDispatcher.dispatch(parsed_url, null);
    }

    /** @short checks the numbers displayed in the title

        @descr cycles through default view and print preview
               and asserts that the title doesn't change.

        disabled until the waitUntilDispatcherAvailable can be replaced
    */
    @Test
    public void checkTitleNumbers() throws Exception
    {
        PropertyValue[] lArgs = new PropertyValue[1];

        lArgs[0]         = new PropertyValue();
        lArgs[0].Name    = "Hidden";
        lArgs[0].Value   = Boolean.FALSE;

        // load doc
        XComponent xDoc=null;
        xDoc = m_xLoader.loadComponentFromURL("private:factory/swriter", "_blank", 0, lArgs);
        assertNotNull("Could not load temporary document", xDoc);

        XModel xModel = UnoRuntime.queryInterface( XModel.class, xDoc );
        XController xController = UnoRuntime.queryInterface( XController.class, xModel.getCurrentController() );
        XTitle xTitle = UnoRuntime.queryInterface( XTitle.class, xModel.getCurrentController().getFrame() );
        XDispatchProvider xDisProv = null;

        // get window title with ui in default mode
        String defaultTitle = xTitle.getTitle();

        xDisProv = UnoRuntime.queryInterface( XDispatchProvider.class, xModel.getCurrentController() );
        prepareQueryAndDispatch( xDisProv, UNO_URL_FOR_PRINT_PREVIEW );
        waitUntilDispatcherAvailable( xModel, UNO_URL_FOR_CLOSING_PRINT_PREVIEW );

        // get window title with ui in print preview mode
        String printPreviewTitle = xTitle.getTitle();
        assertEquals("Title mismatch between default view window title and print preview window title",
                     defaultTitle, printPreviewTitle);

        xDisProv = UnoRuntime.queryInterface( XDispatchProvider.class, xModel.getCurrentController() );
        prepareQueryAndDispatch( xDisProv, UNO_URL_FOR_CLOSING_PRINT_PREVIEW );
        waitUntilDispatcherAvailable( xModel, UNO_URL_FOR_CLOSING_DOC );

        //get window title with ui back in default mode
        String printPreviewClosedTitle = xTitle.getTitle();
        assertEquals("Title mismatch between default view window title and title after switching from print preview to default view window"                     ,defaultTitle, printPreviewClosedTitle);

        xDisProv = UnoRuntime.queryInterface( XDispatchProvider.class, xModel.getCurrentController() );
        prepareQueryAndDispatch( xDisProv, UNO_URL_FOR_CLOSING_DOC );

        xDoc     = null;
        xDisProv = null;
    }

    /** checks the if SuggestedSaveAsName is displayed in the title */
    @Test
    public void checkTitleSuggestedFileName() throws Exception
    {
        PropertyValue[] lArgs = new PropertyValue[2];

        lArgs[0]         = new PropertyValue();
        lArgs[0].Name    = "Hidden";
        lArgs[0].Value   = Boolean.FALSE;
        lArgs[1]         = new PropertyValue();
        lArgs[1].Name    = "SuggestedSaveAsName";
        lArgs[1].Value   = "suggestme.odt";

        // load doc
        XComponent xDoc = m_xLoader.loadComponentFromURL("private:factory/swriter", "_blank", 0, lArgs);
        assertNotNull("Could not load temporary document", xDoc);

        XModel xModel = UnoRuntime.queryInterface( XModel.class, xDoc );
        XTitle xTitle = UnoRuntime.queryInterface( XTitle.class, xModel.getCurrentController().getFrame() );

        String title = xTitle.getTitle();
        assertTrue(title.startsWith("suggestme.odt"));

        XDispatchProvider xDisProv = UnoRuntime.queryInterface( XDispatchProvider.class, xModel.getCurrentController() );
        prepareQueryAndDispatch( xDisProv, UNO_URL_FOR_CLOSING_DOC );
    }

    /** @short sets frame title and checks for infinite recursion

        @descr sets frame title. then cycles through default and
               print preview. then closes the window and checks
               for infinite recursion.

        disabled until the waitUntilDispatcherAvailable can be replaced
    */
    @Test
    public void setTitleAndCheck() throws Exception
    {
        PropertyValue[] lArgs = new PropertyValue[1];

        lArgs[0]       = new PropertyValue();
        lArgs[0].Name  = "Hidden";
        lArgs[0].Value = Boolean.FALSE;

        // load doc
        XComponent xDoc = null;
        xDoc = m_xLoader.loadComponentFromURL("private:factory/swriter", "_blank", 0, lArgs);
        assertNotNull("Could not create office document", xDoc);
        XModel xModel  = UnoRuntime.queryInterface( XModel.class, xDoc );
        XFrame2 xFrame = UnoRuntime.queryInterface( XFrame2.class, xModel.getCurrentController().getFrame() );
        XDispatchProvider xDisProv = null;
        // set doc title
        xFrame.setTitle(DOCUMENT_TITLE);

        // switch to print preview mode
        xDisProv = UnoRuntime.queryInterface( XDispatchProvider.class, xModel.getCurrentController() );
        prepareQueryAndDispatch( xDisProv, UNO_URL_FOR_PRINT_PREVIEW );
        waitUntilDispatcherAvailable( xModel, UNO_URL_FOR_CLOSING_PRINT_PREVIEW );

        // switch back to default mode
        xDisProv = UnoRuntime.queryInterface( XDispatchProvider.class, xModel.getCurrentController() );
        prepareQueryAndDispatch( xDisProv, UNO_URL_FOR_CLOSING_PRINT_PREVIEW );
        waitUntilDispatcherAvailable( xModel, UNO_URL_FOR_CLOSING_DOC );

        // close document
        xDisProv = UnoRuntime.queryInterface( XDispatchProvider.class, xModel.getCurrentController() );
        try{
                prepareQueryAndDispatch( xDisProv, UNO_URL_FOR_CLOSING_DOC );
        } catch( Exception e ) {
            fail(e.toString());
        }

        xDoc     = null;
        xDisProv = null;
    }

    /** @short checks creation of new empty document with readonly set to true

        @descr creation of a new document with readonly property set
               to true.  this should not fail even if as such it can
               be seen as a slightly silly thing to do. But existing
               3rd-party code depends on it to work.
    */
    @Test public void checkLoadingWithReadOnly()
    {
        PropertyValue[] lArgs = new PropertyValue[2];

        lArgs[0]         = new PropertyValue();
        lArgs[0].Name    = "Hidden";
        lArgs[0].Value   = Boolean.FALSE;
        lArgs[1]         = new PropertyValue();
        lArgs[1].Name    = "ReadOnly";
        lArgs[1].Value   = Boolean.TRUE;

        // load doc
        XComponent xDoc = null;
        try{
                xDoc = m_xLoader.loadComponentFromURL("private:factory/swriter", "_default", 0, lArgs);
        } catch (Exception e) {
        }
        assertNotNull("Creating a new document read with ReadOnly property true should work (even if slightly silly)", xDoc);
    }

    private XMultiServiceFactory getMSF()
    {
        return UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
    }

    // setup and close connections
    @BeforeClass
    public static void setUpConnection() throws Exception
    {
        System.out.println("setUpConnection()");
        connection.setUp();
    }

    @AfterClass
    public static void tearDownConnection()
            throws InterruptedException, com.sun.star.uno.Exception
    {
        System.out.println("tearDownConnection()");
        connection.tearDown();
    }
    private static final OfficeConnection connection = new OfficeConnection();
}