1cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved. 2cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 3cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// found in the LICENSE file. 4cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 5cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// This file tests that Service Workers (a Content feature) work in the Chromium 6cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// embedder. 7cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 8cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/bind.h" 9cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/files/scoped_temp_dir.h" 10cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/numerics/safe_conversions.h" 11cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/run_loop.h" 12cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/profiles/profile.h" 13cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/ui/browser.h" 14cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/ui/tabs/tab_strip_model.h" 15cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/test/base/in_process_browser_test.h" 16cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "content/public/browser/browser_context.h" 17cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "content/public/browser/service_worker_context.h" 18cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "content/public/browser/storage_partition.h" 19cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "content/public/browser/web_contents.h" 20cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "net/test/embedded_test_server/embedded_test_server.h" 21cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 22cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)namespace { 23cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 24cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class ChromeServiceWorkerTest : public InProcessBrowserTest { 25cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) protected: 26cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) ChromeServiceWorkerTest() { 27cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) EXPECT_TRUE(service_worker_dir_.CreateUniqueTempDir()); 28cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) } 29cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 30cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) void WriteFile(const base::FilePath::StringType& filename, 31cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) base::StringPiece contents) { 32cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) EXPECT_EQ(base::checked_cast<int>(contents.size()), 33cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) base::WriteFile(service_worker_dir_.path().Append(filename), 34cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) contents.data(), 35cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) contents.size())); 36cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) } 37cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 38cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) base::ScopedTempDir service_worker_dir_; 39cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}; 40cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 41cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)static void ExpectResultAndRun(bool expected, 42cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const base::Closure& continuation, 43cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) bool actual) { 44cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) EXPECT_EQ(expected, actual); 45cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) continuation.Run(); 46cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 47cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 48cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// http://crbug.com/368570 49cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(ChromeServiceWorkerTest, 50cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) CanShutDownWithRegisteredServiceWorker) { 51cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) WriteFile(FILE_PATH_LITERAL("service_worker.js"), ""); 526e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) WriteFile(FILE_PATH_LITERAL("service_worker.js.mock-http-headers"), 536e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) "HTTP/1.1 200 OK\nContent-Type: text/javascript"); 54cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 55cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) embedded_test_server()->ServeFilesFromDirectory(service_worker_dir_.path()); 56cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); 57cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 58cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) content::ServiceWorkerContext* sw_context = 59cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) content::BrowserContext::GetDefaultStoragePartition(browser()->profile()) 60cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) ->GetServiceWorkerContext(); 61cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 62cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) base::RunLoop run_loop; 63cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) sw_context->RegisterServiceWorker( 64cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) embedded_test_server()->GetURL("/*"), 65cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) embedded_test_server()->GetURL("/service_worker.js"), 66cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) base::Bind(&ExpectResultAndRun, true, run_loop.QuitClosure())); 67cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) run_loop.Run(); 68cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 69cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // Leave the Service Worker registered, and make sure that the browser can 70cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // shut down without DCHECK'ing. It'd be nice to check here that the SW is 71cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // actually occupying a process, but we don't yet have the public interface to 72cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // do that. 73cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 74cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 75cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} // namespace 76