Chromium Code Reviews| Index: chrome/browser/browsing_data/site_data_counting_helper_browsertest.cc |
| diff --git a/chrome/browser/browsing_data/site_data_counting_helper_browsertest.cc b/chrome/browser/browsing_data/site_data_counting_helper_browsertest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3605069d892831ac61d7bd6b65a52e51dd09d09f |
| --- /dev/null |
| +++ b/chrome/browser/browsing_data/site_data_counting_helper_browsertest.cc |
| @@ -0,0 +1,198 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <algorithm> |
| +#include <memory> |
| +#include <set> |
| +#include <string> |
| + |
| +#include "base/run_loop.h" |
| +#include "chrome/browser/browsing_data/site_data_counting_helper.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/ui/browser.h" |
| +#include "chrome/test/base/in_process_browser_test.h" |
| +#include "content/public/browser/browser_context.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/storage_partition.h" |
| +#include "net/cookies/cookie_store.h" |
| +#include "net/url_request/url_request_context.h" |
| +#include "net/url_request/url_request_context_getter.h" |
| + |
| +using content::BrowserThread; |
| + |
| +class SiteDataCountingHelperBrowserTest : public InProcessBrowserTest { |
|
msramek
2017/01/10 13:46:32
Optional: Since StoragePartitionImpl seems to test
dullweber
2017/01/10 17:14:38
I converted it to a unittest
|
| + public: |
| + const int64_t kTimeoutMs = 10; |
| + |
| + void SetUpOnMainThread() override { |
| + tasks_ = 0; |
| + cookie_callback_ = |
| + base::Bind(&SiteDataCountingHelperBrowserTest::CookieCallback, |
| + base::Unretained(this)); |
| + } |
| + |
| + void CookieCallback(int count) { |
| + // Negative values represent an unexpected error. |
| + DCHECK(count >= 0); |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + last_count_ = count; |
| + |
| + if (run_loop_) |
| + run_loop_->Quit(); |
| + } |
| + |
| + void DoneOnIOThread(bool success) { |
| + DCHECK(success); |
| + if (--tasks_ > 0) |
| + return; |
| + BrowserThread::PostTask( |
| + BrowserThread::UI, FROM_HERE, |
| + base::Bind(&SiteDataCountingHelperBrowserTest::DoneCallback, |
| + base::Unretained(this))); |
| + } |
| + |
| + void DoneCallback() { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + if (run_loop_) |
| + run_loop_->Quit(); |
| + } |
| + |
| + void WaitForTasksOnIOThread() { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + run_loop_.reset(new base::RunLoop()); |
| + run_loop_->Run(); |
| + } |
| + |
| + void CreateCookies(base::Time creation_time, |
| + const std::vector<std::string>& urls) { |
| + content::StoragePartition* partition = |
| + content::BrowserContext::GetDefaultStoragePartition( |
| + browser()->profile()); |
| + net::URLRequestContextGetter* rq_context = |
| + partition->GetURLRequestContext(); |
| + BrowserThread::PostTask( |
| + BrowserThread::IO, FROM_HERE, |
| + base::Bind(&SiteDataCountingHelperBrowserTest::CreateCookiesOnIOThread, |
| + base::Unretained(this), make_scoped_refptr(rq_context), |
| + creation_time, urls)); |
| + } |
| + |
| + void CreateLocalStorage(base::Time creation_time, |
| + const std::vector<std::string>& storage_origins) { |
| + // Note: This test depends on details of how the dom_storage library |
| + // stores data in the host file system. |
| + base::FilePath storage_path = |
| + browser()->profile()->GetPath().AppendASCII("Local Storage"); |
| + base::CreateDirectory(storage_path); |
| + |
| + // Write some files. |
| + for (const std::string& origin : storage_origins) { |
| + base::WriteFile(storage_path.Append(origin), NULL, 0); |
| + base::TouchFile(storage_path.Append(origin), creation_time, |
| + creation_time); |
| + } |
| + } |
| + |
| + void CreateCookiesOnIOThread( |
| + const scoped_refptr<net::URLRequestContextGetter>& rq_context, |
| + base::Time creation_time, |
| + std::vector<std::string> urls) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + |
| + net::CookieStore* cookie_store = |
| + rq_context->GetURLRequestContext()->cookie_store(); |
| + |
| + tasks_ = urls.size(); |
| + int i = 0; |
| + for (const std::string& url_string : urls) { |
| + GURL url(url_string); |
| + // Cookies need a unique creation time. |
| + base::Time time = creation_time + base::TimeDelta::FromMilliseconds(i++); |
| + cookie_store->SetCookieWithDetailsAsync( |
| + url, "name", "A=1", url.host(), url.path(), time, base::Time(), time, |
| + true, false, net::CookieSameSite::DEFAULT_MODE, false, |
| + net::COOKIE_PRIORITY_DEFAULT, |
| + base::Bind(&SiteDataCountingHelperBrowserTest::DoneOnIOThread, |
| + base::Unretained(this))); |
| + } |
| + } |
| + |
| + void CountEntries(base::Time begin_time) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + last_count_ = -1; |
| + auto helper = new SiteDataCountingHelper(browser()->profile(), begin_time, |
| + cookie_callback_); |
| + helper->CountAndDestroySelfWhenFinished(); |
| + } |
| + |
| + int64_t GetResult() { |
| + DCHECK_GE(last_count_, 0); |
| + return last_count_; |
| + } |
| + |
| + private: |
| + base::Callback<void(int)> cookie_callback_; |
| + std::unique_ptr<base::RunLoop> run_loop_; |
| + |
| + int tasks_; |
| + int64_t last_count_; |
| +}; |
| + |
| +IN_PROC_BROWSER_TEST_F(SiteDataCountingHelperBrowserTest, CheckEmptyResult) { |
| + CountEntries(base::Time()); |
| + WaitForTasksOnIOThread(); |
| + |
| + DCHECK_EQ(0, GetResult()); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(SiteDataCountingHelperBrowserTest, CountCookies) { |
| + base::Time now = base::Time::Now(); |
| + base::Time last_hour = now - base::TimeDelta::FromHours(1); |
| + base::Time yesterday = now - base::TimeDelta::FromDays(1); |
| + |
| + CreateCookies(last_hour, {"https://example.com"}); |
| + WaitForTasksOnIOThread(); |
| + |
| + CreateCookies(yesterday, {"https://google.com", "https://bing.com"}); |
| + WaitForTasksOnIOThread(); |
| + |
| + CountEntries(base::Time()); |
| + WaitForTasksOnIOThread(); |
| + DCHECK_EQ(3, GetResult()); |
| + |
| + CountEntries(yesterday); |
| + WaitForTasksOnIOThread(); |
| + DCHECK_EQ(3, GetResult()); |
| + |
| + CountEntries(last_hour); |
| + WaitForTasksOnIOThread(); |
| + DCHECK_EQ(1, GetResult()); |
| + |
| + CountEntries(now); |
| + WaitForTasksOnIOThread(); |
| + DCHECK_EQ(0, GetResult()); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(SiteDataCountingHelperBrowserTest, LocalStorage) { |
| + base::Time now = base::Time::Now(); |
| + CreateLocalStorage(now, {"https_example.com_443.localstorage", |
| + "https_bing.com_443.localstorage"}); |
| + |
| + CountEntries(base::Time()); |
| + WaitForTasksOnIOThread(); |
| + DCHECK_EQ(2, GetResult()); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(SiteDataCountingHelperBrowserTest, |
| + CookiesAndLocalStorage) { |
| + base::Time now = base::Time::Now(); |
| + CreateCookies(now, {"https://example.com", "https://google.com"}); |
| + CreateLocalStorage(now, {"https_example.com_443.localstorage", |
| + "https_bing.com_443.localstorage"}); |
| + WaitForTasksOnIOThread(); |
| + |
| + CountEntries(base::Time()); |
| + WaitForTasksOnIOThread(); |
| + DCHECK_EQ(3, GetResult()); |
| +} |