| OLD | NEW | 
|---|
| (Empty) |  | 
|  | 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. | 
|  | 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 "content/public/test/test_browser_thread_bundle.h" | 
|  | 14 #include "content/public/test/test_utils.h" | 
|  | 15 #include "testing/gtest/include/gtest/gtest.h" | 
|  | 16 | 
|  | 17 namespace content { | 
|  | 18 | 
|  | 19 namespace { | 
|  | 20 | 
|  | 21 void SaveResponseCallback(bool* called, | 
|  | 22                           int64* store_result, | 
|  | 23                           ServiceWorkerRegistrationStatus status, | 
|  | 24                           int64 result) { | 
|  | 25   *called = true; | 
|  | 26   *store_result = result; | 
|  | 27 } | 
|  | 28 | 
|  | 29 ServiceWorkerContextCore::RegistrationCallback MakeRegisteredCallback( | 
|  | 30     bool* called, | 
|  | 31     int64* store_result) { | 
|  | 32   return base::Bind(&SaveResponseCallback, called, store_result); | 
|  | 33 } | 
|  | 34 | 
|  | 35 void CallCompletedCallback(bool* called, ServiceWorkerRegistrationStatus) { | 
|  | 36   *called = true; | 
|  | 37 } | 
|  | 38 | 
|  | 39 ServiceWorkerContextCore::UnregistrationCallback MakeUnregisteredCallback( | 
|  | 40     bool* called) { | 
|  | 41   return base::Bind(&CallCompletedCallback, called); | 
|  | 42 } | 
|  | 43 | 
|  | 44 }  // namespace | 
|  | 45 | 
|  | 46 class ServiceWorkerContextTest : public testing::Test { | 
|  | 47  public: | 
|  | 48   ServiceWorkerContextTest() | 
|  | 49       : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} | 
|  | 50 | 
|  | 51   virtual void SetUp() OVERRIDE { | 
|  | 52     context_.reset(new ServiceWorkerContextCore(base::FilePath(), NULL)); | 
|  | 53   } | 
|  | 54 | 
|  | 55   virtual void TearDown() OVERRIDE { context_.reset(); } | 
|  | 56 | 
|  | 57  protected: | 
|  | 58   TestBrowserThreadBundle browser_thread_bundle_; | 
|  | 59   scoped_ptr<ServiceWorkerContextCore> context_; | 
|  | 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   base::RunLoop().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   base::RunLoop().RunUntilIdle(); | 
|  | 97   ASSERT_TRUE(called); | 
|  | 98 | 
|  | 99   called = false; | 
|  | 100   context_->UnregisterServiceWorker(pattern, MakeUnregisteredCallback(&called)); | 
|  | 101 | 
|  | 102   ASSERT_FALSE(called); | 
|  | 103   base::RunLoop().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   base::RunLoop().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   base::RunLoop().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   base::RunLoop().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   base::RunLoop().RunUntilIdle(); | 
|  | 163   ASSERT_TRUE(called); | 
|  | 164 | 
|  | 165   ASSERT_EQ(old_registration_id, new_registration_id); | 
|  | 166 } | 
|  | 167 | 
|  | 168 }  // namespace content | 
| OLD | NEW | 
|---|