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.
5 var assertEq = chrome.test.assertEq;
6 var assertTrue = chrome.test.assertTrue;
7 var fail = chrome.test.fail;
8 var succeed = chrome.test.succeed;
10 function assertThrowsError(method, opt_expectedError) {
14 var message = e.message || e;
15 if (opt_expectedError) {
16 assertEq(opt_expectedError, e.name);
19 message.indexOf('is not available in packaged apps') != -1,
20 'Unexpected message ' + message);
25 fail('error not thrown');
28 chrome.test.runTests([
29 function testDocumentBenignMethods() {
30 // The real document.open returns a document.
31 assertEq('undefined', typeof(document.open()));
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()));
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
41 assertEq('undefined', typeof(document.close()));
46 function testDocumentEvilMethods() {
47 assertThrowsError(document.write);
48 assertThrowsError(document.writeln);
53 function testDocumentGetters() {
54 assertEq('undefined', typeof(document.all));
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));
64 function testHistory() {
65 // Accessing these logs warnings to the console.
66 assertEq('undefined', typeof(history.back));
67 assertEq('undefined', typeof(history.forward));
68 assertEq('undefined', typeof(history.go));
69 assertEq('undefined', typeof(history.length));
71 // These are part of the HTML5 History API that are feature detected, so we
72 // remove them altogether, allowing apps to have fallback behavior.
73 chrome.test.assertFalse('pushState' in history);
74 chrome.test.assertFalse('replaceState' in history);
75 chrome.test.assertFalse('state' in history);
80 function testWindowFind() {
81 assertEq('undefined', typeof(Window.prototype.find('needle')));
82 assertEq('undefined', typeof(window.find('needle')));
83 assertEq('undefined', typeof(find('needle')));
87 function testWindowAlert() {
88 assertEq('undefined', typeof(Window.prototype.alert()));
89 assertEq('undefined', typeof(window.alert()));
90 assertEq('undefined', typeof(alert()));
94 function testWindowConfirm() {
95 assertEq('undefined', typeof(Window.prototype.confirm('Failed')));
96 assertEq('undefined', typeof(window.confirm('Failed')));
97 assertEq('undefined', typeof(confirm('Failed')));
101 function testWindowPrompt() {
102 assertEq('undefined', typeof(Window.prototype.prompt('Failed')));
103 assertEq('undefined', typeof(window.prompt('Failed')));
104 assertEq('undefined', typeof(prompt('Failed')));
108 function testBars() {
109 var bars = ['locationbar', 'menubar', 'personalbar',
110 'scrollbars', 'statusbar', 'toolbar'];
111 for (var x = 0; x < bars.length; x++) {
112 assertEq('undefined', typeof(this[bars[x]]));
113 assertEq('undefined', typeof(window[bars[x]]));
118 function testBlockedEvents() {
119 // Fails the test if called by dispatchEvent().
120 var eventHandler = function() { fail('blocked event handled'); };
122 var blockedEvents = ['unload', 'beforeunload'];
124 for (var i = 0; i < blockedEvents.length; ++i) {
125 window['on' + blockedEvents[i]] = eventHandler;
126 assertEq(undefined, window['on' + blockedEvents[i]]);
128 var event = new Event(blockedEvents[i]);
129 window.addEventListener(blockedEvents[i], eventHandler);
130 // Ensures that addEventListener did not actually register the handler.
131 // If eventHandler is registered as a listener, it will be called by
132 // dispatchEvent() and the test will fail.
133 window.dispatchEvent(event);
134 Window.prototype.addEventListener.apply(window,
135 [blockedEvents[i], eventHandler]);
136 window.dispatchEvent(event);
142 function testSyncXhr() {
143 var xhr = new XMLHttpRequest();
144 assertThrowsError(function() {
145 xhr.open('GET', 'data:should not load', false);
146 }, 'InvalidAccessError');
151 * Tests that restrictions apply to iframes as well.
153 function testIframe() {
154 var iframe = document.createElement('iframe');
155 iframe.onload = function() {
156 assertThrowsError(iframe.contentWindow.document.write);
159 iframe.src = 'iframe.html';
160 document.body.appendChild(iframe);
164 * Tests that restrictions apply to sandboxed iframes.
166 function testSandboxedIframe() {
167 function handleWindowMessage(e) {
173 window.addEventListener('message', handleWindowMessage);
175 var iframe = document.createElement('iframe');
176 iframe.src = 'sandboxed_iframe.html';
177 document.body.appendChild(iframe);
180 function testLegacyApis() {
182 assertEq('undefined', typeof(chrome.app.getIsInstalled));
183 assertEq('undefined', typeof(chrome.app.isInstalled));
184 assertEq('undefined', typeof(chrome.app.getDetails));
185 assertEq('undefined', typeof(chrome.app.getDetailsForFrame));
186 assertEq('undefined', typeof(chrome.app.runningState));
188 assertEq('undefined', typeof(chrome.extension));
192 function testExtensionApis() {
193 assertEq('undefined', typeof(chrome.tabs));
194 assertEq('undefined', typeof(chrome.windows));