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.
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
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
20 embedder
.maybePassTest = function() {
21 chrome
.test
.sendMessage('TEST_PASSED');
25 embedder
.setUpGuest_ = function() {
26 document
.querySelector('#webview-tag-container').innerHTML
=
27 '<webview style="width: 100px; height: 100px;"' +
28 ' src="' + embedder
.guestURL
+ '"' +
30 var webview
= document
.querySelector('webview');
32 embedder
.failTest('No <webview> element created');
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
);
52 embedder
.assertCorrectMediaEvent_ = function(e
) {
53 if (e
.permission
!= 'media') {
54 embedder
.failTest('wrong permission: ' + e
.permission
);
57 if (!e
.url
|| !e
.request
.url
) {
58 embedder
.failTest('No url property in event');
61 if (e
.url
.indexOf(embedder
.baseGuestURL
)) {
62 embedder
.failTest('Wrong url: ' + e
.url
+
63 ', expected url to start with ' + embedder
.baseGuestURL
);
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_();
85 var onPermissionRequest = function(e
) {
86 if (!embedder
.assertCorrectMediaEvent_(e
)) {
90 embedder
.maybePassTest();
92 webview
.addEventListener('permissionrequest', onPermissionRequest
);
94 embedder
.setUpLoadStop_(webview
, 'test1');
97 embedder
.tests
.testAllowAndThenDeny = function() {
98 var webview
= embedder
.setUpGuest_();
103 var calledAllow
= false;
105 var exceptionCount
= 0;
106 var checkAndCall = function(e
) {
107 if (!embedder
.assertCorrectMediaEvent_(e
)) {
118 } catch (exception
) {
123 if (callCount
== 1 && exceptionCount
== 1) {
124 embedder
.maybePassTest();
128 var onPermissionRequest1 = function(e
) {
131 var onPermissionRequest2 = function(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_();
146 var onPermissionRequest = function(e
) {
147 if (!embedder
.assertCorrectMediaEvent_(e
)) {
152 // Decide asynchronously.
153 window
.setTimeout(function() {
155 embedder
.maybePassTest();
158 webview
.addEventListener('permissionrequest', onPermissionRequest
);
160 embedder
.setUpLoadStop_(webview
, 'test1');
163 embedder
.tests
.testAllowTwice = function() {
164 var webview
= embedder
.setUpGuest_();
169 var calledAllow
= false;
170 var callbackCount
= 0;
171 var exceptionCount
= 0;
172 var checkAndCall = function(e
) {
173 if (!embedder
.assertCorrectMediaEvent_(e
)) {
180 } catch (exception
) {
184 if (callbackCount
== 2) {
185 if (exceptionCount
== 1) {
186 embedder
.maybePassTest();
188 embedder
.failTest('Expected exceptionCount 1, but got ' +
194 var onPermissionRequest1 = function(e
) {
197 var onPermissionRequest2 = function(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
];
222 embedder
.failTest('No such test: ' + testName
);
229 onload = function() {
230 chrome
.test
.sendMessage('LAUNCHED');