| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/run_loop.h" | 6 #include "base/run_loop.h" |
| 7 #include "base/stl_util.h" | 7 #include "base/stl_util.h" |
| 8 #include "content/browser/service_worker/embedded_worker_instance.h" | 8 #include "content/browser/service_worker/embedded_worker_instance.h" |
| 9 #include "content/browser/service_worker/embedded_worker_registry.h" | 9 #include "content/browser/service_worker/embedded_worker_registry.h" |
| 10 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 10 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} | 36 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} |
| 37 | 37 |
| 38 void SetUp() override { | 38 void SetUp() override { |
| 39 helper_.reset( | 39 helper_.reset( |
| 40 new EmbeddedWorkerTestHelper(base::FilePath(), kRenderProcessId)); | 40 new EmbeddedWorkerTestHelper(base::FilePath(), kRenderProcessId)); |
| 41 } | 41 } |
| 42 | 42 |
| 43 void TearDown() override { helper_.reset(); } | 43 void TearDown() override { helper_.reset(); } |
| 44 | 44 |
| 45 ServiceWorkerStatusCode StartWorker(EmbeddedWorkerInstance* worker, | 45 ServiceWorkerStatusCode StartWorker(EmbeddedWorkerInstance* worker, |
| 46 int id, const GURL& pattern, | 46 std::string id, |
| 47 const GURL& pattern, |
| 47 const GURL& url) { | 48 const GURL& url) { |
| 48 ServiceWorkerStatusCode status; | 49 ServiceWorkerStatusCode status; |
| 49 base::RunLoop run_loop; | 50 base::RunLoop run_loop; |
| 50 worker->Start(id, pattern, url, false, | 51 worker->Start(id, pattern, url, false, |
| 51 base::Bind(&SaveStatusAndCall, &status, | 52 base::Bind(&SaveStatusAndCall, &status, |
| 52 run_loop.QuitClosure())); | 53 run_loop.QuitClosure())); |
| 53 run_loop.Run(); | 54 run_loop.Run(); |
| 54 return status; | 55 return status; |
| 55 } | 56 } |
| 56 | 57 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 68 | 69 |
| 69 private: | 70 private: |
| 70 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstanceTest); | 71 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstanceTest); |
| 71 }; | 72 }; |
| 72 | 73 |
| 73 TEST_F(EmbeddedWorkerInstanceTest, StartAndStop) { | 74 TEST_F(EmbeddedWorkerInstanceTest, StartAndStop) { |
| 74 scoped_ptr<EmbeddedWorkerInstance> worker = | 75 scoped_ptr<EmbeddedWorkerInstance> worker = |
| 75 embedded_worker_registry()->CreateWorker(); | 76 embedded_worker_registry()->CreateWorker(); |
| 76 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); | 77 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); |
| 77 | 78 |
| 78 const int64 service_worker_version_id = 55L; | 79 const std::string service_worker_version_uuid = |
| 80 "12a91837-97e4-46a0-9a9a-d43d75f993d9"; |
| 79 const GURL pattern("http://example.com/"); | 81 const GURL pattern("http://example.com/"); |
| 80 const GURL url("http://example.com/worker.js"); | 82 const GURL url("http://example.com/worker.js"); |
| 81 | 83 |
| 82 // Simulate adding one process to the pattern. | 84 // Simulate adding one process to the pattern. |
| 83 helper_->SimulateAddProcessToPattern(pattern, kRenderProcessId); | 85 helper_->SimulateAddProcessToPattern(pattern, kRenderProcessId); |
| 84 | 86 |
| 85 // Start should succeed. | 87 // Start should succeed. |
| 86 ServiceWorkerStatusCode status; | 88 ServiceWorkerStatusCode status; |
| 87 base::RunLoop run_loop; | 89 base::RunLoop run_loop; |
| 88 worker->Start( | 90 worker->Start( |
| 89 service_worker_version_id, | 91 service_worker_version_uuid, pattern, url, false, |
| 90 pattern, | |
| 91 url, | |
| 92 false, | |
| 93 base::Bind(&SaveStatusAndCall, &status, run_loop.QuitClosure())); | 92 base::Bind(&SaveStatusAndCall, &status, run_loop.QuitClosure())); |
| 94 EXPECT_EQ(EmbeddedWorkerInstance::STARTING, worker->status()); | 93 EXPECT_EQ(EmbeddedWorkerInstance::STARTING, worker->status()); |
| 95 run_loop.Run(); | 94 run_loop.Run(); |
| 96 EXPECT_EQ(SERVICE_WORKER_OK, status); | 95 EXPECT_EQ(SERVICE_WORKER_OK, status); |
| 97 | 96 |
| 98 // The 'WorkerStarted' message should have been sent by | 97 // The 'WorkerStarted' message should have been sent by |
| 99 // EmbeddedWorkerTestHelper. | 98 // EmbeddedWorkerTestHelper. |
| 100 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING, worker->status()); | 99 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING, worker->status()); |
| 101 EXPECT_EQ(kRenderProcessId, worker->process_id()); | 100 EXPECT_EQ(kRenderProcessId, worker->process_id()); |
| 102 | 101 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 114 EmbeddedWorkerMsg_StartWorker::ID)); | 113 EmbeddedWorkerMsg_StartWorker::ID)); |
| 115 ASSERT_TRUE(ipc_sink()->GetUniqueMessageMatching( | 114 ASSERT_TRUE(ipc_sink()->GetUniqueMessageMatching( |
| 116 EmbeddedWorkerMsg_StopWorker::ID)); | 115 EmbeddedWorkerMsg_StopWorker::ID)); |
| 117 } | 116 } |
| 118 | 117 |
| 119 TEST_F(EmbeddedWorkerInstanceTest, StopWhenDevToolsAttached) { | 118 TEST_F(EmbeddedWorkerInstanceTest, StopWhenDevToolsAttached) { |
| 120 scoped_ptr<EmbeddedWorkerInstance> worker = | 119 scoped_ptr<EmbeddedWorkerInstance> worker = |
| 121 embedded_worker_registry()->CreateWorker(); | 120 embedded_worker_registry()->CreateWorker(); |
| 122 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); | 121 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); |
| 123 | 122 |
| 124 const int64 service_worker_version_id = 55L; | 123 const std::string service_worker_version_uuid = |
| 124 "12a91837-97e4-46a0-9a9a-d43d75f993d9"; |
| 125 const GURL pattern("http://example.com/"); | 125 const GURL pattern("http://example.com/"); |
| 126 const GURL url("http://example.com/worker.js"); | 126 const GURL url("http://example.com/worker.js"); |
| 127 | 127 |
| 128 // Simulate adding one process to the pattern. | 128 // Simulate adding one process to the pattern. |
| 129 helper_->SimulateAddProcessToPattern(pattern, kRenderProcessId); | 129 helper_->SimulateAddProcessToPattern(pattern, kRenderProcessId); |
| 130 | 130 |
| 131 // Start the worker and then call StopIfIdle(). | 131 // Start the worker and then call StopIfIdle(). |
| 132 EXPECT_EQ(SERVICE_WORKER_OK, | 132 EXPECT_EQ( |
| 133 StartWorker(worker.get(), service_worker_version_id, pattern, url)); | 133 SERVICE_WORKER_OK, |
| 134 StartWorker(worker.get(), service_worker_version_uuid, pattern, url)); |
| 134 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING, worker->status()); | 135 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING, worker->status()); |
| 135 EXPECT_EQ(kRenderProcessId, worker->process_id()); | 136 EXPECT_EQ(kRenderProcessId, worker->process_id()); |
| 136 worker->StopIfIdle(); | 137 worker->StopIfIdle(); |
| 137 EXPECT_EQ(EmbeddedWorkerInstance::STOPPING, worker->status()); | 138 EXPECT_EQ(EmbeddedWorkerInstance::STOPPING, worker->status()); |
| 138 base::RunLoop().RunUntilIdle(); | 139 base::RunLoop().RunUntilIdle(); |
| 139 | 140 |
| 140 // The worker must be stopped now. | 141 // The worker must be stopped now. |
| 141 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); | 142 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); |
| 142 | 143 |
| 143 // Set devtools_attached to true, and do the same. | 144 // Set devtools_attached to true, and do the same. |
| 144 worker->set_devtools_attached(true); | 145 worker->set_devtools_attached(true); |
| 145 | 146 |
| 146 EXPECT_EQ(SERVICE_WORKER_OK, | 147 EXPECT_EQ( |
| 147 StartWorker(worker.get(), service_worker_version_id, pattern, url)); | 148 SERVICE_WORKER_OK, |
| 149 StartWorker(worker.get(), service_worker_version_uuid, pattern, url)); |
| 148 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING, worker->status()); | 150 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING, worker->status()); |
| 149 EXPECT_EQ(kRenderProcessId, worker->process_id()); | 151 EXPECT_EQ(kRenderProcessId, worker->process_id()); |
| 150 worker->StopIfIdle(); | 152 worker->StopIfIdle(); |
| 151 base::RunLoop().RunUntilIdle(); | 153 base::RunLoop().RunUntilIdle(); |
| 152 | 154 |
| 153 // The worker must not be stopped this time. | 155 // The worker must not be stopped this time. |
| 154 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING, worker->status()); | 156 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING, worker->status()); |
| 155 | 157 |
| 156 // Calling Stop() actually stops the worker regardless of whether devtools | 158 // Calling Stop() actually stops the worker regardless of whether devtools |
| 157 // is attached or not. | 159 // is attached or not. |
| 158 EXPECT_EQ(SERVICE_WORKER_OK, worker->Stop()); | 160 EXPECT_EQ(SERVICE_WORKER_OK, worker->Stop()); |
| 159 base::RunLoop().RunUntilIdle(); | 161 base::RunLoop().RunUntilIdle(); |
| 160 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); | 162 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); |
| 161 } | 163 } |
| 162 | 164 |
| 163 } // namespace content | 165 } // namespace content |
| OLD | NEW |