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.
5function 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
11var currentTestEl = null;
12
13function 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
31function failCommand(fileName, lineNumber, summary) {
32  var testMessageEl = document.createElement('pre');
33  testMessageEl.textContent += fileName + ':' + lineNumber + ': ' + summary;
34  currentTestEl.appendChild(testMessageEl);
35}
36
37function 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
44function handleMessage(event) {
45  var msg = event.data;
46  var firstColon = msg.indexOf(':');
47  var cmd = msg.substr(0, firstColon);
48
49  if (cmd == 'testend') {
50    event.srcElement.postMessage({'testend' : ''});
51    return;
52  }
53
54  var cmdFunctionName = cmd + 'Command';
55  var cmdFunction = window[cmdFunctionName];
56  if (typeof(cmdFunction) !== 'function') {
57    console.log('Unknown command: ' + cmd);
58    console.log('  message: ' + msg);
59    return;
60  }
61
62  var argCount = cmdFunction.length;
63
64  // Don't use split, because it will split all commas (for example any commas
65  // in the test failure summary).
66  var argList = msg.substr(firstColon + 1);
67  args = [];
68  for (var i = 0; i < argCount - 1; ++i) {
69    var arg;
70    var comma = argList.indexOf(',');
71    if (comma === -1) {
72      if (i !== argCount - 1) {
73        console.log('Bad arg count to command "' + cmd + '", expected ' +
74                    argCount);
75        console.log('  message: ' + msg);
76      } else {
77        arg = argList;
78      }
79    } else {
80      arg = argList.substr(0, comma);
81      argList = argList.substr(comma + 1);
82    }
83    args.push(arg);
84  }
85
86  // Last argument is the rest of the message.
87  args.push(argList);
88
89  cmdFunction.apply(null, args);
90}
91