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