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