OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/service_worker/service_worker_registration.h" | 5 #include "content/browser/service_worker/service_worker_registration.h" |
6 | 6 |
7 | |
8 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
9 #include "base/logging.h" | 8 #include "base/logging.h" |
10 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
11 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
12 #include "content/browser/browser_thread_impl.h" | 11 #include "content/browser/browser_thread_impl.h" |
13 #include "content/browser/service_worker/service_worker_context_core.h" | 12 #include "content/browser/service_worker/service_worker_context_core.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
15 #include "url/gurl.h" | 14 #include "url/gurl.h" |
16 | 15 |
17 namespace content { | 16 namespace content { |
(...skipping 12 matching lines...) Expand all Loading... |
30 NULL, | 29 NULL, |
31 NULL)); | 30 NULL)); |
32 context_ptr_ = context_->AsWeakPtr(); | 31 context_ptr_ = context_->AsWeakPtr(); |
33 } | 32 } |
34 | 33 |
35 virtual void TearDown() OVERRIDE { | 34 virtual void TearDown() OVERRIDE { |
36 context_.reset(); | 35 context_.reset(); |
37 base::RunLoop().RunUntilIdle(); | 36 base::RunLoop().RunUntilIdle(); |
38 } | 37 } |
39 | 38 |
| 39 class RegistrationListener : public ServiceWorkerRegistration::Listener { |
| 40 public: |
| 41 RegistrationListener() {} |
| 42 ~RegistrationListener() {} |
| 43 |
| 44 virtual void OnVersionAttributesChanged( |
| 45 ServiceWorkerRegistration* registration, |
| 46 ChangedVersionAttributesMask changed_mask, |
| 47 const ServiceWorkerRegistrationInfo& info) OVERRIDE { |
| 48 observed_registration_ = registration; |
| 49 observed_changed_mask_ = changed_mask; |
| 50 observed_info_ = info; |
| 51 } |
| 52 |
| 53 void Reset() { |
| 54 observed_registration_ = NULL; |
| 55 observed_changed_mask_ = ChangedVersionAttributesMask(); |
| 56 observed_info_ = ServiceWorkerRegistrationInfo(); |
| 57 } |
| 58 |
| 59 scoped_refptr<ServiceWorkerRegistration> observed_registration_; |
| 60 ChangedVersionAttributesMask observed_changed_mask_; |
| 61 ServiceWorkerRegistrationInfo observed_info_; |
| 62 }; |
| 63 |
40 protected: | 64 protected: |
41 scoped_ptr<ServiceWorkerContextCore> context_; | 65 scoped_ptr<ServiceWorkerContextCore> context_; |
42 base::WeakPtr<ServiceWorkerContextCore> context_ptr_; | 66 base::WeakPtr<ServiceWorkerContextCore> context_ptr_; |
43 base::MessageLoopForIO message_loop_; | 67 base::MessageLoopForIO message_loop_; |
44 BrowserThreadImpl io_thread_; | 68 BrowserThreadImpl io_thread_; |
45 }; | 69 }; |
46 | 70 |
| 71 TEST_F(ServiceWorkerRegistrationTest, SetAndUnsetVersions) { |
| 72 const GURL kScope("http://www.example.not/*"); |
| 73 const GURL kScript("http://www.example.not/service_worker.js"); |
| 74 int64 kRegistrationId = 1L; |
| 75 scoped_refptr<ServiceWorkerRegistration> registration = |
| 76 new ServiceWorkerRegistration( |
| 77 kScope, |
| 78 kScript, |
| 79 kRegistrationId, |
| 80 context_ptr_); |
| 81 |
| 82 const int64 version_1_id = 1L; |
| 83 const int64 version_2_id = 2L; |
| 84 scoped_refptr<ServiceWorkerVersion> version_1 = |
| 85 new ServiceWorkerVersion(registration, version_1_id, context_ptr_); |
| 86 scoped_refptr<ServiceWorkerVersion> version_2 = |
| 87 new ServiceWorkerVersion(registration, version_2_id, context_ptr_); |
| 88 |
| 89 RegistrationListener listener; |
| 90 registration->AddListener(&listener); |
| 91 registration->SetActiveVersion(version_1); |
| 92 |
| 93 EXPECT_EQ(version_1, registration->active_version()); |
| 94 EXPECT_EQ(registration, listener.observed_registration_); |
| 95 EXPECT_EQ(ChangedVersionAttributesMask::ACTIVE_VERSION, |
| 96 listener.observed_changed_mask_.changed()); |
| 97 EXPECT_EQ(kScope, listener.observed_info_.pattern); |
| 98 EXPECT_EQ(kScript, listener.observed_info_.script_url); |
| 99 EXPECT_EQ(version_1_id, listener.observed_info_.active_version.version_id); |
| 100 EXPECT_TRUE(listener.observed_info_.installing_version.is_null); |
| 101 EXPECT_TRUE(listener.observed_info_.waiting_version.is_null); |
| 102 EXPECT_TRUE(listener.observed_info_.controlling_version.is_null); |
| 103 listener.Reset(); |
| 104 |
| 105 registration->SetInstallingVersion(version_2); |
| 106 |
| 107 EXPECT_EQ(version_2, registration->installing_version()); |
| 108 EXPECT_EQ(ChangedVersionAttributesMask::INSTALLING_VERSION, |
| 109 listener.observed_changed_mask_.changed()); |
| 110 EXPECT_EQ(version_1_id, listener.observed_info_.active_version.version_id); |
| 111 EXPECT_EQ(version_2_id, |
| 112 listener.observed_info_.installing_version.version_id); |
| 113 EXPECT_TRUE(listener.observed_info_.waiting_version.is_null); |
| 114 EXPECT_TRUE(listener.observed_info_.controlling_version.is_null); |
| 115 listener.Reset(); |
| 116 |
| 117 registration->SetWaitingVersion(version_2); |
| 118 |
| 119 EXPECT_EQ(version_2, registration->waiting_version()); |
| 120 EXPECT_FALSE(registration->installing_version()); |
| 121 EXPECT_TRUE(listener.observed_changed_mask_.waiting_changed()); |
| 122 EXPECT_TRUE(listener.observed_changed_mask_.installing_changed()); |
| 123 EXPECT_EQ(version_1_id, listener.observed_info_.active_version.version_id); |
| 124 EXPECT_EQ(version_2_id, listener.observed_info_.waiting_version.version_id); |
| 125 EXPECT_TRUE(listener.observed_info_.installing_version.is_null); |
| 126 EXPECT_TRUE(listener.observed_info_.controlling_version.is_null); |
| 127 listener.Reset(); |
| 128 |
| 129 registration->UnsetVersion(version_2); |
| 130 |
| 131 EXPECT_FALSE(registration->waiting_version()); |
| 132 EXPECT_EQ(ChangedVersionAttributesMask::WAITING_VERSION, |
| 133 listener.observed_changed_mask_.changed()); |
| 134 EXPECT_EQ(version_1_id, listener.observed_info_.active_version.version_id); |
| 135 EXPECT_TRUE(listener.observed_info_.waiting_version.is_null); |
| 136 EXPECT_TRUE(listener.observed_info_.installing_version.is_null); |
| 137 EXPECT_TRUE(listener.observed_info_.controlling_version.is_null); |
| 138 } |
| 139 |
47 } // namespace content | 140 } // namespace content |
OLD | NEW |