Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / mod / _fwk / UIConfigurationManager.java
blobd32fb8d237d8861704903c26e8dc1e5441f607b0
1 /*
2 * This file is part of the LibreOffice project.
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/.
8 * This file incorporates work covered by the following license notice:
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 .
19 package mod._fwk;
21 import com.sun.star.beans.PropertyValue;
22 import com.sun.star.container.XIndexAccess;
23 import com.sun.star.container.XIndexContainer;
24 import com.sun.star.embed.ElementModes;
25 import com.sun.star.embed.XStorage;
26 import com.sun.star.lang.XMultiServiceFactory;
27 import com.sun.star.lang.XSingleServiceFactory;
28 import com.sun.star.uno.UnoRuntime;
29 import ifc.ui._XUIConfiguration;
30 import java.io.PrintWriter;
31 import com.sun.star.lang.EventObject;
32 import com.sun.star.ui.ConfigurationEvent;
33 import com.sun.star.ui.UIElementType;
34 import com.sun.star.ui.XUIConfigurationManager;
35 import com.sun.star.ui.XUIConfigurationStorage;
36 import ifc.ui._XUIConfigurationManager;
37 import lib.TestCase;
38 import lib.TestEnvironment;
39 import lib.TestParameters;
40 import util.utils;
42 /**
44 public class UIConfigurationManager extends TestCase {
45 XUIConfigurationManager xManager = null;
48 /**
49 * Create test environment:
50 * <ul>
51 * <li>create a text doc</li>
52 * <li>get the model from the text doc</li>
53 * <li>query model for XUIConfigurationManagerSupplier interface</li>
54 * <li>get the manager from the supplier</li>
55 * </ul>
56 * @param tParam The test parameters.
57 * @param log The log writer.
58 * @return The test environment.
60 @Override
61 protected TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
62 TestEnvironment tEnv = null;
63 XMultiServiceFactory xMSF = tParam.getMSF();
65 log.println("Creating instance...");
67 try {
68 xManager = UnoRuntime.queryInterface(
69 XUIConfigurationManager.class, xMSF.createInstance(
70 "com.sun.star.comp.framework.UIConfigurationManager"));
72 catch(com.sun.star.uno.Exception e) {
74 // just to make sure, it's the right one.
75 log.println("TestObject: " + utils.getImplName(xManager));
76 tEnv = new TestEnvironment(xManager);
78 // create a configuration storage
79 try {
80 String sourceDeleteCfg = util.utils.getFullTestURL("delete.cfg");
81 String deleteCfg = util.utils.getFullURL(util.utils.getOfficeTemp(xMSF) + "delete.cfg");
82 util.utils.copyFile(xMSF, sourceDeleteCfg, deleteCfg);
83 XStorage xSubStorage = null;
84 Object o = xMSF.createInstance("com.sun.star.embed.StorageFactory");
85 XSingleServiceFactory xSSF = UnoRuntime.queryInterface(
86 XSingleServiceFactory.class, o);
87 Object[] props = new Object[2];
88 props[0] = deleteCfg;
89 props[1] = Integer.valueOf(ElementModes.READWRITE);
90 XStorage xRootStorage = UnoRuntime.queryInterface(XStorage.class, xSSF.createInstanceWithArguments(props));
91 xSubStorage = xRootStorage.openStorageElement("Configurations2", ElementModes.READWRITE);
93 XUIConfigurationStorage xConfigStorage =UnoRuntime.queryInterface(XUIConfigurationStorage.class, xManager);
94 xConfigStorage.setStorage(xSubStorage);
95 tEnv.addObjRelation("XUIConfigurationStorage.Storage", xSubStorage);
97 catch(com.sun.star.uno.Exception e) {
98 log.println("Could not create storage: " + e.toString());
100 util.dbg.printInterfaces(xManager);
102 tEnv.addObjRelation("XUIConfiguration.XUIConfigurationListenerImpl",
103 new ConfigurationListener(log, xManager, xMSF));
105 // the short cut manager service name
106 // 2do: correct the service name when it's no longer in
107 tEnv.addObjRelation("XConfigurationManager.ShortCutManager",
108 "com.sun.star.ui.DocumentAcceleratorConfiguration");
109 return tEnv;
113 * An implementation of the _XUIConfiguration.XUIConfigurationListenerImpl
114 * interface to trigger the event for a listener call.
115 * @see ifc.ui._XUIConfiguration
117 private static class ConfigurationListener implements _XUIConfiguration.XUIConfigurationListenerImpl {
118 private boolean triggered = false;
119 private final PrintWriter log;
120 private final XUIConfigurationManager xUIManager;
121 private final XMultiServiceFactory xMSF;
122 private static int iUniqueCounter;
124 private ConfigurationListener(PrintWriter _log, XUIConfigurationManager xUIManager, XMultiServiceFactory xMSF) {
125 log = _log;
126 this.xUIManager = xUIManager;
127 this.xMSF = xMSF;
128 iUniqueCounter = 0;
130 public void reset(){
131 triggered = false;
133 public void fireEvent() {
134 try {
135 if (iUniqueCounter == 0) {
136 iUniqueCounter++;
137 xUIManager.getUIElementsInfo(UIElementType.UNKNOWN);
138 XIndexAccess xMenuBarSettings = xUIManager.getSettings(
139 "private:resource/menubar/menubar", true);
141 PropertyValue[]prop = _XUIConfigurationManager.createMenuBarEntry(
142 "Trigger Event", xMenuBarSettings, xMSF, log);
143 _XUIConfigurationManager.createMenuBarItem("Click for Macro",
144 UnoRuntime.queryInterface(
145 XIndexContainer.class, prop[3].Value), log);
146 XIndexContainer x = UnoRuntime.queryInterface(XIndexContainer.class, xMenuBarSettings);
147 x.insertByIndex(x.getCount(), prop);
148 xUIManager.replaceSettings("private:resource/menubar/menubar", xMenuBarSettings);
149 xUIManager.reset();
152 catch(com.sun.star.container.NoSuchElementException e) {
153 log.println("_XUIConfiguration.XUIConfigurationListenerImpl: Exception.");
154 e.printStackTrace(log);
156 catch(com.sun.star.lang.IllegalArgumentException e) {
157 log.println("_XUIConfiguration.XUIConfigurationListenerImpl: Exception.");
158 e.printStackTrace(log);
160 catch(com.sun.star.lang.IllegalAccessException e) {
161 log.println("_XUIConfiguration.XUIConfigurationListenerImpl: Exception.");
162 e.printStackTrace(log);
164 catch(com.sun.star.lang.IndexOutOfBoundsException e) {
165 log.println("_XUIConfiguration.XUIConfigurationListenerImpl: Exception.");
166 e.printStackTrace(log);
168 catch(com.sun.star.lang.WrappedTargetException e) {
169 log.println("_XUIConfiguration.XUIConfigurationListenerImpl: Exception.");
170 e.printStackTrace(log);
173 public boolean actionWasTriggered(){
174 return triggered;
176 public void disposing(EventObject e) {
177 triggered = true;
178 log.println("_XUIConfiguration.XUIConfigurationListenerImpl.disposing the listener.");
180 public void elementInserted(ConfigurationEvent configEvent) {
181 triggered = true;
182 log.println("_XUIConfiguration.XUIConfigurationListenerImpl.elementInserted.");
184 public void elementRemoved(ConfigurationEvent configEvent) {
185 triggered = true;
186 log.println("_XUIConfiguration.XUIConfigurationListenerImpl.elementRemoved.");
188 public void elementReplaced(ConfigurationEvent configEvent) {
189 triggered = true;
190 log.println("_XUIConfiguration.XUIConfigurationListenerImpl.elementReplaced.");