1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir package complex.forms; 28*cdf0e10cSrcweir 29*cdf0e10cSrcweir import com.sun.star.beans.Property; 30*cdf0e10cSrcweir import com.sun.star.beans.PropertyAttribute; 31*cdf0e10cSrcweir import com.sun.star.beans.PropertyChangeEvent; 32*cdf0e10cSrcweir import com.sun.star.beans.XMultiPropertySet; 33*cdf0e10cSrcweir import com.sun.star.beans.XPropertiesChangeListener; 34*cdf0e10cSrcweir import com.sun.star.lang.EventObject; 35*cdf0e10cSrcweir import com.sun.star.drawing.XControlShape; 36*cdf0e10cSrcweir import com.sun.star.lang.XComponent; 37*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 38*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 39*cdf0e10cSrcweir // import complexlib.ComplexTestCase; 40*cdf0e10cSrcweir import com.sun.star.util.CloseVetoException; 41*cdf0e10cSrcweir import com.sun.star.util.XCloseable; 42*cdf0e10cSrcweir import java.util.Vector; 43*cdf0e10cSrcweir import java.util.logging.Level; 44*cdf0e10cSrcweir import java.util.logging.Logger; 45*cdf0e10cSrcweir import util.FormTools; 46*cdf0e10cSrcweir import util.SOfficeFactory; 47*cdf0e10cSrcweir import util.ValueChanger; 48*cdf0e10cSrcweir 49*cdf0e10cSrcweir import org.junit.After; 50*cdf0e10cSrcweir import org.junit.AfterClass; 51*cdf0e10cSrcweir import org.junit.Before; 52*cdf0e10cSrcweir import org.junit.BeforeClass; 53*cdf0e10cSrcweir import org.junit.Test; 54*cdf0e10cSrcweir import org.openoffice.test.OfficeConnection; 55*cdf0e10cSrcweir import static org.junit.Assert.*; 56*cdf0e10cSrcweir 57*cdf0e10cSrcweir /** 58*cdf0e10cSrcweir */ 59*cdf0e10cSrcweir public class CheckOGroupBoxModel 60*cdf0e10cSrcweir { 61*cdf0e10cSrcweir 62*cdf0e10cSrcweir private XMultiPropertySet m_xPropSet; 63*cdf0e10cSrcweir private XComponent m_xDrawDoc; 64*cdf0e10cSrcweir 65*cdf0e10cSrcweir // public String[] getTestMethodNames() { 66*cdf0e10cSrcweir // return new String[] {"setPropertyValues"}; 67*cdf0e10cSrcweir // } 68*cdf0e10cSrcweir @Before public void before() 69*cdf0e10cSrcweir { 70*cdf0e10cSrcweir // XComponent xDrawDoc = null; 71*cdf0e10cSrcweir SOfficeFactory SOF = SOfficeFactory.getFactory(getMSF()); 72*cdf0e10cSrcweir 73*cdf0e10cSrcweir try 74*cdf0e10cSrcweir { 75*cdf0e10cSrcweir System.out.println("creating a draw document"); 76*cdf0e10cSrcweir m_xDrawDoc = SOF.createDrawDoc(null); 77*cdf0e10cSrcweir } 78*cdf0e10cSrcweir catch (com.sun.star.uno.Exception e) 79*cdf0e10cSrcweir { 80*cdf0e10cSrcweir fail("Couldn't create document."); 81*cdf0e10cSrcweir } 82*cdf0e10cSrcweir 83*cdf0e10cSrcweir String objName = "GroupBox"; 84*cdf0e10cSrcweir XControlShape shape = FormTools.insertControlShape(m_xDrawDoc, 5000, 7000, 2000, 2000, objName); 85*cdf0e10cSrcweir m_xPropSet = UnoRuntime.queryInterface(XMultiPropertySet.class, shape.getControl()); 86*cdf0e10cSrcweir } 87*cdf0e10cSrcweir 88*cdf0e10cSrcweir @After public void after() 89*cdf0e10cSrcweir { 90*cdf0e10cSrcweir XCloseable xClose = UnoRuntime.queryInterface(XCloseable.class, m_xDrawDoc); 91*cdf0e10cSrcweir if (xClose != null) 92*cdf0e10cSrcweir { 93*cdf0e10cSrcweir try 94*cdf0e10cSrcweir { 95*cdf0e10cSrcweir xClose.close(true); 96*cdf0e10cSrcweir } 97*cdf0e10cSrcweir catch (CloseVetoException ex) 98*cdf0e10cSrcweir { 99*cdf0e10cSrcweir fail("Can't close document. Exception caught: " + ex.getMessage()); 100*cdf0e10cSrcweir /* ignore! */ 101*cdf0e10cSrcweir } 102*cdf0e10cSrcweir } 103*cdf0e10cSrcweir } 104*cdf0e10cSrcweir @Test public void setPropertyValues() 105*cdf0e10cSrcweir { 106*cdf0e10cSrcweir String[] boundPropsToTest = getBoundPropsToTest(); 107*cdf0e10cSrcweir 108*cdf0e10cSrcweir MyChangeListener ml = new MyChangeListener(); 109*cdf0e10cSrcweir m_xPropSet.addPropertiesChangeListener(boundPropsToTest, ml); 110*cdf0e10cSrcweir 111*cdf0e10cSrcweir Object[] gValues = m_xPropSet.getPropertyValues(boundPropsToTest); 112*cdf0e10cSrcweir Object[] newValue = new Object[gValues.length]; 113*cdf0e10cSrcweir System.out.println("Trying to change all properties."); 114*cdf0e10cSrcweir for (int i = 0; i < boundPropsToTest.length; i++) 115*cdf0e10cSrcweir { 116*cdf0e10cSrcweir newValue[i] = ValueChanger.changePValue(gValues[i]); 117*cdf0e10cSrcweir } 118*cdf0e10cSrcweir try 119*cdf0e10cSrcweir { 120*cdf0e10cSrcweir m_xPropSet.setPropertyValues(boundPropsToTest, newValue); 121*cdf0e10cSrcweir } 122*cdf0e10cSrcweir catch (com.sun.star.beans.PropertyVetoException e) 123*cdf0e10cSrcweir { 124*cdf0e10cSrcweir fail("Exception occured while trying to change the properties."); 125*cdf0e10cSrcweir } 126*cdf0e10cSrcweir catch (com.sun.star.lang.IllegalArgumentException e) 127*cdf0e10cSrcweir { 128*cdf0e10cSrcweir fail("Exception occured while trying to change the properties."); 129*cdf0e10cSrcweir } 130*cdf0e10cSrcweir catch (com.sun.star.lang.WrappedTargetException e) 131*cdf0e10cSrcweir { 132*cdf0e10cSrcweir fail("Exception occured while trying to change the properties."); 133*cdf0e10cSrcweir } // end of try-catch 134*cdf0e10cSrcweir 135*cdf0e10cSrcweir assertTrue("Listener was not called.", ml.wasListenerCalled()); 136*cdf0e10cSrcweir m_xPropSet.removePropertiesChangeListener(ml); 137*cdf0e10cSrcweir } 138*cdf0e10cSrcweir 139*cdf0e10cSrcweir private String[] getBoundPropsToTest() 140*cdf0e10cSrcweir { 141*cdf0e10cSrcweir Property[] properties = m_xPropSet.getPropertySetInfo().getProperties(); 142*cdf0e10cSrcweir String[] testPropsNames = null; 143*cdf0e10cSrcweir 144*cdf0e10cSrcweir Vector<String> tNames = new Vector<String>(); 145*cdf0e10cSrcweir 146*cdf0e10cSrcweir for (int i = 0; i < properties.length; i++) 147*cdf0e10cSrcweir { 148*cdf0e10cSrcweir 149*cdf0e10cSrcweir Property property = properties[i]; 150*cdf0e10cSrcweir String name = property.Name; 151*cdf0e10cSrcweir boolean isWritable = ((property.Attributes 152*cdf0e10cSrcweir & PropertyAttribute.READONLY) == 0); 153*cdf0e10cSrcweir boolean isNotNull = ((property.Attributes 154*cdf0e10cSrcweir & PropertyAttribute.MAYBEVOID) == 0); 155*cdf0e10cSrcweir boolean isBound = ((property.Attributes 156*cdf0e10cSrcweir & PropertyAttribute.BOUND) != 0); 157*cdf0e10cSrcweir 158*cdf0e10cSrcweir if (isWritable && isNotNull && isBound) 159*cdf0e10cSrcweir { 160*cdf0e10cSrcweir tNames.add(name); 161*cdf0e10cSrcweir } 162*cdf0e10cSrcweir 163*cdf0e10cSrcweir } // endfor 164*cdf0e10cSrcweir 165*cdf0e10cSrcweir //get a array of bound properties 166*cdf0e10cSrcweir testPropsNames = new String[tNames.size()]; 167*cdf0e10cSrcweir testPropsNames = tNames.toArray(testPropsNames); 168*cdf0e10cSrcweir return testPropsNames; 169*cdf0e10cSrcweir } 170*cdf0e10cSrcweir 171*cdf0e10cSrcweir /** 172*cdf0e10cSrcweir * Listener implementation which sets a flag when 173*cdf0e10cSrcweir * listener was called. 174*cdf0e10cSrcweir */ 175*cdf0e10cSrcweir public class MyChangeListener implements XPropertiesChangeListener 176*cdf0e10cSrcweir { 177*cdf0e10cSrcweir 178*cdf0e10cSrcweir boolean propertiesChanged = false; 179*cdf0e10cSrcweir 180*cdf0e10cSrcweir public void propertiesChange(PropertyChangeEvent[] e) 181*cdf0e10cSrcweir { 182*cdf0e10cSrcweir propertiesChanged = true; 183*cdf0e10cSrcweir } 184*cdf0e10cSrcweir 185*cdf0e10cSrcweir public void disposing(EventObject obj) 186*cdf0e10cSrcweir { 187*cdf0e10cSrcweir } 188*cdf0e10cSrcweir 189*cdf0e10cSrcweir public boolean wasListenerCalled() 190*cdf0e10cSrcweir { 191*cdf0e10cSrcweir return propertiesChanged; 192*cdf0e10cSrcweir } 193*cdf0e10cSrcweir 194*cdf0e10cSrcweir public void reset() 195*cdf0e10cSrcweir { 196*cdf0e10cSrcweir propertiesChanged = false; 197*cdf0e10cSrcweir } 198*cdf0e10cSrcweir }; 199*cdf0e10cSrcweir 200*cdf0e10cSrcweir private XMultiServiceFactory getMSF() 201*cdf0e10cSrcweir { 202*cdf0e10cSrcweir final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager()); 203*cdf0e10cSrcweir return xMSF1; 204*cdf0e10cSrcweir } 205*cdf0e10cSrcweir 206*cdf0e10cSrcweir // setup and close connections 207*cdf0e10cSrcweir @BeforeClass 208*cdf0e10cSrcweir public static void setUpConnection() throws Exception 209*cdf0e10cSrcweir { 210*cdf0e10cSrcweir System.out.println("setUpConnection()"); 211*cdf0e10cSrcweir connection.setUp(); 212*cdf0e10cSrcweir } 213*cdf0e10cSrcweir 214*cdf0e10cSrcweir @AfterClass 215*cdf0e10cSrcweir public static void tearDownConnection() 216*cdf0e10cSrcweir throws InterruptedException, com.sun.star.uno.Exception 217*cdf0e10cSrcweir { 218*cdf0e10cSrcweir System.out.println("tearDownConnection()"); 219*cdf0e10cSrcweir connection.tearDown(); 220*cdf0e10cSrcweir } 221*cdf0e10cSrcweir private static final OfficeConnection connection = new OfficeConnection(); 222*cdf0e10cSrcweir } 223