1// Copyright (c) 2012 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/strings/stringprintf.h"
7#include "base/strings/utf_string_conversions.h"
8#include "content/browser/web_contents/web_contents_impl.h"
9#include "content/public/common/content_switches.h"
10#include "content/public/test/browser_test_utils.h"
11#include "content/shell/shell.h"
12#include "content/test/content_browser_test.h"
13#include "content/test/content_browser_test_utils.h"
14#include "net/test/embedded_test_server/embedded_test_server.h"
15
16#if defined(OS_WIN)
17#include "base/win/windows_version.h"
18#endif
19
20namespace {
21
22std::string GenerateGetUserMediaCall(int min_width,
23                                     int max_width,
24                                     int min_height,
25                                     int max_height,
26                                     int min_frame_rate,
27                                     int max_frame_rate) {
28  return base::StringPrintf(
29      "getUserMedia({video: {mandatory: {minWidth: %d, maxWidth: %d, "
30      "minHeight: %d, maxHeight: %d, minFrameRate: %d, maxFrameRate: %d}, "
31      "optional: []}});",
32      min_width,
33      max_width,
34      min_height,
35      max_height,
36      min_frame_rate,
37      max_frame_rate);
38}
39}
40
41namespace content {
42
43class WebrtcBrowserTest: public ContentBrowserTest {
44 public:
45  WebrtcBrowserTest() {}
46  virtual ~WebrtcBrowserTest() {}
47
48  virtual void SetUpOnMainThread() OVERRIDE {
49    // We need fake devices in this test since we want to run on naked VMs. We
50    // assume these switches are set by default in content_browsertests.
51    ASSERT_TRUE(CommandLine::ForCurrentProcess()->HasSwitch(
52        switches::kUseFakeDeviceForMediaStream));
53    ASSERT_TRUE(CommandLine::ForCurrentProcess()->HasSwitch(
54        switches::kUseFakeUIForMediaStream));
55
56    ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
57  }
58
59 protected:
60  bool ExecuteJavascript(const std::string& javascript) {
61    return ExecuteScript(shell()->web_contents(), javascript);
62  }
63
64  void ExpectTitle(const std::string& expected_title) const {
65    string16 expected_title16(ASCIIToUTF16(expected_title));
66    TitleWatcher title_watcher(shell()->web_contents(), expected_title16);
67    EXPECT_EQ(expected_title16, title_watcher.WaitAndGetTitle());
68  }
69};
70
71// These tests will all make a getUserMedia call with different constraints and
72// see that the success callback is called. If the error callback is called or
73// none of the callbacks are called the tests will simply time out and fail.
74IN_PROC_BROWSER_TEST_F(WebrtcBrowserTest, GetVideoStreamAndStop) {
75  GURL url(embedded_test_server()->GetURL("/media/getusermedia.html"));
76  NavigateToURL(shell(), url);
77
78  EXPECT_TRUE(ExecuteJavascript("getUserMedia({video: true});"));
79
80  ExpectTitle("OK");
81}
82
83IN_PROC_BROWSER_TEST_F(WebrtcBrowserTest, GetAudioAndVideoStreamAndStop) {
84  GURL url(embedded_test_server()->GetURL("/media/getusermedia.html"));
85  NavigateToURL(shell(), url);
86
87  EXPECT_TRUE(ExecuteJavascript("getUserMedia({video: true, audio: true});"));
88
89  ExpectTitle("OK");
90}
91
92IN_PROC_BROWSER_TEST_F(WebrtcBrowserTest, GetAudioAndVideoStreamAndClone) {
93  GURL url(embedded_test_server()->GetURL("/media/getusermedia.html"));
94  NavigateToURL(shell(), url);
95
96  EXPECT_TRUE(ExecuteJavascript("getUserMediaAndClone();"));
97
98  ExpectTitle("OK");
99}
100
101
102#if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY)
103// Timing out on ARM linux bot: http://crbug.com/238490
104#define MAYBE_CanSetupVideoCall DISABLED_CanSetupVideoCall
105#else
106#define MAYBE_CanSetupVideoCall CanSetupVideoCall
107#endif
108
109// These tests will make a complete PeerConnection-based call and verify that
110// video is playing for the call.
111IN_PROC_BROWSER_TEST_F(WebrtcBrowserTest, MAYBE_CanSetupVideoCall) {
112  GURL url(embedded_test_server()->GetURL("/media/peerconnection-call.html"));
113  NavigateToURL(shell(), url);
114
115  EXPECT_TRUE(ExecuteJavascript("call({video: true});"));
116  ExpectTitle("OK");
117}
118
119#if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY)
120// Timing out on ARM linux, see http://crbug.com/240376
121#define MAYBE_CanSetupAudioAndVideoCall DISABLED_CanSetupAudioAndVideoCall
122#else
123#define MAYBE_CanSetupAudioAndVideoCall CanSetupAudioAndVideoCall
124#endif
125
126IN_PROC_BROWSER_TEST_F(WebrtcBrowserTest, MAYBE_CanSetupAudioAndVideoCall) {
127  GURL url(embedded_test_server()->GetURL("/media/peerconnection-call.html"));
128  NavigateToURL(shell(), url);
129
130  EXPECT_TRUE(ExecuteJavascript("call({video: true, audio: true});"));
131  ExpectTitle("OK");
132}
133
134IN_PROC_BROWSER_TEST_F(WebrtcBrowserTest, MANUAL_CanSetupCallAndSendDtmf) {
135  GURL url(embedded_test_server()->GetURL("/media/peerconnection-call.html"));
136  NavigateToURL(shell(), url);
137
138  EXPECT_TRUE(
139      ExecuteJavascript("callAndSendDtmf('123,abc');"));
140}
141
142IN_PROC_BROWSER_TEST_F(WebrtcBrowserTest,
143                       CanMakeEmptyCallThenAddStreamsAndRenegotiate) {
144  GURL url(embedded_test_server()->GetURL("/media/peerconnection-call.html"));
145  NavigateToURL(shell(), url);
146
147  const char* kJavascript =
148      "callEmptyThenAddOneStreamAndRenegotiate({video: true, audio: true});";
149  EXPECT_TRUE(ExecuteJavascript(kJavascript));
150  ExpectTitle("OK");
151}
152
153// This test will make a complete PeerConnection-based call but remove the
154// MSID and bundle attribute from the initial offer to verify that
155// video is playing for the call even if the initiating client don't support
156// MSID. http://tools.ietf.org/html/draft-alvestrand-rtcweb-msid-02
157#if defined(OS_WIN) && defined(USE_AURA)
158// Disabled for win7_aura, see http://crbug.com/235089.
159#define MAYBE_CanSetupAudioAndVideoCallWithoutMsidAndBundle\
160        DISABLED_CanSetupAudioAndVideoCallWithoutMsidAndBundle
161#elif defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY)
162// Timing out on ARM linux, see http://crbug.com/240373
163#define MAYBE_CanSetupAudioAndVideoCallWithoutMsidAndBundle\
164        DISABLED_CanSetupAudioAndVideoCallWithoutMsidAndBundle
165#else
166#define MAYBE_CanSetupAudioAndVideoCallWithoutMsidAndBundle\
167        CanSetupAudioAndVideoCallWithoutMsidAndBundle
168#endif
169IN_PROC_BROWSER_TEST_F(WebrtcBrowserTest,
170                       MAYBE_CanSetupAudioAndVideoCallWithoutMsidAndBundle) {
171  GURL url(embedded_test_server()->GetURL("/media/peerconnection-call.html"));
172  NavigateToURL(shell(), url);
173
174  EXPECT_TRUE(ExecuteJavascript("callWithoutMsidAndBundle();"));
175  ExpectTitle("OK");
176}
177
178// This test will make a PeerConnection-based call and test an unreliable text
179// dataChannel.
180IN_PROC_BROWSER_TEST_F(WebrtcBrowserTest, CallWithDataOnly) {
181  GURL url(embedded_test_server()->GetURL("/media/peerconnection-call.html"));
182  NavigateToURL(shell(), url);
183
184  EXPECT_TRUE(ExecuteJavascript("callWithDataOnly();"));
185  ExpectTitle("OK");
186}
187
188#if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY)
189// Timing out on ARM linux bot: http://crbug.com/238490
190#define MAYBE_CallWithDataAndMedia DISABLED_CallWithDataAndMedia
191#else
192#define MAYBE_CallWithDataAndMedia CallWithDataAndMedia
193#endif
194
195// This test will make a PeerConnection-based call and test an unreliable text
196// dataChannel and audio and video tracks.
197IN_PROC_BROWSER_TEST_F(WebrtcBrowserTest, MAYBE_CallWithDataAndMedia) {
198  GURL url(embedded_test_server()->GetURL("/media/peerconnection-call.html"));
199  NavigateToURL(shell(), url);
200
201  EXPECT_TRUE(ExecuteJavascript("callWithDataAndMedia();"));
202  ExpectTitle("OK");
203}
204
205#if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY)
206// Timing out on ARM linux bot: http://crbug.com/238490
207#define MAYBE_CallWithDataAndLaterAddMedia DISABLED_CallWithDataAndLaterAddMedia
208#else
209#define MAYBE_CallWithDataAndLaterAddMedia CallWithDataAndLaterAddMedia
210#endif
211
212// This test will make a PeerConnection-based call and test an unreliable text
213// dataChannel and later add an audio and video track.
214IN_PROC_BROWSER_TEST_F(WebrtcBrowserTest, MAYBE_CallWithDataAndLaterAddMedia) {
215  GURL url(embedded_test_server()->GetURL("/media/peerconnection-call.html"));
216  NavigateToURL(shell(), url);
217
218  EXPECT_TRUE(ExecuteJavascript("callWithDataAndLaterAddMedia();"));
219  ExpectTitle("OK");
220}
221
222#if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY)
223// Timing out on ARM linux bot: http://crbug.com/238490
224#define MAYBE_CallWithNewVideoMediaStream DISABLED_CallWithNewVideoMediaStream
225#else
226#define MAYBE_CallWithNewVideoMediaStream CallWithNewVideoMediaStream
227#endif
228
229// This test will make a PeerConnection-based call and send a new Video
230// MediaStream that has been created based on a MediaStream created with
231// getUserMedia.
232IN_PROC_BROWSER_TEST_F(WebrtcBrowserTest, MAYBE_CallWithNewVideoMediaStream) {
233  GURL url(embedded_test_server()->GetURL("/media/peerconnection-call.html"));
234  NavigateToURL(shell(), url);
235
236  EXPECT_TRUE(ExecuteJavascript("callWithNewVideoMediaStream();"));
237  ExpectTitle("OK");
238}
239
240// This test will make a PeerConnection-based call and send a new Video
241// MediaStream that has been created based on a MediaStream created with
242// getUserMedia. When video is flowing, the VideoTrack is removed and an
243// AudioTrack is added instead.
244// TODO(phoglund): This test is manual since not all buildbots has an audio
245// input.
246IN_PROC_BROWSER_TEST_F(WebrtcBrowserTest, MANUAL_CallAndModifyStream) {
247  GURL url(embedded_test_server()->GetURL("/media/peerconnection-call.html"));
248  NavigateToURL(shell(), url);
249
250  EXPECT_TRUE(
251      ExecuteJavascript("callWithNewVideoMediaStreamLaterSwitchToAudio();"));
252  ExpectTitle("OK");
253}
254
255// This test calls getUserMedia in sequence with different constraints.
256IN_PROC_BROWSER_TEST_F(WebrtcBrowserTest, TestGetUserMediaConstraints) {
257  GURL url(embedded_test_server()->GetURL("/media/getusermedia.html"));
258
259  std::vector<std::string> list_of_get_user_media_calls;
260  list_of_get_user_media_calls.push_back(
261      GenerateGetUserMediaCall(320, 320, 180, 180, 30, 30));
262  list_of_get_user_media_calls.push_back(
263      GenerateGetUserMediaCall(320, 320, 240, 240, 30, 30));
264  list_of_get_user_media_calls.push_back(
265      GenerateGetUserMediaCall(640, 640, 360, 360, 30, 30));
266  list_of_get_user_media_calls.push_back(
267      GenerateGetUserMediaCall(640, 640, 480, 480, 30, 30));
268  list_of_get_user_media_calls.push_back(
269      GenerateGetUserMediaCall(960, 960, 720, 720, 30, 30));
270  list_of_get_user_media_calls.push_back(
271      GenerateGetUserMediaCall(1280, 1280, 720, 720, 30, 30));
272  list_of_get_user_media_calls.push_back(
273      GenerateGetUserMediaCall(1920, 1920, 1080, 1080, 30, 30));
274
275  for (std::vector<std::string>::iterator const_iterator =
276           list_of_get_user_media_calls.begin();
277       const_iterator != list_of_get_user_media_calls.end();
278       ++const_iterator) {
279    DVLOG(1) << "Calling getUserMedia: " << *const_iterator;
280    NavigateToURL(shell(), url);
281    EXPECT_TRUE(ExecuteJavascript(*const_iterator));
282    ExpectTitle("OK");
283  }
284}
285
286IN_PROC_BROWSER_TEST_F(WebrtcBrowserTest, AddTwoMediaStreamsToOnePC) {
287  GURL url(embedded_test_server()->GetURL("/media/peerconnection-call.html"));
288  NavigateToURL(shell(), url);
289
290  EXPECT_TRUE(
291      ExecuteJavascript("addTwoMediaStreamsToOneConnection();"));
292  ExpectTitle("OK");
293}
294
295}  // namespace content
296