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
.util
.HashMap
;
22 import java
.util
.Iterator
;
24 import lib
.MultiPropertyTest
;
25 import util
.ValueChanger
;
28 import com
.sun
.star
.beans
.XPropertySet
;
29 import com
.sun
.star
.container
.XNameContainer
;
30 import com
.sun
.star
.uno
.AnyConverter
;
31 import com
.sun
.star
.uno
.Type
;
32 import com
.sun
.star
.xml
.AttributeData
;
36 * Testing <code>com.sun.star.style.CharacterProperties</code>
37 * service properties :
39 * <li><code> CharFontName</code></li>
40 * <li><code> CharFontStyleName</code></li>
41 * <li><code> CharFontFamily</code></li>
42 * <li><code> CharFontCharSet</code></li>
43 * <li><code> CharFontPitch</code></li>
44 * <li><code> CharColor</code></li>
45 * <li><code> CharEscapement</code></li>
46 * <li><code> CharHeight</code></li>
47 * <li><code> CharUnderline</code></li>
48 * <li><code> CharWeight</code></li>
49 * <li><code> CharPosture</code></li>
50 * <li><code> CharAutoKerning</code></li>
51 * <li><code> CharBackColor</code></li>
52 * <li><code> CharBackTransparent</code></li>
53 * <li><code> CharCaseMap</code></li>
54 * <li><code> CharCrossedOut</code></li>
55 * <li><code> CharFlash</code></li>
56 * <li><code> CharStrikeout</code></li>
57 * <li><code> CharWordMode</code></li>
58 * <li><code> CharKerning</code></li>
59 * <li><code> CharLocale</code></li>
60 * <li><code> CharKeepTogether</code></li>
61 * <li><code> CharNoLineBreak</code></li>
62 * <li><code> CharShadowed</code></li>
63 * <li><code> CharFontType</code></li>
64 * <li><code> CharStyleName</code></li>
65 * <li><code> CharContoured</code></li>
66 * <li><code> CharCombineIsOn</code></li>
67 * <li><code> CharCombinePrefix</code></li>
68 * <li><code> CharCombineSuffix</code></li>
69 * <li><code> CharEmphasize</code></li>
70 * <li><code> CharRelief</code></li>
71 * <li><code> RubyText</code></li>
72 * <li><code> RubyAdjust</code></li>
73 * <li><code> RubyCharStyleName</code></li>
74 * <li><code> RubyIsAbove</code></li>
75 * <li><code> CharRotation</code></li>
76 * <li><code> CharRotationIsFitToLine</code></li>
77 * <li><code> CharScaleWidth</code></li>
78 * <li><code> HyperLinkURL</code></li>
79 * <li><code> HyperLinkTarget</code></li>
80 * <li><code> HyperLinkName</code></li>
81 * <li><code> TextUserDefinedAttributes</code></li>
83 * This test needs the following object relations :
85 * <li> <code>'PARA'</code>: <b>optional</b>
86 * (must implement <code>XPropertySet</code>):
87 * if this relation is specified then some properties
88 * testing is performed in a special manner. (e.g. this used in
89 * <code>sw.SwXParagraph</code> component) For details
90 * see {@link #changeProp} method description. </li>
91 * <li> <code>'PORTION'</code>: <b>optional</b>
92 * (must implement <code>XPropertySet</code>):
93 * if this relation is specified then some properties
94 * testing is performed in a special manner. (e.g. this used in
95 * <code>sw.SwXParagraph</code> component) For details
96 * see {@link #changeProp} method description. </li>
98 * Properties testing is automated by <code>lib.MultiPropertyTest</code>.
99 * @see com.sun.star.style.CharacterProperties
101 public class _CharacterProperties
extends MultiPropertyTest
{
103 public void _CharFontName() {
104 testProperty("CharFontName", "Times New Roman", "Arial") ;
107 public void _CharHeight() {
108 testProperty("CharHeight", new PropertyTester() {
110 protected Object
getNewValue(String propName
, Object old
) {
111 if (utils
.isVoid(old
)) {
112 return new Float(10) ;
114 return new Float(((Float
) old
).floatValue() + 10) ;
121 * Custom tester for properties which contains image URLs.
122 * Switches between two JPG images' URLs.
124 protected PropertyTester URLTester
= new PropertyTester() {
126 protected Object
getNewValue(String propName
, Object oldValue
) {
127 if (oldValue
.equals("http://www.sun.com"))
128 return "http://www.openoffice.org"; else
129 return "http://www.sun.com";
133 public void _HyperLinkURL() {
134 testProperty("HyperLinkURL", URLTester
) ;
137 public void _HyperLinkName() {
138 testProperty("HyperLinkName", URLTester
) ;
141 public void _HyperLinkTarget() {
142 testProperty("HyperLinkTarget", URLTester
) ;
145 public void _CharWeight() {
146 testProperty("CharWeight", new Float(com
.sun
.star
.awt
.FontWeight
.BOLD
),
147 new Float(com
.sun
.star
.awt
.FontWeight
.THIN
)) ;
150 public void _CharPosture() {
151 testProperty("CharPosture", com
.sun
.star
.awt
.FontSlant
.ITALIC
,
152 com
.sun
.star
.awt
.FontSlant
.NONE
) ;
156 * Custom tester for style name properties. If object relations "STYLENAME1"
157 * and "STYLENAME2" exists, then testing with these strings, else switches
158 * between 'Citation' and 'Emphasis' names.
160 protected PropertyTester StyleTester
= new PropertyTester() {
162 protected Object
getNewValue(String propName
, Object oldValue
) {
163 String oStyleName1
= (String
) tEnv
.getObjRelation("STYLENAME1");
164 String oStyleName2
= (String
) tEnv
.getObjRelation("STYLENAME2");
165 if ((oStyleName1
!= null) && (oStyleName2
!= null)){
166 log
.println("use strings given by object relation: '"
167 + oStyleName1
+ "' '" + oStyleName2
+"'");
168 if (oldValue
.equals( oStyleName1
))
173 if (utils
.isVoid(oldValue
) || (oldValue
.equals("Standard")))
174 return "Example"; else
180 * Custom tester for style names properties. Switches between
181 * 'Citation' and 'Emphasis' names.
183 protected PropertyTester StylesTester
= new PropertyTester() {
185 protected Object
getNewValue(String propName
, Object oldValue
) {
186 if (utils
.isVoid(oldValue
) || (oldValue
.equals("Standard")))
187 return new String
[] {"Citation"}; else
188 return new String
[] {"Emphasis"};
193 * If relations for paragraph and portion exist, then testing
194 * of this property performed using these objects, else
195 * testing is performed in common way.
197 public void _CharStyleName() {
198 log
.println("Testing with custom Property tester") ;
199 Object oPara
= tEnv
.getObjRelation("PARA");
200 Object oPort
= tEnv
.getObjRelation("PORTION");
202 testProperty("CharStyleName", StyleTester
) ;
204 changeProp((XPropertySet
) oPara
,
205 (XPropertySet
) oPort
,"CharStyleName","Standard");
210 * If relations for paragraph and portion exist, then testing
211 * of this property performed using these objects, else
212 * testing is performed in common way.
214 public void _CharStyleNames() {
215 log
.println("Testing with custom Property tester") ;
216 Object oPara
= tEnv
.getObjRelation("PARA");
217 Object oPort
= tEnv
.getObjRelation("PORTION");
219 testProperty("CharStyleNames", StylesTester
) ;
221 changeProp((XPropertySet
) oPara
,
222 (XPropertySet
) oPort
,"CharStyleNames",new String
[] {"Standard"});
227 * If relations for paragraph and portion exist, then testing
228 * of this property performed using these objects, else
229 * testing is performed in common way.
231 public void _RubyCharStyleName() {
232 log
.println("Testing with custom Property tester") ;
233 Object oPara
= tEnv
.getObjRelation("PARA");
234 Object oPort
= tEnv
.getObjRelation("PORTION");
236 testProperty("RubyCharStyleName", StyleTester
) ;
238 changeProp((XPropertySet
) oPara
, (XPropertySet
)
239 oPort
,"RubyCharStyleName","Standard");
244 * If relations for paragraph and portion exist, then testing
245 * of this property performed using these objects, else
246 * testing is performed in common way.
248 public void _RubyAdjust() {
249 log
.println("Testing with custom Property tester") ;
250 Object oPara
= tEnv
.getObjRelation("PARA");
251 Object oPort
= tEnv
.getObjRelation("PORTION");
253 testProperty("RubyAdjust",Short
.valueOf((short)0),Short
.valueOf((short)1));
255 Short aShort
= Short
.valueOf((short) 1);
256 changeProp((XPropertySet
) oPara
,
257 (XPropertySet
) oPort
,"RubyAdjust", aShort
);
259 Short
.valueOf((short) 1);
263 * Custom tester for the ruby text property.
265 protected PropertyTester RubyTextTester
= new PropertyTester() {
267 protected Object
getNewValue(String propName
, Object oldValue
)
268 throws java
.lang
.IllegalArgumentException
{
269 if (utils
.isVoid(oldValue
)) {
272 return ValueChanger
.changePValue(oldValue
);
278 * If relations for paragraph and portion exist, then testing
279 * of this property performed using these objects, else
280 * testing is performed in common way.
282 public void _RubyText() {
283 log
.println("Testing with custom Property tester") ;
284 Object oPara
= tEnv
.getObjRelation("PARA");
285 Object oPort
= tEnv
.getObjRelation("PORTION");
287 testProperty("RubyText", RubyTextTester
) ;
289 changeProp((XPropertySet
) oPara
, (XPropertySet
) oPort
,
295 * If relations for paragraph and portion exist, then testing
296 * of this property performed using these objects, else
297 * testing is performed in common way.
299 public void _RubyIsAbove() {
300 log
.println("Testing with custom Property tester") ;
301 Object oPara
= tEnv
.getObjRelation("PARA");
302 Object oPort
= tEnv
.getObjRelation("PORTION");
304 testProperty("RubyIsAbove") ;
306 changeProp((XPropertySet
) oPara
, (XPropertySet
) oPort
,
307 "RubyIsAbove",Boolean
.TRUE
);
312 * This property only takes values between 0..100
313 * so ist must be treated special
315 public void _CharEscapementHeight() {
316 Byte aByte
= Byte
.valueOf((byte)75);
317 Byte max
= Byte
.valueOf((byte)100);
318 testProperty("CharEscapementHeight", aByte
, max
) ;
323 * This property can be void, so if old value is <code> null </code>
324 * new value must be specified.
326 public void _CharRotation() {
327 Short aShort
= Short
.valueOf((short) 10);
328 changeProp(oObj
,oObj
, "CharRotation", aShort
);
332 * Tests the property specified by <code>name</code> using
333 * property set <code>oProps</code>, but value after setting
334 * this property to a new value is checked using another
335 * PropertySet <code>get</code>. Special cases used for
336 * <code>CharRotation</code> property (it can have only certain values
337 * 0, 900, ...), and for <code>*StyleName</code> properties
338 * (only existing style names are accepted)
339 * @param oProps PropertySet from which property value is get
341 * @param get PropertySet where property value is checked after
343 * @param name Property name to test.
344 * @param newVal Value used to set as new property value if
345 * the value get is null.
347 public void changeProp(XPropertySet oProps
,
348 XPropertySet get
,String name
, Object newVal
) {
350 Object gValue
= null;
351 Object sValue
= null;
352 Object ValueToSet
= null;
355 gValue
= oProps
.getPropertyValue(name
);
357 if ( (gValue
== null) || (utils
.isVoid(gValue
)) ) {
358 log
.println("Value for "+name
+" is NULL");
362 if (name
.equals("CharRotation")) {
363 Short s1
= Short
.valueOf((short) 0);
364 Short s2
= Short
.valueOf((short) 900);
365 if (gValue
.equals(s1
)) {
371 ValueToSet
= ValueChanger
.changePValue(gValue
);
373 if (name
.endsWith("StyleName")) {
374 if ( ((String
) gValue
).equals("Standard") ) {
375 ValueToSet
="Main index entry";
378 ValueToSet
="Emphasis";
382 oProps
.setPropertyValue(name
,ValueToSet
);
383 sValue
= get
.getPropertyValue(name
);
384 if (sValue
== null) {
385 log
.println("Value for "+name
+" is NULL after setting");
389 //check get-set methods
390 if (gValue
.equals(sValue
)) {
391 log
.println("Value for '"+name
+"' hasn't changed");
392 tRes
.tested(name
, false);
394 log
.println("Property '"+name
+"' OK");
395 log
.println("old: "+gValue
.toString());
396 log
.println("new: "+ValueToSet
.toString());
397 log
.println("result: "+sValue
.toString());
398 tRes
.tested(name
, true);
401 catch (com
.sun
.star
.beans
.UnknownPropertyException ex
) {
402 if (isOptional(name
)) {
403 log
.println("Property '"+name
+
404 "' is optional and not supported");
405 tRes
.tested(name
,true);
408 log
.println("Exception occurred while testing property '" +
410 ex
.printStackTrace(log
);
411 tRes
.tested(name
, false);
413 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
414 log
.println("Exception occurred while testing property '" +
416 e
.printStackTrace(log
);
417 tRes
.tested(name
, false);
418 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
419 log
.println("Exception occurred while testing property '" +
421 e
.printStackTrace(log
);
422 tRes
.tested(name
, false);
423 } catch (com
.sun
.star
.beans
.PropertyVetoException e
) {
424 log
.println("Exception occurred while testing property '" +
426 e
.printStackTrace(log
);
427 tRes
.tested(name
, false);
429 }// end of changeProp
431 public void _TextUserDefinedAttributes() {
432 XNameContainer uda
= null;
437 uda
= (XNameContainer
) AnyConverter
.toObject(
438 new Type(XNameContainer
.class),
439 oObj
.getPropertyValue("TextUserDefinedAttributes"));
440 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
){
441 log
.println("TextUserDefinedAttributes is empty.");
442 uda
= new _CharacterProperties
.OwnUserDefinedAttributes();
444 AttributeData attr
= new AttributeData();
445 attr
.Namespace
= "http://www.sun.com/staroffice/apitest/Cellprop";
448 uda
.insertByName("Cellprop:has-first-alien-attribute", attr
);
450 uda
.getElementNames();
451 oObj
.setPropertyValue("TextUserDefinedAttributes", uda
);
452 uda
= (XNameContainer
) AnyConverter
.toObject(
453 new Type(XNameContainer
.class),
454 oObj
.getPropertyValue("TextUserDefinedAttributes"));
455 uda
.getElementNames();
457 uda
.getByName("Cellprop:has-first-alien-attribute");
459 } catch (com
.sun
.star
.beans
.UnknownPropertyException upe
) {
460 if (isOptional("TextUserDefinedAttributes")) {
461 log
.println("Property is optional and not supported");
464 log
.println("Don't know the Property 'TextUserDefinedAttributes'");
466 } catch (com
.sun
.star
.lang
.WrappedTargetException wte
) {
468 "WrappedTargetException while getting Property 'TextUserDefinedAttributes'");
469 } catch (com
.sun
.star
.container
.NoSuchElementException nee
) {
470 log
.println("added Element isn't part of the NameContainer");
471 } catch (com
.sun
.star
.lang
.IllegalArgumentException iae
) {
473 "IllegalArgumentException while getting Property 'TextUserDefinedAttributes'");
474 } catch (com
.sun
.star
.beans
.PropertyVetoException pve
) {
476 "PropertyVetoException while getting Property 'TextUserDefinedAttributes'");
477 } catch (com
.sun
.star
.container
.ElementExistException eee
) {
479 "ElementExistException while getting Property 'TextUserDefinedAttributes'");
482 tRes
.tested("TextUserDefinedAttributes", res
);
485 private class OwnUserDefinedAttributes
implements XNameContainer
{
486 HashMap
<String
, Object
> members
= null;
489 public OwnUserDefinedAttributes() {
490 members
= new HashMap
<String
, Object
>();
493 public Object
getByName(String str
) throws com
.sun
.star
.container
.NoSuchElementException
, com
.sun
.star
.lang
.WrappedTargetException
{
494 return members
.get(str
);
497 public String
[] getElementNames() {
498 Iterator
<String
> oEnum
= members
.keySet().iterator();
499 int count
= members
.size();
500 String
[] res
= new String
[count
];
502 while(oEnum
.hasNext()) {
503 res
[i
++] = oEnum
.next();
508 public com
.sun
.star
.uno
.Type
getElementType() {
509 Iterator
<String
> oEnum
= members
.keySet().iterator();
510 String key
= oEnum
.next();
511 Object o
= members
.get(key
);
512 return new Type(o
.getClass());
515 public boolean hasByName(String str
) {
516 return members
.get(str
) != null;
519 public boolean hasElements() {
520 return members
.size() > 0;
523 public void insertByName(String str
, Object obj
) throws com
.sun
.star
.lang
.IllegalArgumentException
, com
.sun
.star
.container
.ElementExistException
, com
.sun
.star
.lang
.WrappedTargetException
{
524 members
.put(str
, obj
);
527 public void removeByName(String str
) throws com
.sun
.star
.container
.NoSuchElementException
, com
.sun
.star
.lang
.WrappedTargetException
{
531 public void replaceByName(String str
, Object obj
) throws com
.sun
.star
.lang
.IllegalArgumentException
, com
.sun
.star
.container
.NoSuchElementException
, com
.sun
.star
.lang
.WrappedTargetException
{
532 members
.put(str
, obj
);
537 } //finish class _CharacterProperties