NOTE: This code review based on the running issue:
https://webrtc-codereview.appspot.com/24939004/

R=andresp@webrtc.org

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

git-svn-id: http://webrtc.googlecode.com/svn/trunk/webrtc@7499 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/tools/rtcbot/bot/browser/bot.js b/tools/rtcbot/bot/browser/bot.js
index 99ff84d..a5785df 100644
--- a/tools/rtcbot/bot/browser/bot.js
+++ b/tools/rtcbot/bot/browser/bot.js
@@ -113,9 +113,28 @@
   return null;
 };
 
+function downloadFile(path, onSuccess, onError) {
+  var xhr = new XMLHttpRequest();
+  function onResult() {
+    if (xhr.readyState != 4)
+      return;
+
+    if (xhr.status != 200) {
+      onError("Download request failed!");
+      return;
+    }
+    onSuccess(xhr.responseText);
+  }
+
+  xhr.onreadystatechange = onResult;
+  xhr.open('GET', path, true);
+  xhr.send();
+};
+
 connectToServer({
   ping: ping,
   getUserMedia: getUserMedia,
   createPeerConnection: createPeerConnection,
   showStream: showStream,
+  downloadFile: downloadFile,
 });
diff --git a/tools/rtcbot/test/oneWayVideoStreamingWithDownloadingFile.js b/tools/rtcbot/test/oneWayVideoStreamingWithDownloadingFile.js
new file mode 100644
index 0000000..8ebad87
--- /dev/null
+++ b/tools/rtcbot/test/oneWayVideoStreamingWithDownloadingFile.js
@@ -0,0 +1,127 @@
+// 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.
+//
+// A unidirectional video and audio flowing test from bot 1 to bot 2,
+// and download a file from a server after 2 seconds of establishing
+// the call.
+//
+// The test succeeds after collecting stats for 10 seconds from both bots
+// and then write these stats to a file.
+//
+// Note: the source of the video and audio stream is getUserMedia().
+//
+// // TODO(houssainy)
+// This test is still not working properly because we still missing the
+// Cross-Origin Resource Sharing (CORS) to be able to download the file
+// from non localhost domain.
+//
+function testOneWayVideoWithDownloading(test, bot1, bot2) {
+  var report = test.createStatisticsReport("testOneWayVideoWithDownloading");
+
+  test.wait([
+      createPeerConnection.bind(bot1),
+      createPeerConnection.bind(bot2) ],
+    onPeerConnectionCreated);
+
+  function createPeerConnection(done) {
+    test.createTurnConfig(onTurnConfig.bind(this), test.fail);
+
+    function onTurnConfig(config) {
+      this.createPeerConnection(config, done, test.fail);
+    };
+  }
+
+  function onPeerConnectionCreated(pc1, pc2) {
+    test.log("RTC Peers created.");
+    pc1.addEventListener('addstream', test.fail);
+    pc2.addEventListener('addstream', onAddStream);
+    pc1.addEventListener('icecandidate', onIceCandidate.bind(pc2));
+    pc2.addEventListener('icecandidate', onIceCandidate.bind(pc1));
+
+    bot1.getUserMedia({video:true, audio:true}, onUserMediaSuccess, test.fail);
+
+    function onUserMediaSuccess(stream) {
+      test.log("User has granted access to local media.");
+      pc1.addStream(stream);
+      bot1.showStream(stream.id, true, true);
+
+      createOfferAndAnswer(pc1, pc2);
+    }
+  }
+
+  function onAddStream(event) {
+    test.log("On Add stream.");
+    bot2.showStream(event.stream.id, true, false);
+  }
+
+  function onIceCandidate(event) {
+    if(event.candidate) {
+      test.log(event.candidate.candidate);
+      this.addIceCandidate(event.candidate,
+         onAddIceCandidateSuccess, test.fail);
+    }
+
+    function onAddIceCandidateSuccess() {
+      test.log("Candidate added successfully");
+    }
+  }
+
+  function createOfferAndAnswer(pc1, pc2) {
+    test.log("Creating offer.");
+    pc1.createOffer(gotOffer, test.fail);
+
+    function gotOffer(offer) {
+      test.log("Got offer");
+      pc1.setLocalDescription(offer, onSetSessionDescriptionSuccess, test.fail);
+      pc2.setRemoteDescription(offer, onSetSessionDescriptionSuccess,
+          test.fail);
+      test.log("Creating answer");
+      pc2.createAnswer(gotAnswer, test.fail);
+    }
+
+    function gotAnswer(answer) {
+      test.log("Got answer");
+      pc2.setLocalDescription(answer, onSetSessionDescriptionSuccess,
+          test.fail);
+      pc1.setRemoteDescription(answer, onSetSessionDescriptionSuccess,
+          test.fail);
+      collectStats();
+
+      setTimeout(function() {
+        downloadFile(bot1, "bot1");
+        downloadFile(bot2, "bot2");
+      }, 2000);
+    }
+
+    function onSetSessionDescriptionSuccess() {
+      test.log("Set session description success.");
+    }
+
+    function collectStats() {
+      report.collectStatsFromPeerConnection("bot1", pc1);
+      report.collectStatsFromPeerConnection("bot2", pc2);
+
+      setTimeout(function() {
+        report.finish(test.done);
+        }, 10000);
+    }
+
+    function downloadFile(bot, name) {
+      bot.downloadFile("http://test-needs-a-file-server",
+          onDownloadSuccess.bind(null, name), test.fail);
+
+      function onDownloadSuccess(name, data) {
+        test.log( name + " downloaded " +
+            Math.round(data.length/(1024*1024)) + "MB.");
+      }
+    }
+  }
+}
+
+registerBotTest('testOneWayVideoWithDownloading/chrome-chrome',
+                testOneWayVideoWithDownloading, ['chrome', 'chrome']);