| 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/bind.h" |
| 7 #include "base/file_path.h" | 8 #include "base/file_path.h" |
| 8 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 9 #include "base/memory/scoped_callback_factory.h" | 10 #include "base/memory/scoped_callback_factory.h" |
| 10 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 11 #include "base/message_loop_proxy.h" | 12 #include "base/message_loop_proxy.h" |
| 12 #include "base/scoped_temp_dir.h" | 13 #include "base/scoped_temp_dir.h" |
| 13 #include "content/browser/in_process_webkit/indexed_db_context.h" | 14 #include "content/browser/in_process_webkit/indexed_db_context.h" |
| 14 #include "content/browser/in_process_webkit/indexed_db_quota_client.h" | 15 #include "content/browser/in_process_webkit/indexed_db_quota_client.h" |
| 15 #include "content/browser/in_process_webkit/webkit_context.h" | 16 #include "content/browser/in_process_webkit/webkit_context.h" |
| 16 #include "content/test/test_browser_context.h" | 17 #include "content/test/test_browser_context.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 28 public: | 29 public: |
| 29 const GURL kOriginA; | 30 const GURL kOriginA; |
| 30 const GURL kOriginB; | 31 const GURL kOriginB; |
| 31 const GURL kOriginOther; | 32 const GURL kOriginOther; |
| 32 | 33 |
| 33 IndexedDBQuotaClientTest() | 34 IndexedDBQuotaClientTest() |
| 34 : kOriginA("http://host"), | 35 : kOriginA("http://host"), |
| 35 kOriginB("http://host:8000"), | 36 kOriginB("http://host:8000"), |
| 36 kOriginOther("http://other"), | 37 kOriginOther("http://other"), |
| 37 usage_(0), | 38 usage_(0), |
| 38 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | 39 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 39 message_loop_(MessageLoop::TYPE_IO), | 40 message_loop_(MessageLoop::TYPE_IO), |
| 40 webkit_thread_(BrowserThread::WEBKIT, &message_loop_), | 41 webkit_thread_(BrowserThread::WEBKIT, &message_loop_), |
| 41 io_thread_(BrowserThread::IO, &message_loop_) { | 42 io_thread_(BrowserThread::IO, &message_loop_) { |
| 42 TestBrowserContext browser_context; | 43 TestBrowserContext browser_context; |
| 43 idb_context_ = browser_context.GetWebKitContext()->indexed_db_context(); | 44 idb_context_ = browser_context.GetWebKitContext()->indexed_db_context(); |
| 44 setup_temp_dir(); | 45 setup_temp_dir(); |
| 45 } | 46 } |
| 46 void setup_temp_dir() { | 47 void setup_temp_dir() { |
| 47 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 48 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 48 FilePath indexeddb_dir = temp_dir_.path().Append( | 49 FilePath indexeddb_dir = temp_dir_.path().Append( |
| 49 IndexedDBContext::kIndexedDBDirectory); | 50 IndexedDBContext::kIndexedDBDirectory); |
| 50 ASSERT_TRUE(file_util::CreateDirectory(indexeddb_dir)); | 51 ASSERT_TRUE(file_util::CreateDirectory(indexeddb_dir)); |
| 51 idb_context()->set_data_path_for_testing(indexeddb_dir); | 52 idb_context()->set_data_path_for_testing(indexeddb_dir); |
| 52 } | 53 } |
| 53 | 54 |
| 54 ~IndexedDBQuotaClientTest() { | 55 ~IndexedDBQuotaClientTest() { |
| 55 // IndexedDBContext needs to be destructed on BrowserThread::WEBKIT, which | 56 // IndexedDBContext needs to be destructed on BrowserThread::WEBKIT, which |
| 56 // is also a member variable of this class. Cause IndexedDBContext's | 57 // is also a member variable of this class. Cause IndexedDBContext's |
| 57 // destruction now to ensure that it doesn't outlive BrowserThread::WEBKIT. | 58 // destruction now to ensure that it doesn't outlive BrowserThread::WEBKIT. |
| 58 idb_context_ = NULL; | 59 idb_context_ = NULL; |
| 59 MessageLoop::current()->RunAllPending(); | 60 MessageLoop::current()->RunAllPending(); |
| 60 } | 61 } |
| 61 | 62 |
| 62 int64 GetOriginUsage( | 63 int64 GetOriginUsage( |
| 63 quota::QuotaClient* client, | 64 quota::QuotaClient* client, |
| 64 const GURL& origin, | 65 const GURL& origin, |
| 65 quota::StorageType type) { | 66 quota::StorageType type) { |
| 66 usage_ = -1; | 67 usage_ = -1; |
| 67 client->GetOriginUsage(origin, type, | 68 client->GetOriginUsage( |
| 68 callback_factory_.NewCallback( | 69 origin, type, |
| 69 &IndexedDBQuotaClientTest::OnGetOriginUsageComplete)); | 70 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginUsageComplete, |
| 71 weak_factory_.GetWeakPtr())); |
| 70 MessageLoop::current()->RunAllPending(); | 72 MessageLoop::current()->RunAllPending(); |
| 71 EXPECT_GT(usage_, -1); | 73 EXPECT_GT(usage_, -1); |
| 72 return usage_; | 74 return usage_; |
| 73 } | 75 } |
| 74 | 76 |
| 75 const std::set<GURL>& GetOriginsForType( | 77 const std::set<GURL>& GetOriginsForType( |
| 76 quota::QuotaClient* client, | 78 quota::QuotaClient* client, |
| 77 quota::StorageType type) { | 79 quota::StorageType type) { |
| 78 origins_.clear(); | 80 origins_.clear(); |
| 79 type_ = quota::kStorageTypeTemporary; | 81 type_ = quota::kStorageTypeTemporary; |
| 80 client->GetOriginsForType(type, | 82 client->GetOriginsForType( |
| 81 callback_factory_.NewCallback( | 83 type, |
| 82 &IndexedDBQuotaClientTest::OnGetOriginsComplete)); | 84 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginsComplete, |
| 85 weak_factory_.GetWeakPtr())); |
| 83 MessageLoop::current()->RunAllPending(); | 86 MessageLoop::current()->RunAllPending(); |
| 84 return origins_; | 87 return origins_; |
| 85 } | 88 } |
| 86 | 89 |
| 87 const std::set<GURL>& GetOriginsForHost( | 90 const std::set<GURL>& GetOriginsForHost( |
| 88 quota::QuotaClient* client, | 91 quota::QuotaClient* client, |
| 89 quota::StorageType type, | 92 quota::StorageType type, |
| 90 const std::string& host) { | 93 const std::string& host) { |
| 91 origins_.clear(); | 94 origins_.clear(); |
| 92 type_ = quota::kStorageTypeTemporary; | 95 type_ = quota::kStorageTypeTemporary; |
| 93 client->GetOriginsForHost(type, host, | 96 client->GetOriginsForHost( |
| 94 callback_factory_.NewCallback( | 97 type, host, |
| 95 &IndexedDBQuotaClientTest::OnGetOriginsComplete)); | 98 base::Bind(&IndexedDBQuotaClientTest::OnGetOriginsComplete, |
| 99 weak_factory_.GetWeakPtr())); |
| 96 MessageLoop::current()->RunAllPending(); | 100 MessageLoop::current()->RunAllPending(); |
| 97 return origins_; | 101 return origins_; |
| 98 } | 102 } |
| 99 | 103 |
| 100 quota::QuotaStatusCode DeleteOrigin(quota::QuotaClient* client, | 104 quota::QuotaStatusCode DeleteOrigin(quota::QuotaClient* client, |
| 101 const GURL& origin_url) { | 105 const GURL& origin_url) { |
| 102 delete_status_ = quota::kQuotaStatusUnknown; | 106 delete_status_ = quota::kQuotaStatusUnknown; |
| 103 client->DeleteOriginData(origin_url, kTemp, callback_factory_.NewCallback( | 107 client->DeleteOriginData( |
| 104 &IndexedDBQuotaClientTest::OnDeleteOriginComplete)); | 108 origin_url, kTemp, |
| 109 base::Bind(&IndexedDBQuotaClientTest::OnDeleteOriginComplete, |
| 110 weak_factory_.GetWeakPtr())); |
| 105 MessageLoop::current()->RunAllPending(); | 111 MessageLoop::current()->RunAllPending(); |
| 106 return delete_status_; | 112 return delete_status_; |
| 107 } | 113 } |
| 108 | 114 |
| 109 IndexedDBContext* idb_context() { return idb_context_.get(); } | 115 IndexedDBContext* idb_context() { return idb_context_.get(); } |
| 110 | 116 |
| 111 void SetFileSizeTo(const FilePath& path, int size) { | 117 void SetFileSizeTo(const FilePath& path, int size) { |
| 112 std::string junk(size, 'a'); | 118 std::string junk(size, 'a'); |
| 113 ASSERT_EQ(size, file_util::WriteFile(path, junk.c_str(), size)); | 119 ASSERT_EQ(size, file_util::WriteFile(path, junk.c_str(), size)); |
| 114 } | 120 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 138 | 144 |
| 139 void OnDeleteOriginComplete(quota::QuotaStatusCode code) { | 145 void OnDeleteOriginComplete(quota::QuotaStatusCode code) { |
| 140 delete_status_ = code; | 146 delete_status_ = code; |
| 141 } | 147 } |
| 142 | 148 |
| 143 ScopedTempDir temp_dir_; | 149 ScopedTempDir temp_dir_; |
| 144 int64 usage_; | 150 int64 usage_; |
| 145 std::set<GURL> origins_; | 151 std::set<GURL> origins_; |
| 146 quota::StorageType type_; | 152 quota::StorageType type_; |
| 147 scoped_refptr<IndexedDBContext> idb_context_; | 153 scoped_refptr<IndexedDBContext> idb_context_; |
| 148 base::ScopedCallbackFactory<IndexedDBQuotaClientTest> callback_factory_; | 154 base::WeakPtrFactory<IndexedDBQuotaClientTest> weak_factory_; |
| 149 MessageLoop message_loop_; | 155 MessageLoop message_loop_; |
| 150 BrowserThread webkit_thread_; | 156 BrowserThread webkit_thread_; |
| 151 BrowserThread io_thread_; | 157 BrowserThread io_thread_; |
| 152 quota::QuotaStatusCode delete_status_; | 158 quota::QuotaStatusCode delete_status_; |
| 153 }; | 159 }; |
| 154 | 160 |
| 155 | 161 |
| 156 TEST_F(IndexedDBQuotaClientTest, GetOriginUsage) { | 162 TEST_F(IndexedDBQuotaClientTest, GetOriginUsage) { |
| 157 IndexedDBQuotaClient client( | 163 IndexedDBQuotaClient client( |
| 158 base::MessageLoopProxy::current(), | 164 base::MessageLoopProxy::current(), |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 222 AddFakeIndexedDB(kOriginA, 1000); | 228 AddFakeIndexedDB(kOriginA, 1000); |
| 223 AddFakeIndexedDB(kOriginB, 50); | 229 AddFakeIndexedDB(kOriginB, 50); |
| 224 EXPECT_EQ(1000, GetOriginUsage(&client, kOriginA, kTemp)); | 230 EXPECT_EQ(1000, GetOriginUsage(&client, kOriginA, kTemp)); |
| 225 EXPECT_EQ(50, GetOriginUsage(&client, kOriginB, kTemp)); | 231 EXPECT_EQ(50, GetOriginUsage(&client, kOriginB, kTemp)); |
| 226 | 232 |
| 227 quota::QuotaStatusCode delete_status = DeleteOrigin(&client, kOriginA); | 233 quota::QuotaStatusCode delete_status = DeleteOrigin(&client, kOriginA); |
| 228 EXPECT_EQ(quota::kQuotaStatusOk, delete_status); | 234 EXPECT_EQ(quota::kQuotaStatusOk, delete_status); |
| 229 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kTemp)); | 235 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kTemp)); |
| 230 EXPECT_EQ(50, GetOriginUsage(&client, kOriginB, kTemp)); | 236 EXPECT_EQ(50, GetOriginUsage(&client, kOriginB, kTemp)); |
| 231 } | 237 } |
| OLD | NEW |