OLD | NEW |
1 <script> | 1 <script> |
2 var getURL = chrome.extension.getURL; | 2 var getURL = chrome.extension.getURL; |
3 var expectedEventData; | 3 var expectedEventData; |
4 var capturedEventData; | 4 var capturedEventData; |
5 var startTime; | 5 var startTime; |
| 6 var tabId; |
| 7 |
| 8 // PORT will be changed to the port of the test server. |
| 9 var URL_HTTP_SIMPLE_LOAD = |
| 10 'http://www.a.com:PORT/files/extensions/api_test/webrequest/events/simpleLoa
d/a.html'; |
| 11 |
| 12 function runTests(tests) { |
| 13 chrome.tabs.getSelected(null, function(tab) { |
| 14 tabId = tab.id; |
| 15 chrome.test.getConfig(function(config) { |
| 16 var fixPort = function(url) { |
| 17 return url.replace(/PORT/, config.testServer.port); |
| 18 }; |
| 19 URL_HTTP_SIMPLE_LOAD = fixPort(URL_HTTP_SIMPLE_LOAD); |
| 20 |
| 21 chrome.test.runTests(tests); |
| 22 }); |
| 23 }); |
| 24 } |
6 | 25 |
7 function expect(data, filter, extraInfoSpec) { | 26 function expect(data, filter, extraInfoSpec) { |
8 startTime = new Date(); | 27 startTime = new Date(); |
9 expectedEventData = data; | 28 expectedEventData = data; |
10 capturedEventData = []; | 29 capturedEventData = []; |
11 removeListeners(); | 30 removeListeners(); |
12 initListeners(filter, extraInfoSpec); | 31 initListeners(filter, extraInfoSpec); |
13 } | 32 } |
14 | 33 |
15 function checkExpectations() { | 34 function checkExpectations() { |
(...skipping 22 matching lines...) Expand all Loading... |
38 capturedEventData.push([name, details]); | 57 capturedEventData.push([name, details]); |
39 checkExpectations(); | 58 checkExpectations(); |
40 } | 59 } |
41 | 60 |
42 function initListeners(filter, extraInfoSpec) { | 61 function initListeners(filter, extraInfoSpec) { |
43 chrome.experimental.webRequest.onBeforeRequest.addListener( | 62 chrome.experimental.webRequest.onBeforeRequest.addListener( |
44 function(details) { | 63 function(details) { |
45 captureEvent("onBeforeRequest", details); | 64 captureEvent("onBeforeRequest", details); |
46 return {cancel: true}; // no effect unless event is blocking. | 65 return {cancel: true}; // no effect unless event is blocking. |
47 }, filter, extraInfoSpec); | 66 }, filter, extraInfoSpec); |
| 67 chrome.experimental.webRequest.onBeforeSendHeaders.addListener( |
| 68 function(details) { |
| 69 captureEvent("onBeforeSendHeaders", details); |
| 70 return {cancel: true}; // no effect unless event is blocking. |
| 71 }, filter, extraInfoSpec); |
48 chrome.experimental.webRequest.onRequestSent.addListener( | 72 chrome.experimental.webRequest.onRequestSent.addListener( |
49 function(details) { | 73 function(details) { |
50 captureEvent("onRequestSent", details); | 74 captureEvent("onRequestSent", details); |
51 return {cancel: true}; | 75 return {cancel: true}; |
52 }, filter, extraInfoSpec); | 76 }, filter, extraInfoSpec); |
53 chrome.experimental.webRequest.onHeadersReceived.addListener( | 77 chrome.experimental.webRequest.onHeadersReceived.addListener( |
54 function(details) { | 78 function(details) { |
55 captureEvent("onHeadersReceived", details); | 79 captureEvent("onHeadersReceived", details); |
56 return {cancel: true}; | 80 return {cancel: true}; |
57 }, filter, extraInfoSpec); | 81 }, filter, extraInfoSpec); |
(...skipping 18 matching lines...) Expand all Loading... |
76 function helper(event) { | 100 function helper(event) { |
77 // Note: We're poking at the internal event data, but it's easier than | 101 // Note: We're poking at the internal event data, but it's easier than |
78 // the alternative. If this starts failing, we just need to update this | 102 // the alternative. If this starts failing, we just need to update this |
79 // helper. | 103 // helper. |
80 for (var cb in event.callbackMap_) { | 104 for (var cb in event.callbackMap_) { |
81 event.removeListener(cb); | 105 event.removeListener(cb); |
82 } | 106 } |
83 chrome.test.assertEq(0, event.subEvents_.length); | 107 chrome.test.assertEq(0, event.subEvents_.length); |
84 } | 108 } |
85 helper(chrome.experimental.webRequest.onBeforeRequest); | 109 helper(chrome.experimental.webRequest.onBeforeRequest); |
| 110 helper(chrome.experimental.webRequest.onBeforeSendHeaders); |
86 helper(chrome.experimental.webRequest.onRequestSent); | 111 helper(chrome.experimental.webRequest.onRequestSent); |
87 helper(chrome.experimental.webRequest.onHeadersReceived); | 112 helper(chrome.experimental.webRequest.onHeadersReceived); |
88 helper(chrome.experimental.webRequest.onBeforeRedirect); | 113 helper(chrome.experimental.webRequest.onBeforeRedirect); |
89 helper(chrome.experimental.webRequest.onCompleted); | 114 helper(chrome.experimental.webRequest.onCompleted); |
90 helper(chrome.experimental.webRequest.onErrorOccurred); | 115 helper(chrome.experimental.webRequest.onErrorOccurred); |
91 } | 116 } |
92 | 117 |
93 chrome.tabs.getSelected(null, function(tab) { | 118 runTests([ |
94 var tabId = tab.id; | 119 // Navigates to a blank page. |
| 120 function simpleLoad() { |
| 121 expect([ |
| 122 [ "onBeforeRequest", |
| 123 { |
| 124 method: "GET", |
| 125 tabId: tabId, |
| 126 type: "main_frame", |
| 127 url: getURL("simpleLoad/a.html") |
| 128 } |
| 129 ], |
| 130 ]); |
| 131 chrome.tabs.update(tabId, { url: getURL("simpleLoad/a.html") }); |
| 132 }, |
95 | 133 |
96 chrome.test.runTests([ | 134 // Navigates to a blank page via HTTP. Only HTTP requests get the |
97 // Navigates to a blank page. | 135 // onBeforeSendHeaders event. |
98 function simpleLoad() { | 136 function simpleLoadHttp() { |
99 expect([ | 137 expect([ |
100 [ "onBeforeRequest", | 138 [ "onBeforeRequest", |
101 { | 139 { |
102 method: "GET", | 140 method: "GET", |
103 tabId: tabId, | 141 tabId: tabId, |
104 type: "main_frame", | 142 type: "main_frame", |
105 url: getURL("simpleLoad/a.html") | 143 url: URL_HTTP_SIMPLE_LOAD |
106 } | 144 } |
107 ], | 145 ], |
108 ]); | 146 [ "onBeforeSendHeaders", |
109 chrome.tabs.update(tabId, { url: getURL("simpleLoad/a.html") }); | 147 { |
110 }, | 148 url: URL_HTTP_SIMPLE_LOAD |
| 149 } |
| 150 ], |
| 151 ], {types: ["main_frame"]}, []); |
| 152 chrome.tabs.update(tabId, { url: URL_HTTP_SIMPLE_LOAD }); |
| 153 }, |
111 | 154 |
112 // Navigates to a page with subresources. | 155 // Navigates to a page with subresources. |
113 // TODO(mpcomplete): add multiple subresources; requires support for | 156 // TODO(mpcomplete): add multiple subresources; requires support for |
114 // recognizing partial ordering. | 157 // recognizing partial ordering. |
115 function complexLoad() { | 158 function complexLoad() { |
116 expect([ | 159 expect([ |
117 [ "onBeforeRequest", | 160 [ "onBeforeRequest", |
118 { | 161 { |
119 method: "GET", | 162 method: "GET", |
120 tabId: tabId, | 163 tabId: tabId, |
121 type: "main_frame", | 164 type: "main_frame", |
122 url: getURL("complexLoad/a.html") | 165 url: getURL("complexLoad/a.html") |
123 } | 166 } |
124 ], | 167 ], |
125 [ "onBeforeRequest", | 168 [ "onBeforeRequest", |
126 { | 169 { |
127 method: "GET", | 170 method: "GET", |
128 tabId: tabId, | 171 tabId: tabId, |
129 type: "sub_frame", | 172 type: "sub_frame", |
130 url: getURL("complexLoad/b.html") | 173 url: getURL("complexLoad/b.html") |
131 } | 174 } |
132 ], | 175 ], |
133 [ "onBeforeRequest", | 176 [ "onBeforeRequest", |
134 { | 177 { |
135 method: "GET", | 178 method: "GET", |
136 tabId: tabId, | 179 tabId: tabId, |
137 type: "image", | 180 type: "image", |
138 url: getURL("complexLoad/b.jpg") | 181 url: getURL("complexLoad/b.jpg") |
139 } | 182 } |
140 ], | 183 ], |
141 ]); | 184 ]); |
142 chrome.tabs.update(tabId, { url: getURL("complexLoad/a.html") }); | 185 chrome.tabs.update(tabId, { url: getURL("complexLoad/a.html") }); |
143 }, | 186 }, |
144 | 187 |
145 // Navigates to a page with subresources, with a blocking handler that | 188 // Navigates to a page with subresources, with a blocking handler that |
146 // cancels the page request. The page will not load, and we should not | 189 // cancels the page request. The page will not load, and we should not |
147 // see the subresources. | 190 // see the subresources. |
148 function complexLoadBlocking() { | 191 function complexLoadBlocking() { |
149 expect([ | 192 expect([ |
150 [ "onBeforeRequest", | 193 [ "onBeforeRequest", |
151 { | 194 { |
152 method: "GET", | 195 method: "GET", |
153 tabId: tabId, | 196 tabId: tabId, |
154 type: "main_frame", | 197 type: "main_frame", |
155 url: getURL("complexLoad/a.html") | 198 url: getURL("complexLoad/a.html") |
156 } | 199 } |
157 ] | 200 ] |
158 ], {}, ["blocking"]); | 201 ], {}, ["blocking"]); |
159 chrome.tabs.update(tabId, { url: getURL("complexLoad/a.html") }); | 202 chrome.tabs.update(tabId, { url: getURL("complexLoad/a.html") }); |
160 }, | 203 }, |
161 | 204 |
162 // Loads several resources, but should only see the complexLoad main_frame | 205 // Loads several resources, but should only see the complexLoad main_frame |
163 // and image due to the filter. | 206 // and image due to the filter. |
164 function complexLoadFiltered() { | 207 function complexLoadFiltered() { |
165 expect([ | 208 expect([ |
166 [ "onBeforeRequest", | 209 [ "onBeforeRequest", |
167 { | 210 { |
168 method: "GET", | 211 method: "GET", |
169 tabId: tabId, | 212 tabId: tabId, |
170 type: "main_frame", | 213 type: "main_frame", |
171 url: getURL("complexLoad/a.html") | 214 url: getURL("complexLoad/a.html") |
172 } | 215 } |
173 ], | 216 ], |
174 [ "onBeforeRequest", | 217 [ "onBeforeRequest", |
175 { | 218 { |
176 method: "GET", | 219 method: "GET", |
177 tabId: tabId, | 220 tabId: tabId, |
178 type: "image", | 221 type: "image", |
179 url: getURL("complexLoad/b.jpg") | 222 url: getURL("complexLoad/b.jpg") |
180 } | 223 } |
181 ] | 224 ] |
182 ], {urls: [getURL("complexLoad/*")], | 225 ], {urls: [getURL("complexLoad/*")], |
183 types: ["main_frame", "image"], | 226 types: ["main_frame", "image"], |
184 tabId: tabId}, []); | 227 tabId: tabId}, []); |
185 chrome.tabs.create({ url: getURL("simpleLoad/a.html") }, | 228 chrome.tabs.create({ url: getURL("simpleLoad/a.html") }, |
186 function(newTab) { | 229 function(newTab) { |
187 chrome.tabs.remove(newTab.id); | 230 chrome.tabs.remove(newTab.id); |
188 chrome.tabs.update(tabId, { url: getURL("complexLoad/a.html") }); | 231 chrome.tabs.update(tabId, { url: getURL("complexLoad/a.html") }); |
189 }); | 232 }); |
190 }, | 233 }, |
191 ]); | 234 ]); |
192 }); | |
193 </script> | 235 </script> |
OLD | NEW |