- add sources.
[platform/framework/web/crosswalk.git] / src / native_client_sdk / src / tests / nacl_io_test / example.js
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();
9 }
10
11 var currentTestEl = null;
12 var failedTests = 0;
13 var testsFinished = false;
14
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);
29
30   currentTestEl = testEl;
31 }
32
33 function failCommand(fileName, lineNumber, summary) {
34   var testMessageEl = document.createElement('pre');
35   testMessageEl.textContent += fileName + ':' + lineNumber + ': ' + summary;
36   currentTestEl.appendChild(testMessageEl);
37   failedTests++;
38 }
39
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;
45 }
46
47 function testendCommand() {
48   testsFinished = true;
49   common.removeModule();
50
51   if (failedTests) {
52     common.updateStatus('FAILED');
53     document.getElementById('statusField').classList.add('failed');
54   } else {
55     common.updateStatus('OK');
56     document.getElementById('statusField').classList.add('ok');
57   }
58 }
59
60 function handleMessage(event) {
61   var msg = event.data;
62   var firstColon = msg.indexOf(':');
63   var cmd = firstColon !== -1 ? msg.substr(0, firstColon) : msg;
64   var cmdFunctionName = cmd + 'Command';
65   var cmdFunction = window[cmdFunctionName];
66
67   if (typeof(cmdFunction) !== 'function') {
68     console.log('Unknown command: ' + cmd);
69     console.log('  message: ' + msg);
70     return;
71   }
72
73   var argCount = cmdFunction.length;
74
75   // Don't use split, because it will split all commas (for example any commas
76   // in the test failure summary).
77   var argList = msg.substr(firstColon + 1);
78   args = [];
79   for (var i = 0; i < argCount - 1; ++i) {
80     var arg;
81     var comma = argList.indexOf(',');
82     if (comma === -1) {
83       if (i !== argCount - 1) {
84         console.log('Bad arg count to command "' + cmd + '", expected ' +
85                     argCount);
86         console.log('  message: ' + msg);
87       } else {
88         arg = argList;
89       }
90     } else {
91       arg = argList.substr(0, comma);
92       argList = argList.substr(comma + 1);
93     }
94     args.push(arg);
95   }
96
97   // Last argument is the rest of the message.
98   args.push(argList);
99
100   cmdFunction.apply(null, args);
101 }