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 
24 package org.openoffice.test.vcl.widgets;
25 
26 import java.awt.Rectangle;
27 
28 import org.openoffice.test.common.Condition;
29 import org.openoffice.test.vcl.Tester;
30 import org.openoffice.test.vcl.client.Constant;
31 
32 /**
33  * VCL control proxy
34  */
35 public class VclControl extends VclWidget {
36 
37 	protected String id = null;
38 
39 	protected int type = -1;
40 
41 	/**
42 	 * Construct an instance with the given VclApp and ID
43 	 * @param app
44 	 * @param id
45 	 */
VclControl(VclApp app, String id)46 	public VclControl(VclApp app, String id) {
47 		super(app);
48 		this.id = id;
49 	}
50 
51 	/**
52 	 * Get the ID of the control
53 	 *
54 	 * @return
55 	 */
getId()56 	public String getId() {
57 		return this.id;
58 	}
59 
click()60 	public void click() {
61 		invoke(Constant.M_Click);
62 	}
63 
64 	/**
65 	 * Click
66 	 *
67 	 * @param locator
68 	 * @param x
69 	 * @param y
70 	 */
click(int x, int y)71 	public void click(int x, int y) {
72 		Rectangle rect = getValidScreenRectangle();
73 		Tester.click((int) rect.x + x, (int) rect.y + y);
74 	}
75 
doubleClick(int x, int y)76 	public void doubleClick(int x, int y) {
77 		Rectangle rect = getValidScreenRectangle();
78 		Tester.doubleClick((int) rect.x + x, (int) rect.y + y);
79 	}
80 
click(double xPercent, double yPercent)81 	public void click(double xPercent, double yPercent) {
82 		Rectangle rect = getValidScreenRectangle();
83 		Tester.click((int)(rect.x + xPercent * rect.width), (int) (rect.y + yPercent * rect.height));
84 	}
85 
doubleClick(double xPercent, double yPercent)86 	public void doubleClick(double xPercent, double yPercent) {
87 		Rectangle rect = getValidScreenRectangle();
88 		Tester.doubleClick((int)(rect.x + xPercent * rect.width), (int) (rect.y + yPercent * rect.height));
89 	}
90 
rightClick(int x, int y)91 	public void rightClick(int x, int y) {
92 		Rectangle rect = getValidScreenRectangle();
93 		Tester.rightClick((int) rect.x + x, (int) rect.y + y);
94 	}
95 
drag(int fromX, int fromY, int toX, int toY)96 	public void drag(int fromX, int fromY, int toX, int toY) {
97 		Rectangle rect = getValidScreenRectangle();
98 		Tester.drag((int) rect.x + fromX, (int) rect.y + fromY, (int) rect.x + toX, (int) rect.y + toY);
99 	}
100 
101 	/**
102 	 * Return the caption of control
103 	 *
104 	 * @return
105 	 */
getCaption()106 	public String getCaption() {
107 		return (String) invoke(Constant.M_Caption);
108 	}
109 
110 	/**
111 	 * Returns if the control is enabled
112 	 *
113 	 * @return Returns true if the control is enabled, otherwise false is
114 	 *         returned.
115 	 */
isEnabled()116 	public boolean isEnabled() {
117 		return (Boolean) invoke(Constant.M_IsEnabled);
118 	}
119 
120 	/**
121 	 * Returns if the control is checked
122 	 *
123 	 * @return Returns true if the control is enabled, otherwise false is
124 	 *         returned.
125 	 */
isChecked()126 	public boolean isChecked() {
127 		return (Boolean) invoke(Constant.M_IsChecked);
128 	}
129 
130 	/**
131 	 * Return the count of fixed text in the control
132 	 *
133 	 * @return
134 	 */
getFixedTextCount()135 	protected int getFixedTextCount() {
136 		return (Integer) invoke(Constant.M_GetFixedTextCount);
137 
138 	}
139 
140 	/**
141 	 * Return the fixed text in the control
142 	 *
143 	 * @param i
144 	 *            the index of fixed text
145 	 * @return the text of fixed text
146 	 */
getFixedText(int i)147 	protected String getFixedText(int i) {
148 		return (String) invoke(Constant.M_GetFixedText, new Object[] { new Integer(i + 1) });
149 	}
150 
151 	/**
152 	 * Operate the control via VclHook
153 	 *
154 	 * @param methodId
155 	 * @param args
156 	 * @return
157 	 */
invoke(int methodId, Object... args)158 	public Object invoke(int methodId, Object... args) {
159 		return app.caller.callControl(getId(), methodId, args);
160 	}
161 
162 	/**
163 	 *
164 	 * @param methodId
165 	 * @return
166 	 */
invoke(int methodId)167 	public Object invoke(int methodId) {
168 		return app.caller.callControl(getId(), methodId);
169 	}
170 
171 	/**
172 	 * Internal use
173 	 *
174 	 */
useMenu()175 	protected void useMenu() {
176 		invoke(Constant.M_UseMenu);
177 	}
178 
179 	/**
180 	 * TODO implement it. This is test tool implementation to input keyboard
181 	 * keys.
182 	 *
183 	 */
inputKeys(String keys)184 	public void inputKeys(String keys) {
185 		invoke(Constant.M_TypeKeys, new Object[] { keys });
186 	}
187 
188 	/**
189 	 * Check if the control exists in a period of time
190 	 */
exists(double iTimeout)191 	public boolean exists(double iTimeout) {
192 		return exists(iTimeout, 1);
193 	}
194 
195 	/**
196 	 * Check if the control exists in a period of time
197 	 */
exists(double iTimeout, double interval)198 	public boolean exists(double iTimeout, double interval) {
199 		return new Condition() {
200 			@Override
201 			public boolean value() {
202 				return VclControl.this.exists();
203 			}
204 		}.test(iTimeout, interval);
205 	}
206 
207 	/**
208 	 * Wait for the control to exist in a period of time. If the time is out, an
209 	 * ObjectNotFoundException will be throwed.
210 	 *
211 	 * @param iTimeout
212 	 * @param interval
213 	 */
214 	public void waitForExistence(double iTimeout, double interval) {
215 		new Condition() {
216 			@Override
217 			public boolean value() {
218 				return VclControl.this.exists();
219 			}
220 		}.waitForTrue( this + " is not found!", iTimeout, interval);
221 	}
222 
223 
224 	public void waitForEnabled(double iTimeout, double interval) {
225 		new Condition() {
226 
227 			@Override
228 			public boolean value() {
229 				return VclControl.this.isEnabled();
230 			}
231 
232 		}.waitForTrue("Time out to wait the control to be enabled!", iTimeout, interval);
233 	}
234 
235 	public void waitForDisabled(double iTimeout, double interval) {
236 		new Condition() {
237 
238 			@Override
239 			public boolean value() {
240 				return !VclControl.this.isEnabled();
241 			}
242 
243 		}.waitForTrue("Time out to wait the control to be disabled!", iTimeout, interval);
244 	}
245 
246 	public void waitForText(final String text, double iTimeout, double interval) {
247 		new Condition() {
248 
249 			@Override
250 			public boolean value() {
251 				return text.equals(VclControl.this.getCaption());
252 			}
253 
254 		}.waitForTrue("Time out to wait the control to show the text: " + text, iTimeout, interval);
255 	}
256 
257 
258 	public int getType() {
259 		if (type == -1)
260 			type = ((Long) invoke(Constant.M_GetRT)).intValue();
261 		return type;
262 	}
263 
264 	/**
265 	 * Returns if the control exists
266 	 *
267 	 * @return Returns true if the control is existed, otherwise false is
268 	 *         returned.
269 	 *
270 	 */
271 	public boolean exists() {
272 		if (!app.exists())
273 			return false;
274 		try {
275 			return (Boolean) invoke(Constant.M_Exists);
276 		} catch (Exception e) {
277 			return false;
278 		}
279 	}
280 
281 	public void focus() {
282 		inputKeys("");
283 	}
284 
285 	public void typeKeys(String keys) {
286 		focus();
287 		Tester.typeKeys(keys);
288 	}
289 
290 	public Rectangle getScreenRectangle() {
291 		String ret = (String) invoke(Constant.M_GetScreenRectangle, new Object[] { Boolean.FALSE });
292 		if (ret == null)
293 			return null;
294 		String[] data = ret.split(",");
295 		int x = Integer.parseInt(data[0]);
296 		int y = Integer.parseInt(data[1]);
297 		int w = Integer.parseInt(data[2]);
298 		int h = Integer.parseInt(data[3]);
299 		return new Rectangle(x, y, w, h);
300 	}
301 
302 	public Rectangle getValidScreenRectangle() {
303 		Rectangle rect = getScreenRectangle();
304 		Tester.sleep(0.1);
305 		rect = getScreenRectangle();
306 		if (rect == null)
307 			throw new RuntimeException(this + " - screen rectangle could not be computed! Maybe it is not showing!");
308 		return rect;
309 	}
310 
311 	public VclMenuItem menuItem(String path) {
312 		return new VclMenuItem(new VclMenu(this), path);
313 	}
314 	/**
315 	 * Opens the context menu of the control.
316 	 * <p>
317 	 * The context menu opens at the position of the mouse.
318 	 * <p>
319 	 * To open the context menu at a specific position in a window, you have to
320 	 * first move the mouse to the position before using this method.
321 	 * <p>
322 	 *
323 	 * @param
324 	 */
325 	public void openContextMenu() {
326 		invoke(Constant.M_OpenContextMenu);
327 	}
328 
329 
330 	public String toString() {
331 		return id;
332 	}
333 }
334