1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ef39d40dSAndrew Rist  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ef39d40dSAndrew Rist  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19*ef39d40dSAndrew Rist  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package ifc.sheet;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import java.util.Random;
27cdf0e10cSrcweir 
28cdf0e10cSrcweir import lib.MultiMethodTest;
29cdf0e10cSrcweir import lib.Status;
30cdf0e10cSrcweir import lib.StatusException;
31cdf0e10cSrcweir 
32cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
33cdf0e10cSrcweir import com.sun.star.container.XNameAccess;
34cdf0e10cSrcweir import com.sun.star.sheet.XRecentFunctions;
35cdf0e10cSrcweir 
36cdf0e10cSrcweir /**
37cdf0e10cSrcweir * Testing <code>com.sun.star.sheet.XRecentFunctions</code>
38cdf0e10cSrcweir * interface methods :
39cdf0e10cSrcweir * <ul>
40cdf0e10cSrcweir *  <li><code> getRecentFunctionIds()</code></li>
41cdf0e10cSrcweir *  <li><code> setRecentFunctionIds()</code></li>
42cdf0e10cSrcweir *  <li><code> getMaxRecentFunctions()</code></li>
43cdf0e10cSrcweir * </ul> <p>
44cdf0e10cSrcweir * This test needs the following object relations :
45cdf0e10cSrcweir * <ul>
46cdf0e10cSrcweir *  <li> <code>'FUNCTIONLIST'</code> (of type <code>XNameAccess</code>):
47cdf0e10cSrcweir *   to have the set of available functions </li>
48cdf0e10cSrcweir * <ul> <p>
49cdf0e10cSrcweir * @see com.sun.star.sheet.XRecentFunctions
50cdf0e10cSrcweir */
51cdf0e10cSrcweir public class _XRecentFunctions extends MultiMethodTest {
52cdf0e10cSrcweir 
53cdf0e10cSrcweir     public XRecentFunctions oObj = null;
54cdf0e10cSrcweir     int iMaxNumber = 0;
55cdf0e10cSrcweir 
56cdf0e10cSrcweir     /**
57cdf0e10cSrcweir     * Test calls the method, checks returned value and stores it. <p>
58cdf0e10cSrcweir     * Has <b> OK </b> status if returned value isn't equal to zero. <p>
59cdf0e10cSrcweir     */
_getMaxRecentFunctions()60cdf0e10cSrcweir     public void _getMaxRecentFunctions() {
61cdf0e10cSrcweir 
62cdf0e10cSrcweir         iMaxNumber = oObj.getMaxRecentFunctions();
63cdf0e10cSrcweir         log.println("Maximum recent functions : " + iMaxNumber);
64cdf0e10cSrcweir 
65cdf0e10cSrcweir         tRes.tested("getMaxRecentFunctions()", iMaxNumber != 0);
66cdf0e10cSrcweir     }
67cdf0e10cSrcweir 
68cdf0e10cSrcweir     /**
69cdf0e10cSrcweir     * Test calls the method and checks returned value. <p>
70cdf0e10cSrcweir     * Has <b> OK </b> status if returned value isn't null, if length of returned
71cdf0e10cSrcweir     * array is equal or less to the maximum number of functions and obtained
72cdf0e10cSrcweir     * array doesn't contain equal functions. <p>
73cdf0e10cSrcweir     * The following method tests are to be completed successfully before :
74cdf0e10cSrcweir     * <ul>
75cdf0e10cSrcweir     *  <li> <code> getMaxRecentFunctions() </code> : to have the maximum number
76cdf0e10cSrcweir     *  of recent functions </li>
77cdf0e10cSrcweir     * </ul>
78cdf0e10cSrcweir     */
_getRecentFunctionIds()79cdf0e10cSrcweir     public void _getRecentFunctionIds() {
80cdf0e10cSrcweir         requiredMethod("getMaxRecentFunctions()");
81cdf0e10cSrcweir 
82cdf0e10cSrcweir         boolean bResult = true;
83cdf0e10cSrcweir         int[] IDs = null;
84cdf0e10cSrcweir         int iNumber = 0;
85cdf0e10cSrcweir 
86cdf0e10cSrcweir         IDs = oObj.getRecentFunctionIds();
87cdf0e10cSrcweir         iNumber = IDs.length;
88cdf0e10cSrcweir         bResult &= (iNumber <= iMaxNumber);
89cdf0e10cSrcweir         log.println("Now there are " + iNumber + " recent functions");
90cdf0e10cSrcweir         bResult &= (IDs != null);
91cdf0e10cSrcweir         if (bResult) {
92cdf0e10cSrcweir             for (int i = 0; i < iNumber - 1; i++)
93cdf0e10cSrcweir                 for (int j = i + 1; j < iNumber; j++) {
94cdf0e10cSrcweir                     bResult &= (IDs[i] != IDs[j]);
95cdf0e10cSrcweir                 }
96cdf0e10cSrcweir         }
97cdf0e10cSrcweir 
98cdf0e10cSrcweir         tRes.tested("getRecentFunctionIds()", bResult);
99cdf0e10cSrcweir     }
100cdf0e10cSrcweir 
101cdf0e10cSrcweir     /**
102cdf0e10cSrcweir     * Test gets the set of available functions, sets empty list of recent
103cdf0e10cSrcweir     * functions, sets list of maximum size. <p>
104cdf0e10cSrcweir     * Has <b> OK </b> status if length of recent function list is equal to zero
105cdf0e10cSrcweir     * after list was set to empty, if length of list is equal to maximum size
106cdf0e10cSrcweir     * after list was set to it's maximum size and no exception were thrown. <p>
107cdf0e10cSrcweir     * The following method tests are to be completed successfully before :
108cdf0e10cSrcweir     * <ul>
109cdf0e10cSrcweir     *  <li> <code> getMaxRecentFunctions() </code> : to have the maximum number
110cdf0e10cSrcweir     *  of recent functions </li>
111cdf0e10cSrcweir     * </ul>
112cdf0e10cSrcweir     */
_setRecentFunctionIds()113cdf0e10cSrcweir     public void _setRecentFunctionIds() {
114cdf0e10cSrcweir         requiredMethod("getMaxRecentFunctions()");
115cdf0e10cSrcweir 
116cdf0e10cSrcweir         boolean bResult = true;
117cdf0e10cSrcweir         int[] IDs = new int[0];
118cdf0e10cSrcweir         XNameAccess functionList = null;
119cdf0e10cSrcweir 
120cdf0e10cSrcweir         log.println("First, get the set of available functions.");
121cdf0e10cSrcweir         functionList = (XNameAccess)tEnv.getObjRelation("FUNCTIONLIST");
122cdf0e10cSrcweir         if (functionList == null) throw new StatusException(Status.failed
123cdf0e10cSrcweir             ("Relation 'FUNCTIONLIST' not found"));
124cdf0e10cSrcweir 
125cdf0e10cSrcweir         log.println("Now trying to set empty list.");
126cdf0e10cSrcweir         oObj.setRecentFunctionIds(IDs);
127cdf0e10cSrcweir         bResult &= (oObj.getRecentFunctionIds().length == 0);
128cdf0e10cSrcweir 
129cdf0e10cSrcweir         log.println("Now trying to set list of maximum size.");
130cdf0e10cSrcweir         String[] names = functionList.getElementNames();
131cdf0e10cSrcweir         Random rnd = new Random();
132cdf0e10cSrcweir 
133cdf0e10cSrcweir         IDs = new int[iMaxNumber];
134cdf0e10cSrcweir         int startIdx = rnd.nextInt(names.length - iMaxNumber - 1) + 1;
135cdf0e10cSrcweir 
136cdf0e10cSrcweir         try {
137cdf0e10cSrcweir             for (int i = startIdx; i < startIdx + iMaxNumber; i++) {
138cdf0e10cSrcweir                 PropertyValue[] propVals = (PropertyValue[])
139cdf0e10cSrcweir                     functionList.getByName(names[i]);
140cdf0e10cSrcweir                 for (int j = 0; j < propVals.length; j++) {
141cdf0e10cSrcweir                     String propName = (String)propVals[j].Name;
142cdf0e10cSrcweir                     if (propName.equals("Id")) {
143cdf0e10cSrcweir                         IDs[i - startIdx] =
144cdf0e10cSrcweir                             ((Integer)propVals[j].Value).intValue();
145cdf0e10cSrcweir                         break;
146cdf0e10cSrcweir                     }
147cdf0e10cSrcweir                 }
148cdf0e10cSrcweir             }
149cdf0e10cSrcweir         } catch(com.sun.star.lang.WrappedTargetException e) {
150cdf0e10cSrcweir             e.printStackTrace(log);
151cdf0e10cSrcweir             bResult = false;
152cdf0e10cSrcweir         } catch(com.sun.star.container.NoSuchElementException e) {
153cdf0e10cSrcweir             e.printStackTrace(log);
154cdf0e10cSrcweir             bResult = false;
155cdf0e10cSrcweir         }
156cdf0e10cSrcweir 
157cdf0e10cSrcweir         oObj.setRecentFunctionIds(IDs);
158cdf0e10cSrcweir         bResult &= (oObj.getRecentFunctionIds().length == iMaxNumber);
159cdf0e10cSrcweir 
160cdf0e10cSrcweir         tRes.tested("setRecentFunctionIds()", bResult);
161cdf0e10cSrcweir     }
162cdf0e10cSrcweir 
163cdf0e10cSrcweir }
164cdf0e10cSrcweir 
165