1 // Copyright (c) 2015 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.
5 // This file contains duplicated functions in of
6 // content/test/data/media/webrtc_test_utilities.js
7 // TODO(phoglund): Eliminate this copy and rewrite the
8 // WebRtcBrowserTest.TestVgaReturnsTwoSimulcastStreams test to use the browser
9 // tests style instead.
11 // These must match with how the video and canvas tags are declared in html.
12 const VIDEO_TAG_WIDTH
= 320;
13 const VIDEO_TAG_HEIGHT
= 240;
15 // Number of test events to occur before the test pass. When the test pass,
16 // the function gAllEventsOccured is called.
17 var gNumberOfExpectedEvents
= 0;
19 // Number of events that currently have occurred.
20 var gNumberOfEvents
= 0;
22 var gAllEventsOccured = function () {};
24 // Use this function to set a function that will be called once all expected
25 // events has occurred.
26 function setAllEventsOccuredHandler(handler
) {
27 gAllEventsOccured
= handler
;
30 // See comments on waitForVideo.
31 function detectVideoIn(videoElementName
, callback
) {
32 var width
= VIDEO_TAG_WIDTH
;
33 var height
= VIDEO_TAG_HEIGHT
;
34 var videoElement
= $(videoElementName
);
35 var canvas
= $(videoElementName
+ '-canvas');
36 var waitVideo
= setInterval(function() {
37 var context
= canvas
.getContext('2d');
38 context
.drawImage(videoElement
, 0, 0, width
, height
);
39 var pixels
= context
.getImageData(0, 0, width
, height
).data
;
41 if (isVideoPlaying(pixels
, width
, height
)) {
42 clearInterval(waitVideo
);
49 * Blocks test success until the provided videoElement has playing video.
51 * @param videoElementName The id of the video element. There must also be a
52 * canvas somewhere in the DOM tree with the id |videoElementName|-canvas.
53 * @param callback The callback to call.
55 function waitForVideo(videoElement
) {
56 document
.title
= 'Waiting for video...';
58 detectVideoIn(videoElement
, function () { eventOccured(); });
62 * Blocks test success until the provided peerconnection reports the signaling
65 * @param peerConnection The peer connection to look at.
67 function waitForConnectionToStabilize(peerConnection
) {
69 var waitForStabilization
= setInterval(function() {
70 if (peerConnection
.signalingState
== 'stable') {
71 clearInterval(waitForStabilization
);
78 * Adds an expectation for an event to occur at some later point. You may call
79 * this several times per test, which will each add an expected event. Once all
80 * events have occurred, we'll call the "all events occurred" handler which will
81 * generally succeed the test or move the test to the next phase.
83 function addExpectedEvent() {
84 ++gNumberOfExpectedEvents
;
87 // See comment on addExpectedEvent.
88 function eventOccured() {
90 if (gNumberOfEvents
== gNumberOfExpectedEvents
) {
95 // This very basic video verification algorithm will be satisfied if any
96 // pixels are nonzero in a small sample area in the middle. It relies on the
97 // assumption that a video element with null source just presents zeroes.
98 function isVideoPlaying(pixels
, width
, height
) {
99 // Sample somewhere near the middle of the image.
100 var middle
= width
* height
/ 2;
101 for (var i
= 0; i
< 20; i
++) {
102 if (pixels
[middle
+ i
] > 0) {