diff options
author | michaeln@chromium.org <michaeln@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-08-12 03:07:44 +0000 |
---|---|---|
committer | michaeln@chromium.org <michaeln@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-08-12 03:09:02 +0000 |
commit | 47e0b38eb95db3081b970e90f219a164d87b6940 (patch) | |
tree | 1a0b929994bf23bd312478d9ad9d426561315699 /content | |
parent | 3d2fd542f2eb8c66518d43fef055a206e6ade32d (diff) | |
download | chromium_src-47e0b38eb95db3081b970e90f219a164d87b6940.zip chromium_src-47e0b38eb95db3081b970e90f219a164d87b6940.tar.gz chromium_src-47e0b38eb95db3081b970e90f219a164d87b6940.tar.bz2 |
A browsertest for for blink r179868.
> Bust the MemoryCache to ensure script requests reach the browser-side
> and get added to and retrieved from the ServiceWorker's script cache.
BUG=387589
Review URL: https://codereview.chromium.org/434453005
Cr-Commit-Position: refs/heads/master@{#288887}
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@288887 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content')
3 files changed, 138 insertions, 0 deletions
diff --git a/content/browser/service_worker/service_worker_browsertest.cc b/content/browser/service_worker/service_worker_browsertest.cc index ef9be78..f74f2b2 100644 --- a/content/browser/service_worker/service_worker_browsertest.cc +++ b/content/browser/service_worker/service_worker_browsertest.cc @@ -32,6 +32,9 @@ #include "net/test/embedded_test_server/embedded_test_server.h" #include "net/test/embedded_test_server/http_request.h" #include "net/test/embedded_test_server/http_response.h" +#include "net/url_request/url_request_filter.h" +#include "net/url_request/url_request_interceptor.h" +#include "net/url_request/url_request_test_job.h" #include "webkit/browser/blob/blob_data_handle.h" #include "webkit/browser/blob/blob_storage_context.h" #include "webkit/common/blob/blob_data.h" @@ -172,6 +175,78 @@ scoped_ptr<net::test_server::HttpResponse> VerifyServiceWorkerHeaderInRequest( return http_response.PassAs<net::test_server::HttpResponse>(); } +// The ImportsBustMemcache test requires that the imported script +// would naturally be cached in blink's memcache, but the embedded +// test server doesn't produce headers that allow the blink's memcache +// to do that. This interceptor injects headers that give the import +// an experiration far in the future. +class LongLivedResourceInterceptor : public net::URLRequestInterceptor { + public: + LongLivedResourceInterceptor(const std::string& body) + : body_(body) {} + virtual ~LongLivedResourceInterceptor() {} + + // net::URLRequestInterceptor implementation + virtual net::URLRequestJob* MaybeInterceptRequest( + net::URLRequest* request, + net::NetworkDelegate* network_delegate) const OVERRIDE { + const char kHeaders[] = + "HTTP/1.1 200 OK\0" + "Expires: Thu, 1 Jan 2100 20:00:00 GMT\0" + "\0"; + std::string headers(kHeaders, arraysize(kHeaders)); + return new net::URLRequestTestJob( + request, network_delegate, headers, body_, true); + } + + private: + std::string body_; + DISALLOW_COPY_AND_ASSIGN(LongLivedResourceInterceptor); +}; + +void CreateLongLivedResourceInterceptors( + const GURL& worker_url, const GURL& import_url) { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); + scoped_ptr<net::URLRequestInterceptor> interceptor; + + interceptor.reset(new LongLivedResourceInterceptor( + "importScripts('long_lived_import.js');")); + net::URLRequestFilter::GetInstance()->AddUrlInterceptor( + worker_url, interceptor.Pass()); + + interceptor.reset(new LongLivedResourceInterceptor( + "// the imported script does nothing")); + net::URLRequestFilter::GetInstance()->AddUrlInterceptor( + import_url, interceptor.Pass()); +} + +void CountScriptResources( + ServiceWorkerContextWrapper* wrapper, + const GURL& scope, + int* num_resources) { + *num_resources = -1; + + std::vector<ServiceWorkerRegistrationInfo> infos = + wrapper->context()->GetAllLiveRegistrationInfo(); + if (infos.empty()) + return; + + int version_id; + size_t index = infos.size() - 1; + if (!infos[index].installing_version.is_null) + version_id = infos[index].installing_version.version_id; + else if (!infos[index].waiting_version.is_null) + version_id = infos[1].waiting_version.version_id; + else if (!infos[index].active_version.is_null) + version_id = infos[index].active_version.version_id; + else + return; + + ServiceWorkerVersion* version = + wrapper->context()->GetLiveVersion(version_id); + *num_resources = static_cast<int>(version->script_cache_map()->size()); +} + } // namespace class ServiceWorkerBrowserTest : public ContentBrowserTest { @@ -706,6 +781,36 @@ IN_PROC_BROWSER_TEST_F(ServiceWorkerBrowserTest, Reload) { } } +IN_PROC_BROWSER_TEST_F(ServiceWorkerBrowserTest, ImportsBustMemcache) { + const std::string kScopeUrl = "/service_worker/imports_bust_memcache_scope/"; + const std::string kPageUrl = "/service_worker/imports_bust_memcache.html"; + const std::string kScriptUrl = "/service_worker/worker_with_one_import.js"; + const std::string kImportUrl = "/service_worker/long_lived_import.js"; + const base::string16 kOKTitle(base::ASCIIToUTF16("OK")); + const base::string16 kFailTitle(base::ASCIIToUTF16("FAIL")); + + RunOnIOThread( + base::Bind(&CreateLongLivedResourceInterceptors, + embedded_test_server()->GetURL(kScriptUrl), + embedded_test_server()->GetURL(kImportUrl))); + + TitleWatcher title_watcher(shell()->web_contents(), kOKTitle); + title_watcher.AlsoWaitForTitle(kFailTitle); + NavigateToURL(shell(), embedded_test_server()->GetURL(kPageUrl)); + base::string16 title = title_watcher.WaitAndGetTitle(); + EXPECT_EQ(kOKTitle, title); + + // Verify the number of resources in the implicit script cache is correct. + const int kExpectedNumResources = 2; + int num_resources = 0; + RunOnIOThread( + base::Bind(&CountScriptResources, + base::Unretained(wrapper()), + embedded_test_server()->GetURL(kScopeUrl), + &num_resources)); + EXPECT_EQ(kExpectedNumResources, num_resources); +} + class ServiceWorkerBlackBoxBrowserTest : public ServiceWorkerBrowserTest { public: typedef ServiceWorkerBlackBoxBrowserTest self; diff --git a/content/browser/service_worker/service_worker_script_cache_map.h b/content/browser/service_worker/service_worker_script_cache_map.h index da790f6..45afcc3 100644 --- a/content/browser/service_worker/service_worker_script_cache_map.h +++ b/content/browser/service_worker/service_worker_script_cache_map.h @@ -40,6 +40,8 @@ class CONTENT_EXPORT ServiceWorkerScriptCacheMap { void SetResources( const std::vector<ServiceWorkerDatabase::ResourceRecord>& resources); + size_t size() const { return resource_ids_.size(); } + private: typedef std::map<GURL, int64> ResourceIDMap; diff --git a/content/test/data/service_worker/imports_bust_memcache.html b/content/test/data/service_worker/imports_bust_memcache.html new file mode 100644 index 0000000..cc0db4f --- /dev/null +++ b/content/test/data/service_worker/imports_bust_memcache.html @@ -0,0 +1,31 @@ +<!DOCTYPE html> +<script> +// See ServiceWorkerBrowserTest ImportsBustMemcache. +// Content-Shell data persists so unregister first to clear old data. +// Register, unregister, then reregister. The browsertest examines the +// state of the scriptcache to ensure the script and import are cached. + +var scope = 'imports_bust_memcache_scope/'; +var script = 'worker_with_one_import.js'; + +navigator.serviceWorker.unregister(scope) +.then(function(_) { + console.log('initial unregistration done'); + return navigator.serviceWorker.register(script, {scope: scope}); +}) +.then(function(_) { + console.log('initial registration done'); + return navigator.serviceWorker.unregister(scope); +}) +.then(function(_) { + console.log('unregistration done'); + return navigator.serviceWorker.register(script, {scope: scope}); +}) +.then(function(_) { + console.log('second registration done'); + document.title = 'OK'; // Titlewatcher looks for this. +}) +.catch(function(e) { + document.title = 'FAILED'; +}); +</script> |