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.sheet;
25 
26 import java.util.Random;
27 import java.util.StringTokenizer;
28 
29 import lib.MultiMethodTest;
30 
31 import com.sun.star.sheet.NamedRangeFlag;
32 import com.sun.star.sheet.XNamedRange;
33 import com.sun.star.table.CellAddress;
34 
35 /**
36 * Testing <code>com.sun.star.sheet.XNamedRange</code>
37 * interface methods :
38 * <ul>
39 *  <li><code> getContent()</code></li>
40 *  <li><code> setContent()</code></li>
41 *  <li><code> getReferencePosition()</code></li>
42 *  <li><code> setReferencePosition()</code></li>
43 *  <li><code> getType()</code></li>
44 *  <li><code> setType()</code></li>
45 * </ul> <p>
46 * After test completion object environment has to be recreated.
47 * @see com.sun.star.sheet.XNamedRange
48 */
49 public class _XNamedRange extends MultiMethodTest {
50 
51     public XNamedRange oObj = null;
52     String sContent = "";
53     int type = 0;
54     CellAddress CA = null;
55 
56     /**
57     * Test calls the method and compares returned value to value that was set
58     * by method <code>setContent()</code>. <p>
59     * Has <b> OK </b> status if values are equal. <p>
60     * The following method tests are to be completed successfully before :
61     * <ul>
62     *  <li> <code> setContent() </code> : to have current content </li>
63     * </ul>
64     */
_getContent()65     public void _getContent() {
66         requiredMethod("setContent()");
67         String content = oObj.getContent();
68         log.println("Returned content is \"" + content + "\"");
69         boolean bResult = content.equals(sContent);
70         tRes.tested("getContent()", bResult);
71     }
72 
73     /**
74     * Test creates and stores random content and calls the method. <p>
75     * Has <b> OK </b> status if the method successfully returns. <p>
76     */
_setContent()77     public void _setContent() {
78         sContent = getRandomContent("A1;A4:C5;=B2");
79         log.println("Set content to \"" + sContent + "\"");
80         oObj.setContent(sContent);
81 
82         tRes.tested("setContent()", true);
83     }
84 
85     /**
86     * Test calls the method and compares returned value to value that was set
87     * by method <code>setType()</code>. <p>
88     * Has <b> OK </b> status if values are equal. <p>
89     * The following method tests are to be completed successfully before :
90     * <ul>
91     *  <li> <code> setType() </code> : to have current type </li>
92     * </ul>
93     */
_getType()94     public void _getType() {
95         requiredMethod("setType()");
96 
97         int rtype = oObj.getType();
98         log.println("Returned type is " + rtype);
99 
100         tRes.tested("getType()", type == rtype);
101     }
102 
103     /**
104     * Test sets random type and stores it. <p>
105     * Has <b> OK </b> status if the method successfully returns. <p>
106     */
_setType()107     public void _setType() {
108         /*
109          * The type must be 0 or a combination of the NamedRangeFlag
110          * constants and controls if the named range is listed in
111          * dialogs prompting for special ranges
112          *
113          * NamedRangeFlag:    COLUMN_HEADER
114          *                  FILTER_CRITERIA
115          *                  PRINT_AREA
116          *                  ROW_HEADER
117          *
118          */
119         boolean bResult = true;
120         int types[] = { 0,
121                         NamedRangeFlag.COLUMN_HEADER,
122                         NamedRangeFlag.FILTER_CRITERIA,
123                         NamedRangeFlag.PRINT_AREA,
124                         NamedRangeFlag.ROW_HEADER
125                       };
126 
127         Random rnd = new Random();
128         type = types[rnd.nextInt(5)];
129 
130         oObj.setType(type);
131         log.println("The type was set to " + type);
132 
133         tRes.tested("setType()", bResult);
134     }
135 
136     /**
137     * Test calls the method and compares returned value to value that was set
138     * by method <code>setReferencePosition()</code>. <p>
139     * Has <b> OK </b> status if all fields of values are equal. <p>
140     * The following method tests are to be completed successfully before :
141     * <ul>
142     *  <li> <code> setReferencePosition() </code> : to have current reference
143     *  position </li>
144     * </ul>
145     */
_getReferencePosition()146     public void _getReferencePosition() {
147         requiredMethod("setReferencePosition()");
148 
149         CellAddress rCA = oObj.getReferencePosition();
150         log.println("getReferencePosition returned (" +
151             rCA.Sheet + ", " +
152             rCA.Column + ", " + rCA.Row + ")" );
153 
154         boolean bResult = rCA.Sheet == CA.Sheet;
155         bResult &= rCA.Column == CA.Column;
156         bResult &= rCA.Row == CA.Row;
157 
158         tRes.tested("getReferencePosition()", bResult);
159     }
160 
161     /**
162     * Test creates and stores cell address and calls the method. <p>
163     * Has <b> OK </b> status if the method successfully returns. <p>
164     */
_setReferencePosition()165     public void _setReferencePosition() {
166         CA = new CellAddress((short)0, 2, 3);
167         oObj.setReferencePosition(CA);
168         log.println("ReferencePosition was set to (" +
169             CA.Sheet + ", " +
170             CA.Column + ", " + CA.Row + ")");
171 
172         tRes.tested("setReferencePosition()", true);
173     }
174 
175 
176     /**
177     * Method make string of random content.
178     * @return string of random content
179     */
getRandomContent(String str)180     String getRandomContent(String str) {
181 
182         String gRS = "none";
183         Random rnd = new Random();
184 
185         StringTokenizer ST = new StringTokenizer(str, ";");
186         int nr = rnd.nextInt(ST.countTokens());
187         if (nr < 1) nr++;
188 
189         for (int i = 1; i < nr + 1; i++)
190             gRS = ST.nextToken();
191 
192         return gRS;
193 
194     }
195 
196     /**
197     * Forces object environment recreation.
198     */
after()199     protected void after() {
200         disposeEnvironment();
201     }
202 
203 }
204 
205 
206