1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/file_util.h"
#include "base/path_service.h"
#include "base/process_util.h"
#include "base/strings/string_split.h"
#include "base/strings/stringprintf.h"
#include "base/synchronization/waitable_event.h"
#include "base/test/test_timeouts.h"
#include "base/time/time.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/infobars/infobar.h"
#include "chrome/browser/infobars/infobar_service.h"
#include "chrome/browser/media/media_stream_infobar_delegate.h"
#include "chrome/browser/media/webrtc_log_uploader.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "chrome/test/ui/ui_test.h"
#include "content/public/browser/browser_message_filter.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/test/browser_test_utils.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
static const base::FilePath::CharType kPeerConnectionServer[] =
#if defined(OS_WIN)
FILE_PATH_LITERAL("peerconnection_server.exe");
#else
FILE_PATH_LITERAL("peerconnection_server");
#endif
static const char kMainWebrtcTestHtmlPage[] =
"/webrtc/webrtc_jsep01_test.html";
static const char kTestLoggingSessionId[] = "0123456789abcdef";
// Top-level integration test for WebRTC. Requires a real webcam and microphone
// on the running system. This test is not meant to run in the main browser
// test suite since normal tester machines do not have webcams.
class WebrtcBrowserTest : public InProcessBrowserTest {
public:
// See comment in test where this class is used for purpose.
class BrowserMessageFilter : public content::BrowserMessageFilter {
public:
explicit BrowserMessageFilter(
const base::Closure& on_channel_closing)
: on_channel_closing_(on_channel_closing) {}
virtual void OnChannelClosing() OVERRIDE {
// Posting on the file thread ensures that the callback is run after
// WebRtcLogUploader::UploadLog has finished. See also comment in
// MANUAL_RunsAudioVideoWebRTCCallInTwoTabsWithLogging test.
content::BrowserThread::PostTask(content::BrowserThread::FILE,
FROM_HERE, on_channel_closing_);
}
virtual bool OnMessageReceived(const IPC::Message& message,
bool* message_was_ok) OVERRIDE {
return false;
}
private:
virtual ~BrowserMessageFilter() {}
base::Closure on_channel_closing_;
};
WebrtcBrowserTest() : peerconnection_server_(0) {}
virtual void SetUp() OVERRIDE {
RunPeerConnectionServer();
InProcessBrowserTest::SetUp();
}
virtual void TearDown() OVERRIDE {
ShutdownPeerConnectionServer();
InProcessBrowserTest::TearDown();
}
virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
// TODO(phoglund): check that user actually has the requisite devices and
// print a nice message if not; otherwise the test just times out which can
// be confusing.
// This test expects real device handling and requires a real webcam / audio
// device; it will not work with fake devices.
EXPECT_FALSE(command_line->HasSwitch(
switches::kUseFakeDeviceForMediaStream));
EXPECT_FALSE(command_line->HasSwitch(
switches::kUseFakeUIForMediaStream));
}
// TODO(phoglund): This ugly poll method is only here while we transition
// the test javascript to just post events when things happen. Right now they
// don't because the webrtc_call.py and other tests use this polling way of
// communicating when we are waiting from an asynchronous event in the
// javascript. This method is meant to emulate WaitUntil in the PyAuto
// framework.
bool UglyPollingWaitUntil(const std::string& javascript,
const std::string& evaluates_to,
content::WebContents* tab_contents) {
base::Time start_time = base::Time::Now();
base::TimeDelta timeout = base::TimeDelta::FromSeconds(20);
std::string result;
while (base::Time::Now() - start_time < timeout) {
result = ExecuteJavascript(javascript, tab_contents);
if (evaluates_to == result)
return true;
}
LOG(ERROR) << "Timed out while waiting for " << javascript <<
" to evaluate to " << evaluates_to << "; last result was '" << result <<
"'";
return false;
}
// Convenience method which executes the provided javascript in the context
// of the provided web contents and returns what it evaluated to.
std::string ExecuteJavascript(const std::string& javascript,
content::WebContents* tab_contents) {
std::string result;
EXPECT_TRUE(content::ExecuteScriptAndExtractString(
tab_contents, javascript, &result));
return result;
}
void GetUserMedia(content::WebContents* tab_contents) {
content::WindowedNotificationObserver infobar_added(
chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED,
content::NotificationService::AllSources());
// Request user media: this will launch the media stream info bar.
EXPECT_EQ("ok-requested", ExecuteJavascript(
"getUserMedia('{video: true, audio: true}');", tab_contents));
// Wait for the bar to pop up, then accept.
infobar_added.Wait();
content::Details<InfoBarAddedDetails> details(infobar_added.details());
MediaStreamInfoBarDelegate* media_infobar =
details.ptr()->AsMediaStreamInfoBarDelegate();
media_infobar->Accept();
// Wait for WebRTC to call the success callback.
EXPECT_TRUE(UglyPollingWaitUntil("obtainGetUserMediaResult();",
"ok-got-stream",
tab_contents));
}
// Ensures we didn't get any errors asynchronously (e.g. while no javascript
// call from this test was outstanding).
// TODO(phoglund): this becomes obsolete when we switch to communicating with
// the DOM message queue.
void AssertNoAsynchronousErrors(content::WebContents* tab_contents) {
EXPECT_EQ("ok-no-errors",
ExecuteJavascript("getAnyTestFailures()", tab_contents));
}
// The peer connection server lets our two tabs find each other and talk to
// each other (e.g. it is the application-specific "signaling solution").
void ConnectToPeerConnectionServer(const std::string peer_name,
content::WebContents* tab_contents) {
std::string javascript = base::StringPrintf(
"connect('http://localhost:8888', '%s');", peer_name.c_str());
EXPECT_EQ("ok-connected", ExecuteJavascript(javascript, tab_contents));
}
void EstablishCall(content::WebContents* from_tab,
content::WebContents* to_tab,
bool enable_logging = false) {
std::string javascript = enable_logging ?
base::StringPrintf("preparePeerConnection(\"%s\")",
kTestLoggingSessionId) :
"preparePeerConnection(false)";
EXPECT_EQ("ok-peerconnection-created",
ExecuteJavascript(javascript, from_tab));
EXPECT_EQ("ok-added",
ExecuteJavascript("addLocalStream()", from_tab));
EXPECT_EQ("ok-negotiating",
ExecuteJavascript("negotiateCall()", from_tab));
// Ensure the call gets up on both sides.
EXPECT_TRUE(UglyPollingWaitUntil("getPeerConnectionReadyState()",
"active", from_tab));
EXPECT_TRUE(UglyPollingWaitUntil("getPeerConnectionReadyState()",
"active", to_tab));
}
void StartDetectingVideo(content::WebContents* tab_contents,
const std::string& video_element) {
std::string javascript = base::StringPrintf(
"startDetection('%s', 'frame-buffer', 320, 240)",
video_element.c_str());
EXPECT_EQ("ok-started", ExecuteJavascript(javascript, tab_contents));
}
void WaitForVideoToPlay(content::WebContents* tab_contents) {
EXPECT_TRUE(UglyPollingWaitUntil("isVideoPlaying()", "video-playing",
tab_contents));
}
void WaitForVideoToStopPlaying(content::WebContents* tab_contents) {
EXPECT_TRUE(UglyPollingWaitUntil("isVideoPlaying()", "video-not-playing",
tab_contents));
}
void HangUp(content::WebContents* from_tab) {
EXPECT_EQ("ok-call-hung-up", ExecuteJavascript("hangUp()", from_tab));
}
void WaitUntilHangupVerified(content::WebContents* tab_contents) {
EXPECT_TRUE(UglyPollingWaitUntil("getPeerConnectionReadyState()",
"no-peer-connection", tab_contents));
}
std::string ToggleLocalVideoTrack(content::WebContents* tab_contents) {
// Toggle the only video track in the page (e.g. video track 0).
return ExecuteJavascript("toggleLocalStream("
"function(local) { return local.getVideoTracks()[0]; }, "
"'video');", tab_contents);
}
std::string ToggleRemoteVideoTrack(content::WebContents* tab_contents) {
// Toggle the only video track in the page (e.g. video track 0).
return ExecuteJavascript("toggleRemoteStream("
"function(local) { return local.getVideoTracks()[0]; }, "
"'video');", tab_contents);
}
private:
void RunPeerConnectionServer() {
base::FilePath peerconnection_server;
EXPECT_TRUE(PathService::Get(base::DIR_MODULE, &peerconnection_server));
peerconnection_server = peerconnection_server.Append(kPeerConnectionServer);
EXPECT_TRUE(base::PathExists(peerconnection_server)) <<
"Missing peerconnection_server. You must build "
"it so it ends up next to the browser test binary.";
EXPECT_TRUE(base::LaunchProcess(
CommandLine(peerconnection_server),
base::LaunchOptions(),
&peerconnection_server_)) << "Failed to launch peerconnection_server.";
}
void ShutdownPeerConnectionServer() {
EXPECT_TRUE(base::KillProcess(peerconnection_server_, 0, false)) <<
"Failed to shut down peerconnection_server!";
}
base::ProcessHandle peerconnection_server_;
};
IN_PROC_BROWSER_TEST_F(WebrtcBrowserTest,
MANUAL_RunsAudioVideoWebRTCCallInTwoTabs) {
EXPECT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
ui_test_utils::NavigateToURL(
browser(), embedded_test_server()->GetURL(kMainWebrtcTestHtmlPage));
content::WebContents* left_tab =
browser()->tab_strip_model()->GetActiveWebContents();
GetUserMedia(left_tab);
chrome::AddBlankTabAt(browser(), -1, true);
content::WebContents* right_tab =
browser()->tab_strip_model()->GetActiveWebContents();
ui_test_utils::NavigateToURL(
browser(), embedded_test_server()->GetURL(kMainWebrtcTestHtmlPage));
GetUserMedia(right_tab);
ConnectToPeerConnectionServer("peer 1", left_tab);
ConnectToPeerConnectionServer("peer 2", right_tab);
EstablishCall(left_tab, right_tab);
AssertNoAsynchronousErrors(left_tab);
AssertNoAsynchronousErrors(right_tab);
StartDetectingVideo(left_tab, "remote-view");
StartDetectingVideo(right_tab, "remote-view");
WaitForVideoToPlay(left_tab);
WaitForVideoToPlay(right_tab);
HangUp(left_tab);
WaitUntilHangupVerified(left_tab);
WaitUntilHangupVerified(right_tab);
AssertNoAsynchronousErrors(left_tab);
AssertNoAsynchronousErrors(right_tab);
}
IN_PROC_BROWSER_TEST_F(WebrtcBrowserTest,
MANUAL_TestMediaStreamTrackEnableDisable) {
EXPECT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
ui_test_utils::NavigateToURL(
browser(), embedded_test_server()->GetURL(kMainWebrtcTestHtmlPage));
content::WebContents* left_tab =
browser()->tab_strip_model()->GetActiveWebContents();
GetUserMedia(left_tab);
chrome::AddBlankTabAt(browser(), -1, true);
content::WebContents* right_tab =
browser()->tab_strip_model()->GetActiveWebContents();
ui_test_utils::NavigateToURL(
browser(), embedded_test_server()->GetURL(kMainWebrtcTestHtmlPage));
GetUserMedia(right_tab);
ConnectToPeerConnectionServer("peer 1", left_tab);
ConnectToPeerConnectionServer("peer 2", right_tab);
EstablishCall(left_tab, right_tab);
AssertNoAsynchronousErrors(left_tab);
AssertNoAsynchronousErrors(right_tab);
StartDetectingVideo(left_tab, "remote-view");
StartDetectingVideo(right_tab, "remote-view");
WaitForVideoToPlay(left_tab);
WaitForVideoToPlay(right_tab);
EXPECT_EQ("ok-video-toggled-to-false", ToggleLocalVideoTrack(left_tab));
WaitForVideoToStopPlaying(right_tab);
EXPECT_EQ("ok-video-toggled-to-true", ToggleLocalVideoTrack(left_tab));
WaitForVideoToPlay(right_tab);
HangUp(left_tab);
WaitUntilHangupVerified(left_tab);
WaitUntilHangupVerified(right_tab);
AssertNoAsynchronousErrors(left_tab);
AssertNoAsynchronousErrors(right_tab);
}
// Tests WebRTC diagnostic logging. Sets up the browser to save the multipart
// contents to a buffer instead of uploading it, then verifies it after a call.
// Example of multipart contents:
// ------**--yradnuoBgoLtrapitluMklaTelgooG--**----
// Content-Disposition: form-data; name="prod"
//
// Chrome_Linux
// ------**--yradnuoBgoLtrapitluMklaTelgooG--**----
// Content-Disposition: form-data; name="ver"
//
// 30.0.1554.0
// ------**--yradnuoBgoLtrapitluMklaTelgooG--**----
// Content-Disposition: form-data; name="guid"
//
// 0
// ------**--yradnuoBgoLtrapitluMklaTelgooG--**----
// Content-Disposition: form-data; name="type"
//
// webrtc_log
// ------**--yradnuoBgoLtrapitluMklaTelgooG--**----
// Content-Disposition: form-data; name="app_session_id"
//
// 0123456789abcdef
// ------**--yradnuoBgoLtrapitluMklaTelgooG--**----
// Content-Disposition: form-data; name="url"
//
// http://127.0.0.1:43213/webrtc/webrtc_jsep01_test.html
// ------**--yradnuoBgoLtrapitluMklaTelgooG--**----
// Content-Disposition: form-data; name="webrtc_log"; filename="webrtc_log.gz"
// Content-Type: application/gzip
//
// <compressed data (zip)>
// ------**--yradnuoBgoLtrapitluMklaTelgooG--**------
// Fails on all platforms. http://crbug.com/257606
// When enabling, it should be prefixed MANUAL_.
IN_PROC_BROWSER_TEST_F(WebrtcBrowserTest,
DISABLED_RunsAudioVideoWebRTCCallInTwoTabsWithLogging) {
EXPECT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
// Tell the uploader to save the multipart to a buffer instead of uploading.
std::string multipart;
g_browser_process->webrtc_log_uploader()->
OverrideUploadWithBufferForTesting(&multipart);
ui_test_utils::NavigateToURL(
browser(), embedded_test_server()->GetURL(kMainWebrtcTestHtmlPage));
content::WebContents* left_tab =
browser()->tab_strip_model()->GetActiveWebContents();
GetUserMedia(left_tab);
chrome::AddBlankTabAt(browser(), -1, true);
content::WebContents* right_tab =
browser()->tab_strip_model()->GetActiveWebContents();
ui_test_utils::NavigateToURL(
browser(), embedded_test_server()->GetURL(kMainWebrtcTestHtmlPage));
GetUserMedia(right_tab);
ConnectToPeerConnectionServer("peer 1", left_tab);
ConnectToPeerConnectionServer("peer 2", right_tab);
EstablishCall(left_tab, right_tab, true);
AssertNoAsynchronousErrors(left_tab);
AssertNoAsynchronousErrors(right_tab);
StartDetectingVideo(left_tab, "remote-view");
StartDetectingVideo(right_tab, "remote-view");
WaitForVideoToPlay(left_tab);
WaitForVideoToPlay(right_tab);
HangUp(left_tab);
WaitUntilHangupVerified(left_tab);
WaitUntilHangupVerified(right_tab);
AssertNoAsynchronousErrors(left_tab);
AssertNoAsynchronousErrors(right_tab);
// Uploading (or storing to our buffer in our test case) is triggered when
// the tab is closed. We must ensure that WebRtcLogUploader::UploadLog, which
// runs on the FILE thread, has finished after closing the tab before
// continuing.
// 1. Add a filter which just acts as a listener. It's added after
// WebRtcLoggingHandlerHost, which is the filter that posts a task for
// WebRtcLogUploader::UploadLog. So it's guarantueed to be removed after
// WebRtcLoggingHandlerHost.
// 2. When the filter goes away post a task on the file thread to signal the
// event.
base::WaitableEvent ipc_channel_closed(false, false);
left_tab->GetRenderProcessHost()->GetChannel()->AddFilter(
new BrowserMessageFilter(base::Bind(
&base::WaitableEvent::Signal,
base::Unretained(&ipc_channel_closed))));
chrome::CloseWebContents(browser(), left_tab, false);
ASSERT_TRUE(ipc_channel_closed.TimedWait(TestTimeouts::action_timeout()));
const char boundary[] = "------**--yradnuoBgoLtrapitluMklaTelgooG--**----";
// Remove the compressed data, it may contain "\r\n". Just verify that its
// size is > 0.
const char zip_content_type[] = "Content-Type: application/gzip";
size_t zip_pos = multipart.find(&zip_content_type[0]);
ASSERT_NE(std::string::npos, zip_pos);
// Move pos to where the zip begins. - 1 to remove '\0', + 4 for two "\r\n".
zip_pos += sizeof(zip_content_type) + 3;
size_t zip_length = multipart.find(boundary, zip_pos);
ASSERT_NE(std::string::npos, zip_length);
// Calculate length, adjust for a "\r\n".
zip_length -= zip_pos + 2;
ASSERT_GT(zip_length, 0u);
multipart.erase(zip_pos, zip_length);
// Check the multipart contents.
std::vector<std::string> multipart_lines;
base::SplitStringUsingSubstr(multipart, "\r\n", &multipart_lines);
ASSERT_EQ(31, static_cast<int>(multipart_lines.size()));
EXPECT_STREQ(&boundary[0], multipart_lines[0].c_str());
EXPECT_STREQ("Content-Disposition: form-data; name=\"prod\"",
multipart_lines[1].c_str());
EXPECT_TRUE(multipart_lines[2].empty());
EXPECT_NE(std::string::npos, multipart_lines[3].find("Chrome"));
EXPECT_STREQ(&boundary[0], multipart_lines[4].c_str());
EXPECT_STREQ("Content-Disposition: form-data; name=\"ver\"",
multipart_lines[5].c_str());
EXPECT_TRUE(multipart_lines[6].empty());
// Just check that the version contains a dot.
EXPECT_NE(std::string::npos, multipart_lines[7].find('.'));
EXPECT_STREQ(&boundary[0], multipart_lines[8].c_str());
EXPECT_STREQ("Content-Disposition: form-data; name=\"guid\"",
multipart_lines[9].c_str());
EXPECT_TRUE(multipart_lines[10].empty());
EXPECT_STREQ("0", multipart_lines[11].c_str());
EXPECT_STREQ(&boundary[0], multipart_lines[12].c_str());
EXPECT_STREQ("Content-Disposition: form-data; name=\"type\"",
multipart_lines[13].c_str());
EXPECT_TRUE(multipart_lines[14].empty());
EXPECT_STREQ("webrtc_log", multipart_lines[15].c_str());
EXPECT_STREQ(&boundary[0], multipart_lines[16].c_str());
EXPECT_STREQ("Content-Disposition: form-data; name=\"app_session_id\"",
multipart_lines[17].c_str());
EXPECT_TRUE(multipart_lines[18].empty());
EXPECT_STREQ(kTestLoggingSessionId, multipart_lines[19].c_str());
EXPECT_STREQ(&boundary[0], multipart_lines[20].c_str());
EXPECT_STREQ("Content-Disposition: form-data; name=\"url\"",
multipart_lines[21].c_str());
EXPECT_TRUE(multipart_lines[22].empty());
EXPECT_NE(std::string::npos,
multipart_lines[23].find(&kMainWebrtcTestHtmlPage[0]));
EXPECT_STREQ(&boundary[0], multipart_lines[24].c_str());
EXPECT_STREQ("Content-Disposition: form-data; name=\"webrtc_log\";"
" filename=\"webrtc_log.gz\"",
multipart_lines[25].c_str());
EXPECT_STREQ("Content-Type: application/gzip",
multipart_lines[26].c_str());
EXPECT_TRUE(multipart_lines[27].empty());
EXPECT_TRUE(multipart_lines[28].empty()); // The removed zip part.
std::string final_delimiter = boundary;
final_delimiter += "--";
EXPECT_STREQ(final_delimiter.c_str(), multipart_lines[29].c_str());
EXPECT_TRUE(multipart_lines[30].empty());
}
|