OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/message_loop/message_loop.h" | |
10 #include "content/browser/browser_thread_impl.h" | |
9 #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" | |
10 #include "content/common/service_worker_messages.h" | 13 #include "content/common/service_worker_messages.h" |
11 #include "content/public/common/content_switches.h" | 14 #include "content/public/common/content_switches.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
13 | 16 |
14 using base::MessageLoop; | 17 using base::MessageLoop; |
15 | 18 |
16 namespace content { | 19 namespace content { |
17 | 20 |
18 class ServiceWorkerDispatcherHostTest : public testing::Test { | 21 class ServiceWorkerDispatcherHostTest : public testing::Test { |
19 protected: | 22 protected: |
23 ServiceWorkerDispatcherHostTest() | |
24 : io_thread_(BrowserThread::IO, &message_loop_) {} | |
25 | |
20 virtual void SetUp() { | 26 virtual void SetUp() { |
21 context_.reset(new ServiceWorkerContextCore(base::FilePath(), NULL)); | 27 context_wrapper_ = new ServiceWorkerContextWrapper; |
28 context_wrapper_->Init(base::FilePath(), NULL); | |
22 } | 29 } |
23 | 30 |
24 virtual void TearDown() { | 31 virtual void TearDown() { |
25 context_.reset(); | 32 if (context_wrapper_) { |
33 context_wrapper_->Shutdown(); | |
34 context_wrapper_ = NULL; | |
35 } | |
26 } | 36 } |
27 | 37 |
28 scoped_ptr<ServiceWorkerContextCore> context_; | 38 ServiceWorkerContextCore* context() { return context_wrapper_->context(); } |
39 | |
40 scoped_refptr<ServiceWorkerContextWrapper> context_wrapper_; | |
41 base::MessageLoopForIO message_loop_; | |
42 BrowserThreadImpl io_thread_; | |
29 }; | 43 }; |
30 | 44 |
31 static const int kRenderProcessId = 1; | 45 static const int kRenderProcessId = 1; |
32 | 46 |
33 class TestingServiceWorkerDispatcherHost : public ServiceWorkerDispatcherHost { | 47 class TestingServiceWorkerDispatcherHost : public ServiceWorkerDispatcherHost { |
34 public: | 48 public: |
35 TestingServiceWorkerDispatcherHost(int process_id, | 49 TestingServiceWorkerDispatcherHost( |
36 ServiceWorkerContextCore* context) | 50 int process_id, |
37 : ServiceWorkerDispatcherHost(process_id) { | 51 ServiceWorkerContextWrapper* context_wrapper) |
38 context_ = context->AsWeakPtr(); | 52 : ServiceWorkerDispatcherHost(process_id), |
53 bad_messages_received_count_(0) { | |
54 Init(context_wrapper); | |
39 } | 55 } |
40 | 56 |
41 virtual bool Send(IPC::Message* message) OVERRIDE { | 57 virtual bool Send(IPC::Message* message) OVERRIDE { |
42 sent_messages_.push_back(message); | 58 sent_messages_.push_back(message); |
43 return true; | 59 return true; |
44 } | 60 } |
45 | 61 |
62 virtual void BadMessageReceived() OVERRIDE { | |
63 ++bad_messages_received_count_; | |
64 } | |
65 | |
46 ScopedVector<IPC::Message> sent_messages_; | 66 ScopedVector<IPC::Message> sent_messages_; |
67 int bad_messages_received_count_; | |
47 | 68 |
48 protected: | 69 protected: |
49 virtual ~TestingServiceWorkerDispatcherHost() {} | 70 virtual ~TestingServiceWorkerDispatcherHost() {} |
50 }; | 71 }; |
51 | 72 |
52 TEST_F(ServiceWorkerDispatcherHostTest, DisabledCausesError) { | 73 TEST_F(ServiceWorkerDispatcherHostTest, DisabledCausesError) { |
53 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( | 74 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( |
54 switches::kEnableServiceWorker)); | 75 switches::kEnableServiceWorker)); |
55 | 76 |
56 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = | 77 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = |
57 new TestingServiceWorkerDispatcherHost(kRenderProcessId, context_.get()); | 78 new TestingServiceWorkerDispatcherHost(kRenderProcessId, |
79 context_wrapper_.get()); | |
58 | 80 |
59 bool handled; | 81 bool handled; |
60 dispatcher_host->OnMessageReceived( | 82 dispatcher_host->OnMessageReceived( |
61 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, GURL(), GURL()), | 83 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, GURL(), GURL()), |
62 &handled); | 84 &handled); |
63 DCHECK(handled); | 85 EXPECT_TRUE(handled); |
64 | 86 |
65 // TODO(alecflett): Pump the message loop when this becomes async. | 87 // TODO(alecflett): Pump the message loop when this becomes async. |
66 DCHECK_EQ(1UL, dispatcher_host->sent_messages_.size()); | 88 EXPECT_EQ(1UL, dispatcher_host->sent_messages_.size()); |
kinuko
2013/11/18 05:57:24
nit: ASSERT_EQ may be better to avoid crash in lin
michaeln
2013/11/18 20:18:17
done
i'm really not sure what the best practice i
| |
67 DCHECK_EQ( | 89 EXPECT_EQ( |
68 static_cast<uint32>(ServiceWorkerMsg_ServiceWorkerRegistrationError::ID), | 90 static_cast<uint32>(ServiceWorkerMsg_ServiceWorkerRegistrationError::ID), |
69 dispatcher_host->sent_messages_[0]->type()); | 91 dispatcher_host->sent_messages_[0]->type()); |
70 } | 92 } |
71 | 93 |
72 TEST_F(ServiceWorkerDispatcherHostTest, Enabled) { | 94 TEST_F(ServiceWorkerDispatcherHostTest, Enabled) { |
73 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( | 95 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( |
74 switches::kEnableServiceWorker)); | 96 switches::kEnableServiceWorker)); |
75 CommandLine::ForCurrentProcess()->AppendSwitch( | 97 CommandLine::ForCurrentProcess()->AppendSwitch( |
76 switches::kEnableServiceWorker); | 98 switches::kEnableServiceWorker); |
77 | 99 |
78 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = | 100 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = |
79 new TestingServiceWorkerDispatcherHost(kRenderProcessId, context_.get()); | 101 new TestingServiceWorkerDispatcherHost(kRenderProcessId, |
102 context_wrapper_.get()); | |
80 | 103 |
81 bool handled; | 104 bool handled; |
82 dispatcher_host->OnMessageReceived( | 105 dispatcher_host->OnMessageReceived( |
83 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, GURL(), GURL()), | 106 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, GURL(), GURL()), |
84 &handled); | 107 &handled); |
85 DCHECK(handled); | 108 EXPECT_TRUE(handled); |
86 | 109 |
87 // TODO(alecflett): Pump the message loop when this becomes async. | 110 // TODO(alecflett): Pump the message loop when this becomes async. |
88 DCHECK_EQ(1UL, dispatcher_host->sent_messages_.size()); | 111 EXPECT_EQ(1UL, dispatcher_host->sent_messages_.size()); |
kinuko
2013/11/18 05:57:24
ditto
| |
89 DCHECK_EQ(static_cast<uint32>(ServiceWorkerMsg_ServiceWorkerRegistered::ID), | 112 EXPECT_EQ(static_cast<uint32>(ServiceWorkerMsg_ServiceWorkerRegistered::ID), |
90 dispatcher_host->sent_messages_[0]->type()); | 113 dispatcher_host->sent_messages_[0]->type()); |
91 } | 114 } |
92 | 115 |
93 TEST_F(ServiceWorkerDispatcherHostTest, EarlyContextDeletion) { | 116 TEST_F(ServiceWorkerDispatcherHostTest, EarlyContextDeletion) { |
94 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( | 117 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( |
95 switches::kEnableServiceWorker)); | 118 switches::kEnableServiceWorker)); |
96 CommandLine::ForCurrentProcess()->AppendSwitch( | 119 CommandLine::ForCurrentProcess()->AppendSwitch( |
97 switches::kEnableServiceWorker); | 120 switches::kEnableServiceWorker); |
98 | 121 |
99 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = | 122 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = |
100 new TestingServiceWorkerDispatcherHost(kRenderProcessId, context_.get()); | 123 new TestingServiceWorkerDispatcherHost(kRenderProcessId, |
124 context_wrapper_.get()); | |
101 | 125 |
102 context_.reset(); | 126 context_wrapper_->Shutdown(); |
127 context_wrapper_ = NULL; | |
103 | 128 |
104 bool handled; | 129 bool handled; |
105 dispatcher_host->OnMessageReceived( | 130 dispatcher_host->OnMessageReceived( |
106 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, GURL(), GURL()), | 131 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, GURL(), GURL()), |
107 &handled); | 132 &handled); |
108 DCHECK(handled); | 133 EXPECT_TRUE(handled); |
109 | 134 |
110 // TODO(alecflett): Pump the message loop when this becomes async. | 135 // TODO(alecflett): Pump the message loop when this becomes async. |
111 DCHECK_EQ(1UL, dispatcher_host->sent_messages_.size()); | 136 EXPECT_EQ(1UL, dispatcher_host->sent_messages_.size()); |
112 DCHECK_EQ( | 137 EXPECT_EQ( |
113 static_cast<uint32>(ServiceWorkerMsg_ServiceWorkerRegistrationError::ID), | 138 static_cast<uint32>(ServiceWorkerMsg_ServiceWorkerRegistrationError::ID), |
114 dispatcher_host->sent_messages_[0]->type()); | 139 dispatcher_host->sent_messages_[0]->type()); |
115 } | 140 } |
116 | 141 |
142 TEST_F(ServiceWorkerDispatcherHostTest, ProviderCreatedAndDestroyed) { | |
143 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = | |
144 new TestingServiceWorkerDispatcherHost(kRenderProcessId, | |
145 context_wrapper_.get()); | |
146 | |
147 bool handled = false; | |
148 dispatcher_host->OnMessageReceived( | |
149 ServiceWorkerHostMsg_ProviderCreated(1), | |
150 &handled); | |
151 EXPECT_TRUE(handled); | |
152 EXPECT_TRUE(context()->GetProviderHost(kRenderProcessId, 1)); | |
153 | |
154 handled = false; | |
155 dispatcher_host->OnMessageReceived( | |
156 ServiceWorkerHostMsg_ProviderCreated(1), | |
157 &handled); | |
158 EXPECT_TRUE(handled); | |
159 EXPECT_EQ(1, dispatcher_host->bad_messages_received_count_); | |
160 | |
161 handled = false; | |
162 dispatcher_host->OnMessageReceived( | |
163 ServiceWorkerHostMsg_ProviderDestroyed(1), | |
164 &handled); | |
165 EXPECT_TRUE(handled); | |
166 EXPECT_FALSE(context()->GetProviderHost(kRenderProcessId, 1)); | |
167 | |
168 handled = false; | |
169 dispatcher_host->OnMessageReceived( | |
170 ServiceWorkerHostMsg_ProviderDestroyed(1), | |
171 &handled); | |
172 EXPECT_TRUE(handled); | |
173 EXPECT_EQ(2, dispatcher_host->bad_messages_received_count_); | |
174 } | |
175 | |
117 } // namespace content | 176 } // namespace content |
OLD | NEW |