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.
5 // These must match with how the video and canvas tags are declared in html.
6 const VIDEO_TAG_WIDTH = 320;
7 const VIDEO_TAG_HEIGHT = 240;
9 // Fake video capture background green is of value 135.
10 const COLOR_BACKGROUND_GREEN = 135;
12 // Number of test events to occur before the test pass. When the test pass,
13 // the function gAllEventsOccured is called.
14 var gNumberOfExpectedEvents = 0;
16 // Number of events that currently have occurred.
17 var gNumberOfEvents = 0;
19 var gAllEventsOccured = function () {};
21 // Use this function to set a function that will be called once all expected
22 // events has occurred.
23 function setAllEventsOccuredHandler(handler) {
24 gAllEventsOccured = handler;
27 // Tells the C++ code we succeeded, which will generally exit the test.
28 function reportTestSuccess() {
29 window.domAutomationController.send('OK');
32 // Returns a custom return value to the test.
33 function sendValueToTest(value) {
34 window.domAutomationController.send(value);
37 // Immediately fails the test on the C++ side and throw an exception to
38 // stop execution on the javascript side.
39 function failTest(reason) {
40 var error = new Error(reason);
41 window.domAutomationController.send(error.stack);
44 function detectVideoPlaying(videoElementName, callback) {
45 detectVideo(videoElementName, isVideoPlaying, callback);
48 function detectVideoStopped(videoElementName, callback) {
49 detectVideo(videoElementName,
50 function (pixels, previous_pixels) {
51 return !isVideoPlaying(pixels, previous_pixels);
56 function detectVideo(videoElementName, predicate, callback) {
57 console.log('Looking at video in element ' + videoElementName);
59 var width = VIDEO_TAG_WIDTH;
60 var height = VIDEO_TAG_HEIGHT;
61 var videoElement = $(videoElementName);
62 var canvas = $(videoElementName + '-canvas');
64 var waitVideo = setInterval(function() {
65 var context = canvas.getContext('2d');
66 context.drawImage(videoElement, 0, 0, width, height);
67 var pixels = context.getImageData(0, 0 , width, height / 3).data;
68 // Check that there is an old and a new picture with the same size to
69 // compare and use the function |predicate| to detect the video state in
71 if (oldPixels.length == pixels.length &&
72 predicate(pixels, oldPixels)) {
73 console.log('Done looking at video in element ' + videoElementName);
74 clearInterval(waitVideo);
75 callback(videoElement.videoWidth, videoElement.videoHeight);
81 function waitForVideoWithResolution(element, expected_width, expected_height) {
83 detectVideoPlaying(element,
84 function (width, height) {
85 assertEquals(expected_width, width);
86 assertEquals(expected_height, height);
91 function waitForVideo(videoElement) {
93 detectVideoPlaying(videoElement, function () { eventOccured(); });
96 function waitForVideoToStop(videoElement) {
98 detectVideoStopped(videoElement, function () { eventOccured(); });
101 function waitForConnectionToStabilize(peerConnection, callback) {
102 peerConnection.onsignalingstatechange = function(event) {
103 if (peerConnection.signalingState == 'stable') {
104 peerConnection.onsignalingstatechange = null;
110 // Adds an expected event. You may call this function many times to add more
111 // expected events. Each expected event must later be matched by a call to
112 // eventOccurred. When enough events have occurred, the "all events occurred
113 // handler" will be called.
114 function addExpectedEvent() {
115 ++gNumberOfExpectedEvents;
118 // See addExpectedEvent.
119 function eventOccured() {
121 if (gNumberOfEvents == gNumberOfExpectedEvents) {
126 // This very basic video verification algorithm will be satisfied if any
127 // pixels are changed.
128 function isVideoPlaying(pixels, previousPixels) {
129 for (var i = 0; i < pixels.length; i++) {
130 if (pixels[i] != previousPixels[i]) {
137 // This function matches |left| and |right| and fails the test if the
138 // values don't match using normal javascript equality (i.e. the hard
139 // types of the operands aren't checked).
140 function assertEquals(expected, actual) {
141 if (actual != expected) {
142 failTest("expected '" + expected + "', got '" + actual + "'.");
146 function assertNotEquals(expected, actual) {
147 if (actual === expected) {
148 failTest("expected '" + expected + "', got '" + actual + "'.");