OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/child/service_worker/service_worker_dispatcher.h" |
| 6 #include "content/child/service_worker/service_worker_message_sender.h" |
| 7 #include "content/child/service_worker/web_service_worker_impl.h" |
| 8 #include "content/child/service_worker/web_service_worker_registration_impl.h" |
| 9 #include "content/common/service_worker/service_worker_messages.h" |
| 10 #include "content/common/service_worker/service_worker_types.h" |
| 11 #include "ipc/ipc_test_sink.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace content { |
| 15 |
| 16 class ServiceWorkerTestSender : public ServiceWorkerMessageSender { |
| 17 public: |
| 18 explicit ServiceWorkerTestSender(IPC::TestSink* ipc_sink) |
| 19 : ServiceWorkerMessageSender(nullptr), ipc_sink_(ipc_sink) {} |
| 20 |
| 21 bool Send(IPC::Message* message) override { |
| 22 return ipc_sink_->Send(message); |
| 23 } |
| 24 |
| 25 private: |
| 26 ~ServiceWorkerTestSender() override {} |
| 27 |
| 28 IPC::TestSink* ipc_sink_; |
| 29 |
| 30 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerTestSender); |
| 31 }; |
| 32 |
| 33 class ServiceWorkerDispatcherTest : public testing::Test { |
| 34 public: |
| 35 ServiceWorkerDispatcherTest() {} |
| 36 |
| 37 void SetUp() override { |
| 38 sender_ = new ServiceWorkerTestSender(&ipc_sink_); |
| 39 dispatcher_.reset(new ServiceWorkerDispatcher(sender_.get())); |
| 40 } |
| 41 |
| 42 void CreateObjectInfoAndVersionAttributes( |
| 43 ServiceWorkerRegistrationObjectInfo* info, |
| 44 ServiceWorkerVersionAttributes* attrs) { |
| 45 info->handle_id = 10; |
| 46 info->registration_id = 20; |
| 47 |
| 48 attrs->active.handle_id = 100; |
| 49 attrs->active.version_id = 200; |
| 50 attrs->waiting.handle_id = 101; |
| 51 attrs->waiting.version_id = 201; |
| 52 attrs->installing.handle_id = 102; |
| 53 attrs->installing.version_id = 202; |
| 54 } |
| 55 |
| 56 WebServiceWorkerRegistrationImpl* FindOrCreateRegistration( |
| 57 const ServiceWorkerRegistrationObjectInfo& info, |
| 58 const ServiceWorkerVersionAttributes& attrs) { |
| 59 return dispatcher_->FindOrCreateRegistration(info, attrs); |
| 60 } |
| 61 |
| 62 bool ContainsServiceWorker(int handle_id) { |
| 63 return ContainsKey(dispatcher_->service_workers_, handle_id); |
| 64 } |
| 65 |
| 66 bool ContainsRegistration(int registration_handle_id) { |
| 67 return ContainsKey(dispatcher_->registrations_, registration_handle_id); |
| 68 } |
| 69 |
| 70 ServiceWorkerDispatcher* dispatcher() { return dispatcher_.get(); } |
| 71 IPC::TestSink* ipc_sink() { return &ipc_sink_; } |
| 72 |
| 73 private: |
| 74 IPC::TestSink ipc_sink_; |
| 75 scoped_ptr<ServiceWorkerDispatcher> dispatcher_; |
| 76 scoped_refptr<ServiceWorkerMessageSender> sender_; |
| 77 |
| 78 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDispatcherTest); |
| 79 }; |
| 80 |
| 81 // TODO(nhiroki): Add tests for message handlers especially to receive reference |
| 82 // counts like OnAssociateRegistration(). |
| 83 |
| 84 TEST_F(ServiceWorkerDispatcherTest, GetServiceWorker) { |
| 85 ServiceWorkerRegistrationObjectInfo info; |
| 86 ServiceWorkerVersionAttributes attrs; |
| 87 CreateObjectInfoAndVersionAttributes(&info, &attrs); |
| 88 |
| 89 // Should return a worker object newly created with incrementing refcount. |
| 90 bool adopt_handle = false; |
| 91 scoped_ptr<WebServiceWorkerImpl> worker( |
| 92 dispatcher()->GetServiceWorker(attrs.installing, adopt_handle)); |
| 93 EXPECT_TRUE(worker); |
| 94 EXPECT_TRUE(ContainsServiceWorker(attrs.installing.handle_id)); |
| 95 EXPECT_EQ(1UL, ipc_sink()->message_count()); |
| 96 EXPECT_EQ(ServiceWorkerHostMsg_IncrementServiceWorkerRefCount::ID, |
| 97 ipc_sink()->GetMessageAt(0)->type()); |
| 98 |
| 99 ipc_sink()->ClearMessages(); |
| 100 |
| 101 // Should return the existing worker object. |
| 102 adopt_handle = false; |
| 103 WebServiceWorkerImpl* existing_worker = |
| 104 dispatcher()->GetServiceWorker(attrs.installing, adopt_handle); |
| 105 EXPECT_EQ(worker, existing_worker); |
| 106 EXPECT_EQ(0UL, ipc_sink()->message_count()); |
| 107 |
| 108 // Should return the existing worker object with adopting refcount. |
| 109 adopt_handle = true; |
| 110 existing_worker = |
| 111 dispatcher()->GetServiceWorker(attrs.installing, adopt_handle); |
| 112 EXPECT_EQ(worker, existing_worker); |
| 113 ASSERT_EQ(1UL, ipc_sink()->message_count()); |
| 114 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID, |
| 115 ipc_sink()->GetMessageAt(0)->type()); |
| 116 |
| 117 ipc_sink()->ClearMessages(); |
| 118 |
| 119 // Should return another worker object newly created with adopting refcount. |
| 120 adopt_handle = true; |
| 121 scoped_ptr<WebServiceWorkerImpl> another_worker( |
| 122 dispatcher()->GetServiceWorker(attrs.waiting, adopt_handle)); |
| 123 EXPECT_NE(worker.get(), another_worker.get()); |
| 124 EXPECT_TRUE(ContainsServiceWorker(attrs.waiting.handle_id)); |
| 125 EXPECT_EQ(0UL, ipc_sink()->message_count()); |
| 126 |
| 127 // Should return nullptr when a given object info is invalid. |
| 128 adopt_handle = false; |
| 129 WebServiceWorkerImpl* invalid_worker = |
| 130 dispatcher()->GetServiceWorker(ServiceWorkerObjectInfo(), adopt_handle); |
| 131 EXPECT_FALSE(invalid_worker); |
| 132 EXPECT_EQ(0UL, ipc_sink()->message_count()); |
| 133 |
| 134 adopt_handle = true; |
| 135 invalid_worker = |
| 136 dispatcher()->GetServiceWorker(ServiceWorkerObjectInfo(), adopt_handle); |
| 137 EXPECT_FALSE(invalid_worker); |
| 138 EXPECT_EQ(0UL, ipc_sink()->message_count()); |
| 139 } |
| 140 |
| 141 TEST_F(ServiceWorkerDispatcherTest, CreateServiceWorkerRegistration) { |
| 142 ServiceWorkerRegistrationObjectInfo info; |
| 143 ServiceWorkerVersionAttributes attrs; |
| 144 CreateObjectInfoAndVersionAttributes(&info, &attrs); |
| 145 |
| 146 // Should return a registration object newly created with incrementing |
| 147 // refcount. |
| 148 bool adopt_handle = false; |
| 149 scoped_ptr<WebServiceWorkerRegistrationImpl> registration( |
| 150 dispatcher()->CreateServiceWorkerRegistration(info, adopt_handle)); |
| 151 EXPECT_TRUE(registration); |
| 152 EXPECT_TRUE(ContainsRegistration(info.handle_id)); |
| 153 ASSERT_EQ(1UL, ipc_sink()->message_count()); |
| 154 EXPECT_EQ(ServiceWorkerHostMsg_IncrementRegistrationRefCount::ID, |
| 155 ipc_sink()->GetMessageAt(0)->type()); |
| 156 |
| 157 registration.reset(); |
| 158 EXPECT_FALSE(ContainsRegistration(info.handle_id)); |
| 159 ipc_sink()->ClearMessages(); |
| 160 |
| 161 // Should return another registration object newly created with adopting |
| 162 // refcount. |
| 163 adopt_handle = true; |
| 164 scoped_ptr<WebServiceWorkerRegistrationImpl> another_registration( |
| 165 dispatcher()->CreateServiceWorkerRegistration(info, adopt_handle)); |
| 166 EXPECT_TRUE(another_registration); |
| 167 EXPECT_TRUE(ContainsRegistration(info.handle_id)); |
| 168 EXPECT_EQ(0UL, ipc_sink()->message_count()); |
| 169 |
| 170 another_registration.reset(); |
| 171 ipc_sink()->ClearMessages(); |
| 172 |
| 173 // should return nullptr when a given object info is invalid. |
| 174 adopt_handle = false; |
| 175 scoped_ptr<WebServiceWorkerRegistrationImpl> invalid_registration( |
| 176 dispatcher()->CreateServiceWorkerRegistration( |
| 177 ServiceWorkerRegistrationObjectInfo(), adopt_handle)); |
| 178 EXPECT_FALSE(invalid_registration); |
| 179 EXPECT_FALSE(ContainsRegistration(info.handle_id)); |
| 180 EXPECT_EQ(0UL, ipc_sink()->message_count()); |
| 181 |
| 182 adopt_handle = true; |
| 183 invalid_registration.reset(dispatcher()->CreateServiceWorkerRegistration( |
| 184 ServiceWorkerRegistrationObjectInfo(), adopt_handle)); |
| 185 EXPECT_FALSE(invalid_registration); |
| 186 EXPECT_FALSE(ContainsRegistration(info.handle_id)); |
| 187 EXPECT_EQ(0UL, ipc_sink()->message_count()); |
| 188 } |
| 189 |
| 190 TEST_F(ServiceWorkerDispatcherTest, FindOrCreateRegistration) { |
| 191 ServiceWorkerRegistrationObjectInfo info; |
| 192 ServiceWorkerVersionAttributes attrs; |
| 193 CreateObjectInfoAndVersionAttributes(&info, &attrs); |
| 194 |
| 195 // Should return a registration object newly created with adopting refcounts. |
| 196 scoped_ptr<WebServiceWorkerRegistrationImpl> registration( |
| 197 FindOrCreateRegistration(info, attrs)); |
| 198 EXPECT_TRUE(registration); |
| 199 EXPECT_EQ(info.registration_id, registration->registration_id()); |
| 200 EXPECT_EQ(0UL, ipc_sink()->message_count()); |
| 201 |
| 202 // Should return the existing registration object with adopting refcounts. |
| 203 WebServiceWorkerRegistrationImpl* existing_registration = |
| 204 FindOrCreateRegistration(info, attrs); |
| 205 EXPECT_EQ(registration, existing_registration); |
| 206 ASSERT_EQ(4UL, ipc_sink()->message_count()); |
| 207 EXPECT_EQ(ServiceWorkerHostMsg_DecrementRegistrationRefCount::ID, |
| 208 ipc_sink()->GetMessageAt(0)->type()); |
| 209 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID, |
| 210 ipc_sink()->GetMessageAt(1)->type()); |
| 211 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID, |
| 212 ipc_sink()->GetMessageAt(2)->type()); |
| 213 EXPECT_EQ(ServiceWorkerHostMsg_DecrementServiceWorkerRefCount::ID, |
| 214 ipc_sink()->GetMessageAt(3)->type()); |
| 215 } |
| 216 |
| 217 } // namespace content |
OLD | NEW |