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