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.text;
24 
25 import com.sun.star.beans.PropertyValue;
26 import com.sun.star.container.XIndexAccess;
27 import com.sun.star.container.XIndexReplace;
28 import com.sun.star.text.XTextColumns;
29 import com.sun.star.uno.UnoRuntime;
30 
31 import lib.MultiPropertyTest;
32 
33 
34 /**
35 * Testing <code>com.sun.star.text.BaseIndex</code>
36 * service properties :
37 * <ul>
38 *  <li><code> Title</code></li>
39 *  <li><code> IsProtected</code></li>
40 *  <li><code> ParaStyleHeading</code></li>
41 *  <li><code> ParaStyleLevel1</code></li>
42 *  <li><code> ParaStyleLevel2</code></li>
43 *  <li><code> ParaStyleLevel3</code></li>
44 *  <li><code> ParaStyleLevel4</code></li>
45 *  <li><code> ParaStyleLevel5</code></li>
46 *  <li><code> ParaStyleLevel6</code></li>
47 *  <li><code> ParaStyleLevel7</code></li>
48 *  <li><code> ParaStyleLevel8</code></li>
49 *  <li><code> ParaStyleLevel9</code></li>
50 *  <li><code> ParaStyleLevel10</code></li>
51 *  <li><code> ParaStyleSeparator</code></li>
52 *  <li><code> TextColumns</code></li>
53 *  <li><code> BackGraphicURL</code></li>
54 *  <li><code> BackGraphicFilter</code></li>
55 *  <li><code> BackGraphicLocation</code></li>
56 *  <li><code> BackTransparent</code></li>
57 *  <li><code> LevelFormat</code></li>
58 *  <li><code> CreateFromChapter</code></li>
59 * </ul> <p>
60 * Properties testing is automated by <code>lib.MultiPropertyTest</code>.
61 * @see com.sun.star.text.BaseIndex
62 */
63 public class _BaseIndex extends MultiPropertyTest {
64     /**
65     * Redefined method returns object, that contains changed property value.
66     */
67     protected PropertyTester CustomTester = new PropertyTester() {
68         protected Object getNewValue(String propName, Object oldValue) {
69             XTextColumns TC = (XTextColumns) UnoRuntime.queryInterface(
70                                       XTextColumns.class, oldValue);
71             TC.setColumnCount((short) (TC.getColumnCount() + (short) 1));
72 
73             return TC;
74         }
75     };
76 
77     /**
78     * New value must be defined for this property.
79     */
_TextColumns()80     public void _TextColumns() {
81         log.println(
82                 "Testing property 'TextColumns' with custom property tester");
83         testProperty("TextColumns", CustomTester);
84     }
85 
86     /**
87      * The value of this property is a collection of document index
88      * level formats. This property is tested in the following manner :
89      * the property value (a collection) is obtained, the first element
90      * of this collection is replaced with new non-empty array
91      * (<code>PropertyValue[][]</code>) with some properties set.
92      * After that the collection is set back as property value. <p>
93      *
94      * Comparing of set and got <code>PropertyValue</code> arrays
95      * is difficult because values can be changed after setting
96      * by service miplementation. <p>
97      *
98      * Has <b>OK</b> status if the collection again gotten, has a
99      * new first element (i.e. lengths of the old array and the array
100      * get are different or their contents differ).
101      */
_LevelFormat()102     public void _LevelFormat() {
103         log.println(
104                 "Testing property 'LevelFormat' with custom property tester");
105         testProperty("LevelFormat",
106                      new PropertyTester() {
107             PropertyValue[][] newVal = null;
108             PropertyValue[][] oldVal = null;
109 
110             protected Object getNewValue(String propName, Object oldValue) {
111                 XIndexReplace indProp = (XIndexReplace) UnoRuntime.queryInterface(
112                                                 XIndexReplace.class, oldValue);
113 
114                 try {
115                     oldVal = (PropertyValue[][]) indProp.getByIndex(0);
116 
117                     log.println("Get:");
118                     printLevelFormatProperty(oldValue);
119 
120                     newVal = new PropertyValue[1][2];
121 
122                     for (int i = 0; i < newVal[0].length; i++) {
123                         newVal[0][i] = new PropertyValue();
124                     }
125 
126                     newVal[0][1].Name = "TokenType";
127                     newVal[0][1].Value = "TokenEntryText";
128                     newVal[0][0].Name = "Text";
129                     newVal[0][0].Value = "BaseIndex";
130 
131                     indProp.replaceByIndex(0, newVal);
132                 } catch (com.sun.star.lang.WrappedTargetException e) {
133                     log.println("Exception occurred while testing LevelFormat");
134                     e.printStackTrace(log);
135                 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
136                     log.println("Exception occurred while testing LevelFormat");
137                     e.printStackTrace(log);
138                 } catch (com.sun.star.lang.IllegalArgumentException e) {
139                     log.println("Exception occurred while testing LevelFormat");
140                     e.printStackTrace(log);
141                 }
142 
143                 return indProp;
144             }
145 
146             protected void checkResult(String propName, Object oldValue,
147                                        Object newValue, Object resValue,
148                                        Exception exception)
149                                 throws Exception {
150                 PropertyValue[][] res = (PropertyValue[][]) ((XIndexAccess) UnoRuntime.queryInterface(
151                                                                      XIndexAccess.class,
152                                                                      resValue)).getByIndex(0);
153 
154                 log.println("Result:");
155                 printLevelFormatProperty(resValue);
156 
157                 boolean result = (res.length != oldVal.length) ||
158                                  !util.ValueComparer.equalValue(res, oldVal);
159 
160                 tRes.tested(propName, result);
161             }
162         });
163     }
164 
165     /**
166      * Outputs full description of 'LevelFormat' property
167      * value into <code>log</code>.
168      */
printLevelFormatProperty(Object value)169     private void printLevelFormatProperty(Object value) {
170         XIndexReplace indProp = (XIndexReplace) UnoRuntime.queryInterface(
171                                         XIndexReplace.class, value);
172         PropertyValue[][] val = null;
173 
174         try {
175             log.println(" \u0421ollection has " + indProp.getCount() +
176                         " elements : ");
177 
178             for (int i = 0; i < indProp.getCount(); i++) {
179                 val = (PropertyValue[][]) indProp.getByIndex(i);
180 
181                 log.println("  " + i + ": has " + val.length + " levels :");
182 
183                 for (int j = 0; j < val.length; j++) {
184                     log.println("    " + j + " level :");
185 
186                     for (int k = 0; k < val[j].length; k++) {
187                         log.println("      " + val[j][k].Name + "=" +
188                                     val[j][k].Value);
189                     }
190                 }
191             }
192         } catch (com.sun.star.lang.WrappedTargetException e) {
193             log.println("Exception occurred while printing LevelFormat");
194             e.printStackTrace(log);
195         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
196             log.println("Exception occurred while printing LevelFormat");
197             e.printStackTrace(log);
198         }
199     }
200 } // finish class _NumberingRules
201