Simplify ChildProcessLauncher
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_registration_unittest.cc
blob1965496585c3e21f3ced40beced24f8f2290ec50
1 // Copyright 2013 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 "content/browser/service_worker/service_worker_registration.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "content/browser/browser_thread_impl.h"
13 #include "content/browser/service_worker/service_worker_context_core.h"
14 #include "content/browser/service_worker/service_worker_registration_handle.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "url/gurl.h"
18 namespace content {
20 class ServiceWorkerRegistrationTest : public testing::Test {
21 public:
22 ServiceWorkerRegistrationTest()
23 : io_thread_(BrowserThread::IO, &message_loop_) {}
25 void SetUp() override {
26 scoped_ptr<ServiceWorkerDatabaseTaskManager> database_task_manager(
27 new MockServiceWorkerDatabaseTaskManager(
28 base::ThreadTaskRunnerHandle::Get()));
29 context_.reset(
30 new ServiceWorkerContextCore(base::FilePath(),
31 database_task_manager.Pass(),
32 base::ThreadTaskRunnerHandle::Get(),
33 NULL,
34 NULL,
35 NULL,
36 NULL));
37 context_ptr_ = context_->AsWeakPtr();
40 void TearDown() override {
41 context_.reset();
42 base::RunLoop().RunUntilIdle();
45 class RegistrationListener : public ServiceWorkerRegistration::Listener {
46 public:
47 RegistrationListener() {}
48 ~RegistrationListener() {
49 if (observed_registration_.get())
50 observed_registration_->RemoveListener(this);
53 void OnVersionAttributesChanged(
54 ServiceWorkerRegistration* registration,
55 ChangedVersionAttributesMask changed_mask,
56 const ServiceWorkerRegistrationInfo& info) override {
57 observed_registration_ = registration;
58 observed_changed_mask_ = changed_mask;
59 observed_info_ = info;
62 void OnRegistrationFailed(
63 ServiceWorkerRegistration* registration) override {
64 NOTREACHED();
67 void OnUpdateFound(ServiceWorkerRegistration* registration) override {
68 NOTREACHED();
71 void Reset() {
72 observed_registration_ = NULL;
73 observed_changed_mask_ = ChangedVersionAttributesMask();
74 observed_info_ = ServiceWorkerRegistrationInfo();
77 scoped_refptr<ServiceWorkerRegistration> observed_registration_;
78 ChangedVersionAttributesMask observed_changed_mask_;
79 ServiceWorkerRegistrationInfo observed_info_;
82 protected:
83 scoped_ptr<ServiceWorkerContextCore> context_;
84 base::WeakPtr<ServiceWorkerContextCore> context_ptr_;
85 base::MessageLoopForIO message_loop_;
86 BrowserThreadImpl io_thread_;
89 TEST_F(ServiceWorkerRegistrationTest, SetAndUnsetVersions) {
90 const GURL kScope("http://www.example.not/");
91 const GURL kScript("http://www.example.not/service_worker.js");
92 int64 kRegistrationId = 1L;
93 scoped_refptr<ServiceWorkerRegistration> registration =
94 new ServiceWorkerRegistration(
95 kScope,
96 kRegistrationId,
97 context_ptr_);
99 const int64 version_1_id = 1L;
100 const int64 version_2_id = 2L;
101 scoped_refptr<ServiceWorkerVersion> version_1 = new ServiceWorkerVersion(
102 registration.get(), kScript, version_1_id, context_ptr_);
103 scoped_refptr<ServiceWorkerVersion> version_2 = new ServiceWorkerVersion(
104 registration.get(), kScript, version_2_id, context_ptr_);
106 RegistrationListener listener;
107 registration->AddListener(&listener);
108 registration->SetActiveVersion(version_1.get());
110 EXPECT_EQ(version_1.get(), registration->active_version());
111 EXPECT_EQ(registration, listener.observed_registration_);
112 EXPECT_EQ(ChangedVersionAttributesMask::ACTIVE_VERSION,
113 listener.observed_changed_mask_.changed());
114 EXPECT_EQ(kScope, listener.observed_info_.pattern);
115 EXPECT_EQ(version_1_id, listener.observed_info_.active_version.version_id);
116 EXPECT_EQ(kScript, listener.observed_info_.active_version.script_url);
117 EXPECT_EQ(listener.observed_info_.installing_version.version_id,
118 kInvalidServiceWorkerVersionId);
119 EXPECT_EQ(listener.observed_info_.waiting_version.version_id,
120 kInvalidServiceWorkerVersionId);
121 listener.Reset();
123 registration->SetInstallingVersion(version_2.get());
125 EXPECT_EQ(version_2.get(), registration->installing_version());
126 EXPECT_EQ(ChangedVersionAttributesMask::INSTALLING_VERSION,
127 listener.observed_changed_mask_.changed());
128 EXPECT_EQ(version_1_id, listener.observed_info_.active_version.version_id);
129 EXPECT_EQ(version_2_id,
130 listener.observed_info_.installing_version.version_id);
131 EXPECT_EQ(listener.observed_info_.waiting_version.version_id,
132 kInvalidServiceWorkerVersionId);
133 listener.Reset();
135 registration->SetWaitingVersion(version_2.get());
137 EXPECT_EQ(version_2.get(), registration->waiting_version());
138 EXPECT_FALSE(registration->installing_version());
139 EXPECT_TRUE(listener.observed_changed_mask_.waiting_changed());
140 EXPECT_TRUE(listener.observed_changed_mask_.installing_changed());
141 EXPECT_EQ(version_1_id, listener.observed_info_.active_version.version_id);
142 EXPECT_EQ(version_2_id, listener.observed_info_.waiting_version.version_id);
143 EXPECT_EQ(listener.observed_info_.installing_version.version_id,
144 kInvalidServiceWorkerVersionId);
145 listener.Reset();
147 registration->UnsetVersion(version_2.get());
149 EXPECT_FALSE(registration->waiting_version());
150 EXPECT_EQ(ChangedVersionAttributesMask::WAITING_VERSION,
151 listener.observed_changed_mask_.changed());
152 EXPECT_EQ(version_1_id, listener.observed_info_.active_version.version_id);
153 EXPECT_EQ(listener.observed_info_.waiting_version.version_id,
154 kInvalidServiceWorkerVersionId);
155 EXPECT_EQ(listener.observed_info_.installing_version.version_id,
156 kInvalidServiceWorkerVersionId);
159 TEST_F(ServiceWorkerRegistrationTest, FailedRegistrationNoCrash) {
160 const GURL kScope("http://www.example.not/");
161 int64 kRegistrationId = 1L;
162 scoped_refptr<ServiceWorkerRegistration> registration =
163 new ServiceWorkerRegistration(
164 kScope,
165 kRegistrationId,
166 context_ptr_);
167 scoped_ptr<ServiceWorkerRegistrationHandle> handle(
168 new ServiceWorkerRegistrationHandle(
169 context_ptr_,
170 base::WeakPtr<ServiceWorkerProviderHost>(),
171 registration.get()));
172 registration->NotifyRegistrationFailed();
173 // Don't crash when handle gets destructed.
176 } // namespace content