1 // Copyright 2014 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.
5 // This file tests that Service Workers (a Content feature) work in the Chromium
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/numerics/safe_conversions.h"
11 #include "base/run_loop.h"
12 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_window.h"
16 #include "chrome/browser/ui/tabs/tab_strip_model.h"
17 #include "chrome/test/base/in_process_browser_test.h"
18 #include "chrome/test/base/ui_test_utils.h"
19 #include "content/public/browser/browser_context.h"
20 #include "content/public/browser/service_worker_context.h"
21 #include "content/public/browser/storage_partition.h"
22 #include "content/public/browser/web_contents.h"
23 #include "net/test/embedded_test_server/embedded_test_server.h"
27 class ChromeServiceWorkerTest
: public InProcessBrowserTest
{
29 ChromeServiceWorkerTest() {
30 EXPECT_TRUE(service_worker_dir_
.CreateUniqueTempDir());
33 void WriteFile(const base::FilePath::StringType
& filename
,
34 base::StringPiece contents
) {
35 EXPECT_EQ(base::checked_cast
<int>(contents
.size()),
36 base::WriteFile(service_worker_dir_
.path().Append(filename
),
41 base::ScopedTempDir service_worker_dir_
;
44 static void ExpectResultAndRun(bool expected
,
45 const base::Closure
& continuation
,
47 EXPECT_EQ(expected
, actual
);
51 // http://crbug.com/368570
52 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerTest
,
53 CanShutDownWithRegisteredServiceWorker
) {
54 WriteFile(FILE_PATH_LITERAL("service_worker.js"), "");
55 WriteFile(FILE_PATH_LITERAL("service_worker.js.mock-http-headers"),
56 "HTTP/1.1 200 OK\nContent-Type: text/javascript");
58 embedded_test_server()->ServeFilesFromDirectory(service_worker_dir_
.path());
59 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
61 content::ServiceWorkerContext
* sw_context
=
62 content::BrowserContext::GetDefaultStoragePartition(browser()->profile())
63 ->GetServiceWorkerContext();
65 base::RunLoop run_loop
;
66 sw_context
->RegisterServiceWorker(
67 embedded_test_server()->GetURL("/"),
68 embedded_test_server()->GetURL("/service_worker.js"),
69 base::Bind(&ExpectResultAndRun
, true, run_loop
.QuitClosure()));
72 // Leave the Service Worker registered, and make sure that the browser can
73 // shut down without DCHECK'ing. It'd be nice to check here that the SW is
74 // actually occupying a process, but we don't yet have the public interface to
78 // http://crbug.com/419290
79 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerTest
,
80 CanCloseIncognitoWindowWithServiceWorkerController
) {
81 WriteFile(FILE_PATH_LITERAL("service_worker.js"), "");
82 WriteFile(FILE_PATH_LITERAL("service_worker.js.mock-http-headers"),
83 "HTTP/1.1 200 OK\nContent-Type: text/javascript");
84 WriteFile(FILE_PATH_LITERAL("test.html"), "");
86 embedded_test_server()->ServeFilesFromDirectory(service_worker_dir_
.path());
87 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
89 Browser
* incognito
= CreateIncognitoBrowser();
90 content::ServiceWorkerContext
* sw_context
=
91 content::BrowserContext::GetDefaultStoragePartition(incognito
->profile())
92 ->GetServiceWorkerContext();
94 base::RunLoop run_loop
;
95 sw_context
->RegisterServiceWorker(
96 embedded_test_server()->GetURL("/"),
97 embedded_test_server()->GetURL("/service_worker.js"),
98 base::Bind(&ExpectResultAndRun
, true, run_loop
.QuitClosure()));
101 ui_test_utils::NavigateToURL(incognito
,
102 embedded_test_server()->GetURL("/test.html"));
104 content::WindowedNotificationObserver
observer(
105 chrome::NOTIFICATION_BROWSER_CLOSED
, content::Source
<Browser
>(incognito
));
106 incognito
->window()->Close();
109 // Test passes if we don't crash.