Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / tools / rtcbot / test.js
1 // 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 // Provides a Test class that exposes api to the tests.
10 // Read test.prototype to see what methods are exposed.
11 var fs = require('fs');
12 var request = require('request');
13 var BotManager = require('./botmanager.js');
14
15 function Test() {
16   this.timeout_ = setTimeout(
17       this.fail.bind(this, "Test timeout!"),
18       100000);
19 }
20
21 Test.prototype = {
22   log: function () {
23     console.log.apply(console.log, arguments);
24   },
25
26   abort: function (error) {
27     var error = new Error(error || "Test aborted");
28     console.log(error.stack);
29     process.exit(1);
30   },
31
32   assert: function (value, message) {
33     if (value !== true) {
34       this.abort(message || "Assert failed.");
35     }
36   },
37
38   fail: function () {
39     this.assert(false, "Test failed.");
40   },
41
42   done: function () {
43     clearTimeout(this.timeout_);
44     console.log("Test succeeded");
45     process.exit(0);
46   },
47
48   // Utility method to wait for multiple callbacks to be executed.
49   //  functions - array of functions to call with a callback.
50   //  doneCallback - called when all callbacks on the array have completed.
51   wait: function (functions, doneCallback) {
52     var result = new Array(functions.length);
53     var missingResult = functions.length;
54     for (var i = 0; i != functions.length; ++i)
55       functions[i](complete.bind(this, i));
56
57     function complete(index, value) {
58       missingResult--;
59       result[index] = value;
60       if (missingResult == 0)
61         doneCallback.apply(null, result);
62     }
63   },
64
65   spawnBot: function (name, botType, doneCallback) {
66     // Lazy initialization of botmanager.
67     if (!this.botManager_)
68       this.botManager_ = new BotManager();
69     this.botManager_.spawnNewBot(name, botType, doneCallback);
70   },
71
72   createStatisticsReport: function (outputFileName) {
73     return new StatisticsReport(outputFileName);
74   },
75
76   // Ask computeengineondemand to give us TURN server credentials and URIs.
77   createTurnConfig: function (onSuccess, onError) {
78     request('https://computeengineondemand.appspot.com/turn?username=1234&key=5678',
79         function (error, response, body) {
80           if (error || response.statusCode != 200) {
81             onError('TURN request failed');
82             return;
83           }
84
85           var response = JSON.parse(body);
86           var iceServer = {
87             'username': response.username,
88             'credential': response.password,
89             'urls': response.uris
90           };
91           onSuccess({ 'iceServers': [ iceServer ] });
92         }
93       );
94   },
95 }
96
97 StatisticsReport = function (outputFileName) {
98   this.output_ = [];
99   this.output_.push("Version: 1");
100   this.outputFileName_ = outputFileName;
101 }
102
103 StatisticsReport.prototype = {
104   collectStatsFromPeerConnection: function (prefix, pc) {
105     setInterval(this.addPeerConnectionStats.bind(this, prefix, pc), 100);
106   },
107
108   addPeerConnectionStats: function (prefix, pc) {
109     pc.getStats(onStatsReady.bind(this));
110
111     function onStatsReady(reports) {
112       for (index in reports) {
113         var stats = {};
114         stats[reports[index].id] = collectStats(reports[index].stats);
115
116         var data = {};
117         data[prefix] = stats;
118
119         this.output_.push({
120             type: "UpdateCounters",
121             startTime: (new Date()).getTime(),
122             data: data,
123           });
124       }
125     };
126
127     function collectStats(stats) {
128       var outputStats = {};
129       for (index in stats) {
130         var statValue = parseFloat(stats[index].stat);
131         outputStats[stats[index].name] = isNaN(statValue)?
132             stats[index].stat : statValue;
133       }
134       return outputStats;
135     };
136   },
137
138   finish: function (doneCallback) {
139     fs.exists("test/reports/", function (exists) {
140       if(exists) {
141         writeFile.bind(this)();
142       } else {
143         fs.mkdir("test/reports/", 0777, writeFile.bind(this));
144       }
145     }.bind(this));
146
147     function writeFile () {
148       fs.writeFile("test/reports/" + this.outputFileName_ + "_" +
149         (new Date()).getTime() +".json", JSON.stringify(this.output_),
150         doneCallback);
151     }
152   },
153 };
154
155 module.exports = Test;