001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.visualweb.gravy;
043:
044: import java.util.*;
045: import java.awt.*;
046: import org.netbeans.jemmy.operators.*;
047: import org.netbeans.jemmy.util.DefaultVisualizer;
048: import org.netbeans.jemmy.util.NameComponentChooser;
049: import org.netbeans.jemmy.Waiter;
050: import org.netbeans.jemmy.Waitable;
051: import org.netbeans.jemmy.EventTool;
052: import org.netbeans.jemmy.QueueTool;
053: import java.awt.event.*;
054: import org.netbeans.junit.NbTestCase;
055: import org.netbeans.jellytools.*;
056: import org.netbeans.jellytools.Bundle;
057: import org.netbeans.jemmy.EventDispatcher;
058:
059: /**
060: * Class with different ancillary utils.
061: */
062: public class Util {
063: public static final String WINDOWS_OS_FAMILY_NAME = "Windows",
064: LINUX_OS_FAMILY_NAME = "Linux",
065: SUN_OS_FAMILY_NAME = "SunOS", MAC_OS_FAMILY_NAME = "Mac";
066:
067: static MainWindowOperator mainWindow;
068: static JMenuBarOperator mainMenu;
069:
070: /**
071: * Get delay scale for tests.
072: * @return double Value of delay scale.
073: */
074: public static double getDelayScale() {
075: String s = System.getProperty("Env-RaveTestsDelayScale", "1");
076: try {
077: return Double.parseDouble(s);
078: } catch (NumberFormatException e) {
079: return 1;
080: }
081: }
082:
083: /**
084: * Get operation system's name.
085: * @return String Name of operation system.
086: */
087: public static String getOperatingSystemName() {
088: return System.getProperty("os.name");
089: }
090:
091: /**
092: * @return True if operation system is from Windows family.
093: */
094: public static boolean isWindowsOS() {
095: String osName = getOperatingSystemName();
096: return (osName.toUpperCase().indexOf(
097: WINDOWS_OS_FAMILY_NAME.toUpperCase()) > -1);
098: }
099:
100: /**
101: * @return True if operation system is from Sun (Solaris) family.
102: */
103: public static boolean isSunOS() {
104: String osName = getOperatingSystemName();
105: return (osName.toUpperCase().indexOf(
106: SUN_OS_FAMILY_NAME.toUpperCase()) > -1);
107: }
108:
109: /**
110: * @return True if operation system is from MAC OS X family.
111: */
112: public static boolean isMacOS() {
113: String osName = getOperatingSystemName();
114: return (osName.toUpperCase().indexOf(
115: MAC_OS_FAMILY_NAME.toUpperCase()) > -1);
116: }
117:
118: /**
119: * Get main window.
120: * @return MainWindowOperator.
121: */
122: public static MainWindowOperator getMainWindow() {
123: if (mainWindow == null) {
124: Operator
125: .setDefaultComponentVisualizer(new DefaultVisualizer());
126: mainWindow = new MainWindowOperator();
127: }
128: return (mainWindow);
129: }
130:
131: /**
132: * Get main menu.
133: * @return JMenuBarOperator Main menu.
134: */
135: public static JMenuBarOperator getMainMenu() {
136: if (mainMenu == null) {
137: mainMenu = new JMenuBarOperator(getMainWindow());
138: }
139: return (mainMenu);
140: }
141:
142: /**
143: * Get main tab.
144: * @return JTabbedPaneOperator Main tab.
145: */
146: public static JTabbedPaneOperator getMainTab() {
147: return (new JTabbedPaneOperator(new ContainerOperator(
148: getMainWindow(), new NameComponentChooser("mainTab"))));
149: }
150:
151: /**
152: * Select tab with specified name.
153: * @param tabName Name of the tab.
154: */
155: public static void selectTab(String tabName) {
156: new org.netbeans.jellytools.TopComponentOperator(tabName);
157: }
158:
159: /**
160: * Generate time stamp.
161: * @return String Time stamp.
162: */
163: public static String generateTimeStamp() {
164: Calendar calendar = Calendar.getInstance();
165: return ("" + calendar.get(Calendar.YEAR)
166: + calendar.get(Calendar.MONTH)
167: + calendar.get(Calendar.DAY_OF_MONTH)
168: + calendar.get(Calendar.HOUR_OF_DAY)
169: + calendar.get(Calendar.MINUTE) + calendar
170: .get(Calendar.SECOND));
171: }
172:
173: /**
174: * Close main window.
175: */
176: public static void closeWindow() {
177: (new org.netbeans.jellytools.actions.Action(Bundle
178: .getStringTrimmed("org.netbeans.core.Bundle",
179: "Menu/Window").substring(1)/*Menu/Window=&Window*/
180: + "|"
181: + Bundle.getStringTrimmed(
182: "org.netbeans.core.windows.actions.Bundle",
183: "LBL_CloseWindowAction"), /*Close Window*/
184: null)).performMenu();
185: }
186:
187: /**
188: * Save all that is opened and changed with main menu.
189: */
190: public static void saveAll() {
191: // find item "Save all"
192: JMenuItemOperator saveAll = Util.getMainMenu().showMenuItem(
193: new String[] { "File", "Save All" });
194: if (saveAll.isEnabled()) {
195: Util
196: .getMainMenu()
197: .pushMenu(
198: Bundle.getStringTrimmed(
199: "org.netbeans.core.Bundle",
200: "Menu/File")
201: + "|"
202: + Bundle
203: .getStringTrimmed(
204: "com.sun.rave.gravy.project.actions.Bundle",
205: "LBL_SaveAllAction"));
206: }
207: }
208:
209: /**
210: * Save all that is opened and changed with API function.
211: */
212: public static void saveAllAPICall() {
213: ((org.openide.actions.SaveAllAction) org.openide.actions.SaveAllAction
214: .findObject(org.openide.actions.SaveAllAction.class,
215: true)).performAction();
216: }
217:
218: /**
219: * Get time of execution.
220: * @return long Time in format long.
221: */
222: public static long getExecutionTime() {
223: String execTime = System
224: .getProperty("xtest.ide.execution.time");
225: if (execTime != null) {
226: System.out.println("Property = " + execTime);
227: int comps[] = new int[6];
228: StringTokenizer token = new StringTokenizer(execTime, ":");
229: for (int i = 0; i < 6; i++) {
230: comps[i] = Integer.parseInt(token.nextToken());
231: }
232: Calendar calend = Calendar.getInstance();
233: calend.set(comps[0], comps[1] - 1, comps[2], comps[3],
234: comps[4], comps[5]);
235: long result = calend.getTimeInMillis();
236: System.out.print("Execution time: " + calend.toString());
237: System.out.println(" = " + result);
238: return (result);
239: } else {
240: throw (new RuntimeException(
241: "xtest.ide.execution.time property is empty"));
242: }
243: }
244:
245: /**
246: * Get current time .
247: * @return long Time in format long.
248: */
249: public static long getCurrentTime() {
250: Calendar calend = Calendar.getInstance();
251: long result = calend.getTimeInMillis();
252: System.out.print("Current time: " + calend.toString());
253: System.out.println(" = " + result);
254: return (result);
255: }
256:
257: /**
258: * Save performance data.
259: */
260: public static void savePerformanceData(NbTestCase test, String id,
261: long startTime, long endTime) {
262: System.out.println("Time diff is " + (endTime - startTime));
263: System.out.println("Saving it into " + test.getName()
264: + ".perfdata");
265: test.getLog(test.getName() + ".perfdata").print(
266: test.getName() + "." + id + " = ");
267: test.getLog(test.getName() + ".perfdata").println(
268: endTime - startTime);
269: }
270:
271: /**
272: * Save performance data.
273: */
274: public static void savePerformanceData(NbTestCase test, String id,
275: long startTime) {
276: savePerformanceData(test, id, startTime, getCurrentTime());
277: }
278:
279: /**
280: * Wait for specified time.
281: * @param millisec Amount of milliseconds.
282: */
283: public static void wait(int millisec) {
284: millisec = new Double(millisec * getDelayScale()).intValue();
285: System.out.println("Delay scale() = " + getDelayScale());
286: System.out.println("Sleep to " + millisec / 1000 + " seconds");
287: EventDispatcher.waitQueueEmpty();
288: try {
289: Thread.sleep(millisec);
290: } catch (Exception e) {
291: }
292: EventDispatcher.waitQueueEmpty();
293: }
294:
295: /**
296: * Wait for any keyboard event.
297: */
298: public static void waitKeyboardEvent() {
299: waitKeyboardEvent(1200000);
300: }
301:
302: /**
303: * Wait for any keyboard event with specified delay.
304: * @param delay Time for delay in long format
305: */
306: public static void waitKeyboardEvent(long delay) {
307: String interactiveTestMode = System
308: .getProperty("interactive.test.mode");
309: if ((interactiveTestMode == null)
310: || (!interactiveTestMode.equalsIgnoreCase("Yes"))) {
311: return;
312: }
313: Waiter kbdWaiter = getKeyboardEventWaiter();
314: kbdWaiter.getTimeouts().setTimeout("Waiter.WaitingTime", delay);
315: try {
316: kbdWaiter.waitAction(null);
317: } catch (Exception e) {
318: e.printStackTrace();
319: }
320: }
321:
322: private static Waiter getKeyboardEventWaiter() {
323: final String msgKeyWasPressed = "Key was pressed";
324: Waiter kbdWaiter = new Waiter(new Waitable() {
325: public Object actionProduced(Object obj) {
326: AWTEvent awtEvent = null;
327: try {
328: awtEvent = EventTool.getLastEvent();
329: } catch (Exception e) {
330: TestUtils.outMsg("+++ Exception was thrown = "
331: + e.getMessage());
332: e.printStackTrace();
333: return e.getMessage();
334: }
335: Util.wait(1000);
336: new QueueTool().waitEmpty();
337: if (awtEvent.getID() == KeyEvent.KEY_RELEASED) {
338: TestUtils.outMsg("+++ " + msgKeyWasPressed);
339: return msgKeyWasPressed;
340: }
341: return null;
342: }
343:
344: public String getDescription() {
345: return "Waiting for key press...";
346: }
347: });
348: return kbdWaiter;
349: }
350: }
|