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 22namespace { 23 24class 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 41static 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 49IN_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"); 54 55 embedded_test_server()->ServeFilesFromDirectory(service_worker_dir_.path()); 56 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); 57 58 content::ServiceWorkerContext* sw_context = 59 content::BrowserContext::GetDefaultStoragePartition(browser()->profile()) 60 ->GetServiceWorkerContext(); 61 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(); 68 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. 73} 74 75} // namespace 76