1*b9b79128SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*b9b79128SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*b9b79128SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*b9b79128SAndrew Rist  * distributed with this work for additional information
6*b9b79128SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*b9b79128SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*b9b79128SAndrew Rist  * "License"); you may not use this file except in compliance
9*b9b79128SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*b9b79128SAndrew Rist  *
11*b9b79128SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*b9b79128SAndrew Rist  *
13*b9b79128SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*b9b79128SAndrew Rist  * software distributed under the License is distributed on an
15*b9b79128SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b9b79128SAndrew Rist  * KIND, either express or implied.  See the License for the
17*b9b79128SAndrew Rist  * specific language governing permissions and limitations
18*b9b79128SAndrew Rist  * under the License.
19*b9b79128SAndrew Rist  *
20*b9b79128SAndrew Rist  *************************************************************/
21*b9b79128SAndrew Rist 
22*b9b79128SAndrew Rist 
23cdf0e10cSrcweir package integration.forms;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import com.sun.star.beans.PropertyAttribute;
26cdf0e10cSrcweir import com.sun.star.beans.PropertyChangeEvent;
27cdf0e10cSrcweir import com.sun.star.beans.PropertyExistException;
28cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
29cdf0e10cSrcweir import com.sun.star.beans.PropertyVetoException;
30cdf0e10cSrcweir import com.sun.star.beans.UnknownPropertyException;
31cdf0e10cSrcweir import com.sun.star.beans.XPropertyChangeListener;
32cdf0e10cSrcweir import com.sun.star.beans.XPropertyContainer;
33cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
34cdf0e10cSrcweir import com.sun.star.beans.XPropertySetInfo;
35cdf0e10cSrcweir import com.sun.star.frame.XStorable;
36cdf0e10cSrcweir import com.sun.star.lang.EventObject;
37cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
38cdf0e10cSrcweir 
39cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir import com.sun.star.util.XCloseable;
42cdf0e10cSrcweir 
43cdf0e10cSrcweir public class FormPropertyBags extends complexlib.ComplexTestCase implements XPropertyChangeListener
44cdf0e10cSrcweir {
45cdf0e10cSrcweir     private DocumentHelper          m_document;
46cdf0e10cSrcweir     private FormLayer               m_formLayer;
47cdf0e10cSrcweir     private XMultiServiceFactory    m_orb;
48cdf0e10cSrcweir 
49cdf0e10cSrcweir     private PropertyChangeEvent     m_propertyChangeEvent;
50cdf0e10cSrcweir 
51cdf0e10cSrcweir     /** Creates a new instance of FormPropertyBags */
FormPropertyBags()52cdf0e10cSrcweir     public FormPropertyBags()
53cdf0e10cSrcweir     {
54cdf0e10cSrcweir         m_propertyChangeEvent = new PropertyChangeEvent();
55cdf0e10cSrcweir     }
56cdf0e10cSrcweir 
57cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
getTestMethodNames()58cdf0e10cSrcweir     public String[] getTestMethodNames()
59cdf0e10cSrcweir     {
60cdf0e10cSrcweir         return new String[] {
61cdf0e10cSrcweir             "checkSomething"
62cdf0e10cSrcweir         };
63cdf0e10cSrcweir     }
64cdf0e10cSrcweir 
65cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
getTestObjectName()66cdf0e10cSrcweir     public String getTestObjectName()
67cdf0e10cSrcweir     {
68cdf0e10cSrcweir         return "Form Component Property Bag Test";
69cdf0e10cSrcweir     }
70cdf0e10cSrcweir 
71cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
before()72cdf0e10cSrcweir     public void before() throws com.sun.star.uno.Exception, java.lang.Exception
73cdf0e10cSrcweir     {
74cdf0e10cSrcweir         m_orb = (XMultiServiceFactory)param.getMSF();
75cdf0e10cSrcweir         m_document = DocumentHelper.blankTextDocument( m_orb );
76cdf0e10cSrcweir         m_formLayer = new FormLayer( m_document );
77cdf0e10cSrcweir     }
78cdf0e10cSrcweir 
79cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
impl_closeDoc()80cdf0e10cSrcweir     private void impl_closeDoc() throws com.sun.star.uno.Exception
81cdf0e10cSrcweir     {
82cdf0e10cSrcweir         if ( m_document != null )
83cdf0e10cSrcweir         {
84cdf0e10cSrcweir             XCloseable closeDoc = UnoRuntime.queryInterface( XCloseable.class, m_document.getDocument() );
85cdf0e10cSrcweir             closeDoc.close( true );
86cdf0e10cSrcweir         }
87cdf0e10cSrcweir     }
88cdf0e10cSrcweir 
89cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
after()90cdf0e10cSrcweir     public void after() throws com.sun.star.uno.Exception, java.lang.Exception
91cdf0e10cSrcweir     {
92cdf0e10cSrcweir         impl_closeDoc();
93cdf0e10cSrcweir     }
94cdf0e10cSrcweir 
95cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
checkSomething()96cdf0e10cSrcweir     public void checkSomething() throws com.sun.star.uno.Exception, java.lang.Exception
97cdf0e10cSrcweir     {
98cdf0e10cSrcweir         XPropertySet textFieldModel = m_formLayer.createControlAndShape( "DatabaseTextField", 10, 10, 25, 6 );
99cdf0e10cSrcweir 
100cdf0e10cSrcweir         // check whether adding new properties is successful
101cdf0e10cSrcweir         XPropertyContainer propContainer = UnoRuntime.queryInterface(
102cdf0e10cSrcweir             XPropertyContainer.class, textFieldModel );
103cdf0e10cSrcweir         assure("XPropertyContainer not supported!", propContainer != null );
104cdf0e10cSrcweir 
105cdf0e10cSrcweir         propContainer.addProperty( "SomeBoundText", PropertyAttribute.BOUND, "InitialBoundText" );
106cdf0e10cSrcweir         propContainer.addProperty( "SomeTransientText", PropertyAttribute.TRANSIENT, "InitialTransientProperty" );
107cdf0e10cSrcweir         propContainer.addProperty( "SomeReadonlyText", PropertyAttribute.READONLY, "InitialReadonlyText" );
108cdf0e10cSrcweir         propContainer.addProperty( "SomeNumericValue", PropertyAttribute.BOUND, new Integer( 42 ) );
109cdf0e10cSrcweir 
110cdf0e10cSrcweir         XPropertySetInfo propertyInfo = textFieldModel.getPropertySetInfo();
111cdf0e10cSrcweir         assure( "Per service definition, dynamic properties are expected to be forced to be removeable",
112cdf0e10cSrcweir             ( propertyInfo.getPropertyByName("SomeBoundText").Attributes & PropertyAttribute.REMOVEABLE ) != 0 );
113cdf0e10cSrcweir 
114cdf0e10cSrcweir         // a second addition of a property with an existent name should be rejected
115cdf0e10cSrcweir         boolean caughtExpected = false;
116cdf0e10cSrcweir         try { propContainer.addProperty( "SomeBoundText", PropertyAttribute.BOUND, "InitialBoundText" ); }
117cdf0e10cSrcweir         catch( PropertyExistException e ) { caughtExpected = true; }
118cdf0e10cSrcweir         catch( Exception e ) { }
119cdf0e10cSrcweir         assure( "repeated additions of a property with the same name should be rejected",
120cdf0e10cSrcweir             caughtExpected );
121cdf0e10cSrcweir 
122cdf0e10cSrcweir         // check whether the properties are bound as expected
123cdf0e10cSrcweir         impl_checkPropertyValueNotification( textFieldModel );
124cdf0e10cSrcweir 
125cdf0e10cSrcweir         // check property value persistence
126cdf0e10cSrcweir         impl_checkPropertyPersistence();
127cdf0e10cSrcweir     }
128cdf0e10cSrcweir 
129cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
impl_checkPropertyValueNotification( XPropertySet _controlModel )130cdf0e10cSrcweir     private void impl_checkPropertyValueNotification( XPropertySet _controlModel ) throws com.sun.star.uno.Exception
131cdf0e10cSrcweir     {
132cdf0e10cSrcweir         _controlModel.addPropertyChangeListener( "", this );
133cdf0e10cSrcweir 
134cdf0e10cSrcweir         _controlModel.setPropertyValue( "SomeBoundText", "ChangedBoundText" );
135cdf0e10cSrcweir         assure( "changes in the bound property are not properly notified",
136cdf0e10cSrcweir                 m_propertyChangeEvent.PropertyName.equals( "SomeBoundText" )
137cdf0e10cSrcweir             &&  m_propertyChangeEvent.OldValue.equals( "InitialBoundText" )
138cdf0e10cSrcweir             &&  m_propertyChangeEvent.NewValue.equals( "ChangedBoundText" ) );
139cdf0e10cSrcweir 
140cdf0e10cSrcweir         m_propertyChangeEvent = null;
141cdf0e10cSrcweir         _controlModel.setPropertyValue( "SomeTransientText", "ChangedTransientText" );
142cdf0e10cSrcweir         assure( "changes in non-bound properties should not be notified",
143cdf0e10cSrcweir             m_propertyChangeEvent == null );
144cdf0e10cSrcweir 
145cdf0e10cSrcweir         boolean caughtExpected = false;
146cdf0e10cSrcweir         try { _controlModel.setPropertyValue( "SomeReadonlyText", "ChangedReadonlyText" ); }
147cdf0e10cSrcweir         catch( PropertyVetoException e ) { caughtExpected = true; }
148cdf0e10cSrcweir         catch( Exception e ) { }
149cdf0e10cSrcweir         assure( "trying to write a read-only property did not give the expected result",
150cdf0e10cSrcweir             caughtExpected );
151cdf0e10cSrcweir 
152cdf0e10cSrcweir         _controlModel.removePropertyChangeListener( "", this );
153cdf0e10cSrcweir     }
154cdf0e10cSrcweir 
155cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
impl_checkPropertyPersistence()156cdf0e10cSrcweir     private void impl_checkPropertyPersistence() throws com.sun.star.uno.Exception
157cdf0e10cSrcweir     {
158cdf0e10cSrcweir         // store the document
159cdf0e10cSrcweir         XStorable store = UnoRuntime.queryInterface( XStorable.class, m_document.getDocument() );
160cdf0e10cSrcweir         String documentURL = util.utils.getOfficeTemp( m_orb ) + "document.odt";
161cdf0e10cSrcweir         PropertyValue[] storeArguments = new PropertyValue[] { new PropertyValue() };
162cdf0e10cSrcweir         storeArguments[0].Name = "FilterName";
163cdf0e10cSrcweir         storeArguments[0].Value = "writer8";
164cdf0e10cSrcweir         store.storeAsURL( documentURL, storeArguments );
165cdf0e10cSrcweir 
166cdf0e10cSrcweir         // close and re-load it
167cdf0e10cSrcweir         impl_closeDoc();
168cdf0e10cSrcweir 
169cdf0e10cSrcweir         m_document = DocumentHelper.loadDocument( m_orb, documentURL );
170cdf0e10cSrcweir         m_formLayer = new FormLayer( m_document );
171cdf0e10cSrcweir 
172cdf0e10cSrcweir         XPropertySet textFieldModel = m_formLayer.getControlModel( new int[] { 0, 0 } );
173cdf0e10cSrcweir 
174cdf0e10cSrcweir         // all persistent properties should have the expected values
175cdf0e10cSrcweir         assure( "persistent properties did not survive reload (1)!", ((String)textFieldModel.getPropertyValue( "SomeBoundText" )).equals( "ChangedBoundText" ) );
176cdf0e10cSrcweir         assure( "persistent properties did not survive reload (2)!", ((String)textFieldModel.getPropertyValue( "SomeReadonlyText" )).equals( "InitialReadonlyText" ) );
177cdf0e10cSrcweir //        assure( "persistent properties did not survive reload (3)!", ((Integer)textFieldModel.getPropertyValue( "SomeNumericValue" )).equals( new Integer( 42 ) ) );
178cdf0e10cSrcweir             // cannot check this until the types really survice - at the moment, integers are converted to doubles ...
179cdf0e10cSrcweir 
180cdf0e10cSrcweir         // the transient property should not have survived
181cdf0e10cSrcweir         boolean caughtExpected = false;
182cdf0e10cSrcweir         try { textFieldModel.getPropertyValue( "SomeTransientText" ); }
183cdf0e10cSrcweir         catch( UnknownPropertyException e ) { caughtExpected = true; }
184cdf0e10cSrcweir         assure( "transient property did survive reload!", caughtExpected );
185cdf0e10cSrcweir 
186cdf0e10cSrcweir         // There would be more things to check.
187cdf0e10cSrcweir         // For instance, it would be desirable of the property attributes would have survived
188cdf0e10cSrcweir         // the reload, and the property defaults (XPropertyState).
189cdf0e10cSrcweir         // However, the file format currently doesn't allow for this, so those information
190cdf0e10cSrcweir         // is lost when saving the document.
191cdf0e10cSrcweir     }
192cdf0e10cSrcweir 
193cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
propertyChange(PropertyChangeEvent _propertyChangeEvent)194cdf0e10cSrcweir     public void propertyChange(PropertyChangeEvent _propertyChangeEvent)
195cdf0e10cSrcweir     {
196cdf0e10cSrcweir         m_propertyChangeEvent = _propertyChangeEvent;
197cdf0e10cSrcweir     }
198cdf0e10cSrcweir 
199cdf0e10cSrcweir     /* ------------------------------------------------------------------ */
disposing(EventObject eventObject)200cdf0e10cSrcweir     public void disposing(EventObject eventObject)
201cdf0e10cSrcweir     {
202cdf0e10cSrcweir         // not interested in
203cdf0e10cSrcweir     }
204cdf0e10cSrcweir }
205