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 embedder
.baseGuestURL
= '';
8 embedder
.guestURL
= '';
10 window
.runTest = function(testName
) {
11 if (!embedder
.test
.testList
[testName
]) {
12 console
.log('Incorrect testName: ' + testName
);
18 embedder
.test
.testList
[testName
]();
20 // window.* exported functions end.
23 embedder
.setUpGuest_ = function() {
24 document
.querySelector('#webview-tag-container').innerHTML
=
25 '<webview style="width: 100px; height: 100px;"></webview>';
26 var webview
= document
.querySelector('webview');
28 embedder
.test
.fail('No <webview> element created');
33 embedder
.getHTMLForGuestWithTitle_ = function(title
) {
36 '<html><head><title>%s</title></head>' +
37 '<body>hello world</body>' +
39 return html
.replace('%s', title
);
43 embedder
.test
.succeed = function() {
44 chrome
.test
.sendMessage('TEST_PASSED');
47 embedder
.test
.fail = function() {
48 chrome
.test
.sendMessage('TEST_FAILED');
51 embedder
.test
.assertEq = function(a
, b
) {
53 console
.log('assertion failed: ' + a
+ ' != ' + b
);
58 embedder
.test
.assertTrue = function(condition
) {
60 console
.log('assertion failed: true != ' + condition
);
65 embedder
.test
.assertFalse = function(condition
) {
67 console
.log('assertion failed: false != ' + condition
);
72 function setUpDialogTest(messageCallback
, dialogHandler
) {
73 var guestUrl
= 'data:text/html,guest';
74 var webview
= document
.createElement('webview');
76 var onLoadStop = function(e
) {
77 console
.log('webview has loaded.');
78 webview
.executeScript(
79 {file
: 'inject_dialog.js'},
81 console
.log('Script has been injected into webview.');
82 // Establish a communication channel with the guest.
83 var msg
= ['connect'];
84 webview
.contentWindow
.postMessage(JSON
.stringify(msg
), '*');
87 webview
.addEventListener('loadstop', onLoadStop
);
89 window
.addEventListener('message', function(e
) {
90 var data
= JSON
.parse(e
.data
);
91 if (data
[0] == 'connected') {
92 console
.log('A communication channel has been established with webview.');
95 messageCallback(webview
, data
);
98 webview
.addEventListener('dialog', function(e
) {
102 webview
.setAttribute('src', guestUrl
);
103 document
.body
.appendChild(webview
);
108 function testDialogAlert() {
109 var messageText
= '1337h@x0r';
111 var messageCallback = function(webview
, data
) {
112 if (data
[0] == 'connected') {
113 console
.log('The alert dialog test has started.');
114 var msg
= ['start-alert-dialog-test', messageText
];
115 webview
.contentWindow
.postMessage(JSON
.stringify(msg
), '*');
119 if (data
[0] == 'alert-dialog-done') {
121 'webview has been unblocked after requesting an alert dialog.');
122 embedder
.test
.succeed();
127 var dialogHandler = function(e
) {
128 console
.log('webview has requested a dialog.');
129 embedder
.test
.assertEq('alert', e
.messageType
);
130 embedder
.test
.assertEq(messageText
, e
.messageText
);
132 console
.log('The app has responded to the dialog request.');
135 setUpDialogTest(messageCallback
, dialogHandler
);
138 function testDialogConfirm() {
139 var messageText
= 'foobar';
141 var messageCallback = function(webview
, data
) {
142 if (data
[0] == 'connected') {
143 console
.log('The confirm dialog test has started.');
144 var msg
= ['start-confirm-dialog-test', messageText
];
145 webview
.contentWindow
.postMessage(JSON
.stringify(msg
), '*');
149 if (data
[0] == 'confirm-dialog-result') {
150 console
.log('webview has reported a result for its confirm dialog.');
151 embedder
.test
.assertEq(true, data
[1]);
152 embedder
.test
.succeed();
157 var dialogHandler = function(e
) {
158 console
.log('webview has requested a dialog.');
159 embedder
.test
.assertEq('confirm', e
.messageType
);
160 embedder
.test
.assertEq(messageText
, e
.messageText
);
162 console
.log('The app has responded to the dialog request.');
165 setUpDialogTest(messageCallback
, dialogHandler
);
168 function testDialogConfirmCancel() {
169 var messageText
= 'foobar';
171 var messageCallback = function(webview
, data
) {
172 if (data
[0] == 'connected') {
173 console
.log('The confirm dialog test has started.');
174 var msg
= ['start-confirm-dialog-test', messageText
];
175 webview
.contentWindow
.postMessage(JSON
.stringify(msg
), '*');
179 if (data
[0] == 'confirm-dialog-result') {
180 console
.log('webview has reported a result for its confirm dialog.');
181 embedder
.test
.assertEq(false, data
[1]);
182 embedder
.test
.succeed();
187 var dialogHandler = function(e
) {
188 console
.log('webview has requested a dialog.');
189 embedder
.test
.assertEq('confirm', e
.messageType
);
190 embedder
.test
.assertEq(messageText
, e
.messageText
);
192 console
.log('The app has responded to the dialog request.');
195 setUpDialogTest(messageCallback
, dialogHandler
);
198 function testDialogConfirmDefaultCancel() {
199 var messageText
= 'foobar';
201 var messageCallback = function(webview
, data
) {
202 if (data
[0] == 'connected') {
203 console
.log('The confirm dialog test has started.');
204 var msg
= ['start-confirm-dialog-test', messageText
];
205 webview
.contentWindow
.postMessage(JSON
.stringify(msg
), '*');
209 if (data
[0] == 'confirm-dialog-result') {
210 console
.log('webview has reported a result for its confirm dialog.');
211 embedder
.test
.assertEq(false, data
[1]);
212 embedder
.test
.succeed();
217 var dialogHandler = function(e
) {
218 console
.log('webview has requested a dialog.');
219 embedder
.test
.assertEq('confirm', e
.messageType
);
220 embedder
.test
.assertEq(messageText
, e
.messageText
);
223 setUpDialogTest(messageCallback
, dialogHandler
);
226 function testDialogConfirmDefaultGCCancel() {
227 var messageText
= 'foobar';
229 var messageCallback = function(webview
, data
) {
230 if (data
[0] == 'connected') {
231 console
.log('The confirm dialog test has started.');
232 var msg
= ['start-confirm-dialog-test', messageText
];
233 webview
.contentWindow
.postMessage(JSON
.stringify(msg
), '*');
237 if (data
[0] == 'confirm-dialog-result') {
238 console
.log('webview has reported a result for its confirm dialog.');
239 embedder
.test
.assertEq(false, data
[1]);
240 embedder
.test
.succeed();
245 var dialogHandler = function(e
) {
246 console
.log('webview has requested a dialog.');
247 embedder
.test
.assertEq('confirm', e
.messageType
);
248 embedder
.test
.assertEq(messageText
, e
.messageText
);
249 // Prevent default to leave cleanup in the GC's hands.
251 window
.setTimeout(function() { window
.gc(); });
254 setUpDialogTest(messageCallback
, dialogHandler
);
257 function testDialogPrompt() {
258 var messageText
= 'bleep';
259 var defaultPromptText
= 'bloop';
260 var returnPromptText
= 'blah';
262 var messageCallback = function(webview
, data
) {
263 if (data
[0] == 'connected') {
264 console
.log('The prompt dialog test has started.');
265 var msg
= ['start-prompt-dialog-test', messageText
, defaultPromptText
];
266 webview
.contentWindow
.postMessage(JSON
.stringify(msg
), '*');
270 if (data
[0] == 'prompt-dialog-result') {
271 console
.log('webview has reported a result for its prompt dialog.');
272 embedder
.test
.assertEq(returnPromptText
, data
[1]);
273 embedder
.test
.succeed();
278 var dialogHandler = function(e
) {
279 console
.log('webview has requested a dialog.');
280 embedder
.test
.assertEq('prompt', e
.messageType
);
281 embedder
.test
.assertEq(messageText
, e
.messageText
);
282 embedder
.test
.assertEq(defaultPromptText
, e
.defaultPromptText
);
283 e
.dialog
.ok(returnPromptText
);
284 console
.log('The app has responded to the dialog request.');
287 setUpDialogTest(messageCallback
, dialogHandler
);
290 embedder
.test
.testList
= {
291 'testDialogAlert': testDialogAlert
,
292 'testDialogConfirm': testDialogConfirm
,
293 'testDialogConfirmCancel': testDialogConfirmCancel
,
294 'testDialogConfirmDefaultCancel': testDialogConfirmDefaultCancel
,
295 'testDialogConfirmDefaultGCCancel': testDialogConfirmDefaultGCCancel
,
296 'testDialogPrompt': testDialogPrompt
299 onload = function() {
300 chrome
.test
.getConfig(function(config
) {
301 chrome
.test
.sendMessage('LAUNCHED');