Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / extensions / test / data / web_view / media_access / allow / embedder.js
blob48a841d972441aad5b8cc1327e5fab4e0f519de4
1 // Copyright 2013 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 var embedder = {};
6 embedder.tests = {};
7 // These variables will be filled in chrome.test.getConfig() below.
8 embedder.baseGuestURL = '';
9 embedder.guestURL = '';
11 // Sends a message to WebViewTest denoting it is done and test
12 // has failed.
13 embedder.failTest = function(msg) {
14 window.console.warn('test failure, reason: ' + msg);
15 chrome.test.sendMessage('TEST_FAILED');
18 // Sends a message to WebViewTest denoting it is done and test
19 // has succeeded.
20 embedder.maybePassTest = function() {
21 chrome.test.sendMessage('TEST_PASSED');
24 /** @private */
25 embedder.setUpGuest_ = function() {
26 document.querySelector('#webview-tag-container').innerHTML =
27 '<webview style="width: 100px; height: 100px;"' +
28 ' src="' + embedder.guestURL + '"' +
29 '></webview>';
30 var webview = document.querySelector('webview');
31 if (!webview) {
32 embedder.failTest('No <webview> element created');
33 return null;
35 return webview;
38 /** @private */
39 embedder.setUpLoadStop_ = function(webview, testName) {
40 var onWebViewLoadStop = function(e) {
41 // Send post message to <webview> when it's ready to receive them.
42 // This will make the guest start issueing media request. We do not
43 // worry about the Javascript outcome. MockWebContestsDelegate in
44 // WebViewTest will take care of that.
45 webview.contentWindow.postMessage(
46 JSON.stringify(['check-media-permission', '' + testName]), '*');
48 webview.addEventListener('loadstop', onWebViewLoadStop);
51 /** @private */
52 embedder.assertCorrectMediaEvent_ = function(e) {
53 if (e.permission != 'media') {
54 embedder.failTest('wrong permission: ' + e.permission);
55 return false;
57 if (!e.url || !e.request.url) {
58 embedder.failTest('No url property in event');
59 return false;
61 if (e.url.indexOf(embedder.baseGuestURL)) {
62 embedder.failTest('Wrong url: ' + e.url +
63 ', expected url to start with ' + embedder.baseGuestURL);
64 return false;
66 return true;
69 // Each test loads a guest which requests media access.
70 // All tests listed in this file expect the guest's request to be allowed.
72 // Once the guest is allowed media access, the MockWebContestsDelegate catches
73 // the fact and the test succeeds.
75 // Note that this is a manually run test, not using chrome.test.runTests.
76 // This is because we want to wait for MockWebContestsDelegate to catch media
77 // access request and not actually issue the media request.
79 embedder.tests.testAllow = function() {
80 var webview = embedder.setUpGuest_();
81 if (!webview) {
82 return;
85 var onPermissionRequest = function(e) {
86 if (!embedder.assertCorrectMediaEvent_(e)) {
87 return;
89 e.request.allow();
90 embedder.maybePassTest();
92 webview.addEventListener('permissionrequest', onPermissionRequest);
94 embedder.setUpLoadStop_(webview, 'test1');
97 embedder.tests.testAllowAndThenDeny = function() {
98 var webview = embedder.setUpGuest_();
99 if (!webview) {
100 return;
103 var calledAllow = false;
104 var callCount = 0;
105 var exceptionCount = 0;
106 var checkAndCall = function(e) {
107 if (!embedder.assertCorrectMediaEvent_(e)) {
108 return;
111 if (!calledAllow) {
112 e.request.allow();
113 calledAllow = true;
114 ++callCount
115 } else {
116 try {
117 e.request.deny();
118 } catch (exception) {
119 ++exceptionCount;
123 if (callCount == 1 && exceptionCount == 1) {
124 embedder.maybePassTest();
128 var onPermissionRequest1 = function(e) {
129 checkAndCall(e);
131 var onPermissionRequest2 = function(e) {
132 checkAndCall(e);
134 webview.addEventListener('permissionrequest', onPermissionRequest1);
135 webview.addEventListener('permissionrequest', onPermissionRequest2);
137 embedder.setUpLoadStop_(webview, 'test1');
140 embedder.tests.testAllowAsync = function() {
141 var webview = embedder.setUpGuest_();
142 if (!webview) {
143 return;
146 var onPermissionRequest = function(e) {
147 if (!embedder.assertCorrectMediaEvent_(e)) {
148 return;
151 e.preventDefault();
152 // Decide asynchronously.
153 window.setTimeout(function() {
154 e.request.allow();
155 embedder.maybePassTest();
156 }, 0);
158 webview.addEventListener('permissionrequest', onPermissionRequest);
160 embedder.setUpLoadStop_(webview, 'test1');
163 embedder.tests.testAllowTwice = function() {
164 var webview = embedder.setUpGuest_();
165 if (!webview) {
166 return;
169 var calledAllow = false;
170 var callbackCount = 0;
171 var exceptionCount = 0;
172 var checkAndCall = function(e) {
173 if (!embedder.assertCorrectMediaEvent_(e)) {
174 return;
177 ++callbackCount;
178 try {
179 e.request.allow();
180 } catch (exception) {
181 ++exceptionCount;
184 if (callbackCount == 2) {
185 if (exceptionCount == 1) {
186 embedder.maybePassTest();
187 } else {
188 embedder.failTest('Expected exceptionCount 1, but got ' +
189 exceptionCount);
194 var onPermissionRequest1 = function(e) {
195 checkAndCall(e);
197 var onPermissionRequest2 = function(e) {
198 checkAndCall(e);
200 webview.addEventListener('permissionrequest', onPermissionRequest1);
201 webview.addEventListener('permissionrequest', onPermissionRequest2);
203 embedder.setUpLoadStop_(webview, 'test1');
206 embedder.tests.list = {
207 'testAllow': embedder.tests.testAllow,
208 'testAllowAndThenDeny': embedder.tests.testAllowAndThenDeny,
209 'testAllowAsync': embedder.tests.testAllowAsync,
210 'testAllowTwice': embedder.tests.testAllowTwice
213 // Entry point for test, called by WebViewTest.
214 function runTest(testName) {
215 chrome.test.getConfig(function(config) {
216 embedder.baseGuestURL = 'http://localhost:' + config.testServer.port;
217 embedder.guestURL = embedder.baseGuestURL + '/media_access_guest.html';
218 chrome.test.log('Guest url is: ' + embedder.guestURL);
220 var testFunction = embedder.tests.list[testName];
221 if (!testFunction) {
222 embedder.failTest('No such test: ' + testName);
223 return;
225 testFunction();
229 onload = function() {
230 chrome.test.sendMessage('LAUNCHED');