ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_registration_unittest.cc
blobf228ed84505b9b1c25d02cca21b1604b8ffab5f2
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 base::ThreadTaskRunnerHandle::Get(),
32 database_task_manager.Pass(),
33 base::ThreadTaskRunnerHandle::Get(),
34 NULL,
35 NULL,
36 NULL,
37 NULL));
38 context_ptr_ = context_->AsWeakPtr();
41 void TearDown() override {
42 context_.reset();
43 base::RunLoop().RunUntilIdle();
46 class RegistrationListener : public ServiceWorkerRegistration::Listener {
47 public:
48 RegistrationListener() {}
49 ~RegistrationListener() {
50 if (observed_registration_.get())
51 observed_registration_->RemoveListener(this);
54 void OnVersionAttributesChanged(
55 ServiceWorkerRegistration* registration,
56 ChangedVersionAttributesMask changed_mask,
57 const ServiceWorkerRegistrationInfo& info) override {
58 observed_registration_ = registration;
59 observed_changed_mask_ = changed_mask;
60 observed_info_ = info;
63 void OnRegistrationFailed(
64 ServiceWorkerRegistration* registration) override {
65 NOTREACHED();
68 void OnUpdateFound(ServiceWorkerRegistration* registration) override {
69 NOTREACHED();
72 void Reset() {
73 observed_registration_ = NULL;
74 observed_changed_mask_ = ChangedVersionAttributesMask();
75 observed_info_ = ServiceWorkerRegistrationInfo();
78 scoped_refptr<ServiceWorkerRegistration> observed_registration_;
79 ChangedVersionAttributesMask observed_changed_mask_;
80 ServiceWorkerRegistrationInfo observed_info_;
83 protected:
84 scoped_ptr<ServiceWorkerContextCore> context_;
85 base::WeakPtr<ServiceWorkerContextCore> context_ptr_;
86 base::MessageLoopForIO message_loop_;
87 BrowserThreadImpl io_thread_;
90 TEST_F(ServiceWorkerRegistrationTest, SetAndUnsetVersions) {
91 const GURL kScope("http://www.example.not/");
92 const GURL kScript("http://www.example.not/service_worker.js");
93 int64 kRegistrationId = 1L;
94 scoped_refptr<ServiceWorkerRegistration> registration =
95 new ServiceWorkerRegistration(
96 kScope,
97 kRegistrationId,
98 context_ptr_);
100 const int64 version_1_id = 1L;
101 const int64 version_2_id = 2L;
102 scoped_refptr<ServiceWorkerVersion> version_1 = new ServiceWorkerVersion(
103 registration.get(), kScript, version_1_id, context_ptr_);
104 scoped_refptr<ServiceWorkerVersion> version_2 = new ServiceWorkerVersion(
105 registration.get(), kScript, version_2_id, context_ptr_);
107 RegistrationListener listener;
108 registration->AddListener(&listener);
109 registration->SetActiveVersion(version_1.get());
111 EXPECT_EQ(version_1.get(), registration->active_version());
112 EXPECT_EQ(registration, listener.observed_registration_);
113 EXPECT_EQ(ChangedVersionAttributesMask::ACTIVE_VERSION,
114 listener.observed_changed_mask_.changed());
115 EXPECT_EQ(kScope, listener.observed_info_.pattern);
116 EXPECT_EQ(version_1_id, listener.observed_info_.active_version.version_id);
117 EXPECT_EQ(kScript, listener.observed_info_.active_version.script_url);
118 EXPECT_EQ(listener.observed_info_.installing_version.version_id,
119 kInvalidServiceWorkerVersionId);
120 EXPECT_EQ(listener.observed_info_.waiting_version.version_id,
121 kInvalidServiceWorkerVersionId);
122 listener.Reset();
124 registration->SetInstallingVersion(version_2.get());
126 EXPECT_EQ(version_2.get(), registration->installing_version());
127 EXPECT_EQ(ChangedVersionAttributesMask::INSTALLING_VERSION,
128 listener.observed_changed_mask_.changed());
129 EXPECT_EQ(version_1_id, listener.observed_info_.active_version.version_id);
130 EXPECT_EQ(version_2_id,
131 listener.observed_info_.installing_version.version_id);
132 EXPECT_EQ(listener.observed_info_.waiting_version.version_id,
133 kInvalidServiceWorkerVersionId);
134 listener.Reset();
136 registration->SetWaitingVersion(version_2.get());
138 EXPECT_EQ(version_2.get(), registration->waiting_version());
139 EXPECT_FALSE(registration->installing_version());
140 EXPECT_TRUE(listener.observed_changed_mask_.waiting_changed());
141 EXPECT_TRUE(listener.observed_changed_mask_.installing_changed());
142 EXPECT_EQ(version_1_id, listener.observed_info_.active_version.version_id);
143 EXPECT_EQ(version_2_id, listener.observed_info_.waiting_version.version_id);
144 EXPECT_EQ(listener.observed_info_.installing_version.version_id,
145 kInvalidServiceWorkerVersionId);
146 listener.Reset();
148 registration->UnsetVersion(version_2.get());
150 EXPECT_FALSE(registration->waiting_version());
151 EXPECT_EQ(ChangedVersionAttributesMask::WAITING_VERSION,
152 listener.observed_changed_mask_.changed());
153 EXPECT_EQ(version_1_id, listener.observed_info_.active_version.version_id);
154 EXPECT_EQ(listener.observed_info_.waiting_version.version_id,
155 kInvalidServiceWorkerVersionId);
156 EXPECT_EQ(listener.observed_info_.installing_version.version_id,
157 kInvalidServiceWorkerVersionId);
160 TEST_F(ServiceWorkerRegistrationTest, FailedRegistrationNoCrash) {
161 const GURL kScope("http://www.example.not/");
162 int64 kRegistrationId = 1L;
163 scoped_refptr<ServiceWorkerRegistration> registration =
164 new ServiceWorkerRegistration(
165 kScope,
166 kRegistrationId,
167 context_ptr_);
168 scoped_ptr<ServiceWorkerRegistrationHandle> handle(
169 new ServiceWorkerRegistrationHandle(
170 context_ptr_,
171 base::WeakPtr<ServiceWorkerProviderHost>(),
172 registration.get()));
173 registration->NotifyRegistrationFailed();
174 // Don't crash when handle gets destructed.
177 } // namespace content