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 "content/browser/service_worker/service_worker_dispatcher_host.h" | 5 #include "content/browser/service_worker/service_worker_dispatcher_host.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "content/browser/browser_thread_impl.h" | 10 #include "content/browser/browser_thread_impl.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 namespace content { | 22 namespace content { |
23 | 23 |
24 static const int kRenderProcessId = 1; | 24 static const int kRenderProcessId = 1; |
25 | 25 |
26 class ServiceWorkerDispatcherHostTest : public testing::Test { | 26 class ServiceWorkerDispatcherHostTest : public testing::Test { |
27 protected: | 27 protected: |
28 ServiceWorkerDispatcherHostTest() | 28 ServiceWorkerDispatcherHostTest() |
29 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} | 29 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} |
30 | 30 |
31 virtual void SetUp() { | 31 virtual void SetUp() { |
32 context_wrapper_ = new ServiceWorkerContextWrapper; | 32 helper_.reset(new EmbeddedWorkerTestHelper(kRenderProcessId)); |
33 context_wrapper_->Init(base::FilePath(), NULL); | |
34 helper_.reset(new EmbeddedWorkerTestHelper(context(), kRenderProcessId)); | |
35 } | 33 } |
36 | 34 |
37 virtual void TearDown() { | 35 virtual void TearDown() { |
38 helper_.reset(); | 36 helper_.reset(); |
39 if (context_wrapper_) { | |
40 context_wrapper_->Shutdown(); | |
41 context_wrapper_ = NULL; | |
42 } | |
43 } | 37 } |
44 | 38 |
45 ServiceWorkerContextCore* context() { return context_wrapper_->context(); } | 39 ServiceWorkerContextCore* context() { return helper_->context(); } |
| 40 ServiceWorkerContextWrapper* context_wrapper() { |
| 41 return helper_->context_wrapper(); |
| 42 } |
46 | 43 |
47 TestBrowserThreadBundle browser_thread_bundle_; | 44 TestBrowserThreadBundle browser_thread_bundle_; |
48 scoped_refptr<ServiceWorkerContextWrapper> context_wrapper_; | |
49 scoped_ptr<EmbeddedWorkerTestHelper> helper_; | 45 scoped_ptr<EmbeddedWorkerTestHelper> helper_; |
50 }; | 46 }; |
51 | 47 |
52 | 48 |
53 class TestingServiceWorkerDispatcherHost : public ServiceWorkerDispatcherHost { | 49 class TestingServiceWorkerDispatcherHost : public ServiceWorkerDispatcherHost { |
54 public: | 50 public: |
55 TestingServiceWorkerDispatcherHost( | 51 TestingServiceWorkerDispatcherHost( |
56 int process_id, | 52 int process_id, |
57 ServiceWorkerContextWrapper* context_wrapper, | 53 ServiceWorkerContextWrapper* context_wrapper, |
58 EmbeddedWorkerTestHelper* helper) | 54 EmbeddedWorkerTestHelper* helper) |
(...skipping 19 matching lines...) Expand all Loading... |
78 EmbeddedWorkerTestHelper* helper_; | 74 EmbeddedWorkerTestHelper* helper_; |
79 virtual ~TestingServiceWorkerDispatcherHost() {} | 75 virtual ~TestingServiceWorkerDispatcherHost() {} |
80 }; | 76 }; |
81 | 77 |
82 TEST_F(ServiceWorkerDispatcherHostTest, DisabledCausesError) { | 78 TEST_F(ServiceWorkerDispatcherHostTest, DisabledCausesError) { |
83 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( | 79 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( |
84 switches::kEnableServiceWorker)); | 80 switches::kEnableServiceWorker)); |
85 | 81 |
86 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = | 82 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = |
87 new TestingServiceWorkerDispatcherHost( | 83 new TestingServiceWorkerDispatcherHost( |
88 kRenderProcessId, context_wrapper_.get(), helper_.get()); | 84 kRenderProcessId, context_wrapper(), helper_.get()); |
89 | 85 |
90 bool handled; | 86 bool handled; |
91 dispatcher_host->OnMessageReceived( | 87 dispatcher_host->OnMessageReceived( |
92 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, -1, GURL(), GURL()), | 88 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, -1, GURL(), GURL()), |
93 &handled); | 89 &handled); |
94 EXPECT_TRUE(handled); | 90 EXPECT_TRUE(handled); |
95 | 91 |
96 // TODO(alecflett): Pump the message loop when this becomes async. | 92 // TODO(alecflett): Pump the message loop when this becomes async. |
97 ASSERT_EQ(1UL, dispatcher_host->ipc_sink()->message_count()); | 93 ASSERT_EQ(1UL, dispatcher_host->ipc_sink()->message_count()); |
98 EXPECT_TRUE(dispatcher_host->ipc_sink()->GetUniqueMessageMatching( | 94 EXPECT_TRUE(dispatcher_host->ipc_sink()->GetUniqueMessageMatching( |
99 ServiceWorkerMsg_ServiceWorkerRegistrationError::ID)); | 95 ServiceWorkerMsg_ServiceWorkerRegistrationError::ID)); |
100 } | 96 } |
101 | 97 |
102 // Disable this since now we cache command-line switch in | 98 // Disable this since now we cache command-line switch in |
103 // ServiceWorkerUtils::IsFeatureEnabled() and this could be flaky depending | 99 // ServiceWorkerUtils::IsFeatureEnabled() and this could be flaky depending |
104 // on testing order. (crbug.com/352581) | 100 // on testing order. (crbug.com/352581) |
105 // TODO(kinuko): Just remove DisabledCausesError test above and enable | 101 // TODO(kinuko): Just remove DisabledCausesError test above and enable |
106 // this test when we remove the --enable-service-worker flag. | 102 // this test when we remove the --enable-service-worker flag. |
107 TEST_F(ServiceWorkerDispatcherHostTest, DISABLED_Enabled) { | 103 TEST_F(ServiceWorkerDispatcherHostTest, DISABLED_Enabled) { |
108 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( | 104 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( |
109 switches::kEnableServiceWorker)); | 105 switches::kEnableServiceWorker)); |
110 CommandLine::ForCurrentProcess()->AppendSwitch( | 106 CommandLine::ForCurrentProcess()->AppendSwitch( |
111 switches::kEnableServiceWorker); | 107 switches::kEnableServiceWorker); |
112 | 108 |
113 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = | 109 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = |
114 new TestingServiceWorkerDispatcherHost( | 110 new TestingServiceWorkerDispatcherHost( |
115 kRenderProcessId, context_wrapper_.get(), helper_.get()); | 111 kRenderProcessId, context_wrapper(), helper_.get()); |
116 | 112 |
117 bool handled; | 113 bool handled; |
118 dispatcher_host->OnMessageReceived( | 114 dispatcher_host->OnMessageReceived( |
119 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, -1, GURL(), GURL()), | 115 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, -1, GURL(), GURL()), |
120 &handled); | 116 &handled); |
121 EXPECT_TRUE(handled); | 117 EXPECT_TRUE(handled); |
122 base::RunLoop().RunUntilIdle(); | 118 base::RunLoop().RunUntilIdle(); |
123 | 119 |
124 // TODO(alecflett): Pump the message loop when this becomes async. | 120 // TODO(alecflett): Pump the message loop when this becomes async. |
125 ASSERT_EQ(2UL, dispatcher_host->ipc_sink()->message_count()); | 121 ASSERT_EQ(2UL, dispatcher_host->ipc_sink()->message_count()); |
126 EXPECT_TRUE(dispatcher_host->ipc_sink()->GetUniqueMessageMatching( | 122 EXPECT_TRUE(dispatcher_host->ipc_sink()->GetUniqueMessageMatching( |
127 EmbeddedWorkerMsg_StartWorker::ID)); | 123 EmbeddedWorkerMsg_StartWorker::ID)); |
128 EXPECT_TRUE(dispatcher_host->ipc_sink()->GetUniqueMessageMatching( | 124 EXPECT_TRUE(dispatcher_host->ipc_sink()->GetUniqueMessageMatching( |
129 ServiceWorkerMsg_ServiceWorkerRegistered::ID)); | 125 ServiceWorkerMsg_ServiceWorkerRegistered::ID)); |
130 } | 126 } |
131 | 127 |
132 TEST_F(ServiceWorkerDispatcherHostTest, EarlyContextDeletion) { | 128 TEST_F(ServiceWorkerDispatcherHostTest, EarlyContextDeletion) { |
133 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( | 129 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( |
134 switches::kEnableServiceWorker)); | 130 switches::kEnableServiceWorker)); |
135 CommandLine::ForCurrentProcess()->AppendSwitch( | 131 CommandLine::ForCurrentProcess()->AppendSwitch( |
136 switches::kEnableServiceWorker); | 132 switches::kEnableServiceWorker); |
137 | 133 |
138 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = | 134 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = |
139 new TestingServiceWorkerDispatcherHost( | 135 new TestingServiceWorkerDispatcherHost( |
140 kRenderProcessId, context_wrapper_.get(), helper_.get()); | 136 kRenderProcessId, context_wrapper(), helper_.get()); |
141 | 137 |
142 context_wrapper_->Shutdown(); | 138 helper_->ShutdownContext(); |
143 context_wrapper_ = NULL; | |
144 | 139 |
145 bool handled; | 140 bool handled; |
146 dispatcher_host->OnMessageReceived( | 141 dispatcher_host->OnMessageReceived( |
147 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, -1, GURL(), GURL()), | 142 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, -1, GURL(), GURL()), |
148 &handled); | 143 &handled); |
149 EXPECT_TRUE(handled); | 144 EXPECT_TRUE(handled); |
150 | 145 |
151 // TODO(alecflett): Pump the message loop when this becomes async. | 146 // TODO(alecflett): Pump the message loop when this becomes async. |
152 ASSERT_EQ(1UL, dispatcher_host->ipc_sink()->message_count()); | 147 ASSERT_EQ(1UL, dispatcher_host->ipc_sink()->message_count()); |
153 EXPECT_TRUE(dispatcher_host->ipc_sink()->GetUniqueMessageMatching( | 148 EXPECT_TRUE(dispatcher_host->ipc_sink()->GetUniqueMessageMatching( |
154 ServiceWorkerMsg_ServiceWorkerRegistrationError::ID)); | 149 ServiceWorkerMsg_ServiceWorkerRegistrationError::ID)); |
155 } | 150 } |
156 | 151 |
157 TEST_F(ServiceWorkerDispatcherHostTest, ProviderCreatedAndDestroyed) { | 152 TEST_F(ServiceWorkerDispatcherHostTest, ProviderCreatedAndDestroyed) { |
158 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = | 153 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = |
159 new TestingServiceWorkerDispatcherHost( | 154 new TestingServiceWorkerDispatcherHost( |
160 kRenderProcessId, context_wrapper_.get(), helper_.get()); | 155 kRenderProcessId, context_wrapper(), helper_.get()); |
161 | 156 |
162 const int kProviderId = 1001; // Test with a value != kRenderProcessId. | 157 const int kProviderId = 1001; // Test with a value != kRenderProcessId. |
163 | 158 |
164 bool handled = false; | 159 bool handled = false; |
165 dispatcher_host->OnMessageReceived( | 160 dispatcher_host->OnMessageReceived( |
166 ServiceWorkerHostMsg_ProviderCreated(kProviderId), | 161 ServiceWorkerHostMsg_ProviderCreated(kProviderId), |
167 &handled); | 162 &handled); |
168 EXPECT_TRUE(handled); | 163 EXPECT_TRUE(handled); |
169 EXPECT_TRUE(context()->GetProviderHost(kRenderProcessId, kProviderId)); | 164 EXPECT_TRUE(context()->GetProviderHost(kRenderProcessId, kProviderId)); |
170 | 165 |
(...skipping 26 matching lines...) Expand all Loading... |
197 ServiceWorkerHostMsg_ProviderCreated(kProviderId), | 192 ServiceWorkerHostMsg_ProviderCreated(kProviderId), |
198 &handled); | 193 &handled); |
199 EXPECT_TRUE(handled); | 194 EXPECT_TRUE(handled); |
200 EXPECT_TRUE(context()->GetProviderHost(kRenderProcessId, kProviderId)); | 195 EXPECT_TRUE(context()->GetProviderHost(kRenderProcessId, kProviderId)); |
201 EXPECT_TRUE(dispatcher_host->HasOneRef()); | 196 EXPECT_TRUE(dispatcher_host->HasOneRef()); |
202 dispatcher_host = NULL; | 197 dispatcher_host = NULL; |
203 EXPECT_FALSE(context()->GetProviderHost(kRenderProcessId, kProviderId)); | 198 EXPECT_FALSE(context()->GetProviderHost(kRenderProcessId, kProviderId)); |
204 } | 199 } |
205 | 200 |
206 } // namespace content | 201 } // namespace content |
OLD | NEW |