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