Chromium Code Reviews| Index: content/browser/service_worker/service_worker_browsertest.cc |
| diff --git a/content/browser/service_worker/service_worker_browsertest.cc b/content/browser/service_worker/service_worker_browsertest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e347ec952a58c56e8515977f726e57c8a50caff6 |
| --- /dev/null |
| +++ b/content/browser/service_worker/service_worker_browsertest.cc |
| @@ -0,0 +1,138 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/bind.h" |
| +#include "base/callback.h" |
| +#include "base/command_line.h" |
| +#include "base/location.h" |
| +#include "base/run_loop.h" |
| +#include "content/browser/service_worker/embedded_worker_instance.h" |
| +#include "content/browser/service_worker/embedded_worker_registry.h" |
| +#include "content/browser/service_worker/service_worker_context_core.h" |
| +#include "content/browser/service_worker/service_worker_context_wrapper.h" |
| +#include "content/public/browser/browser_context.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/storage_partition.h" |
| +#include "content/public/browser/web_contents.h" |
| +#include "content/public/common/content_switches.h" |
| +#include "content/shell/browser/shell.h" |
| +#include "content/test/content_browser_test.h" |
| +#include "content/test/content_browser_test_utils.h" |
| +#include "net/test/embedded_test_server/embedded_test_server.h" |
| + |
| +namespace content { |
| + |
| +class ServiceWorkerBrowserTest : public ContentBrowserTest, |
| + public EmbeddedWorkerInstance::Observer { |
| + public: |
| + typedef ServiceWorkerBrowserTest self; |
| + |
| + ServiceWorkerBrowserTest() {} |
| + virtual ~ServiceWorkerBrowserTest() {} |
| + |
| + virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| + command_line->AppendSwitch(switches::kEnableServiceWorker); |
| + } |
| + |
| + virtual void SetUpOnMainThread() OVERRIDE { |
| + ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| + StoragePartition* partition = BrowserContext::GetDefaultStoragePartition( |
| + shell()->web_contents()->GetBrowserContext()); |
| + wrapper_ = partition->GetServiceWorkerContext(); |
| + } |
| + |
| + virtual void TearDownOnMainThread() OVERRIDE { |
| + base::RunLoop run_loop; |
| + BrowserThread::PostTask( |
| + BrowserThread::IO, FROM_HERE, |
| + base::Bind(&self::TearDownOnIOThread, this, run_loop.QuitClosure())); |
| + run_loop.Run(); |
| + wrapper_ = NULL; |
| + } |
| + |
| + void TearDownOnIOThread(const base::Closure& done_closure) { |
| + if (worker_) { |
| + worker_->RemoveObserver(this); |
| + worker_.reset(); |
| + } |
| + BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, done_closure); |
| + } |
| + |
| + void StartEmbeddedWorkerOnIOThread() { |
| + ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + worker_ = wrapper_->context()->embedded_worker_registry()->CreateWorker(); |
| + EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker_->status()); |
| + worker_->AddObserver(this); |
| + |
| + // TODO(kinuko): this manual wiring should go away when this gets wired |
| + // in the actual code path. |
| + ServiceWorkerProviderHost* provider_host = GetRegisteredProviderHost(); |
| + worker_->AddProcessReference(provider_host->process_id()); |
| + |
| + const int64 service_worker_version_id = 33L; |
| + const GURL script_url = embedded_test_server()->GetURL( |
| + "/service_worker/worker.js"); |
| + EXPECT_TRUE(worker_->Start(service_worker_version_id, script_url)); |
| + EXPECT_EQ(EmbeddedWorkerInstance::STARTING, worker_->status()); |
| + } |
| + |
| + void StopEmbeddedWorkerOnIOThread() { |
| + ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + EXPECT_EQ(EmbeddedWorkerInstance::RUNNING, worker_->status()); |
| + |
| + EXPECT_TRUE(worker_->Stop()); |
| + EXPECT_EQ(EmbeddedWorkerInstance::STOPPING, worker_->status()); |
| + } |
| + |
| + protected: |
| + virtual void OnStarted() OVERRIDE { |
| + ASSERT_TRUE(worker_ != NULL); |
| + ASSERT_FALSE(done_closure_.is_null()); |
| + EXPECT_EQ(EmbeddedWorkerInstance::RUNNING, worker_->status()); |
| + BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, done_closure_); |
| + } |
| + |
| + virtual void OnStopped() OVERRIDE { |
| + ASSERT_TRUE(worker_ != NULL); |
| + ASSERT_FALSE(done_closure_.is_null()); |
| + EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker_->status()); |
| + BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, done_closure_); |
| + } |
| + |
| + ServiceWorkerProviderHost* GetRegisteredProviderHost() { |
| + // Assumes only one provider host is registered at this point. |
| + std::vector<ServiceWorkerProviderHost*> providers; |
| + wrapper_->context()->GetAllProviderHosts(&providers); |
| + DCHECK_EQ(1U, providers.size()); |
| + return providers[0]; |
| + } |
| + |
| + scoped_refptr<ServiceWorkerContextWrapper> wrapper_; |
| + scoped_ptr<EmbeddedWorkerInstance> worker_; |
| + base::Closure done_closure_; |
| +}; |
| + |
| +IN_PROC_BROWSER_TEST_F(ServiceWorkerBrowserTest, EmbeddedWorkerBasic) { |
| + // Navigate to the page to set up a provider. |
| + NavigateToURLBlockUntilNavigationsComplete( |
| + shell(), embedded_test_server()->GetURL("/service_worker/index.html"), 1); |
| + |
| + // Start a worker and wait until OnStarted() is called. |
| + base::RunLoop start_run_loop; |
| + done_closure_ = start_run_loop.QuitClosure(); |
| + BrowserThread::PostTask( |
| + BrowserThread::IO, FROM_HERE, |
| + base::Bind(&self::StartEmbeddedWorkerOnIOThread, this)); |
| + start_run_loop.Run(); |
|
alecflett
2014/01/08 23:02:15
is it worth putting a check here that the worker i
kinuko
2014/01/09 09:08:54
Makes sense, I didn't include many cautious guards
|
| + |
| + // Stop a worker and wait until OnStopped() is called. |
| + base::RunLoop stop_run_loop; |
| + done_closure_ = stop_run_loop.QuitClosure(); |
| + BrowserThread::PostTask( |
| + BrowserThread::IO, FROM_HERE, |
| + base::Bind(&self::StopEmbeddedWorkerOnIOThread, this)); |
| + stop_run_loop.Run(); |
| +} |
| + |
| +} // namespace content |