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 
28 import lib.MultiMethodTest;
29 import lib.Status;
30 import lib.StatusException;
31 
32 import com.sun.star.beans.PropertyValue;
33 import com.sun.star.container.XNameAccess;
34 import com.sun.star.sheet.XRecentFunctions;
35 
36 /**
37 * Testing <code>com.sun.star.sheet.XRecentFunctions</code>
38 * interface methods :
39 * <ul>
40 *  <li><code> getRecentFunctionIds()</code></li>
41 *  <li><code> setRecentFunctionIds()</code></li>
42 *  <li><code> getMaxRecentFunctions()</code></li>
43 * </ul> <p>
44 * This test needs the following object relations :
45 * <ul>
46 *  <li> <code>'FUNCTIONLIST'</code> (of type <code>XNameAccess</code>):
47 *   to have the set of available functions </li>
48 * <ul> <p>
49 * @see com.sun.star.sheet.XRecentFunctions
50 */
51 public class _XRecentFunctions extends MultiMethodTest {
52 
53     public XRecentFunctions oObj = null;
54     int iMaxNumber = 0;
55 
56     /**
57     * Test calls the method, checks returned value and stores it. <p>
58     * Has <b> OK </b> status if returned value isn't equal to zero. <p>
59     */
_getMaxRecentFunctions()60     public void _getMaxRecentFunctions() {
61 
62         iMaxNumber = oObj.getMaxRecentFunctions();
63         log.println("Maximum recent functions : " + iMaxNumber);
64 
65         tRes.tested("getMaxRecentFunctions()", iMaxNumber != 0);
66     }
67 
68     /**
69     * Test calls the method and checks returned value. <p>
70     * Has <b> OK </b> status if returned value isn't null, if length of returned
71     * array is equal or less to the maximum number of functions and obtained
72     * array doesn't contain equal functions. <p>
73     * The following method tests are to be completed successfully before :
74     * <ul>
75     *  <li> <code> getMaxRecentFunctions() </code> : to have the maximum number
76     *  of recent functions </li>
77     * </ul>
78     */
_getRecentFunctionIds()79     public void _getRecentFunctionIds() {
80         requiredMethod("getMaxRecentFunctions()");
81 
82         boolean bResult = true;
83         int[] IDs = null;
84         int iNumber = 0;
85 
86         IDs = oObj.getRecentFunctionIds();
87         iNumber = IDs.length;
88         bResult &= (iNumber <= iMaxNumber);
89         log.println("Now there are " + iNumber + " recent functions");
90         bResult &= (IDs != null);
91         if (bResult) {
92             for (int i = 0; i < iNumber - 1; i++)
93                 for (int j = i + 1; j < iNumber; j++) {
94                     bResult &= (IDs[i] != IDs[j]);
95                 }
96         }
97 
98         tRes.tested("getRecentFunctionIds()", bResult);
99     }
100 
101     /**
102     * Test gets the set of available functions, sets empty list of recent
103     * functions, sets list of maximum size. <p>
104     * Has <b> OK </b> status if length of recent function list is equal to zero
105     * after list was set to empty, if length of list is equal to maximum size
106     * after list was set to it's maximum size and no exception were thrown. <p>
107     * The following method tests are to be completed successfully before :
108     * <ul>
109     *  <li> <code> getMaxRecentFunctions() </code> : to have the maximum number
110     *  of recent functions </li>
111     * </ul>
112     */
_setRecentFunctionIds()113     public void _setRecentFunctionIds() {
114         requiredMethod("getMaxRecentFunctions()");
115 
116         boolean bResult = true;
117         int[] IDs = new int[0];
118         XNameAccess functionList = null;
119 
120         log.println("First, get the set of available functions.");
121         functionList = (XNameAccess)tEnv.getObjRelation("FUNCTIONLIST");
122         if (functionList == null) throw new StatusException(Status.failed
123             ("Relation 'FUNCTIONLIST' not found"));
124 
125         log.println("Now trying to set empty list.");
126         oObj.setRecentFunctionIds(IDs);
127         bResult &= (oObj.getRecentFunctionIds().length == 0);
128 
129         log.println("Now trying to set list of maximum size.");
130         String[] names = functionList.getElementNames();
131         Random rnd = new Random();
132 
133         IDs = new int[iMaxNumber];
134         int startIdx = rnd.nextInt(names.length - iMaxNumber - 1) + 1;
135 
136         try {
137             for (int i = startIdx; i < startIdx + iMaxNumber; i++) {
138                 PropertyValue[] propVals = (PropertyValue[])
139                     functionList.getByName(names[i]);
140                 for (int j = 0; j < propVals.length; j++) {
141                     String propName = (String)propVals[j].Name;
142                     if (propName.equals("Id")) {
143                         IDs[i - startIdx] =
144                             ((Integer)propVals[j].Value).intValue();
145                         break;
146                     }
147                 }
148             }
149         } catch(com.sun.star.lang.WrappedTargetException e) {
150             e.printStackTrace(log);
151             bResult = false;
152         } catch(com.sun.star.container.NoSuchElementException e) {
153             e.printStackTrace(log);
154             bResult = false;
155         }
156 
157         oObj.setRecentFunctionIds(IDs);
158         bResult &= (oObj.getRecentFunctionIds().length == iMaxNumber);
159 
160         tRes.tested("setRecentFunctionIds()", bResult);
161     }
162 
163 }
164 
165