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 19 matching lines...) Expand all Loading... |
30 const GURL kOriginB; | 30 const GURL kOriginB; |
31 const GURL kOriginOther; | 31 const GURL kOriginOther; |
32 | 32 |
33 IndexedDBQuotaClientTest() | 33 IndexedDBQuotaClientTest() |
34 : kOriginA("http://host"), | 34 : kOriginA("http://host"), |
35 kOriginB("http://host:8000"), | 35 kOriginB("http://host:8000"), |
36 kOriginOther("http://other"), | 36 kOriginOther("http://other"), |
37 usage_(0), | 37 usage_(0), |
38 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | 38 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
39 message_loop_(MessageLoop::TYPE_IO), | 39 message_loop_(MessageLoop::TYPE_IO), |
40 webkit_thread_(BrowserThread::WEBKIT, &message_loop_) { | 40 webkit_thread_(BrowserThread::WEBKIT, &message_loop_), |
| 41 io_thread_(BrowserThread::IO, &message_loop_) { |
41 TestingProfile profile; | 42 TestingProfile profile; |
42 idb_context_ = profile.GetWebKitContext()->indexed_db_context(); | 43 idb_context_ = profile.GetWebKitContext()->indexed_db_context(); |
43 setup_temp_dir(); | 44 setup_temp_dir(); |
44 } | 45 } |
45 void setup_temp_dir() { | 46 void setup_temp_dir() { |
46 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 47 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
47 FilePath indexeddb_dir = temp_dir_.path().Append( | 48 FilePath indexeddb_dir = temp_dir_.path().Append( |
48 IndexedDBContext::kIndexedDBDirectory); | 49 IndexedDBContext::kIndexedDBDirectory); |
49 ASSERT_TRUE(file_util::CreateDirectory(indexeddb_dir)); | 50 ASSERT_TRUE(file_util::CreateDirectory(indexeddb_dir)); |
50 idb_context()->set_data_path(indexeddb_dir); | 51 idb_context()->set_data_path(indexeddb_dir); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 quota::StorageType type, | 88 quota::StorageType type, |
88 const std::string& host) { | 89 const std::string& host) { |
89 origins_.clear(); | 90 origins_.clear(); |
90 client->GetOriginsForHost(type, host, | 91 client->GetOriginsForHost(type, host, |
91 callback_factory_.NewCallback( | 92 callback_factory_.NewCallback( |
92 &IndexedDBQuotaClientTest::OnGetOriginsComplete)); | 93 &IndexedDBQuotaClientTest::OnGetOriginsComplete)); |
93 MessageLoop::current()->RunAllPending(); | 94 MessageLoop::current()->RunAllPending(); |
94 return origins_; | 95 return origins_; |
95 } | 96 } |
96 | 97 |
| 98 quota::QuotaStatusCode DeleteOrigin(quota::QuotaClient* client, |
| 99 const GURL& origin_url) { |
| 100 delete_status_ = quota::kQuotaStatusUnknown; |
| 101 client->DeleteOriginData(origin_url, kTemp, callback_factory_.NewCallback( |
| 102 &IndexedDBQuotaClientTest::OnDeleteOriginComplete)); |
| 103 MessageLoop::current()->RunAllPending(); |
| 104 return delete_status_; |
| 105 } |
| 106 |
97 IndexedDBContext* idb_context() { return idb_context_.get(); } | 107 IndexedDBContext* idb_context() { return idb_context_.get(); } |
98 | 108 |
99 void SetFileSizeTo(const FilePath& path, int size) { | 109 void SetFileSizeTo(const FilePath& path, int size) { |
100 std::string junk(size, 'a'); | 110 std::string junk(size, 'a'); |
101 ASSERT_EQ(size, file_util::WriteFile(path, junk.c_str(), size)); | 111 ASSERT_EQ(size, file_util::WriteFile(path, junk.c_str(), size)); |
102 } | 112 } |
103 | 113 |
104 void AddFakeIndexedDB(const GURL& origin, int size) { | 114 void AddFakeIndexedDB(const GURL& origin, int size) { |
105 FilePath file_path_origin = idb_context()->GetIndexedDBFilePath( | 115 FilePath file_path_origin = idb_context()->GetIndexedDBFilePath( |
106 DatabaseUtil::GetOriginIdentifier(origin)); | 116 DatabaseUtil::GetOriginIdentifier(origin)); |
107 if (!file_util::CreateDirectory(file_path_origin)) { | 117 if (!file_util::CreateDirectory(file_path_origin)) { |
108 LOG(ERROR) << "failed to file_util::CreateDirectory " | 118 LOG(ERROR) << "failed to file_util::CreateDirectory " |
109 << file_path_origin.value(); | 119 << file_path_origin.value(); |
110 } | 120 } |
111 file_path_origin = file_path_origin.Append(FILE_PATH_LITERAL("fake_file")); | 121 file_path_origin = file_path_origin.Append(FILE_PATH_LITERAL("fake_file")); |
112 SetFileSizeTo(file_path_origin, size); | 122 SetFileSizeTo(file_path_origin, size); |
113 } | 123 } |
114 | 124 |
115 private: | 125 private: |
116 void OnGetOriginUsageComplete(int64 usage) { | 126 void OnGetOriginUsageComplete(int64 usage) { |
117 usage_ = usage; | 127 usage_ = usage; |
118 } | 128 } |
119 | 129 |
120 void OnGetOriginsComplete(const std::set<GURL>& origins) { | 130 void OnGetOriginsComplete(const std::set<GURL>& origins) { |
121 origins_ = origins; | 131 origins_ = origins; |
122 } | 132 } |
123 | 133 |
| 134 void OnDeleteOriginComplete(quota::QuotaStatusCode code) { |
| 135 delete_status_ = code; |
| 136 } |
| 137 |
124 ScopedTempDir temp_dir_; | 138 ScopedTempDir temp_dir_; |
125 int64 usage_; | 139 int64 usage_; |
126 std::set<GURL> origins_; | 140 std::set<GURL> origins_; |
127 scoped_refptr<IndexedDBContext> idb_context_; | 141 scoped_refptr<IndexedDBContext> idb_context_; |
128 base::ScopedCallbackFactory<IndexedDBQuotaClientTest> callback_factory_; | 142 base::ScopedCallbackFactory<IndexedDBQuotaClientTest> callback_factory_; |
129 MessageLoop message_loop_; | 143 MessageLoop message_loop_; |
130 BrowserThread webkit_thread_; | 144 BrowserThread webkit_thread_; |
| 145 BrowserThread io_thread_; |
| 146 quota::QuotaStatusCode delete_status_; |
131 }; | 147 }; |
132 | 148 |
133 | 149 |
134 TEST_F(IndexedDBQuotaClientTest, GetOriginUsage) { | 150 TEST_F(IndexedDBQuotaClientTest, GetOriginUsage) { |
135 IndexedDBQuotaClient client( | 151 IndexedDBQuotaClient client( |
136 base::MessageLoopProxy::CreateForCurrentThread(), | 152 base::MessageLoopProxy::CreateForCurrentThread(), |
137 idb_context()); | 153 idb_context()); |
138 | 154 |
139 AddFakeIndexedDB(kOriginA, 6); | 155 AddFakeIndexedDB(kOriginA, 6); |
140 AddFakeIndexedDB(kOriginB, 3); | 156 AddFakeIndexedDB(kOriginB, 3); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 EXPECT_TRUE(GetOriginsForType(&client, kTemp).empty()); | 200 EXPECT_TRUE(GetOriginsForType(&client, kTemp).empty()); |
185 EXPECT_TRUE(GetOriginsForType(&client, kPerm).empty()); | 201 EXPECT_TRUE(GetOriginsForType(&client, kPerm).empty()); |
186 | 202 |
187 AddFakeIndexedDB(kOriginA, 1000); | 203 AddFakeIndexedDB(kOriginA, 1000); |
188 std::set<GURL> origins = GetOriginsForType(&client, kTemp); | 204 std::set<GURL> origins = GetOriginsForType(&client, kTemp); |
189 EXPECT_EQ(origins.size(), 1ul); | 205 EXPECT_EQ(origins.size(), 1ul); |
190 EXPECT_TRUE(origins.find(kOriginA) != origins.end()); | 206 EXPECT_TRUE(origins.find(kOriginA) != origins.end()); |
191 | 207 |
192 EXPECT_TRUE(GetOriginsForType(&client, kPerm).empty()); | 208 EXPECT_TRUE(GetOriginsForType(&client, kPerm).empty()); |
193 } | 209 } |
| 210 |
| 211 TEST_F(IndexedDBQuotaClientTest, DeleteOrigin) { |
| 212 IndexedDBQuotaClient client( |
| 213 base::MessageLoopProxy::CreateForCurrentThread(), |
| 214 idb_context()); |
| 215 |
| 216 AddFakeIndexedDB(kOriginA, 1000); |
| 217 AddFakeIndexedDB(kOriginB, 50); |
| 218 EXPECT_EQ(1000, GetOriginUsage(&client, kOriginA, kTemp)); |
| 219 EXPECT_EQ(50, GetOriginUsage(&client, kOriginB, kTemp)); |
| 220 |
| 221 quota::QuotaStatusCode delete_status = DeleteOrigin(&client, kOriginA); |
| 222 EXPECT_EQ(quota::kQuotaStatusOk, delete_status); |
| 223 EXPECT_EQ(0, GetOriginUsage(&client, kOriginA, kTemp)); |
| 224 EXPECT_EQ(50, GetOriginUsage(&client, kOriginB, kTemp)); |
| 225 } |
OLD | NEW |