| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 var getURL = chrome.extension.getURL; | 5 var getURL = chrome.extension.getURL; |
| 6 var deepEq = chrome.test.checkDeepEq; | 6 var deepEq = chrome.test.checkDeepEq; |
| 7 var expectedEventData; | 7 var expectedEventData; |
| 8 var capturedEventData; | 8 var capturedEventData; |
| 9 var capturedUnexpectedData; | 9 var capturedUnexpectedData; |
| 10 var expectedEventOrder; | 10 var expectedEventOrder; |
| 11 var tabId; | 11 var tabId; |
| 12 var tabIdMap; | 12 var tabIdMap; |
| 13 var frameIdMap; | 13 var frameIdMap; |
| 14 var testWebSocketPort; |
| 14 var testServerPort; | 15 var testServerPort; |
| 15 var testServer = "www.a.com"; | 16 var testServer = "www.a.com"; |
| 16 var defaultScheme = "http"; | 17 var defaultScheme = "http"; |
| 17 var eventsCaptured; | 18 var eventsCaptured; |
| 18 var listeners = { | 19 var listeners = { |
| 19 'onBeforeRequest': [], | 20 'onBeforeRequest': [], |
| 20 'onBeforeSendHeaders': [], | 21 'onBeforeSendHeaders': [], |
| 21 'onAuthRequired': [], | 22 'onAuthRequired': [], |
| 22 'onSendHeaders': [], | 23 'onSendHeaders': [], |
| 23 'onHeadersReceived': [], | 24 'onHeadersReceived': [], |
| 24 'onResponseStarted': [], | 25 'onResponseStarted': [], |
| 25 'onBeforeRedirect': [], | 26 'onBeforeRedirect': [], |
| 26 'onCompleted': [], | 27 'onCompleted': [], |
| 27 'onErrorOccurred': [] | 28 'onErrorOccurred': [] |
| 28 }; | 29 }; |
| 29 | 30 |
| 30 // If true, don't bark on events that were not registered via expect(). | 31 // If true, don't bark on events that were not registered via expect(). |
| 31 // These events are recorded in capturedUnexpectedData instead of | 32 // These events are recorded in capturedUnexpectedData instead of |
| 32 // capturedEventData. | 33 // capturedEventData. |
| 33 var ignoreUnexpected = false; | 34 var ignoreUnexpected = false; |
| 34 | 35 |
| 35 // This is a debugging aid to print all received events as well as the | 36 // This is a debugging aid to print all received events as well as the |
| 36 // information whether they were expected. | 37 // information whether they were expected. |
| 37 var logAllRequests = false; | 38 var logAllRequests = false; |
| 38 | 39 |
| 40 // Runs the |tests| using the |tab| as a default tab. |
| 41 function runTestsForTab(tests, tab) { |
| 42 tabId = tab.id; |
| 43 tabIdMap = {"-1": -1}; |
| 44 tabIdMap[tabId] = 0; |
| 45 chrome.test.getConfig(function(config) { |
| 46 testServerPort = config.testServer.port; |
| 47 testWebSocketPort = config.testWebSocketPort; |
| 48 chrome.test.runTests(tests); |
| 49 }); |
| 50 } |
| 51 |
| 52 // Creates an "about:blank" tab and runs |tests| with this tab as default. |
| 39 function runTests(tests) { | 53 function runTests(tests) { |
| 40 var waitForAboutBlank = function(_, info, tab) { | 54 var waitForAboutBlank = function(_, info, tab) { |
| 41 if (info.status == "complete" && tab.url == "about:blank") { | 55 if (info.status == "complete" && tab.url == "about:blank") { |
| 42 tabId = tab.id; | |
| 43 tabIdMap = {"-1": -1}; | |
| 44 tabIdMap[tabId] = 0; | |
| 45 chrome.tabs.onUpdated.removeListener(waitForAboutBlank); | 56 chrome.tabs.onUpdated.removeListener(waitForAboutBlank); |
| 46 chrome.test.getConfig(function(config) { | 57 runTestsForTab(tests, tab); |
| 47 testServerPort = config.testServer.port; | |
| 48 chrome.test.runTests(tests); | |
| 49 }); | |
| 50 } | 58 } |
| 51 }; | 59 }; |
| 52 chrome.tabs.onUpdated.addListener(waitForAboutBlank); | 60 chrome.tabs.onUpdated.addListener(waitForAboutBlank); |
| 53 chrome.tabs.create({url: "about:blank"}); | 61 chrome.tabs.create({url: "about:blank"}); |
| 54 } | 62 } |
| 55 | 63 |
| 56 // Returns an URL from the test server, fixing up the port. Must be called | 64 // Returns an URL from the test server, fixing up the port. Must be called |
| 57 // from within a test case passed to runTests. | 65 // from within a test case passed to runTests. |
| 58 function getServerURL(path, opt_host, opt_scheme) { | 66 function getServerURL(path, opt_host, opt_scheme) { |
| 59 if (!testServerPort) | 67 if (!testServerPort) |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 exp.details.type === details.type; | 208 exp.details.type === details.type; |
| 201 }); | 209 }); |
| 202 } | 210 } |
| 203 | 211 |
| 204 function captureEvent(name, details, callback) { | 212 function captureEvent(name, details, callback) { |
| 205 // Ignore system-level requests like safebrowsing updates and favicon fetches | 213 // Ignore system-level requests like safebrowsing updates and favicon fetches |
| 206 // since they are unpredictable. | 214 // since they are unpredictable. |
| 207 if ((details.type == "other" && !details.url.includes('dont-ignore-me')) || | 215 if ((details.type == "other" && !details.url.includes('dont-ignore-me')) || |
| 208 isUnexpectedDetachedRequest(name, details) || | 216 isUnexpectedDetachedRequest(name, details) || |
| 209 details.url.match(/\/favicon.ico$/) || | 217 details.url.match(/\/favicon.ico$/) || |
| 210 details.url.match(/https:\/\/dl.google.com/)) | 218 details.url.match(/https:\/\/dl.google.com/)) { |
| 211 return; | 219 return; |
| 220 } |
| 212 | 221 |
| 213 // Pull the extra per-event options out of the expected data. These let | 222 // Pull the extra per-event options out of the expected data. These let |
| 214 // us specify special return values per event. | 223 // us specify special return values per event. |
| 215 var currentIndex = capturedEventData.length; | 224 var currentIndex = capturedEventData.length; |
| 216 var extraOptions; | 225 var extraOptions; |
| 217 var retval; | 226 var retval; |
| 218 if (expectedEventData.length > currentIndex) { | 227 if (expectedEventData.length > currentIndex) { |
| 219 retval = | 228 retval = |
| 220 expectedEventData[currentIndex].retval_function ? | 229 expectedEventData[currentIndex].retval_function ? |
| 221 expectedEventData[currentIndex].retval_function(name, details) : | 230 expectedEventData[currentIndex].retval_function(name, details) : |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 331 var onSendHeaders = function(details) { | 340 var onSendHeaders = function(details) { |
| 332 return captureEvent("onSendHeaders", details); | 341 return captureEvent("onSendHeaders", details); |
| 333 }; | 342 }; |
| 334 listeners['onSendHeaders'].push(onSendHeaders); | 343 listeners['onSendHeaders'].push(onSendHeaders); |
| 335 | 344 |
| 336 var onHeadersReceived = function(details) { | 345 var onHeadersReceived = function(details) { |
| 337 return captureEvent("onHeadersReceived", details); | 346 return captureEvent("onHeadersReceived", details); |
| 338 }; | 347 }; |
| 339 listeners['onHeadersReceived'].push(onHeadersReceived); | 348 listeners['onHeadersReceived'].push(onHeadersReceived); |
| 340 | 349 |
| 341 var onAuthRequired = function(details) { | 350 var onAuthRequired = function(details, callback) { |
| 342 return captureEvent("onAuthRequired", details, callback); | 351 return captureEvent("onAuthRequired", details, callback); |
| 343 }; | 352 }; |
| 344 listeners['onAuthRequired'].push(onAuthRequired); | 353 listeners['onAuthRequired'].push(onAuthRequired); |
| 345 | 354 |
| 346 var onResponseStarted = function(details) { | 355 var onResponseStarted = function(details) { |
| 347 return captureEvent("onResponseStarted", details); | 356 return captureEvent("onResponseStarted", details); |
| 348 }; | 357 }; |
| 349 listeners['onResponseStarted'].push(onResponseStarted); | 358 listeners['onResponseStarted'].push(onResponseStarted); |
| 350 | 359 |
| 351 var onBeforeRedirect = function(details) { | 360 var onBeforeRedirect = function(details) { |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 413 helper('onHeadersReceived'); | 422 helper('onHeadersReceived'); |
| 414 helper('onResponseStarted'); | 423 helper('onResponseStarted'); |
| 415 helper('onBeforeRedirect'); | 424 helper('onBeforeRedirect'); |
| 416 helper('onCompleted'); | 425 helper('onCompleted'); |
| 417 helper('onErrorOccurred'); | 426 helper('onErrorOccurred'); |
| 418 } | 427 } |
| 419 | 428 |
| 420 function resetDeclarativeRules() { | 429 function resetDeclarativeRules() { |
| 421 chrome.declarativeWebRequest.onRequest.removeRules(); | 430 chrome.declarativeWebRequest.onRequest.removeRules(); |
| 422 } | 431 } |
| OLD | NEW |