- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / media / chrome_webrtc_typing_detection_browsertest.cc
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 "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"
21
22 static const base::FilePath::CharType kReferenceFile[] =
23 #if defined (OS_WIN)
24     FILE_PATH_LITERAL("pyauto_private/webrtc/human-voice-win.wav");
25 #else
26     FILE_PATH_LITERAL("pyauto_private/webrtc/human-voice-linux.wav");
27 #endif
28
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[] =
32 #if defined (OS_WIN)
33     "../pyauto_private/webrtc/human-voice-win.wav";
34 #else
35     "../pyauto_private/webrtc/human-voice-linux.wav";
36 #endif
37
38 static const char kMainWebrtcTestHtmlPage[] =
39     "files/webrtc/webrtc_audio_quality_test.html";
40
41 static base::FilePath GetTestDataDir() {
42   base::FilePath source_dir;
43   PathService::Get(chrome::DIR_TEST_DATA, &source_dir);
44   return source_dir;
45 }
46
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 {
51  public:
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();
56   }
57
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();
64       return false;
65     }
66     return true;
67   }
68
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));
73   }
74
75   void PlayAudioFile(content::WebContents* tab_contents) {
76     EXPECT_EQ("ok-playing", ExecuteJavascript("playAudioFile()", tab_contents));
77   }
78
79   void MixLocalStreamWithPreviouslyLoadedAudioFile(
80       content::WebContents* tab_contents) {
81     EXPECT_EQ("ok-mixed-in", ExecuteJavascript(
82         "mixLocalStreamWithPreviouslyLoadedAudioFile()", tab_contents));
83   }
84
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));
90   }
91
92   void EstablishCall(content::WebContents* from_tab,
93                      content::WebContents* to_tab) {
94     EXPECT_EQ("ok-negotiating",
95               ExecuteJavascript("negotiateCall()", from_tab));
96
97     // Ensure the call gets up on both sides.
98     EXPECT_TRUE(PollingWaitUntil("getPeerConnectionReadyState()",
99                                  "active", from_tab));
100     EXPECT_TRUE(PollingWaitUntil("getPeerConnectionReadyState()",
101                                  "active", to_tab));
102   }
103
104   void HangUp(content::WebContents* from_tab) {
105     EXPECT_EQ("ok-call-hung-up", ExecuteJavascript("hangUp()", from_tab));
106   }
107
108   void WaitUntilHangupVerified(content::WebContents* tab_contents) {
109     EXPECT_TRUE(PollingWaitUntil("getPeerConnectionReadyState()",
110                                  "no-peer-connection", tab_contents));
111   }
112
113   PeerConnectionServerRunner peerconnection_server_;
114 };
115
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());
123
124   ui_test_utils::NavigateToURL(
125       browser(), test_server()->GetURL(kMainWebrtcTestHtmlPage));
126   content::WebContents* left_tab =
127       browser()->tab_strip_model()->GetActiveWebContents();
128
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));
134
135   ConnectToPeerConnectionServer("peer 1", left_tab);
136   ConnectToPeerConnectionServer("peer 2", right_tab);
137
138   GetUserMediaWithSpecificConstraintsAndAccept(left_tab,
139                                                kAudioOnlyCallConstraints);
140   EXPECT_EQ("ok-peerconnection-created",
141             ExecuteJavascript("preparePeerConnection()", left_tab));
142
143   AddAudioFile(kReferenceFileRelativeUrl, left_tab);
144   MixLocalStreamWithPreviouslyLoadedAudioFile(left_tab);
145
146   EstablishCall(left_tab, right_tab);
147
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
151   // file.
152   SleepInJavascript(left_tab, 2000);
153
154   PlayAudioFile(left_tab);
155
156   // TODO(phoglund): simulate key presses, look for changes in typing detection
157   // state.
158   SleepInJavascript(left_tab, 10000);
159
160   AssertNoAsynchronousErrors(left_tab);
161   AssertNoAsynchronousErrors(right_tab);
162
163   HangUp(left_tab);
164   WaitUntilHangupVerified(left_tab);
165   WaitUntilHangupVerified(right_tab);
166
167   AssertNoAsynchronousErrors(left_tab);
168   AssertNoAsynchronousErrors(right_tab);
169
170   ASSERT_TRUE(peerconnection_server_.Stop());
171 }