blob: f731773979856c209f076c9468527b07bc4ec78b [file] [log] [blame]
andresp@webrtc.org468516c2014-09-02 10:52:54 +00001// Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
2//
3// Use of this source code is governed by a BSD-style license
4// that can be found in the LICENSE file in the root of the source
5// tree. An additional intellectual property rights grant can be found
6// in the file PATENTS. All contributing project authors may
7// be found in the AUTHORS file in the root of the source tree.
8//
9// This script loads the test file in the virtual machine and runs it in a
10// context that only exposes a test variable with methods for testing and to
11// spawn bots.
12//
13// Note: an important part of this script is to keep nodejs-isms away from test
14// code and isolate it from implementation details.
15var fs = require('fs');
16var vm = require('vm');
17var BotManager = require('./botmanager.js');
18
houssainy@google.comc77e4d62014-09-08 10:36:11 +000019function Test(botType) {
houssainy@google.com07ca9492014-09-22 13:52:39 +000020 // TODO(houssainy) set the time out.
andresp@webrtc.org468516c2014-09-02 10:52:54 +000021 this.timeout_ = setTimeout(
22 this.fail.bind(this, "Test timeout!"),
houssainy@google.com07ca9492014-09-22 13:52:39 +000023 10000);
houssainy@google.comc77e4d62014-09-08 10:36:11 +000024 this.botType_ = botType;
andresp@webrtc.org468516c2014-09-02 10:52:54 +000025}
26
27Test.prototype = {
28 log: function () {
29 console.log.apply(console.log, arguments);
30 },
31
32 abort: function (error) {
houssainy@google.come0761d02014-09-10 14:35:35 +000033 var error = new Error(error || "Test aborted");
andresp@webrtc.org468516c2014-09-02 10:52:54 +000034 console.log(error.stack);
35 process.exit(1);
36 },
37
38 assert: function (value, message) {
39 if (value !== true) {
40 this.abort(message || "Assert failed.");
41 }
42 },
43
44 fail: function () {
45 this.assert(false, "Test failed.");
46 },
47
48 done: function () {
49 clearTimeout(this.timeout_);
50 console.log("Test succeeded");
51 process.exit(0);
52 },
53
54 // Utility method to wait for multiple callbacks to be executed.
55 // functions - array of functions to call with a callback.
56 // doneCallback - called when all callbacks on the array have completed.
57 wait: function (functions, doneCallback) {
58 var result = new Array(functions.length);
59 var missingResult = functions.length;
60 for (var i = 0; i != functions.length; ++i)
61 functions[i](complete.bind(this, i));
62
63 function complete(index, value) {
64 missingResult--;
65 result[index] = value;
66 if (missingResult == 0)
67 doneCallback.apply(null, result);
68 }
69 },
70
71 spawnBot: function (name, doneCallback) {
72 // Lazy initialization of botmanager.
73 if (!this.botManager_)
74 this.botManager_ = new BotManager();
houssainy@google.comc77e4d62014-09-08 10:36:11 +000075 this.botManager_.spawnNewBot(name, this.botType_, doneCallback);
andresp@webrtc.org468516c2014-09-02 10:52:54 +000076 },
77}
78
79function runTest(testfile) {
80 console.log("Running test: " + testfile);
81 var script = vm.createScript(fs.readFileSync(testfile), testfile);
houssainy@google.comc77e4d62014-09-08 10:36:11 +000082 script.runInNewContext({ test: new Test(process.argv[2]) });
andresp@webrtc.org468516c2014-09-02 10:52:54 +000083}
84
houssainy@google.com07ca9492014-09-22 13:52:39 +000085runTest("./test/webrtc_video_streaming.js");