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