Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 library browser; | 4 library browser; |
| 5 | 5 |
| 6 import "dart:async"; | 6 import "dart:async"; |
| 7 import "dart:core"; | 7 import "dart:core"; |
| 8 import "dart:io"; | 8 import "dart:io"; |
| 9 | 9 |
| 10 import 'android.dart'; | 10 import 'android.dart'; |
| (...skipping 688 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 699 | 699 |
| 700 Future<bool> terminate() { | 700 Future<bool> terminate() { |
| 701 var futures = []; | 701 var futures = []; |
| 702 underTermination = true; | 702 underTermination = true; |
| 703 testingServer.underTermination = true; | 703 testingServer.underTermination = true; |
| 704 for (BrowserTestingStatus status in browserStatus.values) { | 704 for (BrowserTestingStatus status in browserStatus.values) { |
| 705 futures.add(status.browser.close()); | 705 futures.add(status.browser.close()); |
| 706 } | 706 } |
| 707 return Future.wait(futures).then((values) { | 707 return Future.wait(futures).then((values) { |
| 708 testingServer.httpServer.close(); | 708 testingServer.httpServer.close(); |
| 709 testingServer.errorReportingServer.close(); | |
| 709 printDoubleReportingTests(); | 710 printDoubleReportingTests(); |
| 710 return !values.contains(false); | 711 return !values.contains(false); |
| 711 }); | 712 }); |
| 712 } | 713 } |
| 713 | 714 |
| 714 Browser getInstance() { | 715 Browser getInstance() { |
| 715 var browser = new Browser.byName(browserName); | 716 var browser = new Browser.byName(browserName); |
| 716 browser.logger = logger; | 717 browser.logger = logger; |
| 717 return browser; | 718 return browser; |
| 718 } | 719 } |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 734 final String local_ip; | 735 final String local_ip; |
| 735 | 736 |
| 736 const String driverPath = "/driver"; | 737 const String driverPath = "/driver"; |
| 737 const String nextTestPath = "/next_test"; | 738 const String nextTestPath = "/next_test"; |
| 738 const String reportPath = "/report"; | 739 const String reportPath = "/report"; |
| 739 const String waitSignal = "WAIT"; | 740 const String waitSignal = "WAIT"; |
| 740 const String terminateSignal = "TERMINATE"; | 741 const String terminateSignal = "TERMINATE"; |
| 741 | 742 |
| 742 var testCount = 0; | 743 var testCount = 0; |
| 743 var httpServer; | 744 var httpServer; |
| 745 var errorReportingServer; | |
| 744 bool underTermination = false; | 746 bool underTermination = false; |
| 745 bool useIframe = false; | 747 bool useIframe = false; |
| 746 | 748 |
| 747 Function testDoneCallBack; | 749 Function testDoneCallBack; |
| 748 Function nextTestCallBack; | 750 Function nextTestCallBack; |
| 749 | 751 |
| 750 BrowserTestingServer(this.local_ip, this.useIframe); | 752 BrowserTestingServer(this.local_ip, this.useIframe); |
| 751 | 753 |
| 752 Future start() { | 754 Future start() { |
| 753 return HttpServer.bind(local_ip, 0).then((createdServer) { | 755 return HttpServer.bind(local_ip, 0).then((createdServer) { |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 778 if (!underTermination) { | 780 if (!underTermination) { |
| 779 print("URI ${request.uri}"); | 781 print("URI ${request.uri}"); |
| 780 print("Textresponse $textResponse"); | 782 print("Textresponse $textResponse"); |
| 781 throw "Error returning content to browser: $error"; | 783 throw "Error returning content to browser: $error"; |
| 782 } | 784 } |
| 783 }); | 785 }); |
| 784 } | 786 } |
| 785 void errorHandler(e) { | 787 void errorHandler(e) { |
| 786 if (!underTermination) print("Error occured in httpserver: $e"); | 788 if (!underTermination) print("Error occured in httpserver: $e"); |
| 787 }; | 789 }; |
| 790 | |
| 788 httpServer.listen(handler, onError: errorHandler); | 791 httpServer.listen(handler, onError: errorHandler); |
| 789 return true; | 792 |
| 793 // Set up the error reporting server that enables us to send back | |
| 794 // errors from the browser. | |
| 795 return HttpServer.bind(local_ip, 0).then((createdReportServer) { | |
| 796 errorReportingServer = createdReportServer; | |
| 797 void errorReportingHandler(HttpRequest request) { | |
| 798 StringBuffer buffer = new StringBuffer(); | |
| 799 request.transform(new StringDecoder()).listen((data) { | |
| 800 buffer.write(data); | |
| 801 }, onDone: () { | |
| 802 String back = buffer.toString(); | |
| 803 request.response.headers.set("Access-Control-Allow-Origin", "*"); | |
| 804 | |
| 805 request.response.close(); | |
| 806 DebugLogger.info("Error from browser on : " | |
|
kustermann
2013/06/13 12:34:04
Make it DebugLogger.error.
ricow1
2013/06/13 12:39:54
Done.
| |
| 807 "${request.uri.path}, data: $back"); | |
|
kustermann
2013/06/13 12:34:04
Please catch errors on the "request.response.done/
ricow1
2013/06/13 12:39:54
Done.
| |
| 808 }, onError: (error) { print(error); }); | |
| 809 } | |
| 810 errorReportingServer.listen(errorReportingHandler, | |
| 811 onError: errorHandler); | |
| 812 return true; | |
| 813 }); | |
|
kustermann
2013/06/13 12:34:04
indentation
ricow1
2013/06/13 12:39:54
Done.
| |
| 790 }); | 814 }); |
| 791 } | 815 } |
| 792 | 816 |
| 793 void handleReport(HttpRequest request, String browserId, var testId) { | 817 void handleReport(HttpRequest request, String browserId, var testId) { |
| 794 StringBuffer buffer = new StringBuffer(); | 818 StringBuffer buffer = new StringBuffer(); |
| 795 request.transform(new StringDecoder()).listen((data) { | 819 request.transform(new StringDecoder()).listen((data) { |
| 796 buffer.write(data); | 820 buffer.write(data); |
| 797 }, onDone: () { | 821 }, onDone: () { |
| 798 String back = buffer.toString(); | 822 String back = buffer.toString(); |
| 799 request.response.close(); | 823 request.response.close(); |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 820 print("Bad browser testing server, you are not started yet. Can't " | 844 print("Bad browser testing server, you are not started yet. Can't " |
| 821 "produce driver url"); | 845 "produce driver url"); |
| 822 exit(1); | 846 exit(1); |
| 823 // This should never happen - exit immediately; | 847 // This should never happen - exit immediately; |
| 824 } | 848 } |
| 825 return "http://$local_ip:${httpServer.port}/driver/$browserId"; | 849 return "http://$local_ip:${httpServer.port}/driver/$browserId"; |
| 826 } | 850 } |
| 827 | 851 |
| 828 | 852 |
| 829 String getDriverPage(String browserId) { | 853 String getDriverPage(String browserId) { |
| 854 var errorReportingUrl = | |
| 855 "http://$local_ip:${errorReportingServer.port}/$browserId"; | |
| 830 String driverContent = """ | 856 String driverContent = """ |
| 831 <!DOCTYPE html><html> | 857 <!DOCTYPE html><html> |
| 832 <head> | 858 <head> |
| 833 <title>Driving page</title> | 859 <title>Driving page</title> |
| 834 <script type='text/javascript'> | 860 <script type='text/javascript'> |
| 835 | 861 |
| 836 function startTesting() { | 862 function startTesting() { |
| 837 var number_of_tests = 0; | 863 var number_of_tests = 0; |
| 838 var current_id; | 864 var current_id; |
| 839 var last_reported_id; | 865 var last_reported_id; |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 857 if (nextTask != undefined) alert('This is really bad'); | 883 if (nextTask != undefined) alert('This is really bad'); |
| 858 // The task is send to us as: | 884 // The task is send to us as: |
| 859 // URL#ID | 885 // URL#ID |
| 860 var split = this.responseText.split('#'); | 886 var split = this.responseText.split('#'); |
| 861 var nextTask = split[0]; | 887 var nextTask = split[0]; |
| 862 current_id = split[1]; | 888 current_id = split[1]; |
| 863 did_start = false; | 889 did_start = false; |
| 864 run(nextTask); | 890 run(nextTask); |
| 865 } | 891 } |
| 866 } else { | 892 } else { |
| 867 // We are basically in trouble - do something clever. | 893 reportError('Could not contact the server and get a new task'); |
| 868 } | 894 } |
| 869 } | 895 } |
| 870 } | 896 } |
| 871 | 897 |
| 872 function getNextTask() { | 898 function getNextTask() { |
| 873 var client = new XMLHttpRequest(); | 899 var client = new XMLHttpRequest(); |
| 874 client.onreadystatechange = newTaskHandler; | 900 client.onreadystatechange = newTaskHandler; |
| 875 client.open('GET', '$nextTestPath/$browserId'); | 901 client.open('GET', '$nextTestPath/$browserId'); |
| 876 client.send(); | 902 client.send(); |
| 877 } | 903 } |
| 878 | 904 |
| 879 function run(url) { | 905 function run(url) { |
| 880 number_of_tests++; | 906 number_of_tests++; |
| 881 document.getElementById('number').innerHTML = number_of_tests; | 907 document.getElementById('number').innerHTML = number_of_tests; |
| 882 if (use_iframe) { | 908 if (use_iframe) { |
| 883 embedded_iframe.src = url; | 909 embedded_iframe.src = url; |
| 884 } else { | 910 } else { |
| 885 if (testing_window == undefined) { | 911 if (testing_window == undefined) { |
| 886 testing_window = window.open(url); | 912 testing_window = window.open(url); |
| 887 } else { | 913 } else { |
| 888 testing_window.location = url; | 914 testing_window.location = url; |
| 889 } | 915 } |
| 890 } | 916 } |
| 891 } | 917 } |
| 892 | 918 |
| 919 window.onerror = function (message, url, lineNumber) { | |
| 920 if (url) { | |
| 921 reportError(url + ':' + lineNumber + ':' + message); | |
| 922 } else { | |
| 923 reportError(message); | |
| 924 } | |
| 925 } | |
| 926 | |
| 927 function reportError(msg) { | |
| 928 var client = new XMLHttpRequest(); | |
| 929 function handleReady() { | |
| 930 if (this.readyState == this.DONE && this.status != 200) { | |
| 931 // We could not report, pop up to notify if running interactively. | |
| 932 alert(this.status); | |
| 933 } | |
| 934 } | |
| 935 client.onreadystatechange = handleReady; | |
| 936 client.open('POST', '$errorReportingUrl?test=1'); | |
| 937 client.setRequestHeader('Content-type', | |
| 938 'application/x-www-form-urlencoded'); | |
| 939 client.send(msg); | |
| 940 } | |
| 941 | |
| 893 function reportMessage(msg) { | 942 function reportMessage(msg) { |
| 894 if (msg == 'STARTING') { | 943 if (msg == 'STARTING') { |
| 895 did_start = true; | 944 did_start = true; |
| 896 return; | 945 return; |
| 897 } | 946 } |
| 898 var client = new XMLHttpRequest(); | 947 var client = new XMLHttpRequest(); |
| 899 function handleReady() { | 948 function handleReady() { |
| 900 if (this.readyState == this.DONE) { | 949 if (this.readyState == this.DONE) { |
| 901 if (last_reported_id != current_id && did_start) { | 950 if (this.status == 200) { |
| 902 getNextTask(); | 951 if (last_reported_id != current_id && did_start) { |
| 903 last_reported_id = current_id; | 952 getNextTask(); |
| 953 last_reported_id = current_id; | |
| 954 } | |
| 955 } else { | |
| 956 reportError('Error sending result to server'); | |
| 904 } | 957 } |
| 905 } | 958 } |
| 906 } | 959 } |
| 907 client.onreadystatechange = handleReady; | 960 client.onreadystatechange = handleReady; |
| 908 // If did_start is false it means that we did actually set the url on | 961 // If did_start is false it means that we did actually set the url on |
| 909 // the testing_window, but this is a report left in the event loop or | 962 // the testing_window, but this is a report left in the event loop or |
| 910 // a callback because the page did not load yet. | 963 // a callback because the page did not load yet. |
| 911 // In both cases this is a double report from the last test. | 964 // In both cases this is a double report from the last test. |
| 912 var posting_id = did_start ? current_id : last_reported_id; | 965 var posting_id = did_start ? current_id : last_reported_id; |
| 913 client.open('POST', '$reportPath/${browserId}?id=' + posting_id); | 966 client.open('POST', '$reportPath/${browserId}?id=' + posting_id); |
| 914 client.setRequestHeader('Content-type', | 967 client.setRequestHeader('Content-type', |
| 915 'application/x-www-form-urlencoded'); | 968 'application/x-www-form-urlencoded'); |
| 916 client.send(msg); | 969 client.send(msg); |
| 917 // TODO(ricow) add error handling to somehow report the fact that | |
| 918 // we could not send back a result. | |
| 919 } | 970 } |
| 920 | 971 |
| 921 function messageHandler(e) { | 972 function messageHandler(e) { |
| 922 var msg = e.data; | 973 var msg = e.data; |
| 923 if (typeof msg != 'string') return; | 974 if (typeof msg != 'string') return; |
| 924 reportMessage(msg); | 975 reportMessage(msg); |
| 925 } | 976 } |
| 926 | 977 |
| 927 window.addEventListener('message', messageHandler, false); | 978 window.addEventListener('message', messageHandler, false); |
| 928 waitForDone = false; | 979 waitForDone = false; |
| 929 | 980 |
| 930 getNextTask(); | 981 getNextTask(); |
| 931 } | 982 } |
| 932 | 983 |
| 933 </script> | 984 </script> |
| 934 </head> | 985 </head> |
| 935 <body onload="startTesting()"> | 986 <body onload="startTesting()"> |
| 936 Dart test driver, number of tests: <div id="number"></div> | 987 Dart test driver, number of tests: <div id="number"></div> |
| 937 <iframe id="embedded_iframe"></iframe> | 988 <iframe id="embedded_iframe"></iframe> |
| 938 </body> | 989 </body> |
| 939 </html> | 990 </html> |
| 940 """; | 991 """; |
| 941 return driverContent; | 992 return driverContent; |
| 942 } | 993 } |
| 943 } | 994 } |
| OLD | NEW |