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