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));
70 assertEq('undefined', typeof(history.pushState));
71 assertEq('undefined', typeof(history.replaceState));
72 assertEq('undefined', typeof(history.state));
76 function testWindowFind() {
77 assertEq('undefined', typeof(Window.prototype.find('needle')));
78 assertEq('undefined', typeof(window.find('needle')));
79 assertEq('undefined', typeof(find('needle')));
83 function testWindowAlert() {
84 assertEq('undefined', typeof(Window.prototype.alert()));
85 assertEq('undefined', typeof(window.alert()));
86 assertEq('undefined', typeof(alert()));
90 function testWindowConfirm() {
91 assertEq('undefined', typeof(Window.prototype.confirm('Failed')));
92 assertEq('undefined', typeof(window.confirm('Failed')));
93 assertEq('undefined', typeof(confirm('Failed')));
97 function testWindowPrompt() {
98 assertEq('undefined', typeof(Window.prototype.prompt('Failed')));
99 assertEq('undefined', typeof(window.prompt('Failed')));
100 assertEq('undefined', typeof(prompt('Failed')));
104 function testBars() {
105 var bars = ['locationbar', 'menubar', 'personalbar',
106 'scrollbars', 'statusbar', 'toolbar'];
107 for (var x = 0; x < bars.length; x++) {
108 assertEq('undefined', typeof(this[bars[x]]));
109 assertEq('undefined', typeof(window[bars[x]]));
114 function testBlockedEvents() {
115 // Fails the test if called by dispatchEvent().
116 var eventHandler = function() { fail('blocked event handled'); };
118 var blockedEvents = ['unload', 'beforeunload'];
120 for (var i = 0; i < blockedEvents.length; ++i) {
121 window['on' + blockedEvents[i]] = eventHandler;
122 assertEq(undefined, window['on' + blockedEvents[i]]);
124 var event = new Event(blockedEvents[i]);
125 window.addEventListener(blockedEvents[i], eventHandler);
126 // Ensures that addEventListener did not actually register the handler.
127 // If eventHandler is registered as a listener, it will be called by
128 // dispatchEvent() and the test will fail.
129 window.dispatchEvent(event);
130 Window.prototype.addEventListener.apply(window,
131 [blockedEvents[i], eventHandler]);
132 window.dispatchEvent(event);
138 function testSyncXhr() {
139 var xhr = new XMLHttpRequest();
140 assertThrowsError(function() {
141 xhr.open('GET', 'data:should not load', false);
142 }, 'InvalidAccessError');
147 * Tests that restrictions apply to iframes as well.
149 function testIframe() {
150 var iframe = document.createElement('iframe');
151 iframe.onload = function() {
152 assertThrowsError(iframe.contentWindow.document.write);
155 iframe.src = 'iframe.html';
156 document.body.appendChild(iframe);
160 * Tests that restrictions apply to sandboxed iframes.
162 function testSandboxedIframe() {
163 function handleWindowMessage(e) {
169 window.addEventListener('message', handleWindowMessage);
171 var iframe = document.createElement('iframe');
172 iframe.src = 'sandboxed_iframe.html';
173 document.body.appendChild(iframe);
176 function testLegacyApis() {
178 assertEq('undefined', typeof(chrome.app.getIsInstalled));
179 assertEq('undefined', typeof(chrome.app.isInstalled));
180 assertEq('undefined', typeof(chrome.app.getDetails));
181 assertEq('undefined', typeof(chrome.app.runningState));
183 assertEq('undefined', typeof(chrome.extension));
187 function testExtensionApis() {
188 assertEq('undefined', typeof(chrome.tabs));
189 assertEq('undefined', typeof(chrome.windows));