Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_provider_host_unittest.cc
blobfac7cb734abad27d3f45df220478a1ede73b3b9a
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.
5 #include "base/basictypes.h"
6 #include "base/memory/weak_ptr.h"
7 #include "base/thread_task_runner_handle.h"
8 #include "content/browser/service_worker/embedded_worker_test_helper.h"
9 #include "content/browser/service_worker/service_worker_context_core.h"
10 #include "content/browser/service_worker/service_worker_provider_host.h"
11 #include "content/browser/service_worker/service_worker_register_job.h"
12 #include "content/browser/service_worker/service_worker_registration.h"
13 #include "content/browser/service_worker/service_worker_version.h"
14 #include "content/public/test/test_browser_thread_bundle.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 namespace content {
19 static const int kRenderProcessId = 33; // Dummy process ID for testing.
21 class ServiceWorkerProviderHostTest : public testing::Test {
22 protected:
23 ServiceWorkerProviderHostTest()
24 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {}
25 ~ServiceWorkerProviderHostTest() override {}
27 void SetUp() override {
28 helper_.reset(new EmbeddedWorkerTestHelper(kRenderProcessId));
29 context_ = helper_->context();
30 pattern_ = GURL("http://www.example.com/");
31 script_url_ = GURL("http://www.example.com/service_worker.js");
32 registration_ = new ServiceWorkerRegistration(
33 pattern_, 1L, context_->AsWeakPtr());
34 version_ = new ServiceWorkerVersion(
35 registration_.get(), script_url_, 1L, context_->AsWeakPtr());
37 // Prepare provider hosts (for the same process).
38 scoped_ptr<ServiceWorkerProviderHost> host1(new ServiceWorkerProviderHost(
39 kRenderProcessId, 1 /* provider_id */,
40 context_->AsWeakPtr(), NULL));
41 scoped_ptr<ServiceWorkerProviderHost> host2(new ServiceWorkerProviderHost(
42 kRenderProcessId, 2 /* provider_id */,
43 context_->AsWeakPtr(), NULL));
44 provider_host1_ = host1->AsWeakPtr();
45 provider_host2_ = host2->AsWeakPtr();
46 context_->AddProviderHost(make_scoped_ptr(host1.release()));
47 context_->AddProviderHost(make_scoped_ptr(host2.release()));
50 void TearDown() override {
51 version_ = 0;
52 registration_ = 0;
53 helper_.reset();
56 bool HasProcessToRun() const {
57 return context_->process_manager()->PatternHasProcessToRun(pattern_);
60 content::TestBrowserThreadBundle thread_bundle_;
61 scoped_ptr<EmbeddedWorkerTestHelper> helper_;
62 ServiceWorkerContextCore* context_;
63 scoped_refptr<ServiceWorkerRegistration> registration_;
64 scoped_refptr<ServiceWorkerVersion> version_;
65 base::WeakPtr<ServiceWorkerProviderHost> provider_host1_;
66 base::WeakPtr<ServiceWorkerProviderHost> provider_host2_;
67 GURL pattern_;
68 GURL script_url_;
70 private:
71 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest);
74 TEST_F(ServiceWorkerProviderHostTest, SetActiveVersion_ProcessStatus) {
75 provider_host1_->AssociateRegistration(registration_.get());
76 ASSERT_TRUE(HasProcessToRun());
78 // Associating version_ to a provider_host's active version will internally
79 // add the provider_host's process ref to the version.
80 registration_->SetActiveVersion(version_.get());
81 ASSERT_TRUE(HasProcessToRun());
83 // Re-associating the same version and provider_host should just work too.
84 registration_->SetActiveVersion(version_.get());
85 ASSERT_TRUE(HasProcessToRun());
87 // Resetting the provider_host's active version should remove process refs
88 // from the version.
89 provider_host1_->DisassociateRegistration();
90 ASSERT_FALSE(HasProcessToRun());
93 TEST_F(ServiceWorkerProviderHostTest,
94 SetActiveVersion_MultipleHostsForSameProcess) {
95 provider_host1_->AssociateRegistration(registration_.get());
96 provider_host2_->AssociateRegistration(registration_.get());
97 ASSERT_TRUE(HasProcessToRun());
99 // Associating version_ to two providers as active version.
100 registration_->SetActiveVersion(version_.get());
101 ASSERT_TRUE(HasProcessToRun());
103 // Disassociating one provider_host shouldn't remove all process refs
104 // from the version yet.
105 provider_host1_->DisassociateRegistration();
106 ASSERT_TRUE(HasProcessToRun());
108 // Disassociating the other provider_host will remove all process refs.
109 provider_host2_->DisassociateRegistration();
110 ASSERT_FALSE(HasProcessToRun());
113 TEST_F(ServiceWorkerProviderHostTest, SetWaitingVersion_ProcessStatus) {
114 provider_host1_->AssociateRegistration(registration_.get());
115 ASSERT_TRUE(HasProcessToRun());
117 // Associating version_ to a provider_host's waiting version will internally
118 // add the provider_host's process ref to the version.
119 registration_->SetWaitingVersion(version_.get());
120 ASSERT_TRUE(HasProcessToRun());
122 // Re-associating the same version and provider_host should just work too.
123 registration_->SetWaitingVersion(version_.get());
124 ASSERT_TRUE(HasProcessToRun());
126 // Resetting the provider_host's waiting version should remove process refs
127 // from the version.
128 provider_host1_->DisassociateRegistration();
129 ASSERT_FALSE(HasProcessToRun());
132 TEST_F(ServiceWorkerProviderHostTest,
133 SetWaitingVersion_MultipleHostsForSameProcess) {
134 provider_host1_->AssociateRegistration(registration_.get());
135 provider_host2_->AssociateRegistration(registration_.get());
136 ASSERT_TRUE(HasProcessToRun());
138 // Associating version_ to two providers as waiting version.
139 registration_->SetWaitingVersion(version_.get());
140 ASSERT_TRUE(HasProcessToRun());
142 // Disassociating one provider_host shouldn't remove all process refs
143 // from the version yet.
144 provider_host1_->DisassociateRegistration();
145 ASSERT_TRUE(HasProcessToRun());
147 // Disassociating the other provider_host will remove all process refs.
148 provider_host2_->DisassociateRegistration();
149 ASSERT_FALSE(HasProcessToRun());
152 } // namespace content