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 <ctime>
6
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 "content/public/test/browser_test_utils.h"
18 #include "net/test/embedded_test_server/embedded_test_server.h"
19 #include "testing/perf/perf_test.h"
20
21 static const base::FilePath::CharType kReferenceFile[] =
22 #if defined (OS_WIN)
23 FILE_PATH_LITERAL("pyauto_private/webrtc/human-voice-win.wav");
24 #else
25 FILE_PATH_LITERAL("pyauto_private/webrtc/human-voice-linux.wav");
26 #endif
27
28 // The javascript will load the reference file relative to its location,
29 // which is in /webrtc on the web server. Therefore, prepend a '..' traversal.
30 static const char kReferenceFileRelativeUrl[] =
31 #if defined (OS_WIN)
32 "../pyauto_private/webrtc/human-voice-win.wav";
33 #else
34 "../pyauto_private/webrtc/human-voice-linux.wav";
35 #endif
36
37 static const char kMainWebrtcTestHtmlPage[] =
38 "files/webrtc/webrtc_audio_quality_test.html";
39
GetTestDataDir()40 static base::FilePath GetTestDataDir() {
41 base::FilePath source_dir;
42 PathService::Get(chrome::DIR_TEST_DATA, &source_dir);
43 return source_dir;
44 }
45
46 // Test that the typing detection feature works.
47 // You must have the src-internal solution in your .gclient to put the required
48 // pyauto_private directory into chrome/test/data/.
49 class WebRtcTypingDetectionBrowserTest : public WebRtcTestBase {
50 public:
51 // TODO(phoglund): clean up duplication from audio quality browser test when
52 // this test is complete and is proven to work.
HasAllRequiredResources()53 bool HasAllRequiredResources() {
54 base::FilePath reference_file =
55 GetTestDataDir().Append(kReferenceFile);
56 if (!base::PathExists(reference_file)) {
57 LOG(ERROR) << "Cannot find the reference file to be used for audio "
58 << "quality comparison: " << reference_file.value();
59 return false;
60 }
61 return true;
62 }
63
AddAudioFile(const std::string & input_file_relative_url,content::WebContents * tab_contents)64 void AddAudioFile(const std::string& input_file_relative_url,
65 content::WebContents* tab_contents) {
66 EXPECT_EQ("ok-added", ExecuteJavascript(
67 "addAudioFile('" + input_file_relative_url + "')", tab_contents));
68 }
69
PlayAudioFile(content::WebContents * tab_contents)70 void PlayAudioFile(content::WebContents* tab_contents) {
71 EXPECT_EQ("ok-playing", ExecuteJavascript("playAudioFile()", tab_contents));
72 }
73
MixLocalStreamWithPreviouslyLoadedAudioFile(content::WebContents * tab_contents)74 void MixLocalStreamWithPreviouslyLoadedAudioFile(
75 content::WebContents* tab_contents) {
76 EXPECT_EQ("ok-mixed-in", ExecuteJavascript(
77 "mixLocalStreamWithPreviouslyLoadedAudioFile()", tab_contents));
78 }
79
EstablishCall(content::WebContents * from_tab,content::WebContents * to_tab)80 void EstablishCall(content::WebContents* from_tab,
81 content::WebContents* to_tab) {
82 EXPECT_EQ("ok-negotiating",
83 ExecuteJavascript("negotiateCall()", from_tab));
84
85 // Ensure the call gets up on both sides.
86 EXPECT_TRUE(test::PollingWaitUntil("getPeerConnectionReadyState()",
87 "active", from_tab));
88 EXPECT_TRUE(test::PollingWaitUntil("getPeerConnectionReadyState()",
89 "active", to_tab));
90 }
91 };
92
93 // TODO(phoglund): enable when fully implemented.
IN_PROC_BROWSER_TEST_F(WebRtcTypingDetectionBrowserTest,DISABLED_MANUAL_TestTypingDetection)94 IN_PROC_BROWSER_TEST_F(WebRtcTypingDetectionBrowserTest,
95 DISABLED_MANUAL_TestTypingDetection) {
96 // TODO(phoglund): make this use embedded_test_server when that test server
97 // can handle files > ~400Kb.
98 ASSERT_TRUE(test_server()->Start());
99
100 ui_test_utils::NavigateToURL(
101 browser(), test_server()->GetURL(kMainWebrtcTestHtmlPage));
102 content::WebContents* left_tab =
103 browser()->tab_strip_model()->GetActiveWebContents();
104
105 chrome::AddTabAt(browser(), GURL(), -1, true);
106 content::WebContents* right_tab =
107 browser()->tab_strip_model()->GetActiveWebContents();
108 ui_test_utils::NavigateToURL(
109 browser(), test_server()->GetURL(kMainWebrtcTestHtmlPage));
110
111 GetUserMediaWithSpecificConstraintsAndAccept(left_tab,
112 kAudioOnlyCallConstraints);
113 EXPECT_EQ("ok-peerconnection-created",
114 ExecuteJavascript("preparePeerConnection()", left_tab));
115
116 AddAudioFile(kReferenceFileRelativeUrl, left_tab);
117 MixLocalStreamWithPreviouslyLoadedAudioFile(left_tab);
118
119 SetupPeerconnectionWithLocalStream(left_tab);
120 SetupPeerconnectionWithLocalStream(right_tab);
121
122 NegotiateCall(left_tab, right_tab);
123
124 // Note: the media flow isn't necessarily established on the connection just
125 // because the ready state is ok on both sides. We sleep a bit between call
126 // establishment and playing to avoid cutting of the beginning of the audio
127 // file.
128 test::SleepInJavascript(left_tab, 2000);
129
130 PlayAudioFile(left_tab);
131
132 // TODO(phoglund): simulate key presses, look for changes in typing detection
133 // state.
134 test::SleepInJavascript(left_tab, 10000);
135
136 HangUp(left_tab);
137 HangUp(right_tab);
138 }
139