2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package complex
.XTitle
;
21 import static org
.junit
.Assert
.*;
24 import org
.junit
.After
;
25 import org
.junit
.AfterClass
;
26 import org
.junit
.Before
;
27 import org
.junit
.BeforeClass
;
28 import org
.junit
.Test
;
29 import org
.openoffice
.test
.OfficeConnection
;
30 import com
.sun
.star
.beans
.PropertyValue
;
31 import com
.sun
.star
.frame
.Desktop
;
32 import com
.sun
.star
.frame
.XComponentLoader
;
33 import com
.sun
.star
.frame
.XFrame2
;
34 import com
.sun
.star
.frame
.XModel
;
35 import com
.sun
.star
.frame
.XTitle
;
36 import com
.sun
.star
.frame
.XController
;
37 import com
.sun
.star
.lang
.XComponent
;
38 import com
.sun
.star
.lang
.XMultiServiceFactory
;
39 import com
.sun
.star
.uno
.UnoRuntime
;
40 import com
.sun
.star
.util
.URLTransformer
;
41 import com
.sun
.star
.util
.XURLTransformer
;
42 import com
.sun
.star
.util
.URL
;
43 import com
.sun
.star
.frame
.XDispatchProvider
;
44 import com
.sun
.star
.frame
.XDispatch
;
46 /** @short Check the interface XTitle
48 @descr These tests check in various cases the correctness of
49 the title in the ui window.
52 public class CheckXTitle
56 private static final String DOCUMENT_TITLE
= "documentTitle";
57 private static final String UNO_URL_FOR_PRINT_PREVIEW
= ".uno:PrintPreview";
58 private static final String UNO_URL_FOR_CLOSING_PRINT_PREVIEW
= ".uno:ClosePreview";
59 private static final String UNO_URL_FOR_CLOSING_DOC
= ".uno:CloseWin";
63 /** will be set to xDesktop OR xFrame. */
64 private XComponentLoader m_xLoader
= null;
66 /** provides uno service manager */
67 private XMultiServiceFactory m_xMSF
= null;
69 /** used for parsing uno query URLs */
70 private XURLTransformer m_xParser
= null;
73 /** @short Create the environment for following tests.
75 @descr Use either a component loader from desktop or
78 @Before public void before() throws Exception
80 // get uno service manager from global test environment
81 /* points to the global uno service manager. */
84 // define default loader
85 m_xLoader
= Desktop
.create(connection
.getComponentContext());
88 m_xParser
= URLTransformer
.create(connection
.getComponentContext());
91 /** @short close the environment.
93 @After public void after() throws Exception
100 private URL
parseURL(String unoURL
)
102 URL
[] aParseURL
= new URL
[] { new URL() };
103 aParseURL
[0].Complete
= unoURL
;
104 m_xParser
.parseStrict(aParseURL
);
108 private void waitUntilDispatcherAvailable(XModel xModel
, String unoURL
)
109 throws InterruptedException
111 utils
.waitForEventIdle(m_xMSF
);
113 // On Windows, some events such as focus changes are handled
114 // asynchronously using PostMessage (Windows message queue)
115 // so the previous UI transition may still not have completed yet
116 // even though we called waitForEventIdle.
118 // Loop a few times until the desired dispatcher is available, which
119 // is a better indication that the UI transition has completed.
121 XDispatchProvider xDisProv
;
122 XDispatch xDispatcher
= null;
123 URL parsed_url
= parseURL(unoURL
);
125 for (int ntries
= 1; ntries
< 5; ++ntries
) {
126 xDisProv
= UnoRuntime
.queryInterface(
127 XDispatchProvider
.class, xModel
.getCurrentController() );
128 xDispatcher
= xDisProv
.queryDispatch(parsed_url
, "", 0);
129 if (xDispatcher
!= null)
133 assertNotNull("Can not obtain dispatcher for query: " + unoURL
, xDispatcher
);
137 // prepare a uno URL query and dispatch it
138 private void prepareQueryAndDispatch(XDispatchProvider xDisProv
, String unoURL
)
140 XDispatch xDispatcher
= null;
141 URL parsed_url
= parseURL(unoURL
);
143 xDispatcher
= xDisProv
.queryDispatch(parsed_url
, "", 0);
144 assertNotNull("Can not obtain dispatcher for query: " + unoURL
, xDispatcher
);
145 xDispatcher
.dispatch(parsed_url
, null);
148 /** @short checks the numbers displayed in the title
150 @descr cycles through default view and print preview
151 and asserts that the title doesn't change.
153 disabled until the waitUntilDispatcherAvailable can be replaced
156 public void checkTitleNumbers() throws Exception
158 PropertyValue
[] lArgs
= new PropertyValue
[1];
160 lArgs
[0] = new PropertyValue();
161 lArgs
[0].Name
= "Hidden";
162 lArgs
[0].Value
= Boolean
.FALSE
;
165 XComponent xDoc
=null;
166 xDoc
= m_xLoader
.loadComponentFromURL("private:factory/swriter", "_blank", 0, lArgs
);
167 assertNotNull("Could not load temporary document", xDoc
);
169 XModel xModel
= UnoRuntime
.queryInterface( XModel
.class, xDoc
);
170 XController xController
= UnoRuntime
.queryInterface( XController
.class, xModel
.getCurrentController() );
171 XTitle xTitle
= UnoRuntime
.queryInterface( XTitle
.class, xModel
.getCurrentController().getFrame() );
172 XDispatchProvider xDisProv
= null;
174 // get window title with ui in default mode
175 String defaultTitle
= xTitle
.getTitle();
177 xDisProv
= UnoRuntime
.queryInterface( XDispatchProvider
.class, xModel
.getCurrentController() );
178 prepareQueryAndDispatch( xDisProv
, UNO_URL_FOR_PRINT_PREVIEW
);
179 waitUntilDispatcherAvailable( xModel
, UNO_URL_FOR_CLOSING_PRINT_PREVIEW
);
181 // get window title with ui in print preview mode
182 String printPreviewTitle
= xTitle
.getTitle();
183 assertEquals("Title mismatch between default view window title and print preview window title",
184 defaultTitle
, printPreviewTitle
);
186 xDisProv
= UnoRuntime
.queryInterface( XDispatchProvider
.class, xModel
.getCurrentController() );
187 prepareQueryAndDispatch( xDisProv
, UNO_URL_FOR_CLOSING_PRINT_PREVIEW
);
188 waitUntilDispatcherAvailable( xModel
, UNO_URL_FOR_CLOSING_DOC
);
190 //get window title with ui back in default mode
191 String printPreviewClosedTitle
= xTitle
.getTitle();
192 assertEquals("Title mismatch between default view window title and title after switching from print preview to default view window" ,defaultTitle
, printPreviewClosedTitle
);
194 xDisProv
= UnoRuntime
.queryInterface( XDispatchProvider
.class, xModel
.getCurrentController() );
195 prepareQueryAndDispatch( xDisProv
, UNO_URL_FOR_CLOSING_DOC
);
201 /** checks the if SuggestedSaveAsName is displayed in the title */
203 public void checkTitleSuggestedFileName() throws Exception
205 PropertyValue
[] lArgs
= new PropertyValue
[2];
207 lArgs
[0] = new PropertyValue();
208 lArgs
[0].Name
= "Hidden";
209 lArgs
[0].Value
= Boolean
.FALSE
;
210 lArgs
[1] = new PropertyValue();
211 lArgs
[1].Name
= "SuggestedSaveAsName";
212 lArgs
[1].Value
= "suggestme.odt";
215 XComponent xDoc
= m_xLoader
.loadComponentFromURL("private:factory/swriter", "_blank", 0, lArgs
);
216 assertNotNull("Could not load temporary document", xDoc
);
218 XModel xModel
= UnoRuntime
.queryInterface( XModel
.class, xDoc
);
219 XTitle xTitle
= UnoRuntime
.queryInterface( XTitle
.class, xModel
.getCurrentController().getFrame() );
221 String title
= xTitle
.getTitle();
222 assertTrue(title
.startsWith("suggestme.odt"));
224 XDispatchProvider xDisProv
= UnoRuntime
.queryInterface( XDispatchProvider
.class, xModel
.getCurrentController() );
225 prepareQueryAndDispatch( xDisProv
, UNO_URL_FOR_CLOSING_DOC
);
228 /** @short sets frame title and checks for infinite recursion
230 @descr sets frame title. then cycles through default and
231 print preview. then closes the window and checks
232 for infinite recursion.
234 disabled until the waitUntilDispatcherAvailable can be replaced
237 public void setTitleAndCheck() throws Exception
239 PropertyValue
[] lArgs
= new PropertyValue
[1];
241 lArgs
[0] = new PropertyValue();
242 lArgs
[0].Name
= "Hidden";
243 lArgs
[0].Value
= Boolean
.FALSE
;
246 XComponent xDoc
= null;
247 xDoc
= m_xLoader
.loadComponentFromURL("private:factory/swriter", "_blank", 0, lArgs
);
248 assertNotNull("Could not create office document", xDoc
);
249 XModel xModel
= UnoRuntime
.queryInterface( XModel
.class, xDoc
);
250 XFrame2 xFrame
= UnoRuntime
.queryInterface( XFrame2
.class, xModel
.getCurrentController().getFrame() );
251 XDispatchProvider xDisProv
= null;
253 xFrame
.setTitle(DOCUMENT_TITLE
);
255 // switch to print preview mode
256 xDisProv
= UnoRuntime
.queryInterface( XDispatchProvider
.class, xModel
.getCurrentController() );
257 prepareQueryAndDispatch( xDisProv
, UNO_URL_FOR_PRINT_PREVIEW
);
258 waitUntilDispatcherAvailable( xModel
, UNO_URL_FOR_CLOSING_PRINT_PREVIEW
);
260 // switch back to default mode
261 xDisProv
= UnoRuntime
.queryInterface( XDispatchProvider
.class, xModel
.getCurrentController() );
262 prepareQueryAndDispatch( xDisProv
, UNO_URL_FOR_CLOSING_PRINT_PREVIEW
);
263 waitUntilDispatcherAvailable( xModel
, UNO_URL_FOR_CLOSING_DOC
);
266 xDisProv
= UnoRuntime
.queryInterface( XDispatchProvider
.class, xModel
.getCurrentController() );
268 prepareQueryAndDispatch( xDisProv
, UNO_URL_FOR_CLOSING_DOC
);
269 } catch( Exception e
) {
277 /** @short checks creation of new empty document with readonly set to true
279 @descr creation of a new document with readonly property set
280 to true. this should not fail even if as such it can
281 be seen as a slightly silly thing to do. But existing
282 3rd-party code depends on it to work.
284 @Test public void checkLoadingWithReadOnly()
286 PropertyValue
[] lArgs
= new PropertyValue
[2];
288 lArgs
[0] = new PropertyValue();
289 lArgs
[0].Name
= "Hidden";
290 lArgs
[0].Value
= Boolean
.FALSE
;
291 lArgs
[1] = new PropertyValue();
292 lArgs
[1].Name
= "ReadOnly";
293 lArgs
[1].Value
= Boolean
.TRUE
;
296 XComponent xDoc
= null;
298 xDoc
= m_xLoader
.loadComponentFromURL("private:factory/swriter", "_default", 0, lArgs
);
299 } catch (Exception e
) {
301 assertNotNull("Creating a new document read with ReadOnly property true should work (even if slightly silly)", xDoc
);
304 private XMultiServiceFactory
getMSF()
306 return UnoRuntime
.queryInterface(XMultiServiceFactory
.class, connection
.getComponentContext().getServiceManager());
309 // setup and close connections
311 public static void setUpConnection() throws Exception
313 System
.out
.println("setUpConnection()");
318 public static void tearDownConnection()
319 throws InterruptedException
, com
.sun
.star
.uno
.Exception
321 System
.out
.println("tearDownConnection()");
322 connection
.tearDown();
324 private static final OfficeConnection connection
= new OfficeConnection();