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 font size number in limited range 83 * @param max The font size in Excel2003 is [1,409] 84 * @return 85 * @throws Exception 86 */ 87 public static double randFontSize(int max) throws Exception { 88 double basic = random.nextInt(max * 2); 89 double size = 1; 90 if (basic < 2) { 91 size = 1; 92 } 93 else { 94 size = basic / 2; 95 } 96 97 return size; 98 } 99 100 /** 101 * Generate a series of font size number 102 * @param listSize 103 * @param max 104 * @return 105 * @throws Exception 106 */ 107 public static double[] randFontSizeList(int listSize, int max) throws Exception { 108 double[] sizeList = new double[listSize]; 109 for (int i =0; i < listSize; i++) { 110 sizeList[i] = randFontSize(max); 111 } 112 return sizeList; 113 } 114 115 /** 116 * Generate a random decimal RGB color number 117 * @return 118 * @throws Exception 119 */ 120 public static int randColor() throws Exception { 121 int r = random.nextInt(256); 122 int g = random.nextInt(256); 123 int b = random.nextInt(256); 124 125 return r * 65536 + g * 256 + b; 126 } 127 128 /** 129 * Generate a random decimal RGB color number in limited color space 130 * @param rMax The R value limit, get a value in [0, rMax] 131 * @param gMax The G value limit, get a value in [0, gMax] 132 * @param bMax The B value limit, get a value in [0, bMax] 133 * @return 134 * @throws Exception 135 */ 136 public static int randColor(int rMax, int gMax, int bMax) throws Exception { 137 int r = random.nextInt(rMax + 1) % 256; 138 int g = random.nextInt(gMax + 1) % 256; 139 int b = random.nextInt(bMax + 1) % 256; 140 141 return r * 65536 + g * 256 + b; 142 } 143 144 /** 145 * Generate a series of decimal RGB color number 146 * @param size Set the quantity of random color value generated into the array 147 * @return 148 * @throws Exception 149 */ 150 public static int[] randColorList(int size) throws Exception { 151 int[] colorList = new int[size]; 152 for (int i = 0; i < size; i++) { 153 colorList[i] = randColor(); 154 } 155 156 return colorList; 157 } 158 159 /** 160 * Add "=" before a string 161 * @param expression 162 * @return 163 */ 164 public static String toFormula(String expression) { 165 return "=" + expression; 166 } 167 168 /** 169 * Use specific operator to connect a series of number 170 * @param number 171 * @param operator 172 * @return 173 */ 174 public static String connectByOperator(double[] number, String operator) throws Exception{ 175 StringBuffer buffer = new StringBuffer(); 176 177 for (int i = 0; i < number.length; i++) { 178 buffer.append(number[i]); 179 if (i < number.length - 1) { 180 buffer.append(operator); 181 } 182 } 183 return buffer.toString(); 184 } 185 186 /** 187 * Print the properties list of specific object to console 188 * @param obj The instance of the object of which the property list you want to get. e.g. instance of XCell. 189 * @throws Exception 190 */ 191 public static void printPropertiesList(Object obj) throws Exception { 192 // Get the property set of specific object 193 XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, obj); 194 XPropertySetInfo xPropertySetInfo = xPropertySet.getPropertySetInfo(); 195 196 // Get all properties info 197 Property[] aProps = xPropertySetInfo.getProperties(); 198 199 for (int i = 0; i < aProps.length; i++) { 200 // Print name and type of each property 201 System.out.print("[" + (i + 1) + "]: Name=\"" + aProps[i].Name + "\" " + aProps[i].Type.toString() + " ("); 202 203 // Get flag. pay attention to the READONLY properties 204 short nAttribs = aProps[i].Attributes; 205 if ((nAttribs & PropertyAttribute.MAYBEVOID) != 0) 206 System.out.print("MAYBEVOID|"); 207 if ((nAttribs & PropertyAttribute.BOUND) != 0) 208 System.out.print("BOUND|"); 209 if ((nAttribs & PropertyAttribute.CONSTRAINED) != 0) 210 System.out.print("CONSTRAINED|"); 211 if ((nAttribs & PropertyAttribute.READONLY) != 0) 212 System.out.print("READONLY|"); 213 if ((nAttribs & PropertyAttribute.TRANSIENT) != 0) 214 System.out.print("TRANSIENT|"); 215 if ((nAttribs & PropertyAttribute.MAYBEAMBIGUOUS ) != 0) 216 System.out.print("MAYBEAMBIGUOUS|"); 217 if ((nAttribs & PropertyAttribute.MAYBEDEFAULT) != 0) 218 System.out.print("MAYBEDEFAULT|"); 219 if ((nAttribs & PropertyAttribute.REMOVEABLE) != 0) 220 System.out.print("REMOVEABLE|"); 221 222 System.out.println(")"); 223 } 224 225 } 226 227 } 228