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.
7 #include "base/path_service.h"
8 #include "base/strings/stringprintf.h"
9 #include "chrome/browser/media/webrtc_browsertest_base.h"
10 #include "chrome/browser/media/webrtc_browsertest_common.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/browser_tabstrip.h"
14 #include "chrome/browser/ui/tabs/tab_strip_model.h"
15 #include "chrome/common/chrome_paths.h"
16 #include "chrome/test/base/ui_test_utils.h"
17 #include "chrome/test/ui/ui_test.h"
18 #include "content/public/test/browser_test_utils.h"
19 #include "net/test/embedded_test_server/embedded_test_server.h"
20 #include "testing/perf/perf_test.h"
22 static const base::FilePath::CharType kReferenceFile[] =
24 FILE_PATH_LITERAL("pyauto_private/webrtc/human-voice-win.wav");
26 FILE_PATH_LITERAL("pyauto_private/webrtc/human-voice-linux.wav");
29 // The javascript will load the reference file relative to its location,
30 // which is in /webrtc on the web server. Therefore, prepend a '..' traversal.
31 static const char kReferenceFileRelativeUrl[] =
33 "../pyauto_private/webrtc/human-voice-win.wav";
35 "../pyauto_private/webrtc/human-voice-linux.wav";
38 static const char kMainWebrtcTestHtmlPage[] =
39 "files/webrtc/webrtc_audio_quality_test.html";
41 static base::FilePath GetTestDataDir() {
42 base::FilePath source_dir;
43 PathService::Get(chrome::DIR_TEST_DATA, &source_dir);
47 // Test that the typing detection feature works.
48 // You must have the src-internal solution in your .gclient to put the required
49 // pyauto_private directory into chrome/test/data/.
50 class WebrtcTypingDetectionBrowserTest : public WebRtcTestBase {
52 // TODO(phoglund): clean up duplication from audio quality browser test when
53 // this test is complete and is proven to work.
54 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
55 PeerConnectionServerRunner::KillAllPeerConnectionServersOnCurrentSystem();
58 bool HasAllRequiredResources() {
59 base::FilePath reference_file =
60 GetTestDataDir().Append(kReferenceFile);
61 if (!base::PathExists(reference_file)) {
62 LOG(ERROR) << "Cannot find the reference file to be used for audio "
63 << "quality comparison: " << reference_file.value();
69 void AddAudioFile(const std::string& input_file_relative_url,
70 content::WebContents* tab_contents) {
71 EXPECT_EQ("ok-added", ExecuteJavascript(
72 "addAudioFile('" + input_file_relative_url + "')", tab_contents));
75 void PlayAudioFile(content::WebContents* tab_contents) {
76 EXPECT_EQ("ok-playing", ExecuteJavascript("playAudioFile()", tab_contents));
79 void MixLocalStreamWithPreviouslyLoadedAudioFile(
80 content::WebContents* tab_contents) {
81 EXPECT_EQ("ok-mixed-in", ExecuteJavascript(
82 "mixLocalStreamWithPreviouslyLoadedAudioFile()", tab_contents));
85 // Ensures we didn't get any errors asynchronously (e.g. while no javascript
86 // call from this test was outstanding).
87 void AssertNoAsynchronousErrors(content::WebContents* tab_contents) {
88 EXPECT_EQ("ok-no-errors",
89 ExecuteJavascript("getAnyTestFailures()", tab_contents));
92 void EstablishCall(content::WebContents* from_tab,
93 content::WebContents* to_tab) {
94 EXPECT_EQ("ok-negotiating",
95 ExecuteJavascript("negotiateCall()", from_tab));
97 // Ensure the call gets up on both sides.
98 EXPECT_TRUE(PollingWaitUntil("getPeerConnectionReadyState()",
100 EXPECT_TRUE(PollingWaitUntil("getPeerConnectionReadyState()",
104 void HangUp(content::WebContents* from_tab) {
105 EXPECT_EQ("ok-call-hung-up", ExecuteJavascript("hangUp()", from_tab));
108 void WaitUntilHangupVerified(content::WebContents* tab_contents) {
109 EXPECT_TRUE(PollingWaitUntil("getPeerConnectionReadyState()",
110 "no-peer-connection", tab_contents));
113 PeerConnectionServerRunner peerconnection_server_;
116 // TODO(phoglund): enable when fully implemented.
117 IN_PROC_BROWSER_TEST_F(WebrtcTypingDetectionBrowserTest,
118 DISABLED_MANUAL_TestTypingDetection) {
119 // TODO(phoglund): make this use embedded_test_server when that test server
120 // can handle files > ~400Kb.
121 ASSERT_TRUE(test_server()->Start());
122 ASSERT_TRUE(peerconnection_server_.Start());
124 ui_test_utils::NavigateToURL(
125 browser(), test_server()->GetURL(kMainWebrtcTestHtmlPage));
126 content::WebContents* left_tab =
127 browser()->tab_strip_model()->GetActiveWebContents();
129 chrome::AddBlankTabAt(browser(), -1, true);
130 content::WebContents* right_tab =
131 browser()->tab_strip_model()->GetActiveWebContents();
132 ui_test_utils::NavigateToURL(
133 browser(), test_server()->GetURL(kMainWebrtcTestHtmlPage));
135 ConnectToPeerConnectionServer("peer 1", left_tab);
136 ConnectToPeerConnectionServer("peer 2", right_tab);
138 GetUserMediaWithSpecificConstraintsAndAccept(left_tab,
139 kAudioOnlyCallConstraints);
140 EXPECT_EQ("ok-peerconnection-created",
141 ExecuteJavascript("preparePeerConnection()", left_tab));
143 AddAudioFile(kReferenceFileRelativeUrl, left_tab);
144 MixLocalStreamWithPreviouslyLoadedAudioFile(left_tab);
146 EstablishCall(left_tab, right_tab);
148 // Note: the media flow isn't necessarily established on the connection just
149 // because the ready state is ok on both sides. We sleep a bit between call
150 // establishment and playing to avoid cutting of the beginning of the audio
152 SleepInJavascript(left_tab, 2000);
154 PlayAudioFile(left_tab);
156 // TODO(phoglund): simulate key presses, look for changes in typing detection
158 SleepInJavascript(left_tab, 10000);
160 AssertNoAsynchronousErrors(left_tab);
161 AssertNoAsynchronousErrors(right_tab);
164 WaitUntilHangupVerified(left_tab);
165 WaitUntilHangupVerified(right_tab);
167 AssertNoAsynchronousErrors(left_tab);
168 AssertNoAsynchronousErrors(right_tab);
170 ASSERT_TRUE(peerconnection_server_.Stop());