1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ef39d40dSAndrew Rist  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ef39d40dSAndrew Rist  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19*ef39d40dSAndrew Rist  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package mod._sw;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import java.io.PrintWriter;
27cdf0e10cSrcweir 
28cdf0e10cSrcweir import lib.StatusException;
29cdf0e10cSrcweir import lib.TestCase;
30cdf0e10cSrcweir import lib.TestEnvironment;
31cdf0e10cSrcweir import lib.TestParameters;
32cdf0e10cSrcweir import util.DefaultDsc;
33cdf0e10cSrcweir import util.InstCreator;
34cdf0e10cSrcweir import util.SOfficeFactory;
35cdf0e10cSrcweir import com.sun.star.container.XNameAccess;
36cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
37cdf0e10cSrcweir import com.sun.star.text.XAutoTextContainer;
38cdf0e10cSrcweir import com.sun.star.text.XAutoTextEntry;
39cdf0e10cSrcweir import com.sun.star.text.XAutoTextGroup;
40cdf0e10cSrcweir import com.sun.star.text.XText;
41cdf0e10cSrcweir import com.sun.star.text.XTextDocument;
42cdf0e10cSrcweir import com.sun.star.text.XTextRange;
43cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
44cdf0e10cSrcweir import com.sun.star.uno.Type;
45cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
46cdf0e10cSrcweir import com.sun.star.uno.XInterface;
47cdf0e10cSrcweir 
48cdf0e10cSrcweir 
49cdf0e10cSrcweir /**
50cdf0e10cSrcweir  * Test for object which is represented by service
51cdf0e10cSrcweir  * <code>com.sun.star.text.AutoTextEntry</code>.<p>
52cdf0e10cSrcweir  * Object implements the following interfaces :
53cdf0e10cSrcweir  * <ul>
54cdf0e10cSrcweir  *  <li> <code>com::sun::star::text::XSimpleText</code></li>
55cdf0e10cSrcweir  *  <li> <code>com::sun::star::text::XAutoTextEntry</code></li>
56cdf0e10cSrcweir  *  <li> <code>com::sun::star::text::XTextRange</code></li>
57cdf0e10cSrcweir  *  <li> <code>com::sun::star::text::XText</code></li>
58cdf0e10cSrcweir  * </ul> <p>
59cdf0e10cSrcweir  * This object test <b> is NOT </b> designed to be run in several
60cdf0e10cSrcweir  * threads concurently.
61cdf0e10cSrcweir  * @see com.sun.star.text.AutoTextEntry
62cdf0e10cSrcweir  * @see com.sun.star.text.AutoTextContainer
63cdf0e10cSrcweir  * @see com.sun.star.text.XSimpleText
64cdf0e10cSrcweir  * @see com.sun.star.text.XAutoTextEntry
65cdf0e10cSrcweir  * @see com.sun.star.text.XTextRange
66cdf0e10cSrcweir  * @see com.sun.star.text.XText
67cdf0e10cSrcweir  * @see ifc.text._XSimpleText
68cdf0e10cSrcweir  * @see ifc.text._XAutoTextEntry
69cdf0e10cSrcweir  * @see ifc.text._XTextRange
70cdf0e10cSrcweir  * @see ifc.text._XText
71cdf0e10cSrcweir  */
72cdf0e10cSrcweir public class SwXAutoTextEntry extends TestCase {
73cdf0e10cSrcweir     XTextDocument xTextDoc;
74cdf0e10cSrcweir     XAutoTextGroup oGroup;
75cdf0e10cSrcweir 
76cdf0e10cSrcweir     /**
77cdf0e10cSrcweir      * Creates text document.
78cdf0e10cSrcweir      */
initialize( TestParameters tParam, PrintWriter log )79cdf0e10cSrcweir     protected void initialize( TestParameters tParam, PrintWriter log ) {
80cdf0e10cSrcweir         XMultiServiceFactory msf = (XMultiServiceFactory) tParam.getMSF();
81cdf0e10cSrcweir         SOfficeFactory SOF = SOfficeFactory.getFactory( msf );
82cdf0e10cSrcweir         try {
83cdf0e10cSrcweir             log.println( "creating a textdocument" );
84cdf0e10cSrcweir             xTextDoc = SOF.createTextDoc( null );
85cdf0e10cSrcweir         } catch ( com.sun.star.uno.Exception e ) {
86cdf0e10cSrcweir             e.printStackTrace( log );
87cdf0e10cSrcweir             throw new StatusException( "Couldn't create document", e );
88cdf0e10cSrcweir         }
89cdf0e10cSrcweir     }
90cdf0e10cSrcweir 
91cdf0e10cSrcweir     /**
92cdf0e10cSrcweir      *  Removes added element from AutoTextGroup
93cdf0e10cSrcweir      */
cleanup( TestParameters Param, PrintWriter log)94cdf0e10cSrcweir     protected void cleanup( TestParameters Param, PrintWriter log) {
95cdf0e10cSrcweir         try {
96cdf0e10cSrcweir             if ( oGroup.hasByName("NewEntryName") ) {
97cdf0e10cSrcweir                 log.println("Removing 'NewEntryName' element");
98cdf0e10cSrcweir                 oGroup.removeByName("NewEntryName");
99cdf0e10cSrcweir             }
100cdf0e10cSrcweir         } catch ( com.sun.star.container.NoSuchElementException e ) {
101cdf0e10cSrcweir             log.println("Cannot remove TextEntry from group...");
102cdf0e10cSrcweir             e.printStackTrace(log);
103cdf0e10cSrcweir         }
104cdf0e10cSrcweir         log.println( "disposing xTextDoc " );
105cdf0e10cSrcweir         util.DesktopTools.closeDoc(xTextDoc);
106cdf0e10cSrcweir     }
107cdf0e10cSrcweir 
108cdf0e10cSrcweir 
109cdf0e10cSrcweir     /**
110cdf0e10cSrcweir      * Creating a Testenvironment for the interfaces to be tested.
111cdf0e10cSrcweir      * Creates an instance of the service
112cdf0e10cSrcweir      * <code>com.sun.star.text.AutoTextContainer</code>, then selects the 'mytexts'
113cdf0e10cSrcweir      * group from the given container using <code>XNameAccess</code> interface,
114cdf0e10cSrcweir      * and inserts some text entry to this group. Then entry passed as test
115cdf0e10cSrcweir      * component.<p>
116cdf0e10cSrcweir      *     Object relations created :
117cdf0e10cSrcweir      * <ul>
118cdf0e10cSrcweir      *  <li><code>'XTEXTINFO'</code> for
119cdf0e10cSrcweir      *    {@link ifc.text._XText} : creates tables 6x4</li>
120cdf0e10cSrcweir      *  <li><code>'TEXTDOC'</code> for
121cdf0e10cSrcweir      *    {@link ifc.text._XAutoTextEntry} : text document</li>
122cdf0e10cSrcweir      * </ul>
123cdf0e10cSrcweir      */
createTestEnvironment(TestParameters Param, PrintWriter log)124cdf0e10cSrcweir     protected synchronized TestEnvironment createTestEnvironment
125cdf0e10cSrcweir             (TestParameters Param, PrintWriter log) {
126cdf0e10cSrcweir 
127cdf0e10cSrcweir         XAutoTextEntry oEntry = null;
128cdf0e10cSrcweir         XAutoTextContainer oContainer;
129cdf0e10cSrcweir         XInterface oObj = null;
130cdf0e10cSrcweir         int n = 0;
131cdf0e10cSrcweir         int nCount = 0;
132cdf0e10cSrcweir 
133cdf0e10cSrcweir         log.println( "creating a test environment" );
134cdf0e10cSrcweir         try {
135cdf0e10cSrcweir             XMultiServiceFactory myMSF = (XMultiServiceFactory)Param.getMSF();
136cdf0e10cSrcweir             Object oInst = myMSF.createInstance
137cdf0e10cSrcweir                     ("com.sun.star.text.AutoTextContainer");
138cdf0e10cSrcweir             oContainer = (XAutoTextContainer)
139cdf0e10cSrcweir             UnoRuntime.queryInterface(XAutoTextContainer.class,oInst);
140cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {
141cdf0e10cSrcweir             e.printStackTrace(log);
142cdf0e10cSrcweir             throw new StatusException("Couldn't create AutoTextContainer", e);
143cdf0e10cSrcweir         }
144cdf0e10cSrcweir 
145cdf0e10cSrcweir         XNameAccess oContNames = (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class, oContainer);
146cdf0e10cSrcweir 
147cdf0e10cSrcweir         String contNames[] = oContNames.getElementNames();
148cdf0e10cSrcweir         for (int i =0; i < contNames.length; i++){
149cdf0e10cSrcweir             log.println("ContainerNames[ "+ i + "]: " + contNames[i]);
150cdf0e10cSrcweir         }
151cdf0e10cSrcweir 
152cdf0e10cSrcweir         try{
153cdf0e10cSrcweir             oObj = (XInterface) AnyConverter.toObject(new Type(XInterface.class),oContNames.getByName("mytexts"));
154cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {
155cdf0e10cSrcweir             e.printStackTrace(log);
156cdf0e10cSrcweir             throw new StatusException("Couldn't get AutoTextGroup", e);
157cdf0e10cSrcweir         }
158cdf0e10cSrcweir 
159cdf0e10cSrcweir         oGroup = (XAutoTextGroup) UnoRuntime.queryInterface
160cdf0e10cSrcweir                 (XAutoTextGroup.class, oObj);
161cdf0e10cSrcweir         String[] oENames = oGroup.getElementNames();
162cdf0e10cSrcweir         for (int i=0; i<oENames.length; i++) {
163cdf0e10cSrcweir             log.println("AutoTextEntryNames[" + i + "]: " + oENames[i]);
164cdf0e10cSrcweir         }
165cdf0e10cSrcweir 
166cdf0e10cSrcweir         XText oText = xTextDoc.getText();
167cdf0e10cSrcweir         oText.insertString(oText.getStart(), "New AutoText", true);
168cdf0e10cSrcweir         XTextRange oTextRange = (XTextRange) oText;
169cdf0e10cSrcweir         try {
170cdf0e10cSrcweir             if ( oGroup.hasByName("NewEntryName") ) {
171cdf0e10cSrcweir                 oGroup.removeByName("NewEntryName");
172cdf0e10cSrcweir                 log.println("Element 'NewEntryName' exists, removing...");
173cdf0e10cSrcweir             }
174cdf0e10cSrcweir             log.println("Adding new element 'NewEntryName' to group...");
175cdf0e10cSrcweir             oGroup.insertNewByName("NewEntryName", "NewEntryTitle", oTextRange);
176cdf0e10cSrcweir             oEntry = (XAutoTextEntry) AnyConverter.toObject(
177cdf0e10cSrcweir                     new Type(XAutoTextEntry.class),oGroup.getByName("NewEntryName"));
178cdf0e10cSrcweir         } catch ( com.sun.star.container.ElementExistException e ) {
179cdf0e10cSrcweir             e.printStackTrace(log);
180cdf0e10cSrcweir         } catch ( com.sun.star.container.NoSuchElementException e ) {
181cdf0e10cSrcweir             e.printStackTrace(log);
182cdf0e10cSrcweir         } catch ( com.sun.star.lang.WrappedTargetException e ) {
183cdf0e10cSrcweir             e.printStackTrace(log);
184cdf0e10cSrcweir         } catch ( com.sun.star.lang.IllegalArgumentException e ) {
185cdf0e10cSrcweir             e.printStackTrace(log);
186cdf0e10cSrcweir         }
187cdf0e10cSrcweir 
188cdf0e10cSrcweir         oObj = oEntry;
189cdf0e10cSrcweir 
190cdf0e10cSrcweir         log.println("Trying to use XText as TextRange in the method applyTo");
191cdf0e10cSrcweir         oEntry.applyTo(oTextRange);
192cdf0e10cSrcweir 
193cdf0e10cSrcweir         oTextRange = oText.createTextCursor();
194cdf0e10cSrcweir         log.println("Trying to use XTextCursor as TextRange in the method applyTo");
195cdf0e10cSrcweir         oEntry.applyTo(oTextRange);
196cdf0e10cSrcweir 
197cdf0e10cSrcweir         log.println( "creating a new environment for AutoTextEntry object" );
198cdf0e10cSrcweir         TestEnvironment tEnv = new TestEnvironment( oObj );
199cdf0e10cSrcweir 
200cdf0e10cSrcweir         // adding relation for XText
201cdf0e10cSrcweir         DefaultDsc tDsc = new DefaultDsc("com.sun.star.text.XTextContent",
202cdf0e10cSrcweir                 "com.sun.star.text.TextField.DateTime");
203cdf0e10cSrcweir         log.println( "    adding InstCreator object" );
204cdf0e10cSrcweir         tEnv.addObjRelation( "XTEXTINFO", new InstCreator( xTextDoc, tDsc ) );
205cdf0e10cSrcweir 
206cdf0e10cSrcweir         log.println( "adding TextDocument as mod relation to environment" );
207cdf0e10cSrcweir         tEnv.addObjRelation("TEXTDOC", xTextDoc);
208cdf0e10cSrcweir 
209cdf0e10cSrcweir         return tEnv;
210cdf0e10cSrcweir     } // finish method getTestEnvironment
211cdf0e10cSrcweir 
212cdf0e10cSrcweir 
213cdf0e10cSrcweir }    // finish class SwXAutoTextEntry
214