- Adding AndroidDeviceManager to botManager.js to help in selecting devices, in case running test on Android devices.

- Select BotType using nodeJs terminal command.

- ping_pong.js test added.

R=andresp@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/19159004

git-svn-id: http://webrtc.googlecode.com/svn/trunk/webrtc@7099 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/tools/rtcbot/README b/tools/rtcbot/README
index 06fa332..927d377 100644
--- a/tools/rtcbot/README
+++ b/tools/rtcbot/README
@@ -12,12 +12,16 @@
 no access to node.js specifics other than the exposed api via a test variable.
 
 Part of the exposed api (test.spawnBot) allows a test to spawn a bot and
-access its exposed API. Details are in BotManager.js.
+access its exposed API. Details are in botmanager.js.
 
 == How to run the test ==
  $ cd trunk/webrtc/tool/rtcbot
  $ npm install express browserify ws websocket-stream dnode
- $ node test.js
+ $ node test.js <bot_type> <test_file_path>
+
+ <bot_type> — the type of the running bot. For example:
+  - chrome: chrome on host machine.
+  - android: android device. Details in "Android" Section.
 
 == Example on how to install nodejs ==
  $ cd /work/tools/
@@ -25,3 +29,11 @@
  $ export NVM_DIR=/work/tools/nvm; source $NVM_DIR/nvm.sh
  $ nvm install 0.10
  $ nvm use 0.10
+
+== Android ==
+Before running test with Android one MUST forward the device port 8080 to the
+host machine. That is easy to achieve with chrome port forwarding tools.
+ - Visit chrome://inspect/devices on the host machine.
+ - Configure and enable port forwarding 8080 -> localhost:8080
+ - Leave chrome running in the background on your Android device till
+   the test is done.
\ No newline at end of file
diff --git a/tools/rtcbot/botmanager.js b/tools/rtcbot/botmanager.js
index 667c1e8..e8f8b9b 100644
--- a/tools/rtcbot/botmanager.js
+++ b/tools/rtcbot/botmanager.js
@@ -28,10 +28,24 @@
   this.pendingConnections_ = [];
 }
 
+BotManager.BotTypes = {
+  CHROME : 'chrome',
+};
+
 BotManager.prototype = {
-  spawnNewBot: function (name, callback) {
+  createBot_: function (name, botType, callback) {
+    switch(botType) {
+      case BotManager.BotTypes.CHROME:
+        return new BrowserBot(name, callback);
+      default:
+        console.log('Error: Type ' + botType + ' not supported by rtc-Bot!');
+        process.exit(1);
+    }
+  },
+
+  spawnNewBot: function (name, botType, callback) {
     this.startWebSocketServer_();
-    var bot = new BrowserBot(name, callback);
+    var bot = this.createBot_(name, botType, callback);
     this.bots_.push(bot);
     this.pendingConnections_.push(bot.onBotConnected.bind(bot));
   },
@@ -115,4 +129,49 @@
   __proto__: Bot.prototype
 }
 
+AndroidDeviceManager = function () {
+  this.connectedDevices_ = [];
+}
+
+AndroidDeviceManager.prototype = {
+  getNewDevice: function (callback) {
+    this.listDevices_(function (devices) {
+      for (var i = 0; i < devices.length; i++) {
+        if (!this.connectedDevices_[devices[i]]) {
+          this.connectedDevices_[devices[i]] = devices[i];
+          callback(this.connectedDevices_[devices[i]]);
+          return;
+        }
+      }
+      if (devices.length == 0) {
+        console.log('Error: No connected devices!');
+      } else {
+        console.log('Error: There is no enough connected devices.');
+      }
+      process.exit(1);
+    }.bind(this));
+  },
+
+  listDevices_: function (callback) {
+    child.exec('adb devices' , function (error, stdout, stderr) {
+      var devices = [];
+      if (error || stderr) {
+        console.log('' + (error || stderr));
+      }
+      if (stdout) {
+        // The first line is "List of devices attached"
+        // and the following lines:
+        // <serial number>  <device/emulator>
+        var tempList = ('' + stdout).split("\n").slice(1);
+        for (var i = 0; i < tempList.length; i++) {
+          if (tempList[i] == "") {
+            continue;
+          }
+          devices.push(tempList[i].split("\t")[0]);
+        }
+      }
+      callback(devices);
+    });
+  },
+}
 module.exports = BotManager;
diff --git a/tools/rtcbot/test.js b/tools/rtcbot/test.js
index 83bb39f..1768c09 100644
--- a/tools/rtcbot/test.js
+++ b/tools/rtcbot/test.js
@@ -16,11 +16,12 @@
 var vm = require('vm');
 var BotManager = require('./botmanager.js');
 
-function Test() {
+function Test(botType) {
   // Make the test fail if not completed in 3 seconds.
   this.timeout_ = setTimeout(
       this.fail.bind(this, "Test timeout!"),
-      3000);
+      5000);
+  this.botType_ = botType;
 }
 
 Test.prototype = {
@@ -71,14 +72,14 @@
     // Lazy initialization of botmanager.
     if (!this.botManager_)
       this.botManager_ = new BotManager();
-    this.botManager_.spawnNewBot(name, doneCallback);
+    this.botManager_.spawnNewBot(name, this.botType_, doneCallback);
   },
 }
 
 function runTest(testfile) {
   console.log("Running test: " + testfile);
   var script = vm.createScript(fs.readFileSync(testfile), testfile);
-  script.runInNewContext({ test: new Test() });
+  script.runInNewContext({ test: new Test(process.argv[2]) });
 }
 
 runTest("./test/simple_offer_answer.js");
diff --git a/tools/rtcbot/test/ping_pong.js b/tools/rtcbot/test/ping_pong.js
new file mode 100644
index 0000000..e519738
--- /dev/null
+++ b/tools/rtcbot/test/ping_pong.js
@@ -0,0 +1,19 @@
+// Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
+//
+// Use of this source code is governed by a BSD-style license
+// that can be found in the LICENSE file in the root of the source
+// tree. An additional intellectual property rights grant can be found
+// in the file PATENTS.  All contributing project authors may
+// be found in the AUTHORS file in the root of the source tree.
+//
+function testPingPong(bot) {
+  test.log('bot:alice > Sending Ping to bot');
+  bot.ping(gotAnswer);
+
+  function gotAnswer(answer) {
+    test.log('bot > ' + answer);
+    test.done();
+  }
+}
+
+test.spawnBot("alice", testPingPong);