OLD | NEW |
| (Empty) |
1 #import('dart:dom'); | |
2 #import('dart:json'); | |
3 #import('Suites.dart'); | |
4 | |
5 main() { | |
6 new Dromaeo().run(); | |
7 } | |
8 | |
9 class SuiteController { | |
10 final SuiteDescription _suiteDescription; | |
11 final HTMLIFrameElement _suiteIframe; | |
12 | |
13 HTMLDivElement _element; | |
14 double _meanProduct; | |
15 int _nTests; | |
16 | |
17 SuiteController(this._suiteDescription, this._suiteIframe) | |
18 : _meanProduct = 1.0, | |
19 _nTests = 0 { | |
20 _make(); | |
21 _init(); | |
22 } | |
23 | |
24 start() { | |
25 _suiteIframe.contentWindow.dynamic.postMessage('start', '*'); | |
26 } | |
27 | |
28 update(String testName, num mean, num error, double percent) { | |
29 _meanProduct *= mean; | |
30 _nTests++; | |
31 | |
32 final meanAsString = mean.toStringAsFixed(2); | |
33 final errorAsString = error.toStringAsFixed(2); | |
34 final HTMLElement progressDisplay = _element.nextSibling.nextSibling; | |
35 progressDisplay.innerHTML += | |
36 '<li><b>${testName}:</b>' + | |
37 '${meanAsString}<small> runs/s ±${errorAsString}%<small></li>'; | |
38 _updateTestPos(percent); | |
39 } | |
40 | |
41 _make() { | |
42 _element = _createDiv('test'); | |
43 // TODO(antonm): add an onclick functionality. | |
44 _updateTestPos(); | |
45 } | |
46 | |
47 _updateTestPos([double percent = 1.0]) { | |
48 String suiteName = _suiteDescription.name; | |
49 final done = percent >= 100.0; | |
50 String info = ''; | |
51 if (done) { | |
52 final parent = _element.parentNode; | |
53 parent.setAttribute('class', parent.getAttribute('class') + ' done'); | |
54 final mean = Math.pow(_meanProduct, 1.0 / _nTests).toStringAsFixed(2); | |
55 info = '<span>${mean} runs/s</span>'; | |
56 } | |
57 _element.innerHTML = | |
58 '<b>${suiteName}:</b>' + | |
59 '<div class="bar"><div style="width:${percent}%;">${info}</div></div>'; | |
60 } | |
61 | |
62 _init() { | |
63 final div = _createDiv('result-item'); | |
64 div.appendChild(_element); | |
65 final description = _suiteDescription.description; | |
66 final originUrl = _suiteDescription.origin.url; | |
67 final testUrl = 'tests/' + _suiteDescription.file; | |
68 div.innerHTML += | |
69 '<p>${description}<br/><a href="${originUrl}">Origin</a>' + | |
70 ', <a href="${testUrl}">Source</a>' + | |
71 '<ol class="results"></ol>'; | |
72 // Reread the element, as the previous wrapper get disconnected thanks | |
73 // to .innerHTML update above. | |
74 _element = div.firstChild; | |
75 | |
76 document.getElementById('main').appendChild(div); | |
77 } | |
78 | |
79 HTMLDivElement _createDiv(String clazz) { | |
80 final div = document.createElement('div'); | |
81 div.setAttribute('class', clazz); | |
82 return div; | |
83 } | |
84 } | |
85 | |
86 class Dromaeo { | |
87 final List<SuiteController> _suiteControllers; | |
88 Function _handler; | |
89 | |
90 Dromaeo() | |
91 : _suiteControllers = new List<SuiteController>() | |
92 { | |
93 _handler = _createHandler(); | |
94 window.addEventListener( | |
95 'message', | |
96 (MessageEvent event) { | |
97 try { | |
98 final response = JSON.parse(event.data); | |
99 _handler = _handler(response['command'], response['data']); | |
100 } catch (final e, final stacktrace) { | |
101 window.alert('Exception: ${e}: ${stacktrace}'); | |
102 } | |
103 }, | |
104 false | |
105 ); | |
106 } | |
107 | |
108 run() { | |
109 // TODO(antonm): create Re-run tests href. | |
110 final HTMLElement suiteNameElement = _byId('overview').firstChild; | |
111 suiteNameElement.innerHTML = 'DOM Core Tests'; | |
112 _css(_byId('tests'), 'display', 'none'); | |
113 for (SuiteDescription suite in Suites.SUITE_DESCRIPTIONS) { | |
114 final iframe = document.createElement('iframe'); | |
115 _css(iframe, 'height', '1px'); | |
116 _css(iframe, 'width', '1px'); | |
117 iframe.src = 'tests/' + suite.file; | |
118 document.body.appendChild(iframe); | |
119 | |
120 _suiteControllers.add(new SuiteController(suite, iframe)); | |
121 } | |
122 } | |
123 | |
124 static final double _SECS_PER_TEST = 5.0; | |
125 | |
126 Function _createHandler() { | |
127 int suitesLoaded = 0; | |
128 int totalTests = 0; | |
129 int currentSuite; | |
130 double totalTimeSecs, estimatedTimeSecs; | |
131 | |
132 // TODO(jat): Remove void type below. Bug 5269037. | |
133 void _updateTime() { | |
134 final mins = (estimatedTimeSecs / 60).floor().toInt(); | |
135 final secs = (estimatedTimeSecs - mins * 60).round().toInt(); | |
136 final secsAsString = (secs < 10 ? '0' : '') + secs; | |
137 _byId('left').innerHTML = '${mins}:${secsAsString}'; | |
138 | |
139 final elapsed = totalTimeSecs - estimatedTimeSecs; | |
140 final percent = (100 * elapsed / totalTimeSecs).toStringAsFixed(2); | |
141 _css(_byId('timebar'), 'width', '${percent}%'); | |
142 } | |
143 | |
144 Function loading, running, done; | |
145 | |
146 loading = (String command, var data) { | |
147 assert(command == 'inited'); | |
148 suitesLoaded++; | |
149 totalTests += data['nTests']; | |
150 if (suitesLoaded == _suitesTotal) { | |
151 totalTimeSecs = estimatedTimeSecs = _SECS_PER_TEST * totalTests; | |
152 _updateTime(); | |
153 currentSuite = 0; | |
154 _suiteControllers[currentSuite].start(); | |
155 return running; | |
156 } | |
157 | |
158 return loading; | |
159 }; | |
160 | |
161 running = (String command, var data) { | |
162 switch (command) { | |
163 case 'result': | |
164 final testName = data['testName']; | |
165 final mean = data['mean']; | |
166 final error = data['error']; | |
167 final percent = data['percent']; | |
168 _suiteControllers[currentSuite].update(testName, mean, error, percent)
; | |
169 estimatedTimeSecs -= _SECS_PER_TEST; | |
170 _updateTime(); | |
171 return running; | |
172 | |
173 case 'over': | |
174 currentSuite++; | |
175 if (currentSuite < _suitesTotal) { | |
176 _suiteControllers[currentSuite].start(); | |
177 return running; | |
178 } | |
179 document.body.setAttribute('class', 'alldone'); | |
180 return done; | |
181 | |
182 default: | |
183 throw 'Unknown command ${command} [${data}]'; | |
184 } | |
185 }; | |
186 | |
187 done = (String command, var data) { | |
188 }; | |
189 | |
190 return loading; | |
191 } | |
192 | |
193 _css(Element element, String property, String value) { | |
194 // TODO(antonm): remove the last argument when CallWithDefaultValue | |
195 // is implemented. | |
196 element.style.setProperty(property, value, ''); | |
197 } | |
198 | |
199 HTMLElement _byId(String id) { | |
200 return document.getElementById(id); | |
201 } | |
202 | |
203 int get _suitesTotal() { | |
204 return _suiteControllers.length; | |
205 } | |
206 } | |
OLD | NEW |