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// These must match with how the video and canvas tags are declared in html. 6const VIDEO_TAG_WIDTH = 320; 7const VIDEO_TAG_HEIGHT = 240; 8 9// Fake video capture background green is of value 135. 10const COLOR_BACKGROUND_GREEN = 135; 11 12// Number of test events to occur before the test pass. When the test pass, 13// the function gAllEventsOccured is called. 14var gNumberOfExpectedEvents = 0; 15 16// Number of events that currently have occurred. 17var gNumberOfEvents = 0; 18 19var gAllEventsOccured = function () {}; 20 21// Use this function to set a function that will be called once all expected 22// events has occurred. 23function setAllEventsOccuredHandler(handler) { 24 gAllEventsOccured = handler; 25} 26 27// Tells the C++ code we succeeded, which will generally exit the test. 28function reportTestSuccess() { 29 window.domAutomationController.send('OK'); 30} 31 32// Returns a custom return value to the test. 33function sendValueToTest(value) { 34 window.domAutomationController.send(value); 35} 36 37// Immediately fails the test on the C++ side and throw an exception to 38// stop execution on the javascript side. 39function failTest(reason) { 40 var error = new Error(reason); 41 window.domAutomationController.send(error.stack); 42} 43 44function detectVideoPlaying(videoElementName, callback) { 45 detectVideo(videoElementName, isVideoPlaying, callback); 46} 47 48function detectVideoStopped(videoElementName, callback) { 49 detectVideo(videoElementName, 50 function (pixels, previous_pixels) { 51 return !isVideoPlaying(pixels, previous_pixels); 52 }, 53 callback); 54} 55 56function detectVideo(videoElementName, predicate, callback) { 57 console.log('Looking at video in element ' + videoElementName); 58 59 var width = VIDEO_TAG_WIDTH; 60 var height = VIDEO_TAG_HEIGHT; 61 var videoElement = $(videoElementName); 62 var canvas = $(videoElementName + '-canvas'); 63 var oldPixels = []; 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 70 // that case. 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); 76 } 77 oldPixels = pixels; 78 }, 200); 79} 80 81function waitForVideoWithResolution(element, expected_width, expected_height) { 82 addExpectedEvent(); 83 detectVideoPlaying(element, 84 function (width, height) { 85 assertEquals(expected_width, width); 86 assertEquals(expected_height, height); 87 eventOccured(); 88 }); 89} 90 91function waitForVideo(videoElement) { 92 addExpectedEvent(); 93 detectVideoPlaying(videoElement, function () { eventOccured(); }); 94} 95 96function waitForVideoToStop(videoElement) { 97 addExpectedEvent(); 98 detectVideoStopped(videoElement, function () { eventOccured(); }); 99} 100 101function waitForConnectionToStabilize(peerConnection, callback) { 102 peerConnection.onsignalingstatechange = function(event) { 103 if (peerConnection.signalingState == 'stable') { 104 peerConnection.onsignalingstatechange = null; 105 callback(); 106 } 107 } 108} 109 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. 114function addExpectedEvent() { 115 ++gNumberOfExpectedEvents; 116} 117 118// See addExpectedEvent. 119function eventOccured() { 120 ++gNumberOfEvents; 121 if (gNumberOfEvents == gNumberOfExpectedEvents) { 122 gAllEventsOccured(); 123 } 124} 125 126// This very basic video verification algorithm will be satisfied if any 127// pixels are changed. 128function isVideoPlaying(pixels, previousPixels) { 129 for (var i = 0; i < pixels.length; i++) { 130 if (pixels[i] != previousPixels[i]) { 131 return true; 132 } 133 } 134 return false; 135} 136 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). 140function assertEquals(expected, actual) { 141 if (actual != expected) { 142 failTest("expected '" + expected + "', got '" + actual + "'."); 143 } 144} 145 146function assertNotEquals(expected, actual) { 147 if (actual === expected) { 148 failTest("expected '" + expected + "', got '" + actual + "'."); 149 } 150} 151 152