| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 var assertEq = chrome.test.assertEq; | 5 var assertEq = chrome.test.assertEq; |
| 6 var assertTrue = chrome.test.assertTrue; | 6 var assertTrue = chrome.test.assertTrue; |
| 7 var fail = chrome.test.fail; | 7 var fail = chrome.test.fail; |
| 8 var succeed = chrome.test.succeed; | 8 var succeed = chrome.test.succeed; |
| 9 | 9 |
| 10 function assertThrowsError(method, opt_expectedError) { | 10 function assertThrowsError(method, opt_expectedError) { |
| 11 try { | 11 try { |
| 12 method(); | 12 method(); |
| 13 } catch (e) { | 13 } catch (e) { |
| 14 var message = e.message || e; | 14 var message = e.message || e; |
| 15 if (opt_expectedError) { | 15 if (opt_expectedError) { |
| 16 assertEq(opt_expectedError, e.name); | 16 assertEq(opt_expectedError, e.name); |
| 17 } else { | 17 } else { |
| 18 assertTrue( | 18 assertTrue( |
| 19 message.indexOf('is not available in packaged apps') != -1, | 19 message.indexOf('is not available in packaged apps') != -1, |
| 20 'Unexpected message ' + message); | 20 'Unexpected message ' + message); |
| 21 } | 21 } |
| 22 return; | 22 return; |
| 23 } | 23 } |
| 24 | 24 |
| 25 fail('error not thrown'); | 25 fail('error not thrown'); |
| 26 } | 26 } |
| 27 | 27 |
| 28 chrome.test.runTests([ | 28 chrome.test.runTests([ |
| 29 function testDocument() { | 29 function testDocumentBenignMethods() { |
| 30 assertThrowsError(document.open); | 30 // The real document.open returns a document. |
| 31 assertThrowsError(document.clear); | 31 assertEq('undefined', typeof(document.open())); |
| 32 assertThrowsError(document.close); | 32 |
| 33 // document.clear() has been deprecated on the Web as well, so there is no |
| 34 // good method of testing that the method has been stubbed. We have to |
| 35 // settle for testing that calling the method doesn't throw. |
| 36 assertEq('undefined', typeof(document.clear())); |
| 37 |
| 38 // document.close() doesn't do anything on its own, so there is good method |
| 39 // of testing that it has been stubbed. Settle for making sure it doesn't |
| 40 // throw. |
| 41 assertEq('undefined', typeof(document.close())); |
| 42 |
| 43 succeed(); |
| 44 }, |
| 45 |
| 46 function testDocumentEvilMethods() { |
| 33 assertThrowsError(document.write); | 47 assertThrowsError(document.write); |
| 34 assertThrowsError(document.writeln); | 48 assertThrowsError(document.writeln); |
| 35 | 49 |
| 36 assertThrowsError(function() {document.all;}); | 50 succeed(); |
| 37 assertThrowsError(function() {document.bgColor;}); | 51 }, |
| 38 assertThrowsError(function() {document.fgColor;}); | 52 |
| 39 assertThrowsError(function() {document.alinkColor;}); | 53 function testDocumentGetters() { |
| 40 assertThrowsError(function() {document.linkColor;}); | 54 assertEq('undefined', typeof(document.all)); |
| 41 assertThrowsError(function() {document.vlinkColor;}); | 55 assertEq('undefined', typeof(document.bgColor)); |
| 56 assertEq('undefined', typeof(document.fgColor)); |
| 57 assertEq('undefined', typeof(document.alinkColor)); |
| 58 assertEq('undefined', typeof(document.linkColor)); |
| 59 assertEq('undefined', typeof(document.vlinkColor)); |
| 42 | 60 |
| 43 succeed(); | 61 succeed(); |
| 44 }, | 62 }, |
| 45 | 63 |
| 46 function testHistory() { | 64 function testHistory() { |
| 47 // These are replaced by wrappers that throws exceptions. | 65 // Accessing these logs warnings to the console. |
| 48 assertThrowsError(history.back); | 66 assertEq('undefined', typeof(history.back)); |
| 49 assertThrowsError(history.forward); | 67 assertEq('undefined', typeof(history.forward)); |
| 50 assertThrowsError(function() {history.length;}); | 68 assertEq('undefined', typeof(history.length)); |
| 51 | 69 |
| 52 // These are part of the HTML5 History API that is feature detected, so we | 70 // These are part of the HTML5 History API that are feature detected, so we |
| 53 // remove them altogether, allowing apps to have fallback behavior. | 71 // remove them altogether, allowing apps to have fallback behavior. |
| 54 chrome.test.assertFalse('pushState' in history); | 72 chrome.test.assertFalse('pushState' in history); |
| 55 chrome.test.assertFalse('replaceState' in history); | 73 chrome.test.assertFalse('replaceState' in history); |
| 56 chrome.test.assertFalse('state' in history); | 74 chrome.test.assertFalse('state' in history); |
| 57 | 75 |
| 58 succeed(); | 76 succeed(); |
| 59 }, | 77 }, |
| 60 | 78 |
| 61 function testWindowFind() { | 79 function testWindowFind() { |
| 62 assertThrowsError(Window.prototype.find); | 80 assertEq('undefined', typeof(Window.prototype.find('needle'))); |
| 63 assertThrowsError(window.find); | 81 assertEq('undefined', typeof(window.find('needle'))); |
| 64 assertThrowsError(find); | 82 assertEq('undefined', typeof(find('needle'))); |
| 65 succeed(); | 83 succeed(); |
| 66 }, | 84 }, |
| 67 | 85 |
| 68 function testWindowAlert() { | 86 function testWindowAlert() { |
| 69 assertThrowsError(Window.prototype.alert); | 87 assertEq('undefined', typeof(Window.prototype.alert())); |
| 70 assertThrowsError(window.alert); | 88 assertEq('undefined', typeof(window.alert())); |
| 71 assertThrowsError(alert); | 89 assertEq('undefined', typeof(alert())); |
| 72 succeed(); | 90 succeed(); |
| 73 }, | 91 }, |
| 74 | 92 |
| 75 function testWindowConfirm() { | 93 function testWindowConfirm() { |
| 76 assertThrowsError(Window.prototype.confirm); | 94 assertEq('undefined', typeof(Window.prototype.confirm('Failed'))); |
| 77 assertThrowsError(window.confirm); | 95 assertEq('undefined', typeof(window.confirm('Failed'))); |
| 78 assertThrowsError(confirm); | 96 assertEq('undefined', typeof(confirm('Failed'))); |
| 79 succeed(); | 97 succeed(); |
| 80 }, | 98 }, |
| 81 | 99 |
| 82 function testWindowPrompt() { | 100 function testWindowPrompt() { |
| 83 assertThrowsError(Window.prototype.prompt); | 101 assertEq('undefined', typeof(Window.prototype.prompt('Failed'))); |
| 84 assertThrowsError(window.prompt); | 102 assertEq('undefined', typeof(window.prompt('Failed'))); |
| 85 assertThrowsError(prompt); | 103 assertEq('undefined', typeof(prompt('Failed'))); |
| 86 succeed(); | 104 succeed(); |
| 87 }, | 105 }, |
| 88 | 106 |
| 89 function testBars() { | 107 function testBars() { |
| 90 var bars = ['locationbar', 'menubar', 'personalbar', | 108 var bars = ['locationbar', 'menubar', 'personalbar', |
| 91 'scrollbars', 'statusbar', 'toolbar']; | 109 'scrollbars', 'statusbar', 'toolbar']; |
| 92 for (var x = 0; x < bars.length; x++) { | 110 for (var x = 0; x < bars.length; x++) { |
| 93 assertThrowsError(function() { | 111 assertEq('undefined', typeof(this[bars[x]])); |
| 94 var visible = this[bars[x]].visible; | 112 assertEq('undefined', typeof(window[bars[x]])); |
| 95 visible = window[bars[x]].visible; | |
| 96 }); | |
| 97 } | 113 } |
| 98 succeed(); | 114 succeed(); |
| 99 }, | 115 }, |
| 100 | 116 |
| 101 function testBlockedEvents() { | 117 function testBlockedEvents() { |
| 102 var eventHandler = function() { fail('event handled'); }; | 118 // Fails the test if called by dispatchEvent(). |
| 119 var eventHandler = function() { fail('blocked event handled'); }; |
| 120 |
| 103 var blockedEvents = ['unload', 'beforeunload']; | 121 var blockedEvents = ['unload', 'beforeunload']; |
| 104 | 122 |
| 105 for (var i = 0; i < blockedEvents.length; ++i) { | 123 for (var i = 0; i < blockedEvents.length; ++i) { |
| 106 assertThrowsError(function() { | 124 window['on' + blockedEvents[i]] = eventHandler; |
| 107 window['on' + blockedEvents[i]] = eventHandler; | 125 assertEq(undefined, window['on' + blockedEvents[i]]); |
| 108 }); | 126 |
| 109 assertThrowsError(function() { | 127 var event = new Event(blockedEvents[i]); |
| 110 window.addEventListener(blockedEvents[i], eventHandler); | 128 window.addEventListener(blockedEvents[i], eventHandler); |
| 111 }); | 129 // Ensures that addEventListener did not actually register the handler. |
| 112 assertThrowsError(function() { | 130 // If eventHandler is registered as a listener, it will be called by |
| 113 Window.prototype.addEventListener.apply(window, | 131 // dispatchEvent() and the test will fail. |
| 114 [blockedEvents[i], eventHandler]); | 132 window.dispatchEvent(event); |
| 115 }); | 133 Window.prototype.addEventListener.apply(window, |
| 134 [blockedEvents[i], eventHandler]); |
| 135 window.dispatchEvent(event); |
| 116 } | 136 } |
| 117 | 137 |
| 118 succeed(); | 138 succeed(); |
| 119 }, | 139 }, |
| 120 | 140 |
| 121 function testSyncXhr() { | 141 function testSyncXhr() { |
| 122 var xhr = new XMLHttpRequest(); | 142 var xhr = new XMLHttpRequest(); |
| 123 assertThrowsError(function() { | 143 assertThrowsError(function() { |
| 124 xhr.open('GET', 'data:should not load', false); | 144 xhr.open('GET', 'data:should not load', false); |
| 125 }, 'InvalidAccessError'); | 145 }, 'InvalidAccessError'); |
| 126 succeed(); | 146 succeed(); |
| 127 }, | 147 }, |
| 128 | 148 |
| 129 /** | 149 /** |
| 130 * Tests that restrictions apply to iframes as well. | 150 * Tests that restrictions apply to iframes as well. |
| 131 */ | 151 */ |
| 132 function testIframe() { | 152 function testIframe() { |
| 133 var iframe = document.createElement('iframe'); | 153 var iframe = document.createElement('iframe'); |
| 134 iframe.onload = function() { | 154 iframe.onload = function() { |
| 135 assertThrowsError(iframe.contentWindow.alert); | 155 assertThrowsError(iframe.contentWindow.document.write); |
| 136 succeed(); | 156 succeed(); |
| 137 }; | 157 }; |
| 138 iframe.src = 'iframe.html'; | 158 iframe.src = 'iframe.html'; |
| 139 document.body.appendChild(iframe); | 159 document.body.appendChild(iframe); |
| 140 }, | 160 }, |
| 141 | 161 |
| 142 /** | 162 /** |
| 143 * Tests that restrictions apply to sandboxed iframes. | 163 * Tests that restrictions apply to sandboxed iframes. |
| 144 */ | 164 */ |
| 145 function testSandboxedIframe() { | 165 function testSandboxedIframe() { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 167 assertEq('undefined', typeof(chrome.extension)); | 187 assertEq('undefined', typeof(chrome.extension)); |
| 168 succeed(); | 188 succeed(); |
| 169 }, | 189 }, |
| 170 | 190 |
| 171 function testExtensionApis() { | 191 function testExtensionApis() { |
| 172 assertEq('undefined', typeof(chrome.tabs)); | 192 assertEq('undefined', typeof(chrome.tabs)); |
| 173 assertEq('undefined', typeof(chrome.windows)); | 193 assertEq('undefined', typeof(chrome.windows)); |
| 174 succeed(); | 194 succeed(); |
| 175 } | 195 } |
| 176 ]); | 196 ]); |
| OLD | NEW |