Chromium Code Reviews| 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 | 7 |
| 8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 30 NULL, | 30 NULL, |
| 31 NULL)); | 31 NULL)); |
| 32 context_ptr_ = context_->AsWeakPtr(); | 32 context_ptr_ = context_->AsWeakPtr(); |
| 33 } | 33 } |
| 34 | 34 |
| 35 virtual void TearDown() OVERRIDE { | 35 virtual void TearDown() OVERRIDE { |
| 36 context_.reset(); | 36 context_.reset(); |
| 37 base::RunLoop().RunUntilIdle(); | 37 base::RunLoop().RunUntilIdle(); |
| 38 } | 38 } |
| 39 | 39 |
| 40 class RegistrationListener : public ServiceWorkerRegistration::Listener { | |
| 41 public: | |
| 42 RegistrationListener() {} | |
| 43 ~RegistrationListener() {} | |
| 44 | |
| 45 virtual void OnVersionAttributesChanged( | |
| 46 ServiceWorkerRegistration* registration, | |
| 47 ChangedVersionAttributesMask changed_mask, | |
| 48 const ServiceWorkerRegistrationInfo& info) { | |
|
nhiroki
2014/07/16 10:39:20
Can you mark this with 'OVERRIDE'?
michaeln
2014/07/16 19:45:21
Done.
| |
| 49 observed_registration_ = registration; | |
| 50 observed_changed_mask_ = changed_mask; | |
| 51 observed_info_ = info; | |
| 52 } | |
| 53 | |
| 54 void Reset() { | |
| 55 observed_registration_ = NULL; | |
| 56 observed_changed_mask_ = ChangedVersionAttributesMask(); | |
| 57 observed_info_ = ServiceWorkerRegistrationInfo(); | |
| 58 } | |
| 59 | |
| 60 scoped_refptr<ServiceWorkerRegistration> observed_registration_; | |
| 61 ChangedVersionAttributesMask observed_changed_mask_; | |
| 62 ServiceWorkerRegistrationInfo observed_info_; | |
| 63 }; | |
| 64 | |
| 40 protected: | 65 protected: |
| 41 scoped_ptr<ServiceWorkerContextCore> context_; | 66 scoped_ptr<ServiceWorkerContextCore> context_; |
| 42 base::WeakPtr<ServiceWorkerContextCore> context_ptr_; | 67 base::WeakPtr<ServiceWorkerContextCore> context_ptr_; |
| 43 base::MessageLoopForIO message_loop_; | 68 base::MessageLoopForIO message_loop_; |
| 44 BrowserThreadImpl io_thread_; | 69 BrowserThreadImpl io_thread_; |
| 45 }; | 70 }; |
| 46 | 71 |
| 72 TEST_F(ServiceWorkerRegistrationTest, SetAndUnsetVersions) { | |
| 73 const GURL kScope("http://www.example.not/*"); | |
| 74 const GURL kScript("http://www.example.not/service_worker.js"); | |
| 75 int64 kRegistrationId = 1L; | |
| 76 scoped_refptr<ServiceWorkerRegistration> registration = | |
| 77 new ServiceWorkerRegistration( | |
| 78 kScope, | |
| 79 kScript, | |
| 80 kRegistrationId, | |
| 81 context_ptr_); | |
| 82 | |
| 83 const int64 version_1_id = 1L; | |
| 84 const int64 version_2_id = 2L; | |
| 85 scoped_refptr<ServiceWorkerVersion> version_1 = | |
| 86 new ServiceWorkerVersion(registration, version_1_id, context_ptr_); | |
| 87 scoped_refptr<ServiceWorkerVersion> version_2 = | |
| 88 new ServiceWorkerVersion(registration, version_2_id, context_ptr_); | |
| 89 | |
| 90 RegistrationListener listener; | |
| 91 registration->AddListener(&listener); | |
| 92 registration->SetActiveVersion(version_1); | |
| 93 | |
| 94 EXPECT_EQ(version_1, registration->active_version()); | |
| 95 EXPECT_EQ(registration, listener.observed_registration_); | |
| 96 EXPECT_EQ(ChangedVersionAttributesMask::ACTIVE_VERSION, | |
| 97 listener.observed_changed_mask_.changed()); | |
| 98 EXPECT_EQ(kScope, listener.observed_info_.pattern); | |
| 99 EXPECT_EQ(kScript, listener.observed_info_.script_url); | |
| 100 EXPECT_EQ(version_1_id, listener.observed_info_.active_version.version_id); | |
| 101 EXPECT_TRUE(listener.observed_info_.installing_version.is_null); | |
| 102 EXPECT_TRUE(listener.observed_info_.waiting_version.is_null); | |
| 103 EXPECT_TRUE(listener.observed_info_.controlling_version.is_null); | |
| 104 listener.Reset(); | |
| 105 | |
| 106 registration->SetInstallingVersion(version_2); | |
| 107 | |
| 108 EXPECT_EQ(version_2, registration->installing_version()); | |
| 109 EXPECT_EQ(ChangedVersionAttributesMask::INSTALLING_VERSION, | |
| 110 listener.observed_changed_mask_.changed()); | |
| 111 EXPECT_EQ(version_1_id, listener.observed_info_.active_version.version_id); | |
| 112 EXPECT_EQ(version_2_id, | |
| 113 listener.observed_info_.installing_version.version_id); | |
| 114 EXPECT_TRUE(listener.observed_info_.waiting_version.is_null); | |
| 115 EXPECT_TRUE(listener.observed_info_.controlling_version.is_null); | |
| 116 listener.Reset(); | |
| 117 | |
| 118 registration->SetWaitingVersion(version_2); | |
| 119 | |
| 120 EXPECT_EQ(version_2, registration->waiting_version()); | |
| 121 EXPECT_FALSE(registration->installing_version()); | |
| 122 EXPECT_TRUE(listener.observed_changed_mask_.waiting_changed()); | |
| 123 EXPECT_TRUE(listener.observed_changed_mask_.installing_changed()); | |
| 124 EXPECT_EQ(version_1_id, listener.observed_info_.active_version.version_id); | |
| 125 EXPECT_EQ(version_2_id, listener.observed_info_.waiting_version.version_id); | |
| 126 EXPECT_TRUE(listener.observed_info_.installing_version.is_null); | |
| 127 EXPECT_TRUE(listener.observed_info_.controlling_version.is_null); | |
| 128 listener.Reset(); | |
| 129 | |
| 130 registration->UnsetVersion(version_2); | |
| 131 | |
| 132 EXPECT_FALSE(registration->waiting_version()); | |
| 133 EXPECT_EQ(ChangedVersionAttributesMask::WAITING_VERSION, | |
| 134 listener.observed_changed_mask_.changed()); | |
| 135 EXPECT_EQ(version_1_id, listener.observed_info_.active_version.version_id); | |
| 136 EXPECT_TRUE(listener.observed_info_.waiting_version.is_null); | |
| 137 EXPECT_TRUE(listener.observed_info_.installing_version.is_null); | |
| 138 EXPECT_TRUE(listener.observed_info_.controlling_version.is_null); | |
| 139 } | |
| 140 | |
| 47 } // namespace content | 141 } // namespace content |
| OLD | NEW |