OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 var embedder = {}; |
| 6 embedder.tests = {}; |
| 7 embedder.baseGuestURL = ''; |
| 8 embedder.guestURL = ''; |
| 9 |
| 10 window.runTest = function(testName) { |
| 11 if (!embedder.test.testList[testName]) { |
| 12 console.log('Incorrect testName: ' + testName); |
| 13 embedder.test.fail(); |
| 14 return; |
| 15 } |
| 16 |
| 17 // Run the test. |
| 18 embedder.test.testList[testName](); |
| 19 }; |
| 20 // window.* exported functions end. |
| 21 |
| 22 /** @private */ |
| 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'); |
| 27 if (!webview) { |
| 28 embedder.test.fail('No <webview> element created'); |
| 29 } |
| 30 return webview; |
| 31 }; |
| 32 |
| 33 embedder.getHTMLForGuestWithTitle_ = function(title) { |
| 34 var html = |
| 35 'data:text/html,' + |
| 36 '<html><head><title>%s</title></head>' + |
| 37 '<body>hello world</body>' + |
| 38 '</html>'; |
| 39 return html.replace('%s', title); |
| 40 }; |
| 41 |
| 42 embedder.test = {}; |
| 43 embedder.test.succeed = function() { |
| 44 chrome.test.sendMessage('DoneDialogTest.PASSED'); |
| 45 }; |
| 46 |
| 47 embedder.test.fail = function() { |
| 48 chrome.test.sendMessage('DoneDialogTest.FAILED'); |
| 49 }; |
| 50 |
| 51 embedder.test.assertEq = function(a, b) { |
| 52 if (a != b) { |
| 53 console.log('assertion failed: ' + a + ' != ' + b); |
| 54 embedder.test.fail(); |
| 55 } |
| 56 }; |
| 57 |
| 58 embedder.test.assertTrue = function(condition) { |
| 59 if (!condition) { |
| 60 console.log('assertion failed: true != ' + condition); |
| 61 embedder.test.fail(); |
| 62 } |
| 63 }; |
| 64 |
| 65 embedder.test.assertFalse = function(condition) { |
| 66 if (condition) { |
| 67 console.log('assertion failed: false != ' + condition); |
| 68 embedder.test.fail(); |
| 69 } |
| 70 }; |
| 71 |
| 72 function setupDialogTest(messageCallback, dialogHandler) { |
| 73 var guestUrl = 'data:text/html,guest'; |
| 74 var webview = document.createElement('webview'); |
| 75 |
| 76 var onLoadStop = function(e) { |
| 77 console.log('webview has loaded.'); |
| 78 webview.executeScript( |
| 79 {file: 'inject_dialog.js'}, |
| 80 function(results) { |
| 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), '*'); |
| 85 }); |
| 86 }; |
| 87 webview.addEventListener('loadstop', onLoadStop); |
| 88 |
| 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.'); |
| 93 } |
| 94 |
| 95 messageCallback(webview, data); |
| 96 }); |
| 97 |
| 98 webview.addEventListener('dialog', function(e) { |
| 99 dialogHandler(e); |
| 100 }); |
| 101 |
| 102 webview.setAttribute('src', guestUrl); |
| 103 document.body.appendChild(webview); |
| 104 } |
| 105 |
| 106 // Tests begin. |
| 107 |
| 108 function testAlertDialog() { |
| 109 var messageText = '1337h@x0r'; |
| 110 |
| 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), '*'); |
| 116 return; |
| 117 } |
| 118 |
| 119 if (data[0] == 'alert-dialog-requested') { |
| 120 console.log( |
| 121 'webview has been unblocked after requesting an alert dialog.'); |
| 122 embedder.test.succeed(); |
| 123 return; |
| 124 } |
| 125 }; |
| 126 |
| 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); |
| 131 e.dialog.ok(); |
| 132 console.log('The app has responded to the dialog request.'); |
| 133 }; |
| 134 |
| 135 setupDialogTest(messageCallback, dialogHandler); |
| 136 } |
| 137 |
| 138 function testConfirmDialog() { |
| 139 var messageText = 'foobar'; |
| 140 |
| 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), '*'); |
| 146 return; |
| 147 } |
| 148 |
| 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(); |
| 153 return; |
| 154 } |
| 155 }; |
| 156 |
| 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); |
| 161 e.dialog.ok(); |
| 162 console.log('The app has responded to the dialog request.'); |
| 163 }; |
| 164 |
| 165 setupDialogTest(messageCallback, dialogHandler); |
| 166 } |
| 167 |
| 168 function testConfirmDialogCancel() { |
| 169 var messageText = 'foobar'; |
| 170 |
| 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), '*'); |
| 176 return; |
| 177 } |
| 178 |
| 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(); |
| 183 return; |
| 184 } |
| 185 }; |
| 186 |
| 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); |
| 191 e.dialog.cancel(); |
| 192 console.log('The app has responded to the dialog request.'); |
| 193 }; |
| 194 |
| 195 setupDialogTest(messageCallback, dialogHandler); |
| 196 } |
| 197 |
| 198 function testConfirmDialogDefaultCancel() { |
| 199 var messageText = 'foobar'; |
| 200 |
| 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), '*'); |
| 206 return; |
| 207 } |
| 208 |
| 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(); |
| 213 return; |
| 214 } |
| 215 }; |
| 216 |
| 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); |
| 221 }; |
| 222 |
| 223 setupDialogTest(messageCallback, dialogHandler); |
| 224 } |
| 225 |
| 226 function testConfirmDialogDefaultGCCancel() { |
| 227 var messageText = 'foobar'; |
| 228 |
| 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), '*'); |
| 234 return; |
| 235 } |
| 236 |
| 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(); |
| 241 return; |
| 242 } |
| 243 }; |
| 244 |
| 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. |
| 250 e.preventDefault(); |
| 251 }; |
| 252 |
| 253 setupDialogTest(messageCallback, dialogHandler); |
| 254 } |
| 255 |
| 256 function testPromptDialog() { |
| 257 var messageText = 'bleep'; |
| 258 var defaultPromptText = 'bloop'; |
| 259 var returnPromptText = 'blah'; |
| 260 |
| 261 var messageCallback = function(webview, data) { |
| 262 if (data[0] == 'connected') { |
| 263 console.log('The prompt dialog test has started.'); |
| 264 var msg = ['start-prompt-dialog-test', messageText, defaultPromptText]; |
| 265 webview.contentWindow.postMessage(JSON.stringify(msg), '*'); |
| 266 return; |
| 267 } |
| 268 |
| 269 if (data[0] == 'prompt-dialog-result') { |
| 270 console.log('webview has reported a result for its prompt dialog.'); |
| 271 embedder.test.assertEq(returnPromptText, data[1]); |
| 272 embedder.test.succeed(); |
| 273 return; |
| 274 } |
| 275 }; |
| 276 |
| 277 var dialogHandler = function(e) { |
| 278 console.log('webview has requested a dialog.'); |
| 279 embedder.test.assertEq('prompt', e.messageType); |
| 280 embedder.test.assertEq(messageText, e.messageText); |
| 281 embedder.test.assertEq(defaultPromptText, e.defaultPromptText); |
| 282 e.dialog.ok(returnPromptText); |
| 283 console.log('The app has responded to the dialog request.'); |
| 284 }; |
| 285 |
| 286 setupDialogTest(messageCallback, dialogHandler); |
| 287 } |
| 288 |
| 289 embedder.test.testList = { |
| 290 'testAlertDialog': testAlertDialog, |
| 291 'testConfirmDialog': testConfirmDialog, |
| 292 'testConfirmDialogDefaultCancel': testConfirmDialogDefaultCancel, |
| 293 'testConfirmDialogDefaultGCCancel': testConfirmDialogDefaultGCCancel, |
| 294 'testConfirmDialogCancel': testConfirmDialogCancel, |
| 295 'testPromptDialog': testPromptDialog |
| 296 }; |
| 297 |
| 298 onload = function() { |
| 299 chrome.test.getConfig(function(config) { |
| 300 chrome.test.sendMessage("Launched"); |
| 301 }); |
| 302 }; |
OLD | NEW |