| 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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 | 48 |
| 49 static void SaveStatusAndCall(ServiceWorkerStatusCode* out, | 49 static void SaveStatusAndCall(ServiceWorkerStatusCode* out, |
| 50 const base::Closure& callback, | 50 const base::Closure& callback, |
| 51 ServiceWorkerStatusCode status) { | 51 ServiceWorkerStatusCode status) { |
| 52 *out = status; | 52 *out = status; |
| 53 callback.Run(); | 53 callback.Run(); |
| 54 } | 54 } |
| 55 | 55 |
| 56 TEST_F(EmbeddedWorkerInstanceTest, StartAndStop) { | 56 TEST_F(EmbeddedWorkerInstanceTest, StartAndStop) { |
| 57 scoped_ptr<EmbeddedWorkerInstance> worker = | 57 scoped_ptr<EmbeddedWorkerInstance> worker = |
| 58 embedded_worker_registry()->CreateWorker(); | 58 embedded_worker_registry()->CreateWorker(GURL("http://example.com/*")); |
| 59 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); | 59 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); |
| 60 | 60 |
| 61 const int embedded_worker_id = worker->embedded_worker_id(); | 61 const int embedded_worker_id = worker->embedded_worker_id(); |
| 62 const int64 service_worker_version_id = 55L; | 62 const int64 service_worker_version_id = 55L; |
| 63 const GURL scope("http://example.com/*"); | |
| 64 const GURL url("http://example.com/worker.js"); | 63 const GURL url("http://example.com/worker.js"); |
| 65 | 64 |
| 66 // Simulate adding one process to the worker. | 65 // Simulate adding one process to the worker. |
| 67 helper_->SimulateAddProcessToWorker(embedded_worker_id, kRenderProcessId); | 66 helper_->SimulateAddProcessToWorker(embedded_worker_id, kRenderProcessId); |
| 68 | 67 |
| 69 // Start should succeed. | 68 // Start should succeed. |
| 70 ServiceWorkerStatusCode status; | 69 ServiceWorkerStatusCode status; |
| 71 base::RunLoop run_loop; | 70 base::RunLoop run_loop; |
| 72 worker->Start( | 71 worker->Start( |
| 73 service_worker_version_id, | 72 service_worker_version_id, |
| 74 scope, | |
| 75 url, | 73 url, |
| 76 std::vector<int>(), | 74 std::vector<int>(), |
| 77 base::Bind(&SaveStatusAndCall, &status, run_loop.QuitClosure())); | 75 base::Bind(&SaveStatusAndCall, &status, run_loop.QuitClosure())); |
| 78 run_loop.Run(); | 76 run_loop.Run(); |
| 79 EXPECT_EQ(SERVICE_WORKER_OK, status); | 77 EXPECT_EQ(SERVICE_WORKER_OK, status); |
| 80 EXPECT_EQ(EmbeddedWorkerInstance::STARTING, worker->status()); | 78 EXPECT_EQ(EmbeddedWorkerInstance::STARTING, worker->status()); |
| 81 base::RunLoop().RunUntilIdle(); | 79 base::RunLoop().RunUntilIdle(); |
| 82 | 80 |
| 83 // Worker started message should be notified (by EmbeddedWorkerTestHelper). | 81 // Worker started message should be notified (by EmbeddedWorkerTestHelper). |
| 84 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING, worker->status()); | 82 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING, worker->status()); |
| 85 EXPECT_EQ(kRenderProcessId, worker->process_id()); | 83 EXPECT_EQ(kRenderProcessId, worker->process_id()); |
| 86 | 84 |
| 87 // Stop the worker. | 85 // Stop the worker. |
| 88 EXPECT_EQ(SERVICE_WORKER_OK, worker->Stop()); | 86 EXPECT_EQ(SERVICE_WORKER_OK, worker->Stop()); |
| 89 EXPECT_EQ(EmbeddedWorkerInstance::STOPPING, worker->status()); | 87 EXPECT_EQ(EmbeddedWorkerInstance::STOPPING, worker->status()); |
| 90 base::RunLoop().RunUntilIdle(); | 88 base::RunLoop().RunUntilIdle(); |
| 91 | 89 |
| 92 // Worker stopped message should be notified (by EmbeddedWorkerTestHelper). | 90 // Worker stopped message should be notified (by EmbeddedWorkerTestHelper). |
| 93 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); | 91 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); |
| 94 | 92 |
| 95 // Verify that we've sent two messages to start and terminate the worker. | 93 // Verify that we've sent two messages to start and terminate the worker. |
| 96 ASSERT_TRUE(ipc_sink()->GetUniqueMessageMatching( | 94 ASSERT_TRUE(ipc_sink()->GetUniqueMessageMatching( |
| 97 EmbeddedWorkerMsg_StartWorker::ID)); | 95 EmbeddedWorkerMsg_StartWorker::ID)); |
| 98 ASSERT_TRUE(ipc_sink()->GetUniqueMessageMatching( | 96 ASSERT_TRUE(ipc_sink()->GetUniqueMessageMatching( |
| 99 EmbeddedWorkerMsg_StopWorker::ID)); | 97 EmbeddedWorkerMsg_StopWorker::ID)); |
| 100 } | 98 } |
| 101 | 99 |
| 102 TEST_F(EmbeddedWorkerInstanceTest, InstanceDestroyedBeforeStartFinishes) { | 100 TEST_F(EmbeddedWorkerInstanceTest, InstanceDestroyedBeforeStartFinishes) { |
| 103 scoped_ptr<EmbeddedWorkerInstance> worker = | 101 scoped_ptr<EmbeddedWorkerInstance> worker = |
| 104 embedded_worker_registry()->CreateWorker(); | 102 embedded_worker_registry()->CreateWorker(GURL("http://example.com/*")); |
| 105 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); | 103 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); |
| 106 | 104 |
| 107 const int64 service_worker_version_id = 55L; | 105 const int64 service_worker_version_id = 55L; |
| 108 const GURL scope("http://example.com/*"); | |
| 109 const GURL url("http://example.com/worker.js"); | 106 const GURL url("http://example.com/worker.js"); |
| 110 | 107 |
| 111 ServiceWorkerStatusCode status; | 108 ServiceWorkerStatusCode status; |
| 112 base::RunLoop run_loop; | 109 base::RunLoop run_loop; |
| 113 // Begin starting the worker. | 110 // Begin starting the worker. |
| 114 std::vector<int> available_process; | 111 std::vector<int> available_process; |
| 115 available_process.push_back(kRenderProcessId); | 112 available_process.push_back(kRenderProcessId); |
| 116 worker->Start( | 113 worker->Start( |
| 117 service_worker_version_id, | 114 service_worker_version_id, |
| 118 scope, | |
| 119 url, | 115 url, |
| 120 available_process, | 116 available_process, |
| 121 base::Bind(&SaveStatusAndCall, &status, run_loop.QuitClosure())); | 117 base::Bind(&SaveStatusAndCall, &status, run_loop.QuitClosure())); |
| 122 // But destroy it before it gets a chance to complete. | 118 // But destroy it before it gets a chance to complete. |
| 123 worker.reset(); | 119 worker.reset(); |
| 124 run_loop.Run(); | 120 run_loop.Run(); |
| 125 EXPECT_EQ(SERVICE_WORKER_ERROR_ABORT, status); | 121 EXPECT_EQ(SERVICE_WORKER_ERROR_ABORT, status); |
| 126 | 122 |
| 127 // Verify that we didn't send the message to start the worker. | 123 // Verify that we didn't send the message to start the worker. |
| 128 ASSERT_FALSE( | 124 ASSERT_FALSE( |
| 129 ipc_sink()->GetUniqueMessageMatching(EmbeddedWorkerMsg_StartWorker::ID)); | 125 ipc_sink()->GetUniqueMessageMatching(EmbeddedWorkerMsg_StartWorker::ID)); |
| 130 } | 126 } |
| 131 | 127 |
| 132 TEST_F(EmbeddedWorkerInstanceTest, ChooseProcess) { | 128 TEST_F(EmbeddedWorkerInstanceTest, ChooseProcess) { |
| 133 scoped_ptr<EmbeddedWorkerInstance> worker = | 129 scoped_ptr<EmbeddedWorkerInstance> worker = |
| 134 embedded_worker_registry()->CreateWorker(); | 130 embedded_worker_registry()->CreateWorker(GURL("http://example.com/*")); |
| 135 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); | 131 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); |
| 136 | 132 |
| 137 // Simulate adding processes to the worker. | 133 // Simulate adding processes to the worker. |
| 138 // Process 1 has 1 ref, 2 has 2 refs and 3 has 3 refs. | 134 // Process 1 has 1 ref, 2 has 2 refs and 3 has 3 refs. |
| 139 const int embedded_worker_id = worker->embedded_worker_id(); | 135 const int embedded_worker_id = worker->embedded_worker_id(); |
| 140 helper_->SimulateAddProcessToWorker(embedded_worker_id, 1); | 136 helper_->SimulateAddProcessToWorker(embedded_worker_id, 1); |
| 141 helper_->SimulateAddProcessToWorker(embedded_worker_id, 2); | 137 helper_->SimulateAddProcessToWorker(embedded_worker_id, 2); |
| 142 helper_->SimulateAddProcessToWorker(embedded_worker_id, 2); | 138 helper_->SimulateAddProcessToWorker(embedded_worker_id, 2); |
| 143 helper_->SimulateAddProcessToWorker(embedded_worker_id, 3); | 139 helper_->SimulateAddProcessToWorker(embedded_worker_id, 3); |
| 144 helper_->SimulateAddProcessToWorker(embedded_worker_id, 3); | 140 helper_->SimulateAddProcessToWorker(embedded_worker_id, 3); |
| 145 helper_->SimulateAddProcessToWorker(embedded_worker_id, 3); | 141 helper_->SimulateAddProcessToWorker(embedded_worker_id, 3); |
| 146 | 142 |
| 147 // Process 3 has the biggest # of references and it should be chosen. | 143 // Process 3 has the biggest # of references and it should be chosen. |
| 148 ServiceWorkerStatusCode status; | 144 ServiceWorkerStatusCode status; |
| 149 base::RunLoop run_loop; | 145 base::RunLoop run_loop; |
| 150 worker->Start( | 146 worker->Start( |
| 151 1L, | 147 1L, |
| 152 GURL("http://example.com/*"), | |
| 153 GURL("http://example.com/worker.js"), | 148 GURL("http://example.com/worker.js"), |
| 154 std::vector<int>(), | 149 std::vector<int>(), |
| 155 base::Bind(&SaveStatusAndCall, &status, run_loop.QuitClosure())); | 150 base::Bind(&SaveStatusAndCall, &status, run_loop.QuitClosure())); |
| 156 run_loop.Run(); | 151 run_loop.Run(); |
| 157 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); | 152 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); |
| 158 EXPECT_EQ(EmbeddedWorkerInstance::STARTING, worker->status()); | 153 EXPECT_EQ(EmbeddedWorkerInstance::STARTING, worker->status()); |
| 159 EXPECT_EQ(3, worker->process_id()); | 154 EXPECT_EQ(3, worker->process_id()); |
| 160 | 155 |
| 161 // Wait until started message is sent back. | 156 // Wait until started message is sent back. |
| 162 base::RunLoop().RunUntilIdle(); | 157 base::RunLoop().RunUntilIdle(); |
| 163 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING, worker->status()); | 158 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING, worker->status()); |
| 164 } | 159 } |
| 165 | 160 |
| 166 } // namespace content | 161 } // namespace content |
| OLD | NEW |