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 
24 package integration.forms;
25 
26 import com.sun.star.beans.PropertyValue;
27 import com.sun.star.beans.XPropertySet;
28 import com.sun.star.form.binding.IncompatibleTypesException;
29 import com.sun.star.form.binding.XBindableValue;
30 import com.sun.star.form.binding.XValueBinding;
31 import com.sun.star.frame.XStorable;
32 import com.sun.star.io.IOException;
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 com.sun.star.xml.dom.XNode;
38 import com.sun.star.xsd.DataTypeClass;
39 import java.io.File;
40 import org.openoffice.xforms.Instance;
41 import org.openoffice.xforms.Model;
42 import org.openoffice.xforms.XMLDocument;
43 
44 public class XMLFormSettings extends complexlib.ComplexTestCase
45 {
46     private XMultiServiceFactory    m_orb;
47     private XMLDocument             m_document;
48     private Model                   m_defaultModel;
49     private FormLayer               m_formLayer;
50     private XPropertySet            m_stringBinding;
51     private XPropertySet            m_booleanBinding;
52     private XPropertySet            m_dateBinding;
53 
54     /** Creates a new instance of XMLFormSettings */
XMLFormSettings()55     public XMLFormSettings()
56     {
57     }
58 
59     /* ------------------------------------------------------------------ */
getTestMethodNames()60     public String[] getTestMethodNames()
61     {
62         return new String[] {
63             "checkExternalData"
64         };
65     }
66 
67     /* ------------------------------------------------------------------ */
getTestObjectName()68     public String getTestObjectName()
69     {
70         return "Form Control Spreadsheet Cell Binding Test";
71     }
72 
73     /* ------------------------------------------------------------------ */
before()74     public void before() throws java.lang.Exception
75     {
76         // create the document and assign related members
77         m_orb = (XMultiServiceFactory)param.getMSF();
78         m_document = new XMLDocument( m_orb );
79         m_formLayer = new FormLayer( m_document );
80 
81         // create a simple structure in the DOM tree: an element with two attributes
82         String[] modelNames = m_document.getXFormModelNames();
83         m_defaultModel = m_document.getXFormModel( modelNames[0] );
84         final Instance defaultInstance = m_defaultModel.getDefaultInstance();
85         // remove the default root node
86         defaultInstance.removeNode( "instanceData" );
87         // create test structures
88         XNode stringElement = defaultInstance.createElement( "stringElement" );
89         XNode booleanAttrib = defaultInstance.createAttribute( stringElement, "booleanAttribute", "true" );
90         XNode dateAttrib = defaultInstance.createAttribute( stringElement, "dateAttribute" );
91 
92         assure( "booleanAttrib's parent is wrong",
93             UnoRuntime.areSame( stringElement, booleanAttrib.getParentNode() ) );
94         assure( "dateAttrib's parent is wrong",
95             UnoRuntime.areSame( stringElement, dateAttrib.getParentNode() ) );
96 
97         // also create bindings for the element and its attributes, of the proper type
98         m_stringBinding = m_defaultModel.createBindingForNode( stringElement, DataTypeClass.STRING );
99         m_booleanBinding = m_defaultModel.createBindingForNode( booleanAttrib, DataTypeClass.BOOLEAN );
100         m_dateBinding = m_defaultModel.createBindingForNode( dateAttrib, DataTypeClass.DATE );
101 
102         // TODO: set up the bindings so that the date bindings is relevant if and only if
103         // the boolean value is true
104 
105         // store the document
106         File tempFile = File.createTempFile( "xmlforms", ".odt" );
107         tempFile.deleteOnExit();
108         String fileURL = tempFile.toURL().toExternalForm();
109         XStorable store = (XStorable)UnoRuntime.queryInterface( XStorable.class,
110             m_document.getDocument() );
111         store.storeAsURL( fileURL, new PropertyValue[] {} );
112         assure( "document still modified after saving it", !m_document.isModified() );
113     }
114 
115     /* ------------------------------------------------------------------ */
after()116     public void after() throws com.sun.star.uno.Exception, java.lang.Exception
117     {
118         impl_closeDocument();
119     }
120 
121     /* ------------------------------------------------------------------ */
impl_closeDocument()122     private void impl_closeDocument() throws CloseVetoException
123     {
124         if ( m_document != null )
125         {
126             XCloseable closeDoc = (XCloseable)UnoRuntime.queryInterface( XCloseable.class,
127                 m_document.getDocument() );
128             closeDoc.close( true );
129         }
130     }
131 
132     /* ------------------------------------------------------------------ */
impl_bind( XPropertySet _control, XPropertySet _binding )133     private static void impl_bind( XPropertySet _control, XPropertySet _binding ) throws IncompatibleTypesException
134     {
135         XBindableValue bindableControl = (XBindableValue)UnoRuntime.queryInterface(
136             XBindableValue.class, _control );
137         XValueBinding binding = (XValueBinding)UnoRuntime.queryInterface(
138             XValueBinding.class, _binding );
139         bindableControl.setValueBinding( binding );
140     }
141 
142     /* ------------------------------------------------------------------ */
143     /** checks if master-detail relationships including multiple keys work
144      */
checkExternalData()145     public void checkExternalData() throws com.sun.star.uno.Exception, java.lang.Exception
146     {
147         // some controls
148         XPropertySet stringControl = m_formLayer.createLabeledControl(
149             "DatabaseTextField", "Task", 10, 10, 6 );
150         impl_bind( stringControl, m_stringBinding );
151 
152         XPropertySet booleanControl = m_formLayer.createControlAndShape(
153             "DatabaseCheckBox", 35, 18, 25, 6 );
154         booleanControl.setPropertyValue( "Label", "has due date" );
155         impl_bind( booleanControl, m_booleanBinding );
156 
157         XPropertySet dateControl = m_formLayer.createControlAndShape(
158             "DatabaseDateField", 40, 26, 25, 6 );
159         dateControl.setPropertyValue( "Dropdown", new Boolean( true ) );
160         impl_bind( dateControl, m_dateBinding );
161 
162         m_document.getCurrentView( ).toggleFormDesignMode( );
163 
164         // ensure the model is set up as containing "document-internal" data
165         m_defaultModel.setIsDocumentInternalData( true );
166         assure( "setting up the document to contain 'internal data' failed",
167             m_defaultModel.getIsDocumentInternalData() );
168         impl_storeDocument();
169 
170         // okay, here we go ...
171         // what this particular test is about is to check whether we can set up the model
172         // so that any changes to any controls bound to any data in this model actually marks
173         // the containing document as modified
174         m_formLayer.userTextInput( stringControl, "don't break this test" );
175         assure( "model data changed, but document is not modified",
176             m_document.isModified() );
177 
178         // TODO: do this with the other control/binding types, too
179 
180         // no the other way round: set up the model to contain "document-external" data
181         m_defaultModel.setIsDocumentInternalData( false );
182         assure( "setting up the document to contain 'internal data' failed",
183             !m_defaultModel.getIsDocumentInternalData() );
184         impl_storeDocument();
185 
186         // and check that now, changes in the controls / model data are not reflected in
187         // document's modified state
188         m_formLayer.userTextInput( stringControl, "(or any other test, that is)" );
189         assure( "model data changed, but document is modified",
190             !m_document.isModified() );
191 
192         // .................................................................
193         // finally, check whether the flag survives loading and saving
194         Model internalDataModel = m_document.addXFormModel( "internalData" );
195         internalDataModel.setIsDocumentInternalData( true );
196         Model externalDataModel = m_document.addXFormModel( "externalData" );
197         externalDataModel.setIsDocumentInternalData( false );
198 
199         impl_storeDocument();
200         m_document.reload();
201 
202         internalDataModel = m_document.getXFormModel( "internalData" );
203         externalDataModel = m_document.getXFormModel( "externalData" );
204 
205         assure( "setting up a model to contain 'internal data' did not survive reloading",
206             internalDataModel.getIsDocumentInternalData() );
207         assure( "setting up a model to contain 'external data' did not survive reloading",
208             !externalDataModel.getIsDocumentInternalData() );
209     }
210 
211     /* ------------------------------------------------------------------ */
212     /** stores our document
213      * @throws com.sun.star.io.IOException
214      */
impl_storeDocument()215     private void impl_storeDocument() throws IOException
216     {
217         XStorable store = (XStorable)UnoRuntime.queryInterface( XStorable.class,
218             m_document.getDocument() );
219         store.store();
220         assure( "document still modified after saving it", !m_document.isModified() );
221     }
222 }
223