Branch libreoffice-5-0-4
[LibreOffice.git] / framework / qa / complex / dispatches / checkdispatchapi.java
blobc227be25e9bb71697cf5b08f533d3c710cb9182a
1 /*
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 .
18 package complex.dispatches;
20 import com.sun.star.beans.PropertyValue;
21 import com.sun.star.frame.DispatchInformation;
22 import com.sun.star.frame.XComponentLoader;
23 import com.sun.star.frame.XDispatchInformationProvider;
24 import com.sun.star.frame.XDispatchProviderInterception;
25 import com.sun.star.frame.XDispatchProviderInterceptor;
26 import com.sun.star.frame.XFrame;
27 import com.sun.star.lang.XComponent;
28 import com.sun.star.lang.XMultiServiceFactory;
29 import com.sun.star.uno.UnoRuntime;
30 import com.sun.star.util.XCloseable;
31 import java.util.HashMap;
33 import org.junit.After;
34 import org.junit.AfterClass;
35 import org.junit.Before;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.openoffice.test.OfficeConnection;
39 import static org.junit.Assert.*;
43 /** @short Check the interface XDispatchInformationProvider
45 @descr Because there exists more than one implementation of a dispatch
46 object, we have to test all these implementations ...
48 public class checkdispatchapi
51 // some const
54 // member
55 /** points to the global uno service manager. */
56 private XMultiServiceFactory m_xMSF = null;
57 private connectivity.tools.HsqlDatabase db;
58 /** can be used to create new test frames. */
59 private XFrame m_xDesktop = null;
60 /** provides XDispatchInformationProvider interface. */
61 private XFrame m_xFrame = null;
64 // test environment
66 /** @short Create the environment for following tests.
68 @descr create an empty test frame, where we can load
69 different components inside.
71 @Before public void before()
73 try
75 // get uno service manager from global test environment
76 m_xMSF = getMSF();
78 db = new connectivity.tools.HsqlDatabase(m_xMSF);
80 // create desktop
81 m_xDesktop = UnoRuntime.queryInterface(XFrame.class, m_xMSF.createInstance("com.sun.star.frame.Desktop"));
83 m_xFrame = impl_createNewFrame();
85 catch (java.lang.Throwable ex)
87 fail("Can't initialize test environment.");
92 /** @short close the environment.
94 @After public void after()
96 db.close();
97 impl_closeFrame(m_xFrame);
98 m_xFrame = null;
102 @Test public void checkDispatchInfoOfWriter()
104 impl_checkDispatchInfoOfXXX("private:factory/swriter");
108 @Test public void checkDispatchInfoOfCalc()
110 impl_checkDispatchInfoOfXXX("private:factory/scalc");
114 @Test public void checkDispatchInfoOfDraw()
116 impl_checkDispatchInfoOfXXX("private:factory/sdraw");
120 @Test public void checkDispatchInfoOfImpress()
122 impl_checkDispatchInfoOfXXX("private:factory/simpress");
126 @Test public void checkDispatchInfoOfChart()
128 impl_checkDispatchInfoOfXXX("private:factory/schart");
132 @Test public void checkDispatchInfoOfMath()
134 impl_checkDispatchInfoOfXXX("private:factory/smath");
138 @Test public void checkDispatchInfoOfDataBase()
140 impl_checkDispatchInfoOfXXX("private:factory/sdatabase");
144 @Test public void checkDispatchInfoOfBibliography()
146 impl_checkDispatchInfoOfXXX(".component:Bibliography/View1");
150 @Test public void checkDispatchInfoOfQueryDesign()
152 callDatabaseDispatch(".component:DB/QueryDesign");
156 @Test public void checkDispatchInfoOfTableDesign()
158 callDatabaseDispatch(".component:DB/TableDesign");
162 @Test public void checkDispatchInfoOfFormGridView()
164 impl_checkDispatchInfoOfXXX(".component:DB/FormGridView");
168 @Test public void checkDispatchInfoOfDataSourceBrowser()
170 impl_checkDispatchInfoOfXXX(".component:DB/DataSourceBrowser");
174 @Test public void checkDispatchInfoOfRelationDesign()
176 callDatabaseDispatch(".component:DB/RelationDesign");
180 private void callDatabaseDispatch(String url)
184 final PropertyValue args = new PropertyValue();
185 args.Name = "ActiveConnection";
186 args.Value = db.defaultConnection();
188 XFrame xFrame = impl_createNewFrame();
190 impl_loadIntoFrame(xFrame, url, new PropertyValue[]
192 args
194 impl_checkDispatchInfo(xFrame);
195 impl_closeFrame(xFrame);
197 catch (java.lang.Exception e)
203 @Test public void checkDispatchInfoOfBasic()
205 Object aComponent = impl_createUNOComponent("com.sun.star.script.BasicIDE");
206 impl_checkDispatchInfo(aComponent);
210 @Test public void checkDispatchInfoOfStartModule()
212 Object aComponent = impl_createUNOComponent("com.sun.star.frame.StartModule");
213 impl_checkDispatchInfo(aComponent);
217 public void checkInterceptorLifeTime()
219 // Note: It's important for the following test, that aInterceptor will be hold alive by the uno reference
220 // xInterceptor. Otherwhise we can't check some internal states of aInterceptor at the end of this method, because
221 // it was already killed .-)
223 Interceptor aInterceptor = new Interceptor();
224 XDispatchProviderInterceptor xInterceptor = UnoRuntime.queryInterface(XDispatchProviderInterceptor.class, aInterceptor);
226 XFrame xFrame = impl_createNewFrame();
227 XDispatchProviderInterception xInterception = UnoRuntime.queryInterface(XDispatchProviderInterception.class, xFrame);
229 xInterception.registerDispatchProviderInterceptor(xInterceptor);
230 impl_closeFrame(xFrame);
232 int nRegCount = aInterceptor.getRegistrationCount();
233 boolean bIsRegistered = aInterceptor.isRegistered();
235 System.out.println("registration count = " + nRegCount);
236 System.out.println("is registered ? = " + bIsRegistered);
238 if (nRegCount < 1)
240 fail("Interceptor was never registered.");
243 if (bIsRegistered)
245 fail("Interceptor was not deregistered automatically on closing the corresponding frame.");
248 System.out.println("Destruction of interception chain works as designed .-)");
252 public void checkInterception()
254 String[] lDisabledURLs = new String[] { ".uno:Open" };
256 System.out.println("create and initialize interceptor ...");
257 Interceptor aInterceptor = new Interceptor();
258 aInterceptor.setURLs4URLs4Blocking(lDisabledURLs);
260 XDispatchProviderInterceptor xInterceptor = UnoRuntime.queryInterface(XDispatchProviderInterceptor.class, aInterceptor);
262 System.out.println("create and initialize frame ...");
263 XFrame xFrame = impl_createNewFrame();
264 impl_loadIntoFrame(xFrame, "private:factory/swriter", null);
266 XDispatchProviderInterception xInterception = UnoRuntime.queryInterface(XDispatchProviderInterception.class, xFrame);
268 System.out.println("register interceptor ...");
269 xInterception.registerDispatchProviderInterceptor(xInterceptor);
271 System.out.println("deregister interceptor ...");
272 xInterception.releaseDispatchProviderInterceptor(xInterceptor);
276 private void impl_checkDispatchInfoOfXXX(String sXXX)
278 XFrame xFrame = impl_createNewFrame();
279 impl_loadIntoFrame(xFrame, sXXX, null);
280 impl_checkDispatchInfo(xFrame);
281 impl_closeFrame(xFrame);
285 /** @short load an URL into the current test frame.
287 private void impl_loadIntoFrame(XFrame xFrame, String sURL, PropertyValue args[])
289 XComponentLoader xLoader = UnoRuntime.queryInterface(XComponentLoader.class, xFrame);
290 if (xLoader == null)
292 fail("Frame does not provide required interface XComponentLoader.");
295 XComponent xDoc = null;
298 xDoc = xLoader.loadComponentFromURL(sURL, "_self", 0, args);
300 catch (java.lang.Throwable ex)
302 xDoc = null;
305 if (xDoc == null)
307 fail("Could not load \"" + sURL + "\".");
312 /** @short create an uno implementation directly.
314 private Object impl_createUNOComponent(String sName)
316 Object aComponent = null;
319 aComponent = m_xMSF.createInstance(sName);
321 catch (java.lang.Throwable ex)
323 aComponent = null;
326 if (aComponent == null)
328 fail("Could not create UNO component \"" + sName + "\".");
330 return aComponent;
334 /** @short check the interface XDispatchInformationProvider
335 at the specified component.
337 private void impl_checkDispatchInfo(Object aComponent)
339 XDispatchInformationProvider xInfoProvider = UnoRuntime.queryInterface(XDispatchInformationProvider.class, aComponent);
340 if (xInfoProvider == null)
342 // Warning
343 System.out.println("Warning:\tComponent does not provide the [optional!] interface XDispatchInformationProvider.");
344 return;
349 short[] lGroups = xInfoProvider.getSupportedCommandGroups();
350 int c1 = lGroups.length;
351 int i1 = 0;
352 for (i1 = 0; i1 < c1; ++i1)
354 short nGroup = lGroups[i1];
355 DispatchInformation[] lInfos = xInfoProvider.getConfigurableDispatchInformation(nGroup);
356 int c2 = lInfos.length;
357 int i2 = 0;
359 // check for empty lists
360 // Warning
361 if (lInfos.length < 1)
363 System.out.println("Warning:\tCould not get any DispatchInformation for group [" + nGroup + "].");
366 // check for duplicates (and by the way, if the info item match the requested group)
367 HashMap<String, String> aCheckMap = new HashMap<String, String>(c2);
368 for (i2 = 0; i2 < c2; ++i2)
370 DispatchInformation aInfo = lInfos[i2];
371 if (aInfo.GroupId != nGroup)
373 // Error
374 fail("At least one DispatchInformation item does not match the requested group.\n\trequested group=[" + nGroup
375 + "] returned groupd=[" + aInfo.GroupId + "] command=\"" + aInfo.Command + "\""); // true => dont break this test
376 continue;
379 if (aCheckMap.containsKey(aInfo.Command))
381 // Error
382 fail("Found a duplicate item: group=[" + aInfo.GroupId + "] command=\"" + aInfo.Command + "\""); // true => dont break this test
383 continue;
386 aCheckMap.put(aInfo.Command, aInfo.Command);
387 System.out.println("\t[" + aInfo.GroupId + "] \"" + aInfo.Command + "\"");
391 catch (java.lang.Throwable ex)
393 fail("Exception caught during using XDispatchInformationProvider.");
398 private synchronized XFrame impl_createNewFrame()
400 XFrame xFrame = null;
404 xFrame = m_xDesktop.findFrame("_blank", 0);
405 xFrame.getContainerWindow().setVisible(true);
407 catch (java.lang.Throwable ex)
409 fail("Could not create the frame instance.");
412 return xFrame;
416 private synchronized void impl_closeFrame(XFrame xFrame)
418 XCloseable xClose = UnoRuntime.queryInterface(XCloseable.class, xFrame);
421 xClose.close(false);
423 catch (com.sun.star.util.CloseVetoException exVeto)
425 fail("Test frame couldn't be closed successfully.");
429 private XMultiServiceFactory getMSF()
431 return UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
434 // setup and close connections
435 @BeforeClass
436 public static void setUpConnection() throws Exception
438 System.out.println("setUpConnection()");
439 connection.setUp();
442 @AfterClass
443 public static void tearDownConnection()
444 throws InterruptedException, com.sun.star.uno.Exception
446 System.out.println("tearDownConnection()");
447 connection.tearDown();
449 private static final OfficeConnection connection = new OfficeConnection();