Index: chrome/browser/chrome_service_worker_browsertest.cc |
diff --git a/chrome/browser/chrome_service_worker_browsertest.cc b/chrome/browser/chrome_service_worker_browsertest.cc |
index 396f8aaf2e8078fe1cf0a192b84691181b7b32a0..db62d34e150ab1f0ecde19c4119d2248c0c55e2f 100644 |
--- a/chrome/browser/chrome_service_worker_browsertest.cc |
+++ b/chrome/browser/chrome_service_worker_browsertest.cc |
@@ -532,6 +532,63 @@ class ServiceWorkerSpeculativeLaunchTest : public ChromeServiceWorkerTest { |
features::kSpeculativeLaunchServiceWorker.name); |
} |
+ void WriteTestHtmlFile() { |
+ WriteFile( |
+ FILE_PATH_LITERAL("test.html"), |
+ "<script>" |
+ "navigator.serviceWorker.register('./sw.js', {scope: './scope.html'})" |
+ " .then(function(reg) {" |
+ " reg.addEventListener('updatefound', function() {" |
+ " var worker = reg.installing;" |
+ " worker.addEventListener('statechange', function() {" |
+ " if (worker.state == 'activated')" |
+ " document.title = 'READY';" |
+ " });" |
+ " });" |
+ " });" |
+ "</script>" |
+ "<body style='margin:0; padding:0;'>" |
+ "<a href='./scope.html' " |
+ "style='position:fixed; width:1px; height:1px;'></a>" |
+ "</body>"); |
+ } |
falken
2016/10/04 04:50:51
nit: add newline
horo
2016/10/04 06:54:07
Done.
|
+ void RunNavigationHintTest() { |
+ embedded_test_server()->ServeFilesFromDirectory( |
+ service_worker_dir_.GetPath()); |
+ ASSERT_TRUE(embedded_test_server()->Start()); |
+ |
+ content::ServiceWorkerContext* sw_context = |
+ content::BrowserContext::GetDefaultStoragePartition( |
+ browser()->profile()) |
+ ->GetServiceWorkerContext(); |
+ |
+ const base::string16 expected_title1 = base::ASCIIToUTF16("READY"); |
+ content::TitleWatcher title_watcher1( |
+ browser()->tab_strip_model()->GetActiveWebContents(), expected_title1); |
+ ui_test_utils::NavigateToURL(browser(), |
+ embedded_test_server()->GetURL("/test.html")); |
+ EXPECT_EQ(expected_title1, title_watcher1.WaitAndGetTitle()); |
+ |
+ histogram_tester_.ExpectBucketCount("ServiceWorker.StartNewWorker.Status", |
+ 0 /* SERVICE_WORKER_OK */, 1); |
falken
2016/10/04 04:50:51
nit: how about using SERVICE_WORKER_OK directly?
horo
2016/10/04 06:54:07
Unfortunately SERVICE_WORKER_OK is not available u
|
+ |
+ sw_context->StopAllServiceWorkersForOrigin( |
+ embedded_test_server()->base_url()); |
falken
2016/10/04 04:50:51
Since StopWorker is async, it seems like this func
horo
2016/10/04 06:54:07
Even if the worker is still stopping, we record th
|
+ |
+ const base::string16 expected_title2 = base::ASCIIToUTF16("Done"); |
+ content::TitleWatcher title_watcher2( |
+ browser()->tab_strip_model()->GetActiveWebContents(), expected_title2); |
+ |
+ histogram_tester_.ExpectTotalCount( |
+ "ServiceWorker.StartWorker.StatusByPurpose_NAVIGATION_HINT_LINK_MOUSE_" |
+ "DOWN", |
falken
2016/10/04 04:50:51
nit: Can you preserve this string on one line for
horo
2016/10/04 06:54:07
Done.
|
+ 0); |
+ content::SimulateMouseClickAt( |
+ browser()->tab_strip_model()->GetActiveWebContents(), 0, |
+ blink::WebMouseEvent::Button::Left, gfx::Point(0, 0)); |
+ EXPECT_EQ(expected_title2, title_watcher2.WaitAndGetTitle()); |
+ } |
+ |
base::HistogramTester histogram_tester_; |
private: |
@@ -545,58 +602,8 @@ IN_PROC_BROWSER_TEST_F(ServiceWorkerSpeculativeLaunchTest, MouseDown) { |
" e.respondWith(new Response('<title>Done</title>'," |
" {headers: {'Content-Type': 'text/html'}}));" |
"};"); |
- WriteFile( |
- FILE_PATH_LITERAL("test.html"), |
- "<script>" |
- "navigator.serviceWorker.register('./sw.js', {scope: './scope/'})" |
- " .then(function(reg) {" |
- " reg.addEventListener('updatefound', function() {" |
- " var worker = reg.installing;" |
- " worker.addEventListener('statechange', function() {" |
- " if (worker.state == 'activated')" |
- " document.title = 'READY';" |
- " });" |
- " });" |
- " });" |
- "</script>" |
- "<body style='margin:0; padding:0;'>" |
- "<a href='./scope/' style='position:fixed; width:1px; height:1px;'></a>" |
- "</body>"); |
- |
- embedded_test_server()->ServeFilesFromDirectory( |
- service_worker_dir_.GetPath()); |
- ASSERT_TRUE(embedded_test_server()->Start()); |
- |
- content::ServiceWorkerContext* sw_context = |
- content::BrowserContext::GetDefaultStoragePartition(browser()->profile()) |
- ->GetServiceWorkerContext(); |
- |
- const base::string16 expected_title1 = base::ASCIIToUTF16("READY"); |
- content::TitleWatcher title_watcher1( |
- browser()->tab_strip_model()->GetActiveWebContents(), expected_title1); |
- ui_test_utils::NavigateToURL(browser(), |
- embedded_test_server()->GetURL("/test.html")); |
- EXPECT_EQ(expected_title1, title_watcher1.WaitAndGetTitle()); |
- |
- histogram_tester_.ExpectBucketCount("ServiceWorker.StartNewWorker.Status", |
- 0 /* SERVICE_WORKER_OK */, 1); |
- |
- sw_context->StopAllServiceWorkersForOrigin( |
- embedded_test_server()->base_url()); |
- |
- const base::string16 expected_title2 = base::ASCIIToUTF16("Done"); |
- content::TitleWatcher title_watcher2( |
- browser()->tab_strip_model()->GetActiveWebContents(), expected_title2); |
- |
- histogram_tester_.ExpectTotalCount( |
- "ServiceWorker.StartWorker.StatusByPurpose_NAVIGATION_HINT_LINK_MOUSE_" |
- "DOWN", |
- 0); |
- content::SimulateMouseClickAt( |
- browser()->tab_strip_model()->GetActiveWebContents(), 0, |
- blink::WebMouseEvent::Button::Left, gfx::Point(0, 0)); |
- EXPECT_EQ(expected_title2, title_watcher2.WaitAndGetTitle()); |
- |
+ WriteTestHtmlFile(); |
+ RunNavigationHintTest(); |
// The service worker must be started by a navigation hint. |
histogram_tester_.ExpectBucketCount( |
"ServiceWorker.StartWorker.StatusByPurpose_NAVIGATION_HINT_LINK_MOUSE_" |
@@ -604,4 +611,19 @@ IN_PROC_BROWSER_TEST_F(ServiceWorkerSpeculativeLaunchTest, MouseDown) { |
0 /* SERVICE_WORKER_OK */, 1); |
falken
2016/10/04 04:50:51
const string and SERVICE_WORKER_OK here too
horo
2016/10/04 06:54:07
Done.
|
} |
+IN_PROC_BROWSER_TEST_F(ServiceWorkerSpeculativeLaunchTest, |
+ NoFetchEventHandler) { |
+ WriteFile(FILE_PATH_LITERAL("sw.js"), "// no fetch event handler."); |
falken
2016/10/04 04:50:51
This doesn't need mock headers with javascript con
horo
2016/10/04 06:54:07
Ah, we don't need scope.html.mock-http-headers.
e
|
+ WriteFile(FILE_PATH_LITERAL("scope.html"), "<title>Done</title>"); |
+ WriteFile(FILE_PATH_LITERAL("scope.html.mock-http-headers"), |
+ "HTTP/1.1 200 OK\nContent-Type: text/html; charset=utf-8\n"); |
+ WriteTestHtmlFile(); |
+ RunNavigationHintTest(); |
+ // The service worker must NOT be started by a navigation hint. |
+ histogram_tester_.ExpectTotalCount( |
+ "ServiceWorker.StartWorker.StatusByPurpose_NAVIGATION_HINT_LINK_MOUSE_" |
+ "DOWN", |
falken
2016/10/04 04:50:51
const string
horo
2016/10/04 06:54:07
Done.
|
+ 0); |
+} |
+ |
} // namespace |