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. 4 5 // This file tests that Service Workers (a Content feature) work in the Chromium 6 // embedder. 7 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" 21 22 namespace { 23 24 class ChromeServiceWorkerTest : public InProcessBrowserTest { 25 protected: 26 ChromeServiceWorkerTest() { 27 EXPECT_TRUE(service_worker_dir_.CreateUniqueTempDir()); 28 } 29 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())); 36 } 37 38 base::ScopedTempDir service_worker_dir_; 39 }; 40 41 static void ExpectResultAndRun(bool expected, 42 const base::Closure& continuation, 43 bool actual) { 44 EXPECT_EQ(expected, actual); 45 continuation.Run(); 46 } 47 48 // http://crbug.com/368570 49 IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerTest, 50 CanShutDownWithRegisteredServiceWorker) { 51 WriteFile(FILE_PATH_LITERAL("service_worker.js"), ""); 52 53 embedded_test_server()->ServeFilesFromDirectory(service_worker_dir_.path()); 54 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); 55 56 content::ServiceWorkerContext* sw_context = 57 content::BrowserContext::GetDefaultStoragePartition(browser()->profile()) 58 ->GetServiceWorkerContext(); 59 60 base::RunLoop run_loop; 61 sw_context->RegisterServiceWorker( 62 embedded_test_server()->GetURL("/*"), 63 embedded_test_server()->GetURL("/service_worker.js"), 64 base::Bind(&ExpectResultAndRun, true, run_loop.QuitClosure())); 65 run_loop.Run(); 66 67 // Leave the Service Worker registered, and make sure that the browser can 68 // shut down without DCHECK'ing. It'd be nice to check here that the SW is 69 // actually occupying a process, but we don't yet have the public interface to 70 // do that. 71 } 72 73 } // namespace 74