xref: /trunk/test/testgui/source/testlib/gui/SCTool.java (revision 3309286857f19787ae62bd793a98b5af4edd2ad3)
1*b4d2d410SLiu Zhe /**************************************************************
2*b4d2d410SLiu Zhe  *
3*b4d2d410SLiu Zhe  * Licensed to the Apache Software Foundation (ASF) under one
4*b4d2d410SLiu Zhe  * or more contributor license agreements.  See the NOTICE file
5*b4d2d410SLiu Zhe  * distributed with this work for additional information
6*b4d2d410SLiu Zhe  * regarding copyright ownership.  The ASF licenses this file
7*b4d2d410SLiu Zhe  * to you under the Apache License, Version 2.0 (the
8*b4d2d410SLiu Zhe  * "License"); you may not use this file except in compliance
9*b4d2d410SLiu Zhe  * with the License.  You may obtain a copy of the License at
10*b4d2d410SLiu Zhe  *
11*b4d2d410SLiu Zhe  *   http://www.apache.org/licenses/LICENSE-2.0
12*b4d2d410SLiu Zhe  *
13*b4d2d410SLiu Zhe  * Unless required by applicable law or agreed to in writing,
14*b4d2d410SLiu Zhe  * software distributed under the License is distributed on an
15*b4d2d410SLiu Zhe  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b4d2d410SLiu Zhe  * KIND, either express or implied.  See the License for the
17*b4d2d410SLiu Zhe  * specific language governing permissions and limitations
18*b4d2d410SLiu Zhe  * under the License.
19*b4d2d410SLiu Zhe  *
20*b4d2d410SLiu Zhe  *************************************************************/
21*b4d2d410SLiu Zhe 
22*b4d2d410SLiu Zhe package testlib.gui;
23*b4d2d410SLiu Zhe 
24*b4d2d410SLiu Zhe import static org.openoffice.test.vcl.Tester.*;
25*b4d2d410SLiu Zhe import static testlib.gui.UIMap.*;
26*b4d2d410SLiu Zhe 
27*b4d2d410SLiu Zhe import java.lang.reflect.Array;
28*b4d2d410SLiu Zhe import java.util.StringTokenizer;
29*b4d2d410SLiu Zhe import java.util.logging.Logger;
30*b4d2d410SLiu Zhe 
31*b4d2d410SLiu Zhe public class SCTool {
32*b4d2d410SLiu Zhe 
33*b4d2d410SLiu Zhe     private static Logger LOG = Logger.getLogger(SCTool.class.getName());
34*b4d2d410SLiu Zhe 
35*b4d2d410SLiu Zhe     /**
36*b4d2d410SLiu Zhe      * Select a range.
37*b4d2d410SLiu Zhe      *
38*b4d2d410SLiu Zhe      * @param range
39*b4d2d410SLiu Zhe      *            e.g. Sheet1.A10, Sheet1.A11:B30, A11, A30:B45
40*b4d2d410SLiu Zhe      */
selectRange(String range)41*b4d2d410SLiu Zhe     public static void selectRange(String range) {
42*b4d2d410SLiu Zhe         scInputBarPosition.click(0.5, 0.5);
43*b4d2d410SLiu Zhe         scInputBarPosition.setText(range);
44*b4d2d410SLiu Zhe         typeKeys("<enter>");
45*b4d2d410SLiu Zhe         sleep(1);
46*b4d2d410SLiu Zhe     }
47*b4d2d410SLiu Zhe 
48*b4d2d410SLiu Zhe     /**
49*b4d2d410SLiu Zhe      * Get the input at the given cell. If the cell is a formula, return the
50*b4d2d410SLiu Zhe      * formula rather than the result
51*b4d2d410SLiu Zhe      *
52*b4d2d410SLiu Zhe      * @param cell
53*b4d2d410SLiu Zhe      * @return
54*b4d2d410SLiu Zhe      */
getCellInput(String cell)55*b4d2d410SLiu Zhe     public static String getCellInput(String cell) {
56*b4d2d410SLiu Zhe         if (cell != null)
57*b4d2d410SLiu Zhe             selectRange(cell);
58*b4d2d410SLiu Zhe         String caption = scInputBarInput.getCaption();
59*b4d2d410SLiu Zhe         // Fix: When formulaEdit's caption is blank, the hook will return the
60*b4d2d410SLiu Zhe         // document window's caption
61*b4d2d410SLiu Zhe         // So if it's document window caption, return ""
62*b4d2d410SLiu Zhe         return caption.contains("[12479]") ? "" : caption;
63*b4d2d410SLiu Zhe     }
64*b4d2d410SLiu Zhe 
65*b4d2d410SLiu Zhe     /**
66*b4d2d410SLiu Zhe      * Get the text at the given cell. If the cell is a formula, return the
67*b4d2d410SLiu Zhe      * result rather than the formula
68*b4d2d410SLiu Zhe      *
69*b4d2d410SLiu Zhe      * @param cell
70*b4d2d410SLiu Zhe      * @return
71*b4d2d410SLiu Zhe      */
getCellText(String cell)72*b4d2d410SLiu Zhe     public static String getCellText(String cell) {
73*b4d2d410SLiu Zhe         app.setClipboard("$$$$");
74*b4d2d410SLiu Zhe         selectRange(cell);
75*b4d2d410SLiu Zhe         typeKeys("<$copy>");
76*b4d2d410SLiu Zhe         String content = app.getClipboard();
77*b4d2d410SLiu Zhe         if (content.endsWith("\r\n"))
78*b4d2d410SLiu Zhe             content = content.substring(0, content.length() - 2);
79*b4d2d410SLiu Zhe         else if (content.endsWith("\n"))
80*b4d2d410SLiu Zhe             content = content.substring(0, content.length() - 1);
81*b4d2d410SLiu Zhe 
82*b4d2d410SLiu Zhe         return content;
83*b4d2d410SLiu Zhe     }
84*b4d2d410SLiu Zhe 
85*b4d2d410SLiu Zhe     /**
86*b4d2d410SLiu Zhe      * convert the format of column number to integer e.g. A -> 1 AA -> 27 AMJ
87*b4d2d410SLiu Zhe      * -> 1024
88*b4d2d410SLiu Zhe      *
89*b4d2d410SLiu Zhe      * @param no
90*b4d2d410SLiu Zhe      * @return
91*b4d2d410SLiu Zhe      */
toIntColumnNo(String no)92*b4d2d410SLiu Zhe     public static int toIntColumnNo(String no) {
93*b4d2d410SLiu Zhe         int len = no.length();
94*b4d2d410SLiu Zhe         int ret = 0;
95*b4d2d410SLiu Zhe         for (int i = 0; i < len; i++) {
96*b4d2d410SLiu Zhe             char c = no.charAt(len - i - 1);
97*b4d2d410SLiu Zhe             ret += Math.pow(26, i) * (c - 'A' + 1);
98*b4d2d410SLiu Zhe         }
99*b4d2d410SLiu Zhe 
100*b4d2d410SLiu Zhe         return ret;
101*b4d2d410SLiu Zhe     }
102*b4d2d410SLiu Zhe 
103*b4d2d410SLiu Zhe     /**
104*b4d2d410SLiu Zhe      * Convert the format of column number to char
105*b4d2d410SLiu Zhe      *
106*b4d2d410SLiu Zhe      * @param no
107*b4d2d410SLiu Zhe      * @return
108*b4d2d410SLiu Zhe      */
toCharColumnNo(int no)109*b4d2d410SLiu Zhe     public static String toCharColumnNo(int no) {
110*b4d2d410SLiu Zhe         String ret = "";
111*b4d2d410SLiu Zhe         int f = 0;
112*b4d2d410SLiu Zhe         do {
113*b4d2d410SLiu Zhe             f = (no - 1) / 26;
114*b4d2d410SLiu Zhe             int s = (no - 1) % 26;
115*b4d2d410SLiu Zhe             ret = (char) ('A' + s) + ret;
116*b4d2d410SLiu Zhe             no = f;
117*b4d2d410SLiu Zhe         } while (f != 0);
118*b4d2d410SLiu Zhe         return ret;
119*b4d2d410SLiu Zhe     }
120*b4d2d410SLiu Zhe 
121*b4d2d410SLiu Zhe     /**
122*b4d2d410SLiu Zhe      * Parse location string into integer values
123*b4d2d410SLiu Zhe      *
124*b4d2d410SLiu Zhe      * @param loc
125*b4d2d410SLiu Zhe      *            e.g. A1
126*b4d2d410SLiu Zhe      * @return
127*b4d2d410SLiu Zhe      */
parseLocation(String loc)128*b4d2d410SLiu Zhe     public static int[] parseLocation(String loc) {
129*b4d2d410SLiu Zhe         int i = 0;
130*b4d2d410SLiu Zhe         for (; i < loc.length(); i++) {
131*b4d2d410SLiu Zhe             char c = loc.charAt(i);
132*b4d2d410SLiu Zhe             if (c >= '0' && c <= '9')
133*b4d2d410SLiu Zhe                 break;
134*b4d2d410SLiu Zhe         }
135*b4d2d410SLiu Zhe 
136*b4d2d410SLiu Zhe         String col = loc.substring(0, i);
137*b4d2d410SLiu Zhe         String row = loc.substring(i);
138*b4d2d410SLiu Zhe         int sC = toIntColumnNo(col);
139*b4d2d410SLiu Zhe         int sR = Integer.parseInt(row);
140*b4d2d410SLiu Zhe         return new int[] { sC, sR };
141*b4d2d410SLiu Zhe     }
142*b4d2d410SLiu Zhe 
143*b4d2d410SLiu Zhe     /**
144*b4d2d410SLiu Zhe      * Parse range string into integer values
145*b4d2d410SLiu Zhe      *
146*b4d2d410SLiu Zhe      * @param range
147*b4d2d410SLiu Zhe      *            e.g. A3:F9
148*b4d2d410SLiu Zhe      * @return
149*b4d2d410SLiu Zhe      */
parseRange(String range)150*b4d2d410SLiu Zhe     public static int[] parseRange(String range) {
151*b4d2d410SLiu Zhe         int dotIndex = range.indexOf(".");
152*b4d2d410SLiu Zhe         if (dotIndex != -1) {
153*b4d2d410SLiu Zhe             range = range.substring(dotIndex + 1);
154*b4d2d410SLiu Zhe         }
155*b4d2d410SLiu Zhe 
156*b4d2d410SLiu Zhe         String[] locs = range.split(":");
157*b4d2d410SLiu Zhe         int[] ret = new int[4];
158*b4d2d410SLiu Zhe         int[] start = parseLocation(locs[0]);
159*b4d2d410SLiu Zhe         ret[0] = start[0];
160*b4d2d410SLiu Zhe         ret[1] = start[1];
161*b4d2d410SLiu Zhe         if (locs.length == 1) {
162*b4d2d410SLiu Zhe             ret[2] = start[0];
163*b4d2d410SLiu Zhe             ret[3] = start[1];
164*b4d2d410SLiu Zhe         } else {
165*b4d2d410SLiu Zhe             int[] end = parseLocation(locs[1]);
166*b4d2d410SLiu Zhe             ret[2] = end[0];
167*b4d2d410SLiu Zhe             ret[3] = end[1];
168*b4d2d410SLiu Zhe         }
169*b4d2d410SLiu Zhe 
170*b4d2d410SLiu Zhe         return ret;
171*b4d2d410SLiu Zhe     }
172*b4d2d410SLiu Zhe 
173*b4d2d410SLiu Zhe     /**
174*b4d2d410SLiu Zhe      * Get the text at the given cells. If the cell is a formula, return the
175*b4d2d410SLiu Zhe      * result rather than the formula. Note:
176*b4d2d410SLiu Zhe      *
177*b4d2d410SLiu Zhe      * @param range
178*b4d2d410SLiu Zhe      *            e.g. A3:D9
179*b4d2d410SLiu Zhe      * @return
180*b4d2d410SLiu Zhe      */
getCellTexts(String range)181*b4d2d410SLiu Zhe     public static String[][] getCellTexts(String range) {
182*b4d2d410SLiu Zhe         selectRange(range);
183*b4d2d410SLiu Zhe         int[] intRange = parseRange(range);
184*b4d2d410SLiu Zhe         int rowCount = intRange[3] - intRange[1] + 1;
185*b4d2d410SLiu Zhe         int colCount = intRange[2] - intRange[0] + 1;
186*b4d2d410SLiu Zhe         String[][] texts = new String[rowCount][colCount];
187*b4d2d410SLiu Zhe 
188*b4d2d410SLiu Zhe         app.setClipboard("$$$$");
189*b4d2d410SLiu Zhe         typeKeys("<$copy>");
190*b4d2d410SLiu Zhe         sleep(1);
191*b4d2d410SLiu Zhe         String text = app.getClipboard();
192*b4d2d410SLiu Zhe         StringTokenizer tokenizer = new StringTokenizer(text, "\"\t\n", true);
193*b4d2d410SLiu Zhe         int state = 0;
194*b4d2d410SLiu Zhe         String cellContent = "";
195*b4d2d410SLiu Zhe         int r = 0, c = 0;
196*b4d2d410SLiu Zhe         while (tokenizer.hasMoreTokens()) {
197*b4d2d410SLiu Zhe             String token = tokenizer.nextToken();
198*b4d2d410SLiu Zhe             switch (state) {
199*b4d2d410SLiu Zhe             case 0:
200*b4d2d410SLiu Zhe                 if ("\"".equals(token)) {
201*b4d2d410SLiu Zhe                     state = 1;
202*b4d2d410SLiu Zhe                 } else if ("\t".equals(token)) {
203*b4d2d410SLiu Zhe                     texts[r][c] = cellContent;
204*b4d2d410SLiu Zhe                     cellContent = "";
205*b4d2d410SLiu Zhe                     c++;
206*b4d2d410SLiu Zhe                 } else if ("\n".equals(token)) {
207*b4d2d410SLiu Zhe                     if (cellContent.endsWith("\r"))
208*b4d2d410SLiu Zhe                         cellContent = cellContent.substring(0, cellContent.length() - 1);
209*b4d2d410SLiu Zhe                     texts[r][c] = cellContent;
210*b4d2d410SLiu Zhe                     cellContent = "";
211*b4d2d410SLiu Zhe                     c = 0;
212*b4d2d410SLiu Zhe                     r++;
213*b4d2d410SLiu Zhe                 } else {
214*b4d2d410SLiu Zhe                     cellContent += token;
215*b4d2d410SLiu Zhe                 }
216*b4d2d410SLiu Zhe                 break;
217*b4d2d410SLiu Zhe             case 1:
218*b4d2d410SLiu Zhe                 if ("\"".equals(token)) {
219*b4d2d410SLiu Zhe                     state = 0;
220*b4d2d410SLiu Zhe                 } else {
221*b4d2d410SLiu Zhe                     cellContent += token;
222*b4d2d410SLiu Zhe                 }
223*b4d2d410SLiu Zhe                 break;
224*b4d2d410SLiu Zhe             }
225*b4d2d410SLiu Zhe         }
226*b4d2d410SLiu Zhe 
227*b4d2d410SLiu Zhe         LOG.info("Text of range [" + range + "]:\n" + arrayToString(texts));
228*b4d2d410SLiu Zhe         return texts;
229*b4d2d410SLiu Zhe     }
230*b4d2d410SLiu Zhe 
arrayToString(Object array)231*b4d2d410SLiu Zhe     private static String arrayToString(Object array) {
232*b4d2d410SLiu Zhe         if (array == null)
233*b4d2d410SLiu Zhe             return "null";
234*b4d2d410SLiu Zhe         if (!array.getClass().isArray())
235*b4d2d410SLiu Zhe             return array.toString();
236*b4d2d410SLiu Zhe 
237*b4d2d410SLiu Zhe         int len = Array.getLength(array);
238*b4d2d410SLiu Zhe         String ret = "{";
239*b4d2d410SLiu Zhe         for (int i = 0; i < len; i++) {
240*b4d2d410SLiu Zhe             Object el = Array.get(array, i);
241*b4d2d410SLiu Zhe             if (el == null) {
242*b4d2d410SLiu Zhe                 ret += "null";
243*b4d2d410SLiu Zhe             } else if (el.getClass().isArray()) {
244*b4d2d410SLiu Zhe                 ret += arrayToString(el);
245*b4d2d410SLiu Zhe             } else {
246*b4d2d410SLiu Zhe                 ret += "\"" + el + "\"";
247*b4d2d410SLiu Zhe             }
248*b4d2d410SLiu Zhe 
249*b4d2d410SLiu Zhe             if (i + 1 != len)
250*b4d2d410SLiu Zhe                 ret += ",";
251*b4d2d410SLiu Zhe 
252*b4d2d410SLiu Zhe         }
253*b4d2d410SLiu Zhe         ret += "}";
254*b4d2d410SLiu Zhe         return ret;
255*b4d2d410SLiu Zhe     }
256*b4d2d410SLiu Zhe }
257