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 package ifc.style;
28 
29 import com.sun.star.beans.PropertyValue;
30 import com.sun.star.beans.UnknownPropertyException;
31 import com.sun.star.container.XIndexReplace;
32 import com.sun.star.container.XNameContainer;
33 import com.sun.star.lang.WrappedTargetException;
34 import com.sun.star.uno.AnyConverter;
35 import com.sun.star.uno.Type;
36 import com.sun.star.xml.AttributeData;
37 import ifc.text._NumberingLevel;
38 import java.util.Enumeration;
39 import java.util.Hashtable;
40 
41 import lib.MultiPropertyTest;
42 import lib.Status;
43 import share.LogWriter;
44 
45 
46 import util.utils;
47 
48 
49 /**
50 * Testing <code>com.sun.star.style.ParagraphProperties</code>
51 * service properties :
52 * <ul>
53 *  <li><code> ParaAdjust</code></li>
54 *  <li><code> ParaLineSpacing</code></li>
55 *  <li><code> ParaBackColor</code></li>
56 *  <li><code> ParaBackTransparent</code></li>
57 *  <li><code> ParaBackGraphicURL</code></li>
58 *  <li><code> ParaBackGraphicFilter</code></li>
59 *  <li><code> ParaBackGraphicLocation</code></li>
60 *  <li><code> ParaLastLineAdjust</code></li>
61 *  <li><code> ParaExpandSingleWord</code></li>
62 *  <li><code> ParaLeftMargin</code></li>
63 *  <li><code> ParaRightMargin</code></li>
64 *  <li><code> ParaTopMargin</code></li>
65 *  <li><code> ParaBottomMargin</code></li>
66 *  <li><code> ParaLineNumberCount</code></li>
67 *  <li><code> ParaLineNumberStartValue</code></li>
68 *  <li><code> ParaIsHyphenation</code></li>
69 *  <li><code> PageDescName</code></li>
70 *  <li><code> PageNumberOffset</code></li>
71 *  <li><code> ParaRegisterModeActive</code></li>
72 *  <li><code> ParaTabStops</code></li>
73 *  <li><code> ParaStyleName</code></li>
74 *  <li><code> DropCapFormat</code></li>
75 *  <li><code> DropCapWholeWord</code></li>
76 *  <li><code> ParaKeepTogether</code></li>
77 *  <li><code> ParaSplit</code></li>
78 *  <li><code> NumberingLevel</code></li>
79 *  <li><code> NumberingRules</code></li>
80 *  <li><code> NumberingStartValue</code></li>
81 *  <li><code> ParaIsNumberingRestart</code></li>
82 *  <li><code> NumberingStyleName</code></li>
83 *  <li><code> ParaOrphans</code></li>
84 *  <li><code> ParaWidows</code></li>
85 *  <li><code> ParaShadowFormat</code></li>
86 *  <li><code> IsHangingPunctuation</code></li>
87 *  <li><code> IsCharacterDistance</code></li>
88 *  <li><code> IsForbiddenRules</code></li>
89 *  <li><code> LeftBorder</code></li>
90 *  <li><code> RightBorder</code></li>
91 *  <li><code> TopBorder</code></li>
92 *  <li><code> BottomBorder</code></li>
93 *  <li><code> BorderDistance</code></li>
94 *  <li><code> LeftBorderDistance</code></li>
95 *  <li><code> RightBorderDistance</code></li>
96 *  <li><code> TopBorderDistance</code></li>
97 *  <li><code> BottomBorderDistance</code></li>
98 * </ul> <p>
99 * This test needs the following object relations :
100 * <ul>
101 *  <li> <code>'NRULES'</code> : <b>optional</b>
102 *   (service <code>com.sun.star.text.NumberingRules</code>):
103 *   instance of the service which can be set as 'NumberingRules'
104 *   property new value. If the relation doesn't then two
105 *   different <code>NumberingRules</code> objects are tried
106 *   to be obtained by setting different 'NumberingStyleName'
107 *   property styles and getting 'NumberingRules' property values.</li>
108 * <ul> <p>
109 * Properties testing is automated by <code>lib.MultiPropertyTest</code>.
110 * @see com.sun.star.style.ParagraphProperties
111 */
112 public class _ParagraphProperties extends MultiPropertyTest {
113     /**
114      * Custom tester for numbering style properties. Switches between
115      * 'Numbering 1' and 'Numbering 2' styles.
116      */
117     protected PropertyTester NumberingStyleTester = new PropertyTester() {
118         protected Object getNewValue(String propName, Object oldValue)
119                               throws java.lang.IllegalArgumentException {
120             if ((oldValue != null) && (oldValue.equals("Numbering 1"))) {
121                 return "Numbering 2";
122             } else {
123                 return "Numbering 1";
124             }
125         }
126     };
127 
128     /**
129      * Custom tester for paragraph style properties. Switches between
130      * 'Salutation' and 'Heading' styles.
131      */
132     protected PropertyTester charStyleTester = new PropertyTester() {
133         protected Object getNewValue(String propName, Object oldValue) {
134             if (!utils.isVoid(oldValue) && (oldValue.equals("Example"))) {
135                 return "Emphasis";
136             } else {
137                 return "Example";
138             }
139         }
140     };
141 
142     /**
143      * Custom tester for paragraph style properties. Switches between
144      * 'Salutation' and 'Heading' styles.
145      */
146     protected PropertyTester ParaStyleTester = new PropertyTester() {
147         protected Object getNewValue(String propName, Object oldValue) {
148             if (!utils.isVoid(oldValue) && (oldValue.equals("Heading"))) {
149                 return "Salutation";
150             } else {
151                 return "Heading";
152             }
153         }
154     };
155 
156     /**
157      * Custom tester for PageDescName properties. Switches between
158      * 'HTML' and 'Standard' descriptor names.
159      */
160     protected PropertyTester PageDescTester = new PropertyTester() {
161         protected Object getNewValue(String propName, Object oldValue) {
162             if (!util.utils.isVoid(oldValue) &&
163                     (oldValue.equals("Default"))) {
164                 return "HTML";
165             } else {
166                 return "Default";
167             }
168         }
169     };
170 
171     /**
172      * Custom tester for properties which have <code>short</code> type
173      * and can be void, so if they have void value, the new value must
174      * be specified. Switches between two different values.
175      */
176     protected PropertyTester ShortTester = new PropertyTester() {
177         protected Object getNewValue(String propName, Object oldValue) {
178             if ((oldValue != null) &&
179                     (oldValue.equals(new Short((short) 0)))) {
180                 return new Short((short) 2);
181             } else {
182                 return new Short((short) 0);
183             }
184         }
185     };
186 
187     /**
188      * Custom tester for properties which have <code>boolean</code> type
189      * and can be void, so if they have void value, the new value must
190      * be specified. Switches between true and false.
191      */
192     protected PropertyTester BooleanTester = new PropertyTester() {
193         protected Object getNewValue(String propName, Object oldValue) {
194             if ((oldValue != null) &&
195                     (oldValue.equals(new Boolean((boolean) false)))) {
196                 return new Boolean((boolean) true);
197             } else {
198                 return new Boolean((boolean) false);
199             }
200         }
201     };
202 
203     /**
204      * Custom tester for properties which contains image URLs.
205      * Switches between two JPG images' URLs.
206      */
207     protected PropertyTester URLTester = new PropertyTester() {
208         protected Object getNewValue(String propName, Object oldValue) {
209             if (oldValue.equals(util.utils.getFullTestURL("space-metal.jpg"))) {
210                 return util.utils.getFullTestURL("crazy-blue.jpg");
211             } else {
212                 return util.utils.getFullTestURL("space-metal.jpg");
213             }
214         }
215     };
216 
217     protected PropertyTester rules = null;
218 
219     /**
220      * Creates tester for 'NumberingRules' depending on relation.
221      */
222     public void before() {
223         final Object nRules = tEnv.getObjRelation("NRULES");
224 
225         if (nRules != null) {
226             rules = new PropertyTester() {
227                 protected Object getNewValue(String propName, Object oldValue) {
228                     return nRules;
229                 }
230             };
231 
232         } else {
233             Object rules1 = null;
234             Object rules2 = null;
235 
236             try {
237                 oObj.setPropertyValue("NumberingStyleName", "Numbering 1");
238                 rules1 = oObj.getPropertyValue("NumberingRules");
239                 oObj.setPropertyValue("NumberingStyleName", "Numbering 2");
240                 rules2 = oObj.getPropertyValue("NumberingRules");
241             } catch (com.sun.star.lang.WrappedTargetException e) {
242                 log.println("WARNING !!! Exception getting numbering rules :");
243                 e.printStackTrace(log);
244             } catch (com.sun.star.lang.IllegalArgumentException e) {
245                 log.println("WARNING !!! Exception getting numbering rules :");
246                 e.printStackTrace(log);
247             } catch (com.sun.star.beans.PropertyVetoException e) {
248                 log.println("WARNING !!! Exception getting numbering rules :");
249                 e.printStackTrace(log);
250             } catch (com.sun.star.beans.UnknownPropertyException e) {
251                 log.println("Property 'NumberingStyleName' is not supported.");
252             }
253 
254             rules = new PropertyValueSwitcher(rules1, rules2);
255         }
256     }
257 
258     /**
259      * Tested with custom property tester.
260      */
261     public void _NumberingStyleName() {
262         log.println("Testing with custom Property tester");
263         testProperty("NumberingStyleName", NumberingStyleTester);
264     }
265 
266     /**
267      * Tested with custom property tester.
268      */
269     public void _DropCapCharStyleName() {
270         log.println("Testing with custom Property tester");
271         testProperty("DropCapCharStyleName", charStyleTester);
272     }
273 
274     /**
275      * Tested with custom property tester.
276      */
277     public void _ParaStyleName() {
278         log.println("Testing with custom Property tester");
279         testProperty("ParaStyleName", ParaStyleTester);
280     }
281 
282     /**
283      * Tested with custom property tester.
284      */
285     public void _PageDescName() {
286         log.println("Testing with custom Property tester");
287         testProperty("PageDescName", PageDescTester);
288     }
289 
290     /**
291      * Tested with custom property tester. Before testing property
292      * <code>ParaAdjust</code> is setting to value <code>BLOCK</code>
293      * because setting the property <code>ParaLastLineAdjust</code>
294      * makes sense only in this case.
295      */
296     public void _ParaLastLineAdjust() {
297         log.println("Testing with custom Property tester");
298 
299         try {
300             oObj.setPropertyValue("ParaAdjust",
301                                   com.sun.star.style.ParagraphAdjust.BLOCK);
302         } catch (com.sun.star.lang.WrappedTargetException e) {
303             log.println("Exception occured setting property 'ParagraphAdjust'" + e);
304         } catch (com.sun.star.lang.IllegalArgumentException e) {
305             log.println("Exception occured setting property 'ParagraphAdjust'" + e);
306         } catch (com.sun.star.beans.UnknownPropertyException e) {
307             log.println("Exception occured setting property 'ParagraphAdjust'" + e);
308         } catch (com.sun.star.beans.PropertyVetoException e) {
309             log.println("Exception occured setting property 'ParagraphAdjust'" + e);
310         }
311 
312         testProperty("ParaLastLineAdjust", ShortTester);
313     }
314 
315     /**
316      * Tested with custom property tester.
317      */
318     public void _ParaBackGraphicURL() {
319         log.println("Testing with custom Property tester");
320         testProperty("ParaBackGraphicURL", URLTester);
321     }
322 
323     /**
324      * Tested with custom property tester. <p>
325      * The following property tests are to be completed successfully before :
326      * <ul>
327      *  <li> <code> NumberingStyleName </code> : a numbering style must
328      *  be set before testing this property </li>
329      * </ul>
330      */
331     public void _NumberingLevel() {
332         requiredMethod("NumberingStyleName");
333         log.println("Testing with custom Property tester");
334         testProperty("NumberingLevel", ShortTester);
335     }
336 
337     /**
338      * Tested with custom property tester. <p>
339      */
340     public void _ParaIsConnectBorder() {
341 
342         log.println("Testing with custom Property tester");
343         testProperty("ParaIsConnectBorder", BooleanTester);
344     }
345 
346     /**
347      * Tested with custom property tester.
348      */
349     public void _ParaVertAlignment() {
350         log.println("Testing with custom Property tester");
351         testProperty("ParaVertAlignment", ShortTester);
352     }
353 
354     /**
355      * Tested with com.sun.star.text.NumberingLevel <p>
356      * The value of this property is a com.sun.star.container.XIndexReplace which is represneted by
357      * com.sun.star.text.NumberingLevel.
358      * The following property tests are to be completed successfully before :
359      * <ul>
360      *  <li> <code> NumberingStyleName </code> : a numbering style must
361      *  be set before testing this property </li>
362      * </ul>
363      * @see com.sun.star.text.NumberlingLevel
364      * @see com.sun.star.container.XIndexReplace
365      * @see ifc.text._NumberingLevel
366      */
367     public void _NumberingRules() {
368         requiredMethod("NumberingStyleName");
369 
370         XIndexReplace NumberingRules = null;
371         PropertyValue[] propertyValues = null;
372         try {
373             NumberingRules = (XIndexReplace) AnyConverter.toObject(
374                            new Type(XIndexReplace.class), oObj.getPropertyValue("NumberingRules"));
375         } catch (com.sun.star.lang.IllegalArgumentException ex) {
376             Status.failed( "could not get NumberingRuels: "+ ex.toString() );
377             return;
378         } catch (UnknownPropertyException ex) {
379             Status.failed( "could not get NumberingRuels: "+ ex.toString() );
380             return;
381         } catch (WrappedTargetException ex) {
382             Status.failed( "could not get NumberingRuels: "+ ex.toString() );
383             return;
384         }
385         try {
386             propertyValues = (PropertyValue[]) NumberingRules.getByIndex(0);
387 
388         } catch (com.sun.star.lang.IndexOutOfBoundsException ex) {
389             Status.failed( "could not get NumberlingLevel-Array from NumberingRuels: "+ ex.toString() );
390             return;
391         } catch (WrappedTargetException ex) {
392             Status.failed( "could not get NumberlingLevel-Array from NumberingRuels: "+ ex.toString() );
393             return;
394         }
395 
396         _NumberingLevel numb = new _NumberingLevel((LogWriter)log, tParam, propertyValues);
397 
398         boolean result = numb.testPropertieArray();
399 
400         tRes.tested("NumberingRules", result);
401     }
402 
403     public void _ParaUserDefinedAttributes() {
404         XNameContainer uda = null;
405         boolean res = false;
406 
407         try {
408             try{
409                 uda = (XNameContainer) AnyConverter.toObject(
410                           new Type(XNameContainer.class),
411                           oObj.getPropertyValue("ParaUserDefinedAttributes"));
412             } catch (com.sun.star.lang.IllegalArgumentException e){
413                 log.println("ParaUserDefinedAttributes is empty.");
414                 uda = new _ParagraphProperties.OwnUserDefinedAttributes();
415             }
416             AttributeData attr = new AttributeData();
417             attr.Namespace = "http://www.sun.com/staroffice/apitest/Cellprop";
418             attr.Type = "CDATA";
419             attr.Value = "true";
420             uda.insertByName("Cellprop:has-first-alien-attribute", attr);
421 
422             String[] els = uda.getElementNames();
423             oObj.setPropertyValue("ParaUserDefinedAttributes", uda);
424             uda = (XNameContainer) AnyConverter.toObject(
425                           new Type(XNameContainer.class),
426                           oObj.getPropertyValue("ParaUserDefinedAttributes"));
427             els = uda.getElementNames();
428 
429             Object obj = uda.getByName("Cellprop:has-first-alien-attribute");
430             res = true;
431         } catch (com.sun.star.beans.UnknownPropertyException upe) {
432             if (isOptional("ParaUserDefinedAttributes")) {
433                 log.println("Property is optional and not supported");
434                 res = true;
435             } else {
436                 log.println("Don't know the Property 'ParaUserDefinedAttributes'");
437             }
438         } catch (com.sun.star.lang.WrappedTargetException wte) {
439             log.println(
440                     "WrappedTargetException while getting Property 'ParaUserDefinedAttributes'");
441         } catch (com.sun.star.container.NoSuchElementException nee) {
442             log.println("added Element isn't part of the NameContainer");
443         } catch (com.sun.star.lang.IllegalArgumentException iae) {
444             log.println(
445                     "IllegalArgumentException while getting Property 'ParaUserDefinedAttributes'");
446         } catch (com.sun.star.beans.PropertyVetoException pve) {
447             log.println(
448                     "PropertyVetoException while getting Property 'ParaUserDefinedAttributes'");
449         } catch (com.sun.star.container.ElementExistException eee) {
450             log.println(
451                     "ElementExistException while getting Property 'ParaUserDefinedAttributes'");
452         }
453 
454         tRes.tested("ParaUserDefinedAttributes", res);
455     }
456 
457     private class OwnUserDefinedAttributes implements XNameContainer{
458         Hashtable members = null;
459 
460 
461         public OwnUserDefinedAttributes() {
462             members = new Hashtable();
463         }
464 
465         public Object getByName(String str) throws com.sun.star.container.NoSuchElementException, com.sun.star.lang.WrappedTargetException {
466             return members.get(str);
467         }
468 
469         public String[] getElementNames() {
470             Enumeration oEnum = members.keys();
471             int count = members.size();
472             String[] res = new String[count];
473             int i=0;
474             while(oEnum.hasMoreElements())
475                 res[i] = (String)oEnum.nextElement();
476             return res;
477         }
478 
479         public com.sun.star.uno.Type getElementType() {
480             Enumeration oEnum = members.keys();
481             String key = (String)oEnum.nextElement();
482             Object o = members.get(key);
483             return new Type(o.getClass());
484         }
485 
486         public boolean hasByName(String str) {
487             return members.get(str) != null;
488         }
489 
490         public boolean hasElements() {
491             return members.size() > 0;
492         }
493 
494         public void insertByName(String str, Object obj) throws com.sun.star.lang.IllegalArgumentException, com.sun.star.container.ElementExistException, com.sun.star.lang.WrappedTargetException {
495             members.put(str, obj);
496         }
497 
498         public void removeByName(String str) throws com.sun.star.container.NoSuchElementException, com.sun.star.lang.WrappedTargetException {
499             members.remove(str);
500         }
501 
502         public void replaceByName(String str, Object obj) throws com.sun.star.lang.IllegalArgumentException, com.sun.star.container.NoSuchElementException, com.sun.star.lang.WrappedTargetException {
503             members.put(str, obj);
504         }
505 
506     }
507 } // finish class _ParagraphProperties
508