Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(218)

Side by Side Diff: content/browser/service_worker/embedded_worker_instance_unittest.cc

Issue 761923004: Keep alive ServiceWorkers when devtools is attached (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: ASSERT_TRUE -> DCHECK :( Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
11 #include "content/browser/service_worker/service_worker_context_core.h" 11 #include "content/browser/service_worker/service_worker_context_core.h"
12 #include "content/browser/service_worker/service_worker_context_wrapper.h" 12 #include "content/browser/service_worker/service_worker_context_wrapper.h"
13 #include "content/common/service_worker/embedded_worker_messages.h" 13 #include "content/common/service_worker/embedded_worker_messages.h"
14 #include "content/public/test/test_browser_thread_bundle.h" 14 #include "content/public/test/test_browser_thread_bundle.h"
15 #include "testing/gmock/include/gmock/gmock.h" 15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
17 17
18 namespace content { 18 namespace content {
19 19
20 static const int kRenderProcessId = 11; 20 namespace {
21
22 const int kRenderProcessId = 11;
23
24 void SaveStatusAndCall(ServiceWorkerStatusCode* out,
25 const base::Closure& callback,
26 ServiceWorkerStatusCode status) {
27 *out = status;
28 callback.Run();
29 }
30
31 } // namespace
21 32
22 class EmbeddedWorkerInstanceTest : public testing::Test { 33 class EmbeddedWorkerInstanceTest : public testing::Test {
23 protected: 34 protected:
24 EmbeddedWorkerInstanceTest() 35 EmbeddedWorkerInstanceTest()
25 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} 36 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {}
26 37
27 void SetUp() override { 38 void SetUp() override {
28 helper_.reset(new EmbeddedWorkerTestHelper(kRenderProcessId)); 39 helper_.reset(new EmbeddedWorkerTestHelper(kRenderProcessId));
29 } 40 }
30 41
31 void TearDown() override { helper_.reset(); } 42 void TearDown() override { helper_.reset(); }
32 43
44 ServiceWorkerStatusCode StartWorker(EmbeddedWorkerInstance* worker,
45 int id, const GURL& pattern,
46 const GURL& url) {
47 ServiceWorkerStatusCode status;
48 base::RunLoop run_loop;
49 worker->Start(id, pattern, url, false,
50 base::Bind(&SaveStatusAndCall, &status,
51 run_loop.QuitClosure()));
52 run_loop.Run();
53 return status;
54 }
55
33 ServiceWorkerContextCore* context() { return helper_->context(); } 56 ServiceWorkerContextCore* context() { return helper_->context(); }
34 57
35 EmbeddedWorkerRegistry* embedded_worker_registry() { 58 EmbeddedWorkerRegistry* embedded_worker_registry() {
36 DCHECK(context()); 59 DCHECK(context());
37 return context()->embedded_worker_registry(); 60 return context()->embedded_worker_registry();
38 } 61 }
39 62
40 IPC::TestSink* ipc_sink() { return helper_->ipc_sink(); } 63 IPC::TestSink* ipc_sink() { return helper_->ipc_sink(); }
41 64
42 TestBrowserThreadBundle thread_bundle_; 65 TestBrowserThreadBundle thread_bundle_;
43 scoped_ptr<EmbeddedWorkerTestHelper> helper_; 66 scoped_ptr<EmbeddedWorkerTestHelper> helper_;
44 67
45 private: 68 private:
46 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstanceTest); 69 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstanceTest);
47 }; 70 };
48 71
49 static void SaveStatusAndCall(ServiceWorkerStatusCode* out,
50 const base::Closure& callback,
51 ServiceWorkerStatusCode status) {
52 *out = status;
53 callback.Run();
54 }
55
56 TEST_F(EmbeddedWorkerInstanceTest, StartAndStop) { 72 TEST_F(EmbeddedWorkerInstanceTest, StartAndStop) {
57 scoped_ptr<EmbeddedWorkerInstance> worker = 73 scoped_ptr<EmbeddedWorkerInstance> worker =
58 embedded_worker_registry()->CreateWorker(); 74 embedded_worker_registry()->CreateWorker();
59 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); 75 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status());
60 76
61 const int64 service_worker_version_id = 55L; 77 const int64 service_worker_version_id = 55L;
62 const GURL pattern("http://example.com/"); 78 const GURL pattern("http://example.com/");
63 const GURL url("http://example.com/worker.js"); 79 const GURL url("http://example.com/worker.js");
64 80
65 // Simulate adding one process to the pattern. 81 // Simulate adding one process to the pattern.
(...skipping 26 matching lines...) Expand all
92 // EmbeddedWorkerTestHelper. 108 // EmbeddedWorkerTestHelper.
93 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); 109 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status());
94 110
95 // Verify that we've sent two messages to start and terminate the worker. 111 // Verify that we've sent two messages to start and terminate the worker.
96 ASSERT_TRUE(ipc_sink()->GetUniqueMessageMatching( 112 ASSERT_TRUE(ipc_sink()->GetUniqueMessageMatching(
97 EmbeddedWorkerMsg_StartWorker::ID)); 113 EmbeddedWorkerMsg_StartWorker::ID));
98 ASSERT_TRUE(ipc_sink()->GetUniqueMessageMatching( 114 ASSERT_TRUE(ipc_sink()->GetUniqueMessageMatching(
99 EmbeddedWorkerMsg_StopWorker::ID)); 115 EmbeddedWorkerMsg_StopWorker::ID));
100 } 116 }
101 117
118 TEST_F(EmbeddedWorkerInstanceTest, StopWhenDevToolsAttached) {
119 scoped_ptr<EmbeddedWorkerInstance> worker =
120 embedded_worker_registry()->CreateWorker();
121 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status());
122
123 const int64 service_worker_version_id = 55L;
124 const GURL pattern("http://example.com/");
125 const GURL url("http://example.com/worker.js");
126
127 // Simulate adding one process to the pattern.
128 helper_->SimulateAddProcessToPattern(pattern, kRenderProcessId);
129
130 // Start the worker and then call StopIfIdle().
131 EXPECT_EQ(SERVICE_WORKER_OK,
132 StartWorker(worker.get(), service_worker_version_id, pattern, url));
133 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING, worker->status());
134 EXPECT_EQ(kRenderProcessId, worker->process_id());
135 worker->StopIfIdle();
136 EXPECT_EQ(EmbeddedWorkerInstance::STOPPING, worker->status());
137 base::RunLoop().RunUntilIdle();
138
139 // The worker must be stopped now.
140 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status());
141
142 // Set devtools_attached to true, and do the same.
143 worker->set_devtools_attached(true);
144
145 EXPECT_EQ(SERVICE_WORKER_OK,
146 StartWorker(worker.get(), service_worker_version_id, pattern, url));
147 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING, worker->status());
148 EXPECT_EQ(kRenderProcessId, worker->process_id());
149 worker->StopIfIdle();
150 base::RunLoop().RunUntilIdle();
151
152 // The worker must not be stopped this time.
153 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING, worker->status());
154
155 // Calling Stop() actually stops the worker regardless of whether devtools
156 // is attached or not.
157 EXPECT_EQ(SERVICE_WORKER_OK, worker->Stop());
158 base::RunLoop().RunUntilIdle();
159 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status());
160 }
161
102 TEST_F(EmbeddedWorkerInstanceTest, InstanceDestroyedBeforeStartFinishes) { 162 TEST_F(EmbeddedWorkerInstanceTest, InstanceDestroyedBeforeStartFinishes) {
103 scoped_ptr<EmbeddedWorkerInstance> worker = 163 scoped_ptr<EmbeddedWorkerInstance> worker =
104 embedded_worker_registry()->CreateWorker(); 164 embedded_worker_registry()->CreateWorker();
105 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); 165 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status());
106 166
107 const int64 service_worker_version_id = 55L; 167 const int64 service_worker_version_id = 55L;
108 const GURL pattern("http://example.com/"); 168 const GURL pattern("http://example.com/");
109 const GURL url("http://example.com/worker.js"); 169 const GURL url("http://example.com/worker.js");
110 170
111 ServiceWorkerStatusCode status; 171 ServiceWorkerStatusCode status;
(...skipping 11 matching lines...) Expand all
123 worker.reset(); 183 worker.reset();
124 run_loop.Run(); 184 run_loop.Run();
125 EXPECT_EQ(SERVICE_WORKER_ERROR_ABORT, status); 185 EXPECT_EQ(SERVICE_WORKER_ERROR_ABORT, status);
126 186
127 // Verify that we didn't send the message to start the worker. 187 // Verify that we didn't send the message to start the worker.
128 ASSERT_FALSE( 188 ASSERT_FALSE(
129 ipc_sink()->GetUniqueMessageMatching(EmbeddedWorkerMsg_StartWorker::ID)); 189 ipc_sink()->GetUniqueMessageMatching(EmbeddedWorkerMsg_StartWorker::ID));
130 } 190 }
131 191
132 } // namespace content 192 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/service_worker/embedded_worker_instance.cc ('k') | content/browser/service_worker/service_worker_version.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698