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