Avoid potential negative array index access to cached text.
[LibreOffice.git] / qadevOOo / tests / java / ifc / style / _CharacterProperties.java
blob90f6b85315f3dcd7b95b27a740b116aa8509d28a
1 /*
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 .
19 package ifc.style;
21 import java.util.HashMap;
22 import java.util.Iterator;
24 import lib.MultiPropertyTest;
25 import util.ValueChanger;
26 import util.utils;
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;
35 /**
36 * Testing <code>com.sun.star.style.CharacterProperties</code>
37 * service properties :
38 * <ul>
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>
82 * </ul> <p>
83 * This test needs the following object relations :
84 * <ul>
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>
97 * <ul> <p>
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() {
109 @Override
110 protected Object getNewValue(String propName, Object old) {
111 if (utils.isVoid(old)) {
112 return Float.valueOf(10) ;
113 } else {
114 return Float.valueOf(((Float) old).floatValue() + 10) ;
117 }) ;
121 * Custom tester for properties which contains image URLs.
122 * Switches between two JPG images' URLs.
124 protected PropertyTester URLTester = new PropertyTester() {
125 @Override
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", Float.valueOf(com.sun.star.awt.FontWeight.BOLD),
147 Float.valueOf(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() {
161 @Override
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))
169 return oStyleName2;
170 else
171 return oStyleName1;
173 if (utils.isVoid(oldValue) || (oldValue.equals("Standard")))
174 return "Example"; else
175 return "Emphasis";
180 * Custom tester for style names properties. Switches between
181 * 'Citation' and 'Emphasis' names.
183 protected PropertyTester StylesTester = new PropertyTester() {
184 @Override
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");
201 if (oPara == null) {
202 testProperty("CharStyleName", StyleTester) ;
203 } else {
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");
218 if (oPara == null) {
219 testProperty("CharStyleNames", StylesTester) ;
220 } else {
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");
235 if (oPara == null) {
236 testProperty("RubyCharStyleName", StyleTester) ;
237 } else {
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");
252 if (oPara == null) {
253 testProperty("RubyAdjust",Short.valueOf((short)0),Short.valueOf((short)1));
254 } else {
255 Short aShort = Short.valueOf((short) 1);
256 changeProp((XPropertySet) oPara,
257 (XPropertySet) oPort,"RubyAdjust", aShort);
262 * Custom tester for the ruby text property.
264 protected PropertyTester RubyTextTester = new PropertyTester() {
265 @Override
266 protected Object getNewValue(String propName, Object oldValue)
267 throws java.lang.IllegalArgumentException {
268 if (utils.isVoid(oldValue)) {
269 return "RubyText";
270 } else {
271 return ValueChanger.changePValue(oldValue);
277 * If relations for paragraph and portion exist, then testing
278 * of this property performed using these objects, else
279 * testing is performed in common way.
281 public void _RubyText() {
282 log.println("Testing with custom Property tester") ;
283 Object oPara = tEnv.getObjRelation("PARA");
284 Object oPort = tEnv.getObjRelation("PORTION");
285 if (oPara == null) {
286 testProperty("RubyText", RubyTextTester) ;
287 } else {
288 changeProp((XPropertySet) oPara, (XPropertySet) oPort,
289 "RubyText","");
294 * If relations for paragraph and portion exist, then testing
295 * of this property performed using these objects, else
296 * testing is performed in common way.
298 public void _RubyIsAbove() {
299 log.println("Testing with custom Property tester") ;
300 Object oPara = tEnv.getObjRelation("PARA");
301 Object oPort = tEnv.getObjRelation("PORTION");
302 if (oPara == null) {
303 testProperty("RubyIsAbove") ;
304 } else {
305 changeProp((XPropertySet) oPara, (XPropertySet) oPort,
306 "RubyIsAbove",Boolean.TRUE);
311 * This property only takes values between 0..100
312 * so it must be treated specially
314 public void _CharEscapementHeight() {
315 Byte aByte = Byte.valueOf((byte)75);
316 Byte max = Byte.valueOf((byte)100);
317 testProperty("CharEscapementHeight", aByte, max) ;
322 * This property can be void, so if old value is <code> null </code>
323 * new value must be specified.
325 public void _CharRotation() {
326 Short aShort = Short.valueOf((short) 10);
327 changeProp(oObj,oObj, "CharRotation", aShort);
331 * Tests the property specified by <code>name</code> using
332 * property set <code>oProps</code>, but value after setting
333 * this property to a new value is checked using another
334 * PropertySet <code>get</code>. Special cases used for
335 * <code>CharRotation</code> property (it can have only certain values
336 * 0, 900, ...), and for <code>*StyleName</code> properties
337 * (only existing style names are accepted)
338 * @param oProps PropertySet from which property value is get
339 * changed and set.
340 * @param get PropertySet where property value is checked after
341 * setting.
342 * @param name Property name to test.
343 * @param newVal Value used to set as new property value if
344 * the value get is null.
346 public void changeProp(XPropertySet oProps,
347 XPropertySet get,String name, Object newVal) {
349 Object gValue = null;
350 Object sValue = null;
351 Object ValueToSet = null;
353 try {
354 gValue = oProps.getPropertyValue(name);
356 if ( (gValue == null) || (utils.isVoid(gValue)) ) {
357 log.println("Value for "+name+" is NULL");
358 gValue = newVal;
361 if (name.equals("CharRotation")) {
362 Short s1 = Short.valueOf((short) 0);
363 Short s2 = Short.valueOf((short) 900);
364 if (gValue.equals(s1)) {
365 ValueToSet = s2;
366 } else {
367 ValueToSet = s1;
369 } else {
370 ValueToSet = ValueChanger.changePValue(gValue);
372 if (name.endsWith("StyleName")) {
373 if ( ((String) gValue).equals("Standard") ) {
374 ValueToSet="Main index entry";
376 else {
377 ValueToSet="Emphasis";
381 oProps.setPropertyValue(name,ValueToSet);
382 sValue = get.getPropertyValue(name);
383 if (sValue == null) {
384 log.println("Value for "+name+" is NULL after setting");
385 sValue = gValue;
388 //check get-set methods
389 if (gValue.equals(sValue)) {
390 log.println("Value for '"+name+"' hasn't changed");
391 tRes.tested(name, false);
392 } else {
393 log.println("Property '"+name+"' OK");
394 log.println("old: "+gValue.toString());
395 log.println("new: "+ValueToSet.toString());
396 log.println("result: "+sValue.toString());
397 tRes.tested(name, true);
400 catch (com.sun.star.beans.UnknownPropertyException ex) {
401 if (isOptional(name)) {
402 log.println("Property '"+name+
403 "' is optional and not supported");
404 tRes.tested(name,true);
406 else {
407 log.println("Exception occurred while testing property '" +
408 name + "'");
409 ex.printStackTrace(log);
410 tRes.tested(name, false);
412 } catch (com.sun.star.lang.WrappedTargetException e) {
413 log.println("Exception occurred while testing property '" +
414 name + "'");
415 e.printStackTrace(log);
416 tRes.tested(name, false);
417 } catch (com.sun.star.lang.IllegalArgumentException e) {
418 log.println("Exception occurred while testing property '" +
419 name + "'");
420 e.printStackTrace(log);
421 tRes.tested(name, false);
422 } catch (com.sun.star.beans.PropertyVetoException e) {
423 log.println("Exception occurred while testing property '" +
424 name + "'");
425 e.printStackTrace(log);
426 tRes.tested(name, false);
428 }// end of changeProp
430 public void _TextUserDefinedAttributes() {
431 XNameContainer uda = null;
432 boolean res = false;
434 try {
435 try{
436 uda = (XNameContainer) AnyConverter.toObject(
437 new Type(XNameContainer.class),
438 oObj.getPropertyValue("TextUserDefinedAttributes"));
439 } catch (com.sun.star.lang.IllegalArgumentException e){
440 log.println("TextUserDefinedAttributes is empty.");
441 uda = new _CharacterProperties.OwnUserDefinedAttributes();
443 AttributeData attr = new AttributeData();
444 attr.Namespace = "http://www.sun.com/staroffice/apitest/Cellprop";
445 attr.Type = "CDATA";
446 attr.Value = "true";
447 uda.insertByName("Cellprop:has-first-alien-attribute", attr);
449 uda.getElementNames();
450 oObj.setPropertyValue("TextUserDefinedAttributes", uda);
451 uda = (XNameContainer) AnyConverter.toObject(
452 new Type(XNameContainer.class),
453 oObj.getPropertyValue("TextUserDefinedAttributes"));
454 uda.getElementNames();
456 uda.getByName("Cellprop:has-first-alien-attribute");
457 res = true;
458 } catch (com.sun.star.beans.UnknownPropertyException upe) {
459 if (isOptional("TextUserDefinedAttributes")) {
460 log.println("Property is optional and not supported");
461 res = true;
462 } else {
463 log.println("Don't know the Property 'TextUserDefinedAttributes'");
465 } catch (com.sun.star.lang.WrappedTargetException wte) {
466 log.println(
467 "WrappedTargetException while getting Property 'TextUserDefinedAttributes'");
468 } catch (com.sun.star.container.NoSuchElementException nee) {
469 log.println("added Element isn't part of the NameContainer");
470 } catch (com.sun.star.lang.IllegalArgumentException iae) {
471 log.println(
472 "IllegalArgumentException while getting Property 'TextUserDefinedAttributes'");
473 } catch (com.sun.star.beans.PropertyVetoException pve) {
474 log.println(
475 "PropertyVetoException while getting Property 'TextUserDefinedAttributes'");
476 } catch (com.sun.star.container.ElementExistException eee) {
477 log.println(
478 "ElementExistException while getting Property 'TextUserDefinedAttributes'");
481 tRes.tested("TextUserDefinedAttributes", res);
484 private static class OwnUserDefinedAttributes implements XNameContainer{
485 HashMap<String, Object> members = null;
488 public OwnUserDefinedAttributes() {
489 members = new HashMap<String, Object>();
492 public Object getByName(String str) throws com.sun.star.container.NoSuchElementException, com.sun.star.lang.WrappedTargetException {
493 return members.get(str);
496 public String[] getElementNames() {
497 Iterator<String> oEnum = members.keySet().iterator();
498 int count = members.size();
499 String[] res = new String[count];
500 int i=0;
501 while(oEnum.hasNext()) {
502 res[i++] = oEnum.next();
504 return res;
507 public com.sun.star.uno.Type getElementType() {
508 Iterator<String> oEnum = members.keySet().iterator();
509 String key = oEnum.next();
510 Object o = members.get(key);
511 return new Type(o.getClass());
514 public boolean hasByName(String str) {
515 return members.get(str) != null;
518 public boolean hasElements() {
519 return members.size() > 0;
522 public void insertByName(String str, Object obj) throws com.sun.star.lang.IllegalArgumentException, com.sun.star.container.ElementExistException, com.sun.star.lang.WrappedTargetException {
523 members.put(str, obj);
526 public void removeByName(String str) throws com.sun.star.container.NoSuchElementException, com.sun.star.lang.WrappedTargetException {
527 members.remove(str);
530 public void replaceByName(String str, Object obj) throws com.sun.star.lang.IllegalArgumentException, com.sun.star.container.NoSuchElementException, com.sun.star.lang.WrappedTargetException {
531 members.put(str, obj);
536 } //finish class _CharacterProperties