Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / ucb / qa / complex / tdoc / CheckContentProvider.java
blob45ead7e4c276234cfac26f2d266b260a988aeef5
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.tdoc;
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;
36 import util.WriterTools;
38 import org.junit.After;
39 import org.junit.AfterClass;
40 import org.junit.Before;
41 import org.junit.BeforeClass;
42 import org.junit.Test;
43 import org.openoffice.test.OfficeConnection;
44 import static org.junit.Assert.*;
46 /**
47 * Check the TransientDocumentsContentProvider (TDOC). Three documents are
48 * loaded. Then every possible TDCP content type is instantiated and its
49 * interfaces are tested.<br>
50 * Important: opened documents are numbered in the order they are opened and
51 * numbers are not reused. This test will work only, if you start a new office
52 * with an accept parameter (writer is initially opened). Otherwise loaded
53 * documents are not found.
55 public class CheckContentProvider {
56 private final String testDocuments[] = new String[]{"filter.sxw", "chinese.sxw", "Iterator.sxw"};
57 private final int countDocs = testDocuments.length;
58 private XMultiServiceFactory xMSF = null;
59 private XTextDocument[] xTextDoc = null;
60 private XContent xContent = null;
62 /**
63 * The test methods: the test methods have to be executed in a specified
64 * order. This order is:
65 * <ol>
66 * <li>"checkTDOCRoot"</li>
67 * <li>"checkTDOCRootInterfaces"</li>
68 * <li>"checkTDOCDocument"</li>
69 * <li>"checkTDOCDocumentInterfaces"</li>
70 * <li>"checkTDOCFolder"</li>
71 * <li>"checkTDOCFolderInterfaces"</li>
72 * <li>"checkTDOCStream"</li>
73 * <li>"checkTDOCStreamInterfaces"</li>
74 * </ol>
75 * Important is, that the test of the element comes first, then the test of
76 * its interfaces.
77 **/
78 // public String[] getTestMethodNames() {
79 // return new String[]{"checkTDOCRoot",
80 // "checkTDOCRootInterfaces",
81 // "checkTDOCDocument",
82 // "checkTDOCDocumentInterfaces",
83 // "checkTDOCFolder",
84 // "checkTDOCFolderInterfaces",
85 // "checkTDOCStream",
86 // "checkTDOCStreamInterfaces",
87 // };
88 // }
90 /**
91 * Open some documents before the test
93 @Before public void before() {
94 xMSF = getMSF();
95 xTextDoc = new XTextDocument[countDocs];
96 System.out.println("Open some new documents.");
97 for (int i=0; i<countDocs; i++) {
98 String fileName = TestDocument.getUrl(testDocuments[i]);
99 System.out.println("Doc " + i + ": " + fileName);
100 xTextDoc[i] = WriterTools.loadTextDoc(xMSF, fileName);
101 assertNotNull("Can't load document " + fileName, xTextDoc[i]);
106 * Close the documents
108 @After public void after() {
109 System.out.println("Close all documents.");
110 for (int i=0; i<countDocs; i++) {
111 xTextDoc[i].dispose();
116 * Check the tdcp root.
118 @Test public void checkTDOCRoot() {
119 try {
120 // create a content provider
121 Object o = xMSF.createInstance("com.sun.star.comp.ucb.TransientDocumentsContentProvider");
122 XContentProvider xContentProvider =
123 UnoRuntime.queryInterface(XContentProvider.class, o);
125 // create unconfigured ucb
126 XContentIdentifierFactory xContentIdentifierFactory =
127 UnoRuntime.queryInterface(XContentIdentifierFactory.class, xMSF.createInstance("com.sun.star.ucb.UniversalContentBroker"));
128 // create a content identifier from the ucb for tdoc
129 XContentIdentifier xContentIdentifier =
130 xContentIdentifierFactory.createContentIdentifier("vnd.sun.star.tdoc:/");
131 // get content
132 xContent = xContentProvider.queryContent(xContentIdentifier);
134 String content = xContent.getContentType();
135 System.out.println("#### Content root: " + content);
137 // try to get some documents: should be "countDocs" at least.
138 XContentIdentifier[] xContentId = new XContentIdentifier[countDocs+5];
139 XContent[] xCont = new XContent[countDocs+5];
141 for (int i=0; i<countDocs+5; i++) {
142 xContentId[i] = xContentIdentifierFactory.createContentIdentifier("vnd.sun.star.tdoc:/" + i);
143 // get content
144 xCont[i] = xContentProvider.queryContent(xContentId[i]);
145 int returnVal = xContentProvider.compareContentIds(xContentId[i], xContentIdentifier);
146 String cont = null;
147 if (xCont[i] != null)
149 cont = xCont[i].getContentType();
151 System.out.println("Document Content " + i + ": " + cont + " compare with root: " + returnVal);
153 xContentId[i] = xContentIdentifierFactory.createContentIdentifier("vnd.sun.star.tdoc:/" + i + "/content.xml");
154 // get content
155 xCont[i] = xContentProvider.queryContent(xContentId[i]);
156 cont = null;
157 if (xCont[i] != null)
159 cont = xCont[i].getContentType();
161 System.out.println("\tContent.xml Content " + i + ": " + cont);
164 util.dbg.printInterfaces(xContent);
166 catch(Exception e) {
167 e.printStackTrace();
168 fail("Unexpected Exception: " + e.getMessage());
173 * Check the interfaces of the root.
175 @Test public void checkTDOCRootInterfaces() {
176 checkInterfaces(false);
180 * Check the tdcp document: document 3 is used.
182 @Test public void checkTDOCDocument() {
183 try {
184 xContent = null;
185 Object o = xMSF.createInstance("com.sun.star.comp.ucb.TransientDocumentsContentProvider");
186 XContentProvider xContentProvider =
187 UnoRuntime.queryInterface(XContentProvider.class, o);
188 // create unconfigured ucb
189 XContentIdentifierFactory xContentIdentifierFactory =
190 UnoRuntime.queryInterface(XContentIdentifierFactory.class, xMSF.createInstance("com.sun.star.ucb.UniversalContentBroker"));
191 // create a content identifier from the ucb for tdoc
192 XContentIdentifier xContentIdentifier =
193 xContentIdentifierFactory.createContentIdentifier("vnd.sun.star.tdoc:/3");
194 // get content
195 xContent = xContentProvider.queryContent(xContentIdentifier);
196 // assertNotNull(xContent);
197 String content = xContent.getContentType();
198 System.out.println("#### Document root: " + content);
200 catch(Exception e) {
201 e.printStackTrace();
202 fail("Unexpected Exception: " + e.getMessage());
207 * Check the interfaces on the document.
209 @Test public void checkTDOCDocumentInterfaces() {
210 checkInterfaces(true);
214 * Check a folder on document 2 (document 2 contains an embedded picture and
215 * therefore contans a subfolder "Pictures"
217 @Test public void checkTDOCFolder() {
218 try {
219 xContent = null;
220 Object o = xMSF.createInstance("com.sun.star.comp.ucb.TransientDocumentsContentProvider");
221 XContentProvider xContentProvider =
222 UnoRuntime.queryInterface(XContentProvider.class, o);
223 // create unconfigured ucb
224 XContentIdentifierFactory xContentIdentifierFactory =
225 UnoRuntime.queryInterface(XContentIdentifierFactory.class, xMSF.createInstance("com.sun.star.ucb.UniversalContentBroker"));
226 // create a content identifier from the ucb for tdoc
227 XContentIdentifier xContentIdentifier =
228 xContentIdentifierFactory.createContentIdentifier("vnd.sun.star.tdoc:/2/Pictures");
229 // get content
230 xContent = xContentProvider.queryContent(xContentIdentifier);
232 String content = xContent.getContentType();
233 System.out.println("#### Folder type: " + content);
235 catch(Exception e) {
236 e.printStackTrace();
237 fail("Unexpected Exception: " + e.getMessage());
242 * Check the interfaces on the folder.
244 @Test public void checkTDOCFolderInterfaces() {
245 checkInterfaces(true);
249 * Open a stream to the embedded picture of document 1.
251 @Test public void checkTDOCStream() {
252 try {
253 xContent = null;
254 Object o = xMSF.createInstance("com.sun.star.comp.ucb.TransientDocumentsContentProvider");
255 XContentProvider xContentProvider =
256 UnoRuntime.queryInterface(XContentProvider.class, o);
258 // create unconfigured ucb
259 XContentIdentifierFactory xContentIdentifierFactory =
260 UnoRuntime.queryInterface(XContentIdentifierFactory.class, xMSF.createInstance("com.sun.star.ucb.UniversalContentBroker"));
261 // create a content identifier from the ucb for tdoc
262 XContentIdentifier xContentIdentifier =
263 xContentIdentifierFactory.createContentIdentifier("vnd.sun.star.tdoc:/1/Pictures/10000000000000640000004B9C743800.gif");
264 // get content
265 xContent = xContentProvider.queryContent(xContentIdentifier);
267 String content = xContent.getContentType();
268 System.out.println("#### Folder type: " + content);
270 catch(Exception e) {
271 e.printStackTrace();
272 fail("Unexpected Exception: " + e.getMessage());
277 * Check the interfaces on the stream.
279 @Test public void checkTDOCStreamInterfaces() {
280 checkInterfaces(true);
284 * Since all tdcp content types implement (nearly) the same interfaces, they
285 * are called here.
286 * Executed interface tests are (in this order):
287 * <ol>
288 * <li>XTypeProvider</li>
289 * <li>XServiceInfo</li>
290 * <li>XCommandProcessor</li>
291 * <li>XChild</li>
292 * <li>XPropertiesChangeNotifier</li>
293 * <li>XPropertySetInfoChangeNotifier</li>
294 * <li>XCommandInfoChangeNotifier</li>
295 * <li>XContent</li>
296 * <li>XPropertyContainer</li>
297 * <li>XComponent</li>
298 * </ol>
299 * @param hasParent True, if the tested content type does have a parent:
300 * only the root has not. Used in the XChild interface test.
302 private void checkInterfaces(boolean hasParent) {
303 // check the XTypeProvider interface
304 _XTypeProvider xTypeProvider = new _XTypeProvider();
305 _XTypeProvider.oObj = UnoRuntime.queryInterface(XTypeProvider.class, xContent);
306 // xTypeProvider.log = log;
307 assertNotNull("getImplementationId()", xTypeProvider._getImplementationId());
308 assertNotNull("getTypes()", xTypeProvider._getTypes());
310 // check the XSewrviceInfo interface
311 _XServiceInfo xServiceInfo = new _XServiceInfo();
312 _XServiceInfo.oObj = UnoRuntime.queryInterface(XServiceInfo.class, xContent);
313 // xServiceInfo.log = log;
314 assertNotNull("getImplementationName()", xServiceInfo._getImplementationName());
315 assertNotNull("getSupportedServiceNames()", xServiceInfo._getSupportedServiceNames());
316 assertNotNull("supportsService()", xServiceInfo._supportsService());
318 // check the XCommandProcessor interface
319 _XCommandProcessor xCommandProcessor = new _XCommandProcessor();
320 xCommandProcessor.oObj = UnoRuntime.queryInterface(XCommandProcessor.class, xContent);
321 // xCommandProcessor.log = log;
322 xCommandProcessor.before(getMSF());
323 assertNotNull("createCommandIdentifier()", xCommandProcessor._createCommandIdentifier());
324 assertNotNull("execute()", xCommandProcessor._execute());
325 assertNotNull("abort()", xCommandProcessor._abort());
327 // check the XChild interface
328 _XChild xChild = new _XChild();
329 xChild.oObj = UnoRuntime.queryInterface(XChild.class, xContent);
330 // xChild.log = log;
331 // hasParent dermines, if this content has a parent
332 assertNotNull("getParent()", xChild._getParent(hasParent));
333 // parameter does dermine, if this funczion is supported: generally not supported with tdcp content
334 assertNotNull("setParent()", xChild._setParent(false));
336 // check the XPropertyChangeNotifier interface
337 _XPropertiesChangeNotifier xPropChange = new _XPropertiesChangeNotifier();
338 xPropChange.oObj = UnoRuntime.queryInterface(XPropertiesChangeNotifier.class, xContent);
339 // xPropChange.log = log;
340 assertNotNull("addPropertiesChangeListener()", xPropChange._addPropertiesChangeListener());
341 assertNotNull("removePropertiesChangeListener()", xPropChange._removePropertiesChangeListener());
343 // check the XPropertySetInfoChangeNotifier interface
344 _XPropertySetInfoChangeNotifier xPropSetInfo = new _XPropertySetInfoChangeNotifier();
345 xPropSetInfo.oObj = UnoRuntime.queryInterface(XPropertySetInfoChangeNotifier.class, xContent);
346 // xPropSetInfo.log = log;
347 assertNotNull("addPropertiesChangeListener()", xPropSetInfo._addPropertiesChangeListener());
348 assertNotNull("removePropertiesChangeListener()", xPropSetInfo._removePropertiesChangeListener());
350 // check the XCommandInfoChangeNotifier interface
351 _XCommandInfoChangeNotifier xCommandChange = new _XCommandInfoChangeNotifier();
352 xCommandChange.oObj = UnoRuntime.queryInterface(XCommandInfoChangeNotifier.class, xContent);
353 // xCommandChange.log = log;
354 assertNotNull("addCommandInfoChangeListener()", xCommandChange._addCommandInfoChangeListener());
355 assertNotNull("removeCommandInfoChangeListener()", xCommandChange._removeCommandInfoChangeListener());
357 // check the XContent interface
358 _XContent xCont = new _XContent();
359 xCont.oObj = UnoRuntime.queryInterface(XContent.class, xContent);
360 // xCont.log = log;
361 assertNotNull("addContentEventListener()", xCont._addContentEventListener());
362 assertNotNull("getContentType()", xCont._getContentType());
363 assertNotNull("getIdentifier()", xCont._getIdentifier());
364 assertNotNull("removeContentEventListener()", xCont._removeContentEventListener());
366 // check the XPropertyContainer interface
367 _XPropertyContainer xPropCont = new _XPropertyContainer();
368 xPropCont.oObj = UnoRuntime.queryInterface(XPropertyContainer.class, xContent);
369 // xPropCont.log = log;
370 assertNotNull("addProperty()", xPropCont._addProperty());
371 assertNotNull("removeProperty()", xPropCont._removeProperty());
373 // check the XComponent interface
374 _XComponent xComponent = new _XComponent();
375 _XComponent.oObj = UnoRuntime.queryInterface(XComponent.class, xContent);
376 // xComponent.log = log;
377 assertNotNull("addEventListener()", xComponent._addEventListener());
378 assertNotNull("removeEventListener()", xComponent._removeEventListener());
379 // assure("dispose()", xComponent._dispose());
385 private XMultiServiceFactory getMSF()
387 final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
388 return xMSF1;
391 // setup and close connections
392 @BeforeClass public static void setUpConnection() throws Exception {
393 System.out.println("setUpConnection()");
394 connection.setUp();
397 @AfterClass public static void tearDownConnection()
398 throws InterruptedException, com.sun.star.uno.Exception
400 System.out.println("tearDownConnection()");
401 connection.tearDown();
404 private static final OfficeConnection connection = new OfficeConnection();