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 complex.forms; 24 25 import com.sun.star.beans.Property; 26 import com.sun.star.beans.PropertyAttribute; 27 import com.sun.star.beans.PropertyChangeEvent; 28 import com.sun.star.beans.XMultiPropertySet; 29 import com.sun.star.beans.XPropertiesChangeListener; 30 import com.sun.star.lang.EventObject; 31 import com.sun.star.drawing.XControlShape; 32 import com.sun.star.lang.XComponent; 33 import com.sun.star.lang.XMultiServiceFactory; 34 import com.sun.star.uno.UnoRuntime; 35 import com.sun.star.util.CloseVetoException; 36 import com.sun.star.util.XCloseable; 37 import java.util.Vector; 38 import java.util.logging.Level; 39 import java.util.logging.Logger; 40 import util.FormTools; 41 import util.SOfficeFactory; 42 import util.ValueChanger; 43 44 import org.junit.After; 45 import org.junit.AfterClass; 46 import org.junit.Before; 47 import org.junit.BeforeClass; 48 import org.junit.Test; 49 import org.openoffice.test.OfficeConnection; 50 import static org.junit.Assert.*; 51 52 /** 53 */ 54 public class CheckOGroupBoxModel 55 { 56 57 private XMultiPropertySet m_xPropSet; 58 private XComponent m_xDrawDoc; 59 before()60 @Before public void before() 61 { 62 // XComponent xDrawDoc = null; 63 SOfficeFactory SOF = SOfficeFactory.getFactory(getMSF()); 64 65 try 66 { 67 System.out.println("creating a draw document"); 68 m_xDrawDoc = SOF.createDrawDoc(null); 69 } 70 catch (com.sun.star.uno.Exception e) 71 { 72 fail("Couldn't create document."); 73 } 74 75 String objName = "GroupBox"; 76 XControlShape shape = FormTools.insertControlShape(m_xDrawDoc, 5000, 7000, 2000, 2000, objName); 77 m_xPropSet = UnoRuntime.queryInterface(XMultiPropertySet.class, shape.getControl()); 78 } 79 after()80 @After public void after() 81 { 82 XCloseable xClose = UnoRuntime.queryInterface(XCloseable.class, m_xDrawDoc); 83 if (xClose != null) 84 { 85 try 86 { 87 xClose.close(true); 88 } 89 catch (CloseVetoException ex) 90 { 91 fail("Can't close document. Exception caught: " + ex.getMessage()); 92 /* ignore! */ 93 } 94 } 95 } setPropertyValues()96 @Test public void setPropertyValues() 97 { 98 String[] boundPropsToTest = getBoundPropsToTest(); 99 100 MyChangeListener ml = new MyChangeListener(); 101 m_xPropSet.addPropertiesChangeListener(boundPropsToTest, ml); 102 103 Object[] gValues = m_xPropSet.getPropertyValues(boundPropsToTest); 104 Object[] newValue = new Object[gValues.length]; 105 System.out.println("Trying to change all properties."); 106 for (int i = 0; i < boundPropsToTest.length; i++) 107 { 108 newValue[i] = ValueChanger.changePValue(gValues[i]); 109 } 110 try 111 { 112 m_xPropSet.setPropertyValues(boundPropsToTest, newValue); 113 } 114 catch (com.sun.star.beans.PropertyVetoException e) 115 { 116 fail("Exception occurred while trying to change the properties."); 117 } 118 catch (com.sun.star.lang.IllegalArgumentException e) 119 { 120 fail("Exception occurred while trying to change the properties."); 121 } 122 catch (com.sun.star.lang.WrappedTargetException e) 123 { 124 fail("Exception occurred while trying to change the properties."); 125 } // end of try-catch 126 127 assertTrue("Listener was not called.", ml.wasListenerCalled()); 128 m_xPropSet.removePropertiesChangeListener(ml); 129 } 130 getBoundPropsToTest()131 private String[] getBoundPropsToTest() 132 { 133 Property[] properties = m_xPropSet.getPropertySetInfo().getProperties(); 134 String[] testPropsNames = null; 135 136 Vector<String> tNames = new Vector<String>(); 137 138 for (int i = 0; i < properties.length; i++) 139 { 140 141 Property property = properties[i]; 142 String name = property.Name; 143 boolean isWritable = ((property.Attributes 144 & PropertyAttribute.READONLY) == 0); 145 boolean isNotNull = ((property.Attributes 146 & PropertyAttribute.MAYBEVOID) == 0); 147 boolean isBound = ((property.Attributes 148 & PropertyAttribute.BOUND) != 0); 149 150 if (isWritable && isNotNull && isBound) 151 { 152 tNames.add(name); 153 } 154 155 } // endfor 156 157 //get a array of bound properties 158 testPropsNames = new String[tNames.size()]; 159 testPropsNames = tNames.toArray(testPropsNames); 160 return testPropsNames; 161 } 162 163 /** 164 * Listener implementation which sets a flag when 165 * listener was called. 166 */ 167 public class MyChangeListener implements XPropertiesChangeListener 168 { 169 170 boolean propertiesChanged = false; 171 propertiesChange(PropertyChangeEvent[] e)172 public void propertiesChange(PropertyChangeEvent[] e) 173 { 174 propertiesChanged = true; 175 } 176 disposing(EventObject obj)177 public void disposing(EventObject obj) 178 { 179 } 180 wasListenerCalled()181 public boolean wasListenerCalled() 182 { 183 return propertiesChanged; 184 } 185 reset()186 public void reset() 187 { 188 propertiesChanged = false; 189 } 190 }; 191 getMSF()192 private XMultiServiceFactory getMSF() 193 { 194 final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager()); 195 return xMSF1; 196 } 197 198 // setup and close connections 199 @BeforeClass setUpConnection()200 public static void setUpConnection() throws Exception 201 { 202 System.out.println("setUpConnection()"); 203 connection.setUp(); 204 } 205 206 @AfterClass tearDownConnection()207 public static void tearDownConnection() 208 throws InterruptedException, com.sun.star.uno.Exception 209 { 210 System.out.println("tearDownConnection()"); 211 connection.tearDown(); 212 } 213 private static final OfficeConnection connection = new OfficeConnection(); 214 } 215