Chromium Code Reviews| Index: chrome/browser/browsing_data/cookie_counting_helper_browsertest.cc |
| diff --git a/chrome/browser/browsing_data/cookie_counting_helper_browsertest.cc b/chrome/browser/browsing_data/cookie_counting_helper_browsertest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..929c270d83107281a4e3f81091d4ba9de4b01b22 |
| --- /dev/null |
| +++ b/chrome/browser/browsing_data/cookie_counting_helper_browsertest.cc |
| @@ -0,0 +1,161 @@ |
| +// Copyright (c) 2015 The Chromium Authors. All rights reserved. |
|
msramek
2017/01/09 12:54:44
Ditto.
dullweber
2017/01/09 16:05:46
Done.
|
| +// 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/cookie_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 CookieCountingHelperBrowserTest : public InProcessBrowserTest { |
| + public: |
| + const int64_t kTimeoutMs = 10; |
| + |
| + void SetUpOnMainThread() override { |
| + tasks_ = 0; |
| + cookie_callback_ = |
| + base::Bind(&CookieCountingHelperBrowserTest::CookieCallback, |
| + base::Unretained(this)); |
| + } |
| + |
| + void TearDownOnMainThread() override {} |
| + |
| + 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); |
| + LOG(ERROR) << "done: " << success; |
| + if (--tasks_ > 0) |
| + return; |
| + BrowserThread::PostTask( |
| + BrowserThread::UI, FROM_HERE, |
| + base::Bind(&CookieCountingHelperBrowserTest::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, std::vector<GURL> urls) { |
| + content::StoragePartition* partition = |
| + content::BrowserContext::GetDefaultStoragePartition( |
| + browser()->profile()); |
| + net::URLRequestContextGetter* rq_context = |
| + partition->GetURLRequestContext(); |
| + BrowserThread::PostTask( |
| + BrowserThread::IO, FROM_HERE, |
| + base::Bind(&CookieCountingHelperBrowserTest::CreateCookiesOnIOThread, |
| + base::Unretained(this), make_scoped_refptr(rq_context), |
| + creation_time, urls)); |
| + } |
| + |
| + void CreateCookiesOnIOThread( |
| + const scoped_refptr<net::URLRequestContextGetter>& rq_context, |
| + base::Time creation_time, |
| + std::vector<GURL> urls) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + |
| + net::CookieStore* cookie_store = |
| + rq_context->GetURLRequestContext()->cookie_store(); |
| + |
| + tasks_ = urls.size(); |
| + int i = 0; |
| + for (const GURL& url : urls) { |
| + // 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(&CookieCountingHelperBrowserTest::DoneOnIOThread, |
| + base::Unretained(this))); |
| + } |
| + } |
| + |
| + void CountEntries(base::Time begin_time) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + last_count_ = -1; |
| + auto helper = new CookieCountingHelper(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_; |
| + |
| + volatile int tasks_; |
| + int64_t last_count_; |
| +}; |
| + |
| +IN_PROC_BROWSER_TEST_F(CookieCountingHelperBrowserTest, CheckEmptyResult) { |
| + CountEntries(base::Time()); |
| + WaitForTasksOnIOThread(); |
| + |
| + DCHECK_EQ(0, GetResult()); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(CookieCountingHelperBrowserTest, 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, {GURL("https://example.com")}); |
| + WaitForTasksOnIOThread(); |
| + |
| + CreateCookies(yesterday, |
| + {GURL("https://google.com"), GURL("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()); |
| +} |