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 "content/browser/service_worker/embedded_worker_test_helper.h" |
7 #include "content/browser/service_worker/service_worker_context_core.h" | 8 #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_provider_host.h" |
9 #include "content/browser/service_worker/service_worker_register_job.h" | 10 #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_registration.h" |
11 #include "content/browser/service_worker/service_worker_version.h" | 12 #include "content/browser/service_worker/service_worker_version.h" |
12 #include "content/public/test/test_browser_thread_bundle.h" | 13 #include "content/public/test/test_browser_thread_bundle.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
14 | 15 |
15 namespace content { | 16 namespace content { |
16 | 17 |
17 static const int kRenderProcessId = 33; // Dummy process ID for testing. | 18 static const int kRenderProcessId = 33; // Dummy process ID for testing. |
18 | 19 |
19 class ServiceWorkerProviderHostTest : public testing::Test { | 20 class ServiceWorkerProviderHostTest : public testing::Test { |
20 protected: | 21 protected: |
21 ServiceWorkerProviderHostTest() | 22 ServiceWorkerProviderHostTest() |
22 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} | 23 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} |
23 virtual ~ServiceWorkerProviderHostTest() {} | 24 virtual ~ServiceWorkerProviderHostTest() {} |
24 | 25 |
25 virtual void SetUp() OVERRIDE { | 26 virtual void SetUp() OVERRIDE { |
26 context_.reset( | 27 helper_.reset(new EmbeddedWorkerTestHelper(kRenderProcessId)); |
27 new ServiceWorkerContextCore(base::FilePath(), | 28 context_ = helper_->context(); |
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/"); | 29 scope_ = GURL("http://www.example.com/"); |
36 script_url_ = GURL("http://www.example.com/service_worker.js"); | 30 script_url_ = GURL("http://www.example.com/service_worker.js"); |
37 registration_ = new ServiceWorkerRegistration( | 31 registration_ = new ServiceWorkerRegistration( |
38 scope_, script_url_, 1L, context_->AsWeakPtr()); | 32 scope_, script_url_, 1L, context_->AsWeakPtr()); |
39 version_ = new ServiceWorkerVersion( | 33 version_ = new ServiceWorkerVersion( |
40 registration_, | 34 registration_, |
41 1L, context_->AsWeakPtr()); | 35 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()->ScopeHasProcessToRun(scope_); |
60 } | 58 } |
61 | 59 |
62 void SetActiveVersion( | 60 void SetActiveVersion( |
63 base::WeakPtr<ServiceWorkerProviderHost> provider_host, | 61 base::WeakPtr<ServiceWorkerProviderHost> provider_host, |
64 ServiceWorkerVersion* version) { | 62 ServiceWorkerVersion* version) { |
65 provider_host->SetActiveVersion(version); | 63 provider_host->SetActiveVersion(version); |
66 } | 64 } |
67 | 65 |
68 void SetWaitingVersion( | 66 void SetWaitingVersion( |
69 base::WeakPtr<ServiceWorkerProviderHost> provider_host, | 67 base::WeakPtr<ServiceWorkerProviderHost> provider_host, |
70 ServiceWorkerVersion* version) { | 68 ServiceWorkerVersion* version) { |
71 provider_host->SetWaitingVersion(version); | 69 provider_host->SetWaitingVersion(version); |
72 } | 70 } |
73 | 71 |
74 content::TestBrowserThreadBundle thread_bundle_; | 72 content::TestBrowserThreadBundle thread_bundle_; |
75 scoped_ptr<ServiceWorkerContextCore> context_; | 73 scoped_ptr<EmbeddedWorkerTestHelper> helper_; |
| 74 ServiceWorkerContextCore* context_; |
76 scoped_refptr<ServiceWorkerRegistration> registration_; | 75 scoped_refptr<ServiceWorkerRegistration> registration_; |
77 scoped_refptr<ServiceWorkerVersion> version_; | 76 scoped_refptr<ServiceWorkerVersion> version_; |
78 base::WeakPtr<ServiceWorkerProviderHost> provider_host1_; | 77 base::WeakPtr<ServiceWorkerProviderHost> provider_host1_; |
79 base::WeakPtr<ServiceWorkerProviderHost> provider_host2_; | 78 base::WeakPtr<ServiceWorkerProviderHost> provider_host2_; |
80 GURL scope_; | 79 GURL scope_; |
81 GURL script_url_; | 80 GURL script_url_; |
82 | 81 |
83 private: | 82 private: |
84 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest); | 83 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest); |
85 }; | 84 }; |
86 | 85 |
87 TEST_F(ServiceWorkerProviderHostTest, SetActiveVersion_ProcessStatus) { | 86 TEST_F(ServiceWorkerProviderHostTest, SetActiveVersion_ProcessStatus) { |
88 ASSERT_FALSE(version_->HasProcessToRun()); | 87 ASSERT_FALSE(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 SetActiveVersion(provider_host1_, version_); | 91 SetActiveVersion(provider_host1_, version_); |
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 SetActiveVersion(provider_host1_, version_); | 95 SetActiveVersion(provider_host1_, version_); |
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 SetActiveVersion(provider_host1_, NULL); | 100 SetActiveVersion(provider_host1_, NULL); |
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 ASSERT_FALSE(version_->HasProcessToRun()); | 106 ASSERT_FALSE(HasProcessToRun()); |
108 | 107 |
109 // Associating version_ to two providers as active version. | 108 // Associating version_ to two providers as active version. |
110 SetActiveVersion(provider_host1_, version_); | 109 SetActiveVersion(provider_host1_, version_); |
111 SetActiveVersion(provider_host2_, version_); | 110 SetActiveVersion(provider_host2_, version_); |
112 ASSERT_TRUE(version_->HasProcessToRun()); | 111 ASSERT_TRUE(HasProcessToRun()); |
113 | 112 |
114 // Disassociating one provider_host shouldn't remove all process refs | 113 // Disassociating one provider_host shouldn't remove all process refs |
115 // from the version yet. | 114 // from the version yet. |
116 SetActiveVersion(provider_host1_, NULL); | 115 SetActiveVersion(provider_host1_, NULL); |
117 ASSERT_TRUE(version_->HasProcessToRun()); | 116 ASSERT_TRUE(HasProcessToRun()); |
118 | 117 |
119 // Disassociating the other provider_host will remove all process refs. | 118 // Disassociating the other provider_host will remove all process refs. |
120 SetActiveVersion(provider_host2_, NULL); | 119 SetActiveVersion(provider_host2_, NULL); |
121 ASSERT_FALSE(version_->HasProcessToRun()); | 120 ASSERT_FALSE(HasProcessToRun()); |
122 } | 121 } |
123 | 122 |
124 TEST_F(ServiceWorkerProviderHostTest, SetWaitingVersion_ProcessStatus) { | 123 TEST_F(ServiceWorkerProviderHostTest, SetWaitingVersion_ProcessStatus) { |
125 ASSERT_FALSE(version_->HasProcessToRun()); | 124 ASSERT_FALSE(HasProcessToRun()); |
126 | 125 |
127 // Associating version_ to a provider_host's waiting version will internally | 126 // Associating version_ to a provider_host's waiting version will internally |
128 // add the provider_host's process ref to the version. | 127 // add the provider_host's process ref to the version. |
129 SetWaitingVersion(provider_host1_, version_); | 128 SetWaitingVersion(provider_host1_, version_); |
130 ASSERT_TRUE(version_->HasProcessToRun()); | 129 ASSERT_TRUE(HasProcessToRun()); |
131 | 130 |
132 // Re-associating the same version and provider_host should just work too. | 131 // Re-associating the same version and provider_host should just work too. |
133 SetWaitingVersion(provider_host1_, version_); | 132 SetWaitingVersion(provider_host1_, version_); |
134 ASSERT_TRUE(version_->HasProcessToRun()); | 133 ASSERT_TRUE(HasProcessToRun()); |
135 | 134 |
136 // Resetting the provider_host's waiting version should remove process refs | 135 // Resetting the provider_host's waiting version should remove process refs |
137 // from the version. | 136 // from the version. |
138 SetWaitingVersion(provider_host1_, NULL); | 137 SetWaitingVersion(provider_host1_, NULL); |
139 ASSERT_FALSE(version_->HasProcessToRun()); | 138 ASSERT_FALSE(HasProcessToRun()); |
140 } | 139 } |
141 | 140 |
142 TEST_F(ServiceWorkerProviderHostTest, | 141 TEST_F(ServiceWorkerProviderHostTest, |
143 SetWaitingVersion_MultipleHostsForSameProcess) { | 142 SetWaitingVersion_MultipleHostsForSameProcess) { |
144 ASSERT_FALSE(version_->HasProcessToRun()); | 143 ASSERT_FALSE(HasProcessToRun()); |
145 | 144 |
146 // Associating version_ to two providers as active version. | 145 // Associating version_ to two providers as active version. |
147 SetWaitingVersion(provider_host1_, version_); | 146 SetWaitingVersion(provider_host1_, version_); |
148 SetWaitingVersion(provider_host2_, version_); | 147 SetWaitingVersion(provider_host2_, version_); |
149 ASSERT_TRUE(version_->HasProcessToRun()); | 148 ASSERT_TRUE(HasProcessToRun()); |
150 | 149 |
151 // Disassociating one provider_host shouldn't remove all process refs | 150 // Disassociating one provider_host shouldn't remove all process refs |
152 // from the version yet. | 151 // from the version yet. |
153 SetWaitingVersion(provider_host1_, NULL); | 152 SetWaitingVersion(provider_host1_, NULL); |
154 ASSERT_TRUE(version_->HasProcessToRun()); | 153 ASSERT_TRUE(HasProcessToRun()); |
155 | 154 |
156 // Disassociating the other provider_host will remove all process refs. | 155 // Disassociating the other provider_host will remove all process refs. |
157 SetWaitingVersion(provider_host2_, NULL); | 156 SetWaitingVersion(provider_host2_, NULL); |
158 ASSERT_FALSE(version_->HasProcessToRun()); | 157 ASSERT_FALSE(HasProcessToRun()); |
159 } | 158 } |
160 | 159 |
161 } // namespace content | 160 } // namespace content |
OLD | NEW |