OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 <string> | 5 #include <string> |
6 | 6 |
7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
8 #include "webkit/appcache/appcache.h" | 8 #include "webkit/appcache/appcache.h" |
9 #include "webkit/appcache/appcache_group.h" | 9 #include "webkit/appcache/appcache_group.h" |
10 #include "webkit/appcache/appcache_host.h" | 10 #include "webkit/appcache/appcache_host.h" |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 } | 92 } |
93 | 93 |
94 bool update_completed_; | 94 bool update_completed_; |
95 }; | 95 }; |
96 | 96 |
97 class AppCacheGroupTest : public testing::Test { | 97 class AppCacheGroupTest : public testing::Test { |
98 }; | 98 }; |
99 | 99 |
100 TEST(AppCacheGroupTest, AddRemoveCache) { | 100 TEST(AppCacheGroupTest, AddRemoveCache) { |
101 MockAppCacheService service; | 101 MockAppCacheService service; |
102 scoped_refptr<AppCacheGroup> group = | 102 scoped_refptr<AppCacheGroup> group( |
103 new AppCacheGroup(&service, GURL("http://foo.com"), 111); | 103 new AppCacheGroup(&service, GURL("http://foo.com"), 111)); |
104 | 104 |
105 base::Time now = base::Time::Now(); | 105 base::Time now = base::Time::Now(); |
106 | 106 |
107 scoped_refptr<AppCache> cache1 = new AppCache(&service, 111); | 107 scoped_refptr<AppCache> cache1(new AppCache(&service, 111)); |
108 cache1->set_complete(true); | 108 cache1->set_complete(true); |
109 cache1->set_update_time(now); | 109 cache1->set_update_time(now); |
110 group->AddCache(cache1); | 110 group->AddCache(cache1); |
111 EXPECT_EQ(cache1, group->newest_complete_cache()); | 111 EXPECT_EQ(cache1, group->newest_complete_cache()); |
112 | 112 |
113 // Adding older cache does not change newest complete cache. | 113 // Adding older cache does not change newest complete cache. |
114 scoped_refptr<AppCache> cache2 = new AppCache(&service, 222); | 114 scoped_refptr<AppCache> cache2(new AppCache(&service, 222)); |
115 cache2->set_complete(true); | 115 cache2->set_complete(true); |
116 cache2->set_update_time(now - base::TimeDelta::FromDays(1)); | 116 cache2->set_update_time(now - base::TimeDelta::FromDays(1)); |
117 group->AddCache(cache2); | 117 group->AddCache(cache2); |
118 EXPECT_EQ(cache1, group->newest_complete_cache()); | 118 EXPECT_EQ(cache1, group->newest_complete_cache()); |
119 | 119 |
120 // Adding newer cache does change newest complete cache. | 120 // Adding newer cache does change newest complete cache. |
121 scoped_refptr<AppCache> cache3 = new AppCache(&service, 333); | 121 scoped_refptr<AppCache> cache3(new AppCache(&service, 333)); |
122 cache3->set_complete(true); | 122 cache3->set_complete(true); |
123 cache3->set_update_time(now + base::TimeDelta::FromDays(1)); | 123 cache3->set_update_time(now + base::TimeDelta::FromDays(1)); |
124 group->AddCache(cache3); | 124 group->AddCache(cache3); |
125 EXPECT_EQ(cache3, group->newest_complete_cache()); | 125 EXPECT_EQ(cache3, group->newest_complete_cache()); |
126 | 126 |
127 // Adding cache with same update time uses one with larger ID. | 127 // Adding cache with same update time uses one with larger ID. |
128 scoped_refptr<AppCache> cache4 = new AppCache(&service, 444); | 128 scoped_refptr<AppCache> cache4(new AppCache(&service, 444)); |
129 cache4->set_complete(true); | 129 cache4->set_complete(true); |
130 cache4->set_update_time(now + base::TimeDelta::FromDays(1)); // same as 3 | 130 cache4->set_update_time(now + base::TimeDelta::FromDays(1)); // same as 3 |
131 group->AddCache(cache4); | 131 group->AddCache(cache4); |
132 EXPECT_EQ(cache4, group->newest_complete_cache()); | 132 EXPECT_EQ(cache4, group->newest_complete_cache()); |
133 | 133 |
134 scoped_refptr<AppCache> cache5 = new AppCache(&service, 55); // smaller id | 134 scoped_refptr<AppCache> cache5(new AppCache(&service, 55)); // smaller id |
135 cache5->set_complete(true); | 135 cache5->set_complete(true); |
136 cache5->set_update_time(now + base::TimeDelta::FromDays(1)); // same as 4 | 136 cache5->set_update_time(now + base::TimeDelta::FromDays(1)); // same as 4 |
137 group->AddCache(cache5); | 137 group->AddCache(cache5); |
138 EXPECT_EQ(cache4, group->newest_complete_cache()); // no change | 138 EXPECT_EQ(cache4, group->newest_complete_cache()); // no change |
139 | 139 |
140 // Old caches can always be removed. | 140 // Old caches can always be removed. |
141 group->RemoveCache(cache1); | 141 group->RemoveCache(cache1); |
142 EXPECT_FALSE(cache1->owning_group()); | 142 EXPECT_FALSE(cache1->owning_group()); |
143 EXPECT_EQ(cache4, group->newest_complete_cache()); // newest unchanged | 143 EXPECT_EQ(cache4, group->newest_complete_cache()); // newest unchanged |
144 | 144 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 // Unassociate all hosts from older cache. | 202 // Unassociate all hosts from older cache. |
203 host1.AssociateCache(NULL); | 203 host1.AssociateCache(NULL); |
204 host2.AssociateCache(NULL); | 204 host2.AssociateCache(NULL); |
205 EXPECT_EQ(frontend.last_host_id_, host2.host_id()); | 205 EXPECT_EQ(frontend.last_host_id_, host2.host_id()); |
206 EXPECT_EQ(frontend.last_cache_id_, appcache::kNoCacheId); | 206 EXPECT_EQ(frontend.last_cache_id_, appcache::kNoCacheId); |
207 EXPECT_EQ(frontend.last_status_, appcache::UNCACHED); | 207 EXPECT_EQ(frontend.last_status_, appcache::UNCACHED); |
208 } | 208 } |
209 | 209 |
210 TEST(AppCacheGroupTest, StartUpdate) { | 210 TEST(AppCacheGroupTest, StartUpdate) { |
211 MockAppCacheService service; | 211 MockAppCacheService service; |
212 scoped_refptr<AppCacheGroup> group = | 212 scoped_refptr<AppCacheGroup> group( |
213 new AppCacheGroup(&service, GURL("http://foo.com"), 111); | 213 new AppCacheGroup(&service, GURL("http://foo.com"), 111)); |
214 | 214 |
215 // Set state to checking to prevent update job from executing fetches. | 215 // Set state to checking to prevent update job from executing fetches. |
216 group->update_status_ = AppCacheGroup::CHECKING; | 216 group->update_status_ = AppCacheGroup::CHECKING; |
217 group->StartUpdate(); | 217 group->StartUpdate(); |
218 AppCacheUpdateJob* update = group->update_job_; | 218 AppCacheUpdateJob* update = group->update_job_; |
219 EXPECT_TRUE(update != NULL); | 219 EXPECT_TRUE(update != NULL); |
220 | 220 |
221 // Start another update, check that same update job is in use. | 221 // Start another update, check that same update job is in use. |
222 group->StartUpdateWithHost(NULL); | 222 group->StartUpdateWithHost(NULL); |
223 EXPECT_EQ(update, group->update_job_); | 223 EXPECT_EQ(update, group->update_job_); |
224 | 224 |
225 // Deleting the update should restore the group to IDLE. | 225 // Deleting the update should restore the group to IDLE. |
226 delete update; | 226 delete update; |
227 EXPECT_TRUE(group->update_job_ == NULL); | 227 EXPECT_TRUE(group->update_job_ == NULL); |
228 EXPECT_EQ(AppCacheGroup::IDLE, group->update_status()); | 228 EXPECT_EQ(AppCacheGroup::IDLE, group->update_status()); |
229 } | 229 } |
230 | 230 |
231 TEST(AppCacheGroupTest, CancelUpdate) { | 231 TEST(AppCacheGroupTest, CancelUpdate) { |
232 MockAppCacheService service; | 232 MockAppCacheService service; |
233 scoped_refptr<AppCacheGroup> group = | 233 scoped_refptr<AppCacheGroup> group( |
234 new AppCacheGroup(&service, GURL("http://foo.com"), 111); | 234 new AppCacheGroup(&service, GURL("http://foo.com"), 111)); |
235 | 235 |
236 // Set state to checking to prevent update job from executing fetches. | 236 // Set state to checking to prevent update job from executing fetches. |
237 group->update_status_ = AppCacheGroup::CHECKING; | 237 group->update_status_ = AppCacheGroup::CHECKING; |
238 group->StartUpdate(); | 238 group->StartUpdate(); |
239 AppCacheUpdateJob* update = group->update_job_; | 239 AppCacheUpdateJob* update = group->update_job_; |
240 EXPECT_TRUE(update != NULL); | 240 EXPECT_TRUE(update != NULL); |
241 | 241 |
242 // Deleting the group should cancel the update. | 242 // Deleting the group should cancel the update. |
243 TestUpdateObserver observer; | 243 TestUpdateObserver observer; |
244 group->AddUpdateObserver(&observer); | 244 group->AddUpdateObserver(&observer); |
245 group = NULL; // causes group to be deleted | 245 group = NULL; // causes group to be deleted |
246 EXPECT_TRUE(observer.update_completed_); | 246 EXPECT_TRUE(observer.update_completed_); |
247 EXPECT_FALSE(observer.group_has_cache_); | 247 EXPECT_FALSE(observer.group_has_cache_); |
248 } | 248 } |
249 | 249 |
250 TEST(AppCacheGroupTest, QueueUpdate) { | 250 TEST(AppCacheGroupTest, QueueUpdate) { |
251 MockAppCacheService service; | 251 MockAppCacheService service; |
252 scoped_refptr<AppCacheGroup> group = | 252 scoped_refptr<AppCacheGroup> group( |
253 new AppCacheGroup(&service, GURL("http://foo.com"), 111); | 253 new AppCacheGroup(&service, GURL("http://foo.com"), 111)); |
254 | 254 |
255 // Set state to checking to prevent update job from executing fetches. | 255 // Set state to checking to prevent update job from executing fetches. |
256 group->update_status_ = AppCacheGroup::CHECKING; | 256 group->update_status_ = AppCacheGroup::CHECKING; |
257 group->StartUpdate(); | 257 group->StartUpdate(); |
258 EXPECT_TRUE(group->update_job_); | 258 EXPECT_TRUE(group->update_job_); |
259 | 259 |
260 // Pretend group's update job is terminating so that next update is queued. | 260 // Pretend group's update job is terminating so that next update is queued. |
261 group->update_job_->internal_state_ = AppCacheUpdateJob::REFETCH_MANIFEST; | 261 group->update_job_->internal_state_ = AppCacheUpdateJob::REFETCH_MANIFEST; |
262 EXPECT_TRUE(group->update_job_->IsTerminating()); | 262 EXPECT_TRUE(group->update_job_->IsTerminating()); |
263 | 263 |
(...skipping 25 matching lines...) Expand all Loading... |
289 EXPECT_TRUE(group->FindObserver(&host, group->observers_)); | 289 EXPECT_TRUE(group->FindObserver(&host, group->observers_)); |
290 | 290 |
291 // Delete update to cause it to complete. Verify host is notified. | 291 // Delete update to cause it to complete. Verify host is notified. |
292 delete group->update_job_; | 292 delete group->update_job_; |
293 EXPECT_EQ(AppCacheGroup::IDLE, group->update_status_); | 293 EXPECT_EQ(AppCacheGroup::IDLE, group->update_status_); |
294 EXPECT_FALSE(group->restart_update_task_); | 294 EXPECT_FALSE(group->restart_update_task_); |
295 EXPECT_TRUE(host.update_completed_); | 295 EXPECT_TRUE(host.update_completed_); |
296 } | 296 } |
297 | 297 |
298 } // namespace appcache | 298 } // namespace appcache |
OLD | NEW |