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 void EstablishCall(content::WebContents
* from_tab
,
86 content::WebContents
* to_tab
) {
87 EXPECT_EQ("ok-negotiating",
88 ExecuteJavascript("negotiateCall()", from_tab
));
90 // Ensure the call gets up on both sides.
91 EXPECT_TRUE(PollingWaitUntil("getPeerConnectionReadyState()",
93 EXPECT_TRUE(PollingWaitUntil("getPeerConnectionReadyState()",
97 void HangUp(content::WebContents
* from_tab
) {
98 EXPECT_EQ("ok-call-hung-up", ExecuteJavascript("hangUp()", from_tab
));
101 void WaitUntilHangupVerified(content::WebContents
* tab_contents
) {
102 EXPECT_TRUE(PollingWaitUntil("getPeerConnectionReadyState()",
103 "no-peer-connection", tab_contents
));
106 PeerConnectionServerRunner peerconnection_server_
;
109 // TODO(phoglund): enable when fully implemented.
110 IN_PROC_BROWSER_TEST_F(WebRtcTypingDetectionBrowserTest
,
111 DISABLED_MANUAL_TestTypingDetection
) {
112 // TODO(phoglund): make this use embedded_test_server when that test server
113 // can handle files > ~400Kb.
114 ASSERT_TRUE(test_server()->Start());
115 ASSERT_TRUE(peerconnection_server_
.Start());
117 ui_test_utils::NavigateToURL(
118 browser(), test_server()->GetURL(kMainWebrtcTestHtmlPage
));
119 content::WebContents
* left_tab
=
120 browser()->tab_strip_model()->GetActiveWebContents();
122 chrome::AddTabAt(browser(), GURL(), -1, true);
123 content::WebContents
* right_tab
=
124 browser()->tab_strip_model()->GetActiveWebContents();
125 ui_test_utils::NavigateToURL(
126 browser(), test_server()->GetURL(kMainWebrtcTestHtmlPage
));
128 ConnectToPeerConnectionServer("peer 1", left_tab
);
129 ConnectToPeerConnectionServer("peer 2", right_tab
);
131 GetUserMediaWithSpecificConstraintsAndAccept(left_tab
,
132 kAudioOnlyCallConstraints
);
133 EXPECT_EQ("ok-peerconnection-created",
134 ExecuteJavascript("preparePeerConnection()", left_tab
));
136 AddAudioFile(kReferenceFileRelativeUrl
, left_tab
);
137 MixLocalStreamWithPreviouslyLoadedAudioFile(left_tab
);
139 EstablishCall(left_tab
, right_tab
);
141 // Note: the media flow isn't necessarily established on the connection just
142 // because the ready state is ok on both sides. We sleep a bit between call
143 // establishment and playing to avoid cutting of the beginning of the audio
145 SleepInJavascript(left_tab
, 2000);
147 PlayAudioFile(left_tab
);
149 // TODO(phoglund): simulate key presses, look for changes in typing detection
151 SleepInJavascript(left_tab
, 10000);
154 WaitUntilHangupVerified(left_tab
);
155 WaitUntilHangupVerified(right_tab
);
157 ASSERT_TRUE(peerconnection_server_
.Stop());