Remove PlatformFile from profile_browsertest
[chromium-blink-merge.git] / content / browser / storage_partition_impl_unittest.cc
blobbc2dca85d940f0e913821ac815360e55a8e034a9
1 // Copyright 2013 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.
5 #include "base/file_util.h"
6 #include "base/message_loop/message_loop_proxy.h"
7 #include "base/run_loop.h"
8 #include "base/threading/thread.h"
9 #include "content/browser/browser_thread_impl.h"
10 #include "content/browser/gpu/shader_disk_cache.h"
11 #include "content/browser/storage_partition_impl.h"
12 #include "content/public/browser/local_storage_usage_info.h"
13 #include "content/public/browser/storage_partition.h"
14 #include "content/public/test/test_browser_context.h"
15 #include "content/public/test/test_browser_thread.h"
16 #include "content/public/test/test_browser_thread_bundle.h"
17 #include "net/base/test_completion_callback.h"
18 #include "net/cookies/cookie_monster.h"
19 #include "net/url_request/url_request_context.h"
20 #include "net/url_request/url_request_context_getter.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "webkit/browser/quota/mock_quota_manager.h"
23 #include "webkit/browser/quota/mock_special_storage_policy.h"
24 #include "webkit/browser/quota/quota_manager.h"
26 namespace content {
27 namespace {
29 const int kDefaultClientId = 42;
30 const char kCacheKey[] = "key";
31 const char kCacheValue[] = "cached value";
33 const char kTestOrigin1[] = "http://host1:1/";
34 const char kTestOrigin2[] = "http://host2:1/";
35 const char kTestOrigin3[] = "http://host3:1/";
36 const char kTestOriginDevTools[] = "chrome-devtools://abcdefghijklmnopqrstuvw/";
38 const GURL kOrigin1(kTestOrigin1);
39 const GURL kOrigin2(kTestOrigin2);
40 const GURL kOrigin3(kTestOrigin3);
41 const GURL kOriginDevTools(kTestOriginDevTools);
43 const base::FilePath::CharType kDomStorageOrigin1[] =
44 FILE_PATH_LITERAL("http_host1_1.localstorage");
46 const base::FilePath::CharType kDomStorageOrigin2[] =
47 FILE_PATH_LITERAL("http_host2_1.localstorage");
49 const base::FilePath::CharType kDomStorageOrigin3[] =
50 FILE_PATH_LITERAL("http_host3_1.localstorage");
52 const quota::StorageType kTemporary = quota::kStorageTypeTemporary;
53 const quota::StorageType kPersistent = quota::kStorageTypePersistent;
55 const quota::QuotaClient::ID kClientFile = quota::QuotaClient::kFileSystem;
57 const uint32 kAllQuotaRemoveMask =
58 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB |
59 StoragePartition::REMOVE_DATA_MASK_WEBSQL |
60 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS |
61 StoragePartition::REMOVE_DATA_MASK_APPCACHE;
63 class AwaitCompletionHelper {
64 public:
65 AwaitCompletionHelper() : start_(false), already_quit_(false) {}
66 virtual ~AwaitCompletionHelper() {}
68 void BlockUntilNotified() {
69 if (!already_quit_) {
70 DCHECK(!start_);
71 start_ = true;
72 base::MessageLoop::current()->Run();
73 } else {
74 DCHECK(!start_);
75 already_quit_ = false;
79 void Notify() {
80 if (start_) {
81 DCHECK(!already_quit_);
82 base::MessageLoop::current()->Quit();
83 start_ = false;
84 } else {
85 DCHECK(!already_quit_);
86 already_quit_ = true;
90 private:
91 // Helps prevent from running message_loop, if the callback invoked
92 // immediately.
93 bool start_;
94 bool already_quit_;
96 DISALLOW_COPY_AND_ASSIGN(AwaitCompletionHelper);
99 class RemoveCookieTester {
100 public:
101 explicit RemoveCookieTester(TestBrowserContext* context)
102 : get_cookie_success_(false), monster_(NULL) {
103 SetMonster(context->GetRequestContext()->GetURLRequestContext()->
104 cookie_store()->GetCookieMonster());
107 // Returns true, if the given cookie exists in the cookie store.
108 bool ContainsCookie() {
109 get_cookie_success_ = false;
110 monster_->GetCookiesWithOptionsAsync(
111 kOrigin1, net::CookieOptions(),
112 base::Bind(&RemoveCookieTester::GetCookieCallback,
113 base::Unretained(this)));
114 await_completion_.BlockUntilNotified();
115 return get_cookie_success_;
118 void AddCookie() {
119 monster_->SetCookieWithOptionsAsync(
120 kOrigin1, "A=1", net::CookieOptions(),
121 base::Bind(&RemoveCookieTester::SetCookieCallback,
122 base::Unretained(this)));
123 await_completion_.BlockUntilNotified();
126 protected:
127 void SetMonster(net::CookieStore* monster) {
128 monster_ = monster;
131 private:
132 void GetCookieCallback(const std::string& cookies) {
133 if (cookies == "A=1") {
134 get_cookie_success_ = true;
135 } else {
136 EXPECT_EQ("", cookies);
137 get_cookie_success_ = false;
139 await_completion_.Notify();
142 void SetCookieCallback(bool result) {
143 ASSERT_TRUE(result);
144 await_completion_.Notify();
147 bool get_cookie_success_;
148 AwaitCompletionHelper await_completion_;
149 net::CookieStore* monster_;
151 DISALLOW_COPY_AND_ASSIGN(RemoveCookieTester);
154 class RemoveLocalStorageTester {
155 public:
156 explicit RemoveLocalStorageTester(TestBrowserContext* profile)
157 : profile_(profile), dom_storage_context_(NULL) {
158 dom_storage_context_ =
159 content::BrowserContext::GetDefaultStoragePartition(profile)->
160 GetDOMStorageContext();
163 // Returns true, if the given origin URL exists.
164 bool DOMStorageExistsForOrigin(const GURL& origin) {
165 GetLocalStorageUsage();
166 await_completion_.BlockUntilNotified();
167 for (size_t i = 0; i < infos_.size(); ++i) {
168 if (origin == infos_[i].origin)
169 return true;
171 return false;
174 void AddDOMStorageTestData() {
175 // Note: This test depends on details of how the dom_storage library
176 // stores data in the host file system.
177 base::FilePath storage_path =
178 profile_->GetPath().AppendASCII("Local Storage");
179 base::CreateDirectory(storage_path);
181 // Write some files.
182 base::WriteFile(storage_path.Append(kDomStorageOrigin1), NULL, 0);
183 base::WriteFile(storage_path.Append(kDomStorageOrigin2), NULL, 0);
184 base::WriteFile(storage_path.Append(kDomStorageOrigin3), NULL, 0);
186 // Tweak their dates.
187 base::Time now = base::Time::Now();
188 base::TouchFile(storage_path.Append(kDomStorageOrigin1), now, now);
190 base::Time one_day_ago = now - base::TimeDelta::FromDays(1);
191 base::TouchFile(storage_path.Append(kDomStorageOrigin2),
192 one_day_ago, one_day_ago);
194 base::Time sixty_days_ago = now - base::TimeDelta::FromDays(60);
195 base::TouchFile(storage_path.Append(kDomStorageOrigin3),
196 sixty_days_ago, sixty_days_ago);
199 private:
200 void GetLocalStorageUsage() {
201 dom_storage_context_->GetLocalStorageUsage(
202 base::Bind(&RemoveLocalStorageTester::OnGotLocalStorageUsage,
203 base::Unretained(this)));
205 void OnGotLocalStorageUsage(
206 const std::vector<content::LocalStorageUsageInfo>& infos) {
207 infos_ = infos;
208 await_completion_.Notify();
211 // We don't own these pointers.
212 TestBrowserContext* profile_;
213 content::DOMStorageContext* dom_storage_context_;
215 std::vector<content::LocalStorageUsageInfo> infos_;
217 AwaitCompletionHelper await_completion_;
219 DISALLOW_COPY_AND_ASSIGN(RemoveLocalStorageTester);
222 bool IsWebSafeSchemeForTest(const std::string& scheme) {
223 return scheme == "http";
226 bool DoesOriginMatchForUnprotectedWeb(
227 const GURL& origin,
228 quota::SpecialStoragePolicy* special_storage_policy) {
229 if (IsWebSafeSchemeForTest(origin.scheme()))
230 return !special_storage_policy->IsStorageProtected(origin.GetOrigin());
232 return false;
235 bool DoesOriginMatchForBothProtectedAndUnprotectedWeb(
236 const GURL& origin,
237 quota::SpecialStoragePolicy* special_storage_policy) {
238 return true;
241 bool DoesOriginMatchUnprotected(
242 const GURL& origin,
243 quota::SpecialStoragePolicy* special_storage_policy) {
244 return origin.GetOrigin().scheme() != kOriginDevTools.scheme();
247 void ClearQuotaData(content::StoragePartition* partition,
248 base::RunLoop* loop_to_quit) {
249 partition->ClearData(
250 kAllQuotaRemoveMask,
251 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL,
252 GURL(), StoragePartition::OriginMatcherFunction(),
253 base::Time(), base::Time::Max(), loop_to_quit->QuitClosure());
256 void ClearQuotaDataWithOriginMatcher(
257 content::StoragePartition* partition,
258 const GURL& remove_origin,
259 const StoragePartition::OriginMatcherFunction& origin_matcher,
260 const base::Time delete_begin,
261 base::RunLoop* loop_to_quit) {
262 partition->ClearData(kAllQuotaRemoveMask,
263 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL,
264 remove_origin, origin_matcher, delete_begin,
265 base::Time::Max(), loop_to_quit->QuitClosure());
268 void ClearQuotaDataForOrigin(
269 content::StoragePartition* partition,
270 const GURL& remove_origin,
271 const base::Time delete_begin,
272 base::RunLoop* loop_to_quit) {
273 ClearQuotaDataWithOriginMatcher(
274 partition, remove_origin,
275 StoragePartition::OriginMatcherFunction(), delete_begin,
276 loop_to_quit);
279 void ClearQuotaDataForNonPersistent(
280 content::StoragePartition* partition,
281 const base::Time delete_begin,
282 base::RunLoop* loop_to_quit) {
283 uint32 quota_storage_remove_mask_no_persistent =
284 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL &
285 ~StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT;
286 partition->ClearData(
287 kAllQuotaRemoveMask, quota_storage_remove_mask_no_persistent,
288 GURL(), StoragePartition::OriginMatcherFunction(),
289 delete_begin, base::Time::Max(), loop_to_quit->QuitClosure());
292 void ClearCookies(content::StoragePartition* partition,
293 const base::Time delete_begin,
294 const base::Time delete_end,
295 base::RunLoop* run_loop) {
296 partition->ClearData(
297 StoragePartition::REMOVE_DATA_MASK_COOKIES,
298 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL,
299 GURL(), StoragePartition::OriginMatcherFunction(),
300 delete_begin, delete_end, run_loop->QuitClosure());
303 void ClearStuff(uint32 remove_mask,
304 content::StoragePartition* partition,
305 const base::Time delete_begin,
306 const base::Time delete_end,
307 const StoragePartition::OriginMatcherFunction& origin_matcher,
308 base::RunLoop* run_loop) {
309 partition->ClearData(
310 remove_mask, StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL,
311 GURL(), origin_matcher, delete_begin, delete_end,
312 run_loop->QuitClosure());
315 void ClearData(content::StoragePartition* partition,
316 base::RunLoop* run_loop) {
317 base::Time time;
318 partition->ClearData(
319 StoragePartition::REMOVE_DATA_MASK_SHADER_CACHE,
320 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL,
321 GURL(), StoragePartition::OriginMatcherFunction(),
322 time, time, run_loop->QuitClosure());
325 } // namespace
327 class StoragePartitionImplTest : public testing::Test {
328 public:
329 StoragePartitionImplTest()
330 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
331 browser_context_(new TestBrowserContext()) {
334 quota::MockQuotaManager* GetMockManager() {
335 if (!quota_manager_.get()) {
336 quota_manager_ = new quota::MockQuotaManager(
337 browser_context_->IsOffTheRecord(),
338 browser_context_->GetPath(),
339 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO).get(),
340 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB).get(),
341 browser_context_->GetSpecialStoragePolicy());
343 return quota_manager_.get();
346 TestBrowserContext* browser_context() {
347 return browser_context_.get();
350 private:
351 content::TestBrowserThreadBundle thread_bundle_;
352 scoped_ptr<TestBrowserContext> browser_context_;
353 scoped_refptr<quota::MockQuotaManager> quota_manager_;
355 DISALLOW_COPY_AND_ASSIGN(StoragePartitionImplTest);
358 class StoragePartitionShaderClearTest : public testing::Test {
359 public:
360 StoragePartitionShaderClearTest()
361 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
362 browser_context_(new TestBrowserContext()) {
363 ShaderCacheFactory::GetInstance()->SetCacheInfo(
364 kDefaultClientId,
365 BrowserContext::GetDefaultStoragePartition(
366 browser_context())->GetPath());
367 cache_ = ShaderCacheFactory::GetInstance()->Get(kDefaultClientId);
370 virtual ~StoragePartitionShaderClearTest() {
371 cache_ = NULL;
372 ShaderCacheFactory::GetInstance()->RemoveCacheInfo(kDefaultClientId);
375 void InitCache() {
376 net::TestCompletionCallback available_cb;
377 int rv = cache_->SetAvailableCallback(available_cb.callback());
378 ASSERT_EQ(net::OK, available_cb.GetResult(rv));
379 EXPECT_EQ(0, cache_->Size());
381 cache_->Cache(kCacheKey, kCacheValue);
383 net::TestCompletionCallback complete_cb;
385 rv = cache_->SetCacheCompleteCallback(complete_cb.callback());
386 ASSERT_EQ(net::OK, complete_cb.GetResult(rv));
389 size_t Size() { return cache_->Size(); }
391 TestBrowserContext* browser_context() {
392 return browser_context_.get();
395 private:
396 content::TestBrowserThreadBundle thread_bundle_;
397 scoped_ptr<TestBrowserContext> browser_context_;
399 scoped_refptr<ShaderDiskCache> cache_;
402 // Tests ---------------------------------------------------------------------
404 TEST_F(StoragePartitionShaderClearTest, ClearShaderCache) {
405 InitCache();
406 EXPECT_EQ(1u, Size());
408 base::RunLoop run_loop;
409 base::MessageLoop::current()->PostTask(
410 FROM_HERE, base::Bind(
411 &ClearData,
412 BrowserContext::GetDefaultStoragePartition(browser_context()),
413 &run_loop));
414 run_loop.Run();
415 EXPECT_EQ(0u, Size());
418 TEST_F(StoragePartitionImplTest, QuotaClientMaskGeneration) {
419 EXPECT_EQ(quota::QuotaClient::kFileSystem,
420 StoragePartitionImpl::GenerateQuotaClientMask(
421 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS));
422 EXPECT_EQ(quota::QuotaClient::kDatabase,
423 StoragePartitionImpl::GenerateQuotaClientMask(
424 StoragePartition::REMOVE_DATA_MASK_WEBSQL));
425 EXPECT_EQ(quota::QuotaClient::kAppcache,
426 StoragePartitionImpl::GenerateQuotaClientMask(
427 StoragePartition::REMOVE_DATA_MASK_APPCACHE));
428 EXPECT_EQ(quota::QuotaClient::kIndexedDatabase,
429 StoragePartitionImpl::GenerateQuotaClientMask(
430 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB));
431 EXPECT_EQ(quota::QuotaClient::kFileSystem |
432 quota::QuotaClient::kDatabase |
433 quota::QuotaClient::kAppcache |
434 quota::QuotaClient::kIndexedDatabase,
435 StoragePartitionImpl::GenerateQuotaClientMask(
436 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS |
437 StoragePartition::REMOVE_DATA_MASK_WEBSQL |
438 StoragePartition::REMOVE_DATA_MASK_APPCACHE |
439 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB));
442 void PopulateTestQuotaManagedPersistentData(quota::MockQuotaManager* manager) {
443 manager->AddOrigin(kOrigin2, kPersistent, kClientFile, base::Time());
444 manager->AddOrigin(kOrigin3, kPersistent, kClientFile,
445 base::Time::Now() - base::TimeDelta::FromDays(1));
447 EXPECT_FALSE(manager->OriginHasData(kOrigin1, kPersistent, kClientFile));
448 EXPECT_TRUE(manager->OriginHasData(kOrigin2, kPersistent, kClientFile));
449 EXPECT_TRUE(manager->OriginHasData(kOrigin3, kPersistent, kClientFile));
452 void PopulateTestQuotaManagedTemporaryData(quota::MockQuotaManager* manager) {
453 manager->AddOrigin(kOrigin1, kTemporary, kClientFile, base::Time::Now());
454 manager->AddOrigin(kOrigin3, kTemporary, kClientFile,
455 base::Time::Now() - base::TimeDelta::FromDays(1));
457 EXPECT_TRUE(manager->OriginHasData(kOrigin1, kTemporary, kClientFile));
458 EXPECT_FALSE(manager->OriginHasData(kOrigin2, kTemporary, kClientFile));
459 EXPECT_TRUE(manager->OriginHasData(kOrigin3, kTemporary, kClientFile));
462 void PopulateTestQuotaManagedData(quota::MockQuotaManager* manager) {
463 // Set up kOrigin1 with a temporary quota, kOrigin2 with a persistent
464 // quota, and kOrigin3 with both. kOrigin1 is modified now, kOrigin2
465 // is modified at the beginning of time, and kOrigin3 is modified one day
466 // ago.
467 PopulateTestQuotaManagedPersistentData(manager);
468 PopulateTestQuotaManagedTemporaryData(manager);
471 void PopulateTestQuotaManagedNonBrowsingData(quota::MockQuotaManager* manager) {
472 manager->AddOrigin(kOriginDevTools, kTemporary, kClientFile, base::Time());
473 manager->AddOrigin(kOriginDevTools, kPersistent, kClientFile, base::Time());
476 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedDataForeverBoth) {
477 PopulateTestQuotaManagedData(GetMockManager());
479 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
480 BrowserContext::GetDefaultStoragePartition(browser_context()));
481 partition->OverrideQuotaManagerForTesting(
482 GetMockManager());
484 base::RunLoop run_loop;
485 base::MessageLoop::current()->PostTask(
486 FROM_HERE, base::Bind(&ClearQuotaData, partition, &run_loop));
487 run_loop.Run();
489 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
490 kClientFile));
491 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
492 kClientFile));
493 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
494 kClientFile));
495 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
496 kClientFile));
497 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
498 kClientFile));
499 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
500 kClientFile));
503 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedDataForeverOnlyTemporary) {
504 PopulateTestQuotaManagedTemporaryData(GetMockManager());
506 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
507 BrowserContext::GetDefaultStoragePartition(browser_context()));
508 partition->OverrideQuotaManagerForTesting(
509 GetMockManager());
511 base::RunLoop run_loop;
512 base::MessageLoop::current()->PostTask(
513 FROM_HERE, base::Bind(&ClearQuotaData, partition, &run_loop));
514 run_loop.Run();
516 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
517 kClientFile));
518 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
519 kClientFile));
520 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
521 kClientFile));
522 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
523 kClientFile));
524 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
525 kClientFile));
526 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
527 kClientFile));
530 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedDataForeverOnlyPersistent) {
531 PopulateTestQuotaManagedPersistentData(GetMockManager());
533 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
534 BrowserContext::GetDefaultStoragePartition(browser_context()));
535 partition->OverrideQuotaManagerForTesting(
536 GetMockManager());
538 base::RunLoop run_loop;
539 base::MessageLoop::current()->PostTask(
540 FROM_HERE, base::Bind(&ClearQuotaData, partition, &run_loop));
541 run_loop.Run();
543 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
544 kClientFile));
545 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
546 kClientFile));
547 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
548 kClientFile));
549 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
550 kClientFile));
551 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
552 kClientFile));
553 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
554 kClientFile));
557 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedDataForeverNeither) {
558 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
559 BrowserContext::GetDefaultStoragePartition(browser_context()));
560 partition->OverrideQuotaManagerForTesting(
561 GetMockManager());
563 base::RunLoop run_loop;
564 base::MessageLoop::current()->PostTask(
565 FROM_HERE, base::Bind(&ClearQuotaData, partition, &run_loop));
566 run_loop.Run();
568 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
569 kClientFile));
570 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
571 kClientFile));
572 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
573 kClientFile));
574 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
575 kClientFile));
576 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
577 kClientFile));
578 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
579 kClientFile));
582 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedDataForeverSpecificOrigin) {
583 PopulateTestQuotaManagedData(GetMockManager());
585 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
586 BrowserContext::GetDefaultStoragePartition(browser_context()));
587 partition->OverrideQuotaManagerForTesting(
588 GetMockManager());
590 base::RunLoop run_loop;
591 base::MessageLoop::current()->PostTask(
592 FROM_HERE, base::Bind(&ClearQuotaDataForOrigin,
593 partition, kOrigin1, base::Time(),
594 &run_loop));
595 run_loop.Run();
597 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
598 kClientFile));
599 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
600 kClientFile));
601 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
602 kClientFile));
603 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
604 kClientFile));
605 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
606 kClientFile));
607 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
608 kClientFile));
611 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedDataForLastHour) {
612 PopulateTestQuotaManagedData(GetMockManager());
614 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
615 BrowserContext::GetDefaultStoragePartition(browser_context()));
616 partition->OverrideQuotaManagerForTesting(
617 GetMockManager());
619 base::RunLoop run_loop;
620 base::MessageLoop::current()->PostTask(
621 FROM_HERE, base::Bind(&ClearQuotaDataForOrigin,
622 partition, GURL(),
623 base::Time::Now() - base::TimeDelta::FromHours(1),
624 &run_loop));
625 run_loop.Run();
627 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
628 kClientFile));
629 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
630 kClientFile));
631 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
632 kClientFile));
633 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
634 kClientFile));
635 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
636 kClientFile));
637 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
638 kClientFile));
641 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedDataForLastWeek) {
642 PopulateTestQuotaManagedData(GetMockManager());
644 base::RunLoop run_loop;
645 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
646 BrowserContext::GetDefaultStoragePartition(browser_context()));
647 partition->OverrideQuotaManagerForTesting(
648 GetMockManager());
649 base::MessageLoop::current()->PostTask(
650 FROM_HERE, base::Bind(&ClearQuotaDataForNonPersistent,
651 partition,
652 base::Time::Now() - base::TimeDelta::FromDays(7),
653 &run_loop));
654 run_loop.Run();
656 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
657 kClientFile));
658 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
659 kClientFile));
660 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
661 kClientFile));
662 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
663 kClientFile));
664 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
665 kClientFile));
666 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
667 kClientFile));
670 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedUnprotectedOrigins) {
671 // Protect kOrigin1.
672 scoped_refptr<quota::MockSpecialStoragePolicy> mock_policy =
673 new quota::MockSpecialStoragePolicy;
674 mock_policy->AddProtected(kOrigin1.GetOrigin());
676 PopulateTestQuotaManagedData(GetMockManager());
678 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
679 BrowserContext::GetDefaultStoragePartition(browser_context()));
680 partition->OverrideQuotaManagerForTesting(
681 GetMockManager());
682 partition->OverrideSpecialStoragePolicyForTesting(mock_policy);
684 base::RunLoop run_loop;
685 base::MessageLoop::current()->PostTask(
686 FROM_HERE, base::Bind(&ClearQuotaDataWithOriginMatcher,
687 partition, GURL(),
688 base::Bind(&DoesOriginMatchForUnprotectedWeb),
689 base::Time(), &run_loop));
690 run_loop.Run();
692 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
693 kClientFile));
694 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
695 kClientFile));
696 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
697 kClientFile));
698 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
699 kClientFile));
700 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
701 kClientFile));
702 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
703 kClientFile));
706 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedProtectedSpecificOrigin) {
707 // Protect kOrigin1.
708 scoped_refptr<quota::MockSpecialStoragePolicy> mock_policy =
709 new quota::MockSpecialStoragePolicy;
710 mock_policy->AddProtected(kOrigin1.GetOrigin());
712 PopulateTestQuotaManagedData(GetMockManager());
714 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
715 BrowserContext::GetDefaultStoragePartition(browser_context()));
716 partition->OverrideQuotaManagerForTesting(
717 GetMockManager());
718 partition->OverrideSpecialStoragePolicyForTesting(mock_policy);
720 // Try to remove kOrigin1. Expect failure.
721 base::RunLoop run_loop;
722 base::MessageLoop::current()->PostTask(
723 FROM_HERE, base::Bind(&ClearQuotaDataWithOriginMatcher,
724 partition, kOrigin1,
725 base::Bind(&DoesOriginMatchForUnprotectedWeb),
726 base::Time(), &run_loop));
727 run_loop.Run();
729 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
730 kClientFile));
731 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
732 kClientFile));
733 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
734 kClientFile));
735 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
736 kClientFile));
737 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
738 kClientFile));
739 EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
740 kClientFile));
743 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedProtectedOrigins) {
744 // Protect kOrigin1.
745 scoped_refptr<quota::MockSpecialStoragePolicy> mock_policy =
746 new quota::MockSpecialStoragePolicy;
747 mock_policy->AddProtected(kOrigin1.GetOrigin());
749 PopulateTestQuotaManagedData(GetMockManager());
751 // Try to remove kOrigin1. Expect success.
752 base::RunLoop run_loop;
753 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
754 BrowserContext::GetDefaultStoragePartition(browser_context()));
755 partition->OverrideQuotaManagerForTesting(
756 GetMockManager());
757 partition->OverrideSpecialStoragePolicyForTesting(mock_policy);
758 base::MessageLoop::current()->PostTask(
759 FROM_HERE,
760 base::Bind(&ClearQuotaDataWithOriginMatcher,
761 partition, GURL(),
762 base::Bind(&DoesOriginMatchForBothProtectedAndUnprotectedWeb),
763 base::Time(), &run_loop));
764 run_loop.Run();
766 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary,
767 kClientFile));
768 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary,
769 kClientFile));
770 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary,
771 kClientFile));
772 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent,
773 kClientFile));
774 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kPersistent,
775 kClientFile));
776 EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kPersistent,
777 kClientFile));
780 TEST_F(StoragePartitionImplTest, RemoveQuotaManagedIgnoreDevTools) {
781 PopulateTestQuotaManagedNonBrowsingData(GetMockManager());
783 base::RunLoop run_loop;
784 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
785 BrowserContext::GetDefaultStoragePartition(browser_context()));
786 partition->OverrideQuotaManagerForTesting(
787 GetMockManager());
788 base::MessageLoop::current()->PostTask(
789 FROM_HERE, base::Bind(&ClearQuotaDataWithOriginMatcher,
790 partition, GURL(),
791 base::Bind(&DoesOriginMatchUnprotected),
792 base::Time(), &run_loop));
793 run_loop.Run();
795 // Check that devtools data isn't removed.
796 EXPECT_TRUE(GetMockManager()->OriginHasData(kOriginDevTools, kTemporary,
797 kClientFile));
798 EXPECT_TRUE(GetMockManager()->OriginHasData(kOriginDevTools, kPersistent,
799 kClientFile));
802 TEST_F(StoragePartitionImplTest, RemoveCookieForever) {
803 RemoveCookieTester tester(browser_context());
805 tester.AddCookie();
806 ASSERT_TRUE(tester.ContainsCookie());
808 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
809 BrowserContext::GetDefaultStoragePartition(browser_context()));
810 partition->SetURLRequestContext(browser_context()->GetRequestContext());
812 base::RunLoop run_loop;
813 base::MessageLoop::current()->PostTask(
814 FROM_HERE, base::Bind(&ClearCookies,
815 partition, base::Time(), base::Time::Max(),
816 &run_loop));
817 run_loop.Run();
819 EXPECT_FALSE(tester.ContainsCookie());
822 TEST_F(StoragePartitionImplTest, RemoveCookieLastHour) {
823 RemoveCookieTester tester(browser_context());
825 tester.AddCookie();
826 ASSERT_TRUE(tester.ContainsCookie());
828 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
829 BrowserContext::GetDefaultStoragePartition(browser_context()));
830 base::Time an_hour_ago = base::Time::Now() - base::TimeDelta::FromHours(1);
831 partition->SetURLRequestContext(browser_context()->GetRequestContext());
833 base::RunLoop run_loop;
834 base::MessageLoop::current()->PostTask(
835 FROM_HERE, base::Bind(&ClearCookies,
836 partition, an_hour_ago, base::Time::Max(),
837 &run_loop));
838 run_loop.Run();
840 EXPECT_FALSE(tester.ContainsCookie());
843 TEST_F(StoragePartitionImplTest, RemoveUnprotectedLocalStorageForever) {
844 // Protect kOrigin1.
845 scoped_refptr<quota::MockSpecialStoragePolicy> mock_policy =
846 new quota::MockSpecialStoragePolicy;
847 mock_policy->AddProtected(kOrigin1.GetOrigin());
849 RemoveLocalStorageTester tester(browser_context());
851 tester.AddDOMStorageTestData();
852 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin1));
853 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin2));
854 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin3));
856 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
857 BrowserContext::GetDefaultStoragePartition(browser_context()));
858 partition->OverrideSpecialStoragePolicyForTesting(mock_policy);
860 base::RunLoop run_loop;
861 base::MessageLoop::current()->PostTask(
862 FROM_HERE,
863 base::Bind(&ClearStuff,
864 StoragePartitionImpl::REMOVE_DATA_MASK_LOCAL_STORAGE,
865 partition, base::Time(), base::Time::Max(),
866 base::Bind(&DoesOriginMatchForUnprotectedWeb),
867 &run_loop));
868 run_loop.Run();
870 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin1));
871 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin2));
872 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin3));
875 TEST_F(StoragePartitionImplTest, RemoveProtectedLocalStorageForever) {
876 // Protect kOrigin1.
877 scoped_refptr<quota::MockSpecialStoragePolicy> mock_policy =
878 new quota::MockSpecialStoragePolicy;
879 mock_policy->AddProtected(kOrigin1.GetOrigin());
881 RemoveLocalStorageTester tester(browser_context());
883 tester.AddDOMStorageTestData();
884 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin1));
885 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin2));
886 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin3));
888 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
889 BrowserContext::GetDefaultStoragePartition(browser_context()));
890 partition->OverrideSpecialStoragePolicyForTesting(mock_policy);
892 base::RunLoop run_loop;
893 base::MessageLoop::current()->PostTask(
894 FROM_HERE,
895 base::Bind(&ClearStuff,
896 StoragePartitionImpl::REMOVE_DATA_MASK_LOCAL_STORAGE,
897 partition, base::Time(), base::Time::Max(),
898 base::Bind(&DoesOriginMatchForBothProtectedAndUnprotectedWeb),
899 &run_loop));
900 run_loop.Run();
902 // Even if kOrigin1 is protected, it will be deleted since we specify
903 // ClearData to delete protected data.
904 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin1));
905 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin2));
906 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin3));
909 TEST_F(StoragePartitionImplTest, RemoveLocalStorageForLastWeek) {
910 RemoveLocalStorageTester tester(browser_context());
912 tester.AddDOMStorageTestData();
913 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin1));
914 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin2));
915 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin3));
917 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
918 BrowserContext::GetDefaultStoragePartition(browser_context()));
919 base::Time a_week_ago = base::Time::Now() - base::TimeDelta::FromDays(7);
921 base::RunLoop run_loop;
922 base::MessageLoop::current()->PostTask(
923 FROM_HERE,
924 base::Bind(&ClearStuff,
925 StoragePartitionImpl::REMOVE_DATA_MASK_LOCAL_STORAGE,
926 partition, a_week_ago, base::Time::Max(),
927 base::Bind(&DoesOriginMatchForBothProtectedAndUnprotectedWeb),
928 &run_loop));
929 run_loop.Run();
931 // kOrigin1 and kOrigin2 do not have age more than a week.
932 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin1));
933 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin2));
934 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin3));
937 } // namespace content