Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / content / browser / service_worker / service_worker_provider_host_unittest.cc
1 // Copyright 2014 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 "base/basictypes.h"
6 #include "base/memory/weak_ptr.h"
7 #include "content/browser/service_worker/service_worker_context_core.h"
8 #include "content/browser/service_worker/service_worker_provider_host.h"
9 #include "content/browser/service_worker/service_worker_register_job.h"
10 #include "content/browser/service_worker/service_worker_registration.h"
11 #include "content/browser/service_worker/service_worker_version.h"
12 #include "content/public/test/test_browser_thread_bundle.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace content {
16
17 static const int kRenderProcessId = 33;  // Dummy process ID for testing.
18
19 class ServiceWorkerProviderHostTest : public testing::Test {
20  protected:
21   ServiceWorkerProviderHostTest()
22       : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {}
23   virtual ~ServiceWorkerProviderHostTest() {}
24
25   virtual void SetUp() OVERRIDE {
26     context_.reset(
27         new ServiceWorkerContextCore(base::FilePath(),
28                                      base::MessageLoopProxy::current(),
29                                      base::MessageLoopProxy::current(),
30                                      base::MessageLoopProxy::current(),
31                                      NULL,
32                                      NULL,
33                                      NULL));
34
35     scope_ = GURL("http://www.example.com/");
36     script_url_ = GURL("http://www.example.com/service_worker.js");
37     registration_ = new ServiceWorkerRegistration(
38         scope_, script_url_, 1L, context_->AsWeakPtr());
39     version_ = new ServiceWorkerVersion(
40         registration_,
41         1L, context_->AsWeakPtr());
42
43     // Prepare provider hosts (for the same process).
44     scoped_ptr<ServiceWorkerProviderHost> host1(new ServiceWorkerProviderHost(
45         kRenderProcessId, 1 /* provider_id */,
46         context_->AsWeakPtr(), NULL));
47     scoped_ptr<ServiceWorkerProviderHost> host2(new ServiceWorkerProviderHost(
48         kRenderProcessId, 2 /* provider_id */,
49         context_->AsWeakPtr(), NULL));
50     provider_host1_ = host1->AsWeakPtr();
51     provider_host2_ = host2->AsWeakPtr();
52     context_->AddProviderHost(make_scoped_ptr(host1.release()));
53     context_->AddProviderHost(make_scoped_ptr(host2.release()));
54   }
55
56   virtual void TearDown() OVERRIDE {
57     version_ = 0;
58     registration_ = 0;
59     context_.reset();
60   }
61
62   void VerifyVersionAttributes(
63       base::WeakPtr<ServiceWorkerProviderHost> provider_host,
64       ServiceWorkerVersion* installing,
65       ServiceWorkerVersion* waiting,
66       ServiceWorkerVersion* active) {
67     EXPECT_EQ(installing, provider_host->installing_version_);
68     EXPECT_EQ(waiting, provider_host->waiting_version_);
69     EXPECT_EQ(active, provider_host->active_version_);
70     EXPECT_FALSE(provider_host->controlling_version_);
71   }
72
73   content::TestBrowserThreadBundle thread_bundle_;
74   scoped_ptr<ServiceWorkerContextCore> context_;
75   scoped_refptr<ServiceWorkerRegistration> registration_;
76   scoped_refptr<ServiceWorkerVersion> version_;
77   base::WeakPtr<ServiceWorkerProviderHost> provider_host1_;
78   base::WeakPtr<ServiceWorkerProviderHost> provider_host2_;
79   GURL scope_;
80   GURL script_url_;
81
82  private:
83   DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest);
84 };
85
86 TEST_F(ServiceWorkerProviderHostTest, SetActiveVersion_ProcessStatus) {
87   provider_host1_->AssociateRegistration(registration_);
88   ASSERT_FALSE(version_->HasProcessToRun());
89
90   // Associating version_ to a provider_host's active version will internally
91   // add the provider_host's process ref to the version.
92   registration_->SetActiveVersion(version_);
93   ASSERT_TRUE(version_->HasProcessToRun());
94
95   // Re-associating the same version and provider_host should just work too.
96   registration_->SetActiveVersion(version_);
97   ASSERT_TRUE(version_->HasProcessToRun());
98
99   // Resetting the provider_host's active version should remove process refs
100   // from the version.
101   provider_host1_->UnassociateRegistration();
102   ASSERT_FALSE(version_->HasProcessToRun());
103 }
104
105 TEST_F(ServiceWorkerProviderHostTest,
106        SetActiveVersion_MultipleHostsForSameProcess) {
107   provider_host1_->AssociateRegistration(registration_);
108   provider_host2_->AssociateRegistration(registration_);
109   ASSERT_FALSE(version_->HasProcessToRun());
110
111   // Associating version_ to two providers as active version.
112   registration_->SetActiveVersion(version_);
113   ASSERT_TRUE(version_->HasProcessToRun());
114
115   // Disassociating one provider_host shouldn't remove all process refs
116   // from the version yet.
117   provider_host1_->UnassociateRegistration();
118   ASSERT_TRUE(version_->HasProcessToRun());
119
120   // Disassociating the other provider_host will remove all process refs.
121   provider_host2_->UnassociateRegistration();
122   ASSERT_FALSE(version_->HasProcessToRun());
123 }
124
125 TEST_F(ServiceWorkerProviderHostTest, SetWaitingVersion_ProcessStatus) {
126   provider_host1_->AssociateRegistration(registration_);
127   ASSERT_FALSE(version_->HasProcessToRun());
128
129   // Associating version_ to a provider_host's waiting version will internally
130   // add the provider_host's process ref to the version.
131   registration_->SetWaitingVersion(version_);
132   ASSERT_TRUE(version_->HasProcessToRun());
133
134   // Re-associating the same version and provider_host should just work too.
135   registration_->SetWaitingVersion(version_);
136   ASSERT_TRUE(version_->HasProcessToRun());
137
138   // Resetting the provider_host's waiting version should remove process refs
139   // from the version.
140   provider_host1_->UnassociateRegistration();
141   ASSERT_FALSE(version_->HasProcessToRun());
142 }
143
144 TEST_F(ServiceWorkerProviderHostTest,
145        SetWaitingVersion_MultipleHostsForSameProcess) {
146   provider_host1_->AssociateRegistration(registration_);
147   provider_host2_->AssociateRegistration(registration_);
148   ASSERT_FALSE(version_->HasProcessToRun());
149
150   // Associating version_ to two providers as waiting version.
151   registration_->SetWaitingVersion(version_);
152   ASSERT_TRUE(version_->HasProcessToRun());
153
154   // Disassociating one provider_host shouldn't remove all process refs
155   // from the version yet.
156   provider_host1_->UnassociateRegistration();
157   ASSERT_TRUE(version_->HasProcessToRun());
158
159   // Disassociating the other provider_host will remove all process refs.
160   provider_host2_->UnassociateRegistration();
161   ASSERT_FALSE(version_->HasProcessToRun());
162 }
163
164 TEST_F(ServiceWorkerProviderHostTest,
165        ObserveVersionAttributesChanged_Basic) {
166   provider_host1_->AssociateRegistration(registration_);
167   provider_host2_->AssociateRegistration(registration_);
168   VerifyVersionAttributes(provider_host1_, NULL, NULL, NULL);
169   VerifyVersionAttributes(provider_host2_, NULL, NULL, NULL);
170
171   registration_->SetInstallingVersion(version_);
172   VerifyVersionAttributes(provider_host1_, version_, NULL, NULL);
173   VerifyVersionAttributes(provider_host2_, version_, NULL, NULL);
174
175   registration_->SetWaitingVersion(version_);
176   VerifyVersionAttributes(provider_host1_, NULL, version_, NULL);
177   VerifyVersionAttributes(provider_host2_, NULL, version_, NULL);
178
179   // Disassociating the registration should clear all version attributes.
180   provider_host2_->UnassociateRegistration();
181   VerifyVersionAttributes(provider_host1_, NULL, version_, NULL);
182   VerifyVersionAttributes(provider_host2_, NULL, NULL, NULL);
183
184   // Shouldn't notify the disassociated provider of the change.
185   registration_->SetActiveVersion(version_);
186   VerifyVersionAttributes(provider_host1_, NULL, NULL, version_);
187   VerifyVersionAttributes(provider_host2_, NULL, NULL, NULL);
188 }
189
190 TEST_F(ServiceWorkerProviderHostTest,
191        ObserveVersionAttributesChanged_MultipleVersions) {
192   provider_host1_->AssociateRegistration(registration_);
193   provider_host2_->AssociateRegistration(registration_);
194   VerifyVersionAttributes(provider_host1_, NULL, NULL, NULL);
195   VerifyVersionAttributes(provider_host2_, NULL, NULL, NULL);
196
197   scoped_refptr<ServiceWorkerVersion> version1 =
198       new ServiceWorkerVersion(registration_, 10L, context_->AsWeakPtr());
199   scoped_refptr<ServiceWorkerVersion> version2 =
200       new ServiceWorkerVersion(registration_, 20L, context_->AsWeakPtr());
201
202   registration_->SetInstallingVersion(version1);
203   VerifyVersionAttributes(provider_host1_, version1, NULL, NULL);
204   VerifyVersionAttributes(provider_host2_, version1, NULL, NULL);
205
206   registration_->SetWaitingVersion(version1);
207   VerifyVersionAttributes(provider_host1_, NULL, version1, NULL);
208   VerifyVersionAttributes(provider_host2_, NULL, version1, NULL);
209
210   registration_->SetInstallingVersion(version2);
211   VerifyVersionAttributes(provider_host1_, version2, version1, NULL);
212   VerifyVersionAttributes(provider_host2_, version2, version1, NULL);
213
214   // Disassociating the registration should clear all version attributes.
215   provider_host2_->UnassociateRegistration();
216   VerifyVersionAttributes(provider_host1_, version2, version1, NULL);
217   VerifyVersionAttributes(provider_host2_, NULL, NULL, NULL);
218
219   // Shouldn't notify the disassociated provider of the change.
220   registration_->SetActiveVersion(version1);
221   VerifyVersionAttributes(provider_host1_, version2, NULL, version1);
222   VerifyVersionAttributes(provider_host2_, NULL, NULL, NULL);
223
224   registration_->SetActiveVersion(version2);
225   VerifyVersionAttributes(provider_host1_, NULL, NULL, version2);
226   VerifyVersionAttributes(provider_host2_, NULL, NULL, NULL);
227 }
228
229 }  // namespace content