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 package testlib.uno;
24 
25 import java.util.Random;
26 
27 import com.sun.star.beans.Property;
28 import com.sun.star.beans.PropertyAttribute;
29 import com.sun.star.beans.XPropertySet;
30 import com.sun.star.beans.XPropertySetInfo;
31 import com.sun.star.uno.UnoRuntime;
32 
33 import testlib.uno.CellInfo;
34 
35 
36 /**
37  * Utilities for UNO automation testing
38  *
39  */
40 
41 public class TestUtil {
42 
43 	private static int colLimit = 1024;
44 	private static int rowLimit = 1048576;
45 	private static Random random = new Random();
46 
47 	public TestUtil() {
48 
49 	}
50 
51 	/**
52 	 * Generate a random cell index
53 	 * @return cellIndex    column: cellIndex[0]  row: cellIndex[1]
54 	 * @throws Exception
55 	 */
56 	public static CellInfo randCell() throws Exception {
57 		CellInfo cInfo = new CellInfo();
58 
59 		cInfo.setCol(random.nextInt(colLimit));
60 		cInfo.setRow(random.nextInt(rowLimit));
61 
62 		return cInfo;
63 	}
64 
65 	/**
66 	 * Generate a random cell index, in the limited range
67 	 * @param colTop  The max column limit
68 	 * @param rowTop  The max row limit
69 	 * @return
70 	 * @throws Exception
71 	 */
72 	public static CellInfo randCell(int colTop, int rowTop) throws Exception {
73 		CellInfo cInfo = new CellInfo();
74 
75 		cInfo.setCol(random.nextInt(colTop));
76 		cInfo.setRow(random.nextInt(rowTop));
77 
78 		return cInfo;
79 	}
80 
81 	/**
82 	 * Generate a random decimal RGB color number
83 	 * @return
84 	 * @throws Exception
85 	 */
86 	public static int randColor() throws Exception {
87 		int r = random.nextInt(256);
88 		int g = random.nextInt(256);
89 		int b = random.nextInt(256);
90 
91 		return r * 65536 + g * 256 + b;
92 	}
93 
94 	/**
95 	 * Generate a random decimal RGB color number in limited color space
96 	 * @param rMax  The R value limit, get a value in [0, rMax]
97 	 * @param gMax  The G value limit, get a value in [0, gMax]
98 	 * @param bMax  The B value limit, get a value in [0, bMax]
99 	 * @return
100 	 * @throws Exception
101 	 */
102 	public static int randColor(int rMax, int gMax, int bMax) throws Exception {
103 		int r = random.nextInt(rMax + 1) % 256;
104 		int g = random.nextInt(gMax + 1) % 256;
105 		int b = random.nextInt(bMax + 1) % 256;
106 
107 		return r * 65536 + g * 256 + b;
108 	}
109 
110 	/**
111 	 * Generate a series of decimal RGB color number
112 	 * @param size Set the quantity of random color value generated into the array
113 	 * @return
114 	 * @throws Exception
115 	 */
116 	public static int[] randColorList(int size) throws Exception {
117 		int[] colorList = new int[size];
118 		for (int i = 0; i < size; i++) {
119 			colorList[i] = randColor();
120 		}
121 
122 		return colorList;
123 	}
124 
125 	/**
126 	 * Add "=" before a string
127 	 * @param expression
128 	 * @return
129 	 */
130 	public static String toFormula(String expression) {
131 		return "=" + expression;
132 	}
133 
134 	/**
135 	 * Use specific operator to connect a series of number
136 	 * @param number
137 	 * @param operator
138 	 * @return
139 	 */
140 	public static String connectByOperator(double[] number, String operator) throws Exception{
141 		StringBuffer buffer = new StringBuffer();
142 
143 		for (int i = 0; i < number.length; i++) {
144 			buffer.append(number[i]);
145 			if (i < number.length - 1) {
146 				buffer.append(operator);
147 			}
148 		}
149 		return buffer.toString();
150 	}
151 
152 	/**
153 	 * Print the properties list of specific object to console
154 	 * @param obj   The instance of the object of which the property list you want to get. e.g. instance of XCell.
155 	 * @throws Exception
156 	 */
157 	public static void printPropertiesList(Object obj) throws Exception {
158 		// Get the property set of specific object
159 		XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, obj);
160 		XPropertySetInfo xPropertySetInfo = xPropertySet.getPropertySetInfo();
161 
162 	    // Get all properties info
163 	    Property[] aProps = xPropertySetInfo.getProperties();
164 
165 	    for (int i = 0; i < aProps.length; i++) {
166 	        // Print name and type of each property
167 	    	System.out.print("[" + (i + 1) + "]: Name=\"" + aProps[i].Name + "\" " + aProps[i].Type.toString() + " (");
168 
169 	        // Get flag. pay attention to the READONLY properties
170 	        short nAttribs = aProps[i].Attributes;
171 	        if ((nAttribs & PropertyAttribute.MAYBEVOID) != 0)
172 	        	System.out.print("MAYBEVOID|");
173 	        if ((nAttribs & PropertyAttribute.BOUND) != 0)
174 	            System.out.print("BOUND|");
175 	        if ((nAttribs & PropertyAttribute.CONSTRAINED) != 0)
176 	            System.out.print("CONSTRAINED|");
177 	        if ((nAttribs & PropertyAttribute.READONLY) != 0)
178 	            System.out.print("READONLY|");
179 	        if ((nAttribs & PropertyAttribute.TRANSIENT) != 0)
180 	            System.out.print("TRANSIENT|");
181 	        if ((nAttribs & PropertyAttribute.MAYBEAMBIGUOUS ) != 0)
182 	            System.out.print("MAYBEAMBIGUOUS|");
183 	        if ((nAttribs & PropertyAttribute.MAYBEDEFAULT) != 0)
184 	            System.out.print("MAYBEDEFAULT|");
185 	        if ((nAttribs & PropertyAttribute.REMOVEABLE) != 0)
186 	            System.out.print("REMOVEABLE|");
187 
188 	        System.out.println(")");
189         }
190 
191 	}
192 
193 }
194