| 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 <map> | 5 #include <map> |
| 6 | 6 |
| 7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/memory/scoped_callback_factory.h" | 9 #include "base/memory/scoped_callback_factory.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 quota::StorageType type, | 86 quota::StorageType type, |
| 87 const std::string& host) { | 87 const std::string& host) { |
| 88 origins_.clear(); | 88 origins_.clear(); |
| 89 client->GetOriginsForHost(type, host, | 89 client->GetOriginsForHost(type, host, |
| 90 callback_factory_.NewCallback( | 90 callback_factory_.NewCallback( |
| 91 &IndexedDBQuotaClientTest::OnGetOriginsComplete)); | 91 &IndexedDBQuotaClientTest::OnGetOriginsComplete)); |
| 92 MessageLoop::current()->RunAllPending(); | 92 MessageLoop::current()->RunAllPending(); |
| 93 return origins_; | 93 return origins_; |
| 94 } | 94 } |
| 95 | 95 |
| 96 quota::QuotaStatusCode DeleteOrigin(quota::QuotaClient* client, |
| 97 const GURL& origin_url) { |
| 98 delete_status_ = quota::kQuotaStatusUnknown; |
| 99 client->DeleteOriginData(origin_url, kTemp, callback_factory_.NewCallback( |
| 100 &IndexedDBQuotaClientTest::OnDeleteOriginComplete)); |
| 101 MessageLoop::current()->RunAllPending(); |
| 102 return delete_status_; |
| 103 } |
| 104 |
| 96 IndexedDBContext* idb_context() { return idb_context_.get(); } | 105 IndexedDBContext* idb_context() { return idb_context_.get(); } |
| 97 | 106 |
| 98 void SetFileSizeTo(const FilePath& path, int size) { | 107 void SetFileSizeTo(const FilePath& path, int size) { |
| 99 std::string junk(size, 'a'); | 108 std::string junk(size, 'a'); |
| 100 ASSERT_EQ(size, file_util::WriteFile(path, junk.c_str(), size)); | 109 ASSERT_EQ(size, file_util::WriteFile(path, junk.c_str(), size)); |
| 101 } | 110 } |
| 102 | 111 |
| 103 void AddFakeIndexedDB(const GURL& origin, int size) { | 112 void AddFakeIndexedDB(const GURL& origin, int size) { |
| 104 FilePath file_path_origin = idb_context()->GetIndexedDBFilePath( | 113 FilePath file_path_origin = idb_context()->GetIndexedDBFilePath( |
| 105 DatabaseUtil::GetOriginIdentifier(origin)); | 114 DatabaseUtil::GetOriginIdentifier(origin)); |
| 106 if (!file_util::CreateDirectory(file_path_origin)) { | 115 if (!file_util::CreateDirectory(file_path_origin)) { |
| 107 LOG(ERROR) << "failed to file_util::CreateDirectory " | 116 LOG(ERROR) << "failed to file_util::CreateDirectory " |
| 108 << file_path_origin.value(); | 117 << file_path_origin.value(); |
| 109 } | 118 } |
| 110 file_path_origin = file_path_origin.Append(FILE_PATH_LITERAL("fake_file")); | 119 file_path_origin = file_path_origin.Append(FILE_PATH_LITERAL("fake_file")); |
| 111 SetFileSizeTo(file_path_origin, size); | 120 SetFileSizeTo(file_path_origin, size); |
| 112 } | 121 } |
| 113 | 122 |
| 114 private: | 123 private: |
| 115 void OnGetOriginUsageComplete(int64 usage) { | 124 void OnGetOriginUsageComplete(int64 usage) { |
| 116 usage_ = usage; | 125 usage_ = usage; |
| 117 } | 126 } |
| 118 | 127 |
| 119 void OnGetOriginsComplete(const std::set<GURL>& origins) { | 128 void OnGetOriginsComplete(const std::set<GURL>& origins) { |
| 120 origins_ = origins; | 129 origins_ = origins; |
| 121 } | 130 } |
| 122 | 131 |
| 132 void OnDeleteOriginComplete(quota::QuotaStatusCode code) { |
| 133 delete_status_ = code; |
| 134 } |
| 135 |
| 123 ScopedTempDir temp_dir_; | 136 ScopedTempDir temp_dir_; |
| 124 int64 usage_; | 137 int64 usage_; |
| 125 std::set<GURL> origins_; | 138 std::set<GURL> origins_; |
| 126 scoped_refptr<IndexedDBContext> idb_context_; | 139 scoped_refptr<IndexedDBContext> idb_context_; |
| 127 base::ScopedCallbackFactory<IndexedDBQuotaClientTest> callback_factory_; | 140 base::ScopedCallbackFactory<IndexedDBQuotaClientTest> callback_factory_; |
| 128 MessageLoop message_loop_; | 141 MessageLoop message_loop_; |
| 129 BrowserThread webkit_thread_; | 142 BrowserThread webkit_thread_; |
| 143 quota::QuotaStatusCode delete_status_; |
| 130 }; | 144 }; |
| 131 | 145 |
| 132 | 146 |
| 133 TEST_F(IndexedDBQuotaClientTest, GetOriginUsage) { | 147 TEST_F(IndexedDBQuotaClientTest, GetOriginUsage) { |
| 134 IndexedDBQuotaClient client( | 148 IndexedDBQuotaClient client( |
| 135 base::MessageLoopProxy::CreateForCurrentThread(), | 149 base::MessageLoopProxy::CreateForCurrentThread(), |
| 136 idb_context()); | 150 idb_context()); |
| 137 | 151 |
| 138 AddFakeIndexedDB(kOriginA, 6); | 152 AddFakeIndexedDB(kOriginA, 6); |
| 139 AddFakeIndexedDB(kOriginB, 3); | 153 AddFakeIndexedDB(kOriginB, 3); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 EXPECT_TRUE(GetOriginsForType(&client, kTemp).empty()); | 197 EXPECT_TRUE(GetOriginsForType(&client, kTemp).empty()); |
| 184 EXPECT_TRUE(GetOriginsForType(&client, kPerm).empty()); | 198 EXPECT_TRUE(GetOriginsForType(&client, kPerm).empty()); |
| 185 | 199 |
| 186 AddFakeIndexedDB(kOriginA, 1000); | 200 AddFakeIndexedDB(kOriginA, 1000); |
| 187 std::set<GURL> origins = GetOriginsForType(&client, kTemp); | 201 std::set<GURL> origins = GetOriginsForType(&client, kTemp); |
| 188 EXPECT_EQ(origins.size(), 1ul); | 202 EXPECT_EQ(origins.size(), 1ul); |
| 189 EXPECT_TRUE(origins.find(kOriginA) != origins.end()); | 203 EXPECT_TRUE(origins.find(kOriginA) != origins.end()); |
| 190 | 204 |
| 191 EXPECT_TRUE(GetOriginsForType(&client, kPerm).empty()); | 205 EXPECT_TRUE(GetOriginsForType(&client, kPerm).empty()); |
| 192 } | 206 } |
| 207 |
| 208 TEST_F(IndexedDBQuotaClientTest, DeleteOrigin) { |
| 209 IndexedDBQuotaClient client( |
| 210 base::MessageLoopProxy::CreateForCurrentThread(), |
| 211 idb_context()); |
| 212 |
| 213 AddFakeIndexedDB(kOriginA, 1000); |
| 214 AddFakeIndexedDB(kOriginB, 50); |
| 215 EXPECT_EQ(1000, GetOriginUsage(&client, kOriginA, kTemp)); |
| 216 EXPECT_EQ(50, GetOriginUsage(&client, kOriginB, kTemp)); |
| 217 |
| 218 quota::QuotaStatusCode delete_status = DeleteOrigin(&client, kOriginA); |
| 219 EXPECT_EQ(quota::kQuotaStatusOk, delete_status); |
| 220 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kTemp)); |
| 221 EXPECT_EQ(50, GetOriginUsage(&client, kOriginB, kTemp)); |
| 222 } |
| OLD | NEW |