merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / tests / java / ifc / ui / _XUIConfigurationManager.java
blob83db39e12f5ef1d73fa63cc4815d48b199ca5623
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XUIConfigurationManager.java,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 package ifc.ui;
32 import com.sun.star.beans.PropertyValue;
33 import com.sun.star.beans.XPropertySet;
34 import com.sun.star.container.XIndexAccess;
35 import com.sun.star.container.XIndexContainer;
36 import com.sun.star.lang.XMultiServiceFactory;
37 import com.sun.star.lang.XServiceInfo;
38 import com.sun.star.lang.XSingleComponentFactory;
39 import com.sun.star.uno.UnoRuntime;
40 import com.sun.star.uno.XComponentContext;
41 import com.sun.star.ui.UIElementType;
42 import com.sun.star.ui.XImageManager;
43 import com.sun.star.ui.XUIConfigurationManager;
44 import java.io.PrintWriter;
45 import lib.MultiMethodTest;
47 public class _XUIConfigurationManager extends MultiMethodTest {
49 public XUIConfigurationManager oObj;
50 private String msResourceUrl = "private:resource/menubar/menubar";
51 private String msMyResourceUrl = "private:resource/menubar/mymenubar";
52 private XIndexContainer mxSettings = null;
53 private XIndexAccess mxMenuBarSettings = null;
54 private XMultiServiceFactory mxMSF = null;
55 private String sShortCutManagerServiceName = null;
58 /**
59 * Some stuff before the tests:
60 * extract the multi service factory.
62 protected void before() {
63 mxMSF = (XMultiServiceFactory)tParam.getMSF();
64 sShortCutManagerServiceName = (String)tEnv.getObjRelation("XConfigurationManager.ShortCutManager");
68 /**
69 * reset all changes: do at the end.
71 public void _reset() {
72 requiredMethod("removeSettings()");
73 oObj.reset();
74 tRes.tested("reset()", true);
77 public void _getUIElementsInfo() {
78 boolean result = true;
79 try {
80 PropertyValue[][]props = oObj.getUIElementsInfo(UIElementType.UNKNOWN);
81 for (int i=0; i<props.length; i++)
82 for(int j=0; j<props[i].length; j++)
83 log.println("Prop["+i+"]["+j+"]: " + props[i][j].Name + " " + props[i][j].Value.toString());
85 catch(com.sun.star.lang.IllegalArgumentException e) {
86 result = false;
87 e.printStackTrace(log);
89 tRes.tested("getUIElementsInfo()", result);
92 public void _createSettings() {
93 mxSettings = oObj.createSettings();
94 util.dbg.printInterfaces(mxSettings);
95 tRes.tested("createSettings()", mxSettings != null);
98 public void _hasSettings() {
99 boolean result = false;
100 try {
101 result = oObj.hasSettings(msResourceUrl);
103 catch(com.sun.star.lang.IllegalArgumentException e) {
104 log.println(e);
105 result = false;
107 tRes.tested("hasSettings()", result);
110 public void _getSettings() {
111 requiredMethod("hasSettings()");
112 boolean result = true;
113 try {
114 mxMenuBarSettings = oObj.getSettings(msResourceUrl, true);
115 result = mxMenuBarSettings != null;
116 for (int i=0; i<mxMenuBarSettings.getCount(); i++) {
117 Object[] o = (Object[])mxMenuBarSettings.getByIndex(i);
118 log.println("+++++++++ i = " + i);
119 for (int j=0; j<o.length; j++) {
120 PropertyValue prop = (PropertyValue)o[j];
121 log.println("Property" + j + ": " + prop.Name + " " + prop.Value.toString());
125 catch(com.sun.star.container.NoSuchElementException e) {
126 result = false;
127 e.printStackTrace(log);
129 catch(com.sun.star.lang.IllegalArgumentException e) {
130 result = false;
131 e.printStackTrace(log);
133 catch(com.sun.star.lang.IndexOutOfBoundsException e) {
134 result = false;
135 e.printStackTrace(log);
137 catch(com.sun.star.lang.WrappedTargetException e) {
138 result = false;
139 e.printStackTrace(log);
141 tRes.tested("getSettings()", result);
144 public void _replaceSettings() {
145 requiredMethod("getSettings()");
146 boolean result = true;
147 PropertyValue[] prop = createMenuBarEntry("My Entry", mxMenuBarSettings, mxMSF, log);
148 if (prop == null) {
149 tRes.tested("replaceSettings()", false);
150 return;
153 createMenuBarItem("Click for Macro", (XIndexContainer)UnoRuntime.queryInterface(
154 XIndexContainer.class, prop[3].Value), log);
156 XIndexContainer x = (XIndexContainer)UnoRuntime.queryInterface(XIndexContainer.class, mxMenuBarSettings);
157 try {
158 x.insertByIndex(x.getCount(), prop);
160 catch(com.sun.star.lang.IllegalArgumentException e) {
161 result = false;
162 e.printStackTrace(log);
164 catch(com.sun.star.lang.IndexOutOfBoundsException e) {
165 result = false;
166 e.printStackTrace(log);
168 catch(com.sun.star.lang.WrappedTargetException e) {
169 result = false;
170 e.printStackTrace(log);
173 try {
174 oObj.replaceSettings(msResourceUrl, mxMenuBarSettings);
176 catch(com.sun.star.container.NoSuchElementException e) {
177 result = false;
178 e.printStackTrace(log);
180 catch(com.sun.star.lang.IllegalArgumentException e) {
181 result = false;
182 e.printStackTrace(log);
184 catch(com.sun.star.lang.IllegalAccessException e) {
185 result = false;
186 e.printStackTrace(log);
188 _getSettings();
189 tRes.tested("replaceSettings()", result);
192 public void _removeSettings() {
193 requiredMethod("insertSettings()");
194 boolean result = true;
195 try {
196 oObj.removeSettings(msMyResourceUrl);
198 catch(com.sun.star.container.NoSuchElementException e) {
199 result = false;
200 e.printStackTrace(log);
202 catch(com.sun.star.lang.IllegalArgumentException e) {
203 result = false;
204 e.printStackTrace(log);
206 catch(com.sun.star.lang.IllegalAccessException e) {
207 e.printStackTrace(log);
208 result = false;
210 tRes.tested("removeSettings()", result);
213 public void _insertSettings() {
214 requiredMethod("createSettings()");
215 requiredMethod("replaceSettings()");
216 boolean result = true;
217 util.dbg.printInterfaces(mxSettings);
218 PropertyValue[] prop = createMenuBarEntry("A new entry", mxSettings, mxMSF, log);
219 if (prop == null) {
220 tRes.tested("replaceSettings()", false);
221 return;
224 createMenuBarItem("A new sub entry", (XIndexContainer)UnoRuntime.queryInterface(
225 XIndexContainer.class, prop[3].Value), log);
227 XIndexContainer x = (XIndexContainer)UnoRuntime.queryInterface(XIndexContainer.class,mxSettings);
228 try {
229 int count = x.getCount();
230 x.insertByIndex(count, prop);
232 catch(com.sun.star.lang.IllegalArgumentException e) {
233 result = false;
234 e.printStackTrace(log);
236 catch(com.sun.star.lang.IndexOutOfBoundsException e) {
237 result = false;
238 e.printStackTrace(log);
240 catch(com.sun.star.lang.WrappedTargetException e) {
241 result = false;
242 e.printStackTrace(log);
245 try {
246 oObj.insertSettings(msMyResourceUrl, mxSettings);
248 catch(com.sun.star.container.ElementExistException e) {
249 e.printStackTrace(log);
250 result = false;
252 catch(com.sun.star.lang.IllegalArgumentException e) {
253 e.printStackTrace(log);
254 result = false;
256 catch(com.sun.star.lang.IllegalAccessException e) {
257 e.printStackTrace(log);
258 result = false;
260 tRes.tested("insertSettings()", result);
264 * Only a short test.
265 * See complex.imageManager.CheckImageManager for a more extensive test of
266 * this implementation.
268 public void _getImageManager() {
269 Object o = oObj.getImageManager();
270 log.println("###### ImageManager ");
271 XImageManager xImageManager = (XImageManager)UnoRuntime.queryInterface(XImageManager.class, o);
272 tRes.tested("getImageManager()", xImageManager != null);
277 * get a shortcut manager
279 public void _getShortCutManager() {
280 Object o = oObj.getShortCutManager();
281 XServiceInfo xSI = (XServiceInfo)UnoRuntime.queryInterface(XServiceInfo.class,o);
282 String[] serviceNames = xSI.getSupportedServiceNames();
283 boolean bSupportedServiceFound = false;
284 for (int i=0; i<serviceNames.length; i++) {
285 log.println("SuppService: " + serviceNames[i]);
286 if (serviceNames[i].equals(sShortCutManagerServiceName)) {
287 bSupportedServiceFound = true;
290 tRes.tested("getShortCutManager()", bSupportedServiceFound);
293 public void _getEventsManager() {
294 Object o = oObj.getEventsManager();
295 tRes.tested("getEventsManager()", o == null);
299 * Create a menu bar entry for adding to the menu bar of the Office.
300 * @param sLabelName The name of the new entry.
301 * @param xMenuBarSettings The existing menu bar settings, used for creating the new entry.
302 * @return An array of properties of the new entry.
304 public static PropertyValue[] createMenuBarEntry(String sLabelName, XIndexAccess xMenuBarSettings, XMultiServiceFactory xMSF, PrintWriter log) {
305 PropertyValue[] prop = new PropertyValue[4];
306 prop[0] = new PropertyValue();
307 prop[0].Name = "CommandURL";
308 prop[0].Value = "vnd.openoffice.org:MyMenu";
309 prop[1] = new PropertyValue();
310 prop[1].Name = "Label";
311 prop[1].Value = sLabelName;
312 prop[2] = new PropertyValue();
313 prop[2].Name = "Type";
314 prop[2].Value = new Short((short)0);
315 prop[3] = new PropertyValue();
316 prop[3].Name = "ItemDescriptorContainer";
318 XSingleComponentFactory xFactory = (XSingleComponentFactory)UnoRuntime.queryInterface(
319 XSingleComponentFactory.class, xMenuBarSettings);
320 try {
321 XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xMSF);
322 XComponentContext xContext = (XComponentContext)UnoRuntime.queryInterface(
323 XComponentContext.class, xProp.getPropertyValue("DefaultContext"));
324 prop[3].Value = xFactory.createInstanceWithContext(xContext);
326 catch(com.sun.star.uno.Exception e) {
327 log.println("Could not create an instance for ItemDescriptorContainer property.");
328 e.printStackTrace(log);
329 return null;
331 return prop;
335 * Create a sub entry to the menu bar.
336 * @param sLabelName The name of the entry in the UI.
337 * @param xDescriptionContainer The parent entry in the menu bar where
338 * this entry is added.
340 public static void createMenuBarItem(String sLabelName, XIndexContainer xDescriptionContainer, PrintWriter log) {
341 PropertyValue[]aMenuItem = new PropertyValue[3];
342 // create a menu item
343 aMenuItem[0] = new PropertyValue();
344 aMenuItem[0].Name = "CommandURL";
345 aMenuItem[0].Value = "macro:///Standard.Module1.Test()";
346 aMenuItem[1] = new PropertyValue();
347 aMenuItem[1].Name = "Label";
348 aMenuItem[1].Value = sLabelName;
349 aMenuItem[2] = new PropertyValue();
350 aMenuItem[2].Name = "Type";
351 aMenuItem[2].Value = new Short((short)0);
353 try {
354 xDescriptionContainer.insertByIndex(0, aMenuItem);
356 catch(com.sun.star.lang.IllegalArgumentException e) {
357 e.printStackTrace(log);
359 catch(com.sun.star.lang.IndexOutOfBoundsException e) {
360 e.printStackTrace(log);
362 catch(com.sun.star.lang.WrappedTargetException e) {
363 e.printStackTrace(log);