OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "webkit/appcache/appcache_service.h" |
| 6 |
| 7 #include "base/message_loop.h" |
| 8 #include "net/base/completion_callback.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "webkit/appcache/mock_appcache_storage.h" |
| 11 |
| 12 namespace appcache { |
| 13 |
| 14 class AppCacheServiceTest : public testing::Test { |
| 15 public: |
| 16 AppCacheServiceTest() |
| 17 : kOrigin("http://hello/"), |
| 18 service_(new AppCacheService(NULL)), |
| 19 delete_result_(net::OK), delete_completion_count_(0), |
| 20 ALLOW_THIS_IN_INITIALIZER_LIST(deletion_callback_( |
| 21 this, &AppCacheServiceTest::OnDeleteAppCachesComplete)) { |
| 22 // Setup to use mock storage. |
| 23 service_->storage_.reset(new MockAppCacheStorage(service_.get())); |
| 24 } |
| 25 |
| 26 void OnDeleteAppCachesComplete(int result) { |
| 27 delete_result_ = result; |
| 28 ++delete_completion_count_; |
| 29 } |
| 30 |
| 31 MockAppCacheStorage* mock_storage() { |
| 32 return static_cast<MockAppCacheStorage*>(service_->storage()); |
| 33 } |
| 34 |
| 35 const GURL kOrigin; |
| 36 |
| 37 scoped_ptr<AppCacheService> service_; |
| 38 int delete_result_; |
| 39 int delete_completion_count_; |
| 40 net::CompletionCallbackImpl<AppCacheServiceTest> deletion_callback_; |
| 41 }; |
| 42 |
| 43 TEST_F(AppCacheServiceTest, DeleteAppCachesForOrigin) { |
| 44 // Without giving mock storage simiulated info, should fail. |
| 45 service_->DeleteAppCachesForOrigin(kOrigin, &deletion_callback_); |
| 46 EXPECT_EQ(0, delete_completion_count_); |
| 47 MessageLoop::current()->RunAllPending(); |
| 48 EXPECT_EQ(1, delete_completion_count_); |
| 49 EXPECT_EQ(net::ERR_FAILED, delete_result_); |
| 50 delete_completion_count_ = 0; |
| 51 |
| 52 // Should succeed given an empty info collection. |
| 53 mock_storage()->SimulateGetAllInfo(new AppCacheInfoCollection); |
| 54 service_->DeleteAppCachesForOrigin(kOrigin, &deletion_callback_); |
| 55 EXPECT_EQ(0, delete_completion_count_); |
| 56 MessageLoop::current()->RunAllPending(); |
| 57 EXPECT_EQ(1, delete_completion_count_); |
| 58 EXPECT_EQ(net::OK, delete_result_); |
| 59 delete_completion_count_ = 0; |
| 60 |
| 61 scoped_refptr<AppCacheInfoCollection> info(new AppCacheInfoCollection); |
| 62 |
| 63 // Should succeed given a non-empty info collection. |
| 64 AppCacheInfo mock_manifest_1; |
| 65 AppCacheInfo mock_manifest_2; |
| 66 AppCacheInfo mock_manifest_3; |
| 67 mock_manifest_1.manifest_url = kOrigin.Resolve("manifest1"); |
| 68 mock_manifest_2.manifest_url = kOrigin.Resolve("manifest2"); |
| 69 mock_manifest_3.manifest_url = kOrigin.Resolve("manifest3"); |
| 70 AppCacheInfoVector info_vector; |
| 71 info_vector.push_back(mock_manifest_1); |
| 72 info_vector.push_back(mock_manifest_2); |
| 73 info_vector.push_back(mock_manifest_3); |
| 74 info->infos_by_origin[kOrigin] = info_vector; |
| 75 mock_storage()->SimulateGetAllInfo(info); |
| 76 service_->DeleteAppCachesForOrigin(kOrigin, &deletion_callback_); |
| 77 EXPECT_EQ(0, delete_completion_count_); |
| 78 MessageLoop::current()->RunAllPending(); |
| 79 EXPECT_EQ(1, delete_completion_count_); |
| 80 EXPECT_EQ(net::OK, delete_result_); |
| 81 delete_completion_count_ = 0; |
| 82 |
| 83 // Should fail if storage fails to delete. |
| 84 info->infos_by_origin[kOrigin] = info_vector; |
| 85 mock_storage()->SimulateGetAllInfo(info); |
| 86 mock_storage()->SimulateMakeGroupObsoleteFailure(); |
| 87 service_->DeleteAppCachesForOrigin(kOrigin, &deletion_callback_); |
| 88 EXPECT_EQ(0, delete_completion_count_); |
| 89 MessageLoop::current()->RunAllPending(); |
| 90 EXPECT_EQ(1, delete_completion_count_); |
| 91 EXPECT_EQ(net::ERR_FAILED, delete_result_); |
| 92 delete_completion_count_ = 0; |
| 93 |
| 94 // Should complete with abort error if the service is deleted |
| 95 // prior to a delete completion. |
| 96 service_->DeleteAppCachesForOrigin(kOrigin, &deletion_callback_); |
| 97 EXPECT_EQ(0, delete_completion_count_); |
| 98 service_.reset(); // kill it |
| 99 EXPECT_EQ(1, delete_completion_count_); |
| 100 EXPECT_EQ(net::ERR_ABORTED, delete_result_); |
| 101 delete_completion_count_ = 0; |
| 102 |
| 103 // Let any tasks lingering from the sudden deletion run and verify |
| 104 // no other completion calls occur. |
| 105 MessageLoop::current()->RunAllPending(); |
| 106 EXPECT_EQ(0, delete_completion_count_); |
| 107 } |
| 108 |
| 109 } // namespace appcache |
OLD | NEW |