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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 ServiceWorkerStatusCode status) { | 53 ServiceWorkerStatusCode status) { |
54 *out = status; | 54 *out = status; |
55 callback.Run(); | 55 callback.Run(); |
56 } | 56 } |
57 | 57 |
58 TEST_F(EmbeddedWorkerInstanceTest, StartAndStop) { | 58 TEST_F(EmbeddedWorkerInstanceTest, StartAndStop) { |
59 scoped_ptr<EmbeddedWorkerInstance> worker = | 59 scoped_ptr<EmbeddedWorkerInstance> worker = |
60 embedded_worker_registry()->CreateWorker(); | 60 embedded_worker_registry()->CreateWorker(); |
61 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); | 61 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); |
62 | 62 |
63 const int embedded_worker_id = worker->embedded_worker_id(); | |
64 const int64 service_worker_version_id = 55L; | 63 const int64 service_worker_version_id = 55L; |
65 const GURL scope("http://example.com/*"); | 64 const GURL scope("http://example.com/*"); |
66 const GURL url("http://example.com/worker.js"); | 65 const GURL url("http://example.com/worker.js"); |
67 | 66 |
68 // Simulate adding one process to the worker. | 67 // Simulate adding one process to the scope. |
69 helper_->SimulateAddProcessToWorker(embedded_worker_id, kRenderProcessId); | 68 helper_->SimulateAddProcessToScope(scope, kRenderProcessId); |
70 | 69 |
71 // Start should succeed. | 70 // Start should succeed. |
72 ServiceWorkerStatusCode status; | 71 ServiceWorkerStatusCode status; |
73 base::RunLoop run_loop; | 72 base::RunLoop run_loop; |
74 worker->Start( | 73 worker->Start( |
75 service_worker_version_id, | 74 service_worker_version_id, |
76 scope, | 75 scope, |
77 url, | 76 url, |
78 false, | 77 false, |
79 std::vector<int>(), | |
80 base::Bind(&SaveStatusAndCall, &status, run_loop.QuitClosure())); | 78 base::Bind(&SaveStatusAndCall, &status, run_loop.QuitClosure())); |
81 run_loop.Run(); | 79 run_loop.Run(); |
82 EXPECT_EQ(SERVICE_WORKER_OK, status); | 80 EXPECT_EQ(SERVICE_WORKER_OK, status); |
83 EXPECT_EQ(EmbeddedWorkerInstance::STARTING, worker->status()); | 81 EXPECT_EQ(EmbeddedWorkerInstance::STARTING, worker->status()); |
84 base::RunLoop().RunUntilIdle(); | 82 base::RunLoop().RunUntilIdle(); |
85 | 83 |
86 // Worker started message should be notified (by EmbeddedWorkerTestHelper). | 84 // Worker started message should be notified (by EmbeddedWorkerTestHelper). |
87 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING, worker->status()); | 85 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING, worker->status()); |
88 EXPECT_EQ(kRenderProcessId, worker->process_id()); | 86 EXPECT_EQ(kRenderProcessId, worker->process_id()); |
89 | 87 |
(...skipping 19 matching lines...) Expand all Loading... |
109 | 107 |
110 const int64 service_worker_version_id = 55L; | 108 const int64 service_worker_version_id = 55L; |
111 const GURL scope("http://example.com/*"); | 109 const GURL scope("http://example.com/*"); |
112 const GURL url("http://example.com/worker.js"); | 110 const GURL url("http://example.com/worker.js"); |
113 | 111 |
114 ServiceWorkerStatusCode status; | 112 ServiceWorkerStatusCode status; |
115 base::RunLoop run_loop; | 113 base::RunLoop run_loop; |
116 // Begin starting the worker. | 114 // Begin starting the worker. |
117 std::vector<int> available_process; | 115 std::vector<int> available_process; |
118 available_process.push_back(kRenderProcessId); | 116 available_process.push_back(kRenderProcessId); |
| 117 context()->process_manager()->AddScopePendingProcesses( |
| 118 scope, available_process); |
119 worker->Start( | 119 worker->Start( |
120 service_worker_version_id, | 120 service_worker_version_id, |
121 scope, | 121 scope, |
122 url, | 122 url, |
123 false, | 123 false, |
124 available_process, | |
125 base::Bind(&SaveStatusAndCall, &status, run_loop.QuitClosure())); | 124 base::Bind(&SaveStatusAndCall, &status, run_loop.QuitClosure())); |
126 // But destroy it before it gets a chance to complete. | 125 // But destroy it before it gets a chance to complete. |
127 worker.reset(); | 126 worker.reset(); |
128 run_loop.Run(); | 127 run_loop.Run(); |
129 EXPECT_EQ(SERVICE_WORKER_ERROR_ABORT, status); | 128 EXPECT_EQ(SERVICE_WORKER_ERROR_ABORT, status); |
130 | 129 |
131 // Verify that we didn't send the message to start the worker. | 130 // Verify that we didn't send the message to start the worker. |
132 ASSERT_FALSE( | 131 ASSERT_FALSE( |
133 ipc_sink()->GetUniqueMessageMatching(EmbeddedWorkerMsg_StartWorker::ID)); | 132 ipc_sink()->GetUniqueMessageMatching(EmbeddedWorkerMsg_StartWorker::ID)); |
134 } | 133 } |
135 | 134 |
136 TEST_F(EmbeddedWorkerInstanceTest, SortProcesses) { | |
137 scoped_ptr<EmbeddedWorkerInstance> worker = | |
138 embedded_worker_registry()->CreateWorker(); | |
139 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); | |
140 | |
141 // Simulate adding processes to the worker. | |
142 // Process 1 has 1 ref, 2 has 2 refs and 3 has 3 refs. | |
143 const int embedded_worker_id = worker->embedded_worker_id(); | |
144 helper_->SimulateAddProcessToWorker(embedded_worker_id, 1); | |
145 helper_->SimulateAddProcessToWorker(embedded_worker_id, 2); | |
146 helper_->SimulateAddProcessToWorker(embedded_worker_id, 2); | |
147 helper_->SimulateAddProcessToWorker(embedded_worker_id, 3); | |
148 helper_->SimulateAddProcessToWorker(embedded_worker_id, 3); | |
149 helper_->SimulateAddProcessToWorker(embedded_worker_id, 3); | |
150 | |
151 // Process 3 has the biggest # of references and it should be chosen. | |
152 EXPECT_THAT(worker->SortProcesses(std::vector<int>()), | |
153 testing::ElementsAre(3, 2, 1)); | |
154 EXPECT_EQ(-1, worker->process_id()); | |
155 | |
156 // Argument processes are added to the existing set, but only for a single | |
157 // call. | |
158 std::vector<int> registering_processes; | |
159 registering_processes.push_back(1); | |
160 registering_processes.push_back(1); | |
161 registering_processes.push_back(1); | |
162 registering_processes.push_back(4); | |
163 EXPECT_THAT(worker->SortProcesses(registering_processes), | |
164 testing::ElementsAre(1, 3, 2, 4)); | |
165 | |
166 EXPECT_THAT(worker->SortProcesses(std::vector<int>()), | |
167 testing::ElementsAre(3, 2, 1)); | |
168 } | |
169 | |
170 } // namespace content | 135 } // namespace content |
OLD | NEW |