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