Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / native_client_sdk / src / tests / nacl_io_socket_test / example.js
blob2ffb98d6f771d523595833a2a414c8af7e11f575
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 // Called by the common.js module.
5 function moduleDidLoad() {
6 // The module is not hidden by default so we can easily see if the plugin
7 // failed to load.
8 common.hideModule();
11 var currentTestEl = null;
12 var failedTests = 0;
13 var testsFinished = false;
15 function startCommand(testName) {
16 var testListEl = document.getElementById('tests');
17 var testEl = document.createElement('li');
18 var testRowEl = document.createElement('div');
19 var testNameEl = document.createElement('span');
20 var testResultEl = document.createElement('span');
21 testRowEl.classList.add('row');
22 testNameEl.classList.add('name');
23 testNameEl.textContent = testName;
24 testResultEl.classList.add('result');
25 testRowEl.appendChild(testNameEl);
26 testRowEl.appendChild(testResultEl);
27 testEl.appendChild(testRowEl);
28 testListEl.appendChild(testEl);
30 currentTestEl = testEl;
33 function failCommand(fileName, lineNumber, summary) {
34 var testMessageEl = document.createElement('pre');
35 testMessageEl.textContent += fileName + ':' + lineNumber + ': ' + summary;
36 currentTestEl.appendChild(testMessageEl);
37 failedTests++;
40 function endCommand(testName, testResult) {
41 var testRowEl = currentTestEl.querySelector('.row');
42 var testResultEl = currentTestEl.querySelector('.result');
43 testRowEl.classList.add(testResult);
44 testResultEl.textContent = testResult;
47 function testendCommand(exitCode) {
48 testsFinished = true;
50 if (failedTests) {
51 common.updateStatus('FAILED');
52 document.getElementById('statusField').classList.add('failed');
53 } else {
54 common.updateStatus('OK');
55 document.getElementById('statusField').classList.add('ok');
59 function handleMessage(event) {
60 var msg = event.data;
61 var firstColon = msg.indexOf(':');
62 var cmd = firstColon !== -1 ? msg.substr(0, firstColon) : msg;
63 var cmdFunctionName = cmd + 'Command';
64 var cmdFunction = window[cmdFunctionName];
66 if (cmd == 'testend')
67 event.srcElement.postMessage({'testend' : ''});
69 if (typeof(cmdFunction) !== 'function') {
70 console.log('Unknown command: ' + cmd);
71 console.log(' message: ' + msg);
72 return;
75 var argCount = cmdFunction.length;
77 // Don't use split, because it will split all commas (for example any commas
78 // in the test failure summary).
79 var argList = msg.substr(firstColon + 1);
80 args = [];
81 for (var i = 0; i < argCount - 1; ++i) {
82 var arg;
83 var comma = argList.indexOf(',');
84 if (comma === -1) {
85 if (i !== argCount - 1) {
86 console.log('Bad arg count to command "' + cmd + '", expected ' +
87 argCount);
88 console.log(' message: ' + msg);
89 } else {
90 arg = argList;
92 } else {
93 arg = argList.substr(0, comma);
94 argList = argList.substr(comma + 1);
96 args.push(arg);
99 // Last argument is the rest of the message.
100 args.push(argList);
102 cmdFunction.apply(null, args);