Debian Codegraph @

rhino/testsrc/tests/src/com/netscape/javascript/qa/drivers/NavEnv.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 netscape.javascript.JSObject;
11 import com.netscape.javascript.qa.drivers.*;
12
13 /**
14 * TestEnvironment for running JavaScript tests in Navigator. NavDrv uses
15 * LiveConnect to create Navigator windows in which JavaScript tests are
16 * opened and evaluated.
17 *
18 *
19 * @see com.netscape.javascript.qa.drivers.NavDrv
20 *
21 * @author [email protected]
22 *
23 */
24 public class NavEnv implements TestEnvironment {
25 TestFile file;
26 TestSuite suite;
27 NavDrv driver;
28 private JSObject result;
29 JSObject opener;
30 JSObject testcases;
31 JSObject location;
32
33 boolean evaluatedSuccessfully = false;
34 JSObject window;
35
36 private String WINDOW_NAME;
37
38 /**
39 * Constructor a new NavEnv.
40 */
41 public NavEnv( TestFile f, TestSuite s, NavDrv d ) {
42 this.file = f;
43 this.suite = s;
44 this.driver =d;
45
46 this.opener = (JSObject) JSObject.getWindow( d );
47 this.window = null;
48 this.WINDOW_NAME = "js" + getRandomWindowName();
49 }
50 /**
51 * Called by NavDrv to run the current TestFile.
52 */
53 public synchronized void runTest() {
54 int i = 0;
55 System.out.println( file.name );
56 try {
57 createContext();
58 file.startTime = driver.getCurrentTime();
59 System.out.println( i++ );
60 executeTestFile();
61 System.out.println( i++ );
62
63 if ( evaluatedSuccessfully ) {
64 System.out.println( i++ );
65
66 parseResult();
67 System.out.println( i++ );
68
69 }
70 file.endTime = driver.getCurrentTime();
71
72 } catch ( Exception e ) {
73 suite.passed = false;
74 file.passed = false;
75 file.exception = "file failed with exception: " + e ;
76 }
77 }
78
79 /**
80 * Creates a new JavaScript context, in this case a Navigator window, and
81 * returns the JSObject associated with that window.
82 */
83 public Object createContext() {
84 System.out.println( "opening window" );
85 opener.eval( WINDOW_NAME +" = window.open( '', '" + WINDOW_NAME + "' )" );
86 window = (JSObject) opener.getMember( WINDOW_NAME );
87 return window;
88 }
89
90 /**
91 * Open the current TestFile in the window by using LiveConnect to set the
92 * window's location.href property to a URL where the TestFile can be
93 * found.
94 */
95 public Object executeTestFile() {
96 System.out.println( "executeTestFile()" );
97 try {
98 location = (JSObject) window.getMember( "location" );
99 System.out.println( file.name );
100 String s = driver.HTTP_PATH + suite.name +"/" + file.name;
101 location.setMember( "href", driver.HTTP_PATH + suite.name +"/" +
102 file.name );
103 evaluatedSuccessfully = waitForCompletion();
104
105 } catch ( Exception e ) {
106 System.err.println( file.name + " failed with exception: " + e );
107 file.exception = e.toString();
108 if ( file.name.endsWith( "-n.js" ) ) {
109 this.file.passed = true;
110 evaluatedSuccessfully = true;
111 } else {
112 this.file.passed = false;
113 this.suite.passed = false;
114 evaluatedSuccessfully = false;
115 }
116 }
117 return null;
118 }
119
120 /**
121 * Checks the value of the variable "completed", which is set by the
122 * stopTest() function in each test.
123 *
124 * If the stopTest() function is not called in 20 seconds, the test
125 * fails.
126 *
127 * Negative tests will still succeed, since the onerror handler should
128 * call the stopTest() function.
129 *
130 */
131 public boolean waitForCompletion() {
132 int counter = 0;
133 if ( ! window.getMember( "completed" ).toString().equals("true") ) {
134 while (!window.getMember("completed").toString().equals("true")) {
135 try {
136 if ( counter > 20 ) {
137 file.passed = false;
138 file.exception += "test failed to complete";
139 System.out.println( "test failed to complete" );
140 return false;
141 }
142 System.out.println(".");
143 driver.sleep( 1000 );
144 counter++;
145 } catch ( Exception e ) {
146 System.out.println( "sleep failed: " + e );
147 return false;
148 }
149 }
150 }
151 return true;
152 }
153
154 /**
155 * Use LiveConnect to get the Navigator window's testcases property, which
156 * is defined in all tests. Parse the testcases array, and create a new
157 * TestCase object for each TestCase object it finds.
158 */
159 public synchronized boolean parseResult() {
160 try {
161 JSObject testcases = (JSObject) window.getMember("testcases");
162 file.totalCases = ((Number) ((JSObject) testcases).getMember("length")).intValue();
163 System.out.println( "testcases.length is " + file.totalCases );
164 for ( int i = 0; i < file.totalCases; i++ ) {
165 JSObject tc = (JSObject) ((JSObject) testcases).getSlot(i);
166
167 TestCase nc = new TestCase(
168 tc.getMember("passed") == null ? "null" : tc.getMember("passed").toString(),
169 tc.getMember("name") == null ? "null " : tc.getMember("name").toString(),
170 tc.getMember("description") == null ? "null " : tc.getMember("description").toString(),
171 tc.getMember("expect") == null ? "null " : tc.getMember("expect").toString(),
172 tc.getMember("actual") == null ? "null " : tc.getMember("actual").toString(),
173 tc.getMember("reason") == null ? "null " : tc.getMember("reason").toString()
174 );
175
176 file.caseVector.addElement( nc );
177
178 if ( nc.passed.equals("false") ) {
179 if ( file.name.endsWith( "-n.js" ) ) {
180 this.file.passed = true;
181 } else {
182 this.file.passed = false;
183 this.suite.passed = false;
184 }
185 }
186 }
187 } catch ( Exception e ) {
188 System.out.println( e );
189 file.exception = e.toString();
190 if ( file.name.endsWith( "-n.html" ) ) {
191 file.passed = true;
192 } else {
193 file.passed = false;
194 suite.passed = false;
195 }
196 }
197
198 // if the file failed, try to get the file's BUGNUMBER.
199 if ( this.file.passed == false ) {
200 try {
201 this.file.bugnumber = window.getMember("BUGNUMBER").toString();
202 } catch ( Exception e ) {
203 // do nothing
204 }
205 }
206
207 return true;
208 }
209
210 /**
211 * Close Navigator window.
212 */
213 public void close() {
214 opener.eval( WINDOW_NAME +".close()" );
215 opener.eval( "delete " + WINDOW_NAME );
216 }
217 /**
218 * Generate a random window name, so that each test is associated with a
219 * unique window.
220 */
221 public String getRandomWindowName() {
222 return (Integer.toString((new Double(Math.random()*100000)).intValue()));
223 }
224 }