| 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 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 }; | 76 }; |
| 77 | 77 |
| 78 TEST_F(ServiceWorkerDispatcherHostTest, DisabledCausesError) { | 78 TEST_F(ServiceWorkerDispatcherHostTest, DisabledCausesError) { |
| 79 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( | 79 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( |
| 80 switches::kEnableServiceWorker)); | 80 switches::kEnableServiceWorker)); |
| 81 | 81 |
| 82 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = | 82 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = |
| 83 new TestingServiceWorkerDispatcherHost( | 83 new TestingServiceWorkerDispatcherHost( |
| 84 kRenderProcessId, context_wrapper(), helper_.get()); | 84 kRenderProcessId, context_wrapper(), helper_.get()); |
| 85 | 85 |
| 86 bool handled; | |
| 87 dispatcher_host->OnMessageReceived( | 86 dispatcher_host->OnMessageReceived( |
| 88 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, -1, GURL(), GURL()), | 87 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, -1, GURL(), GURL())); |
| 89 &handled); | |
| 90 EXPECT_TRUE(handled); | |
| 91 | 88 |
| 92 // TODO(alecflett): Pump the message loop when this becomes async. | 89 // TODO(alecflett): Pump the message loop when this becomes async. |
| 93 ASSERT_EQ(1UL, dispatcher_host->ipc_sink()->message_count()); | 90 ASSERT_EQ(1UL, dispatcher_host->ipc_sink()->message_count()); |
| 94 EXPECT_TRUE(dispatcher_host->ipc_sink()->GetUniqueMessageMatching( | 91 EXPECT_TRUE(dispatcher_host->ipc_sink()->GetUniqueMessageMatching( |
| 95 ServiceWorkerMsg_ServiceWorkerRegistrationError::ID)); | 92 ServiceWorkerMsg_ServiceWorkerRegistrationError::ID)); |
| 96 } | 93 } |
| 97 | 94 |
| 98 // Disable this since now we cache command-line switch in | 95 // Disable this since now we cache command-line switch in |
| 99 // ServiceWorkerUtils::IsFeatureEnabled() and this could be flaky depending | 96 // ServiceWorkerUtils::IsFeatureEnabled() and this could be flaky depending |
| 100 // on testing order. (crbug.com/352581) | 97 // on testing order. (crbug.com/352581) |
| 101 // TODO(kinuko): Just remove DisabledCausesError test above and enable | 98 // TODO(kinuko): Just remove DisabledCausesError test above and enable |
| 102 // this test when we remove the --enable-service-worker flag. | 99 // this test when we remove the --enable-service-worker flag. |
| 103 TEST_F(ServiceWorkerDispatcherHostTest, DISABLED_Enabled) { | 100 TEST_F(ServiceWorkerDispatcherHostTest, DISABLED_Enabled) { |
| 104 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( | 101 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( |
| 105 switches::kEnableServiceWorker)); | 102 switches::kEnableServiceWorker)); |
| 106 CommandLine::ForCurrentProcess()->AppendSwitch( | 103 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 107 switches::kEnableServiceWorker); | 104 switches::kEnableServiceWorker); |
| 108 | 105 |
| 109 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = | 106 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = |
| 110 new TestingServiceWorkerDispatcherHost( | 107 new TestingServiceWorkerDispatcherHost( |
| 111 kRenderProcessId, context_wrapper(), helper_.get()); | 108 kRenderProcessId, context_wrapper(), helper_.get()); |
| 112 | 109 |
| 113 bool handled; | |
| 114 dispatcher_host->OnMessageReceived( | 110 dispatcher_host->OnMessageReceived( |
| 115 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, -1, GURL(), GURL()), | 111 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, -1, GURL(), GURL())); |
| 116 &handled); | |
| 117 EXPECT_TRUE(handled); | |
| 118 base::RunLoop().RunUntilIdle(); | 112 base::RunLoop().RunUntilIdle(); |
| 119 | 113 |
| 120 // TODO(alecflett): Pump the message loop when this becomes async. | 114 // TODO(alecflett): Pump the message loop when this becomes async. |
| 121 ASSERT_EQ(2UL, dispatcher_host->ipc_sink()->message_count()); | 115 ASSERT_EQ(2UL, dispatcher_host->ipc_sink()->message_count()); |
| 122 EXPECT_TRUE(dispatcher_host->ipc_sink()->GetUniqueMessageMatching( | 116 EXPECT_TRUE(dispatcher_host->ipc_sink()->GetUniqueMessageMatching( |
| 123 EmbeddedWorkerMsg_StartWorker::ID)); | 117 EmbeddedWorkerMsg_StartWorker::ID)); |
| 124 EXPECT_TRUE(dispatcher_host->ipc_sink()->GetUniqueMessageMatching( | 118 EXPECT_TRUE(dispatcher_host->ipc_sink()->GetUniqueMessageMatching( |
| 125 ServiceWorkerMsg_ServiceWorkerRegistered::ID)); | 119 ServiceWorkerMsg_ServiceWorkerRegistered::ID)); |
| 126 } | 120 } |
| 127 | 121 |
| 128 TEST_F(ServiceWorkerDispatcherHostTest, EarlyContextDeletion) { | 122 TEST_F(ServiceWorkerDispatcherHostTest, EarlyContextDeletion) { |
| 129 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( | 123 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( |
| 130 switches::kEnableServiceWorker)); | 124 switches::kEnableServiceWorker)); |
| 131 CommandLine::ForCurrentProcess()->AppendSwitch( | 125 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 132 switches::kEnableServiceWorker); | 126 switches::kEnableServiceWorker); |
| 133 | 127 |
| 134 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = | 128 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = |
| 135 new TestingServiceWorkerDispatcherHost( | 129 new TestingServiceWorkerDispatcherHost( |
| 136 kRenderProcessId, context_wrapper(), helper_.get()); | 130 kRenderProcessId, context_wrapper(), helper_.get()); |
| 137 | 131 |
| 138 helper_->ShutdownContext(); | 132 helper_->ShutdownContext(); |
| 139 | 133 |
| 140 bool handled; | |
| 141 dispatcher_host->OnMessageReceived( | 134 dispatcher_host->OnMessageReceived( |
| 142 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, -1, GURL(), GURL()), | 135 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, -1, GURL(), GURL())); |
| 143 &handled); | |
| 144 EXPECT_TRUE(handled); | |
| 145 | 136 |
| 146 // TODO(alecflett): Pump the message loop when this becomes async. | 137 // TODO(alecflett): Pump the message loop when this becomes async. |
| 147 ASSERT_EQ(1UL, dispatcher_host->ipc_sink()->message_count()); | 138 ASSERT_EQ(1UL, dispatcher_host->ipc_sink()->message_count()); |
| 148 EXPECT_TRUE(dispatcher_host->ipc_sink()->GetUniqueMessageMatching( | 139 EXPECT_TRUE(dispatcher_host->ipc_sink()->GetUniqueMessageMatching( |
| 149 ServiceWorkerMsg_ServiceWorkerRegistrationError::ID)); | 140 ServiceWorkerMsg_ServiceWorkerRegistrationError::ID)); |
| 150 } | 141 } |
| 151 | 142 |
| 152 TEST_F(ServiceWorkerDispatcherHostTest, ProviderCreatedAndDestroyed) { | 143 TEST_F(ServiceWorkerDispatcherHostTest, ProviderCreatedAndDestroyed) { |
| 153 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = | 144 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = |
| 154 new TestingServiceWorkerDispatcherHost( | 145 new TestingServiceWorkerDispatcherHost( |
| 155 kRenderProcessId, context_wrapper(), helper_.get()); | 146 kRenderProcessId, context_wrapper(), helper_.get()); |
| 156 | 147 |
| 157 const int kProviderId = 1001; // Test with a value != kRenderProcessId. | 148 const int kProviderId = 1001; // Test with a value != kRenderProcessId. |
| 158 | 149 |
| 159 bool handled = false; | |
| 160 dispatcher_host->OnMessageReceived( | 150 dispatcher_host->OnMessageReceived( |
| 161 ServiceWorkerHostMsg_ProviderCreated(kProviderId), | 151 ServiceWorkerHostMsg_ProviderCreated(kProviderId)); |
| 162 &handled); | |
| 163 EXPECT_TRUE(handled); | |
| 164 EXPECT_TRUE(context()->GetProviderHost(kRenderProcessId, kProviderId)); | 152 EXPECT_TRUE(context()->GetProviderHost(kRenderProcessId, kProviderId)); |
| 165 | 153 |
| 166 // Two with the same ID should be seen as a bad message. | 154 // Two with the same ID should be seen as a bad message. |
| 167 handled = false; | |
| 168 dispatcher_host->OnMessageReceived( | 155 dispatcher_host->OnMessageReceived( |
| 169 ServiceWorkerHostMsg_ProviderCreated(kProviderId), | 156 ServiceWorkerHostMsg_ProviderCreated(kProviderId)); |
| 170 &handled); | |
| 171 EXPECT_TRUE(handled); | |
| 172 EXPECT_EQ(1, dispatcher_host->bad_messages_received_count_); | 157 EXPECT_EQ(1, dispatcher_host->bad_messages_received_count_); |
| 173 | 158 |
| 174 handled = false; | |
| 175 dispatcher_host->OnMessageReceived( | 159 dispatcher_host->OnMessageReceived( |
| 176 ServiceWorkerHostMsg_ProviderDestroyed(kProviderId), | 160 ServiceWorkerHostMsg_ProviderDestroyed(kProviderId)); |
| 177 &handled); | |
| 178 EXPECT_TRUE(handled); | |
| 179 EXPECT_FALSE(context()->GetProviderHost(kRenderProcessId, kProviderId)); | 161 EXPECT_FALSE(context()->GetProviderHost(kRenderProcessId, kProviderId)); |
| 180 | 162 |
| 181 // Destroying an ID that does not exist warrants a bad message. | 163 // Destroying an ID that does not exist warrants a bad message. |
| 182 handled = false; | |
| 183 dispatcher_host->OnMessageReceived( | 164 dispatcher_host->OnMessageReceived( |
| 184 ServiceWorkerHostMsg_ProviderDestroyed(kProviderId), | 165 ServiceWorkerHostMsg_ProviderDestroyed(kProviderId)); |
| 185 &handled); | |
| 186 EXPECT_TRUE(handled); | |
| 187 EXPECT_EQ(2, dispatcher_host->bad_messages_received_count_); | 166 EXPECT_EQ(2, dispatcher_host->bad_messages_received_count_); |
| 188 | 167 |
| 189 // Deletion of the dispatcher_host should cause providers for that | 168 // Deletion of the dispatcher_host should cause providers for that |
| 190 // process to get deleted as well. | 169 // process to get deleted as well. |
| 191 dispatcher_host->OnMessageReceived( | 170 dispatcher_host->OnMessageReceived( |
| 192 ServiceWorkerHostMsg_ProviderCreated(kProviderId), | 171 ServiceWorkerHostMsg_ProviderCreated(kProviderId)); |
| 193 &handled); | |
| 194 EXPECT_TRUE(handled); | |
| 195 EXPECT_TRUE(context()->GetProviderHost(kRenderProcessId, kProviderId)); | 172 EXPECT_TRUE(context()->GetProviderHost(kRenderProcessId, kProviderId)); |
| 196 EXPECT_TRUE(dispatcher_host->HasOneRef()); | 173 EXPECT_TRUE(dispatcher_host->HasOneRef()); |
| 197 dispatcher_host = NULL; | 174 dispatcher_host = NULL; |
| 198 EXPECT_FALSE(context()->GetProviderHost(kRenderProcessId, kProviderId)); | 175 EXPECT_FALSE(context()->GetProviderHost(kRenderProcessId, kProviderId)); |
| 199 } | 176 } |
| 200 | 177 |
| 201 } // namespace content | 178 } // namespace content |
| OLD | NEW |