Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / chrome / browser / chrome_service_worker_browsertest.cc
blob8d00da3bb4a4b9fc272319762a07b605a47cfb20
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
6 // embedder.
8 #include "base/bind.h"
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/profiles/profile.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/tabs/tab_strip_model.h"
15 #include "chrome/test/base/in_process_browser_test.h"
16 #include "content/public/browser/browser_context.h"
17 #include "content/public/browser/service_worker_context.h"
18 #include "content/public/browser/storage_partition.h"
19 #include "content/public/browser/web_contents.h"
20 #include "net/test/embedded_test_server/embedded_test_server.h"
22 namespace {
24 class ChromeServiceWorkerTest : public InProcessBrowserTest {
25 protected:
26 ChromeServiceWorkerTest() {
27 EXPECT_TRUE(service_worker_dir_.CreateUniqueTempDir());
30 void WriteFile(const base::FilePath::StringType& filename,
31 base::StringPiece contents) {
32 EXPECT_EQ(base::checked_cast<int>(contents.size()),
33 base::WriteFile(service_worker_dir_.path().Append(filename),
34 contents.data(),
35 contents.size()));
38 base::ScopedTempDir service_worker_dir_;
41 static void ExpectResultAndRun(bool expected,
42 const base::Closure& continuation,
43 bool actual) {
44 EXPECT_EQ(expected, actual);
45 continuation.Run();
48 // http://crbug.com/368570
49 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerTest,
50 CanShutDownWithRegisteredServiceWorker) {
51 WriteFile(FILE_PATH_LITERAL("service_worker.js"), "");
52 WriteFile(FILE_PATH_LITERAL("service_worker.js.mock-http-headers"),
53 "HTTP/1.1 200 OK\nContent-Type: text/javascript");
55 embedded_test_server()->ServeFilesFromDirectory(service_worker_dir_.path());
56 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
58 content::ServiceWorkerContext* sw_context =
59 content::BrowserContext::GetDefaultStoragePartition(browser()->profile())
60 ->GetServiceWorkerContext();
62 base::RunLoop run_loop;
63 sw_context->RegisterServiceWorker(
64 embedded_test_server()->GetURL("/*"),
65 embedded_test_server()->GetURL("/service_worker.js"),
66 base::Bind(&ExpectResultAndRun, true, run_loop.QuitClosure()));
67 run_loop.Run();
69 // Leave the Service Worker registered, and make sure that the browser can
70 // shut down without DCHECK'ing. It'd be nice to check here that the SW is
71 // actually occupying a process, but we don't yet have the public interface to
72 // do that.
75 } // namespace