| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* This file is part of the LibreOffice project. |
| 4 |
* |
| 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/. |
| 8 |
* |
| 9 |
* This file incorporates work covered by the following license notice: |
| 10 |
* |
| 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 . |
| 18 |
*/ |
| 19 |
package complex.dispatches; |
| 20 |
|
| 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; |
| 35 |
|
| 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.*; |
| 43 |
|
| 44 |
/** @short Check the interface XDispatchInformationProvider |
| 45 |
|
| 46 |
@descr Because there exists more than one implementation of a dispatch |
| 47 |
object, we have to test all these implementations ... |
| 48 |
*/ |
| 49 |
public class checkdispatchapi |
| 50 |
{ |
| 51 |
// member |
| 52 |
/** points to the global uno service manager. */ |
| 53 |
private XMultiServiceFactory m_xMSF = null; |
| 54 |
|
| 55 |
// private connectivity.tools.HsqlDatabase db; |
| 56 |
|
| 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; |
| 61 |
|
| 62 |
|
| 63 |
// test environment |
| 64 |
|
| 65 |
/** @short Create the environment for following tests. |
| 66 |
|
| 67 |
@descr create an empty test frame, where we can load |
| 68 |
different components inside. |
| 69 |
*/ |
| 70 |
@Before public void before() throws Exception |
| 71 |
{ |
| 72 |
// get uno service manager from global test environment |
| 73 |
m_xMSF = getMSF(); |
| 74 |
|
| 75 |
// db = new connectivity.tools.HsqlDatabase(m_xMSF); |
| 76 |
|
| 77 |
// create desktop |
| 78 |
m_xDesktop = UnoRuntime.queryInterface(XFrame.class, m_xMSF.createInstance("com.sun.star.frame.Desktop")); |
| 79 |
|
| 80 |
m_xFrame = impl_createNewFrame(); |
| 81 |
} |
| 82 |
|
| 83 |
/** @short close the environment. |
| 84 |
*/ |
| 85 |
@After public void after() throws Exception |
| 86 |
{ |
| 87 |
// db.close(); |
| 88 |
impl_closeFrame(m_xFrame); |
| 89 |
m_xFrame = null; |
| 90 |
} |
| 91 |
|
| 92 |
@Test public void checkDispatchInfoOfWriter() throws Exception |
| 93 |
{ |
| 94 |
impl_checkDispatchInfoOfXXX("private:factory/swriter"); |
| 95 |
} |
| 96 |
|
| 97 |
@Test public void checkDispatchInfoOfCalc() throws Exception |
| 98 |
{ |
| 99 |
impl_checkDispatchInfoOfXXX("private:factory/scalc"); |
| 100 |
} |
| 101 |
|
| 102 |
@Test public void checkDispatchInfoOfDraw() throws Exception |
| 103 |
{ |
| 104 |
impl_checkDispatchInfoOfXXX("private:factory/sdraw"); |
| 105 |
} |
| 106 |
|
| 107 |
@Test public void checkDispatchInfoOfImpress() throws Exception |
| 108 |
{ |
| 109 |
impl_checkDispatchInfoOfXXX("private:factory/simpress"); |
| 110 |
} |
| 111 |
|
| 112 |
@Test public void checkDispatchInfoOfChart() throws Exception |
| 113 |
{ |
| 114 |
impl_checkDispatchInfoOfXXX("private:factory/schart"); |
| 115 |
} |
| 116 |
|
| 117 |
@Test public void checkDispatchInfoOfMath() throws Exception |
| 118 |
{ |
| 119 |
impl_checkDispatchInfoOfXXX("private:factory/smath"); |
| 120 |
} |
| 121 |
|
| 122 |
@Test public void checkDispatchInfoOfDataBase() throws Exception |
| 123 |
{ |
| 124 |
// impl_checkDispatchInfoOfXXX("private:factory/sdatabase"); |
| 125 |
} |
| 126 |
|
| 127 |
@Test public void checkDispatchInfoOfBibliography() throws Exception |
| 128 |
{ |
| 129 |
// impl_checkDispatchInfoOfXXX(".component:Bibliography/View1"); |
| 130 |
} |
| 131 |
|
| 132 |
@Test public void checkDispatchInfoOfQueryDesign() |
| 133 |
{ |
| 134 |
// callDatabaseDispatch(".component:DB/QueryDesign"); |
| 135 |
} |
| 136 |
|
| 137 |
@Test public void checkDispatchInfoOfTableDesign() throws Exception |
| 138 |
{ |
| 139 |
// callDatabaseDispatch(".component:DB/TableDesign"); |
| 140 |
} |
| 141 |
|
| 142 |
@Test public void checkDispatchInfoOfFormGridView() throws Exception |
| 143 |
{ |
| 144 |
// impl_checkDispatchInfoOfXXX(".component:DB/FormGridView"); |
| 145 |
} |
| 146 |
|
| 147 |
@Test public void checkDispatchInfoOfDataSourceBrowser() throws Exception |
| 148 |
{ |
| 149 |
// impl_checkDispatchInfoOfXXX(".component:DB/DataSourceBrowser"); |
| 150 |
} |
| 151 |
|
| 152 |
@Test public void checkDispatchInfoOfRelationDesign() |
| 153 |
{ |
| 154 |
// callDatabaseDispatch(".component:DB/RelationDesign"); |
| 155 |
} |
| 156 |
|
| 157 |
private void callDatabaseDispatch(String url) |
| 158 |
{ |
| 159 |
/* disabled along with all db related tests for now. |
| 160 |
|
| 161 |
try |
| 162 |
{ |
| 163 |
final PropertyValue args = new PropertyValue(); |
| 164 |
args.Name = "ActiveConnection"; |
| 165 |
args.Value = db.defaultConnection(); |
| 166 |
|
| 167 |
XFrame xFrame = impl_createNewFrame(); |
| 168 |
|
| 169 |
impl_loadIntoFrame(xFrame, url, new PropertyValue[] |
| 170 |
{ |
| 171 |
args |
| 172 |
}); |
| 173 |
impl_checkDispatchInfo(xFrame); |
| 174 |
impl_closeFrame(xFrame); |
| 175 |
} |
| 176 |
catch (java.lang.Exception e) |
| 177 |
{ |
| 178 |
} */ |
| 179 |
} |
| 180 |
|
| 181 |
@Test public void checkDispatchInfoOfBasic() throws Exception |
| 182 |
{ |
| 183 |
Object aComponent = impl_createUNOComponent("com.sun.star.script.BasicIDE"); |
| 184 |
impl_checkDispatchInfo(aComponent); |
| 185 |
} |
| 186 |
|
| 187 |
@Test public void checkDispatchInfoOfStartModule() throws Exception |
| 188 |
{ |
| 189 |
Object aComponent = impl_createUNOComponent("com.sun.star.frame.StartModule"); |
| 190 |
impl_checkDispatchInfo(aComponent); |
| 191 |
} |
| 192 |
|
| 193 |
@Test public void checkInterceptorLifeTime() throws Exception |
| 194 |
{ |
| 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 .-) |
| 198 |
|
| 199 |
Interceptor aInterceptor = new Interceptor(m_xMSF); |
| 200 |
XDispatchProviderInterceptor xInterceptor = UnoRuntime.queryInterface(XDispatchProviderInterceptor.class, aInterceptor); |
| 201 |
|
| 202 |
XFrame xFrame = impl_createNewFrame(); |
| 203 |
XDispatchProviderInterception xInterception = UnoRuntime.queryInterface(XDispatchProviderInterception.class, xFrame); |
| 204 |
|
| 205 |
xInterception.registerDispatchProviderInterceptor(xInterceptor); |
| 206 |
impl_closeFrame(xFrame); |
| 207 |
|
| 208 |
int nRegCount = aInterceptor.getRegistrationCount(); |
| 209 |
boolean bIsRegistered = aInterceptor.isRegistered(); |
| 210 |
|
| 211 |
System.out.println("registration count = " + nRegCount); |
| 212 |
System.out.println("is registered ? = " + bIsRegistered); |
| 213 |
|
| 214 |
if (nRegCount < 1) |
| 215 |
{ |
| 216 |
fail("Interceptor was never registered."); |
| 217 |
} |
| 218 |
|
| 219 |
if (bIsRegistered) |
| 220 |
{ |
| 221 |
fail("Interceptor was not deregistered automatically on closing the corresponding frame."); |
| 222 |
} |
| 223 |
|
| 224 |
System.out.println("Destruction of interception chain works as designed .-)"); |
| 225 |
} |
| 226 |
|
| 227 |
@Test public void checkInterception() throws Exception |
| 228 |
{ |
| 229 |
String[] lDisabledURLs = new String[] { ".uno:Open" }; |
| 230 |
|
| 231 |
System.out.println("create and initialize interceptor ..."); |
| 232 |
Interceptor aInterceptor = new Interceptor(m_xMSF); |
| 233 |
aInterceptor.setURLs4URLs4Blocking(lDisabledURLs); |
| 234 |
|
| 235 |
XDispatchProviderInterceptor xInterceptor = UnoRuntime.queryInterface(XDispatchProviderInterceptor.class, aInterceptor); |
| 236 |
|
| 237 |
System.out.println("create and initialize frame ..."); |
| 238 |
XFrame xFrame = impl_createNewFrame(); |
| 239 |
|
| 240 |
XDispatchProviderInterception xInterception = UnoRuntime.queryInterface(XDispatchProviderInterception.class, xFrame); |
| 241 |
System.out.println("register interceptor ..."); |
| 242 |
xInterception.registerDispatchProviderInterceptor(xInterceptor); |
| 243 |
|
| 244 |
impl_loadIntoFrame(xFrame, "private:factory/swriter", null); |
| 245 |
|
| 246 |
// Framework dispatcher update is on a ~50ms wait. |
| 247 |
Thread.sleep(100); |
| 248 |
|
| 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(); |
| 257 |
|
| 258 |
System.out.println("deregister interceptor ..."); |
| 259 |
xInterception.releaseDispatchProviderInterceptor(xInterceptor); |
| 260 |
} |
| 261 |
|
| 262 |
private void impl_checkDispatchInfoOfXXX(String sXXX) throws Exception |
| 263 |
{ |
| 264 |
XFrame xFrame = impl_createNewFrame(); |
| 265 |
impl_loadIntoFrame(xFrame, sXXX, null); |
| 266 |
impl_checkDispatchInfo(xFrame); |
| 267 |
impl_closeFrame(xFrame); |
| 268 |
} |
| 269 |
|
| 270 |
/** @short load a URL into the current test frame. |
| 271 |
*/ |
| 272 |
private void impl_loadIntoFrame(XFrame xFrame, String sURL, PropertyValue args[]) throws Exception |
| 273 |
{ |
| 274 |
XComponentLoader xLoader = UnoRuntime.queryInterface(XComponentLoader.class, xFrame); |
| 275 |
if (xLoader == null) |
| 276 |
{ |
| 277 |
fail("Frame does not provide required interface XComponentLoader."); |
| 278 |
} |
| 279 |
|
| 280 |
XComponent xDoc = xLoader.loadComponentFromURL(sURL, "_self", 0, args); |
| 281 |
|
| 282 |
if (xDoc == null) |
| 283 |
{ |
| 284 |
fail("Could not load \"" + sURL + "\"."); |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
/** @short create a uno implementation directly. |
| 289 |
*/ |
| 290 |
private Object impl_createUNOComponent(String sName) |
| 291 |
{ |
| 292 |
Object aComponent = null; |
| 293 |
try |
| 294 |
{ |
| 295 |
aComponent = m_xMSF.createInstance(sName); |
| 296 |
} |
| 297 |
catch (java.lang.Throwable ex) |
| 298 |
{ |
| 299 |
aComponent = null; |
| 300 |
} |
| 301 |
|
| 302 |
if (aComponent == null) |
| 303 |
{ |
| 304 |
fail("Could not create UNO component \"" + sName + "\"."); |
| 305 |
} |
| 306 |
return aComponent; |
| 307 |
} |
| 308 |
|
| 309 |
/** @short check the interface XDispatchInformationProvider |
| 310 |
at the specified component. |
| 311 |
*/ |
| 312 |
private void impl_checkDispatchInfo(Object aComponent) throws Exception |
| 313 |
{ |
| 314 |
XDispatchInformationProvider xInfoProvider = UnoRuntime.queryInterface(XDispatchInformationProvider.class, aComponent); |
| 315 |
if (xInfoProvider == null) |
| 316 |
{ |
| 317 |
// Warning |
| 318 |
System.out.println("Warning:\tComponent does not provide the [optional!] interface XDispatchInformationProvider."); |
| 319 |
return; |
| 320 |
} |
| 321 |
|
| 322 |
short[] lGroups = xInfoProvider.getSupportedCommandGroups(); |
| 323 |
int c1 = lGroups.length; |
| 324 |
int i1 = 0; |
| 325 |
for (i1 = 0; i1 < c1; ++i1) |
| 326 |
{ |
| 327 |
short nGroup = lGroups[i1]; |
| 328 |
DispatchInformation[] lInfos = xInfoProvider.getConfigurableDispatchInformation(nGroup); |
| 329 |
int c2 = lInfos.length; |
| 330 |
int i2 = 0; |
| 331 |
|
| 332 |
// check for empty lists |
| 333 |
// Warning |
| 334 |
if (lInfos.length < 1) |
| 335 |
{ |
| 336 |
System.out.println("Warning:\tCould not get any DispatchInformation for group [" + nGroup + "]."); |
| 337 |
} |
| 338 |
|
| 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) |
| 342 |
{ |
| 343 |
DispatchInformation aInfo = lInfos[i2]; |
| 344 |
if (aInfo.GroupId != nGroup) |
| 345 |
{ |
| 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; |
| 350 |
} |
| 351 |
|
| 352 |
if (aCheckMap.containsKey(aInfo.Command)) |
| 353 |
{ |
| 354 |
// Error |
| 355 |
fail("Found a duplicate item: group=[" + aInfo.GroupId + "] command=\"" + aInfo.Command + "\""); // true => don't break this test |
| 356 |
continue; |
| 357 |
} |
| 358 |
|
| 359 |
aCheckMap.put(aInfo.Command, aInfo.Command); |
| 360 |
System.out.println("\t[" + aInfo.GroupId + "] \"" + aInfo.Command + "\""); |
| 361 |
} |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
private synchronized XFrame impl_createNewFrame() |
| 366 |
{ |
| 367 |
XFrame xFrame = m_xDesktop.findFrame("_blank", 0); |
| 368 |
xFrame.getContainerWindow().setVisible(true); |
| 369 |
return xFrame; |
| 370 |
} |
| 371 |
|
| 372 |
private synchronized void impl_closeFrame(XFrame xFrame) throws Exception |
| 373 |
{ |
| 374 |
XCloseable xClose = UnoRuntime.queryInterface(XCloseable.class, xFrame); |
| 375 |
xClose.close(false); |
| 376 |
} |
| 377 |
|
| 378 |
private XMultiServiceFactory getMSF() |
| 379 |
{ |
| 380 |
return UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager()); |
| 381 |
} |
| 382 |
|
| 383 |
// setup and close connections |
| 384 |
@BeforeClass |
| 385 |
public static void setUpConnection() throws Exception |
| 386 |
{ |
| 387 |
System.out.println("setUpConnection()"); |
| 388 |
connection.setUp(); |
| 389 |
} |
| 390 |
|
| 391 |
@AfterClass |
| 392 |
public static void tearDownConnection() |
| 393 |
throws InterruptedException, com.sun.star.uno.Exception |
| 394 |
{ |
| 395 |
System.out.println("tearDownConnection()"); |
| 396 |
connection.tearDown(); |
| 397 |
} |
| 398 |
private static final OfficeConnection connection = new OfficeConnection(); |
| 399 |
} |
| 400 |
|
| 401 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |