| OLD | NEW |
| (Empty) |
| 1 # Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
| 2 # for details. All rights reserved. Use of this source code is governed by a | |
| 3 # BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 #!/usr/bin/env python | |
| 6 # | |
| 7 | |
| 8 """A test for htmlconverter.py | |
| 9 """ | |
| 10 | |
| 11 from os.path import abspath, basename, dirname, exists, join, split | |
| 12 import optparse | |
| 13 import os | |
| 14 import sys | |
| 15 import subprocess | |
| 16 | |
| 17 # The inputs to our test | |
| 18 TEST1_HTML = """ | |
| 19 <html> | |
| 20 <head></head> | |
| 21 <body> | |
| 22 <script type="application/javascript"> | |
| 23 if (window.layoutTestController) { | |
| 24 window.layoutTestController.dumpAsText(); | |
| 25 } | |
| 26 </script> | |
| 27 | |
| 28 <!-- embed source code --> | |
| 29 <script type="application/dart"> | |
| 30 #import('dart:dom'); | |
| 31 main() { | |
| 32 window.alert('hi'); | |
| 33 } | |
| 34 </script> | |
| 35 </body> | |
| 36 </html> | |
| 37 """ | |
| 38 | |
| 39 TEST1_OUTPUT = """ | |
| 40 ALERT: hi | |
| 41 Content-Type: text/plain | |
| 42 | |
| 43 #EOF | |
| 44 """ | |
| 45 | |
| 46 TEST2_HTML = """ | |
| 47 <html> | |
| 48 <head></head> | |
| 49 <body> | |
| 50 <script type="application/javascript"> | |
| 51 if (window.layoutTestController) { | |
| 52 window.layoutTestController.dumpAsText(); | |
| 53 } | |
| 54 </script> | |
| 55 | |
| 56 <!-- embed source code --> | |
| 57 <script type="application/dart" src="test_2.dart"></script> | |
| 58 </body> | |
| 59 </html> | |
| 60 """ | |
| 61 | |
| 62 TEST2_DART = """ | |
| 63 #library('test2'); | |
| 64 #import('dart:dom'); | |
| 65 main() { | |
| 66 window.alert('test2!'); | |
| 67 } | |
| 68 """ | |
| 69 | |
| 70 TEST2_OUTPUT = """ | |
| 71 ALERT: test2! | |
| 72 Content-Type: text/plain | |
| 73 | |
| 74 #EOF | |
| 75 """ | |
| 76 | |
| 77 TEST3_HTML = """ | |
| 78 <html> | |
| 79 <head></head> | |
| 80 <body> | |
| 81 <script type="application/javascript"> | |
| 82 if (window.layoutTestController) { | |
| 83 window.layoutTestController.dumpAsText(); | |
| 84 } | |
| 85 </script> | |
| 86 | |
| 87 <!-- embed source code --> | |
| 88 <script type="application/dart" src="test_3.dart"></script> | |
| 89 </body> | |
| 90 </html> | |
| 91 """ | |
| 92 | |
| 93 TEST3_DART = """ | |
| 94 #import('dart:dom'); | |
| 95 #source('test_3a.dart'); | |
| 96 #source('test_3b.dart'); | |
| 97 """ | |
| 98 | |
| 99 TEST3_DART_A = """ | |
| 100 class MyClass { | |
| 101 static myprint() { | |
| 102 window.alert('test3!'); | |
| 103 } | |
| 104 } | |
| 105 """ | |
| 106 | |
| 107 TEST3_DART_B = """ | |
| 108 main() { | |
| 109 MyClass.myprint(); | |
| 110 } | |
| 111 """ | |
| 112 | |
| 113 TEST3_OUTPUT = """ | |
| 114 ALERT: test3! | |
| 115 Content-Type: text/plain | |
| 116 | |
| 117 #EOF | |
| 118 """ | |
| 119 | |
| 120 TEST4_HTML = """ | |
| 121 <html> | |
| 122 <head></head> | |
| 123 <body> | |
| 124 <script type="application/javascript"> | |
| 125 if (window.layoutTestController) { | |
| 126 window.layoutTestController.dumpAsText(); | |
| 127 } | |
| 128 </script> | |
| 129 | |
| 130 <script type="application/dart" src="test_4.dart"></script> | |
| 131 </body> | |
| 132 </html> | |
| 133 """ | |
| 134 | |
| 135 TEST4_DART = """ | |
| 136 #import('dart:dom'); | |
| 137 #import('../samples/ui_lib/observable/observable.dart'); | |
| 138 | |
| 139 main() { | |
| 140 // use imported code | |
| 141 var arr = new ObservableList(); | |
| 142 arr.addChangeListener((EventSummary events) { | |
| 143 var t = ['update', 'add ', | |
| 144 'remove', 'global'][events.events[0].type]; | |
| 145 var o = events.events[0].oldValue; | |
| 146 o = (o != null ? o : '_'); | |
| 147 var n = events.events[0].newValue; | |
| 148 n = (n != null ? n : '_'); | |
| 149 window.alert(" " + t + " " + o + " -> " + n); | |
| 150 }); | |
| 151 EventBatch.wrap((e) { arr.add(3); })(null); | |
| 152 EventBatch.wrap((e) { arr.add(2); })(null); | |
| 153 EventBatch.wrap((e) { arr.add(1); })(null); | |
| 154 EventBatch.wrap((e) { arr[0] = 5; })(null); | |
| 155 EventBatch.wrap((e) { arr[2] = 0; })(null); | |
| 156 EventBatch.wrap((e) { arr.removeAt(1); })(null); | |
| 157 EventBatch.wrap((e) { arr.clear(); })(null); | |
| 158 } | |
| 159 """ | |
| 160 | |
| 161 # Expected output when run in DumpRenderTree | |
| 162 TEST4_OUTPUT = """ | |
| 163 ALERT: add _ -> 3 | |
| 164 ALERT: add _ -> 2 | |
| 165 ALERT: add _ -> 1 | |
| 166 ALERT: update 3 -> 5 | |
| 167 ALERT: update 1 -> 0 | |
| 168 ALERT: remove 2 -> _ | |
| 169 ALERT: global _ -> _ | |
| 170 Content-Type: text/plain | |
| 171 | |
| 172 #EOF | |
| 173 """ | |
| 174 | |
| 175 TEST5_HTML = """ | |
| 176 <html> | |
| 177 <head></head> | |
| 178 <body> | |
| 179 <script type="application/javascript"> | |
| 180 if (window.layoutTestController) { | |
| 181 window.layoutTestController.dumpAsText(); | |
| 182 } | |
| 183 </script> | |
| 184 | |
| 185 <!-- embed source code --> | |
| 186 <script type="application/dart"> | |
| 187 #import('dart:dom'); | |
| 188 main() { | |
| 189 var element = document.getElementById("test5div"); | |
| 190 if (element == null) { | |
| 191 window.alert("this script shoulnd't be run synchronously"); | |
| 192 } else { | |
| 193 window.alert(element.innerHTML); | |
| 194 } | |
| 195 } | |
| 196 </script> | |
| 197 <div id="test5div">this is visible on DOMContentLoaded</div> | |
| 198 </body> | |
| 199 </html> | |
| 200 """ | |
| 201 | |
| 202 TEST5_OUTPUT = """ | |
| 203 ALERT: this is visible on DOMContentLoaded | |
| 204 Content-Type: text/plain | |
| 205 this is visible on DOMContentLoaded | |
| 206 #EOF | |
| 207 """ | |
| 208 | |
| 209 TEST6_HTML = """ | |
| 210 <html> | |
| 211 <head></head> | |
| 212 <body> | |
| 213 <script type="application/javascript"> | |
| 214 if (window.layoutTestController) { | |
| 215 window.layoutTestController.dumpAsText(); | |
| 216 } | |
| 217 </script> | |
| 218 | |
| 219 <!-- embed source code --> | |
| 220 <script type="application/dart"> | |
| 221 #import('dart:html', prefix: 'html'); | |
| 222 main() { | |
| 223 html.window.alert('hi'); | |
| 224 } | |
| 225 </script> | |
| 226 </body> | |
| 227 </html> | |
| 228 """ | |
| 229 | |
| 230 TEST6_OUTPUT = """ | |
| 231 ALERT: hi | |
| 232 Content-Type: text/plain | |
| 233 | |
| 234 #EOF | |
| 235 """ | |
| 236 | |
| 237 | |
| 238 FILES = { | |
| 239 'test_1.html': TEST1_HTML, | |
| 240 | |
| 241 'test_2.html': TEST2_HTML, | |
| 242 'test_2.dart': TEST2_DART, | |
| 243 | |
| 244 'test_3.html': TEST3_HTML, | |
| 245 'test_3.dart': TEST3_DART, | |
| 246 'test_3a.dart': TEST3_DART_A, | |
| 247 'test_3b.dart': TEST3_DART_B, | |
| 248 | |
| 249 'test_4.html': TEST4_HTML, | |
| 250 'test_4.dart': TEST4_DART, | |
| 251 | |
| 252 'test_5.html': TEST5_HTML, | |
| 253 'test_6.html': TEST6_HTML, | |
| 254 } | |
| 255 | |
| 256 INPUTS = [ | |
| 257 'test_1.html', | |
| 258 'test_2.html', | |
| 259 'test_3.html', | |
| 260 'test_4.html', | |
| 261 'test_5.html', | |
| 262 'test_6.html' | |
| 263 ] | |
| 264 | |
| 265 OUTPUTS = [ | |
| 266 TEST1_OUTPUT, | |
| 267 TEST2_OUTPUT, | |
| 268 TEST3_OUTPUT, | |
| 269 TEST4_OUTPUT, | |
| 270 TEST5_OUTPUT, | |
| 271 TEST6_OUTPUT | |
| 272 ] | |
| 273 | |
| 274 CLIENT_PATH = dirname(dirname(abspath(__file__))) | |
| 275 RED_COLOR = "\033[31m" | |
| 276 GREEN_COLOR = "\033[32m" | |
| 277 YELLOW_COLOR = "\033[33m" | |
| 278 NO_COLOR = "\033[0m" | |
| 279 | |
| 280 last_line_length = 0 | |
| 281 def printLine(s): | |
| 282 """ Prints a line in place (erasing the previous line). """ | |
| 283 global last_line_length | |
| 284 s = " Testing htmlconverter.py: " + s | |
| 285 if last_line_length > 0: | |
| 286 print "\r" + (" " * last_line_length) + "\r", | |
| 287 last_line_length = len(s) | |
| 288 print s, | |
| 289 sys.stdout.flush() | |
| 290 | |
| 291 def execute(cmd, verbose=False): | |
| 292 """Execute a command in a subprocess. """ | |
| 293 if verbose: print 'Executing: ' + ' '.join(cmd) | |
| 294 pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
| 295 output, err = pipe.communicate() | |
| 296 if pipe.returncode != 0: | |
| 297 print 'Execution failed: ' + output + '\n' + err | |
| 298 if verbose or pipe.returncode != 0: | |
| 299 print output | |
| 300 print err | |
| 301 return pipe.returncode, output, err | |
| 302 | |
| 303 def browserRun(message, htmlfile, test, verbose): | |
| 304 # run the generated code | |
| 305 printLine(message + ' [%d]' % (test + 1)) | |
| 306 status, out, err = execute([ | |
| 307 'tests/drt/DumpRenderTree', | |
| 308 htmlfile], | |
| 309 verbose) | |
| 310 if status != 0: | |
| 311 printLine("%sERROR%s test output [%d]" % (RED_COLOR, NO_COLOR, test + 1)) | |
| 312 return status | |
| 313 | |
| 314 # check that the output is equivalent and cleanup | |
| 315 out = '\n' + out | |
| 316 if out == OUTPUTS[test]: | |
| 317 printLine("%sPASS%s [%d]" % (GREEN_COLOR, NO_COLOR, test + 1)) | |
| 318 else: | |
| 319 printLine("%sFAIL%s [%d]" % (RED_COLOR, NO_COLOR, test + 1)) | |
| 320 print out | |
| 321 print err | |
| 322 return 1 | |
| 323 | |
| 324 return 0 | |
| 325 | |
| 326 def createInputFiles(): | |
| 327 printLine("... creating input files") | |
| 328 for filename in FILES: | |
| 329 with open(filename, 'w') as f: | |
| 330 f.write(FILES[filename]) | |
| 331 | |
| 332 def deleteInputFiles(): | |
| 333 for filename in FILES: | |
| 334 os.remove(filename) | |
| 335 | |
| 336 def runTest(test, target, verbose, keep_temporary_files): | |
| 337 inputfile = INPUTS[test] | |
| 338 suffix = '-js.html' if target == 'chromium' else '-dart.html' | |
| 339 outfile = abspath(join('out', inputfile.replace(".html", suffix))) | |
| 340 | |
| 341 # TODO(sigmund): tests should also run in dartium before converting them | |
| 342 | |
| 343 # run the htmlconverter.py script on it | |
| 344 printLine("... converting input html [%d]" % (test + 1)) | |
| 345 cmd = [sys.executable, 'tools/htmlconverter.py', inputfile, | |
| 346 '-o', 'out/', '-t', target] | |
| 347 if verbose: cmd.append('--verbose') | |
| 348 status, out, err = execute(cmd, verbose) | |
| 349 if status != 0: | |
| 350 printLine("%sERROR%s converting [%d]" % (RED_COLOR, NO_COLOR, test + 1)) | |
| 351 print out | |
| 352 print err | |
| 353 return status | |
| 354 | |
| 355 status = browserRun( | |
| 356 "... running compiled html in %s" % target, outfile, test, verbose) | |
| 357 if not keep_temporary_files: | |
| 358 os.remove(outfile) | |
| 359 return status | |
| 360 | |
| 361 def Flags(): | |
| 362 """ Consturcts a parser for extracting flags from the command line. """ | |
| 363 result = optparse.OptionParser() | |
| 364 result.add_option("--keep_temporary_files", | |
| 365 help="Keep temporary files created for each test", | |
| 366 default=False, | |
| 367 action="store_true") | |
| 368 result.add_option("-v", "--verbose", | |
| 369 help="Print verbose output", | |
| 370 default=False, | |
| 371 action="store_true") | |
| 372 result.add_option("-t", "--target", | |
| 373 help="The target html to generate", | |
| 374 metavar="[chromium,dartium]", | |
| 375 default='chromium,dartium') | |
| 376 result.set_usage("htmlconverter_test.py [--verbose -t chromium,dartium]") | |
| 377 return result | |
| 378 | |
| 379 def shouldRunTest(test, prefixes): | |
| 380 if len(prefixes) == 0: | |
| 381 return True | |
| 382 for a in prefixes: | |
| 383 if INPUTS[test].startswith(a): | |
| 384 return True | |
| 385 return False | |
| 386 | |
| 387 | |
| 388 def main(): | |
| 389 os.chdir(CLIENT_PATH) | |
| 390 parser = Flags() | |
| 391 options, args = parser.parse_args() | |
| 392 verbose = options.verbose | |
| 393 keep_temporary_files = options.keep_temporary_files | |
| 394 | |
| 395 createInputFiles() | |
| 396 for test in range(len(INPUTS)): | |
| 397 if shouldRunTest(test, args): | |
| 398 if 'chromium' in options.target: | |
| 399 if runTest(test, 'chromium', verbose, keep_temporary_files) != 0: | |
| 400 if not keep_temporary_files: | |
| 401 deleteInputFiles() | |
| 402 return 1 | |
| 403 if 'dartium' in options.target: | |
| 404 if runTest(test, 'dartium', verbose, keep_temporary_files) != 0: | |
| 405 if not keep_temporary_files: | |
| 406 deleteInputFiles() | |
| 407 return 1 | |
| 408 | |
| 409 if not keep_temporary_files: | |
| 410 deleteInputFiles() | |
| 411 return 0 | |
| 412 | |
| 413 | |
| 414 if __name__ == '__main__': | |
| 415 sys.exit(main()) | |
| OLD | NEW |