| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/message_loop.h" |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 8 #include "webkit/appcache/appcache.h" | 9 #include "webkit/appcache/appcache.h" |
| 9 #include "webkit/appcache/appcache_group.h" | 10 #include "webkit/appcache/appcache_group.h" |
| 10 #include "webkit/appcache/appcache_host.h" | 11 #include "webkit/appcache/appcache_host.h" |
| 11 #include "webkit/appcache/mock_appcache_service.h" | 12 #include "webkit/appcache/mock_appcache_service.h" |
| 12 #include "webkit/appcache/appcache_update_job.h" | 13 #include "webkit/appcache/appcache_update_job.h" |
| 13 #include "webkit/appcache/appcache_interfaces.h" | 14 #include "webkit/appcache/appcache_interfaces.h" |
| 14 | 15 |
| 15 namespace { | 16 namespace { |
| 16 | 17 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 } | 89 } |
| 89 | 90 |
| 90 virtual void OnUpdateComplete(AppCacheGroup* group) { | 91 virtual void OnUpdateComplete(AppCacheGroup* group) { |
| 91 update_completed_ = true; | 92 update_completed_ = true; |
| 92 } | 93 } |
| 93 | 94 |
| 94 bool update_completed_; | 95 bool update_completed_; |
| 95 }; | 96 }; |
| 96 | 97 |
| 97 class AppCacheGroupTest : public testing::Test { | 98 class AppCacheGroupTest : public testing::Test { |
| 99 private: |
| 100 MessageLoop message_loop_; |
| 98 }; | 101 }; |
| 99 | 102 |
| 100 TEST(AppCacheGroupTest, AddRemoveCache) { | 103 TEST_F(AppCacheGroupTest, AddRemoveCache) { |
| 101 MockAppCacheService service; | 104 MockAppCacheService service; |
| 102 scoped_refptr<AppCacheGroup> group( | 105 scoped_refptr<AppCacheGroup> group( |
| 103 new AppCacheGroup(&service, GURL("http://foo.com"), 111)); | 106 new AppCacheGroup(&service, GURL("http://foo.com"), 111)); |
| 104 | 107 |
| 105 base::Time now = base::Time::Now(); | 108 base::Time now = base::Time::Now(); |
| 106 | 109 |
| 107 scoped_refptr<AppCache> cache1(new AppCache(&service, 111)); | 110 scoped_refptr<AppCache> cache1(new AppCache(&service, 111)); |
| 108 cache1->set_complete(true); | 111 cache1->set_complete(true); |
| 109 cache1->set_update_time(now); | 112 cache1->set_update_time(now); |
| 110 group->AddCache(cache1); | 113 group->AddCache(cache1); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 // Can remove newest cache if there are older caches. | 162 // Can remove newest cache if there are older caches. |
| 160 group->AddCache(cache1); | 163 group->AddCache(cache1); |
| 161 EXPECT_EQ(cache1, group->newest_complete_cache()); | 164 EXPECT_EQ(cache1, group->newest_complete_cache()); |
| 162 group->AddCache(cache4); | 165 group->AddCache(cache4); |
| 163 EXPECT_EQ(cache4, group->newest_complete_cache()); | 166 EXPECT_EQ(cache4, group->newest_complete_cache()); |
| 164 group->RemoveCache(cache4); // remove newest | 167 group->RemoveCache(cache4); // remove newest |
| 165 EXPECT_FALSE(cache4->owning_group()); | 168 EXPECT_FALSE(cache4->owning_group()); |
| 166 EXPECT_FALSE(group->newest_complete_cache()); // newest removed | 169 EXPECT_FALSE(group->newest_complete_cache()); // newest removed |
| 167 } | 170 } |
| 168 | 171 |
| 169 TEST(AppCacheGroupTest, CleanupUnusedGroup) { | 172 TEST_F(AppCacheGroupTest, CleanupUnusedGroup) { |
| 170 MockAppCacheService service; | 173 MockAppCacheService service; |
| 171 TestAppCacheFrontend frontend; | 174 TestAppCacheFrontend frontend; |
| 172 AppCacheGroup* group = | 175 AppCacheGroup* group = |
| 173 new AppCacheGroup(&service, GURL("http://foo.com"), 111); | 176 new AppCacheGroup(&service, GURL("http://foo.com"), 111); |
| 174 | 177 |
| 175 AppCacheHost host1(1, &frontend, &service); | 178 AppCacheHost host1(1, &frontend, &service); |
| 176 AppCacheHost host2(2, &frontend, &service); | 179 AppCacheHost host2(2, &frontend, &service); |
| 177 | 180 |
| 178 base::Time now = base::Time::Now(); | 181 base::Time now = base::Time::Now(); |
| 179 | 182 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 200 EXPECT_EQ(cache2, group->newest_complete_cache()); | 203 EXPECT_EQ(cache2, group->newest_complete_cache()); |
| 201 | 204 |
| 202 // Unassociate all hosts from older cache. | 205 // Unassociate all hosts from older cache. |
| 203 host1.AssociateNoCache(GURL()); | 206 host1.AssociateNoCache(GURL()); |
| 204 host2.AssociateNoCache(GURL()); | 207 host2.AssociateNoCache(GURL()); |
| 205 EXPECT_EQ(frontend.last_host_id_, host2.host_id()); | 208 EXPECT_EQ(frontend.last_host_id_, host2.host_id()); |
| 206 EXPECT_EQ(frontend.last_cache_id_, appcache::kNoCacheId); | 209 EXPECT_EQ(frontend.last_cache_id_, appcache::kNoCacheId); |
| 207 EXPECT_EQ(frontend.last_status_, appcache::UNCACHED); | 210 EXPECT_EQ(frontend.last_status_, appcache::UNCACHED); |
| 208 } | 211 } |
| 209 | 212 |
| 210 TEST(AppCacheGroupTest, StartUpdate) { | 213 TEST_F(AppCacheGroupTest, StartUpdate) { |
| 211 MockAppCacheService service; | 214 MockAppCacheService service; |
| 212 scoped_refptr<AppCacheGroup> group( | 215 scoped_refptr<AppCacheGroup> group( |
| 213 new AppCacheGroup(&service, GURL("http://foo.com"), 111)); | 216 new AppCacheGroup(&service, GURL("http://foo.com"), 111)); |
| 214 | 217 |
| 215 // Set state to checking to prevent update job from executing fetches. | 218 // Set state to checking to prevent update job from executing fetches. |
| 216 group->update_status_ = AppCacheGroup::CHECKING; | 219 group->update_status_ = AppCacheGroup::CHECKING; |
| 217 group->StartUpdate(); | 220 group->StartUpdate(); |
| 218 AppCacheUpdateJob* update = group->update_job_; | 221 AppCacheUpdateJob* update = group->update_job_; |
| 219 EXPECT_TRUE(update != NULL); | 222 EXPECT_TRUE(update != NULL); |
| 220 | 223 |
| 221 // Start another update, check that same update job is in use. | 224 // Start another update, check that same update job is in use. |
| 222 group->StartUpdateWithHost(NULL); | 225 group->StartUpdateWithHost(NULL); |
| 223 EXPECT_EQ(update, group->update_job_); | 226 EXPECT_EQ(update, group->update_job_); |
| 224 | 227 |
| 225 // Deleting the update should restore the group to IDLE. | 228 // Deleting the update should restore the group to IDLE. |
| 226 delete update; | 229 delete update; |
| 227 EXPECT_TRUE(group->update_job_ == NULL); | 230 EXPECT_TRUE(group->update_job_ == NULL); |
| 228 EXPECT_EQ(AppCacheGroup::IDLE, group->update_status()); | 231 EXPECT_EQ(AppCacheGroup::IDLE, group->update_status()); |
| 229 } | 232 } |
| 230 | 233 |
| 231 TEST(AppCacheGroupTest, CancelUpdate) { | 234 TEST_F(AppCacheGroupTest, CancelUpdate) { |
| 232 MockAppCacheService service; | 235 MockAppCacheService service; |
| 233 scoped_refptr<AppCacheGroup> group( | 236 scoped_refptr<AppCacheGroup> group( |
| 234 new AppCacheGroup(&service, GURL("http://foo.com"), 111)); | 237 new AppCacheGroup(&service, GURL("http://foo.com"), 111)); |
| 235 | 238 |
| 236 // Set state to checking to prevent update job from executing fetches. | 239 // Set state to checking to prevent update job from executing fetches. |
| 237 group->update_status_ = AppCacheGroup::CHECKING; | 240 group->update_status_ = AppCacheGroup::CHECKING; |
| 238 group->StartUpdate(); | 241 group->StartUpdate(); |
| 239 AppCacheUpdateJob* update = group->update_job_; | 242 AppCacheUpdateJob* update = group->update_job_; |
| 240 EXPECT_TRUE(update != NULL); | 243 EXPECT_TRUE(update != NULL); |
| 241 | 244 |
| 242 // Deleting the group should cancel the update. | 245 // Deleting the group should cancel the update. |
| 243 TestUpdateObserver observer; | 246 TestUpdateObserver observer; |
| 244 group->AddUpdateObserver(&observer); | 247 group->AddUpdateObserver(&observer); |
| 245 group = NULL; // causes group to be deleted | 248 group = NULL; // causes group to be deleted |
| 246 EXPECT_TRUE(observer.update_completed_); | 249 EXPECT_TRUE(observer.update_completed_); |
| 247 EXPECT_FALSE(observer.group_has_cache_); | 250 EXPECT_FALSE(observer.group_has_cache_); |
| 248 } | 251 } |
| 249 | 252 |
| 250 TEST(AppCacheGroupTest, QueueUpdate) { | 253 TEST_F(AppCacheGroupTest, QueueUpdate) { |
| 251 MockAppCacheService service; | 254 MockAppCacheService service; |
| 252 scoped_refptr<AppCacheGroup> group( | 255 scoped_refptr<AppCacheGroup> group( |
| 253 new AppCacheGroup(&service, GURL("http://foo.com"), 111)); | 256 new AppCacheGroup(&service, GURL("http://foo.com"), 111)); |
| 254 | 257 |
| 255 // Set state to checking to prevent update job from executing fetches. | 258 // Set state to checking to prevent update job from executing fetches. |
| 256 group->update_status_ = AppCacheGroup::CHECKING; | 259 group->update_status_ = AppCacheGroup::CHECKING; |
| 257 group->StartUpdate(); | 260 group->StartUpdate(); |
| 258 EXPECT_TRUE(group->update_job_); | 261 EXPECT_TRUE(group->update_job_); |
| 259 | 262 |
| 260 // Pretend group's update job is terminating so that next update is queued. | 263 // Pretend group's update job is terminating so that next update is queued. |
| (...skipping 28 matching lines...) Expand all Loading... |
| 289 EXPECT_TRUE(group->FindObserver(&host, group->observers_)); | 292 EXPECT_TRUE(group->FindObserver(&host, group->observers_)); |
| 290 | 293 |
| 291 // Delete update to cause it to complete. Verify host is notified. | 294 // Delete update to cause it to complete. Verify host is notified. |
| 292 delete group->update_job_; | 295 delete group->update_job_; |
| 293 EXPECT_EQ(AppCacheGroup::IDLE, group->update_status_); | 296 EXPECT_EQ(AppCacheGroup::IDLE, group->update_status_); |
| 294 EXPECT_TRUE(group->restart_update_task_.IsCancelled()); | 297 EXPECT_TRUE(group->restart_update_task_.IsCancelled()); |
| 295 EXPECT_TRUE(host.update_completed_); | 298 EXPECT_TRUE(host.update_completed_); |
| 296 } | 299 } |
| 297 | 300 |
| 298 } // namespace appcache | 301 } // namespace appcache |
| OLD | NEW |