1 // Copyright (c) 2012 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 "base/message_loop/message_loop.h" 6 #include "testing/gtest/include/gtest/gtest.h" 7 #include "webkit/browser/appcache/appcache.h" 8 #include "webkit/browser/appcache/appcache_group.h" 9 #include "webkit/browser/appcache/appcache_response.h" 10 #include "webkit/browser/appcache/appcache_storage.h" 11 #include "webkit/browser/appcache/mock_appcache_service.h" 12 #include "webkit/browser/quota/mock_quota_manager.h" 13 14 namespace appcache { 15 16 namespace { 17 const quota::StorageType kTemp = quota::kStorageTypeTemporary; 18 } 19 20 class AppCacheStorageTest : public testing::Test { 21 public: 22 class MockStorageDelegate : public AppCacheStorage::Delegate { 23 public: 24 }; 25 }; 26 27 TEST_F(AppCacheStorageTest, AddRemoveCache) { 28 MockAppCacheService service; 29 scoped_refptr<AppCache> cache(new AppCache(service.storage(), 111)); 30 31 EXPECT_EQ(cache.get(), 32 service.storage()->working_set()->GetCache(111)); 33 34 service.storage()->working_set()->RemoveCache(cache.get()); 35 36 EXPECT_TRUE(!service.storage()->working_set()->GetCache(111)); 37 38 // Removing non-existing cache from service should not fail. 39 MockAppCacheService dummy; 40 dummy.storage()->working_set()->RemoveCache(cache.get()); 41 } 42 43 TEST_F(AppCacheStorageTest, AddRemoveGroup) { 44 MockAppCacheService service; 45 scoped_refptr<AppCacheGroup> group( 46 new AppCacheGroup(service.storage(), GURL(), 111)); 47 48 EXPECT_EQ(group.get(), service.storage()->working_set()->GetGroup(GURL())); 49 50 service.storage()->working_set()->RemoveGroup(group.get()); 51 52 EXPECT_TRUE(!service.storage()->working_set()->GetGroup(GURL())); 53 54 // Removing non-existing group from service should not fail. 55 MockAppCacheService dummy; 56 dummy.storage()->working_set()->RemoveGroup(group.get()); 57 } 58 59 TEST_F(AppCacheStorageTest, AddRemoveResponseInfo) { 60 MockAppCacheService service; 61 scoped_refptr<AppCacheResponseInfo> info( 62 new AppCacheResponseInfo(service.storage(), GURL(), 63 111, new net::HttpResponseInfo, 64 kUnkownResponseDataSize)); 65 66 EXPECT_EQ(info.get(), 67 service.storage()->working_set()->GetResponseInfo(111)); 68 69 service.storage()->working_set()->RemoveResponseInfo(info.get()); 70 71 EXPECT_TRUE(!service.storage()->working_set()->GetResponseInfo(111)); 72 73 // Removing non-existing info from service should not fail. 74 MockAppCacheService dummy; 75 dummy.storage()->working_set()->RemoveResponseInfo(info.get()); 76 } 77 78 TEST_F(AppCacheStorageTest, DelegateReferences) { 79 typedef scoped_refptr<AppCacheStorage::DelegateReference> 80 ScopedDelegateReference; 81 MockAppCacheService service; 82 MockStorageDelegate delegate; 83 ScopedDelegateReference delegate_reference1; 84 ScopedDelegateReference delegate_reference2; 85 86 EXPECT_FALSE(service.storage()->GetDelegateReference(&delegate)); 87 88 delegate_reference1 = 89 service.storage()->GetOrCreateDelegateReference(&delegate); 90 EXPECT_TRUE(delegate_reference1.get()); 91 EXPECT_TRUE(delegate_reference1->HasOneRef()); 92 EXPECT_TRUE(service.storage()->GetDelegateReference(&delegate)); 93 EXPECT_EQ(&delegate, 94 service.storage()->GetDelegateReference(&delegate)->delegate); 95 EXPECT_EQ(service.storage()->GetDelegateReference(&delegate), 96 service.storage()->GetOrCreateDelegateReference(&delegate)); 97 delegate_reference1 = NULL; 98 EXPECT_FALSE(service.storage()->GetDelegateReference(&delegate)); 99 100 delegate_reference1 = 101 service.storage()->GetOrCreateDelegateReference(&delegate); 102 service.storage()->CancelDelegateCallbacks(&delegate); 103 EXPECT_TRUE(delegate_reference1.get()); 104 EXPECT_TRUE(delegate_reference1->HasOneRef()); 105 EXPECT_FALSE(delegate_reference1->delegate); 106 EXPECT_FALSE(service.storage()->GetDelegateReference(&delegate)); 107 108 delegate_reference2 = 109 service.storage()->GetOrCreateDelegateReference(&delegate); 110 EXPECT_TRUE(delegate_reference2.get()); 111 EXPECT_TRUE(delegate_reference2->HasOneRef()); 112 EXPECT_EQ(&delegate, delegate_reference2->delegate); 113 EXPECT_NE(delegate_reference1.get(), delegate_reference2.get()); 114 } 115 116 TEST_F(AppCacheStorageTest, UsageMap) { 117 const GURL kOrigin("http://origin/"); 118 const GURL kOrigin2("http://origin2/"); 119 120 MockAppCacheService service; 121 scoped_refptr<quota::MockQuotaManagerProxy> mock_proxy( 122 new quota::MockQuotaManagerProxy(NULL, NULL)); 123 service.set_quota_manager_proxy(mock_proxy.get()); 124 125 service.storage()->UpdateUsageMapAndNotify(kOrigin, 0); 126 EXPECT_EQ(0, mock_proxy->notify_storage_modified_count()); 127 128 service.storage()->UpdateUsageMapAndNotify(kOrigin, 10); 129 EXPECT_EQ(1, mock_proxy->notify_storage_modified_count()); 130 EXPECT_EQ(10, mock_proxy->last_notified_delta()); 131 EXPECT_EQ(kOrigin, mock_proxy->last_notified_origin()); 132 EXPECT_EQ(kTemp, mock_proxy->last_notified_type()); 133 134 service.storage()->UpdateUsageMapAndNotify(kOrigin, 100); 135 EXPECT_EQ(2, mock_proxy->notify_storage_modified_count()); 136 EXPECT_EQ(90, mock_proxy->last_notified_delta()); 137 EXPECT_EQ(kOrigin, mock_proxy->last_notified_origin()); 138 EXPECT_EQ(kTemp, mock_proxy->last_notified_type()); 139 140 service.storage()->UpdateUsageMapAndNotify(kOrigin, 0); 141 EXPECT_EQ(3, mock_proxy->notify_storage_modified_count()); 142 EXPECT_EQ(-100, mock_proxy->last_notified_delta()); 143 EXPECT_EQ(kOrigin, mock_proxy->last_notified_origin()); 144 EXPECT_EQ(kTemp, mock_proxy->last_notified_type()); 145 146 service.storage()->NotifyStorageAccessed(kOrigin2); 147 EXPECT_EQ(0, mock_proxy->notify_storage_accessed_count()); 148 149 service.storage()->usage_map_[kOrigin2] = 1; 150 service.storage()->NotifyStorageAccessed(kOrigin2); 151 EXPECT_EQ(1, mock_proxy->notify_storage_accessed_count()); 152 EXPECT_EQ(kOrigin2, mock_proxy->last_notified_origin()); 153 EXPECT_EQ(kTemp, mock_proxy->last_notified_type()); 154 155 service.storage()->usage_map_.clear(); 156 service.storage()->usage_map_[kOrigin] = 5000; 157 service.storage()->ClearUsageMapAndNotify(); 158 EXPECT_EQ(4, mock_proxy->notify_storage_modified_count()); 159 EXPECT_EQ(-5000, mock_proxy->last_notified_delta()); 160 EXPECT_EQ(kOrigin, mock_proxy->last_notified_origin()); 161 EXPECT_EQ(kTemp, mock_proxy->last_notified_type()); 162 EXPECT_TRUE(service.storage()->usage_map_.empty()); 163 } 164 165 } // namespace appcache 166