Chromium Code Reviews| Index: chrome/browser/prerender/prerender_nostate_prefetch_browsertest.cc |
| diff --git a/chrome/browser/prerender/prerender_nostate_prefetch_browsertest.cc b/chrome/browser/prerender/prerender_nostate_prefetch_browsertest.cc |
| index 6c1d72ff9bc201bdcb292665c6110df47712c3d7..786bcd854975fc8620a52fa6075b8fb99afc5bfa 100644 |
| --- a/chrome/browser/prerender/prerender_nostate_prefetch_browsertest.cc |
| +++ b/chrome/browser/prerender/prerender_nostate_prefetch_browsertest.cc |
| @@ -20,6 +20,8 @@ |
| #include "chrome/common/chrome_switches.h" |
| #include "chrome/grit/generated_resources.h" |
| #include "chrome/test/base/ui_test_utils.h" |
| +#include "content/public/browser/service_worker_context.h" |
| +#include "content/public/browser/storage_partition.h" |
| #include "content/public/common/url_constants.h" |
| #include "content/public/test/browser_test_utils.h" |
| #include "net/base/escape.h" |
| @@ -33,6 +35,13 @@ using prerender::test_utils::DestructionWaiter; |
| using prerender::test_utils::RequestCounter; |
| using prerender::test_utils::TestPrerender; |
| +namespace { |
|
pasko
2017/01/02 18:15:36
nit: empty line after namespace for function decla
mattcary
2017/01/03 08:43:20
Done.
|
| +void ClosureOnUiThread(const base::Closure& callback) { |
| + content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| + callback); |
| +} |
| +} // namespace |
|
pasko
2017/01/02 18:15:36
nit: git grep -B 2 '} // namespace$'
mattcary
2017/01/03 08:43:20
Done.
|
| + |
| namespace prerender { |
| // These URLs used for test resources must be relative with the exception of |
| @@ -49,6 +58,8 @@ const char kPrefetchResponseHeaderCSP[] = |
| "prerender/prefetch_response_csp.html"; |
| const char kPrefetchScript[] = "prerender/prefetch.js"; |
| const char kPrefetchScript2[] = "prerender/prefetch2.js"; |
| +const char kServiceWorkerLoader[] = "prerender/service_worker.html"; |
| +const char kServiceWorkerJavascript[] = "prerender/service_worker.js"; |
| const char kPrefetchSubresourceRedirectPage[] = |
| "prerender/prefetch_subresource_redirect.html"; |
| @@ -514,4 +525,35 @@ IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, IssuesIdlePriorityRequests) { |
| script_counter.WaitForCount(1); |
| } |
| +// Checks that a registered ServiceWorker (SW) without a renderer intercepts a |
| +// prefetch request. |
| +IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, ServiceWorkerIntercept) { |
| + // Register and launch a SW. |
|
pasko
2017/01/02 18:15:36
Is it easy to add a similar test, but for the case
mattcary
2017/01/03 08:43:20
Yes, in fact the other tests already do that (if p
|
| + RequestCounter sw_counter; |
| + CountRequestFor(kServiceWorkerJavascript, &sw_counter); |
| + ui_test_utils::NavigateToURL( |
| + current_browser(), |
| + src_server()->GetURL(MakeAbsolute(kServiceWorkerLoader))); |
| + sw_counter.WaitForCount(1); |
| + |
| + // Stop the SW renderer process. |
| + base::RunLoop run_loop; |
| + content::BrowserContext::GetStoragePartitionForSite( |
| + GetActiveWebContents()->GetBrowserContext(), |
| + src_server()->GetURL(MakeAbsolute(kPrefetchPng))) |
|
pasko
2017/01/02 18:15:36
nit: hard to count brackets, formatting does not h
mattcary
2017/01/03 08:43:19
Done.
|
| + ->GetServiceWorkerContext() |
| + ->StopAllServiceWorkersForOriginWithCallback( |
| + src_server()->GetURL("/"), |
| + base::Bind(&ClosureOnUiThread, run_loop.QuitClosure())); |
|
pasko
2017/01/02 18:15:36
why not just run_loop.QuitClosure()? // i.e. witho
mattcary
2017/01/03 08:43:19
The SW callback fires from the IO thread.
pasko
2017/01/03 13:09:14
Thanks. I did not know that.
|
| + run_loop.Run(); |
| + |
| + // The SW intercepts kPrefetchPage and replaces it with a body that contains |
| + // an <img> tage for kPrefetchPng. We verify that the SW ran correctly by |
|
pasko
2017/01/02 18:15:36
s/We verify/Verify/
(we prefer to avoid 'we' in co
mattcary
2017/01/03 08:43:20
Done, although git grep -i ' we ' | grep cc | head
|
| + // observing the fetch of the image. |
| + RequestCounter image_counter; |
| + CountRequestFor(kPrefetchPng, &image_counter); |
| + PrefetchFromFile(kPrefetchPage, FINAL_STATUS_NOSTATE_PREFETCH_FINISHED); |
| + image_counter.WaitForCount(1); |
| +} |
| + |
| } // namespace prerender |