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