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