bump product version to 5.0.4.1
[LibreOffice.git] / ucb / qa / complex / tdoc / CheckContentProvider.java
bloba802d30ac81b06e05e0fe59cbb4a30f42cdc8878
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;
63 /**
64 * Open some documents before the test
66 @Before public void before() {
67 xMSF = getMSF();
68 xTextDoc = new XTextDocument[countDocs];
69 System.out.println("Open some new documents.");
70 for (int i=0; i<countDocs; i++) {
71 String fileName = TestDocument.getUrl(testDocuments[i]);
72 System.out.println("Doc " + i + ": " + fileName);
73 xTextDoc[i] = WriterTools.loadTextDoc(xMSF, fileName);
74 assertNotNull("Can't load document " + fileName, xTextDoc[i]);
78 /**
79 * Close the documents
81 @After public void after() {
82 System.out.println("Close all documents.");
83 for (int i=0; i<countDocs; i++) {
84 xTextDoc[i].dispose();
88 /**
89 * Check the tdcp root.
91 @Test public void checkTDOCRoot() {
92 try {
93 // create a content provider
94 Object o = xMSF.createInstance("com.sun.star.comp.ucb.TransientDocumentsContentProvider");
95 XContentProvider xContentProvider =
96 UnoRuntime.queryInterface(XContentProvider.class, o);
98 // create unconfigured ucb
99 XContentIdentifierFactory xContentIdentifierFactory =
100 UnoRuntime.queryInterface(XContentIdentifierFactory.class, xMSF.createInstance("com.sun.star.ucb.UniversalContentBroker"));
101 // create a content identifier from the ucb for tdoc
102 XContentIdentifier xContentIdentifier =
103 xContentIdentifierFactory.createContentIdentifier("vnd.sun.star.tdoc:/");
104 // get content
105 xContent = xContentProvider.queryContent(xContentIdentifier);
107 String content = xContent.getContentType();
108 System.out.println("#### Content root: " + content);
110 // try to get some documents: should be "countDocs" at least.
111 XContentIdentifier[] xContentId = new XContentIdentifier[countDocs+5];
112 XContent[] xCont = new XContent[countDocs+5];
114 for (int i=0; i<countDocs+5; i++) {
115 xContentId[i] = xContentIdentifierFactory.createContentIdentifier("vnd.sun.star.tdoc:/" + i);
116 // get content
117 xCont[i] = xContentProvider.queryContent(xContentId[i]);
118 int returnVal = xContentProvider.compareContentIds(xContentId[i], xContentIdentifier);
119 String cont = null;
120 if (xCont[i] != null)
122 cont = xCont[i].getContentType();
124 System.out.println("Document Content " + i + ": " + cont + " compare with root: " + returnVal);
126 xContentId[i] = xContentIdentifierFactory.createContentIdentifier("vnd.sun.star.tdoc:/" + i + "/content.xml");
127 // get content
128 xCont[i] = xContentProvider.queryContent(xContentId[i]);
129 cont = null;
130 if (xCont[i] != null)
132 cont = xCont[i].getContentType();
134 System.out.println("\tContent.xml Content " + i + ": " + cont);
137 util.dbg.printInterfaces(xContent);
139 catch(Exception e) {
140 e.printStackTrace();
141 fail("Unexpected Exception: " + e.getMessage());
146 * Check the interfaces of the root.
148 @Test public void checkTDOCRootInterfaces() {
149 checkInterfaces(false);
153 * Check the tdcp document: document 3 is used.
155 @Test public void checkTDOCDocument() {
156 try {
157 xContent = null;
158 Object o = xMSF.createInstance("com.sun.star.comp.ucb.TransientDocumentsContentProvider");
159 XContentProvider xContentProvider =
160 UnoRuntime.queryInterface(XContentProvider.class, o);
161 // create unconfigured ucb
162 XContentIdentifierFactory xContentIdentifierFactory =
163 UnoRuntime.queryInterface(XContentIdentifierFactory.class, xMSF.createInstance("com.sun.star.ucb.UniversalContentBroker"));
164 // create a content identifier from the ucb for tdoc
165 XContentIdentifier xContentIdentifier =
166 xContentIdentifierFactory.createContentIdentifier("vnd.sun.star.tdoc:/3");
167 // get content
168 xContent = xContentProvider.queryContent(xContentIdentifier);
169 // assertNotNull(xContent);
170 String content = xContent.getContentType();
171 System.out.println("#### Document root: " + content);
173 catch(Exception e) {
174 e.printStackTrace();
175 fail("Unexpected Exception: " + e.getMessage());
180 * Check the interfaces on the document.
182 @Test public void checkTDOCDocumentInterfaces() {
183 checkInterfaces(true);
187 * Check a folder on document 2 (document 2 contains an embedded picture and
188 * therefore contans a subfolder "Pictures"
190 @Test public void checkTDOCFolder() {
191 try {
192 xContent = null;
193 Object o = xMSF.createInstance("com.sun.star.comp.ucb.TransientDocumentsContentProvider");
194 XContentProvider xContentProvider =
195 UnoRuntime.queryInterface(XContentProvider.class, o);
196 // create unconfigured ucb
197 XContentIdentifierFactory xContentIdentifierFactory =
198 UnoRuntime.queryInterface(XContentIdentifierFactory.class, xMSF.createInstance("com.sun.star.ucb.UniversalContentBroker"));
199 // create a content identifier from the ucb for tdoc
200 XContentIdentifier xContentIdentifier =
201 xContentIdentifierFactory.createContentIdentifier("vnd.sun.star.tdoc:/2/Pictures");
202 // get content
203 xContent = xContentProvider.queryContent(xContentIdentifier);
205 String content = xContent.getContentType();
206 System.out.println("#### Folder type: " + content);
208 catch(Exception e) {
209 e.printStackTrace();
210 fail("Unexpected Exception: " + e.getMessage());
215 * Check the interfaces on the folder.
217 @Test public void checkTDOCFolderInterfaces() {
218 checkInterfaces(true);
222 * Open a stream to the embedded picture of document 1.
224 @Test public void checkTDOCStream() {
225 try {
226 xContent = null;
227 Object o = xMSF.createInstance("com.sun.star.comp.ucb.TransientDocumentsContentProvider");
228 XContentProvider xContentProvider =
229 UnoRuntime.queryInterface(XContentProvider.class, o);
231 // create unconfigured ucb
232 XContentIdentifierFactory xContentIdentifierFactory =
233 UnoRuntime.queryInterface(XContentIdentifierFactory.class, xMSF.createInstance("com.sun.star.ucb.UniversalContentBroker"));
234 // create a content identifier from the ucb for tdoc
235 XContentIdentifier xContentIdentifier =
236 xContentIdentifierFactory.createContentIdentifier("vnd.sun.star.tdoc:/1/Pictures/10000000000000640000004B9C743800.gif");
237 // get content
238 xContent = xContentProvider.queryContent(xContentIdentifier);
240 String content = xContent.getContentType();
241 System.out.println("#### Folder type: " + content);
243 catch(Exception e) {
244 e.printStackTrace();
245 fail("Unexpected Exception: " + e.getMessage());
250 * Check the interfaces on the stream.
252 @Test public void checkTDOCStreamInterfaces() {
253 checkInterfaces(true);
257 * Since all tdcp content types implement (nearly) the same interfaces, they
258 * are called here.
259 * Executed interface tests are (in this order):
260 * <ol>
261 * <li>XTypeProvider</li>
262 * <li>XServiceInfo</li>
263 * <li>XCommandProcessor</li>
264 * <li>XChild</li>
265 * <li>XPropertiesChangeNotifier</li>
266 * <li>XPropertySetInfoChangeNotifier</li>
267 * <li>XCommandInfoChangeNotifier</li>
268 * <li>XContent</li>
269 * <li>XPropertyContainer</li>
270 * <li>XComponent</li>
271 * </ol>
272 * @param hasParent True, if the tested content type does have a parent:
273 * only the root has not. Used in the XChild interface test.
275 private void checkInterfaces(boolean hasParent) {
276 // check the XTypeProvider interface
277 _XTypeProvider xTypeProvider = new _XTypeProvider();
278 _XTypeProvider.oObj = UnoRuntime.queryInterface(XTypeProvider.class, xContent);
279 // xTypeProvider.log = log;
280 assertNotNull("getImplementationId()", xTypeProvider._getImplementationId());
281 assertNotNull("getTypes()", xTypeProvider._getTypes());
283 // check the XSewrviceInfo interface
284 _XServiceInfo xServiceInfo = new _XServiceInfo();
285 _XServiceInfo.oObj = UnoRuntime.queryInterface(XServiceInfo.class, xContent);
286 // xServiceInfo.log = log;
287 assertNotNull("getImplementationName()", xServiceInfo._getImplementationName());
288 assertNotNull("getSupportedServiceNames()", xServiceInfo._getSupportedServiceNames());
289 assertNotNull("supportsService()", xServiceInfo._supportsService());
291 // check the XCommandProcessor interface
292 _XCommandProcessor xCommandProcessor = new _XCommandProcessor();
293 xCommandProcessor.oObj = UnoRuntime.queryInterface(XCommandProcessor.class, xContent);
294 // xCommandProcessor.log = log;
295 xCommandProcessor.before(getMSF());
296 assertNotNull("createCommandIdentifier()", xCommandProcessor._createCommandIdentifier());
297 assertNotNull("execute()", xCommandProcessor._execute());
298 assertNotNull("abort()", xCommandProcessor._abort());
300 // check the XChild interface
301 _XChild xChild = new _XChild();
302 xChild.oObj = UnoRuntime.queryInterface(XChild.class, xContent);
303 // hasParent dermines, if this content has a parent
304 assertNotNull("getParent()", xChild._getParent(hasParent));
305 // parameter does dermine, if this funczion is supported: generally not supported with tdcp content
306 assertNotNull("setParent()", xChild._setParent(false));
308 // check the XPropertyChangeNotifier interface
309 _XPropertiesChangeNotifier xPropChange = new _XPropertiesChangeNotifier();
310 xPropChange.oObj = UnoRuntime.queryInterface(XPropertiesChangeNotifier.class, xContent);
311 assertNotNull("addPropertiesChangeListener()", xPropChange._addPropertiesChangeListener());
312 assertNotNull("removePropertiesChangeListener()", xPropChange._removePropertiesChangeListener());
314 // check the XPropertySetInfoChangeNotifier interface
315 _XPropertySetInfoChangeNotifier xPropSetInfo = new _XPropertySetInfoChangeNotifier();
316 xPropSetInfo.oObj = UnoRuntime.queryInterface(XPropertySetInfoChangeNotifier.class, xContent);
317 // xPropSetInfo.log = log;
318 assertNotNull("addPropertiesChangeListener()", xPropSetInfo._addPropertiesChangeListener());
319 assertNotNull("removePropertiesChangeListener()", xPropSetInfo._removePropertiesChangeListener());
321 // check the XCommandInfoChangeNotifier interface
322 _XCommandInfoChangeNotifier xCommandChange = new _XCommandInfoChangeNotifier();
323 xCommandChange.oObj = UnoRuntime.queryInterface(XCommandInfoChangeNotifier.class, xContent);
324 // xCommandChange.log = log;
325 assertNotNull("addCommandInfoChangeListener()", xCommandChange._addCommandInfoChangeListener());
326 assertNotNull("removeCommandInfoChangeListener()", xCommandChange._removeCommandInfoChangeListener());
328 // check the XContent interface
329 _XContent xCont = new _XContent();
330 xCont.oObj = UnoRuntime.queryInterface(XContent.class, xContent);
331 // xCont.log = log;
332 assertNotNull("addContentEventListener()", xCont._addContentEventListener());
333 assertNotNull("getContentType()", xCont._getContentType());
334 assertNotNull("getIdentifier()", xCont._getIdentifier());
335 assertNotNull("removeContentEventListener()", xCont._removeContentEventListener());
337 // check the XPropertyContainer interface
338 _XPropertyContainer xPropCont = new _XPropertyContainer();
339 xPropCont.oObj = UnoRuntime.queryInterface(XPropertyContainer.class, xContent);
340 // xPropCont.log = log;
341 assertNotNull("addProperty()", xPropCont._addProperty());
342 assertNotNull("removeProperty()", xPropCont._removeProperty());
344 // check the XComponent interface
345 _XComponent xComponent = new _XComponent();
346 _XComponent.oObj = UnoRuntime.queryInterface(XComponent.class, xContent);
347 // xComponent.log = log;
348 assertNotNull("addEventListener()", xComponent._addEventListener());
349 assertNotNull("removeEventListener()", xComponent._removeEventListener());
355 private XMultiServiceFactory getMSF()
357 return UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
360 // setup and close connections
361 @BeforeClass public static void setUpConnection() throws Exception {
362 System.out.println("setUpConnection()");
363 connection.setUp();
366 @AfterClass public static void tearDownConnection()
367 throws InterruptedException, com.sun.star.uno.Exception
369 System.out.println("tearDownConnection()");
370 connection.tearDown();
373 private static final OfficeConnection connection = new OfficeConnection();