| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // This file extends framework.js with functionality for testing the behavior of |
| 6 // the webRequest API upon unloading a frame or tab. Loading test_unload.html?N |
| 7 // causes the tests in test_unloadN.js to be run. Each file contains tests for |
| 8 // a single tab (opposed to testing multiple tabs in a file to avoid unnecessary |
| 9 // dependencies in the value of "tabId" between unrelated tests). |
| 10 |
| 11 // Returns a URL of a page that generates a response after waiting for a long |
| 12 // while. Callers are expected to abort the request as soon as possible. |
| 13 // |hostname| can be set to make sure that the frame is created in a new process |
| 14 // if site isolation is enabled. |
| 15 function getSlowURL(hostname) { |
| 16 // Waiting for 10 seconds should be more than sufficient. |
| 17 return getServerURL('slow?10', hostname); |
| 18 } |
| 19 |
| 20 // Get the URL of a page that inserts a frame with the given URL upon load. |
| 21 function getPageWithFrame(frameUrl, hostname) { |
| 22 return getServerURL('extensions/api_test/webrequest/unload/load_frame.html?' + |
| 23 encodeURIComponent(frameUrl), hostname); |
| 24 } |
| 25 |
| 26 // Invokes |callback| when when the onSendHeaders event occurs. When used with |
| 27 // getSlowURL(), this signals when the request has been processed and that there |
| 28 // won't be any webRequest events for a long while. |
| 29 // This allows the test to deterministically cancel the request, which should |
| 30 // trigger onErrorOccurred. |
| 31 function waitUntilSendHeaders(type, url, callback) { |
| 32 chrome.test.assertTrue(/^https?:.+\/slow\?/.test(url), |
| 33 'Must be a slow URL, but was ' + url); |
| 34 |
| 35 chrome.webRequest.onSendHeaders.addListener(function listener() { |
| 36 chrome.webRequest.onSendHeaders.removeListener(listener); |
| 37 callback(); |
| 38 }, { |
| 39 types: [type], |
| 40 urls: [url], |
| 41 }); |
| 42 } |
| 43 |
| 44 (function() { |
| 45 // Load the actual test file. |
| 46 var id = location.search.slice(1); |
| 47 chrome.test.assertTrue(/^\d+$/.test(id), |
| 48 'Page URL should end with digits, but got ' + id); |
| 49 console.log('Running test_unload ' + id); |
| 50 |
| 51 var s = document.createElement('script'); |
| 52 // test_unload1.js, test_unload2.js, ..., etc. |
| 53 // These tests are in separate files to make sure that the tests are |
| 54 // independent of each other. If they were put in one file, then the tabId |
| 55 // of one test would depend on the number of tabs from the previous tests. |
| 56 s.src = 'test_unload' + id + '.js'; |
| 57 s.onerror = function() { |
| 58 chrome.test.fail('Failed to load test ' + s.src); |
| 59 }; |
| 60 |
| 61 // At the next test, a call to RunExtensionSubtest causes the extension to |
| 62 // reload. As a result, all extension pages are closed. If the extension page |
| 63 // was the only tab in the browser, then the browser would exit and cause the |
| 64 // test to end too early. To avoid this problem, create an extra non-extension |
| 65 // tab before starting tests. |
| 66 chrome.tabs.create({ |
| 67 url: 'data:,' |
| 68 }, function() { |
| 69 document.body.appendChild(s); |
| 70 }); |
| 71 })(); |
| OLD | NEW |