| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012 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. |
| 4 |
| 5 #include "content/browser/service_worker/service_worker_context.h" |
| 6 |
| 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/service_worker_context_core.h" |
| 12 #include "content/browser/service_worker/service_worker_registration.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace content { |
| 16 |
| 17 class ServiceWorkerContextTest : public testing::Test { |
| 18 public: |
| 19 ServiceWorkerContextTest() : io_thread_(BrowserThread::IO, &message_loop_) {} |
| 20 |
| 21 virtual void SetUp() OVERRIDE { |
| 22 context_.reset(new ServiceWorkerContextCore(base::FilePath(), NULL)); |
| 23 } |
| 24 |
| 25 virtual void TearDown() OVERRIDE { |
| 26 context_->Shutdown(); |
| 27 context_.reset(); |
| 28 } |
| 29 |
| 30 static ServiceWorkerContextCore::RegistrationCallback MakeRegisteredCallback( |
| 31 bool* called, |
| 32 int64* store_result) { |
| 33 return base::Bind(&SaveResponseCallback, called, store_result); |
| 34 } |
| 35 |
| 36 static ServiceWorkerContextCore::UnregistrationCallback |
| 37 MakeUnregisteredCallback(bool* called) { |
| 38 return base::Bind(&CallCompletedCallback, called); |
| 39 } |
| 40 |
| 41 protected: |
| 42 scoped_ptr<ServiceWorkerContextCore> context_; |
| 43 |
| 44 static void SaveResponseCallback(bool* called, |
| 45 int64* store_result, |
| 46 ServiceWorkerContextCore::RegistrationStatus, |
| 47 int64 result) { |
| 48 *called = true; |
| 49 *store_result = result; |
| 50 } |
| 51 |
| 52 static void CallCompletedCallback( |
| 53 bool* called, |
| 54 ServiceWorkerContextCore::UnregistrationStatus) { |
| 55 *called = true; |
| 56 } |
| 57 |
| 58 base::MessageLoopForIO message_loop_; |
| 59 BrowserThreadImpl io_thread_; |
| 60 }; |
| 61 |
| 62 void RegistrationCallback( |
| 63 scoped_refptr<ServiceWorkerRegistration>* registration, |
| 64 const scoped_refptr<ServiceWorkerRegistration>& result) { |
| 65 *registration = result; |
| 66 } |
| 67 |
| 68 // Make sure basic registration is working. |
| 69 TEST_F(ServiceWorkerContextTest, Register) { |
| 70 int64 registration_id = -1L; |
| 71 bool called = false; |
| 72 context_->RegisterServiceWorker( |
| 73 GURL("http://www.example.com/*"), |
| 74 GURL("http://www.example.com/service_worker.js"), |
| 75 MakeRegisteredCallback(&called, ®istration_id)); |
| 76 |
| 77 ASSERT_FALSE(called); |
| 78 message_loop_.RunUntilIdle(); |
| 79 ASSERT_TRUE(called); |
| 80 |
| 81 ASSERT_NE(-1L, registration_id); |
| 82 } |
| 83 |
| 84 // Make sure registrations are cleaned up when they are unregistered. |
| 85 TEST_F(ServiceWorkerContextTest, Unregister) { |
| 86 GURL pattern("http://www.example.com/*"); |
| 87 |
| 88 bool called = false; |
| 89 int64 registration_id = -1L; |
| 90 context_->RegisterServiceWorker( |
| 91 pattern, |
| 92 GURL("http://www.example.com/service_worker.js"), |
| 93 MakeRegisteredCallback(&called, ®istration_id)); |
| 94 |
| 95 ASSERT_FALSE(called); |
| 96 message_loop_.RunUntilIdle(); |
| 97 ASSERT_TRUE(called); |
| 98 |
| 99 called = false; |
| 100 context_->UnregisterServiceWorker(pattern, MakeUnregisteredCallback(&called)); |
| 101 |
| 102 ASSERT_FALSE(called); |
| 103 message_loop_.RunUntilIdle(); |
| 104 ASSERT_TRUE(called); |
| 105 } |
| 106 |
| 107 // Make sure that when a new registration replaces an existing |
| 108 // registration, that the old one is cleaned up. |
| 109 TEST_F(ServiceWorkerContextTest, RegisterNewScript) { |
| 110 GURL pattern("http://www.example.com/*"); |
| 111 |
| 112 bool called = false; |
| 113 int64 old_registration_id = -1L; |
| 114 context_->RegisterServiceWorker( |
| 115 pattern, |
| 116 GURL("http://www.example.com/service_worker.js"), |
| 117 MakeRegisteredCallback(&called, &old_registration_id)); |
| 118 |
| 119 ASSERT_FALSE(called); |
| 120 message_loop_.RunUntilIdle(); |
| 121 ASSERT_TRUE(called); |
| 122 |
| 123 called = false; |
| 124 int64 new_registration_id = -1L; |
| 125 context_->RegisterServiceWorker( |
| 126 pattern, |
| 127 GURL("http://www.example.com/service_worker_new.js"), |
| 128 MakeRegisteredCallback(&called, &new_registration_id)); |
| 129 |
| 130 ASSERT_FALSE(called); |
| 131 message_loop_.RunUntilIdle(); |
| 132 ASSERT_TRUE(called); |
| 133 |
| 134 ASSERT_NE(old_registration_id, new_registration_id); |
| 135 } |
| 136 |
| 137 // Make sure that when registering a duplicate pattern+script_url |
| 138 // combination, that the same registration is used. |
| 139 TEST_F(ServiceWorkerContextTest, RegisterDuplicateScript) { |
| 140 GURL pattern("http://www.example.com/*"); |
| 141 GURL script_url("http://www.example.com/service_worker.js"); |
| 142 |
| 143 bool called = false; |
| 144 int64 old_registration_id = -1L; |
| 145 context_->RegisterServiceWorker( |
| 146 pattern, |
| 147 script_url, |
| 148 MakeRegisteredCallback(&called, &old_registration_id)); |
| 149 |
| 150 ASSERT_FALSE(called); |
| 151 message_loop_.RunUntilIdle(); |
| 152 ASSERT_TRUE(called); |
| 153 |
| 154 called = false; |
| 155 int64 new_registration_id = -1L; |
| 156 context_->RegisterServiceWorker( |
| 157 pattern, |
| 158 script_url, |
| 159 MakeRegisteredCallback(&called, &new_registration_id)); |
| 160 |
| 161 ASSERT_FALSE(called); |
| 162 message_loop_.RunUntilIdle(); |
| 163 ASSERT_TRUE(called); |
| 164 |
| 165 ASSERT_EQ(old_registration_id, new_registration_id); |
| 166 } |
| 167 |
| 168 } // namespace content |
| OLD | NEW |