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 .
20 import com
.sun
.star
.beans
.XPropertiesChangeNotifier
;
21 import com
.sun
.star
.beans
.XPropertyContainer
;
22 import com
.sun
.star
.beans
.XPropertySetInfoChangeNotifier
;
23 import com
.sun
.star
.container
.XChild
;
24 import com
.sun
.star
.lang
.XComponent
;
25 import com
.sun
.star
.lang
.XMultiServiceFactory
;
26 import com
.sun
.star
.lang
.XServiceInfo
;
27 import com
.sun
.star
.lang
.XTypeProvider
;
28 import com
.sun
.star
.text
.XTextDocument
;
29 import com
.sun
.star
.ucb
.XCommandInfoChangeNotifier
;
30 import com
.sun
.star
.ucb
.XCommandProcessor
;
31 import com
.sun
.star
.ucb
.XContent
;
32 import com
.sun
.star
.ucb
.XContentIdentifier
;
33 import com
.sun
.star
.ucb
.XContentIdentifierFactory
;
34 import com
.sun
.star
.ucb
.XContentProvider
;
35 import com
.sun
.star
.uno
.UnoRuntime
;
37 import util
.WriterTools
;
39 import org
.junit
.After
;
40 import org
.junit
.AfterClass
;
41 import org
.junit
.Before
;
42 import org
.junit
.BeforeClass
;
43 import org
.junit
.Test
;
44 import org
.openoffice
.test
.OfficeConnection
;
46 import static org
.junit
.Assert
.*;
49 * Check the TransientDocumentsContentProvider (TDOC). Three documents are
50 * loaded. Then every possible TDCP content type is instantiated and its
51 * interfaces are tested.<br>
52 * Important: opened documents are numbered in the order they are opened and
53 * numbers are not reused. This test will work only, if you start a new office
54 * with an accept parameter (writer is initially opened). Otherwise loaded
55 * documents are not found.
57 public class CheckContentProvider
{
58 private final String testDocuments
[] = new String
[]{"filter.sxw", "chinese.sxw", "Iterator.sxw"};
59 private final int countDocs
= testDocuments
.length
;
60 private XMultiServiceFactory xMSF
= null;
61 private XTextDocument
[] xTextDoc
= null;
62 private XContent xContent
= null;
66 * Open some documents before the test
68 @Before public void before() {
70 xTextDoc
= new XTextDocument
[countDocs
];
71 System
.out
.println("Open some new documents.");
72 for (int i
=0; i
<countDocs
; i
++) {
73 String fileName
= TestDocument
.getUrl(testDocuments
[i
]);
74 System
.out
.println("Doc " + i
+ ": " + fileName
);
75 xTextDoc
[i
] = WriterTools
.loadTextDoc(xMSF
, fileName
);
76 assertNotNull("Can't load document " + fileName
, xTextDoc
[i
]);
83 @After public void after() {
84 System
.out
.println("Close all documents.");
85 for (int i
=0; i
<countDocs
; i
++) {
86 xTextDoc
[i
].dispose();
91 * Check the tdcp root.
93 @Test public void checkTDOCRoot() {
95 // create a content provider
96 Object o
= xMSF
.createInstance("com.sun.star.comp.ucb.TransientDocumentsContentProvider");
97 XContentProvider xContentProvider
=
98 UnoRuntime
.queryInterface(XContentProvider
.class, o
);
100 // create unconfigured ucb
101 XContentIdentifierFactory xContentIdentifierFactory
=
102 UnoRuntime
.queryInterface(XContentIdentifierFactory
.class, xMSF
.createInstance("com.sun.star.ucb.UniversalContentBroker"));
103 // create a content identifier from the ucb for tdoc
104 XContentIdentifier xContentIdentifier
=
105 xContentIdentifierFactory
.createContentIdentifier("vnd.sun.star.tdoc:/");
107 xContent
= xContentProvider
.queryContent(xContentIdentifier
);
109 String content
= xContent
.getContentType();
110 System
.out
.println("#### Content root: " + content
);
112 // try to get some documents: should be "countDocs" at least.
113 XContentIdentifier
[] xContentId
= new XContentIdentifier
[countDocs
+5];
114 XContent
[] xCont
= new XContent
[countDocs
+5];
116 for (int i
=0; i
<countDocs
+5; i
++) {
117 xContentId
[i
] = xContentIdentifierFactory
.createContentIdentifier("vnd.sun.star.tdoc:/" + i
);
119 xCont
[i
] = xContentProvider
.queryContent(xContentId
[i
]);
120 int returnVal
= xContentProvider
.compareContentIds(xContentId
[i
], xContentIdentifier
);
122 if (xCont
[i
] != null)
124 cont
= xCont
[i
].getContentType();
126 System
.out
.println("Document Content " + i
+ ": " + cont
+ " compare with root: " + returnVal
);
128 xContentId
[i
] = xContentIdentifierFactory
.createContentIdentifier("vnd.sun.star.tdoc:/" + i
+ "/content.xml");
130 xCont
[i
] = xContentProvider
.queryContent(xContentId
[i
]);
132 if (xCont
[i
] != null)
134 cont
= xCont
[i
].getContentType();
136 System
.out
.println("\tContent.xml Content " + i
+ ": " + cont
);
139 util
.dbg
.printInterfaces(xContent
);
143 fail("Unexpected Exception: " + e
.getMessage());
148 * Check the interfaces of the root.
150 @Test public void checkTDOCRootInterfaces() throws Exception
{
151 checkInterfaces(false);
155 * Check the tdcp document: document 3 is used.
157 @Test public void checkTDOCDocument() {
160 Object o
= xMSF
.createInstance("com.sun.star.comp.ucb.TransientDocumentsContentProvider");
161 XContentProvider xContentProvider
=
162 UnoRuntime
.queryInterface(XContentProvider
.class, o
);
163 // create unconfigured ucb
164 XContentIdentifierFactory xContentIdentifierFactory
=
165 UnoRuntime
.queryInterface(XContentIdentifierFactory
.class, xMSF
.createInstance("com.sun.star.ucb.UniversalContentBroker"));
166 // create a content identifier from the ucb for tdoc
167 XContentIdentifier xContentIdentifier
=
168 xContentIdentifierFactory
.createContentIdentifier("vnd.sun.star.tdoc:/3");
170 xContent
= xContentProvider
.queryContent(xContentIdentifier
);
171 // assertNotNull(xContent);
172 String content
= xContent
.getContentType();
173 System
.out
.println("#### Document root: " + content
);
177 fail("Unexpected Exception: " + e
.getMessage());
182 * Check the interfaces on the document.
184 @Test public void checkTDOCDocumentInterfaces() throws Exception
{
185 checkInterfaces(true);
189 * Check a folder on document 2 (document 2 contains an embedded picture and
190 * therefore contains a subfolder "Pictures"
192 @Test public void checkTDOCFolder() {
195 Object o
= xMSF
.createInstance("com.sun.star.comp.ucb.TransientDocumentsContentProvider");
196 XContentProvider xContentProvider
=
197 UnoRuntime
.queryInterface(XContentProvider
.class, o
);
198 // create unconfigured ucb
199 XContentIdentifierFactory xContentIdentifierFactory
=
200 UnoRuntime
.queryInterface(XContentIdentifierFactory
.class, xMSF
.createInstance("com.sun.star.ucb.UniversalContentBroker"));
201 // create a content identifier from the ucb for tdoc
202 XContentIdentifier xContentIdentifier
=
203 xContentIdentifierFactory
.createContentIdentifier("vnd.sun.star.tdoc:/2/Pictures");
205 xContent
= xContentProvider
.queryContent(xContentIdentifier
);
207 String content
= xContent
.getContentType();
208 System
.out
.println("#### Folder type: " + content
);
212 fail("Unexpected Exception: " + e
.getMessage());
217 * Check the interfaces on the folder.
219 @Test public void checkTDOCFolderInterfaces() throws Exception
{
220 checkInterfaces(true);
224 * Open a stream to the embedded picture of document 1.
226 @Test public void checkTDOCStream() {
229 Object o
= xMSF
.createInstance("com.sun.star.comp.ucb.TransientDocumentsContentProvider");
230 XContentProvider xContentProvider
=
231 UnoRuntime
.queryInterface(XContentProvider
.class, o
);
233 // create unconfigured ucb
234 XContentIdentifierFactory xContentIdentifierFactory
=
235 UnoRuntime
.queryInterface(XContentIdentifierFactory
.class, xMSF
.createInstance("com.sun.star.ucb.UniversalContentBroker"));
236 // create a content identifier from the ucb for tdoc
237 XContentIdentifier xContentIdentifier
=
238 xContentIdentifierFactory
.createContentIdentifier("vnd.sun.star.tdoc:/1/Pictures/10000000000000640000004B9C743800.gif");
240 xContent
= xContentProvider
.queryContent(xContentIdentifier
);
242 String content
= xContent
.getContentType();
243 System
.out
.println("#### Folder type: " + content
);
247 fail("Unexpected Exception: " + e
.getMessage());
252 * Check the interfaces on the stream.
254 @Test public void checkTDOCStreamInterfaces() throws Exception
{
255 checkInterfaces(true);
259 * Since all tdcp content types implement (nearly) the same interfaces, they
261 * Executed interface tests are (in this order):
263 * <li>XTypeProvider</li>
264 * <li>XServiceInfo</li>
265 * <li>XCommandProcessor</li>
267 * <li>XPropertiesChangeNotifier</li>
268 * <li>XPropertySetInfoChangeNotifier</li>
269 * <li>XCommandInfoChangeNotifier</li>
271 * <li>XPropertyContainer</li>
272 * <li>XComponent</li>
274 * @param hasParent True, if the tested content type does have a parent:
275 * only the root has not. Used in the XChild interface test.
277 private void checkInterfaces(boolean hasParent
) throws Exception
{
278 // check the XTypeProvider interface
279 _XTypeProvider xTypeProvider
= new _XTypeProvider();
280 _XTypeProvider
.oObj
= UnoRuntime
.queryInterface(XTypeProvider
.class, xContent
);
281 // xTypeProvider.log = log;
282 assertNotNull("getImplementationId()", xTypeProvider
._getImplementationId());
283 assertNotNull("getTypes()", xTypeProvider
._getTypes());
285 // check the XServiceInfo interface
286 _XServiceInfo xServiceInfo
= new _XServiceInfo();
287 _XServiceInfo
.oObj
= UnoRuntime
.queryInterface(XServiceInfo
.class, xContent
);
288 // xServiceInfo.log = log;
289 assertNotNull("getImplementationName()", xServiceInfo
._getImplementationName());
290 assertNotNull("getSupportedServiceNames()", xServiceInfo
._getSupportedServiceNames());
291 assertNotNull("supportsService()", xServiceInfo
._supportsService());
293 // check the XCommandProcessor interface
294 _XCommandProcessor xCommandProcessor
= new _XCommandProcessor();
295 xCommandProcessor
.oObj
= UnoRuntime
.queryInterface(XCommandProcessor
.class, xContent
);
296 // xCommandProcessor.log = log;
297 xCommandProcessor
.before(getMSF());
298 assertNotNull("createCommandIdentifier()", xCommandProcessor
._createCommandIdentifier());
299 assertNotNull("execute()", xCommandProcessor
._execute());
300 assertNotNull("abort()", xCommandProcessor
._abort());
302 // check the XChild interface
303 _XChild xChild
= new _XChild();
304 xChild
.oObj
= UnoRuntime
.queryInterface(XChild
.class, xContent
);
305 // hasParent determines if this content has a parent
306 assertNotNull("getParent()", xChild
._getParent(hasParent
));
307 // parameter does determine, if this function is supported: generally not supported with tdcp content
308 assertNotNull("setParent()", xChild
._setParent(false));
310 // check the XPropertyChangeNotifier interface
311 _XPropertiesChangeNotifier xPropChange
= new _XPropertiesChangeNotifier();
312 xPropChange
.oObj
= UnoRuntime
.queryInterface(XPropertiesChangeNotifier
.class, xContent
);
313 assertNotNull("addPropertiesChangeListener()", xPropChange
._addPropertiesChangeListener());
314 assertNotNull("removePropertiesChangeListener()", xPropChange
._removePropertiesChangeListener());
316 // check the XPropertySetInfoChangeNotifier interface
317 _XPropertySetInfoChangeNotifier xPropSetInfo
= new _XPropertySetInfoChangeNotifier();
318 xPropSetInfo
.oObj
= UnoRuntime
.queryInterface(XPropertySetInfoChangeNotifier
.class, xContent
);
319 // xPropSetInfo.log = log;
320 assertNotNull("addPropertiesChangeListener()", xPropSetInfo
._addPropertiesChangeListener());
321 assertNotNull("removePropertiesChangeListener()", xPropSetInfo
._removePropertiesChangeListener());
323 // check the XCommandInfoChangeNotifier interface
324 _XCommandInfoChangeNotifier xCommandChange
= new _XCommandInfoChangeNotifier();
325 xCommandChange
.oObj
= UnoRuntime
.queryInterface(XCommandInfoChangeNotifier
.class, xContent
);
326 // xCommandChange.log = log;
327 assertNotNull("addCommandInfoChangeListener()", xCommandChange
._addCommandInfoChangeListener());
328 assertNotNull("removeCommandInfoChangeListener()", xCommandChange
._removeCommandInfoChangeListener());
330 // check the XContent interface
331 _XContent xCont
= new _XContent();
332 xCont
.oObj
= UnoRuntime
.queryInterface(XContent
.class, xContent
);
334 assertNotNull("addContentEventListener()", xCont
._addContentEventListener());
335 assertNotNull("getContentType()", xCont
._getContentType());
336 assertNotNull("getIdentifier()", xCont
._getIdentifier());
337 assertNotNull("removeContentEventListener()", xCont
._removeContentEventListener());
339 // check the XPropertyContainer interface
340 _XPropertyContainer xPropCont
= new _XPropertyContainer();
341 xPropCont
.oObj
= UnoRuntime
.queryInterface(XPropertyContainer
.class, xContent
);
342 // xPropCont.log = log;
343 assertNotNull("addProperty()", xPropCont
._addProperty());
344 assertNotNull("removeProperty()", xPropCont
._removeProperty());
346 // check the XComponent interface
347 _XComponent xComponent
= new _XComponent();
348 _XComponent
.oObj
= UnoRuntime
.queryInterface(XComponent
.class, xContent
);
349 // xComponent.log = log;
350 assertNotNull("addEventListener()", xComponent
._addEventListener());
351 assertNotNull("removeEventListener()", xComponent
._removeEventListener());
357 private XMultiServiceFactory
getMSF()
359 return UnoRuntime
.queryInterface(XMultiServiceFactory
.class, connection
.getComponentContext().getServiceManager());
362 // setup and close connections
363 @BeforeClass public static void setUpConnection() throws Exception
{
364 System
.out
.println("setUpConnection()");
368 @AfterClass public static void tearDownConnection()
369 throws InterruptedException
, com
.sun
.star
.uno
.Exception
371 System
.out
.println("tearDownConnection()");
372 connection
.tearDown();
375 private static final OfficeConnection connection
= new OfficeConnection();