18191ab5fSLiu Zhe /**************************************************************
28191ab5fSLiu Zhe  *
38191ab5fSLiu Zhe  * Licensed to the Apache Software Foundation (ASF) under one
48191ab5fSLiu Zhe  * or more contributor license agreements.  See the NOTICE file
58191ab5fSLiu Zhe  * distributed with this work for additional information
68191ab5fSLiu Zhe  * regarding copyright ownership.  The ASF licenses this file
78191ab5fSLiu Zhe  * to you under the Apache License, Version 2.0 (the
88191ab5fSLiu Zhe  * "License"); you may not use this file except in compliance
98191ab5fSLiu Zhe  * with the License.  You may obtain a copy of the License at
108191ab5fSLiu Zhe  *
118191ab5fSLiu Zhe  *   http://www.apache.org/licenses/LICENSE-2.0
128191ab5fSLiu Zhe  *
138191ab5fSLiu Zhe  * Unless required by applicable law or agreed to in writing,
148191ab5fSLiu Zhe  * software distributed under the License is distributed on an
158191ab5fSLiu Zhe  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
168191ab5fSLiu Zhe  * KIND, either express or implied.  See the License for the
178191ab5fSLiu Zhe  * specific language governing permissions and limitations
188191ab5fSLiu Zhe  * under the License.
198191ab5fSLiu Zhe  *
208191ab5fSLiu Zhe  *************************************************************/
218191ab5fSLiu Zhe 
228191ab5fSLiu Zhe 
238191ab5fSLiu Zhe package testlib.uno;
248191ab5fSLiu Zhe 
258191ab5fSLiu Zhe import java.util.Random;
268191ab5fSLiu Zhe 
278191ab5fSLiu Zhe import com.sun.star.beans.Property;
288191ab5fSLiu Zhe import com.sun.star.beans.PropertyAttribute;
298191ab5fSLiu Zhe import com.sun.star.beans.XPropertySet;
308191ab5fSLiu Zhe import com.sun.star.beans.XPropertySetInfo;
318191ab5fSLiu Zhe import com.sun.star.uno.UnoRuntime;
328191ab5fSLiu Zhe 
338191ab5fSLiu Zhe import testlib.uno.CellInfo;
348191ab5fSLiu Zhe 
358191ab5fSLiu Zhe 
368191ab5fSLiu Zhe /**
378191ab5fSLiu Zhe  * Utilities for UNO automation testing
388191ab5fSLiu Zhe  *
398191ab5fSLiu Zhe  */
408191ab5fSLiu Zhe 
418191ab5fSLiu Zhe public class TestUtil {
428191ab5fSLiu Zhe 
438191ab5fSLiu Zhe 	private static int colLimit = 1024;
448191ab5fSLiu Zhe 	private static int rowLimit = 1048576;
458191ab5fSLiu Zhe 	private static Random random = new Random();
468191ab5fSLiu Zhe 
TestUtil()478191ab5fSLiu Zhe 	public TestUtil() {
488191ab5fSLiu Zhe 
498191ab5fSLiu Zhe 	}
508191ab5fSLiu Zhe 
518191ab5fSLiu Zhe 	/**
528191ab5fSLiu Zhe 	 * Generate a random cell index
538191ab5fSLiu Zhe 	 * @return cellIndex    column: cellIndex[0]  row: cellIndex[1]
548191ab5fSLiu Zhe 	 * @throws Exception
558191ab5fSLiu Zhe 	 */
randCell()568191ab5fSLiu Zhe 	public static CellInfo randCell() throws Exception {
578191ab5fSLiu Zhe 		CellInfo cInfo = new CellInfo();
588191ab5fSLiu Zhe 
598191ab5fSLiu Zhe 		cInfo.setCol(random.nextInt(colLimit));
608191ab5fSLiu Zhe 		cInfo.setRow(random.nextInt(rowLimit));
618191ab5fSLiu Zhe 
628191ab5fSLiu Zhe 		return cInfo;
638191ab5fSLiu Zhe 	}
648191ab5fSLiu Zhe 
658191ab5fSLiu Zhe 	/**
668191ab5fSLiu Zhe 	 * Generate a random cell index, in the limited range
678191ab5fSLiu Zhe 	 * @param colTop  The max column limit
688191ab5fSLiu Zhe 	 * @param rowTop  The max row limit
698191ab5fSLiu Zhe 	 * @return
708191ab5fSLiu Zhe 	 * @throws Exception
718191ab5fSLiu Zhe 	 */
randCell(int colTop, int rowTop)728191ab5fSLiu Zhe 	public static CellInfo randCell(int colTop, int rowTop) throws Exception {
738191ab5fSLiu Zhe 		CellInfo cInfo = new CellInfo();
748191ab5fSLiu Zhe 
758191ab5fSLiu Zhe 		cInfo.setCol(random.nextInt(colTop));
768191ab5fSLiu Zhe 		cInfo.setRow(random.nextInt(rowTop));
778191ab5fSLiu Zhe 
788191ab5fSLiu Zhe 		return cInfo;
798191ab5fSLiu Zhe 	}
808191ab5fSLiu Zhe 
81b4bc5e95SLiu Zhe 	/**
82b4bc5e95SLiu Zhe 	 * Generate a font size number in limited range
83b4bc5e95SLiu Zhe 	 * @param max  The font size in Excel2003 is [1,409]
84b4bc5e95SLiu Zhe 	 * @return
85b4bc5e95SLiu Zhe 	 * @throws Exception
86b4bc5e95SLiu Zhe 	 */
randFontSize(int max)87b4bc5e95SLiu Zhe 	public static double randFontSize(int max) throws Exception {
88b4bc5e95SLiu Zhe 		double basic = random.nextInt(max * 2);
89b4bc5e95SLiu Zhe 		double size = 1;
90b4bc5e95SLiu Zhe 		if (basic < 2) {
91b4bc5e95SLiu Zhe 			size = 1;
92b4bc5e95SLiu Zhe 		}
93b4bc5e95SLiu Zhe 		else {
94b4bc5e95SLiu Zhe 			size = basic / 2;
95b4bc5e95SLiu Zhe 		}
96b4bc5e95SLiu Zhe 
97b4bc5e95SLiu Zhe 		return size;
98b4bc5e95SLiu Zhe 	}
99b4bc5e95SLiu Zhe 
100b4bc5e95SLiu Zhe 	/**
101b4bc5e95SLiu Zhe 	 * Generate a series of font size number
102b4bc5e95SLiu Zhe 	 * @param listSize
103b4bc5e95SLiu Zhe 	 * @param max
104b4bc5e95SLiu Zhe 	 * @return
105b4bc5e95SLiu Zhe 	 * @throws Exception
106b4bc5e95SLiu Zhe 	 */
randFontSizeList(int listSize, int max)107b4bc5e95SLiu Zhe 	public static double[] randFontSizeList(int listSize, int max) throws Exception {
108b4bc5e95SLiu Zhe 		double[] sizeList = new double[listSize];
109b4bc5e95SLiu Zhe 		for (int i =0; i < listSize; i++) {
110b4bc5e95SLiu Zhe 			sizeList[i] = randFontSize(max);
111b4bc5e95SLiu Zhe 		}
112b4bc5e95SLiu Zhe 		return sizeList;
113b4bc5e95SLiu Zhe 	}
114b4bc5e95SLiu Zhe 
1158191ab5fSLiu Zhe 	/**
1168191ab5fSLiu Zhe 	 * Generate a random decimal RGB color number
1178191ab5fSLiu Zhe 	 * @return
1188191ab5fSLiu Zhe 	 * @throws Exception
1198191ab5fSLiu Zhe 	 */
randColor()1208191ab5fSLiu Zhe 	public static int randColor() throws Exception {
1218191ab5fSLiu Zhe 		int r = random.nextInt(256);
1228191ab5fSLiu Zhe 		int g = random.nextInt(256);
1238191ab5fSLiu Zhe 		int b = random.nextInt(256);
1248191ab5fSLiu Zhe 
1258191ab5fSLiu Zhe 		return r * 65536 + g * 256 + b;
1268191ab5fSLiu Zhe 	}
1278191ab5fSLiu Zhe 
1288191ab5fSLiu Zhe 	/**
1298191ab5fSLiu Zhe 	 * Generate a random decimal RGB color number in limited color space
1301ea6643fSLiu Zhe 	 * @param rMax  The R value limit, get a value in [0, rMax]
1311ea6643fSLiu Zhe 	 * @param gMax  The G value limit, get a value in [0, gMax]
1321ea6643fSLiu Zhe 	 * @param bMax  The B value limit, get a value in [0, bMax]
1338191ab5fSLiu Zhe 	 * @return
1348191ab5fSLiu Zhe 	 * @throws Exception
1358191ab5fSLiu Zhe 	 */
randColor(int rMax, int gMax, int bMax)1368191ab5fSLiu Zhe 	public static int randColor(int rMax, int gMax, int bMax) throws Exception {
1371ea6643fSLiu Zhe 		int r = random.nextInt(rMax + 1) % 256;
1381ea6643fSLiu Zhe 		int g = random.nextInt(gMax + 1) % 256;
1391ea6643fSLiu Zhe 		int b = random.nextInt(bMax + 1) % 256;
1408191ab5fSLiu Zhe 
1418191ab5fSLiu Zhe 		return r * 65536 + g * 256 + b;
1428191ab5fSLiu Zhe 	}
1431ea6643fSLiu Zhe 
1441ea6643fSLiu Zhe 	/**
1451ea6643fSLiu Zhe 	 * Generate a series of decimal RGB color number
1461ea6643fSLiu Zhe 	 * @param size Set the quantity of random color value generated into the array
1471ea6643fSLiu Zhe 	 * @return
1481ea6643fSLiu Zhe 	 * @throws Exception
1491ea6643fSLiu Zhe 	 */
randColorList(int size)1501ea6643fSLiu Zhe 	public static int[] randColorList(int size) throws Exception {
1511ea6643fSLiu Zhe 		int[] colorList = new int[size];
1521ea6643fSLiu Zhe 		for (int i = 0; i < size; i++) {
1531ea6643fSLiu Zhe 			colorList[i] = randColor();
1541ea6643fSLiu Zhe 		}
1558191ab5fSLiu Zhe 
1561ea6643fSLiu Zhe 		return colorList;
1571ea6643fSLiu Zhe 	}
1581ea6643fSLiu Zhe 
1598191ab5fSLiu Zhe 	/**
1608191ab5fSLiu Zhe 	 * Add "=" before a string
1618191ab5fSLiu Zhe 	 * @param expression
1628191ab5fSLiu Zhe 	 * @return
1638191ab5fSLiu Zhe 	 */
toFormula(String expression)1648191ab5fSLiu Zhe 	public static String toFormula(String expression) {
1658191ab5fSLiu Zhe 		return "=" + expression;
1668191ab5fSLiu Zhe 	}
1678191ab5fSLiu Zhe 
1688191ab5fSLiu Zhe 	/**
1698191ab5fSLiu Zhe 	 * Use specific operator to connect a series of number
1708191ab5fSLiu Zhe 	 * @param number
1718191ab5fSLiu Zhe 	 * @param operator
1728191ab5fSLiu Zhe 	 * @return
1738191ab5fSLiu Zhe 	 */
connectByOperator(double[] number, String operator)1748191ab5fSLiu Zhe 	public static String connectByOperator(double[] number, String operator) throws Exception{
1758191ab5fSLiu Zhe 		StringBuffer buffer = new StringBuffer();
1768191ab5fSLiu Zhe 
1778191ab5fSLiu Zhe 		for (int i = 0; i < number.length; i++) {
1788191ab5fSLiu Zhe 			buffer.append(number[i]);
1798191ab5fSLiu Zhe 			if (i < number.length - 1) {
1808191ab5fSLiu Zhe 				buffer.append(operator);
1818191ab5fSLiu Zhe 			}
1828191ab5fSLiu Zhe 		}
1838191ab5fSLiu Zhe 		return buffer.toString();
1848191ab5fSLiu Zhe 	}
1858191ab5fSLiu Zhe 
1868191ab5fSLiu Zhe 	/**
1878191ab5fSLiu Zhe 	 * Print the properties list of specific object to console
1888191ab5fSLiu Zhe 	 * @param obj   The instance of the object of which the property list you want to get. e.g. instance of XCell.
1898191ab5fSLiu Zhe 	 * @throws Exception
1908191ab5fSLiu Zhe 	 */
printPropertiesList(Object obj)1918191ab5fSLiu Zhe 	public static void printPropertiesList(Object obj) throws Exception {
1928191ab5fSLiu Zhe 		// Get the property set of specific object
1938191ab5fSLiu Zhe 		XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, obj);
1948191ab5fSLiu Zhe 		XPropertySetInfo xPropertySetInfo = xPropertySet.getPropertySetInfo();
1958191ab5fSLiu Zhe 
1968191ab5fSLiu Zhe 	    // Get all properties info
1978191ab5fSLiu Zhe 	    Property[] aProps = xPropertySetInfo.getProperties();
1988191ab5fSLiu Zhe 
1998191ab5fSLiu Zhe 	    for (int i = 0; i < aProps.length; i++) {
2008191ab5fSLiu Zhe 	        // Print name and type of each property
2018191ab5fSLiu Zhe 	    	System.out.print("[" + (i + 1) + "]: Name=\"" + aProps[i].Name + "\" " + aProps[i].Type.toString() + " (");
2028191ab5fSLiu Zhe 
2038191ab5fSLiu Zhe 	        // Get flag. pay attention to the READONLY properties
2048191ab5fSLiu Zhe 	        short nAttribs = aProps[i].Attributes;
2058191ab5fSLiu Zhe 	        if ((nAttribs & PropertyAttribute.MAYBEVOID) != 0)
2068191ab5fSLiu Zhe 	        	System.out.print("MAYBEVOID|");
2078191ab5fSLiu Zhe 	        if ((nAttribs & PropertyAttribute.BOUND) != 0)
2088191ab5fSLiu Zhe 	            System.out.print("BOUND|");
2098191ab5fSLiu Zhe 	        if ((nAttribs & PropertyAttribute.CONSTRAINED) != 0)
2108191ab5fSLiu Zhe 	            System.out.print("CONSTRAINED|");
2118191ab5fSLiu Zhe 	        if ((nAttribs & PropertyAttribute.READONLY) != 0)
2128191ab5fSLiu Zhe 	            System.out.print("READONLY|");
2138191ab5fSLiu Zhe 	        if ((nAttribs & PropertyAttribute.TRANSIENT) != 0)
2148191ab5fSLiu Zhe 	            System.out.print("TRANSIENT|");
2158191ab5fSLiu Zhe 	        if ((nAttribs & PropertyAttribute.MAYBEAMBIGUOUS ) != 0)
2168191ab5fSLiu Zhe 	            System.out.print("MAYBEAMBIGUOUS|");
2178191ab5fSLiu Zhe 	        if ((nAttribs & PropertyAttribute.MAYBEDEFAULT) != 0)
2188191ab5fSLiu Zhe 	            System.out.print("MAYBEDEFAULT|");
2198191ab5fSLiu Zhe 	        if ((nAttribs & PropertyAttribute.REMOVEABLE) != 0)
2208191ab5fSLiu Zhe 	            System.out.print("REMOVEABLE|");
2218191ab5fSLiu Zhe 
2228191ab5fSLiu Zhe 	        System.out.println(")");
2238191ab5fSLiu Zhe         }
2248191ab5fSLiu Zhe 
2258191ab5fSLiu Zhe 	}
2268191ab5fSLiu Zhe 
2278191ab5fSLiu Zhe }
228