001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008:
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.test.xslt.lib;
021:
022: import java.awt.Component;
023: import java.awt.Container;
024: import java.awt.Point;
025: import java.util.zip.CRC32;
026: import javax.swing.JComponent;
027: import javax.swing.JLabel;
028: import javax.swing.JPanel;
029: import javax.swing.JTextField;
030: import org.netbeans.jellytools.TopComponentOperator;
031: import org.netbeans.jemmy.ComponentChooser;
032: import org.netbeans.jemmy.EventTool;
033: import org.netbeans.jemmy.JemmyProperties;
034: import org.netbeans.jemmy.TimeoutExpiredException;
035: import org.netbeans.jemmy.Timeouts;
036: import org.netbeans.jemmy.operators.ContainerOperator;
037: import org.netbeans.jemmy.operators.JComponentOperator;
038: import org.netbeans.jemmy.operators.JLabelOperator;
039: import org.netbeans.jemmy.operators.JTextFieldOperator;
040:
041: /**
042: *
043: * @author ca@netbeans.org
044: */
045:
046: public class Helpers {
047: public static final String WAIT_COMPONENT_TIMEOUT = "ComponentOperator.WaitComponentTimeout";
048: public static final int NO_EVENT_TIMEOUT = 500;
049: public static EventTool et = new EventTool();
050:
051: /** Creates a new instance of Helpers */
052: public Helpers() {
053: }
054:
055: public static JComponentOperator getComponentOperator(
056: ContainerOperator opContainer,
057: final String strComponentClass) {
058: return getComponentOperator(opContainer, strComponentClass,
059: null, 0);
060: }
061:
062: public static JComponentOperator getComponentOperator(
063: ContainerOperator opContainer,
064: final String strComponentClass,
065: final String strComponentName) {
066: return getComponentOperator(opContainer, strComponentClass,
067: strComponentName, 0);
068: }
069:
070: public static JComponentOperator getComponentOperator(
071: ContainerOperator opContainer,
072: final String strComponentClass, final int index) {
073: return getComponentOperator(opContainer, strComponentClass,
074: null, index);
075: }
076:
077: public static JComponentOperator getComponentOperator(
078: ContainerOperator opContainer,
079: final String strComponentClass,
080: final String strComponentName, final int index) {
081: return getComponentOperator(opContainer, strComponentClass,
082: strComponentName, index, 2000);
083: }
084:
085: public static JComponentOperator getComponentOperator(
086: ContainerOperator opContainer,
087: final String strComponentClass,
088: final String strComponentName, final int index,
089: final int timeout) {
090:
091: Timeouts times = JemmyProperties.getCurrentTimeouts();
092: long to = times.setTimeout(Helpers.WAIT_COMPONENT_TIMEOUT,
093: timeout);
094:
095: JComponentOperator opJComponent = null;
096:
097: try {
098: opJComponent = new JComponentOperator(opContainer,
099: new ComponentChooser() {
100:
101: public boolean checkComponent(
102: java.awt.Component comp) {
103: boolean result = true;
104: if (strComponentName != null) {
105: String name = comp.getName();
106: if (name != null) {
107: result = name
108: .equals(strComponentName);
109: }
110: }
111: return result
112: && comp
113: .getClass()
114: .toString()
115: .equals(
116: "class "
117: + strComponentClass);
118: }
119:
120: public String getDescription() {
121: return strComponentClass + " with name "
122: + strComponentName;
123: }
124: }, index);
125: } catch (TimeoutExpiredException e) {
126: } finally {
127: times.setTimeout(Helpers.WAIT_COMPONENT_TIMEOUT, to);
128: }
129:
130: return opJComponent;
131: }
132:
133: public static void waitNoEvent() {
134: waitNoEvent(NO_EVENT_TIMEOUT);
135: }
136:
137: public static void waitNoEvent(int milliseconds) {
138: et.waitNoEvent(milliseconds);
139: }
140:
141: public static void pause(int milliseconds) {
142: System.out.println("Paused for " + milliseconds);
143: try {
144: Thread.currentThread().sleep(milliseconds);
145: } catch (Exception e) {
146: }
147: }
148:
149: public static void writeJemmyLog(String str) {
150: JemmyProperties.getCurrentOutput().printLine(str);
151: }
152:
153: public static void recurseComponent(int level, Component component) {
154:
155: JemmyProperties.getCurrentOutput().print("*");
156:
157: for (int j = 0; j < level; j++) {
158: JemmyProperties.getCurrentOutput().print("|");
159: }
160:
161: // JemmyProperties.getCurrentOutput().printLine("Name: " + component.toString() + ", Class: " + component.getClass());
162: JemmyProperties.getCurrentOutput().printLine(
163: "Class: " + component.getClass());
164:
165: if (component instanceof Container) {
166: Component[] comps = ((Container) component).getComponents();
167: for (int i = 0; i < comps.length; i++) {
168: recurseComponent(level + 1, comps[i]);
169: }
170: }
171: }
172:
173: public static Point getContainerPoint(
174: JComponentOperator opComponent, Point componentPoint,
175: JComponentOperator opContainer) {
176: Point pComponent = opComponent.getLocationOnScreen();
177: Point pContainer = opContainer.getLocationOnScreen();
178:
179: return new Point(
180: pComponent.x + componentPoint.x - pContainer.x,
181: pComponent.y + componentPoint.y - pContainer.y);
182: }
183:
184: public static void closeTopComponentIfOpened(String strName) {
185: for (int i = 0; i < 5; i++) {
186: JComponent theOneToClose = TopComponentOperator
187: .findTopComponent(strName, 0);
188: if (theOneToClose != null) {
189: new TopComponentOperator(theOneToClose).close();
190: break;
191: } else {
192: pause(200);
193: }
194: }
195: }
196:
197: public static boolean isCRC32Equal(String strText, long checkSum) {
198: strText = strText.trim().replaceAll("[ [\t\f\r]]", "");
199:
200: CRC32 crc32 = new CRC32();
201: crc32.update(strText.getBytes());
202: long newCheckSum = crc32.getValue();
203:
204: writeJemmyLog("CRC32=" + newCheckSum);
205:
206: return newCheckSum == checkSum;
207: }
208:
209: public static String getFullTestName(String strName) {
210: return strName.replaceAll(" |:|,|#", "_");
211: }
212:
213: public static JTextFieldOperator getTextFieldOpByLabel(
214: JLabelOperator opAssociatedLabel) {
215: JLabel component = (JLabel) opAssociatedLabel.getSource();
216:
217: if (component.getLabelFor() instanceof JPanel) {
218: return new JTextFieldOperator(new ContainerOperator(
219: (Container) component.getLabelFor().getParent()));
220: } else {
221: return new JTextFieldOperator((JTextField) component
222: .getLabelFor());
223: }
224: }
225:
226: }
|