OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/memory/weak_ptr.h" | 6 #include "base/memory/weak_ptr.h" |
7 #include "base/thread_task_runner_handle.h" | 7 #include "base/thread_task_runner_handle.h" |
| 8 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
8 #include "content/browser/service_worker/service_worker_context_core.h" | 9 #include "content/browser/service_worker/service_worker_context_core.h" |
9 #include "content/browser/service_worker/service_worker_provider_host.h" | 10 #include "content/browser/service_worker/service_worker_provider_host.h" |
10 #include "content/browser/service_worker/service_worker_register_job.h" | 11 #include "content/browser/service_worker/service_worker_register_job.h" |
11 #include "content/browser/service_worker/service_worker_registration.h" | 12 #include "content/browser/service_worker/service_worker_registration.h" |
12 #include "content/browser/service_worker/service_worker_version.h" | 13 #include "content/browser/service_worker/service_worker_version.h" |
13 #include "content/public/test/test_browser_thread_bundle.h" | 14 #include "content/public/test/test_browser_thread_bundle.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
15 | 16 |
16 namespace content { | 17 namespace content { |
17 | 18 |
18 static const int kRenderProcessId = 33; // Dummy process ID for testing. | 19 static const int kRenderProcessId = 33; // Dummy process ID for testing. |
19 | 20 |
20 class ServiceWorkerProviderHostTest : public testing::Test { | 21 class ServiceWorkerProviderHostTest : public testing::Test { |
21 protected: | 22 protected: |
22 ServiceWorkerProviderHostTest() | 23 ServiceWorkerProviderHostTest() |
23 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} | 24 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} |
24 virtual ~ServiceWorkerProviderHostTest() {} | 25 virtual ~ServiceWorkerProviderHostTest() {} |
25 | 26 |
26 virtual void SetUp() OVERRIDE { | 27 virtual void SetUp() OVERRIDE { |
27 context_.reset( | 28 helper_.reset(new EmbeddedWorkerTestHelper(kRenderProcessId)); |
28 new ServiceWorkerContextCore(base::FilePath(), | 29 context_ = helper_->context(); |
29 base::ThreadTaskRunnerHandle::Get(), | 30 pattern_ = GURL("http://www.example.com/"); |
30 base::ThreadTaskRunnerHandle::Get(), | |
31 base::ThreadTaskRunnerHandle::Get(), | |
32 NULL, | |
33 NULL, | |
34 NULL)); | |
35 | |
36 scope_ = GURL("http://www.example.com/"); | |
37 script_url_ = GURL("http://www.example.com/service_worker.js"); | 31 script_url_ = GURL("http://www.example.com/service_worker.js"); |
38 registration_ = new ServiceWorkerRegistration( | 32 registration_ = new ServiceWorkerRegistration( |
39 scope_, 1L, context_->AsWeakPtr()); | 33 pattern_, 1L, context_->AsWeakPtr()); |
40 version_ = new ServiceWorkerVersion( | 34 version_ = new ServiceWorkerVersion( |
41 registration_.get(), script_url_, 1L, context_->AsWeakPtr()); | 35 registration_.get(), script_url_, 1L, context_->AsWeakPtr()); |
42 | 36 |
43 // Prepare provider hosts (for the same process). | 37 // Prepare provider hosts (for the same process). |
44 scoped_ptr<ServiceWorkerProviderHost> host1(new ServiceWorkerProviderHost( | 38 scoped_ptr<ServiceWorkerProviderHost> host1(new ServiceWorkerProviderHost( |
45 kRenderProcessId, 1 /* provider_id */, | 39 kRenderProcessId, 1 /* provider_id */, |
46 context_->AsWeakPtr(), NULL)); | 40 context_->AsWeakPtr(), NULL)); |
47 scoped_ptr<ServiceWorkerProviderHost> host2(new ServiceWorkerProviderHost( | 41 scoped_ptr<ServiceWorkerProviderHost> host2(new ServiceWorkerProviderHost( |
48 kRenderProcessId, 2 /* provider_id */, | 42 kRenderProcessId, 2 /* provider_id */, |
49 context_->AsWeakPtr(), NULL)); | 43 context_->AsWeakPtr(), NULL)); |
50 provider_host1_ = host1->AsWeakPtr(); | 44 provider_host1_ = host1->AsWeakPtr(); |
51 provider_host2_ = host2->AsWeakPtr(); | 45 provider_host2_ = host2->AsWeakPtr(); |
52 context_->AddProviderHost(make_scoped_ptr(host1.release())); | 46 context_->AddProviderHost(make_scoped_ptr(host1.release())); |
53 context_->AddProviderHost(make_scoped_ptr(host2.release())); | 47 context_->AddProviderHost(make_scoped_ptr(host2.release())); |
54 } | 48 } |
55 | 49 |
56 virtual void TearDown() OVERRIDE { | 50 virtual void TearDown() OVERRIDE { |
57 version_ = 0; | 51 version_ = 0; |
58 registration_ = 0; | 52 registration_ = 0; |
59 context_.reset(); | 53 helper_.reset(); |
| 54 } |
| 55 |
| 56 bool HasProcessToRun() const { |
| 57 return context_->process_manager()->PatternHasProcessToRun(pattern_); |
60 } | 58 } |
61 | 59 |
62 void VerifyVersionAttributes( | 60 void VerifyVersionAttributes( |
63 base::WeakPtr<ServiceWorkerProviderHost> provider_host, | 61 base::WeakPtr<ServiceWorkerProviderHost> provider_host, |
64 ServiceWorkerVersion* installing, | 62 ServiceWorkerVersion* installing, |
65 ServiceWorkerVersion* waiting, | 63 ServiceWorkerVersion* waiting, |
66 ServiceWorkerVersion* active) { | 64 ServiceWorkerVersion* active) { |
67 EXPECT_EQ(installing, provider_host->installing_version_.get()); | 65 EXPECT_EQ(installing, provider_host->installing_version_.get()); |
68 EXPECT_EQ(waiting, provider_host->waiting_version_.get()); | 66 EXPECT_EQ(waiting, provider_host->waiting_version_.get()); |
69 EXPECT_EQ(active, provider_host->active_version_.get()); | 67 EXPECT_EQ(active, provider_host->active_version_.get()); |
70 EXPECT_FALSE(provider_host->controlling_version_.get()); | 68 EXPECT_FALSE(provider_host->controlling_version_.get()); |
71 } | 69 } |
72 | 70 |
73 content::TestBrowserThreadBundle thread_bundle_; | 71 content::TestBrowserThreadBundle thread_bundle_; |
74 scoped_ptr<ServiceWorkerContextCore> context_; | 72 scoped_ptr<EmbeddedWorkerTestHelper> helper_; |
| 73 ServiceWorkerContextCore* context_; |
75 scoped_refptr<ServiceWorkerRegistration> registration_; | 74 scoped_refptr<ServiceWorkerRegistration> registration_; |
76 scoped_refptr<ServiceWorkerVersion> version_; | 75 scoped_refptr<ServiceWorkerVersion> version_; |
77 base::WeakPtr<ServiceWorkerProviderHost> provider_host1_; | 76 base::WeakPtr<ServiceWorkerProviderHost> provider_host1_; |
78 base::WeakPtr<ServiceWorkerProviderHost> provider_host2_; | 77 base::WeakPtr<ServiceWorkerProviderHost> provider_host2_; |
79 GURL scope_; | 78 GURL pattern_; |
80 GURL script_url_; | 79 GURL script_url_; |
81 | 80 |
82 private: | 81 private: |
83 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest); | 82 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest); |
84 }; | 83 }; |
85 | 84 |
86 TEST_F(ServiceWorkerProviderHostTest, SetActiveVersion_ProcessStatus) { | 85 TEST_F(ServiceWorkerProviderHostTest, SetActiveVersion_ProcessStatus) { |
87 provider_host1_->AssociateRegistration(registration_.get()); | 86 provider_host1_->AssociateRegistration(registration_.get()); |
88 ASSERT_FALSE(version_->HasProcessToRun()); | 87 ASSERT_TRUE(HasProcessToRun()); |
89 | 88 |
90 // Associating version_ to a provider_host's active version will internally | 89 // Associating version_ to a provider_host's active version will internally |
91 // add the provider_host's process ref to the version. | 90 // add the provider_host's process ref to the version. |
92 registration_->SetActiveVersion(version_.get()); | 91 registration_->SetActiveVersion(version_.get()); |
93 ASSERT_TRUE(version_->HasProcessToRun()); | 92 ASSERT_TRUE(HasProcessToRun()); |
94 | 93 |
95 // Re-associating the same version and provider_host should just work too. | 94 // Re-associating the same version and provider_host should just work too. |
96 registration_->SetActiveVersion(version_.get()); | 95 registration_->SetActiveVersion(version_.get()); |
97 ASSERT_TRUE(version_->HasProcessToRun()); | 96 ASSERT_TRUE(HasProcessToRun()); |
98 | 97 |
99 // Resetting the provider_host's active version should remove process refs | 98 // Resetting the provider_host's active version should remove process refs |
100 // from the version. | 99 // from the version. |
101 provider_host1_->UnassociateRegistration(); | 100 provider_host1_->UnassociateRegistration(); |
102 ASSERT_FALSE(version_->HasProcessToRun()); | 101 ASSERT_FALSE(HasProcessToRun()); |
103 } | 102 } |
104 | 103 |
105 TEST_F(ServiceWorkerProviderHostTest, | 104 TEST_F(ServiceWorkerProviderHostTest, |
106 SetActiveVersion_MultipleHostsForSameProcess) { | 105 SetActiveVersion_MultipleHostsForSameProcess) { |
107 provider_host1_->AssociateRegistration(registration_.get()); | 106 provider_host1_->AssociateRegistration(registration_.get()); |
108 provider_host2_->AssociateRegistration(registration_.get()); | 107 provider_host2_->AssociateRegistration(registration_.get()); |
109 ASSERT_FALSE(version_->HasProcessToRun()); | 108 ASSERT_TRUE(HasProcessToRun()); |
110 | 109 |
111 // Associating version_ to two providers as active version. | 110 // Associating version_ to two providers as active version. |
112 registration_->SetActiveVersion(version_.get()); | 111 registration_->SetActiveVersion(version_.get()); |
113 ASSERT_TRUE(version_->HasProcessToRun()); | 112 ASSERT_TRUE(HasProcessToRun()); |
114 | 113 |
115 // Disassociating one provider_host shouldn't remove all process refs | 114 // Disassociating one provider_host shouldn't remove all process refs |
116 // from the version yet. | 115 // from the version yet. |
117 provider_host1_->UnassociateRegistration(); | 116 provider_host1_->UnassociateRegistration(); |
118 ASSERT_TRUE(version_->HasProcessToRun()); | 117 ASSERT_TRUE(HasProcessToRun()); |
119 | 118 |
120 // Disassociating the other provider_host will remove all process refs. | 119 // Disassociating the other provider_host will remove all process refs. |
121 provider_host2_->UnassociateRegistration(); | 120 provider_host2_->UnassociateRegistration(); |
122 ASSERT_FALSE(version_->HasProcessToRun()); | 121 ASSERT_FALSE(HasProcessToRun()); |
123 } | 122 } |
124 | 123 |
125 TEST_F(ServiceWorkerProviderHostTest, SetWaitingVersion_ProcessStatus) { | 124 TEST_F(ServiceWorkerProviderHostTest, SetWaitingVersion_ProcessStatus) { |
126 provider_host1_->AssociateRegistration(registration_.get()); | 125 provider_host1_->AssociateRegistration(registration_.get()); |
127 ASSERT_FALSE(version_->HasProcessToRun()); | 126 ASSERT_TRUE(HasProcessToRun()); |
128 | 127 |
129 // Associating version_ to a provider_host's waiting version will internally | 128 // Associating version_ to a provider_host's waiting version will internally |
130 // add the provider_host's process ref to the version. | 129 // add the provider_host's process ref to the version. |
131 registration_->SetWaitingVersion(version_.get()); | 130 registration_->SetWaitingVersion(version_.get()); |
132 ASSERT_TRUE(version_->HasProcessToRun()); | 131 ASSERT_TRUE(HasProcessToRun()); |
133 | 132 |
134 // Re-associating the same version and provider_host should just work too. | 133 // Re-associating the same version and provider_host should just work too. |
135 registration_->SetWaitingVersion(version_.get()); | 134 registration_->SetWaitingVersion(version_.get()); |
136 ASSERT_TRUE(version_->HasProcessToRun()); | 135 ASSERT_TRUE(HasProcessToRun()); |
137 | 136 |
138 // Resetting the provider_host's waiting version should remove process refs | 137 // Resetting the provider_host's waiting version should remove process refs |
139 // from the version. | 138 // from the version. |
140 provider_host1_->UnassociateRegistration(); | 139 provider_host1_->UnassociateRegistration(); |
141 ASSERT_FALSE(version_->HasProcessToRun()); | 140 ASSERT_FALSE(HasProcessToRun()); |
142 } | 141 } |
143 | 142 |
144 TEST_F(ServiceWorkerProviderHostTest, | 143 TEST_F(ServiceWorkerProviderHostTest, |
145 SetWaitingVersion_MultipleHostsForSameProcess) { | 144 SetWaitingVersion_MultipleHostsForSameProcess) { |
146 provider_host1_->AssociateRegistration(registration_.get()); | 145 provider_host1_->AssociateRegistration(registration_.get()); |
147 provider_host2_->AssociateRegistration(registration_.get()); | 146 provider_host2_->AssociateRegistration(registration_.get()); |
148 ASSERT_FALSE(version_->HasProcessToRun()); | 147 ASSERT_TRUE(HasProcessToRun()); |
149 | 148 |
150 // Associating version_ to two providers as waiting version. | 149 // Associating version_ to two providers as waiting version. |
151 registration_->SetWaitingVersion(version_.get()); | 150 registration_->SetWaitingVersion(version_.get()); |
152 ASSERT_TRUE(version_->HasProcessToRun()); | 151 ASSERT_TRUE(HasProcessToRun()); |
153 | 152 |
154 // Disassociating one provider_host shouldn't remove all process refs | 153 // Disassociating one provider_host shouldn't remove all process refs |
155 // from the version yet. | 154 // from the version yet. |
156 provider_host1_->UnassociateRegistration(); | 155 provider_host1_->UnassociateRegistration(); |
157 ASSERT_TRUE(version_->HasProcessToRun()); | 156 ASSERT_TRUE(HasProcessToRun()); |
158 | 157 |
159 // Disassociating the other provider_host will remove all process refs. | 158 // Disassociating the other provider_host will remove all process refs. |
160 provider_host2_->UnassociateRegistration(); | 159 provider_host2_->UnassociateRegistration(); |
161 ASSERT_FALSE(version_->HasProcessToRun()); | 160 ASSERT_FALSE(HasProcessToRun()); |
162 } | 161 } |
163 | 162 |
164 TEST_F(ServiceWorkerProviderHostTest, | 163 TEST_F(ServiceWorkerProviderHostTest, |
165 ObserveVersionAttributesChanged_Basic) { | 164 ObserveVersionAttributesChanged_Basic) { |
166 provider_host1_->AssociateRegistration(registration_.get()); | 165 provider_host1_->AssociateRegistration(registration_.get()); |
167 provider_host2_->AssociateRegistration(registration_.get()); | 166 provider_host2_->AssociateRegistration(registration_.get()); |
168 VerifyVersionAttributes(provider_host1_, NULL, NULL, NULL); | 167 VerifyVersionAttributes(provider_host1_, NULL, NULL, NULL); |
169 VerifyVersionAttributes(provider_host2_, NULL, NULL, NULL); | 168 VerifyVersionAttributes(provider_host2_, NULL, NULL, NULL); |
170 | 169 |
171 registration_->SetInstallingVersion(version_.get()); | 170 registration_->SetInstallingVersion(version_.get()); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 VerifyVersionAttributes( | 223 VerifyVersionAttributes( |
225 provider_host1_, version2.get(), NULL, version1.get()); | 224 provider_host1_, version2.get(), NULL, version1.get()); |
226 VerifyVersionAttributes(provider_host2_, NULL, NULL, NULL); | 225 VerifyVersionAttributes(provider_host2_, NULL, NULL, NULL); |
227 | 226 |
228 registration_->SetActiveVersion(version2.get()); | 227 registration_->SetActiveVersion(version2.get()); |
229 VerifyVersionAttributes(provider_host1_, NULL, NULL, version2.get()); | 228 VerifyVersionAttributes(provider_host1_, NULL, NULL, version2.get()); |
230 VerifyVersionAttributes(provider_host2_, NULL, NULL, NULL); | 229 VerifyVersionAttributes(provider_host2_, NULL, NULL, NULL); |
231 } | 230 } |
232 | 231 |
233 } // namespace content | 232 } // namespace content |
OLD | NEW |