Extension SW: Add tests that serve web_accessible_resources from a SW.
This CL also fixes an issue in DocumentThreadableLoader: DTL used to
only intercept http and https protocol requests. This CL expands it
to match content/ by using
SchemeRegistry::shouldTreatURLSchemeAsAllowingServiceWorkers().
Without this change fetch interception from extension SW would always
hit an ASSERT (in debug).
ServiceWorkerTest.WebAccessibleResourcesFetch:
Tests that a Fetch request is served correctly by a SW when
SW is controlling an extension.
ServiceWorkerTest.WebAccessibleResourcesIframeSrc:
From a page outside an extension, an iframe is loaded with a .src of an
extension URL that is listed in the extension's web_accessible_resources.
The test verifies that if a Service Worker is controlling the extension,
it will also control the iframe's requested extension resource.
BUG=545535, 567382
Review URL: https://codereview.chromium.org/1532633003
Cr-Commit-Position: refs/heads/master@{#367375}
diff --git a/chrome/browser/extensions/service_worker_apitest.cc b/chrome/browser/extensions/service_worker_apitest.cc
index 1dbd2f34..4e9ddf7c7 100644
--- a/chrome/browser/extensions/service_worker_apitest.cc
+++ b/chrome/browser/extensions/service_worker_apitest.cc
@@ -572,6 +572,69 @@
"page.html"));
}
+IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, WebAccessibleResourcesFetch) {
+ EXPECT_TRUE(RunExtensionSubtest(
+ "service_worker/web_accessible_resources/fetch/", "page.html"));
+}
+
+// This test loads a web page that has an iframe pointing to a
+// chrome-extension:// URL. The URL is listed in the extension's
+// web_accessible_resources. Initially the iframe is served from the extension's
+// resource file. After verifying that, we register a Service Worker that
+// controls the extension. Further requests to the same resource as before
+// should now be served by the Service Worker.
+// This test also verifies that if the requested resource exists in the manifest
+// but is not present in the extension directory, the Service Worker can still
+// serve the resource file.
+IN_PROC_BROWSER_TEST_F(ServiceWorkerTest, WebAccessibleResourcesIframeSrc) {
+ const Extension* extension = LoadExtensionWithFlags(
+ test_data_dir_.AppendASCII(
+ "service_worker/web_accessible_resources/iframe_src"),
+ kFlagNone);
+ ASSERT_TRUE(extension);
+ ASSERT_TRUE(StartEmbeddedTestServer());
+ GURL page_url = embedded_test_server()->GetURL(
+ "/extensions/api_test/service_worker/web_accessible_resources/"
+ "webpage.html");
+
+ content::WebContents* web_contents = AddTab(browser(), page_url);
+ std::string result;
+ // webpage.html will create an iframe pointing to a resource from |extension|.
+ // Expect the resource to be served by the extension.
+ EXPECT_TRUE(content::ExecuteScriptAndExtractString(
+ web_contents, base::StringPrintf("window.testIframe('%s', 'iframe.html')",
+ extension->id().c_str()),
+ &result));
+ EXPECT_EQ("FROM_EXTENSION_RESOURCE", result);
+
+ ExtensionTestMessageListener service_worker_ready_listener("SW_READY", false);
+ EXPECT_TRUE(ExecuteScriptInBackgroundPageNoWait(
+ extension->id(), "window.registerServiceWorker()"));
+ EXPECT_TRUE(service_worker_ready_listener.WaitUntilSatisfied());
+
+ result.clear();
+ // webpage.html will create another iframe pointing to a resource from
+ // |extension| as before. But this time, the resource should be be served
+ // from the Service Worker.
+ EXPECT_TRUE(content::ExecuteScriptAndExtractString(
+ web_contents, base::StringPrintf("window.testIframe('%s', 'iframe.html')",
+ extension->id().c_str()),
+ &result));
+ EXPECT_EQ("FROM_SW_RESOURCE", result);
+
+ result.clear();
+ // webpage.html will create yet another iframe pointing to a resource that
+ // exists in the extension manifest's web_accessible_resources, but is not
+ // present in the extension directory. Expect the resources of the iframe to
+ // be served by the Service Worker.
+ EXPECT_TRUE(content::ExecuteScriptAndExtractString(
+ web_contents,
+ base::StringPrintf("window.testIframe('%s', 'iframe_non_existent.html')",
+ extension->id().c_str()),
+ &result));
+ EXPECT_EQ("FROM_SW_RESOURCE", result);
+}
+
IN_PROC_BROWSER_TEST_F(ServiceWorkerBackgroundSyncTest, Sync) {
const Extension* extension = LoadExtensionWithFlags(
test_data_dir_.AppendASCII("service_worker/sync"), kFlagNone);