Home | History | Annotate | Download | only in media
      1 // Copyright 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 
      5 #include "base/command_line.h"
      6 #include "base/files/file_enumerator.h"
      7 #include "base/path_service.h"
      8 #include "base/process/launch.h"
      9 #include "base/rand_util.h"
     10 #include "base/strings/stringprintf.h"
     11 #include "chrome/browser/browser_process.h"
     12 #include "chrome/browser/media/webrtc_browsertest_base.h"
     13 #include "chrome/browser/media/webrtc_browsertest_common.h"
     14 #include "chrome/browser/ui/browser.h"
     15 #include "chrome/browser/ui/browser_tabstrip.h"
     16 #include "chrome/browser/ui/tabs/tab_strip_model.h"
     17 #include "chrome/common/chrome_switches.h"
     18 #include "chrome/test/base/ui_test_utils.h"
     19 #include "content/public/test/browser_test_utils.h"
     20 #include "media/base/media_switches.h"
     21 #include "net/test/python_utils.h"
     22 #include "ui/gl/gl_switches.h"
     23 
     24 // You need this solution to run this test. The solution will download appengine
     25 // and the apprtc code for you.
     26 const char kAdviseOnGclientSolution[] =
     27     "You need to add this solution to your .gclient to run this test:\n"
     28     "{\n"
     29     "  \"name\"        : \"webrtc.DEPS\",\n"
     30     "  \"url\"         : \"svn://svn.chromium.org/chrome/trunk/deps/"
     31     "third_party/webrtc/webrtc.DEPS\",\n"
     32     "}";
     33 const char kTitlePageOfAppEngineAdminPage[] = "Instances";
     34 
     35 
     36 // WebRTC-AppRTC integration test. Requires a real webcam and microphone
     37 // on the running system. This test is not meant to run in the main browser
     38 // test suite since normal tester machines do not have webcams. Chrome will use
     39 // its fake camera for both tests, but Firefox will use the real webcam in the
     40 // Firefox interop test. Thus, this test must on a machine with a real webcam.
     41 //
     42 // This test will bring up a AppRTC instance on localhost and verify that the
     43 // call gets up when connecting to the same room from two tabs in a browser.
     44 class WebRtcApprtcBrowserTest : public WebRtcTestBase {
     45  public:
     46   WebRtcApprtcBrowserTest()
     47       : dev_appserver_(base::kNullProcessHandle),
     48         firefox_(base::kNullProcessHandle) {
     49   }
     50 
     51   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
     52     EXPECT_FALSE(command_line->HasSwitch(switches::kUseFakeUIForMediaStream));
     53 
     54     // The video playback will not work without a GPU, so force its use here.
     55     command_line->AppendSwitch(switches::kUseGpuInTests);
     56     CommandLine::ForCurrentProcess()->AppendSwitch(
     57         switches::kUseFakeDeviceForMediaStream);
     58   }
     59 
     60   virtual void TearDown() OVERRIDE {
     61     // Kill any processes we may have brought up.
     62     LOG(INFO) << "Entering TearDown";
     63     if (dev_appserver_ != base::kNullProcessHandle)
     64       base::KillProcess(dev_appserver_, 0, false);
     65     // TODO(phoglund): Find some way to shut down Firefox cleanly on Windows.
     66     if (firefox_ != base::kNullProcessHandle)
     67       base::KillProcess(firefox_, 0, false);
     68     LOG(INFO) << "Exiting TearDown";
     69   }
     70 
     71  protected:
     72   bool LaunchApprtcInstanceOnLocalhost() {
     73     base::FilePath appengine_dev_appserver =
     74         GetSourceDir().Append(
     75             FILE_PATH_LITERAL("../google_appengine/dev_appserver.py"));
     76     if (!base::PathExists(appengine_dev_appserver)) {
     77       LOG(ERROR) << "Missing appengine sdk at " <<
     78           appengine_dev_appserver.value() << ". " << kAdviseOnGclientSolution;
     79       return false;
     80     }
     81 
     82     base::FilePath apprtc_dir =
     83         GetSourceDir().Append(FILE_PATH_LITERAL("out/apprtc"));
     84     if (!base::PathExists(apprtc_dir)) {
     85       LOG(ERROR) << "Missing AppRTC code at " <<
     86           apprtc_dir.value() << ". " << kAdviseOnGclientSolution;
     87       return false;
     88     }
     89 
     90     CommandLine command_line(CommandLine::NO_PROGRAM);
     91     EXPECT_TRUE(GetPythonCommand(&command_line));
     92 
     93     command_line.AppendArgPath(appengine_dev_appserver);
     94     command_line.AppendArgPath(apprtc_dir);
     95     command_line.AppendArg("--port=9999");
     96     command_line.AppendArg("--admin_port=9998");
     97     command_line.AppendArg("--skip_sdk_update_check");
     98 
     99     VLOG(1) << "Running " << command_line.GetCommandLineString();
    100     return base::LaunchProcess(command_line, base::LaunchOptions(),
    101                                &dev_appserver_);
    102   }
    103 
    104   bool LocalApprtcInstanceIsUp() {
    105     // Load the admin page and see if we manage to load it right.
    106     ui_test_utils::NavigateToURL(browser(), GURL("localhost:9998"));
    107     content::WebContents* tab_contents =
    108         browser()->tab_strip_model()->GetActiveWebContents();
    109     std::string javascript =
    110         "window.domAutomationController.send(document.title)";
    111     std::string result;
    112     if (!content::ExecuteScriptAndExtractString(tab_contents, javascript,
    113                                                 &result))
    114       return false;
    115 
    116     return result == kTitlePageOfAppEngineAdminPage;
    117   }
    118 
    119   bool WaitForCallToComeUp(content::WebContents* tab_contents) {
    120     // Apprtc will set remoteVideo.style.opacity to 1 when the call comes up.
    121     std::string javascript =
    122         "window.domAutomationController.send(remoteVideo.style.opacity)";
    123     return test::PollingWaitUntil(javascript, "1", tab_contents);
    124   }
    125 
    126   bool WaitForCallToHangUp(content::WebContents* tab_contents) {
    127     // Apprtc will set remoteVideo.style.opacity to 1 when the call comes up.
    128     std::string javascript =
    129         "window.domAutomationController.send(remoteVideo.style.opacity)";
    130     return test::PollingWaitUntil(javascript, "0", tab_contents);
    131   }
    132 
    133   bool EvalInJavascriptFile(content::WebContents* tab_contents,
    134                             const base::FilePath& path) {
    135     std::string javascript;
    136     if (!ReadFileToString(path, &javascript)) {
    137       LOG(ERROR) << "Missing javascript code at " << path.value() << ".";
    138       return false;
    139     }
    140 
    141     if (!content::ExecuteScript(tab_contents, javascript)) {
    142       LOG(ERROR) << "Failed to execute the following javascript: " <<
    143           javascript;
    144       return false;
    145     }
    146     return true;
    147   }
    148 
    149   bool DetectRemoteVideoPlaying(content::WebContents* tab_contents) {
    150     if (!EvalInJavascriptFile(tab_contents, GetSourceDir().Append(
    151         FILE_PATH_LITERAL("chrome/test/data/webrtc/test_functions.js"))))
    152       return false;
    153     if (!EvalInJavascriptFile(tab_contents, GetSourceDir().Append(
    154         FILE_PATH_LITERAL("chrome/test/data/webrtc/video_detector.js"))))
    155       return false;
    156 
    157     // The remote video tag is called remoteVideo in the AppRTC code.
    158     StartDetectingVideo(tab_contents, "remoteVideo");
    159     WaitForVideoToPlay(tab_contents);
    160     return true;
    161   }
    162 
    163   bool HangUpApprtcCall(content::WebContents* tab_contents) {
    164     // This is the same as clicking the Hangup button in the AppRTC call.
    165     return content::ExecuteScript(tab_contents, "onHangup()");
    166   }
    167 
    168   base::FilePath GetSourceDir() {
    169     base::FilePath source_dir;
    170     PathService::Get(base::DIR_SOURCE_ROOT, &source_dir);
    171     return source_dir;
    172   }
    173 
    174   bool LaunchFirefoxWithUrl(const GURL& url) {
    175     base::FilePath firefox_binary =
    176         GetSourceDir().Append(
    177             FILE_PATH_LITERAL("../firefox-nightly/firefox/firefox"));
    178     if (!base::PathExists(firefox_binary)) {
    179       LOG(ERROR) << "Missing firefox binary at " <<
    180           firefox_binary.value() << ". " << kAdviseOnGclientSolution;
    181       return false;
    182     }
    183     base::FilePath firefox_launcher =
    184         GetSourceDir().Append(
    185             FILE_PATH_LITERAL("../webrtc.DEPS/run_firefox_webrtc.py"));
    186     if (!base::PathExists(firefox_launcher)) {
    187       LOG(ERROR) << "Missing firefox launcher at " <<
    188           firefox_launcher.value() << ". " << kAdviseOnGclientSolution;
    189       return false;
    190     }
    191 
    192     CommandLine command_line(firefox_launcher);
    193     command_line.AppendSwitchPath("--binary", firefox_binary);
    194     command_line.AppendSwitchASCII("--webpage", url.spec());
    195 
    196     VLOG(1) << "Running " << command_line.GetCommandLineString();
    197     return base::LaunchProcess(command_line, base::LaunchOptions(),
    198                                &firefox_);
    199   }
    200 
    201   bool HasWebcamOnSystem() {
    202 #if defined(OS_LINUX)
    203     // Implementation note: normally we would be able to figure this out with
    204     // MediaStreamTrack.getSources, but we can't ask Chrome since it runs in
    205     // fake device mode where it will not enumerate webcams on the system.
    206     // Therefore, look for /dev/video* entries directly since this test only
    207     // runs on Linux for now anyway.
    208     base::FileEnumerator dev_video(base::FilePath(FILE_PATH_LITERAL("/dev")),
    209                                    false, base::FileEnumerator::FILES,
    210                                    FILE_PATH_LITERAL("video*"));
    211     return !dev_video.Next().empty();
    212 #endif
    213     NOTREACHED();
    214     return false;
    215   }
    216 
    217  private:
    218   base::ProcessHandle dev_appserver_;
    219   base::ProcessHandle firefox_;
    220 };
    221 
    222 IN_PROC_BROWSER_TEST_F(WebRtcApprtcBrowserTest, MANUAL_WorksOnApprtc) {
    223   // Disabled on Win XP: http://code.google.com/p/webrtc/issues/detail?id=2703.
    224   if (OnWinXp())
    225     return;
    226 
    227   DetectErrorsInJavaScript();
    228   ASSERT_TRUE(LaunchApprtcInstanceOnLocalhost());
    229   while (!LocalApprtcInstanceIsUp())
    230     VLOG(1) << "Waiting for AppRTC to come up...";
    231 
    232   GURL room_url = GURL(base::StringPrintf("localhost:9999?r=room_%d",
    233                                           base::RandInt(0, 65536)));
    234 
    235   chrome::AddTabAt(browser(), GURL(), -1, true);
    236   content::WebContents* left_tab = OpenPageAndAcceptUserMedia(room_url);
    237 
    238   chrome::AddTabAt(browser(), GURL(), -1, true);
    239   content::WebContents* right_tab = OpenPageAndAcceptUserMedia(room_url);
    240 
    241   ASSERT_TRUE(WaitForCallToComeUp(left_tab));
    242   ASSERT_TRUE(WaitForCallToComeUp(right_tab));
    243 
    244   ASSERT_TRUE(DetectRemoteVideoPlaying(left_tab));
    245   ASSERT_TRUE(DetectRemoteVideoPlaying(right_tab));
    246 
    247   ASSERT_TRUE(HangUpApprtcCall(left_tab));
    248 
    249   ASSERT_TRUE(WaitForCallToHangUp(left_tab));
    250   ASSERT_TRUE(WaitForCallToHangUp(right_tab));
    251 
    252   chrome::CloseWebContents(browser(), left_tab, false);
    253   chrome::CloseWebContents(browser(), right_tab, false);
    254 }
    255 
    256 #if defined(OS_LINUX)
    257 #define MAYBE_MANUAL_FirefoxApprtcInteropTest MANUAL_FirefoxApprtcInteropTest
    258 #else
    259 // Not implemented yet on Windows and Mac.
    260 #define MAYBE_MANUAL_FirefoxApprtcInteropTest DISABLED_MANUAL_FirefoxApprtcInteropTest
    261 #endif
    262 
    263 IN_PROC_BROWSER_TEST_F(WebRtcApprtcBrowserTest,
    264                        MAYBE_MANUAL_FirefoxApprtcInteropTest) {
    265   // Disabled on Win XP: http://code.google.com/p/webrtc/issues/detail?id=2703.
    266   if (OnWinXp())
    267     return;
    268 
    269   if (!HasWebcamOnSystem()) {
    270     LOG(INFO)
    271         << "Didn't find a webcam on the system; skipping test since Firefox "
    272         << "needs to be able to acquire a webcam.";
    273     return;
    274   }
    275 
    276   DetectErrorsInJavaScript();
    277   ASSERT_TRUE(LaunchApprtcInstanceOnLocalhost());
    278   while (!LocalApprtcInstanceIsUp())
    279     VLOG(1) << "Waiting for AppRTC to come up...";
    280 
    281   GURL room_url = GURL(base::StringPrintf("http://localhost:9999?r=room_%d",
    282                                           base::RandInt(0, 65536)));
    283   content::WebContents* chrome_tab = OpenPageAndAcceptUserMedia(room_url);
    284 
    285   ASSERT_TRUE(LaunchFirefoxWithUrl(room_url));
    286 
    287   ASSERT_TRUE(WaitForCallToComeUp(chrome_tab));
    288 
    289   // Ensure Firefox manages to send video our way.
    290   ASSERT_TRUE(DetectRemoteVideoPlaying(chrome_tab));
    291 }
    292