Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(336)

Side by Side Diff: tools/testing/dart/browser_controller.dart

Issue 16050006: Support for running tests in an iframe (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/unittest/lib/test_controller.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 String id; 45 String id;
46 46
47 /** Callback that will be executed when the browser has closed */ 47 /** Callback that will be executed when the browser has closed */
48 Function onClose; 48 Function onClose;
49 49
50 /** Print everything (stdout, stderr, usageLog) whenever we add to it */ 50 /** Print everything (stdout, stderr, usageLog) whenever we add to it */
51 bool debugPrint = false; 51 bool debugPrint = false;
52 52
53 // We use this to gracefully handle double calls to close. 53 // We use this to gracefully handle double calls to close.
54 bool underTermination = false; 54 bool underTermination = false;
55 55
56 Browser(); 56 Browser();
57 57
58 factory Browser.byName(String name) { 58 factory Browser.byName(String name) {
59 if (name == 'ff' || name == 'firefox') { 59 if (name == 'ff' || name == 'firefox') {
60 return new Firefox(); 60 return new Firefox();
61 } else if (name == 'chrome') { 61 } else if (name == 'chrome') {
62 return new Chrome(); 62 return new Chrome();
63 } else if (name == 'safari') { 63 } else if (name == 'safari') {
64 return new Safari(); 64 return new Safari();
65 } else { 65 } else {
66 throw "Non supported browser"; 66 throw "Non supported browser";
67 } 67 }
68 } 68 }
69 69
70 static const List<String> SUPPORTED_BROWSERS = 70 static const List<String> SUPPORTED_BROWSERS =
71 const ['safari', 'ff', 'firefox', 'chrome'];
72
73 static const List<String> BROWSERS_WITH_WINDOW_SUPPORT =
71 const ['safari', 'ff', 'firefox', 'chrome']; 74 const ['safari', 'ff', 'firefox', 'chrome'];
72 75
73 // TODO(kustermann): add standard support for chrome on android 76 // TODO(kustermann): add standard support for chrome on android
74 static bool supportedBrowser(String name) { 77 static bool supportedBrowser(String name) {
75 return SUPPORTED_BROWSERS.contains(name); 78 return SUPPORTED_BROWSERS.contains(name);
76 } 79 }
77 80
78 void _logEvent(String event) { 81 void _logEvent(String event) {
79 String toLog = "$this ($id) - ${new DateTime.now()}: $event \n"; 82 String toLog = "$this ($id) - ${new DateTime.now()}: $event \n";
80 if (debugPrint) print("usageLog: $toLog"); 83 if (debugPrint) print("usageLog: $toLog");
(...skipping 444 matching lines...) Expand 10 before | Expand all | Expand 10 after
525 // If we do we need to provide developers with this information. 528 // If we do we need to provide developers with this information.
526 // We don't add urls to the cache until we have run it. 529 // We don't add urls to the cache until we have run it.
527 Map<int, String> testCache = new Map<int, String>(); 530 Map<int, String> testCache = new Map<int, String>();
528 List<int> doubleReportingTests = new List<int>(); 531 List<int> doubleReportingTests = new List<int>();
529 532
530 BrowserTestingServer testingServer; 533 BrowserTestingServer testingServer;
531 534
532 BrowserTestRunner(this.local_ip, this.browserName, this.maxNumBrowsers); 535 BrowserTestRunner(this.local_ip, this.browserName, this.maxNumBrowsers);
533 536
534 Future<bool> start() { 537 Future<bool> start() {
535 testingServer = new BrowserTestingServer(local_ip); 538 // If [browserName] doesn't support opening new windows, we use new iframes
539 // instead.
540 bool useIframe =
541 !Browser.BROWSERS_WITH_WINDOW_SUPPORT.contains(browserName);
542 testingServer = new BrowserTestingServer(local_ip, useIframe);
536 return testingServer.start().then((_) { 543 return testingServer.start().then((_) {
537 testingServer.testDoneCallBack = handleResults; 544 testingServer.testDoneCallBack = handleResults;
538 testingServer.nextTestCallBack = getNextTest; 545 testingServer.nextTestCallBack = getNextTest;
539 return getBrowsers().then((browsers) { 546 return getBrowsers().then((browsers) {
540 var futures = []; 547 var futures = [];
541 for (var browser in browsers) { 548 for (var browser in browsers) {
542 var url = testingServer.getDriverUrl(browser.id); 549 var url = testingServer.getDriverUrl(browser.id);
543 var future = browser.start(url).then((success) { 550 var future = browser.start(url).then((success) {
544 if (success) { 551 if (success) {
545 browserStatus[browser.id] = new BrowserTestingStatus(browser); 552 browserStatus[browser.id] = new BrowserTestingStatus(browser);
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after
754 761
755 const String driverPath = "/driver"; 762 const String driverPath = "/driver";
756 const String nextTestPath = "/next_test"; 763 const String nextTestPath = "/next_test";
757 const String reportPath = "/report"; 764 const String reportPath = "/report";
758 const String waitSignal = "WAIT"; 765 const String waitSignal = "WAIT";
759 const String terminateSignal = "TERMINATE"; 766 const String terminateSignal = "TERMINATE";
760 767
761 var testCount = 0; 768 var testCount = 0;
762 var httpServer; 769 var httpServer;
763 bool underTermination = false; 770 bool underTermination = false;
771 bool useIframe = false;
764 772
765 Function testDoneCallBack; 773 Function testDoneCallBack;
766 Function nextTestCallBack; 774 Function nextTestCallBack;
767 775
768 BrowserTestingServer(this.local_ip); 776 BrowserTestingServer(this.local_ip, this.useIframe);
769 777
770 Future start() { 778 Future start() {
771 return HttpServer.bind(local_ip, 0).then((createdServer) { 779 return HttpServer.bind(local_ip, 0).then((createdServer) {
772 httpServer = createdServer; 780 httpServer = createdServer;
773 void handler(HttpRequest request) { 781 void handler(HttpRequest request) {
774 if (request.uri.path.startsWith(reportPath)) { 782 if (request.uri.path.startsWith(reportPath)) {
775 var browserId = request.uri.path.substring(reportPath.length + 1); 783 var browserId = request.uri.path.substring(reportPath.length + 1);
776 var testId = int.parse(request.queryParameters["id"].split("=")[1]); 784 var testId = int.parse(request.queryParameters["id"].split("=")[1]);
777 785
778 handleReport(request, browserId, testId); 786 handleReport(request, browserId, testId);
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
843 return "http://$local_ip:${httpServer.port}/driver/$browserId"; 851 return "http://$local_ip:${httpServer.port}/driver/$browserId";
844 } 852 }
845 853
846 854
847 String getDriverPage(String browserId) { 855 String getDriverPage(String browserId) {
848 String driverContent = """ 856 String driverContent = """
849 <!DOCTYPE html><html> 857 <!DOCTYPE html><html>
850 <head> 858 <head>
851 <title>Driving page</title> 859 <title>Driving page</title>
852 <script type='text/javascript'> 860 <script type='text/javascript'>
853 var number_of_tests = 0;
854 var current_id;
855 var last_reported_id;
856 var testing_window;
857 // We use this to determine if we did actually get back a start event
858 // from the test we just loaded.
859 var did_start = false;
860 861
861 function newTaskHandler() { 862 function startTesting() {
862 if (this.readyState == this.DONE) { 863 var number_of_tests = 0;
863 if (this.status == 200) { 864 var current_id;
864 if (this.responseText == '$waitSignal') { 865 var last_reported_id;
865 setTimeout(getNextTask, 500); 866 var testing_window;
866 } else if (this.responseText == '$terminateSignal') { 867 // We use this to determine if we did actually get back a start event
867 // Don't do anything, we will be killed shortly. 868 // from the test we just loaded.
869 var did_start = false;
870
871 var embedded_iframe = document.getElementById('embedded_iframe');
872 var use_iframe = ${useIframe};
873
874 function newTaskHandler() {
875 if (this.readyState == this.DONE) {
876 if (this.status == 200) {
877 if (this.responseText == '$waitSignal') {
878 setTimeout(getNextTask, 500);
879 } else if (this.responseText == '$terminateSignal') {
880 // Don't do anything, we will be killed shortly.
881 } else {
882 // TODO(ricow): Do something more clever here.
883 if (nextTask != undefined) alert('This is really bad');
884 // The task is send to us as:
885 // URL#ID
886 var split = this.responseText.split('#');
887 var nextTask = split[0];
888 current_id = split[1];
889 did_start = false;
890 run(nextTask);
891 }
868 } else { 892 } else {
869 // TODO(ricow): Do something more clever here. 893 // We are basically in trouble - do something clever.
870 if (nextTask != undefined) alert('This is really bad');
871 // The task is send to us as:
872 // URL#ID
873 var split = this.responseText.split('#');
874 var nextTask = split[0];
875 current_id = split[1];
876 did_start = false;
877 run(nextTask);
878 }
879 } else {
880 // We are basically in trouble - do something clever.
881 }
882 }
883 }
884
885 function getNextTask() {
886 var client = new XMLHttpRequest();
887 client.onreadystatechange = newTaskHandler;
888 client.open('GET', '$nextTestPath/$browserId');
889 client.send();
890 }
891
892 function run(url) {
893 number_of_tests++;
894 document.getElementById('number').innerHTML = number_of_tests;
895 if (testing_window == undefined) {
896 testing_window = window.open(url);
897 } else {
898 testing_window.location = url;
899 }
900 }
901
902 function reportMessage(msg) {
903 if (msg == 'STARTING') {
904 did_start = true;
905 return;
906 }
907 var client = new XMLHttpRequest();
908 function handleReady() {
909 if (this.readyState == this.DONE) {
910 if (last_reported_id != current_id && did_start) {
911 getNextTask();
912 last_reported_id = current_id;
913 } 894 }
914 } 895 }
915 } 896 }
916 client.onreadystatechange = handleReady; 897
917 // If did_start is false it means that we did actually set the url on 898 function getNextTask() {
918 // the testing_window, but this is a report left in the event loop or 899 var client = new XMLHttpRequest();
919 // a callback because the page did not load yet. 900 client.onreadystatechange = newTaskHandler;
920 // In both cases this is a double report from the last test. 901 client.open('GET', '$nextTestPath/$browserId');
921 var posting_id = did_start ? current_id : last_reported_id; 902 client.send();
922 client.open('POST', '$reportPath/${browserId}?id=' + posting_id); 903 }
923 client.setRequestHeader('Content-type', 904
924 'application/x-www-form-urlencoded'); 905 function run(url) {
925 client.send(msg); 906 number_of_tests++;
926 // TODO(ricow) add error handling to somehow report the fact that 907 document.getElementById('number').innerHTML = number_of_tests;
927 // we could not send back a result. 908 if (use_iframe) {
909 embedded_iframe.src = url;
910 } else {
911 if (testing_window == undefined) {
912 testing_window = window.open(url);
913 } else {
914 testing_window.location = url;
915 }
916 }
917 }
918
919 function reportMessage(msg) {
920 if (msg == 'STARTING') {
921 did_start = true;
922 return;
923 }
924 var client = new XMLHttpRequest();
925 function handleReady() {
926 if (this.readyState == this.DONE) {
927 if (last_reported_id != current_id && did_start) {
928 getNextTask();
929 last_reported_id = current_id;
930 }
931 }
932 }
933 client.onreadystatechange = handleReady;
934 // If did_start is false it means that we did actually set the url on
935 // the testing_window, but this is a report left in the event loop or
936 // a callback because the page did not load yet.
937 // In both cases this is a double report from the last test.
938 var posting_id = did_start ? current_id : last_reported_id;
939 client.open('POST', '$reportPath/${browserId}?id=' + posting_id);
940 client.setRequestHeader('Content-type',
941 'application/x-www-form-urlencoded');
942 client.send(msg);
943 // TODO(ricow) add error handling to somehow report the fact that
944 // we could not send back a result.
945 }
946
947 function messageHandler(e) {
948 var msg = e.data;
949 if (typeof msg != 'string') return;
950 reportMessage(msg);
951 }
952
953 window.addEventListener('message', messageHandler, false);
954 waitForDone = false;
955
956 getNextTask();
928 } 957 }
929 958
930 function messageHandler(e) {
931 var msg = e.data;
932 if (typeof msg != 'string') return;
933 reportMessage(msg);
934 }
935
936 window.addEventListener('message', messageHandler, false);
937 waitForDone = false;
938
939 getNextTask();
940
941 </script> 959 </script>
942 </head> 960 </head>
943 <body> 961 <body onload="startTesting()">
944 Dart test driver, number of tests: <div id="number"></div> 962 Dart test driver, number of tests: <div id="number"></div>
963 <iframe id="embedded_iframe"></iframe>
945 </body> 964 </body>
946 </html> 965 </html>
947 """; 966 """;
948 return driverContent; 967 return driverContent;
949 } 968 }
950 } 969 }
OLDNEW
« no previous file with comments | « pkg/unittest/lib/test_controller.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698