2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 import java
.io
.PrintWriter
;
24 import lib
.TestEnvironment
;
25 import lib
.TestParameters
;
26 import util
.DefaultDsc
;
27 import util
.InstCreator
;
28 import util
.SOfficeFactory
;
30 import com
.sun
.star
.container
.XNameAccess
;
31 import com
.sun
.star
.lang
.XMultiServiceFactory
;
32 import com
.sun
.star
.text
.XAutoTextContainer
;
33 import com
.sun
.star
.text
.XAutoTextEntry
;
34 import com
.sun
.star
.text
.XAutoTextGroup
;
35 import com
.sun
.star
.text
.XText
;
36 import com
.sun
.star
.text
.XTextDocument
;
37 import com
.sun
.star
.text
.XTextRange
;
38 import com
.sun
.star
.uno
.AnyConverter
;
39 import com
.sun
.star
.uno
.Type
;
40 import com
.sun
.star
.uno
.UnoRuntime
;
41 import com
.sun
.star
.uno
.XInterface
;
45 * Test for object which is represented by service
46 * <code>com.sun.star.text.AutoTextEntry</code>.<p>
47 * Object implements the following interfaces :
49 * <li> <code>com::sun::star::text::XSimpleText</code></li>
50 * <li> <code>com::sun::star::text::XAutoTextEntry</code></li>
51 * <li> <code>com::sun::star::text::XTextRange</code></li>
52 * <li> <code>com::sun::star::text::XText</code></li>
54 * This object test <b> is NOT </b> designed to be run in several
55 * threads concurrently.
56 * @see com.sun.star.text.AutoTextEntry
57 * @see com.sun.star.text.AutoTextContainer
58 * @see com.sun.star.text.XSimpleText
59 * @see com.sun.star.text.XAutoTextEntry
60 * @see com.sun.star.text.XTextRange
61 * @see com.sun.star.text.XText
62 * @see ifc.text._XSimpleText
63 * @see ifc.text._XAutoTextEntry
64 * @see ifc.text._XTextRange
65 * @see ifc.text._XText
67 public class SwXAutoTextEntry
extends TestCase
{
68 XTextDocument xTextDoc
;
69 XAutoTextGroup oGroup
;
72 * Creates text document.
75 protected void initialize( TestParameters tParam
, PrintWriter log
) throws Exception
{
76 XMultiServiceFactory msf
= tParam
.getMSF();
77 SOfficeFactory SOF
= SOfficeFactory
.getFactory( msf
);
78 log
.println( "creating a textdocument" );
79 xTextDoc
= SOF
.createTextDoc( null );
83 * Removes added element from AutoTextGroup
86 protected void cleanup( TestParameters Param
, PrintWriter log
) {
88 if ( oGroup
.hasByName("NewEntryName") ) {
89 log
.println("Removing 'NewEntryName' element");
90 ((com
.sun
.star
.lang
.XComponent
) AnyConverter
.toObject(
91 new Type(com
.sun
.star
.lang
.XComponent
.class),
92 oGroup
.getByName("NewEntryName"))).dispose();
93 oGroup
.removeByName("NewEntryName");
95 } catch ( com
.sun
.star
.container
.NoSuchElementException e
) {
96 log
.println("Cannot remove TextEntry from group...");
97 e
.printStackTrace(log
);
98 } catch ( com
.sun
.star
.lang
.WrappedTargetException e
) {
99 log
.println("Cannot remove TextEntry from group...");
100 e
.printStackTrace(log
);
102 log
.println( "disposing xTextDoc " );
103 util
.DesktopTools
.closeDoc(xTextDoc
);
108 * Creating a TestEnvironment for the interfaces to be tested.
109 * Creates an instance of the service
110 * <code>com.sun.star.text.AutoTextContainer</code>, then selects the 'mytexts'
111 * group from the given container using <code>XNameAccess</code> interface,
112 * and inserts some text entry to this group. Then entry passed as test
114 * Object relations created :
116 * <li><code>'XTEXTINFO'</code> for
117 * {@link ifc.text._XText} : creates tables 6x4</li>
118 * <li><code>'TEXTDOC'</code> for
119 * {@link ifc.text._XAutoTextEntry} : text document</li>
123 protected TestEnvironment createTestEnvironment
124 (TestParameters Param
, PrintWriter log
) throws Exception
{
126 XAutoTextEntry oEntry
= null;
127 XAutoTextContainer oContainer
;
128 XInterface oObj
= null;
129 log
.println( "creating a test environment" );
130 XMultiServiceFactory myMSF
= Param
.getMSF();
131 Object oInst
= myMSF
.createInstance
132 ("com.sun.star.text.AutoTextContainer");
133 oContainer
= UnoRuntime
.queryInterface(XAutoTextContainer
.class,oInst
);
135 XNameAccess oContNames
= UnoRuntime
.queryInterface(XNameAccess
.class, oContainer
);
137 String contNames
[] = oContNames
.getElementNames();
138 for (int i
=0; i
< contNames
.length
; i
++){
139 log
.println("ContainerNames[ "+ i
+ "]: " + contNames
[i
]);
142 oObj
= (XInterface
) AnyConverter
.toObject(new Type(XInterface
.class),oContNames
.getByName("mytexts"));
144 oGroup
= UnoRuntime
.queryInterface
145 (XAutoTextGroup
.class, oObj
);
146 String
[] oENames
= oGroup
.getElementNames();
147 for (int i
=0; i
<oENames
.length
; i
++) {
148 log
.println("AutoTextEntryNames[" + i
+ "]: " + oENames
[i
]);
151 XText oText
= xTextDoc
.getText();
152 oText
.insertString(oText
.getStart(), "New AutoText", true);
153 XTextRange oTextRange
= oText
;
155 if ( oGroup
.hasByName("NewEntryName") ) {
156 oGroup
.removeByName("NewEntryName");
157 log
.println("Element 'NewEntryName' exists, removing...");
159 log
.println("Adding new element 'NewEntryName' to group...");
160 oGroup
.insertNewByName("NewEntryName", "NewEntryTitle", oTextRange
);
161 oEntry
= (XAutoTextEntry
) AnyConverter
.toObject(
162 new Type(XAutoTextEntry
.class),oGroup
.getByName("NewEntryName"));
163 } catch ( com
.sun
.star
.container
.ElementExistException e
) {
164 e
.printStackTrace(log
);
165 } catch ( com
.sun
.star
.container
.NoSuchElementException e
) {
166 e
.printStackTrace(log
);
167 } catch ( com
.sun
.star
.lang
.WrappedTargetException e
) {
168 e
.printStackTrace(log
);
169 } catch ( com
.sun
.star
.lang
.IllegalArgumentException e
) {
170 e
.printStackTrace(log
);
175 log
.println("Trying to use XText as TextRange in the method applyTo");
176 oEntry
.applyTo(oTextRange
);
178 oTextRange
= oText
.createTextCursor();
179 log
.println("Trying to use XTextCursor as TextRange in the method applyTo");
180 oEntry
.applyTo(oTextRange
);
182 log
.println( "creating a new environment for AutoTextEntry object" );
183 TestEnvironment tEnv
= new TestEnvironment( oObj
);
185 // adding relation for XText
186 DefaultDsc tDsc
= new DefaultDsc("com.sun.star.text.XTextContent",
187 "com.sun.star.text.TextField.DateTime");
188 log
.println( " adding InstCreator object" );
189 tEnv
.addObjRelation( "XTEXTINFO", new InstCreator( xTextDoc
, tDsc
) );
191 log
.println( "adding TextDocument as mod relation to environment" );
192 tEnv
.addObjRelation("TEXTDOC", xTextDoc
);
195 } // finish method getTestEnvironment
198 } // finish class SwXAutoTextEntry