Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(104)

Side by Side Diff: content/browser/service_worker/service_worker_provider_host_unittest.cc

Issue 2396273002: ServiceWorker: Mojofication of ServiceWorkerDispatcherHost (Closed)
Patch Set: Removed GetWeakPtr and moved a method to private Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "content/browser/service_worker/service_worker_provider_host.h" 5 #include "content/browser/service_worker/service_worker_provider_host.h"
6 6
7 #include "base/macros.h" 7 #include "base/macros.h"
8 #include "base/memory/ptr_util.h" 8 #include "base/memory/ptr_util.h"
9 #include "base/memory/weak_ptr.h" 9 #include "base/memory/weak_ptr.h"
10 #include "base/threading/thread_task_runner_handle.h" 10 #include "base/threading/thread_task_runner_handle.h"
11 #include "content/browser/service_worker/embedded_worker_test_helper.h" 11 #include "content/browser/service_worker/embedded_worker_test_helper.h"
12 #include "content/browser/service_worker/service_worker_context_core.h" 12 #include "content/browser/service_worker/service_worker_context_core.h"
13 #include "content/browser/service_worker/service_worker_register_job.h" 13 #include "content/browser/service_worker/service_worker_register_job.h"
14 #include "content/browser/service_worker/service_worker_registration.h" 14 #include "content/browser/service_worker/service_worker_registration.h"
15 #include "content/browser/service_worker/service_worker_test_utils.h"
15 #include "content/browser/service_worker/service_worker_version.h" 16 #include "content/browser/service_worker/service_worker_version.h"
16 #include "content/public/common/origin_util.h" 17 #include "content/public/common/origin_util.h"
17 #include "content/public/test/test_browser_thread_bundle.h" 18 #include "content/public/test/test_browser_thread_bundle.h"
18 #include "content/test/test_content_browser_client.h" 19 #include "content/test/test_content_browser_client.h"
19 #include "content/test/test_content_client.h" 20 #include "content/test/test_content_client.h"
20 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
21 22
22 namespace content { 23 namespace content {
23 24
24 const char kServiceWorkerScheme[] = "i-can-use-service-worker"; 25 const char kServiceWorkerScheme[] = "i-can-use-service-worker";
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 base::WeakPtr<ServiceWorkerProviderHost> provider_host2_; 95 base::WeakPtr<ServiceWorkerProviderHost> provider_host2_;
95 GURL script_url_; 96 GURL script_url_;
96 ServiceWorkerTestContentClient test_content_client_; 97 ServiceWorkerTestContentClient test_content_client_;
97 TestContentBrowserClient test_content_browser_client_; 98 TestContentBrowserClient test_content_browser_client_;
98 ContentBrowserClient* old_content_browser_client_; 99 ContentBrowserClient* old_content_browser_client_;
99 100
100 private: 101 private:
101 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest); 102 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest);
102 }; 103 };
103 104
104 TEST_F(ServiceWorkerProviderHostTest, PotentialRegistration_ProcessStatus) { 105 class ServiceWorkerProviderHostTestP
106 : public MojoServiceWorkerTestP<ServiceWorkerProviderHostTest> {};
107
108 TEST_P(ServiceWorkerProviderHostTestP, PotentialRegistration_ProcessStatus) {
105 // Matching registrations have already been set by SetDocumentUrl. 109 // Matching registrations have already been set by SetDocumentUrl.
106 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); 110 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern()));
107 111
108 // Different matching registrations have already been added. 112 // Different matching registrations have already been added.
109 ASSERT_TRUE(PatternHasProcessToRun(registration2_->pattern())); 113 ASSERT_TRUE(PatternHasProcessToRun(registration2_->pattern()));
110 114
111 // Adding the same registration twice has no effect. 115 // Adding the same registration twice has no effect.
112 provider_host1_->AddMatchingRegistration(registration1_.get()); 116 provider_host1_->AddMatchingRegistration(registration1_.get());
113 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); 117 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern()));
114 118
115 // Removing a matching registration will decrease the process refs for its 119 // Removing a matching registration will decrease the process refs for its
116 // pattern. 120 // pattern.
117 provider_host1_->RemoveMatchingRegistration(registration1_.get()); 121 provider_host1_->RemoveMatchingRegistration(registration1_.get());
118 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); 122 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern()));
119 provider_host2_->RemoveMatchingRegistration(registration1_.get()); 123 provider_host2_->RemoveMatchingRegistration(registration1_.get());
120 ASSERT_FALSE(PatternHasProcessToRun(registration1_->pattern())); 124 ASSERT_FALSE(PatternHasProcessToRun(registration1_->pattern()));
121 125
122 // Matching registration will be removed when moving out of scope 126 // Matching registration will be removed when moving out of scope
123 ASSERT_TRUE(PatternHasProcessToRun(registration2_->pattern())); // host1,2 127 ASSERT_TRUE(PatternHasProcessToRun(registration2_->pattern())); // host1,2
124 ASSERT_FALSE(PatternHasProcessToRun(registration3_->pattern())); // no host 128 ASSERT_FALSE(PatternHasProcessToRun(registration3_->pattern())); // no host
125 provider_host1_->SetDocumentUrl(GURL("https://other.example.com/")); 129 provider_host1_->SetDocumentUrl(GURL("https://other.example.com/"));
126 ASSERT_TRUE(PatternHasProcessToRun(registration2_->pattern())); // host2 130 ASSERT_TRUE(PatternHasProcessToRun(registration2_->pattern())); // host2
127 ASSERT_TRUE(PatternHasProcessToRun(registration3_->pattern())); // host1 131 ASSERT_TRUE(PatternHasProcessToRun(registration3_->pattern())); // host1
128 provider_host2_->SetDocumentUrl(GURL("https://other.example.com/")); 132 provider_host2_->SetDocumentUrl(GURL("https://other.example.com/"));
129 ASSERT_FALSE(PatternHasProcessToRun(registration2_->pattern())); // no host 133 ASSERT_FALSE(PatternHasProcessToRun(registration2_->pattern())); // no host
130 ASSERT_TRUE(PatternHasProcessToRun(registration3_->pattern())); // host1,2 134 ASSERT_TRUE(PatternHasProcessToRun(registration3_->pattern())); // host1,2
131 } 135 }
132 136
133 TEST_F(ServiceWorkerProviderHostTest, AssociatedRegistration_ProcessStatus) { 137 TEST_P(ServiceWorkerProviderHostTestP, AssociatedRegistration_ProcessStatus) {
134 // Associating the registration will also increase the process refs for 138 // Associating the registration will also increase the process refs for
135 // the registration's pattern. 139 // the registration's pattern.
136 provider_host1_->AssociateRegistration(registration1_.get(), 140 provider_host1_->AssociateRegistration(registration1_.get(),
137 false /* notify_controllerchange */); 141 false /* notify_controllerchange */);
138 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); 142 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern()));
139 143
140 // Disassociating the registration shouldn't affect the process refs for 144 // Disassociating the registration shouldn't affect the process refs for
141 // the registration's pattern. 145 // the registration's pattern.
142 provider_host1_->DisassociateRegistration(); 146 provider_host1_->DisassociateRegistration();
143 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); 147 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern()));
144 } 148 }
145 149
146 TEST_F(ServiceWorkerProviderHostTest, MatchRegistration) { 150 TEST_P(ServiceWorkerProviderHostTestP, MatchRegistration) {
147 // Match registration should return the longest matching one. 151 // Match registration should return the longest matching one.
148 ASSERT_EQ(registration2_, provider_host1_->MatchRegistration()); 152 ASSERT_EQ(registration2_, provider_host1_->MatchRegistration());
149 provider_host1_->RemoveMatchingRegistration(registration2_.get()); 153 provider_host1_->RemoveMatchingRegistration(registration2_.get());
150 ASSERT_EQ(registration1_, provider_host1_->MatchRegistration()); 154 ASSERT_EQ(registration1_, provider_host1_->MatchRegistration());
151 155
152 // Should return nullptr after removing all matching registrations. 156 // Should return nullptr after removing all matching registrations.
153 provider_host1_->RemoveMatchingRegistration(registration1_.get()); 157 provider_host1_->RemoveMatchingRegistration(registration1_.get());
154 ASSERT_EQ(nullptr, provider_host1_->MatchRegistration()); 158 ASSERT_EQ(nullptr, provider_host1_->MatchRegistration());
155 159
156 // SetDocumentUrl sets all of matching registrations 160 // SetDocumentUrl sets all of matching registrations
157 provider_host1_->SetDocumentUrl(GURL("https://www.example.com/example1")); 161 provider_host1_->SetDocumentUrl(GURL("https://www.example.com/example1"));
158 ASSERT_EQ(registration2_, provider_host1_->MatchRegistration()); 162 ASSERT_EQ(registration2_, provider_host1_->MatchRegistration());
159 provider_host1_->RemoveMatchingRegistration(registration2_.get()); 163 provider_host1_->RemoveMatchingRegistration(registration2_.get());
160 ASSERT_EQ(registration1_, provider_host1_->MatchRegistration()); 164 ASSERT_EQ(registration1_, provider_host1_->MatchRegistration());
161 165
162 // SetDocumentUrl with another origin also updates matching registrations 166 // SetDocumentUrl with another origin also updates matching registrations
163 provider_host1_->SetDocumentUrl(GURL("https://other.example.com/example")); 167 provider_host1_->SetDocumentUrl(GURL("https://other.example.com/example"));
164 ASSERT_EQ(registration3_, provider_host1_->MatchRegistration()); 168 ASSERT_EQ(registration3_, provider_host1_->MatchRegistration());
165 provider_host1_->RemoveMatchingRegistration(registration3_.get()); 169 provider_host1_->RemoveMatchingRegistration(registration3_.get());
166 ASSERT_EQ(nullptr, provider_host1_->MatchRegistration()); 170 ASSERT_EQ(nullptr, provider_host1_->MatchRegistration());
167 } 171 }
168 172
169 TEST_F(ServiceWorkerProviderHostTest, ContextSecurity) { 173 TEST_P(ServiceWorkerProviderHostTestP, ContextSecurity) {
170 using FrameSecurityLevel = ServiceWorkerProviderHost::FrameSecurityLevel; 174 using FrameSecurityLevel = ServiceWorkerProviderHost::FrameSecurityLevel;
171 content::ResetSchemesAndOriginsWhitelistForTesting(); 175 content::ResetSchemesAndOriginsWhitelistForTesting();
172 176
173 // Insecure document URL. 177 // Insecure document URL.
174 provider_host1_->SetDocumentUrl(GURL("http://host")); 178 provider_host1_->SetDocumentUrl(GURL("http://host"));
175 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::SECURE; 179 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::SECURE;
176 EXPECT_FALSE(provider_host1_->IsContextSecureForServiceWorker()); 180 EXPECT_FALSE(provider_host1_->IsContextSecureForServiceWorker());
177 181
178 // Insecure parent frame. 182 // Insecure parent frame.
179 provider_host1_->SetDocumentUrl(GURL("https://host")); 183 provider_host1_->SetDocumentUrl(GURL("https://host"));
(...skipping 12 matching lines...) Expand all
192 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::SECURE; 196 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::SECURE;
193 EXPECT_FALSE(IsOriginSecure(url)); 197 EXPECT_FALSE(IsOriginSecure(url));
194 EXPECT_TRUE(OriginCanAccessServiceWorkers(url)); 198 EXPECT_TRUE(OriginCanAccessServiceWorkers(url));
195 EXPECT_TRUE(provider_host1_->IsContextSecureForServiceWorker()); 199 EXPECT_TRUE(provider_host1_->IsContextSecureForServiceWorker());
196 200
197 // Exceptional service worker scheme with insecure parent frame. 201 // Exceptional service worker scheme with insecure parent frame.
198 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::INSECURE; 202 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::INSECURE;
199 EXPECT_FALSE(provider_host1_->IsContextSecureForServiceWorker()); 203 EXPECT_FALSE(provider_host1_->IsContextSecureForServiceWorker());
200 } 204 }
201 205
206 INSTANTIATE_TEST_CASE_P(ServiceWorkerProviderHostTest,
207 ServiceWorkerProviderHostTestP,
208 ::testing::Values(false, true));
209
202 } // namespace content 210 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698