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