1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package ifc.style;
29 
30 import com.sun.star.beans.XPropertySet;
31 import com.sun.star.container.XNameContainer;
32 import com.sun.star.uno.AnyConverter;
33 import com.sun.star.uno.Type;
34 import com.sun.star.xml.AttributeData;
35 import java.util.Enumeration;
36 import java.util.Hashtable;
37 import lib.MultiPropertyTest;
38 
39 import util.ValueChanger;
40 import util.utils;
41 
42 
43 /**
44 * Testing <code>com.sun.star.style.CharacterProperties</code>
45 * service properties :
46 * <ul>
47 *  <li><code> CharFontName</code></li>
48 *  <li><code> CharFontStyleName</code></li>
49 *  <li><code> CharFontFamily</code></li>
50 *  <li><code> CharFontCharSet</code></li>
51 *  <li><code> CharFontPitch</code></li>
52 *  <li><code> CharColor</code></li>
53 *  <li><code> CharEscapement</code></li>
54 *  <li><code> CharHeight</code></li>
55 *  <li><code> CharUnderline</code></li>
56 *  <li><code> CharWeight</code></li>
57 *  <li><code> CharPosture</code></li>
58 *  <li><code> CharAutoKerning</code></li>
59 *  <li><code> CharBackColor</code></li>
60 *  <li><code> CharBackTransparent</code></li>
61 *  <li><code> CharCaseMap</code></li>
62 *  <li><code> CharCrossedOut</code></li>
63 *  <li><code> CharFlash</code></li>
64 *  <li><code> CharStrikeout</code></li>
65 *  <li><code> CharWordMode</code></li>
66 *  <li><code> CharKerning</code></li>
67 *  <li><code> CharLocale</code></li>
68 *  <li><code> CharKeepTogether</code></li>
69 *  <li><code> CharNoLineBreak</code></li>
70 *  <li><code> CharShadowed</code></li>
71 *  <li><code> CharFontType</code></li>
72 *  <li><code> CharStyleName</code></li>
73 *  <li><code> CharContoured</code></li>
74 *  <li><code> CharCombineIsOn</code></li>
75 *  <li><code> CharCombinePrefix</code></li>
76 *  <li><code> CharCombineSuffix</code></li>
77 *  <li><code> CharEmphasize</code></li>
78 *  <li><code> CharRelief</code></li>
79 *  <li><code> RubyText</code></li>
80 *  <li><code> RubyAdjust</code></li>
81 *  <li><code> RubyCharStyleName</code></li>
82 *  <li><code> RubyIsAbove</code></li>
83 *  <li><code> CharRotation</code></li>
84 *  <li><code> CharRotationIsFitToLine</code></li>
85 *  <li><code> CharScaleWidth</code></li>
86 *  <li><code> HyperLinkURL</code></li>
87 *  <li><code> HyperLinkTarget</code></li>
88 *  <li><code> HyperLinkName</code></li>
89 *  <li><code> TextUserDefinedAttributes</code></li>
90 * </ul> <p>
91 * This test needs the following object relations :
92 * <ul>
93 *  <li> <code>'PARA'</code>: <b>optional</b>
94 *   (must implement <code>XPropertySet</code>):
95 *   if this relation is specified then some properties
96 *   testing is performed in a special manner. (e.g. this used in
97 *   <code>sw.SwXParagraph</code> component) For details
98 *   see {@link #changeProp} method description. </li>
99 *  <li> <code>'PORTION'</code>: <b>optional</b>
100 *   (must implement <code>XPropertySet</code>):
101 *   if this relation is specified then some properties
102 *   testing is performed in a special manner. (e.g. this used in
103 *   <code>sw.SwXParagraph</code> component) For details
104 *   see {@link #changeProp} method description. </li>
105 * <ul> <p>
106 * Properties testing is automated by <code>lib.MultiPropertyTest</code>.
107 * @see com.sun.star.style.CharacterProperties
108 */
109 public class _CharacterProperties extends MultiPropertyTest {
110 
111     public void _CharFontName() {
112         testProperty("CharFontName", "Times New Roman", "Arial") ;
113     }
114 
115     public void _CharHeight() {
116         testProperty("CharHeight", new PropertyTester() {
117             protected Object getNewValue(String propName, Object old) {
118                 if (utils.isVoid(old)) {
119                     return new Float(10) ;
120                 } else {
121                     return new Float(((Float) old).floatValue() + 10) ;
122                 }
123             }
124         }) ;
125     }
126 
127     /**
128      * Custom tester for properties which contains image URLs.
129      * Switches between two JPG images' URLs.
130      */
131     protected PropertyTester URLTester = new PropertyTester() {
132         protected Object getNewValue(String propName, Object oldValue) {
133             if (oldValue.equals("http://www.sun.com"))
134                 return "http://www.openoffice.org"; else
135                 return "http://www.sun.com";
136         }
137     } ;
138 
139     public void _HyperLinkURL() {
140         testProperty("HyperLinkURL", URLTester) ;
141     }
142 
143     public void _HyperLinkName() {
144         testProperty("HyperLinkName", URLTester) ;
145     }
146 
147     public void _HyperLinkTarget() {
148         testProperty("HyperLinkTarget", URLTester) ;
149     }
150 
151     public void _CharWeight() {
152         testProperty("CharWeight", new Float(com.sun.star.awt.FontWeight.BOLD),
153             new Float(com.sun.star.awt.FontWeight.THIN)) ;
154     }
155 
156     public void _CharPosture() {
157         testProperty("CharPosture", com.sun.star.awt.FontSlant.ITALIC,
158             com.sun.star.awt.FontSlant.NONE) ;
159     }
160 
161     /**
162      * Custom tester for style name properties. If object relations "STYLENAME1"
163      * and "STYLENAME2" exists, then testing with these strings, else switches
164      * between 'Citation' and 'Emphasis' names.
165      */
166     protected PropertyTester StyleTester = new PropertyTester() {
167         protected Object getNewValue(String propName, Object oldValue) {
168             String oStyleName1 = (String) tEnv.getObjRelation("STYLENAME1");
169             String oStyleName2 = (String) tEnv.getObjRelation("STYLENAME2");
170             if ((oStyleName1 != null) && (oStyleName2 != null)){
171                 log.println("use strings given by object relation: '"
172                             + oStyleName1 + "' '" + oStyleName2 +"'");
173                 if (oldValue.equals( oStyleName1))
174                     return oStyleName2;
175                 else
176                     return oStyleName1;
177             }
178             if (utils.isVoid(oldValue) || (oldValue.equals("Standard")))
179                 return "Example"; else
180                 return "Emphasis";
181         }
182     } ;
183 
184     /**
185      * Custom tester for style names properties. Switches between
186      * 'Citation' and 'Emphasis' names.
187      */
188     protected PropertyTester StylesTester = new PropertyTester() {
189         protected Object getNewValue(String propName, Object oldValue) {
190             if (utils.isVoid(oldValue) || (oldValue.equals("Standard")))
191                 return new String[] {"Citation"}; else
192                 return new String[] {"Emphasis"};
193         }
194     } ;
195 
196     /**
197      * If relations for paragraph and portion exist, then testing
198      * of this property performed using these objects, else
199      * testing is performed in common way.
200      */
201     public void _CharStyleName() {
202         log.println("Testing with custom Property tester") ;
203         Object oPara = tEnv.getObjRelation("PARA");
204         Object oPort = tEnv.getObjRelation("PORTION");
205         if (oPara == null) {
206             testProperty("CharStyleName", StyleTester) ;
207         } else {
208             changeProp((XPropertySet) oPara,
209                 (XPropertySet) oPort,"CharStyleName","Standard");
210         }
211     }
212 
213     /**
214      * If relations for paragraph and portion exist, then testing
215      * of this property performed using these objects, else
216      * testing is performed in common way.
217      */
218     public void _CharStyleNames() {
219         log.println("Testing with custom Property tester") ;
220         Object oPara = tEnv.getObjRelation("PARA");
221         Object oPort = tEnv.getObjRelation("PORTION");
222         if (oPara == null) {
223             testProperty("CharStyleNames", StylesTester) ;
224         } else {
225             String[] newNames = {"Standard"};
226             changeProp((XPropertySet) oPara,
227                (XPropertySet) oPort,"CharStyleNames",new String[] {"Standard"});
228         }
229     }
230 
231     /**
232      * If relations for paragraph and portion exist, then testing
233      * of this property performed using these objects, else
234      * testing is performed in common way.
235      */
236     public void _RubyCharStyleName() {
237         log.println("Testing with custom Property tester") ;
238         Object oPara = tEnv.getObjRelation("PARA");
239         Object oPort = tEnv.getObjRelation("PORTION");
240         if (oPara == null) {
241             testProperty("RubyCharStyleName", StyleTester) ;
242         } else {
243             changeProp((XPropertySet) oPara, (XPropertySet)
244                 oPort,"RubyCharStyleName","Standard");
245         }
246     }
247 
248     /**
249      * If relations for paragraph and portion exist, then testing
250      * of this property performed using these objects, else
251      * testing is performed in common way.
252      */
253     public void _RubyAdjust() {
254         log.println("Testing with custom Property tester") ;
255         Object oPara = tEnv.getObjRelation("PARA");
256         Object oPort = tEnv.getObjRelation("PORTION");
257         if (oPara == null) {
258             testProperty("RubyAdjust",new Short((short)0),new Short((short)1));
259         } else {
260             Short aShort = new Short((short) 1);
261             changeProp((XPropertySet) oPara,
262                 (XPropertySet) oPort,"RubyAdjust", aShort);
263         }
264         Short aShort = new Short((short) 1);
265     }
266 
267     /**
268      * Custom tester for the ruby text property.
269      */
270     protected PropertyTester RubyTextTester = new PropertyTester() {
271         protected Object getNewValue(String propName, Object oldValue)
272                 throws java.lang.IllegalArgumentException {
273             if (utils.isVoid(oldValue)) {
274                 return new String("RubyText");
275             } else {
276                 return ValueChanger.changePValue(oldValue);
277             }
278         }
279     } ;
280 
281     /**
282      * If relations for paragraph and portion exist, then testing
283      * of this property performed using these objects, else
284      * testing is performed in common way.
285      */
286     public void _RubyText() {
287         log.println("Testing with custom Property tester") ;
288         Object oPara = tEnv.getObjRelation("PARA");
289         Object oPort = tEnv.getObjRelation("PORTION");
290         if (oPara == null) {
291             testProperty("RubyText", RubyTextTester) ;
292         } else {
293             changeProp((XPropertySet) oPara, (XPropertySet) oPort,
294                 "RubyText","");
295         }
296     }
297 
298     /**
299      * If relations for paragraph and portion exist, then testing
300      * of this property performed using these objects, else
301      * testing is performed in common way.
302      */
303     public void _RubyIsAbove() {
304         log.println("Testing with custom Property tester") ;
305         Object oPara = tEnv.getObjRelation("PARA");
306         Object oPort = tEnv.getObjRelation("PORTION");
307         if (oPara == null) {
308             testProperty("RubyIsAbove") ;
309         } else {
310             changeProp((XPropertySet) oPara, (XPropertySet) oPort,
311                 "RubyIsAbove",new Boolean(true));
312         }
313     }
314 
315     /**
316     * This property only takes values between 0..100
317     * so ist must be treated special
318     */
319     public void _CharEscapementHeight() {
320         Byte aByte = new Byte((byte)75);
321         Byte max = new Byte((byte)100);
322         testProperty("CharEscapementHeight", aByte, max) ;
323     }
324 
325 
326     /**
327     * This property can be void, so if old value is <code> null </code>
328     * new value must be specified.
329     */
330     public void _CharRotation() {
331         Short aShort = new Short((short) 10);
332         changeProp(oObj,oObj, "CharRotation", aShort);
333     }
334 
335     /**
336      * Tests the property specified by <code>name</code> using
337      * property set <code>oProps</code>, but value after setting
338      * this property to a new value is checked using another
339      * PropertySet <code>get</code>. Special cases used for
340      * <code>CharRotation</code> property (it can have only certain values
341      * 0, 900, ...), and for  <code>*StyleName</code> properties
342      * (only existing style names are accepted)
343      * @param oProps PropertySet from which property value is get
344      * changed and set.
345      * @param get PropertySet where property value is checked after
346      * setting.
347      * @param name Propety name to test.
348      * @param newVal Value used to set as new property value if
349      * the value get is null.
350      */
351     public void changeProp(XPropertySet oProps,
352             XPropertySet get,String name, Object newVal) {
353 
354         Object gValue = null;
355         Object sValue = null;
356         Object ValueToSet = null;
357 
358         try {
359             //waitForAllThreads();
360             gValue = oProps.getPropertyValue(name);
361 
362             if ( (gValue == null) || (utils.isVoid(gValue)) ) {
363                 log.println("Value for "+name+" is NULL");
364                 gValue = newVal;
365             }
366 
367             //waitForAllThreads();
368             if (name.equals("CharRotation")) {
369                 Short s1 = new Short((short) 0);
370                 Short s2 = new Short((short) 900);
371                 if (gValue.equals(s1)) {
372                     ValueToSet = s2;
373                 } else {
374                     ValueToSet = s1;
375                 }
376             } else {
377                 ValueToSet = ValueChanger.changePValue(gValue);
378             }
379             if (name.endsWith("StyleName")) {
380                 if ( ((String) gValue).equals("Standard") ) {
381                     ValueToSet="Main index entry";
382                 }
383                 else {
384                     ValueToSet="Emphasis";
385                 }
386             }
387 
388             //waitForAllThreads();
389             oProps.setPropertyValue(name,ValueToSet);
390             sValue = get.getPropertyValue(name);
391             if (sValue == null) {
392                 log.println("Value for "+name+" is NULL after setting");
393                 sValue = gValue;
394             }
395 
396             //check get-set methods
397             if (gValue.equals(sValue)) {
398                 log.println("Value for '"+name+"' hasn't changed");
399                 tRes.tested(name, false);
400             } else {
401                 log.println("Property '"+name+"' OK");
402                 log.println("old: "+gValue.toString());
403                 log.println("new: "+ValueToSet.toString());
404                 log.println("result: "+sValue.toString());
405                 tRes.tested(name, true);
406             }
407         }
408         catch (com.sun.star.beans.UnknownPropertyException ex) {
409             if (isOptional(name)) {
410                 log.println("Property '"+name+
411                     "' is optional and not supported");
412                 tRes.tested(name,true);
413             }
414             else {
415                 log.println("Exception occured while testing property '" +
416                      name + "'");
417                 ex.printStackTrace(log);
418                 tRes.tested(name, false);
419             }
420         } catch (com.sun.star.lang.WrappedTargetException e) {
421             log.println("Exception occured while testing property '" +
422                 name + "'");
423             e.printStackTrace(log);
424             tRes.tested(name, false);
425         } catch (com.sun.star.lang.IllegalArgumentException e) {
426             log.println("Exception occured while testing property '" +
427                  name + "'");
428             e.printStackTrace(log);
429             tRes.tested(name, false);
430         } catch (com.sun.star.beans.PropertyVetoException e) {
431             log.println("Exception occured while testing property '" +
432                  name + "'");
433             e.printStackTrace(log);
434             tRes.tested(name, false);
435         }
436     }// end of changeProp
437 
438     public void _TextUserDefinedAttributes() {
439         XNameContainer uda = null;
440         boolean res = false;
441 
442         try {
443             try{
444                 uda = (XNameContainer) AnyConverter.toObject(
445                           new Type(XNameContainer.class),
446                           oObj.getPropertyValue("TextUserDefinedAttributes"));
447             } catch (com.sun.star.lang.IllegalArgumentException e){
448                 log.println("TextUserDefinedAttributes is empty.");
449                 uda = new _CharacterProperties.OwnUserDefinedAttributes();
450             }
451             AttributeData attr = new AttributeData();
452             attr.Namespace = "http://www.sun.com/staroffice/apitest/Cellprop";
453             attr.Type = "CDATA";
454             attr.Value = "true";
455             uda.insertByName("Cellprop:has-first-alien-attribute", attr);
456 
457             String[] els = uda.getElementNames();
458             oObj.setPropertyValue("TextUserDefinedAttributes", uda);
459             uda = (XNameContainer) AnyConverter.toObject(
460                           new Type(XNameContainer.class),
461                           oObj.getPropertyValue("TextUserDefinedAttributes"));
462             els = uda.getElementNames();
463 
464             Object obj = uda.getByName("Cellprop:has-first-alien-attribute");
465             res = true;
466         } catch (com.sun.star.beans.UnknownPropertyException upe) {
467             if (isOptional("TextUserDefinedAttributes")) {
468                 log.println("Property is optional and not supported");
469                 res = true;
470             } else {
471                 log.println("Don't know the Property 'TextUserDefinedAttributes'");
472             }
473         } catch (com.sun.star.lang.WrappedTargetException wte) {
474             log.println(
475                     "WrappedTargetException while getting Property 'TextUserDefinedAttributes'");
476         } catch (com.sun.star.container.NoSuchElementException nee) {
477             log.println("added Element isn't part of the NameContainer");
478         } catch (com.sun.star.lang.IllegalArgumentException iae) {
479             log.println(
480                     "IllegalArgumentException while getting Property 'TextUserDefinedAttributes'");
481         } catch (com.sun.star.beans.PropertyVetoException pve) {
482             log.println(
483                     "PropertyVetoException while getting Property 'TextUserDefinedAttributes'");
484         } catch (com.sun.star.container.ElementExistException eee) {
485             log.println(
486                     "ElementExistException while getting Property 'TextUserDefinedAttributes'");
487         }
488 
489         tRes.tested("TextUserDefinedAttributes", res);
490     }
491 
492     private class OwnUserDefinedAttributes implements XNameContainer{
493         Hashtable members = null;
494 
495 
496         public OwnUserDefinedAttributes() {
497             members = new Hashtable();
498         }
499 
500         public Object getByName(String str) throws com.sun.star.container.NoSuchElementException, com.sun.star.lang.WrappedTargetException {
501             return members.get(str);
502         }
503 
504         public String[] getElementNames() {
505             Enumeration oEnum = members.keys();
506             int count = members.size();
507             String[] res = new String[count];
508             int i=0;
509             while(oEnum.hasMoreElements())
510                 res[i] = (String)oEnum.nextElement();
511             return res;
512         }
513 
514         public com.sun.star.uno.Type getElementType() {
515             Enumeration oEnum = members.keys();
516             String key = (String)oEnum.nextElement();
517             Object o = members.get(key);
518             return new Type(o.getClass());
519         }
520 
521         public boolean hasByName(String str) {
522             return members.get(str) != null;
523         }
524 
525         public boolean hasElements() {
526             return members.size() > 0;
527         }
528 
529         public void insertByName(String str, Object obj) throws com.sun.star.lang.IllegalArgumentException, com.sun.star.container.ElementExistException, com.sun.star.lang.WrappedTargetException {
530             members.put(str, obj);
531         }
532 
533         public void removeByName(String str) throws com.sun.star.container.NoSuchElementException, com.sun.star.lang.WrappedTargetException {
534             members.remove(str);
535         }
536 
537         public void replaceByName(String str, Object obj) throws com.sun.star.lang.IllegalArgumentException, com.sun.star.container.NoSuchElementException, com.sun.star.lang.WrappedTargetException {
538             members.put(str, obj);
539         }
540 
541     }
542 
543 } //finish class _CharacterProperties
544 
545