| OLD | NEW |
| (Empty) |
| 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 var util = {}; | |
| 6 | |
| 7 // Creates a <webview> tag in document.body and returns the reference to it. | |
| 8 // It also sets a dummy src. The dummy src is significant because this makes | |
| 9 // sure that the <object> shim is created (asynchronously at this point) for the | |
| 10 // <webview> tag. This makes the <webview> tag ready for add/removeEventListener | |
| 11 // calls. | |
| 12 util.createWebViewTagInDOM = function() { | |
| 13 var webview = document.createElement('webview'); | |
| 14 webview.style.width = '300px'; | |
| 15 webview.style.height = '200px'; | |
| 16 document.body.appendChild(webview); | |
| 17 var urlDummy = 'data:text/html,<body>Initial dummy guest</body>'; | |
| 18 webview.setAttribute('src', urlDummy); | |
| 19 return webview; | |
| 20 }; | |
| 21 | |
| 22 onload = function() { | |
| 23 chrome.test.runTests([ | |
| 24 function webView() { | |
| 25 var webview = document.querySelector('webview'); | |
| 26 // Since we can't currently inspect the page loaded inside the <webview>, | |
| 27 // the only way we can check that the shim is working is by changing the | |
| 28 // size and seeing if the shim updates the size of the DOM. | |
| 29 chrome.test.assertEq(300, webview.offsetWidth); | |
| 30 chrome.test.assertEq(200, webview.offsetHeight); | |
| 31 | |
| 32 webview.style.width = '310px'; | |
| 33 webview.style.height = '210px'; | |
| 34 | |
| 35 chrome.test.assertEq(310, webview.offsetWidth); | |
| 36 chrome.test.assertEq(210, webview.offsetHeight); | |
| 37 | |
| 38 webview.style.width = '320px'; | |
| 39 webview.style.height = '220px'; | |
| 40 | |
| 41 chrome.test.assertEq(320, webview.offsetWidth); | |
| 42 chrome.test.assertEq(220, webview.offsetHeight); | |
| 43 | |
| 44 var dynamicWebViewTag = document.createElement('webview'); | |
| 45 dynamicWebViewTag.setAttribute('src', 'data:text/html,dynamic browser'); | |
| 46 dynamicWebViewTag.style.width = '330px'; | |
| 47 dynamicWebViewTag.style.height = '230px'; | |
| 48 document.body.appendChild(dynamicWebViewTag); | |
| 49 | |
| 50 // Timeout is necessary to give the mutation observers a chance to fire. | |
| 51 setTimeout(function() { | |
| 52 chrome.test.assertEq(330, dynamicWebViewTag.offsetWidth); | |
| 53 chrome.test.assertEq(230, dynamicWebViewTag.offsetHeight); | |
| 54 | |
| 55 chrome.test.succeed(); | |
| 56 }, 0); | |
| 57 }, | |
| 58 | |
| 59 function webViewApiMethodExistence() { | |
| 60 var apiMethodsToCheck = [ | |
| 61 'back', | |
| 62 'canGoBack', | |
| 63 'canGoForward', | |
| 64 'forward', | |
| 65 'getProcessId', | |
| 66 'go', | |
| 67 'reload', | |
| 68 'stop', | |
| 69 'terminate' | |
| 70 ]; | |
| 71 var webview = document.createElement('webview'); | |
| 72 webview.addEventListener('loadstop', function(e) { | |
| 73 for (var i = 0; i < apiMethodsToCheck.length; ++i) { | |
| 74 chrome.test.assertEq('function', | |
| 75 typeof webview[apiMethodsToCheck[i]]); | |
| 76 } | |
| 77 | |
| 78 // Check contentWindow. | |
| 79 chrome.test.assertEq('object', typeof webview.contentWindow); | |
| 80 chrome.test.assertEq('function', | |
| 81 typeof webview.contentWindow.postMessage); | |
| 82 | |
| 83 chrome.test.succeed(); | |
| 84 }); | |
| 85 webview.setAttribute('src', 'data:text/html,webview check api'); | |
| 86 document.body.appendChild(webview); | |
| 87 }, | |
| 88 | |
| 89 function webViewEventName() { | |
| 90 var webview = document.createElement('webview'); | |
| 91 webview.setAttribute('src', 'data:text/html,webview check api'); | |
| 92 document.body.appendChild(webview); | |
| 93 | |
| 94 webview.addEventListener('loadstart', function(evt) { | |
| 95 chrome.test.assertEq('loadstart', evt.type); | |
| 96 }); | |
| 97 | |
| 98 webview.addEventListener('loadstop', function(evt) { | |
| 99 chrome.test.assertEq('loadstop', evt.type); | |
| 100 webview.terminate(); | |
| 101 }); | |
| 102 | |
| 103 webview.addEventListener('exit', function(evt) { | |
| 104 chrome.test.assertEq('exit', evt.type); | |
| 105 chrome.test.succeed(); | |
| 106 }); | |
| 107 | |
| 108 webview.setAttribute('src', 'data:text/html,trigger navigation'); | |
| 109 }, | |
| 110 | |
| 111 // This test registers two listeners on an event (loadcommit) and removes | |
| 112 // the <webview> tag when the first listener fires. | |
| 113 // Current expected behavior is that the second event listener will still | |
| 114 // fire without crashing. | |
| 115 function webviewDestroyOnEventListener() { | |
| 116 var webview = util.createWebViewTagInDOM(); | |
| 117 var url = 'data:text/html,<body>Destroy test</body>'; | |
| 118 | |
| 119 var loadCommitCount = 0; | |
| 120 function loadCommitCommon(e) { | |
| 121 chrome.test.assertEq('loadcommit', e.type); | |
| 122 if (url != e.url) | |
| 123 return; | |
| 124 ++loadCommitCount; | |
| 125 if (loadCommitCount == 1) { | |
| 126 webview.parentNode.removeChild(webview); | |
| 127 webview = null; | |
| 128 setTimeout(function() { | |
| 129 chrome.test.succeed(); | |
| 130 }, 0); | |
| 131 } else if (loadCommitCount > 2) { | |
| 132 chrome.test.fail(); | |
| 133 } | |
| 134 }; | |
| 135 | |
| 136 // The test starts from here, by setting the src to |url|. | |
| 137 webview.addEventListener('loadcommit', function(e) { | |
| 138 loadCommitCommon(e); | |
| 139 }); | |
| 140 webview.addEventListener('loadcommit', function(e) { | |
| 141 loadCommitCommon(e); | |
| 142 }); | |
| 143 webview.setAttribute('src', url); | |
| 144 }, | |
| 145 | |
| 146 // This test registers two event listeners on a same event (loadcommit). | |
| 147 // Each of the listener tries to change some properties on the event param, | |
| 148 // which should not be possible. | |
| 149 function cannotMutateEventName() { | |
| 150 var webview = util.createWebViewTagInDOM(); | |
| 151 var url = 'data:text/html,<body>Two</body>'; | |
| 152 | |
| 153 var loadCommitACalled = false; | |
| 154 var loadCommitBCalled = false; | |
| 155 | |
| 156 var maybeFinishTest = function(e) { | |
| 157 if (loadCommitACalled && loadCommitBCalled) { | |
| 158 chrome.test.assertEq('loadcommit', e.type); | |
| 159 chrome.test.succeed(); | |
| 160 } | |
| 161 }; | |
| 162 | |
| 163 var onLoadCommitA = function(e) { | |
| 164 if (e.url == url) { | |
| 165 chrome.test.assertEq('loadcommit', e.type); | |
| 166 chrome.test.assertTrue(e.isTopLevel); | |
| 167 chrome.test.assertFalse(loadCommitACalled); | |
| 168 loadCommitACalled = true; | |
| 169 // Try mucking with properities inside |e|. | |
| 170 e.type = 'modified'; | |
| 171 maybeFinishTest(e); | |
| 172 } | |
| 173 }; | |
| 174 var onLoadCommitB = function(e) { | |
| 175 if (e.url == url) { | |
| 176 chrome.test.assertEq('loadcommit', e.type); | |
| 177 chrome.test.assertTrue(e.isTopLevel); | |
| 178 chrome.test.assertFalse(loadCommitBCalled); | |
| 179 loadCommitBCalled = true; | |
| 180 // Try mucking with properities inside |e|. | |
| 181 e.type = 'modified'; | |
| 182 maybeFinishTest(e); | |
| 183 } | |
| 184 }; | |
| 185 | |
| 186 // The test starts from here, by setting the src to |url|. Event | |
| 187 // listener registration works because we already have a (dummy) src set | |
| 188 // on the <webview> tag. | |
| 189 webview.addEventListener('loadcommit', onLoadCommitA); | |
| 190 webview.addEventListener('loadcommit', onLoadCommitB); | |
| 191 webview.setAttribute('src', url); | |
| 192 }, | |
| 193 | |
| 194 // This test verifies that setting the partition attribute after the src has | |
| 195 // been set raises an exception. | |
| 196 function partitionRaisesException() { | |
| 197 var webview = document.createElement('webview'); | |
| 198 webview.setAttribute('src', 'data:text/html,trigger navigation'); | |
| 199 document.body.appendChild(webview); | |
| 200 setTimeout(function() { | |
| 201 try { | |
| 202 webview.partition = 'illegal'; | |
| 203 chrome.test.fail(); | |
| 204 } catch (e) { | |
| 205 chrome.test.succeed(); | |
| 206 } | |
| 207 }, 0); | |
| 208 }, | |
| 209 | |
| 210 function webViewExecuteScript() { | |
| 211 var webview = document.createElement('webview'); | |
| 212 webview.addEventListener('loadstop', function() { | |
| 213 webview.executeScript( | |
| 214 {code:'document.body.style.backgroundColor = "red";'}, | |
| 215 function(results) { | |
| 216 chrome.test.assertEq(1, results.length); | |
| 217 chrome.test.assertEq('red', results[0]); | |
| 218 chrome.test.succeed(); | |
| 219 }); | |
| 220 }); | |
| 221 webview.setAttribute('src', 'data:text/html,trigger navigation'); | |
| 222 document.body.appendChild(webview); | |
| 223 } | |
| 224 ]); | |
| 225 }; | |
| OLD | NEW |