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-2006 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.test.gui.web.util;
043:
044: import org.netbeans.jellytools.actions.OptionsViewAction;
045:
046: import org.netbeans.jellytools.NbDialogOperator;
047: import org.netbeans.jellytools.OptionsOperator;
048: import org.netbeans.jellytools.Bundle;
049:
050: import org.netbeans.jellytools.properties.ComboBoxProperty;
051: import org.netbeans.jellytools.properties.TextFieldProperty;
052: import org.netbeans.jellytools.properties.PropertySheetTabOperator;
053: import org.netbeans.jellytools.properties.PropertySheetOperator;
054:
055: import org.netbeans.jemmy.operators.JTextComponentOperator;
056:
057: import java.io.File;
058:
059: public class BrowserUtils {
060: private static String iSep = "|";
061: private String sampleUserAgentNS7 = "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.0.2) Gecko/20030208 Netscape/7.02";
062: private String sampleUserAgentNS4 = "User-Agent: Mozilla/4.78 [en] (X11; U; Linux 2.4.7-10smp i686)";
063: private String sampleUserAgentIE6 = "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)";
064:
065: public static final int NETSCAPE4 = 0;
066: public static final int NETSCAPE6 = 1;
067: public static final int NETSCAPE7 = 2;
068: public static final int MSIE6 = 3;
069: public static final int UNKNOWN = 32;
070:
071: public BrowserUtils() {
072: }
073:
074: public static void setExternalUnixBrowser() {
075: String browser = Bundle.getString(
076: "org.netbeans.modules.extbrowser.Bundle",
077: "CTL_UnixBrowserName");
078: setBrowser(browser);
079: }
080:
081: public static void setExternalWinBrowser() {
082: String browser = Bundle.getString(
083: "org.netbeans.modules.extbrowser.Bundle",
084: "CTL_WinBrowserName");
085: setBrowser(browser);
086: }
087:
088: public static void setSwingBrowser() {
089: String browser = Bundle.getString(
090: "org.netbeans.beaninfo.Bundle", "CTL_SwingBrowser");
091: setBrowser(browser);
092: }
093:
094: public static void setCLBrowser() {
095: String browser = Bundle.getString(
096: "org.netbeans.modules.extbrowser.Bundle",
097: "CTL_SimpleExtBrowser");
098: setBrowser(browser);
099: }
100:
101: public static void setCLBrowserCommand(String command) {
102: OptionsOperator oo = OptionsOperator.invoke();
103: String ideConfiguration = Bundle.getString(
104: "org.netbeans.core.Bundle",
105: "UI/Services/IDEConfiguration");
106: String sets = Bundle
107: .getString("org.netbeans.core.Bundle",
108: "UI/Services/IDEConfiguration/ServerAndExternalToolSettings");
109: String browsers = Bundle.getString("org.netbeans.core.Bundle",
110: "Services/Browsers");
111: String cl = Bundle.getString(
112: "org.netbeans.modules.extbrowser.Bundle",
113: "CTL_SimpleExtBrowser");
114: oo.selectOption(ideConfiguration + iSep + sets + iSep
115: + browsers + iSep + cl);
116: PropertySheetOperator pso = PropertySheetOperator.invoke();
117: PropertySheetTabOperator psto = new PropertySheetTabOperator(
118: pso);
119: String pnameBrowserExecutable = Bundle.getString(
120: "org.netbeans.modules.extbrowser.Bundle",
121: "PROP_browserExecutable");
122: TextFieldProperty pr = new TextFieldProperty(psto,
123: pnameBrowserExecutable);
124: if (!pr.getValue().equals(command)) {
125: pr.setValue(command);
126: }
127: }
128:
129: public static void setEBUBrowserCommand(String command) {
130: OptionsOperator oo = OptionsOperator.invoke();
131: String ideConfiguration = Bundle.getString(
132: "org.netbeans.core.Bundle",
133: "UI/Services/IDEConfiguration");
134: String sets = Bundle
135: .getString("org.netbeans.core.Bundle",
136: "UI/Services/IDEConfiguration/ServerAndExternalToolSettings");
137: String browsers = Bundle.getString("org.netbeans.core.Bundle",
138: "Services/Browsers");
139: String cl = Bundle.getString(
140: "org.netbeans.modules.extbrowser.Bundle",
141: "CTL_UnixBrowserName");
142: oo.selectOption(ideConfiguration + iSep + sets + iSep
143: + browsers + iSep + cl);
144: PropertySheetOperator pso = PropertySheetOperator.invoke();
145: PropertySheetTabOperator psto = new PropertySheetTabOperator(
146: pso);
147: String pnameBrowserExecutable = Bundle.getString(
148: "org.netbeans.modules.extbrowser.Bundle",
149: "PROP_browserExecutable");
150: TextFieldProperty pr = new TextFieldProperty(psto,
151: pnameBrowserExecutable);
152: if (!pr.getValue().equals(command)) {
153: pr.setValue(command);
154: }
155: }
156:
157: public static void setBrowser(String name) {
158: //new OptionsViewAction().perform();
159: OptionsOperator oo = OptionsOperator.invoke();
160: String ideConfiguration = Bundle.getString(
161: "org.netbeans.core.Bundle",
162: "UI/Services/IDEConfiguration");
163: String system = Bundle.getString("org.netbeans.core.Bundle",
164: "UI/Services/IDEConfiguration/System");
165: String systemSettings = Bundle.getString(
166: "org.netbeans.core.Bundle",
167: "Services/org-netbeans-core-IDESettings.settings");
168: oo.selectOption(ideConfiguration + iSep + system + iSep
169: + systemSettings);
170: PropertySheetOperator pso = PropertySheetOperator.invoke();
171: PropertySheetTabOperator psto = new PropertySheetTabOperator(
172: pso);
173: String pnameWebBrowser = Bundle.getString(
174: "org.netbeans.core.Bundle", "PROP_WWW_BROWSER");
175: ComboBoxProperty pr = new ComboBoxProperty(psto,
176: pnameWebBrowser);
177: if (!pr.getValue().equals(name)) {
178: pr.setValue(name);
179: }
180: }
181:
182: public static boolean handleSwingBrowserDialog() {
183: String title = Bundle.getString("org.openide.Bundle",
184: "NTF_InformationTitle");
185: try {
186: NbDialogOperator dialog = new NbDialogOperator(title);
187: dialog.ok();
188: return true;
189: } catch (Exception e) {
190: return false;
191: }
192: }
193:
194: /**
195: DDE servers section
196: */
197: public static void setDDEServerExplorer() {
198: System.out.println("#####\nNot implemented yet\n#####");
199: }
200:
201: public static void setDDEServerNetscape() {
202: System.out.println("#####\nNot implemented yet\n#####");
203: }
204:
205: public static void setDDEServerNetscape6() {
206: System.out.println("#####\nNot implemented yet\n#####");
207: }
208:
209: /* End of DDE servers section */
210:
211: /**
212: Get Browsers section
213: */
214:
215: public static String getIEInPath() {
216: System.out.println("#####\nNot implemented yet\n#####");
217: return null;
218: }
219:
220: public static String getIEFullPath() {
221: System.out.println("#####\nNot implemented yet\n#####");
222: return null;
223: }
224:
225: public static String getNetscapeFullPath() {
226: String[] paths = null;
227: String command = null;
228: if (System.getProperty("os.name").indexOf("Windows") != -1) {
229: System.out
230: .println("##########\nThis test must be extended for Windows platform\n#######");
231: return null;
232: } else {
233: paths = new String[] { "/usr/bin/netscape",
234: "/usr/local/bin/netscape", "/bin/netscape" };
235: }
236: for (int i = 0; i < paths.length; i++) {
237: if ((new File(paths[i])).exists()) {
238: command = paths[i] + " {URL}";
239: i = paths.length;
240: }
241: }
242: if (command == null) {
243: StringBuffer reason = new StringBuffer(
244: "Nothing of following commands found on your system : ");
245: for (int i = 0; i < paths.length; i++) {
246: reason.append(paths[i] + ";");
247: }
248: System.out.println("##########\n" + reason.toString()
249: + "\n##########");
250: return null;
251: }
252: return command;
253: }
254:
255: public static String getNetscapeInPath() {
256: String netscapeWin = "netscape.exe";
257: String netscapeUx = "netscape";
258: if (System.getProperty("os.name").indexOf("Windows") != -1) {
259: return netscapeWin;
260: } else {
261: return netscapeUx;
262: }
263: }
264:
265: public static String getNetscape6FullPath() {
266: String[] paths = null;
267: String command = null;
268: if (System.getProperty("os.name").indexOf("Windows") != -1) {
269: System.out
270: .println("##########\nThis test must be extended for Windows platform\n#######");
271: return null;
272: } else {
273: paths = new String[] { "/usr/local/netscape6/netscape",
274: "/usr/bin/netscape6", "/usr/local/bin/netscape6",
275: "/bin/netscape6" };
276: }
277: for (int i = 0; i < paths.length; i++) {
278: if ((new File(paths[i])).exists()) {
279: command = paths[i] + " {URL}";
280: i = paths.length;
281: }
282: }
283: if (command == null) {
284: StringBuffer reason = new StringBuffer(
285: "Nothing of following commands found on your system : ");
286: for (int i = 0; i < paths.length; i++) {
287: reason.append(paths[i] + ";");
288: }
289: System.out.println("##########\n" + reason.toString()
290: + "\n##########");
291: return null;
292: }
293: return command;
294: }
295:
296: public static String getNetscape6InPath() {
297: String netscapeWin = "netscp6.exe";
298: String netscapeUx = "netscape6";
299: if (System.getProperty("os.name").indexOf("Windows") != -1) {
300: return netscapeWin;
301: } else {
302: return netscapeUx;
303: }
304: }
305:
306: /* Errors handling section */
307:
308: /**
309: Handle error in CL browser configuration
310: */
311: public static boolean handleErrorInCLBrowser() {
312: String title = Bundle.getString("org.openide.Bundle",
313: "NTF_WarningTitle");
314: String text = Bundle.getString(
315: "org.netbeans.modules.extbrowser.Bundle",
316: "EXC_Invalid_Processor");
317: NbDialogOperator op = null;
318: try {
319: op = new NbDialogOperator(title);
320: } catch (Exception e) {
321: System.out.println(title + " dialog not found");
322: return false;
323: }
324: // JTextComponentOperator tco = new JTextComponentOperator(op,0);
325: // System.out.println("#####TEXT:" + tco.getText());
326: op.ok();
327: return true;
328: }
329:
330: /**
331: Handle error in CL browser configuration
332: */
333: public static boolean handleErrorInUnixBrowser() {
334: String title = Bundle.getString("org.openide.Bundle",
335: "NTF_WarningTitle");
336: String text = Bundle.getString(
337: "org.netbeans.modules.extbrowser.Bundle",
338: "MSG_Cant_run_netscape"); //{0} - name of command, for example netscape88
339: try {
340: NbDialogOperator op = new NbDialogOperator(title);
341: op.ok();
342: return true;
343: } catch (Exception e) {
344: return false;
345: }
346: }
347:
348: public static int getBrowserVersion(String userAgent) {
349: if (userAgent.indexOf("MSIE 6") != -1) {
350: return MSIE6;
351: }
352: if (userAgent.indexOf("Mozilla/4") != -1) {
353: return NETSCAPE4;
354: }
355: if (userAgent.indexOf("Mozilla/5") != -1) {
356: if (userAgent.indexOf("Netscape/7") != -1) {
357: return NETSCAPE7;
358: }
359: if (userAgent.indexOf("Netscape6/6") != -1) {
360: return NETSCAPE6;
361: }
362: }
363: return UNKNOWN;
364: }
365:
366: public static String getBrowserDescription(int version) {
367: String descr = null;
368: switch (version) {
369: case NETSCAPE4:
370: descr = "Netscape4.x browser";
371: break;
372: case NETSCAPE6:
373: descr = "Netscape6.x browser";
374: break;
375: case NETSCAPE7:
376: descr = "Netscape7.x browser";
377: break;
378: case MSIE6:
379: descr = "Internet Explorer 6";
380: break;
381: case UNKNOWN:
382: descr = "This browser is unknown by BrowserUtils";
383: break;
384: default:
385: descr = "Can't understand version " + version;
386: }
387: return descr;
388: }
389: }
|