- add sources.
[platform/framework/web/crosswalk.git] / src / native_client_sdk / src / examples / tutorial / testing / example.js
1 // Copyright (c) 2013 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
13 function startCommand(testName) {
14   var testListEl = document.getElementById('tests');
15   var testEl = document.createElement('li');
16   var testRowEl = document.createElement('div');
17   var testNameEl = document.createElement('span');
18   var testResultEl = document.createElement('span');
19   testRowEl.classList.add('row');
20   testNameEl.classList.add('name');
21   testNameEl.textContent = testName;
22   testResultEl.classList.add('result');
23   testRowEl.appendChild(testNameEl);
24   testRowEl.appendChild(testResultEl);
25   testEl.appendChild(testRowEl);
26   testListEl.appendChild(testEl);
27
28   currentTestEl = testEl;
29 }
30
31 function failCommand(fileName, lineNumber, summary) {
32   var testMessageEl = document.createElement('pre');
33   testMessageEl.textContent += fileName + ':' + lineNumber + ': ' + summary;
34   currentTestEl.appendChild(testMessageEl);
35 }
36
37 function endCommand(testName, testResult) {
38   var testRowEl = currentTestEl.querySelector('.row');
39   var testResultEl = currentTestEl.querySelector('.result');
40   testRowEl.classList.add(testResult);
41   testResultEl.textContent = testResult;
42 }
43
44 function handleMessage(event) {
45   var msg = event.data;
46   var firstColon = msg.indexOf(':');
47   var cmd = msg.substr(0, firstColon);
48   var cmdFunctionName = cmd + 'Command';
49   var cmdFunction = window[cmdFunctionName];
50
51   if (typeof(cmdFunction) !== 'function') {
52     console.log('Unknown command: ' + cmd);
53     console.log('  message: ' + msg);
54     return;
55   }
56
57   var argCount = cmdFunction.length;
58
59   // Don't use split, because it will split all commas (for example any commas
60   // in the test failure summary).
61   var argList = msg.substr(firstColon + 1);
62   args = [];
63   for (var i = 0; i < argCount - 1; ++i) {
64     var arg;
65     var comma = argList.indexOf(',');
66     if (comma === -1) {
67       if (i !== argCount - 1) {
68         console.log('Bad arg count to command "' + cmd + '", expected ' +
69                     argCount);
70         console.log('  message: ' + msg);
71       } else {
72         arg = argList;
73       }
74     } else {
75       arg = argList.substr(0, comma);
76       argList = argList.substr(comma + 1);
77     }
78     args.push(arg);
79   }
80
81   // Last argument is the rest of the message.
82   args.push(argList);
83
84   cmdFunction.apply(null, args);
85 }