xref: /AOO41X/test/testuno/source/testlib/uno/TestUtil.java (revision e6e6073ddaad3a04a985e8f05823629a884eb203)
1*e6e6073dSLiu Zhe /**************************************************************
2*e6e6073dSLiu Zhe  *
3*e6e6073dSLiu Zhe  * Licensed to the Apache Software Foundation (ASF) under one
4*e6e6073dSLiu Zhe  * or more contributor license agreements.  See the NOTICE file
5*e6e6073dSLiu Zhe  * distributed with this work for additional information
6*e6e6073dSLiu Zhe  * regarding copyright ownership.  The ASF licenses this file
7*e6e6073dSLiu Zhe  * to you under the Apache License, Version 2.0 (the
8*e6e6073dSLiu Zhe  * "License"); you may not use this file except in compliance
9*e6e6073dSLiu Zhe  * with the License.  You may obtain a copy of the License at
10*e6e6073dSLiu Zhe  *
11*e6e6073dSLiu Zhe  *   http://www.apache.org/licenses/LICENSE-2.0
12*e6e6073dSLiu Zhe  *
13*e6e6073dSLiu Zhe  * Unless required by applicable law or agreed to in writing,
14*e6e6073dSLiu Zhe  * software distributed under the License is distributed on an
15*e6e6073dSLiu Zhe  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*e6e6073dSLiu Zhe  * KIND, either express or implied.  See the License for the
17*e6e6073dSLiu Zhe  * specific language governing permissions and limitations
18*e6e6073dSLiu Zhe  * under the License.
19*e6e6073dSLiu Zhe  *
20*e6e6073dSLiu Zhe  *************************************************************/
21*e6e6073dSLiu Zhe 
22*e6e6073dSLiu Zhe 
23*e6e6073dSLiu Zhe package testlib.uno;
24*e6e6073dSLiu Zhe 
25*e6e6073dSLiu Zhe import java.util.Random;
26*e6e6073dSLiu Zhe 
27*e6e6073dSLiu Zhe import com.sun.star.beans.Property;
28*e6e6073dSLiu Zhe import com.sun.star.beans.PropertyAttribute;
29*e6e6073dSLiu Zhe import com.sun.star.beans.XPropertySet;
30*e6e6073dSLiu Zhe import com.sun.star.beans.XPropertySetInfo;
31*e6e6073dSLiu Zhe import com.sun.star.uno.UnoRuntime;
32*e6e6073dSLiu Zhe 
33*e6e6073dSLiu Zhe import testlib.uno.CellInfo;
34*e6e6073dSLiu Zhe 
35*e6e6073dSLiu Zhe 
36*e6e6073dSLiu Zhe /**
37*e6e6073dSLiu Zhe  * Utilities for UNO automation testing
38*e6e6073dSLiu Zhe  *
39*e6e6073dSLiu Zhe  */
40*e6e6073dSLiu Zhe 
41*e6e6073dSLiu Zhe public class TestUtil {
42*e6e6073dSLiu Zhe 
43*e6e6073dSLiu Zhe 	private static int colLimit = 1024;
44*e6e6073dSLiu Zhe 	private static int rowLimit = 1048576;
45*e6e6073dSLiu Zhe 	private static Random random = new Random();
46*e6e6073dSLiu Zhe 
TestUtil()47*e6e6073dSLiu Zhe 	public TestUtil() {
48*e6e6073dSLiu Zhe 
49*e6e6073dSLiu Zhe 	}
50*e6e6073dSLiu Zhe 
51*e6e6073dSLiu Zhe 	/**
52*e6e6073dSLiu Zhe 	 * Generate a random cell index
53*e6e6073dSLiu Zhe 	 * @return cellIndex    column: cellIndex[0]  row: cellIndex[1]
54*e6e6073dSLiu Zhe 	 * @throws Exception
55*e6e6073dSLiu Zhe 	 */
randCell()56*e6e6073dSLiu Zhe 	public static CellInfo randCell() throws Exception {
57*e6e6073dSLiu Zhe 		CellInfo cInfo = new CellInfo();
58*e6e6073dSLiu Zhe 
59*e6e6073dSLiu Zhe 		cInfo.setCol(random.nextInt(colLimit));
60*e6e6073dSLiu Zhe 		cInfo.setRow(random.nextInt(rowLimit));
61*e6e6073dSLiu Zhe 
62*e6e6073dSLiu Zhe 		return cInfo;
63*e6e6073dSLiu Zhe 	}
64*e6e6073dSLiu Zhe 
65*e6e6073dSLiu Zhe 	/**
66*e6e6073dSLiu Zhe 	 * Generate a random cell index, in the limited range
67*e6e6073dSLiu Zhe 	 * @param colTop  The max column limit
68*e6e6073dSLiu Zhe 	 * @param rowTop  The max row limit
69*e6e6073dSLiu Zhe 	 * @return
70*e6e6073dSLiu Zhe 	 * @throws Exception
71*e6e6073dSLiu Zhe 	 */
randCell(int colTop, int rowTop)72*e6e6073dSLiu Zhe 	public static CellInfo randCell(int colTop, int rowTop) throws Exception {
73*e6e6073dSLiu Zhe 		CellInfo cInfo = new CellInfo();
74*e6e6073dSLiu Zhe 
75*e6e6073dSLiu Zhe 		cInfo.setCol(random.nextInt(colTop));
76*e6e6073dSLiu Zhe 		cInfo.setRow(random.nextInt(rowTop));
77*e6e6073dSLiu Zhe 
78*e6e6073dSLiu Zhe 		return cInfo;
79*e6e6073dSLiu Zhe 	}
80*e6e6073dSLiu Zhe 
81*e6e6073dSLiu Zhe 	/**
82*e6e6073dSLiu Zhe 	 * Generate a font size number in limited range
83*e6e6073dSLiu Zhe 	 * @param max  The font size in Excel2003 is [1,409]
84*e6e6073dSLiu Zhe 	 * @return
85*e6e6073dSLiu Zhe 	 * @throws Exception
86*e6e6073dSLiu Zhe 	 */
randFontSize(int max)87*e6e6073dSLiu Zhe 	public static double randFontSize(int max) throws Exception {
88*e6e6073dSLiu Zhe 		double basic = random.nextInt(max * 2);
89*e6e6073dSLiu Zhe 		double size = 1;
90*e6e6073dSLiu Zhe 		if (basic < 2) {
91*e6e6073dSLiu Zhe 			size = 1;
92*e6e6073dSLiu Zhe 		}
93*e6e6073dSLiu Zhe 		else {
94*e6e6073dSLiu Zhe 			size = basic / 2;
95*e6e6073dSLiu Zhe 		}
96*e6e6073dSLiu Zhe 
97*e6e6073dSLiu Zhe 		return size;
98*e6e6073dSLiu Zhe 	}
99*e6e6073dSLiu Zhe 
100*e6e6073dSLiu Zhe 	/**
101*e6e6073dSLiu Zhe 	 * Generate a series of font size number
102*e6e6073dSLiu Zhe 	 * @param listSize
103*e6e6073dSLiu Zhe 	 * @param max
104*e6e6073dSLiu Zhe 	 * @return
105*e6e6073dSLiu Zhe 	 * @throws Exception
106*e6e6073dSLiu Zhe 	 */
randFontSizeList(int listSize, int max)107*e6e6073dSLiu Zhe 	public static double[] randFontSizeList(int listSize, int max) throws Exception {
108*e6e6073dSLiu Zhe 		double[] sizeList = new double[listSize];
109*e6e6073dSLiu Zhe 		for (int i =0; i < listSize; i++) {
110*e6e6073dSLiu Zhe 			sizeList[i] = randFontSize(max);
111*e6e6073dSLiu Zhe 		}
112*e6e6073dSLiu Zhe 		return sizeList;
113*e6e6073dSLiu Zhe 	}
114*e6e6073dSLiu Zhe 
115*e6e6073dSLiu Zhe 	/**
116*e6e6073dSLiu Zhe 	 * Generate a random decimal RGB color number
117*e6e6073dSLiu Zhe 	 * @return
118*e6e6073dSLiu Zhe 	 * @throws Exception
119*e6e6073dSLiu Zhe 	 */
randColor()120*e6e6073dSLiu Zhe 	public static int randColor() throws Exception {
121*e6e6073dSLiu Zhe 		int r = random.nextInt(256);
122*e6e6073dSLiu Zhe 		int g = random.nextInt(256);
123*e6e6073dSLiu Zhe 		int b = random.nextInt(256);
124*e6e6073dSLiu Zhe 
125*e6e6073dSLiu Zhe 		return r * 65536 + g * 256 + b;
126*e6e6073dSLiu Zhe 	}
127*e6e6073dSLiu Zhe 
128*e6e6073dSLiu Zhe 	/**
129*e6e6073dSLiu Zhe 	 * Generate a random decimal RGB color number in limited color space
130*e6e6073dSLiu Zhe 	 * @param rMax  The R value limit, get a value in [0, rMax]
131*e6e6073dSLiu Zhe 	 * @param gMax  The G value limit, get a value in [0, gMax]
132*e6e6073dSLiu Zhe 	 * @param bMax  The B value limit, get a value in [0, bMax]
133*e6e6073dSLiu Zhe 	 * @return
134*e6e6073dSLiu Zhe 	 * @throws Exception
135*e6e6073dSLiu Zhe 	 */
randColor(int rMax, int gMax, int bMax)136*e6e6073dSLiu Zhe 	public static int randColor(int rMax, int gMax, int bMax) throws Exception {
137*e6e6073dSLiu Zhe 		int r = random.nextInt(rMax + 1) % 256;
138*e6e6073dSLiu Zhe 		int g = random.nextInt(gMax + 1) % 256;
139*e6e6073dSLiu Zhe 		int b = random.nextInt(bMax + 1) % 256;
140*e6e6073dSLiu Zhe 
141*e6e6073dSLiu Zhe 		return r * 65536 + g * 256 + b;
142*e6e6073dSLiu Zhe 	}
143*e6e6073dSLiu Zhe 
144*e6e6073dSLiu Zhe 	/**
145*e6e6073dSLiu Zhe 	 * Generate a series of decimal RGB color number
146*e6e6073dSLiu Zhe 	 * @param size Set the quantity of random color value generated into the array
147*e6e6073dSLiu Zhe 	 * @return
148*e6e6073dSLiu Zhe 	 * @throws Exception
149*e6e6073dSLiu Zhe 	 */
randColorList(int size)150*e6e6073dSLiu Zhe 	public static int[] randColorList(int size) throws Exception {
151*e6e6073dSLiu Zhe 		int[] colorList = new int[size];
152*e6e6073dSLiu Zhe 		for (int i = 0; i < size; i++) {
153*e6e6073dSLiu Zhe 			colorList[i] = randColor();
154*e6e6073dSLiu Zhe 		}
155*e6e6073dSLiu Zhe 
156*e6e6073dSLiu Zhe 		return colorList;
157*e6e6073dSLiu Zhe 	}
158*e6e6073dSLiu Zhe 
159*e6e6073dSLiu Zhe 	/**
160*e6e6073dSLiu Zhe 	 * Add "=" before a string
161*e6e6073dSLiu Zhe 	 * @param expression
162*e6e6073dSLiu Zhe 	 * @return
163*e6e6073dSLiu Zhe 	 */
toFormula(String expression)164*e6e6073dSLiu Zhe 	public static String toFormula(String expression) {
165*e6e6073dSLiu Zhe 		return "=" + expression;
166*e6e6073dSLiu Zhe 	}
167*e6e6073dSLiu Zhe 
168*e6e6073dSLiu Zhe 	/**
169*e6e6073dSLiu Zhe 	 * Use specific operator to connect a series of number
170*e6e6073dSLiu Zhe 	 * @param number
171*e6e6073dSLiu Zhe 	 * @param operator
172*e6e6073dSLiu Zhe 	 * @return
173*e6e6073dSLiu Zhe 	 */
connectByOperator(double[] number, String operator)174*e6e6073dSLiu Zhe 	public static String connectByOperator(double[] number, String operator) throws Exception{
175*e6e6073dSLiu Zhe 		StringBuffer buffer = new StringBuffer();
176*e6e6073dSLiu Zhe 
177*e6e6073dSLiu Zhe 		for (int i = 0; i < number.length; i++) {
178*e6e6073dSLiu Zhe 			buffer.append(number[i]);
179*e6e6073dSLiu Zhe 			if (i < number.length - 1) {
180*e6e6073dSLiu Zhe 				buffer.append(operator);
181*e6e6073dSLiu Zhe 			}
182*e6e6073dSLiu Zhe 		}
183*e6e6073dSLiu Zhe 		return buffer.toString();
184*e6e6073dSLiu Zhe 	}
185*e6e6073dSLiu Zhe 
186*e6e6073dSLiu Zhe 	/**
187*e6e6073dSLiu Zhe 	 * Print the properties list of specific object to console
188*e6e6073dSLiu Zhe 	 * @param obj   The instance of the object of which the property list you want to get. e.g. instance of XCell.
189*e6e6073dSLiu Zhe 	 * @throws Exception
190*e6e6073dSLiu Zhe 	 */
printPropertiesList(Object obj)191*e6e6073dSLiu Zhe 	public static void printPropertiesList(Object obj) throws Exception {
192*e6e6073dSLiu Zhe 		// Get the property set of specific object
193*e6e6073dSLiu Zhe 		XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, obj);
194*e6e6073dSLiu Zhe 		XPropertySetInfo xPropertySetInfo = xPropertySet.getPropertySetInfo();
195*e6e6073dSLiu Zhe 
196*e6e6073dSLiu Zhe 	    // Get all properties info
197*e6e6073dSLiu Zhe 	    Property[] aProps = xPropertySetInfo.getProperties();
198*e6e6073dSLiu Zhe 
199*e6e6073dSLiu Zhe 	    for (int i = 0; i < aProps.length; i++) {
200*e6e6073dSLiu Zhe 	        // Print name and type of each property
201*e6e6073dSLiu Zhe 	    	System.out.print("[" + (i + 1) + "]: Name=\"" + aProps[i].Name + "\" " + aProps[i].Type.toString() + " (");
202*e6e6073dSLiu Zhe 
203*e6e6073dSLiu Zhe 	        // Get flag. pay attention to the READONLY properties
204*e6e6073dSLiu Zhe 	        short nAttribs = aProps[i].Attributes;
205*e6e6073dSLiu Zhe 	        if ((nAttribs & PropertyAttribute.MAYBEVOID) != 0)
206*e6e6073dSLiu Zhe 	        	System.out.print("MAYBEVOID|");
207*e6e6073dSLiu Zhe 	        if ((nAttribs & PropertyAttribute.BOUND) != 0)
208*e6e6073dSLiu Zhe 	            System.out.print("BOUND|");
209*e6e6073dSLiu Zhe 	        if ((nAttribs & PropertyAttribute.CONSTRAINED) != 0)
210*e6e6073dSLiu Zhe 	            System.out.print("CONSTRAINED|");
211*e6e6073dSLiu Zhe 	        if ((nAttribs & PropertyAttribute.READONLY) != 0)
212*e6e6073dSLiu Zhe 	            System.out.print("READONLY|");
213*e6e6073dSLiu Zhe 	        if ((nAttribs & PropertyAttribute.TRANSIENT) != 0)
214*e6e6073dSLiu Zhe 	            System.out.print("TRANSIENT|");
215*e6e6073dSLiu Zhe 	        if ((nAttribs & PropertyAttribute.MAYBEAMBIGUOUS ) != 0)
216*e6e6073dSLiu Zhe 	            System.out.print("MAYBEAMBIGUOUS|");
217*e6e6073dSLiu Zhe 	        if ((nAttribs & PropertyAttribute.MAYBEDEFAULT) != 0)
218*e6e6073dSLiu Zhe 	            System.out.print("MAYBEDEFAULT|");
219*e6e6073dSLiu Zhe 	        if ((nAttribs & PropertyAttribute.REMOVEABLE) != 0)
220*e6e6073dSLiu Zhe 	            System.out.print("REMOVEABLE|");
221*e6e6073dSLiu Zhe 
222*e6e6073dSLiu Zhe 	        System.out.println(")");
223*e6e6073dSLiu Zhe         }
224*e6e6073dSLiu Zhe 
225*e6e6073dSLiu Zhe 	}
226*e6e6073dSLiu Zhe 
227*e6e6073dSLiu Zhe }
228