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