OLD | NEW |
(Empty) | |
| 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 #include "base/bind.h" |
| 6 #include "base/callback.h" |
| 7 #include "base/command_line.h" |
| 8 #include "base/location.h" |
| 9 #include "base/run_loop.h" |
| 10 #include "content/browser/service_worker/embedded_worker_instance.h" |
| 11 #include "content/browser/service_worker/embedded_worker_registry.h" |
| 12 #include "content/browser/service_worker/service_worker_context_core.h" |
| 13 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 14 #include "content/public/browser/browser_context.h" |
| 15 #include "content/public/browser/browser_thread.h" |
| 16 #include "content/public/browser/storage_partition.h" |
| 17 #include "content/public/browser/web_contents.h" |
| 18 #include "content/public/common/content_switches.h" |
| 19 #include "content/shell/browser/shell.h" |
| 20 #include "content/test/content_browser_test.h" |
| 21 #include "content/test/content_browser_test_utils.h" |
| 22 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 23 |
| 24 namespace content { |
| 25 |
| 26 class ServiceWorkerBrowserTest : public ContentBrowserTest, |
| 27 public EmbeddedWorkerInstance::Observer { |
| 28 public: |
| 29 typedef ServiceWorkerBrowserTest self; |
| 30 |
| 31 ServiceWorkerBrowserTest() |
| 32 : last_worker_status_(EmbeddedWorkerInstance::STOPPED) {} |
| 33 virtual ~ServiceWorkerBrowserTest() {} |
| 34 |
| 35 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| 36 command_line->AppendSwitch(switches::kEnableServiceWorker); |
| 37 } |
| 38 |
| 39 virtual void SetUpOnMainThread() OVERRIDE { |
| 40 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| 41 StoragePartition* partition = BrowserContext::GetDefaultStoragePartition( |
| 42 shell()->web_contents()->GetBrowserContext()); |
| 43 wrapper_ = partition->GetServiceWorkerContext(); |
| 44 } |
| 45 |
| 46 virtual void TearDownOnMainThread() OVERRIDE { |
| 47 base::RunLoop run_loop; |
| 48 BrowserThread::PostTask( |
| 49 BrowserThread::IO, FROM_HERE, |
| 50 base::Bind(&self::TearDownOnIOThread, this, run_loop.QuitClosure())); |
| 51 run_loop.Run(); |
| 52 wrapper_ = NULL; |
| 53 } |
| 54 |
| 55 void TearDownOnIOThread(const base::Closure& done_closure) { |
| 56 if (worker_) { |
| 57 worker_->RemoveObserver(this); |
| 58 worker_.reset(); |
| 59 } |
| 60 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, done_closure); |
| 61 } |
| 62 |
| 63 void StartEmbeddedWorkerOnIOThread() { |
| 64 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 65 worker_ = wrapper_->context()->embedded_worker_registry()->CreateWorker(); |
| 66 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker_->status()); |
| 67 worker_->AddObserver(this); |
| 68 |
| 69 // TODO(kinuko): this manual wiring should go away when this gets wired |
| 70 // in the actual code path. |
| 71 ServiceWorkerProviderHost* provider_host = GetRegisteredProviderHost(); |
| 72 worker_->AddProcessReference(provider_host->process_id()); |
| 73 |
| 74 const int64 service_worker_version_id = 33L; |
| 75 const GURL script_url = embedded_test_server()->GetURL( |
| 76 "/service_worker/worker.js"); |
| 77 const bool started = worker_->Start(service_worker_version_id, script_url); |
| 78 |
| 79 last_worker_status_ = worker_->status(); |
| 80 EXPECT_TRUE(started); |
| 81 EXPECT_EQ(EmbeddedWorkerInstance::STARTING, last_worker_status_); |
| 82 |
| 83 if (!started && !done_closure_.is_null()) |
| 84 done_closure_.Run(); |
| 85 } |
| 86 |
| 87 void StopEmbeddedWorkerOnIOThread() { |
| 88 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 89 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING, worker_->status()); |
| 90 |
| 91 const bool stopped = worker_->Stop(); |
| 92 |
| 93 last_worker_status_ = worker_->status(); |
| 94 EXPECT_TRUE(stopped); |
| 95 EXPECT_EQ(EmbeddedWorkerInstance::STOPPING, last_worker_status_); |
| 96 |
| 97 if (!stopped && !done_closure_.is_null()) |
| 98 done_closure_.Run(); |
| 99 } |
| 100 |
| 101 protected: |
| 102 // Embe3ddedWorkerInstance::Observer overrides: |
| 103 virtual void OnStarted() OVERRIDE { |
| 104 ASSERT_TRUE(worker_ != NULL); |
| 105 ASSERT_FALSE(done_closure_.is_null()); |
| 106 last_worker_status_ = worker_->status(); |
| 107 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, done_closure_); |
| 108 } |
| 109 virtual void OnStopped() OVERRIDE { |
| 110 ASSERT_TRUE(worker_ != NULL); |
| 111 ASSERT_FALSE(done_closure_.is_null()); |
| 112 last_worker_status_ = worker_->status(); |
| 113 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, done_closure_); |
| 114 } |
| 115 |
| 116 ServiceWorkerProviderHost* GetRegisteredProviderHost() { |
| 117 // Assumes only one provider host is registered at this point. |
| 118 std::vector<ServiceWorkerProviderHost*> providers; |
| 119 wrapper_->context()->GetAllProviderHosts(&providers); |
| 120 DCHECK_EQ(1U, providers.size()); |
| 121 return providers[0]; |
| 122 } |
| 123 |
| 124 scoped_refptr<ServiceWorkerContextWrapper> wrapper_; |
| 125 scoped_ptr<EmbeddedWorkerInstance> worker_; |
| 126 EmbeddedWorkerInstance::Status last_worker_status_; |
| 127 |
| 128 // Called by EmbeddedWorkerInstance::Observer overrides so that |
| 129 // test code can wait for the worker status notifications. |
| 130 base::Closure done_closure_; |
| 131 }; |
| 132 |
| 133 IN_PROC_BROWSER_TEST_F(ServiceWorkerBrowserTest, EmbeddedWorkerBasic) { |
| 134 // Navigate to the page to set up a provider. |
| 135 NavigateToURLBlockUntilNavigationsComplete( |
| 136 shell(), embedded_test_server()->GetURL("/service_worker/index.html"), 1); |
| 137 |
| 138 // Start a worker and wait until OnStarted() is called. |
| 139 base::RunLoop start_run_loop; |
| 140 done_closure_ = start_run_loop.QuitClosure(); |
| 141 BrowserThread::PostTask( |
| 142 BrowserThread::IO, FROM_HERE, |
| 143 base::Bind(&self::StartEmbeddedWorkerOnIOThread, this)); |
| 144 start_run_loop.Run(); |
| 145 |
| 146 ASSERT_EQ(EmbeddedWorkerInstance::RUNNING, last_worker_status_); |
| 147 |
| 148 // Stop a worker and wait until OnStopped() is called. |
| 149 base::RunLoop stop_run_loop; |
| 150 done_closure_ = stop_run_loop.QuitClosure(); |
| 151 BrowserThread::PostTask( |
| 152 BrowserThread::IO, FROM_HERE, |
| 153 base::Bind(&self::StopEmbeddedWorkerOnIOThread, this)); |
| 154 stop_run_loop.Run(); |
| 155 |
| 156 ASSERT_EQ(EmbeddedWorkerInstance::STOPPED, last_worker_status_); |
| 157 } |
| 158 |
| 159 } // namespace content |
OLD | NEW |