xref: /AOO41X/main/qadevOOo/tests/java/mod/_fwk/ModuleUIConfigurationManager.java (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
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 
28 package mod._fwk;
29 
30 import com.sun.star.beans.PropertyValue;
31 import com.sun.star.container.XIndexAccess;
32 import com.sun.star.container.XIndexContainer;
33 import com.sun.star.container.XNameAccess;
34 import com.sun.star.embed.ElementModes;
35 import com.sun.star.embed.XStorage;
36 import com.sun.star.embed.XTransactedObject;
37 import com.sun.star.lang.XMultiServiceFactory;
38 import com.sun.star.lang.XSingleServiceFactory;
39 import com.sun.star.uno.UnoRuntime;
40 import com.sun.star.uno.XInterface;
41 import com.sun.star.ui.XModuleUIConfigurationManagerSupplier;
42 import ifc.ui._XUIConfiguration;
43 import java.io.PrintWriter;
44 import com.sun.star.lang.EventObject;
45 import com.sun.star.text.XTextDocument;
46 import com.sun.star.util.XCloseable;
47 import com.sun.star.ui.ConfigurationEvent;
48 import com.sun.star.ui.XUIConfigurationManager;
49 import ifc.ui._XUIConfigurationManager;
50 import lib.StatusException;
51 import lib.TestCase;
52 import lib.TestEnvironment;
53 import lib.TestParameters;
54 import util.WriterTools;
55 import util.utils;
56 
57 /**
58  */
59 public class ModuleUIConfigurationManager extends TestCase {
60     XInterface oObj = null;
61     XMultiServiceFactory xMSF = null;
62     XTextDocument xTextDoc = null;
63     XStorage xStore = null;
64 
65     /**
66      * Cleanup: close the created document
67      * @param tParam The test parameters.
68      * @param The log writer.
69      * @return The test environment.
70      */
71     protected void cleanup(TestParameters tParam, PrintWriter log) {
72         log.println("    disposing xTextDoc ");
73         if (xTextDoc != null) {
74             try {
75                 XCloseable closer = (XCloseable) UnoRuntime.queryInterface(
76                     XCloseable.class, xTextDoc);
77                 closer.close(true);
78             } catch (com.sun.star.util.CloseVetoException e) {
79                 log.println("couldn't close document");
80             } catch (com.sun.star.lang.DisposedException e) {
81                 log.println("couldn't close document");
82             }
83         }
84         log.println("   disposing storage");
85         if (xStore != null) {
86             xStore.dispose();
87         }
88     }
89 
90     /**
91      * Create environment.
92      */
93     protected TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
94         TestEnvironment tEnv = null;
95 
96         try {
97             xMSF = (XMultiServiceFactory)tParam.getMSF();
98 
99             log.println("Creating instance...");
100             xTextDoc = WriterTools.createTextDoc(xMSF);
101 
102             Object o = (XInterface)xMSF.createInstance("com.sun.star.ui.ModuleUIConfigurationManagerSupplier");
103             XModuleUIConfigurationManagerSupplier xMUICMS = (XModuleUIConfigurationManagerSupplier)
104             UnoRuntime.queryInterface(XModuleUIConfigurationManagerSupplier.class, o);
105 
106             util.dbg.printInterfaces(xMUICMS);
107             oObj = xMUICMS.getUIConfigurationManager("com.sun.star.text.TextDocument");
108 
109             log.println("TestObject: " + utils.getImplName(oObj));
110             tEnv = new TestEnvironment(oObj);
111 
112             XNameAccess xMM = (XNameAccess)UnoRuntime.queryInterface(XNameAccess.class, xMSF.createInstance("com.sun.star.comp.framework.ModuleManager"));
113             String[] names = xMM.getElementNames();
114 
115             o = xMSF.createInstance("com.sun.star.embed.StorageFactory");
116             XSingleServiceFactory xStorageService = (XSingleServiceFactory)
117                     UnoRuntime.queryInterface(XSingleServiceFactory.class, o);
118             Object[]props = new Object[2];
119 
120             String aFile = util.utils.getOfficeTempDir(xMSF) + "dummyFile.dat";
121             log.println("storage file : '"+ aFile + "'");
122 
123             props[0] = aFile;
124             props[1] = new Integer(ElementModes.READWRITE);
125             xStore = (XStorage)UnoRuntime.queryInterface(XStorage.class, xStorageService.createInstanceWithArguments(props));
126 
127             PropertyValue[] initProps = new PropertyValue[4];
128             PropertyValue propVal = new PropertyValue();
129             propVal.Name = "DefaultConfigStorage";
130             propVal.Value = xStore;
131             initProps[0] = propVal;
132             propVal = new PropertyValue();
133             propVal.Name = "UserConfigStorage";
134             propVal.Value = xStore;
135             initProps[1] = propVal;
136             propVal = new PropertyValue();
137             propVal.Name = "ModuleIdentifier";
138             propVal.Value = "swriter";
139             initProps[2] = propVal;
140             propVal = new PropertyValue();
141             propVal.Name = "UserRootCommit";
142             propVal.Value = (XTransactedObject)UnoRuntime.queryInterface(XTransactedObject.class, xStore);
143             initProps[3] = propVal;
144 
145 
146             tEnv.addObjRelation("XInitialization.args", initProps);
147 
148             // the short cut manager service name
149             // 2do: correct the service name when it's no longer in
150             tEnv.addObjRelation("XConfigurationManager.ShortCutManager",
151                 "com.sun.star.ui.ModuleAcceleratorConfiguration");
152 
153             // the resourceURL
154             tEnv.addObjRelation("XModuleUIConfigurationManager.ResourceURL",
155                                             "private:resource/menubar/menubar");
156             tEnv.addObjRelation("XUIConfiguration.XUIConfigurationListenerImpl",
157                             new ConfigurationListener(log,
158                             (XUIConfigurationManager)UnoRuntime.queryInterface(
159                             XUIConfigurationManager.class, oObj), xMSF));
160             tEnv.addObjRelation("XModuleUIConfigurationManagerSupplier.ConfigManagerImplementationName",
161                         "com.sun.star.comp.framework.ModuleUIConfigurationManager");
162         }
163         catch(com.sun.star.uno.Exception e) {
164             e.printStackTrace(log);
165             throw new StatusException("Cannot create test object", e);
166         }
167         return tEnv;
168     }
169 
170     /**
171      * An implementation of the _XUIConfiguration.XUIConfigurationListenerImpl
172      * interface to trigger the event for a listener call.
173      * @see ifc.ui._XUIConfiguration
174      */
175     public static class ConfigurationListener implements _XUIConfiguration.XUIConfigurationListenerImpl {
176         private boolean triggered = false;
177         private PrintWriter log = null;
178         private XUIConfigurationManager xUIManager = null;
179         private XMultiServiceFactory xMSF = null;
180 
181         public ConfigurationListener(PrintWriter _log, XUIConfigurationManager xUIManager, XMultiServiceFactory xMSF) {
182             log = _log;
183             this.xUIManager = xUIManager;
184             this.xMSF = xMSF;
185         }
186         public void reset(){
187             triggered = false;
188         }
189         public void fireEvent() {
190             try {
191                 XIndexAccess xMenuBarSettings = xUIManager.getSettings(
192                                         "private:resource/menubar/menubar", true);
193 
194                 PropertyValue[]prop = _XUIConfigurationManager.createMenuBarEntry(
195                                         "Trigger Event", xMenuBarSettings, xMSF, log);
196                 _XUIConfigurationManager.createMenuBarItem("Click for Macro",
197                                 (XIndexContainer)UnoRuntime.queryInterface(
198                                 XIndexContainer.class, prop[3].Value), log);
199                 XIndexContainer x = (XIndexContainer)UnoRuntime.queryInterface(XIndexContainer.class, xMenuBarSettings);
200                 x.insertByIndex(x.getCount(), prop);
201                 xUIManager.replaceSettings("private:resource/menubar/menubar", xMenuBarSettings);
202                 xUIManager.reset();
203             }
204             catch(com.sun.star.container.NoSuchElementException e) {
205                 log.println("_XUIConfiguration.XUIConfigurationListenerImpl: Exception.");
206                 e.printStackTrace(log);
207             }
208             catch(com.sun.star.lang.IllegalArgumentException e) {
209                 log.println("_XUIConfiguration.XUIConfigurationListenerImpl: Exception.");
210                 e.printStackTrace(log);
211             }
212             catch(com.sun.star.lang.IllegalAccessException e) {
213                 log.println("_XUIConfiguration.XUIConfigurationListenerImpl: Exception.");
214                 e.printStackTrace(log);
215             }
216             catch(com.sun.star.lang.IndexOutOfBoundsException e) {
217                 log.println("_XUIConfiguration.XUIConfigurationListenerImpl: Exception.");
218                 e.printStackTrace(log);
219             }
220             catch(com.sun.star.lang.WrappedTargetException e) {
221                 log.println("_XUIConfiguration.XUIConfigurationListenerImpl: Exception.");
222                 e.printStackTrace(log);
223             }
224         }
225         public boolean actionWasTriggered(){
226             return triggered;
227         }
228         public void disposing(EventObject e) {
229             log.println("_XUIConfiguration.XUIConfigurationListenerImpl.disposing the listener.");
230         }
231         public void elementInserted(ConfigurationEvent configEvent) {
232             triggered = true;
233             log.println("_XUIConfiguration.XUIConfigurationListenerImpl.elementInserted.");
234         }
235         public void elementRemoved(ConfigurationEvent configEvent) {
236             triggered = true;
237             log.println("_XUIConfiguration.XUIConfigurationListenerImpl.elementRemoved.");
238         }
239         public void elementReplaced(ConfigurationEvent configEvent) {
240             triggered = true;
241             log.println("_XUIConfiguration.XUIConfigurationListenerImpl.elementReplaced.");
242         }
243     }
244 
245 }
246