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