Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / framework / qa / complex / dispatches / checkdispatchapi.java
blob851b467daeb7429f1686369f67f6f5d96f998c23
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package complex.dispatches;
21 import com.sun.star.beans.PropertyValue;
22 import com.sun.star.frame.DispatchInformation;
23 import com.sun.star.frame.XComponentLoader;
24 import com.sun.star.frame.XDispatchInformationProvider;
25 import com.sun.star.frame.XDispatchProviderInterception;
26 import com.sun.star.frame.XDispatchProviderInterceptor;
27 import com.sun.star.frame.XFrame;
28 import com.sun.star.lang.XComponent;
29 import com.sun.star.lang.XMultiServiceFactory;
30 import com.sun.star.uno.UnoRuntime;
31 import com.sun.star.util.XCloseable;
32 import com.sun.star.awt.XReschedule;
33 import com.sun.star.awt.XToolkitExperimental;
34 import java.util.HashMap;
36 import org.junit.After;
37 import org.junit.AfterClass;
38 import org.junit.Before;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41 import org.openoffice.test.OfficeConnection;
42 import static org.junit.Assert.*;
44 /** @short Check the interface XDispatchInformationProvider
46 @descr Because there exists more than one implementation of a dispatch
47 object, we have to test all these implementations ...
49 public class checkdispatchapi
51 // member
52 /** points to the global uno service manager. */
53 private XMultiServiceFactory m_xMSF = null;
55 // private connectivity.tools.HsqlDatabase db;
57 /** can be used to create new test frames. */
58 private XFrame m_xDesktop = null;
59 /** provides XDispatchInformationProvider interface. */
60 private XFrame m_xFrame = null;
63 // test environment
65 /** @short Create the environment for following tests.
67 @descr create an empty test frame, where we can load
68 different components inside.
70 @Before public void before() throws Exception
72 // get uno service manager from global test environment
73 m_xMSF = getMSF();
75 // db = new connectivity.tools.HsqlDatabase(m_xMSF);
77 // create desktop
78 m_xDesktop = UnoRuntime.queryInterface(XFrame.class, m_xMSF.createInstance("com.sun.star.frame.Desktop"));
80 m_xFrame = impl_createNewFrame();
83 /** @short close the environment.
85 @After public void after() throws Exception
87 // db.close();
88 impl_closeFrame(m_xFrame);
89 m_xFrame = null;
92 @Test public void checkDispatchInfoOfWriter() throws Exception
94 impl_checkDispatchInfoOfXXX("private:factory/swriter");
97 @Test public void checkDispatchInfoOfCalc() throws Exception
99 impl_checkDispatchInfoOfXXX("private:factory/scalc");
102 @Test public void checkDispatchInfoOfDraw() throws Exception
104 impl_checkDispatchInfoOfXXX("private:factory/sdraw");
107 @Test public void checkDispatchInfoOfImpress() throws Exception
109 impl_checkDispatchInfoOfXXX("private:factory/simpress");
112 @Test public void checkDispatchInfoOfChart() throws Exception
114 impl_checkDispatchInfoOfXXX("private:factory/schart");
117 @Test public void checkDispatchInfoOfMath() throws Exception
119 impl_checkDispatchInfoOfXXX("private:factory/smath");
122 @Test public void checkDispatchInfoOfDataBase() throws Exception
124 // impl_checkDispatchInfoOfXXX("private:factory/sdatabase");
127 @Test public void checkDispatchInfoOfBibliography() throws Exception
129 // impl_checkDispatchInfoOfXXX(".component:Bibliography/View1");
132 @Test public void checkDispatchInfoOfQueryDesign()
134 // callDatabaseDispatch(".component:DB/QueryDesign");
137 @Test public void checkDispatchInfoOfTableDesign() throws Exception
139 // callDatabaseDispatch(".component:DB/TableDesign");
142 @Test public void checkDispatchInfoOfFormGridView() throws Exception
144 // impl_checkDispatchInfoOfXXX(".component:DB/FormGridView");
147 @Test public void checkDispatchInfoOfDataSourceBrowser() throws Exception
149 // impl_checkDispatchInfoOfXXX(".component:DB/DataSourceBrowser");
152 @Test public void checkDispatchInfoOfRelationDesign()
154 // callDatabaseDispatch(".component:DB/RelationDesign");
157 private void callDatabaseDispatch(String url)
159 /* disabled along with all db related tests for now.
163 final PropertyValue args = new PropertyValue();
164 args.Name = "ActiveConnection";
165 args.Value = db.defaultConnection();
167 XFrame xFrame = impl_createNewFrame();
169 impl_loadIntoFrame(xFrame, url, new PropertyValue[]
171 args
173 impl_checkDispatchInfo(xFrame);
174 impl_closeFrame(xFrame);
176 catch (java.lang.Exception e)
178 } */
181 @Test public void checkDispatchInfoOfBasic() throws Exception
183 Object aComponent = impl_createUNOComponent("com.sun.star.script.BasicIDE");
184 impl_checkDispatchInfo(aComponent);
187 @Test public void checkDispatchInfoOfStartModule() throws Exception
189 Object aComponent = impl_createUNOComponent("com.sun.star.frame.StartModule");
190 impl_checkDispatchInfo(aComponent);
193 @Test public void checkInterceptorLifeTime() throws Exception
195 // Note: It's important for the following test, that aInterceptor will be hold alive by the uno reference
196 // xInterceptor. Otherwise we can't check some internal states of aInterceptor at the end of this method, because
197 // it was already killed .-)
199 Interceptor aInterceptor = new Interceptor(m_xMSF);
200 XDispatchProviderInterceptor xInterceptor = UnoRuntime.queryInterface(XDispatchProviderInterceptor.class, aInterceptor);
202 XFrame xFrame = impl_createNewFrame();
203 XDispatchProviderInterception xInterception = UnoRuntime.queryInterface(XDispatchProviderInterception.class, xFrame);
205 xInterception.registerDispatchProviderInterceptor(xInterceptor);
206 impl_closeFrame(xFrame);
208 int nRegCount = aInterceptor.getRegistrationCount();
209 boolean bIsRegistered = aInterceptor.isRegistered();
211 System.out.println("registration count = " + nRegCount);
212 System.out.println("is registered ? = " + bIsRegistered);
214 if (nRegCount < 1)
216 fail("Interceptor was never registered.");
219 if (bIsRegistered)
221 fail("Interceptor was not deregistered automatically on closing the corresponding frame.");
224 System.out.println("Destruction of interception chain works as designed .-)");
227 @Test public void checkInterception() throws Exception
229 String[] lDisabledURLs = new String[] { ".uno:Open" };
231 System.out.println("create and initialize interceptor ...");
232 Interceptor aInterceptor = new Interceptor(m_xMSF);
233 aInterceptor.setURLs4URLs4Blocking(lDisabledURLs);
235 XDispatchProviderInterceptor xInterceptor = UnoRuntime.queryInterface(XDispatchProviderInterceptor.class, aInterceptor);
237 System.out.println("create and initialize frame ...");
238 XFrame xFrame = impl_createNewFrame();
240 XDispatchProviderInterception xInterception = UnoRuntime.queryInterface(XDispatchProviderInterception.class, xFrame);
241 System.out.println("register interceptor ...");
242 xInterception.registerDispatchProviderInterceptor(xInterceptor);
244 impl_loadIntoFrame(xFrame, "private:factory/swriter", null);
246 // Framework dispatcher update is on a ~50ms wait.
247 Thread.sleep(100);
249 XReschedule m_xReschedule = UnoRuntime.queryInterface(
250 XReschedule.class, m_xMSF.createInstance("com.sun.star.awt.Toolkit"));
251 // queryDispatch for toolbars etc. happens asynchronously.
252 System.out.println("process deferred events ...");
253 m_xReschedule.reschedule();
254 XToolkitExperimental m_xIdles = UnoRuntime.queryInterface(
255 XToolkitExperimental.class, m_xReschedule);
256 m_xIdles.processEventsToIdle();
258 System.out.println("deregister interceptor ...");
259 xInterception.releaseDispatchProviderInterceptor(xInterceptor);
262 private void impl_checkDispatchInfoOfXXX(String sXXX) throws Exception
264 XFrame xFrame = impl_createNewFrame();
265 impl_loadIntoFrame(xFrame, sXXX, null);
266 impl_checkDispatchInfo(xFrame);
267 impl_closeFrame(xFrame);
270 /** @short load a URL into the current test frame.
272 private void impl_loadIntoFrame(XFrame xFrame, String sURL, PropertyValue args[]) throws Exception
274 XComponentLoader xLoader = UnoRuntime.queryInterface(XComponentLoader.class, xFrame);
275 if (xLoader == null)
277 fail("Frame does not provide required interface XComponentLoader.");
280 XComponent xDoc = xLoader.loadComponentFromURL(sURL, "_self", 0, args);
282 if (xDoc == null)
284 fail("Could not load \"" + sURL + "\".");
288 /** @short create a uno implementation directly.
290 private Object impl_createUNOComponent(String sName)
292 Object aComponent = null;
295 aComponent = m_xMSF.createInstance(sName);
297 catch (java.lang.Throwable ex)
299 aComponent = null;
302 if (aComponent == null)
304 fail("Could not create UNO component \"" + sName + "\".");
306 return aComponent;
309 /** @short check the interface XDispatchInformationProvider
310 at the specified component.
312 private void impl_checkDispatchInfo(Object aComponent) throws Exception
314 XDispatchInformationProvider xInfoProvider = UnoRuntime.queryInterface(XDispatchInformationProvider.class, aComponent);
315 if (xInfoProvider == null)
317 // Warning
318 System.out.println("Warning:\tComponent does not provide the [optional!] interface XDispatchInformationProvider.");
319 return;
322 short[] lGroups = xInfoProvider.getSupportedCommandGroups();
323 int c1 = lGroups.length;
324 int i1 = 0;
325 for (i1 = 0; i1 < c1; ++i1)
327 short nGroup = lGroups[i1];
328 DispatchInformation[] lInfos = xInfoProvider.getConfigurableDispatchInformation(nGroup);
329 int c2 = lInfos.length;
330 int i2 = 0;
332 // check for empty lists
333 // Warning
334 if (lInfos.length < 1)
336 System.out.println("Warning:\tCould not get any DispatchInformation for group [" + nGroup + "].");
339 // check for duplicates (and by the way, if the info item match the requested group)
340 HashMap<String, String> aCheckMap = new HashMap<String, String>(c2);
341 for (i2 = 0; i2 < c2; ++i2)
343 DispatchInformation aInfo = lInfos[i2];
344 if (aInfo.GroupId != nGroup)
346 // Error
347 fail("At least one DispatchInformation item does not match the requested group.\n\trequested group=[" + nGroup
348 + "] returned group=[" + aInfo.GroupId + "] command=\"" + aInfo.Command + "\""); // true => don't break this test
349 continue;
352 if (aCheckMap.containsKey(aInfo.Command))
354 // Error
355 fail("Found a duplicate item: group=[" + aInfo.GroupId + "] command=\"" + aInfo.Command + "\""); // true => don't break this test
356 continue;
359 aCheckMap.put(aInfo.Command, aInfo.Command);
360 System.out.println("\t[" + aInfo.GroupId + "] \"" + aInfo.Command + "\"");
365 private synchronized XFrame impl_createNewFrame()
367 XFrame xFrame = m_xDesktop.findFrame("_blank", 0);
368 xFrame.getContainerWindow().setVisible(true);
369 return xFrame;
372 private synchronized void impl_closeFrame(XFrame xFrame) throws Exception
374 XCloseable xClose = UnoRuntime.queryInterface(XCloseable.class, xFrame);
375 xClose.close(false);
378 private XMultiServiceFactory getMSF()
380 return UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
383 // setup and close connections
384 @BeforeClass
385 public static void setUpConnection() throws Exception
387 System.out.println("setUpConnection()");
388 connection.setUp();
391 @AfterClass
392 public static void tearDownConnection()
393 throws InterruptedException, com.sun.star.uno.Exception
395 System.out.println("tearDownConnection()");
396 connection.tearDown();
398 private static final OfficeConnection connection = new OfficeConnection();
401 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */