Debian Codegraph @

rhino/testsrc/tests/src/com/netscape/javascript/qa/drivers/LiveNavEnv.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8 -*-
2 * Copyright (C) 1997, 1998 Netscape Communications Corporation,
3 * All Rights Reserved.
4 */
5
6 package com.netscape.javascript.qa.drivers;
7
8 import java.io.*;
9
10 import java.applet.Applet;
11 import java.util.Vector;
12 import netscape.security.PrivilegeManager;
13 import netscape.javascript.JSObject;
14 import com.netscape.javascript.qa.liveconnect.LiveConnectTest;
15 import com.netscape.javascript.qa.liveconnect.jsobject.JSObject_001;
16
17 /**
18 * TestEnvironment for running JSObject test applets in Navigator. LiveNavDrv
19 * uses LiveConnect to create Navigator windows in which JavaScript tests are
20 * opened and evaluated.
21 *
22 * @see com.netscape.javascript.qa.drivers.LiveNavDrv
23 * @see com.netscape.javascript.qa.drivers.NavDrv
24 * @see com.netscape.javascript.qa.drivers.NavEnv
25 *
26 * @author [email protected]
27 *
28 */
29 public class LiveNavEnv extends NavEnv {
30 TestFile file;
31 TestSuite suite;
32 LiveNavDrv driver;
33 private JSObject result;
34 JSObject opener;
35 JSObject testcases;
36 JSObject location;
37
38 boolean evaluatedSuccessfully = false;
39 JSObject window;
40
41 Applet applet;
42
43 private String WINDOW_NAME;
44
45 /**
46 * Construct a new LiveNavEnv.
47 *
48 * @param file TestFile whose program will be evaluated in this
49 * environment
50 * @param suite TestSuite which this TestFile belongs to
51 * @param driver TestDriver that is currently running
52 */
53 public LiveNavEnv( TestFile file, TestSuite suite, LiveNavDrv driver ) {
54 super( file, suite, driver );
55 }
56
57 /**
58 * Open the current TestFile in the window by using LiveConnect to set the
59 * window's location.href property to a URL where the TestFile can be
60 * found.
61 *
62 * XXX need to generate HTML file on the fly.
63 */
64 public Object executeTestFile() {
65 try {
66 location = (JSObject) window.getMember( "location" );
67 driver.p( file.name );
68
69 // we need to look for the html file in the test directory.
70 // the name of the file is the class name + ".html"
71
72 String classname = (file.name.substring(0, file.name.length() -
73 ".class".length()) + ".html" );
74
75 String s = driver.HTTP_PATH + classname;
76
77 System.out.println( "trying to set browser window to " + s );
78
79 location.setMember( "href", s );
80 evaluatedSuccessfully = waitForCompletion();
81
82 } catch ( Exception e ) {
83 driver.p( file.name + " failed with exception: " + e );
84 file.exception = e.toString();
85 if ( file.name.endsWith( "-n.js" ) ) {
86 this.file.passed = true;
87 evaluatedSuccessfully = true;
88 } else {
89 this.file.passed = false;
90 this.suite.passed = false;
91 evaluatedSuccessfully = false;
92 }
93 }
94 return null;
95 }
96
97 /**
98 * Get a reference to the LiveConnectTest applet. Unfortunately, due
99 * to reasons I don't understand, we can't just get the TestFile object
100 * from the LiveConnectTest via LiveConnect. Attempting to cast the
101 * applet to LiveConnectTest fails with a java.lang.ClassCastException,
102 * so we continue to use the temporary file hack in Navigator. Need a
103 * reference to the applet so we can properly destroy it before closing
104 * the Navigator window.
105 *
106 * This is http://scopus/bugsplat/show_bug.cgi?id=300350, and when that's
107 * fixed we can ue the getAppletClass method below to directly access
108 * the LiveConnectTest's TestFile file object so we don't have use the
109 * temporary file hack.
110 *
111 */
112 public Applet getApplet() {
113 Object document = (Object) window.getMember("document");
114 Object applets = ((JSObject) document).getMember("applets");
115 return (Applet) ((JSObject) applets).getSlot( 0 );
116 }
117
118 /**
119 * Currently does not work.
120 *
121 * @see #getApplet
122 */
123
124 public void getAppletClass(Applet applet) {
125 try {
126 driver.p( "the class of applet is " +
127 applet.getClass().toString() );
128
129 driver.p( "is it a JSObject_001? " +
130 (applet instanceof JSObject_001 ));
131
132 driver.p( "is it a LiveConnectTest? " +
133 (applet instanceof LiveConnectTest ));
134
135 driver.p( "is it an Applet? " +
136 (applet instanceof Applet ));
137
138 driver.p( "Try to cast applet to JSObject_001" );
139
140 // this throws the ClassCastException
141 driver.p( ((JSObject_001) applet).toString() );
142
143 } catch ( Exception e ) {
144 driver.p( "parseResult threw exception: " + e );
145 }
146 }
147
148 /**
149 * Parse the results file for this test. The which contains data in the
150 * following format:
151 * <pre>
152 * CLASSNAME [LiveConnectTest class name]
153 * PASSED [true, false]
154 * LENGTH [number of testcases in this test]
155 * NO_PASSED [number of testcases that passed]
156 * NO_FAILED [number of testcases that failed]
157 * </pre>
158 *
159 * @see com.netscape.javascript.qa.liveconnect.LiveConnectTest#writeResultsToTempFile
160 */
161 public boolean parseResult() {
162 applet = getApplet();
163
164 Vector label = null;
165 Vector value = null;
166
167 try {
168 FileReader fr = new FileReader(
169 driver.OUTPUT_DIRECTORY.getAbsolutePath()+
170 (driver.OUTPUT_DIRECTORY.getAbsolutePath().endsWith(File.separator)
171 ? ""
172 : File.separator ) +
173 LiveConnectTest.TEMP_LOG_NAME);
174 BufferedReader br = new BufferedReader( fr );
175 String classname = br.readLine();
176 boolean passed = (new Boolean(br.readLine())).booleanValue();
177 int length = (new Double(br.readLine())).intValue();
178 int no_passed = (new Double(br.readLine())).intValue();
179 int no_failed = (new Double(br.readLine())).intValue();
180 if ( ! passed ) {
181 this.file.passed = false;
182 this.suite.passed = false;
183 }
184 this.file.totalCases += length;
185
186 this.file.casesPassed += no_passed;
187 this.suite.casesPassed += no_passed;
188
189 this.file.casesFailed += no_failed;
190 this.suite.casesFailed += no_failed;
191
192 } catch ( IOException e ) {
193 driver.p( e.toString() );
194 return false;
195 }
196 return true;
197 }
198
199 /**
200 * Destroy the test applet, close Navigator window, and delete the
201 * reference to the test applet's window.
202 */
203 public void close() {
204 applet.destroy();
205 opener.eval( WINDOW_NAME +".close()" );
206 opener.eval( "delete " + WINDOW_NAME );
207 }
208 }