Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / content / test / data / media / webrtc_test_utilities.js
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.
6 const VIDEO_TAG_WIDTH = 320;
7 const VIDEO_TAG_HEIGHT = 240;
8
9 // Fake video capture background green is of value 135.
10 const 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.
14 var gNumberOfExpectedEvents = 0;
15
16 // Number of events that currently have occurred.
17 var gNumberOfEvents = 0;
18
19 var gAllEventsOccured = function () {};
20
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;
25 }
26
27 // Tells the C++ code we succeeded, which will generally exit the test.
28 function reportTestSuccess() {
29   window.domAutomationController.send('OK');
30 }
31
32 // Returns a custom return value to the test.
33 function sendValueToTest(value) {
34   window.domAutomationController.send(value);
35 }
36
37 // Immediately fails the test on the C++ side.
38 function failTest(reason) {
39   var error = new Error(reason);
40   window.domAutomationController.send(error.stack);
41 }
42
43 function detectVideoPlaying(videoElementName, callback) {
44   detectVideo(videoElementName, isVideoPlaying, callback);
45 }
46
47 function detectVideoStopped(videoElementName, callback) {
48   detectVideo(videoElementName,
49               function (pixels, previous_pixels) {
50                 return !isVideoPlaying(pixels, previous_pixels);
51               },
52               callback);
53 }
54
55 function detectVideo(videoElementName, predicate, callback) {
56   console.log('Looking at video in element ' + videoElementName);
57
58   var width = VIDEO_TAG_WIDTH;
59   var height = VIDEO_TAG_HEIGHT;
60   var videoElement = $(videoElementName);
61   var canvas = $(videoElementName + '-canvas');
62   var oldPixels = [];
63   var waitVideo = setInterval(function() {
64     var context = canvas.getContext('2d');
65     context.drawImage(videoElement, 0, 0, width, height);
66     var pixels = context.getImageData(0, 0 , width, height / 3).data;
67     // Check that there is an old and a new picture with the same size to
68     // compare and use the function |predicate| to detect the video state in
69     // that case.
70     if (oldPixels.length == pixels.length &&
71         predicate(pixels, oldPixels)) {
72       console.log('Done looking at video in element ' + videoElementName);
73       clearInterval(waitVideo);
74       callback(videoElement.videoWidth, videoElement.videoHeight);
75     }
76     oldPixels = pixels;
77   }, 200);
78 }
79
80 function waitForVideoWithResolution(element, expected_width, expected_height) {
81   addExpectedEvent();
82   detectVideoPlaying(element,
83       function (width, height) {
84         assertEquals(expected_width, width);
85         assertEquals(expected_height, height);
86         eventOccured();
87       });
88 }
89
90 function waitForVideo(videoElement) {
91   addExpectedEvent();
92   detectVideoPlaying(videoElement, function () { eventOccured(); });
93 }
94
95 function waitForVideoToStop(videoElement) {
96   addExpectedEvent();
97   detectVideoStopped(videoElement, function () { eventOccured(); });
98 }
99
100 // Calculates the current frame rate and compares to |expected_frame_rate|
101 // |callback| is triggered with value |true| if the calculated frame rate
102 // is +-1 the expected or |false| if five calculations fail to match
103 // |expected_frame_rate|.
104 function validateFrameRate(videoElementName, expected_frame_rate, callback) {
105   var videoElement = $(videoElementName);
106   var startTime = new Date().getTime();
107   var decodedFrames = videoElement.webkitDecodedFrameCount;
108   var attempts = 0;
109
110   if (videoElement.readyState <= HTMLMediaElement.HAVE_CURRENT_DATA ||
111           videoElement.paused || videoElement.ended) {
112     failTest("getFrameRate - " + videoElementName + " is not plaing.");
113     return;
114   }
115
116   var waitVideo = setInterval(function() {
117     attempts++;
118     currentTime = new Date().getTime();
119     deltaTime = (currentTime - startTime) / 1000;
120     startTime = currentTime;
121
122     // Calculate decoded frames per sec.
123     var fps =
124         (videoElement.webkitDecodedFrameCount - decodedFrames) / deltaTime;
125     decodedFrames = videoElement.webkitDecodedFrameCount;
126
127     console.log('FrameRate in ' + videoElementName + ' is ' + fps);
128     if (fps < expected_frame_rate + 1  && fps > expected_frame_rate - 1) {
129       clearInterval(waitVideo);
130       callback(true);
131     } else if (attempts == 5) {
132       clearInterval(waitVideo);
133       callback(false);
134     }
135   }, 1000);
136 }
137
138 function waitForConnectionToStabilize(peerConnection, callback) {
139   peerConnection.onsignalingstatechange = function(event) {
140     if (peerConnection.signalingState == 'stable') {
141       peerConnection.onsignalingstatechange = null;
142       callback();
143     }
144   }
145 }
146
147 // Adds an expected event. You may call this function many times to add more
148 // expected events. Each expected event must later be matched by a call to
149 // eventOccurred. When enough events have occurred, the "all events occurred
150 // handler" will be called.
151 function addExpectedEvent() {
152   ++gNumberOfExpectedEvents;
153 }
154
155 // See addExpectedEvent.
156 function eventOccured() {
157   ++gNumberOfEvents;
158   if (gNumberOfEvents == gNumberOfExpectedEvents) {
159     gAllEventsOccured();
160   }
161 }
162
163 // This very basic video verification algorithm will be satisfied if any
164 // pixels are changed.
165 function isVideoPlaying(pixels, previousPixels) {
166   for (var i = 0; i < pixels.length; i++) {
167     if (pixels[i] != previousPixels[i]) {
168       return true;
169     }
170   }
171   return false;
172 }
173
174 // This function matches |left| and |right| and fails the test if the
175 // values don't match using normal javascript equality (i.e. the hard
176 // types of the operands aren't checked).
177 function assertEquals(expected, actual) {
178   if (actual != expected) {
179     failTest("expected '" + expected + "', got '" + actual + "'.");
180   }
181 }
182
183 function assertNotEquals(expected, actual) {
184   if (actual === expected) {
185     failTest("expected '" + expected + "', got '" + actual + "'.");
186   }
187 }
188