Cast: Skip receiver log messages with time delta that can't be encoded.
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_context_unittest.cc
blobc4d8a642d2aa3b90075fbaea81fdad8f80e25b73
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_context.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "content/browser/browser_thread_impl.h"
11 #include "content/browser/service_worker/embedded_worker_registry.h"
12 #include "content/browser/service_worker/embedded_worker_test_helper.h"
13 #include "content/browser/service_worker/service_worker_context_core.h"
14 #include "content/browser/service_worker/service_worker_registration.h"
15 #include "content/public/test/test_browser_thread_bundle.h"
16 #include "content/public/test/test_utils.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 namespace content {
21 namespace {
23 void SaveResponseCallback(bool* called,
24 int64* store_result,
25 ServiceWorkerStatusCode status,
26 int64 result) {
27 *called = true;
28 *store_result = result;
31 ServiceWorkerContextCore::RegistrationCallback MakeRegisteredCallback(
32 bool* called,
33 int64* store_result) {
34 return base::Bind(&SaveResponseCallback, called, store_result);
37 void CallCompletedCallback(bool* called, ServiceWorkerStatusCode) {
38 *called = true;
41 ServiceWorkerContextCore::UnregistrationCallback MakeUnregisteredCallback(
42 bool* called) {
43 return base::Bind(&CallCompletedCallback, called);
46 } // namespace
48 class ServiceWorkerContextTest : public testing::Test {
49 public:
50 ServiceWorkerContextTest()
51 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP),
52 render_process_id_(99) {}
54 virtual void SetUp() OVERRIDE {
55 context_.reset(new ServiceWorkerContextCore(base::FilePath(), NULL));
56 helper_.reset(new EmbeddedWorkerTestHelper(
57 context_.get(), render_process_id_));
60 virtual void TearDown() OVERRIDE {
61 helper_.reset();
62 context_.reset();
65 protected:
66 TestBrowserThreadBundle browser_thread_bundle_;
67 scoped_ptr<ServiceWorkerContextCore> context_;
68 scoped_ptr<EmbeddedWorkerTestHelper> helper_;
69 const int render_process_id_;
72 void RegistrationCallback(
73 scoped_refptr<ServiceWorkerRegistration>* registration,
74 const scoped_refptr<ServiceWorkerRegistration>& result) {
75 *registration = result;
78 // Make sure basic registration is working.
79 TEST_F(ServiceWorkerContextTest, Register) {
80 int64 registration_id = -1L;
81 bool called = false;
82 context_->RegisterServiceWorker(
83 GURL("http://www.example.com/*"),
84 GURL("http://www.example.com/service_worker.js"),
85 render_process_id_,
86 MakeRegisteredCallback(&called, &registration_id));
88 ASSERT_FALSE(called);
89 base::RunLoop().RunUntilIdle();
90 EXPECT_TRUE(called);
92 EXPECT_EQ(1UL, helper_->ipc_sink()->message_count());
93 EXPECT_NE(-1L, registration_id);
96 // Make sure registrations are cleaned up when they are unregistered.
97 TEST_F(ServiceWorkerContextTest, Unregister) {
98 GURL pattern("http://www.example.com/*");
100 bool called = false;
101 int64 registration_id = -1L;
102 context_->RegisterServiceWorker(
103 pattern,
104 GURL("http://www.example.com/service_worker.js"),
105 render_process_id_,
106 MakeRegisteredCallback(&called, &registration_id));
108 ASSERT_FALSE(called);
109 base::RunLoop().RunUntilIdle();
110 ASSERT_TRUE(called);
112 called = false;
113 context_->UnregisterServiceWorker(
114 pattern, render_process_id_, MakeUnregisteredCallback(&called));
116 ASSERT_FALSE(called);
117 base::RunLoop().RunUntilIdle();
118 ASSERT_TRUE(called);
121 // Make sure that when a new registration replaces an existing
122 // registration, that the old one is cleaned up.
123 TEST_F(ServiceWorkerContextTest, RegisterNewScript) {
124 GURL pattern("http://www.example.com/*");
126 bool called = false;
127 int64 old_registration_id = -1L;
128 context_->RegisterServiceWorker(
129 pattern,
130 GURL("http://www.example.com/service_worker.js"),
131 render_process_id_,
132 MakeRegisteredCallback(&called, &old_registration_id));
134 ASSERT_FALSE(called);
135 base::RunLoop().RunUntilIdle();
136 ASSERT_TRUE(called);
138 called = false;
139 int64 new_registration_id = -1L;
140 context_->RegisterServiceWorker(
141 pattern,
142 GURL("http://www.example.com/service_worker_new.js"),
143 render_process_id_,
144 MakeRegisteredCallback(&called, &new_registration_id));
146 ASSERT_FALSE(called);
147 base::RunLoop().RunUntilIdle();
148 ASSERT_TRUE(called);
150 ASSERT_NE(old_registration_id, new_registration_id);
153 // Make sure that when registering a duplicate pattern+script_url
154 // combination, that the same registration is used.
155 TEST_F(ServiceWorkerContextTest, RegisterDuplicateScript) {
156 GURL pattern("http://www.example.com/*");
157 GURL script_url("http://www.example.com/service_worker.js");
159 bool called = false;
160 int64 old_registration_id = -1L;
161 context_->RegisterServiceWorker(
162 pattern,
163 script_url,
164 render_process_id_,
165 MakeRegisteredCallback(&called, &old_registration_id));
167 ASSERT_FALSE(called);
168 base::RunLoop().RunUntilIdle();
169 ASSERT_TRUE(called);
171 called = false;
172 int64 new_registration_id = -1L;
173 context_->RegisterServiceWorker(
174 pattern,
175 script_url,
176 render_process_id_,
177 MakeRegisteredCallback(&called, &new_registration_id));
179 ASSERT_FALSE(called);
180 base::RunLoop().RunUntilIdle();
181 ASSERT_TRUE(called);
183 ASSERT_EQ(old_registration_id, new_registration_id);
186 } // namespace content